-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlfp_simulate.py
172 lines (146 loc) · 5.78 KB
/
lfp_simulate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import streamlit as st
import plotly.express as px
from datetime import datetime
from rolling_and_plot import normalize, rolling_split, validate
from ecm_model import *
st.title("Lithium-Ion Cell Simulator Using the 2nd Order Equivalent Circuit Model")
st.markdown(
"The purpose of this simulator is to generate SOC, Voltage, and Current data for lithium-ion cells.")
st.markdown("Open Sidebar for Static ECM Parameterization.")
tab = st.tabs(["Simulation","Graphs","LSTM Predictions"])
sidebar = st.sidebar
with sidebar: # the sidebar of the GUI
r_int = st.number_input(label="Internal Resistance mΩ", value = 1.3) / 1000
# RC-pairs for 2nd order ECM
rc = st.columns(2)
with rc[0]:
r_1 = st.number_input(
label="Resistance of R1 mΩ", value=1.20) / 1000
c_1 = st.number_input(
label="Capacitance of C1 F", value=12000)
with rc[1]:
r_2 = st.number_input(
label="Resistance of R2 mΩ", value=1.0) / 1000
c_2 = st.number_input(
label="Capacitance of C2 F", value=120000)
if not r_2:
assert r_1 != 0, "Change 1st RC pair first"
if not c_2:
assert c_1 != 0, "Change 1st RC pair first"
capacity = st.number_input(label="Battery Capacity inAh", value=20.0)
ocv = st.text_input(
label="11 OCV values from 100 to 0 SOC split by commas:")
"Example OCV: 3.557, 3.394, ..., 3.222, 3.174, 2.750"
cur = st.columns(2)
with cur[0]:
min_I = st.number_input(
label = "Most (-) current (A)",
value = -10.0, max_value = 0.0)
with cur[1]:
max_I = st.number_input(
label = "Most (+) current (A)",
value = 10.0, min_value = 0.0)
start = st.checkbox(label="Run Simulation")
#------------------------------
#converting dataframes to csv/parquet
@st.cache_data
def convert_df(df, format = "csv"):
if format == "parquet":
return df.to_parquet()
elif format == "csv":
return df.to_csv().encode('utf-8')
#------------------------------
if start:
if len(ocv) > 0:
ocv = ocv.split(",")
assert len(ocv) == 11, "Need 11 OCV values"
for i in ocv:
i = i.strip()
assert i.replace(".","").isnumeric(), "Need numerical values"
ocv = [float(val) for val in ocv]
assert abs(min_I) < capacity / 1.2, "Current cannot exceed C/1.2"
assert abs(max_I) < capacity / 1.2, "Current cannot exceed C/1.2"
current = np.array((max_I - min_I) * np.random.random_sample(24) + min_I).round(2)
with tab[0]:
"Progress Bar:"
progress = st.progress(0)
#sim
try:
del df_sim
except NameError:
df_sim = simulate(capacity, current, progress, ocv = ocv, r_int = r_int,
r_1 = r_1, c_1 = c_1, r_2 = r_2, c_2 = c_2
)
with tab[0]:
"Check the other Tabs for more!"
st.dataframe(data = df_sim)
with sidebar:
file_date = datetime.today().strftime("%Y_%m_%d")
file_name = st.text_input(label = "File name for download", value = f"simulated_data_{file_date}")
st.write("Download Data:")
download_columns = st.columns(2)
with download_columns[0]:
st.download_button(
"CSV",
convert_df(df_sim, format = "csv"),
file_name,
key='csv_data'
)
with download_columns[1]:
st.download_button(
"Parquet",
convert_df(df_sim, format = "parquet"),
file_name,
key='parquet_data'
)
" "
" "
one = px.line(data_frame = df_sim, x= "time", y = "soc", title = "SOC v Time")
one["data"][0]["line"]["color"] = "red"
two = px.line(data_frame = df_sim, x= "time", y = "voltage", title = "Voltage v Time")
two["data"][0]["line"]["color"] = "pink"
three = px.line(data_frame = df_sim, x = "time", y = "current", title = "Current v Time")
three["data"][0]["line"]["color"] = "lightgreen"
with tab[1]:
st.plotly_chart(one)
st.plotly_chart(two)
st.plotly_chart(three)
#plot
# fig, ax = plt.subplots(3, sharex=True, figsize = (12,9))
# ax[0].set_ylabel("SOC (%)", fontsize = 12 )
# ax[0].set_xlabel("Time (sec)", fontsize = 12)
# ax[0].plot(df_sim["time"].values, df_sim["soc"].values, "r-")
# ax[0].set_title("SOC vs Time")
# ax[0].set_ylim([0,105])
# ax[0].set_yticks(list(range(0,105,20)))
# ax[1].set_ylabel("Voltage (V)", fontsize = 12)
# ax[1].set_xlabel("Time (sec)", fontsize = 12)
# ax[1].plot(df_sim["time"].values, df_sim["voltage"].values, "b-")
# ax[1].set_title("Voltage vs Time")
# ax[1].set_ylim([1.9,4])
# ax[1].set_yticks(np.arange(2.0,4.1,0.4))
# ax[2].set_ylabel("Current (A)", fontsize = 12 )
# ax[2].set_xlabel("Time (sec)", fontsize = 12)
# ax[2].plot(df_sim["time"].values, df_sim["current"].values, "g-")
# ax[2].set_title("Current vs Time")
# ax[2].set_yticks(list(range(int(min_I),
# int(max_I + 4),
# 4)))
# fig.tight_layout()
# st.pyplot(fig)
#-----------------------------------
#LSTM
with tab[2]:
with st.container():
"Prediction Progress"
prediction_bar = st.progress(0)
lstm_cols = st.columns(2)
df_sim_norm = normalize(df_sim, capacity)
x_set, y_set = rolling_split(df_sim_norm)
set_dataloader = [set for set in zip(x_set,y_set)]
model = load_model()
with lstm_cols[0]:
visualize, fig = validate(model, set_dataloader, prediction_bar)
st.dataframe(visualize)
with lstm_cols[1]:
st.plotly_chart(fig)