dimanche 17 mai 2020

How do I create a Python script to simulate a lithium ion battery?

I am trying to create a python script that simulates the charging and discharging of a simple lithium ion battery. I want to send a list of 1’s and 0’s that indicate charging (1) and no charge (0). The function will iterate through the list (data) and use the corresponding equations to plot the correct output. This is a continuation of another question I asked on the Electrical Engineering Stack Exchange (https://electronics.stackexchange.com/questions/495348/are-my-equations-correct-for-a-simple-lithium-ion-battery-model-in-python), which has more details on what I am trying to do, and also shows the plots I am trying to achieve. A lot of the code is based off a RC circuit from, http://firsttimeprogrammer.blogspot.com/2015/07/electric-circuits-101-rc-and-rl-circuits.html.

I was thinking something like (pseudo code):

import numpy as np
import matplotlib.pyplot as plt

data = [0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0]

def lith_battery(data):
    q_plot = []
    i_plot = []
    vc_plot = []
    #capacitor (battery)
    c = 100 * 10**(-6) #farad
    #voltage source
    vs = 5 #volts
    #resistor
    r = 2000 #ohms
    #Max voltage
    v_max = 4.8 #volts 

    #time
    t = np.linspace(0,1,1000)

    for charge in data:
        #charging
        if charge  == 1:
            q = c*vs*(1-np.exp((-1/(r*c))*t)) #charge
            i = (vs/r)*np.exp((-1/(r*c))*t) #battery current
            vc = vs*(1-np.exp((-1/(r*c))*t)) #battery voltage
            q_plot.append(q) 
            i_plot.append(i)           
            vc_plot.append(vc)
            #If voltage equals v_max stop charging
        #discharging
        else:
            q = c*vs*(np.exp((-1/(r*c))*t)) #discharge
            i = (vs/r)*np.exp((-1/(r*c))*t) #battery current
            v = vs*(np.exp((-1/(r*c))*t)) #battery voltage
            q_plot.append(q) 
            i_plot.append(i)           
            vc_plot.append(vc)

    return q_plot, i_plot, vc_plot, t

q, i, vc, t = lith_battery(data)  

plt.plot([0,t[-1]],[c*vs,c*vs],label='Charge peak')
plt.plot(t,q,label='Charge of the capacitor (C)')
plt.plot(t,i,label='Current (A)')
plt.plot(t,vc,label='Voltage across capacitor')

print('Tau',1/(r*c))
print('Peak current (A)',vs/r)

plt.xlabel('Time (s)')
plt.title('Battery')
plt.legend()
plt.show()

Questions:

  1. I need to somehow connect time and data, so does every index in data need to have a corresponding entry in my time list t?
  2. I would also like to add a part that says when the plot is plotting, if the voltage gets to v_max, stop charging and hold at that voltage until power is consumed (i.e discharge).
  3. Would a while loop be better suited for what I am trying to do as opposed to a for loop?

Any help, suggestions, or a working version of this code, would be greatly appreciated. If more clarification on my question is needed, just let me know.

Aucun commentaire:

Enregistrer un commentaire