Skip to content

Commit b67a457

Browse files
#1100 working on examples
1 parent 4c6bf59 commit b67a457

File tree

6 files changed

+1718
-1150
lines changed

6 files changed

+1718
-1150
lines changed

examples/notebooks/DFN-sensitivity.ipynb

+93-111
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
},
4343
{
4444
"cell_type": "code",
45-
"execution_count": 2,
45+
"execution_count": 14,
4646
"metadata": {},
4747
"outputs": [],
4848
"source": [
49-
"model = pybamm.lithium_ion.DFN()"
49+
"model = pybamm.lithium_ion.SPMe()"
5050
]
5151
},
5252
{
@@ -64,56 +64,33 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 62,
68-
"metadata": {},
69-
"outputs": [
70-
{
71-
"data": {
72-
"text/plain": [
73-
"7.900401128126567"
74-
]
75-
},
76-
"execution_count": 62,
77-
"metadata": {},
78-
"output_type": "execute_result"
79-
}
80-
],
81-
"source": [
82-
"ce_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
83-
"csn_ref = param[\"Maximum concentration in negative electrode [mol.m-3]\"]\n",
84-
"T_ref = param[\"Reference temperature [K]\"]\n",
85-
"param.evaluate(param[\"Negative electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csn_ref, T_ref))"
86-
]
87-
},
88-
{
89-
"cell_type": "code",
90-
"execution_count": 122,
67+
"execution_count": 15,
9168
"metadata": {},
9269
"outputs": [],
9370
"source": [
9471
"param = model.default_parameter_values\n",
9572
"# Get reference values for evaluating functions\n",
96-
"c_e_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
73+
"ce_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
9774
"csn_ref = param[\"Maximum concentration in negative electrode [mol.m-3]\"]\n",
9875
"csp_ref = param[\"Maximum concentration in positive electrode [mol.m-3]\"]\n",
9976
"T_ref = param[\"Reference temperature [K]\"]\n",
10077
"# Evaluate functions at reference values\n",
10178
"Dsn_ref = param[\"Negative electrode diffusivity [m2.s-1]\"](0.5, T_ref).evaluate()\n",
10279
"Dsp_ref = param[\"Positive electrode diffusivity [m2.s-1]\"](0.5, T_ref).evaluate()\n",
103-
"De_ref = param[\"Electrolyte diffusivity [m2.s-1]\"](c_e_ref, T_ref).evaluate()\n",
104-
"kappae_ref = param[\"Electrolyte conductivity [S.m-1]\"](c_e_ref, T_ref).evaluate()\n",
80+
"De_ref = param[\"Electrolyte diffusivity [m2.s-1]\"](ce_ref, T_ref).evaluate()\n",
81+
"kappae_ref = param[\"Electrolyte conductivity [S.m-1]\"](ce_ref, T_ref).evaluate()\n",
10582
"j0n_ref = param.evaluate(param[\"Negative electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csn_ref, T_ref))\n",
10683
"j0p_ref = param.evaluate(param[\"Positive electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csp_ref, T_ref))"
10784
]
10885
},
10986
{
11087
"cell_type": "code",
111-
"execution_count": 123,
88+
"execution_count": 16,
11289
"metadata": {},
11390
"outputs": [],
11491
"source": [
11592
"param[\"Negative electrode diffusivity [m2.s-1]\"] = Dsn_ref * pybamm.InputParameter(\"Dsn\")\n",
116-
"# param[\"Positive electrode diffusivity [m2.s-1]\"] = Dsp_ref * pybamm.InputParameter(\"Dsp\")\n",
93+
"param[\"Positive electrode diffusivity [m2.s-1]\"] = Dsp_ref * pybamm.InputParameter(\"Dsp\")\n",
11794
"# param[\"Electrolyte diffusivity [m2.s-1]\"] = De_ref * pybamm.InputParameter(\"D_e\")\n",
11895
"# param[\"Electrolyte conductivity [S.m-1]\"] = kappae_ref * pybamm.InputParameter(\"kappa_e\")\n",
11996
"# param[\"Negative electrode exchange-current density [A.m-2]\"] = j0n_ref * pybamm.InputParameter(\"j0n\")\n",
@@ -129,13 +106,33 @@
129106
},
130107
{
131108
"cell_type": "code",
132-
"execution_count": 124,
109+
"execution_count": 19,
133110
"metadata": {},
134111
"outputs": [],
135112
"source": [
136-
"solver = pybamm.CasadiSolver(extra_options_setup={\"ad_weight\": 0})\n",
113+
"solver = pybamm.CasadiSolver(mode=\"fast\", solve_sensitivity_equations=True)\n",
137114
"sim = pybamm.Simulation(model, parameter_values=param, solver=solver)\n",
138-
"solution = sim.solve(t_eval=np.linspace(0,3600,5))"
115+
"solution = sim.solve(t_eval=np.linspace(0,3600), inputs={\"Dsn\": 1, \"Dsp\": 1})"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": 20,
121+
"metadata": {},
122+
"outputs": [
123+
{
124+
"data": {
125+
"text/plain": [
126+
"1.7811190900000042"
127+
]
128+
},
129+
"execution_count": 20,
130+
"metadata": {},
131+
"output_type": "execute_result"
132+
}
133+
],
134+
"source": [
135+
"solution.solve_time"
139136
]
140137
},
141138
{
@@ -147,16 +144,47 @@
147144
},
148145
{
149146
"cell_type": "code",
150-
"execution_count": 125,
147+
"execution_count": 17,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"solver = pybamm.CasadiSolver(mode=\"fast\")\n",
152+
"sim = pybamm.Simulation(model, parameter_values=param, solver=solver)\n",
153+
"solution = sim.solve(t_eval=np.linspace(0,3600), inputs={\"Dsn\": 1, \"Dsp\": 1})"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 18,
159+
"metadata": {},
160+
"outputs": [
161+
{
162+
"data": {
163+
"text/plain": [
164+
"0.005656635999997661"
165+
]
166+
},
167+
"execution_count": 18,
168+
"metadata": {},
169+
"output_type": "execute_result"
170+
}
171+
],
172+
"source": [
173+
"solution.solve_time"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 11,
151179
"metadata": {},
152180
"outputs": [
153181
{
154182
"data": {
155183
"text/plain": [
156-
"<pybamm.solvers.processed_symbolic_variable.ProcessedSymbolicVariable at 0x1401c88d0>"
184+
"<pybamm.solvers.processed_variable.ProcessedVariable at 0x13da41210>"
157185
]
158186
},
159-
"execution_count": 125,
187+
"execution_count": 11,
160188
"metadata": {},
161189
"output_type": "execute_result"
162190
}
@@ -175,28 +203,30 @@
175203
},
176204
{
177205
"cell_type": "code",
178-
"execution_count": 128,
206+
"execution_count": 12,
179207
"metadata": {},
180208
"outputs": [
181-
{
182-
"name": "stdout",
183-
"output_type": "stream",
184-
"text": [
185-
"CPU times: user 144 ms, sys: 2.01 ms, total: 146 ms\n",
186-
"Wall time: 146 ms\n"
187-
]
188-
},
189209
{
190210
"data": {
191211
"text/plain": [
192-
"DM([[3.77171, 3.67082, 3.61315, 3.58406, 3.16798]])"
212+
"{'all': DM([0, 0.000305216, 0.000323451, 0.000307878, 0.000281315, 0.000252542, 0.000226269, 0.00020523, 0.000191031, 0.000184763, 0.000187613, 0.000201625, 0.000230913, 0.000283916, 0.000377805, 0.000547086, 0.000859691, 0.00144399, 0.00252183, 0.00440237, 0.00728527, 0.0106768, 0.0129031, 0.0123404, 0.00953625, 0.00642767, 0.00416935, 0.00284452, 0.00214823, 0.00179002, 0.00157891, 0.00140318, 0.00120162, 0.000947612, 0.000645371, 0.0003332, 8.76979e-05, 2.15342e-05, 0.000267893, 0.000949333, 0.00213811, 0.00382395, 0.00590563, 0.00821168, 0.0105401, 0.0127, 0.0145411, 0.0159704, 0.0169657, 0.017614]),\n",
213+
" 'Dsn': DM([0, 0.000305216, 0.000323451, 0.000307878, 0.000281315, 0.000252542, 0.000226269, 0.00020523, 0.000191031, 0.000184763, 0.000187613, 0.000201625, 0.000230913, 0.000283916, 0.000377805, 0.000547086, 0.000859691, 0.00144399, 0.00252183, 0.00440237, 0.00728527, 0.0106768, 0.0129031, 0.0123404, 0.00953625, 0.00642767, 0.00416935, 0.00284452, 0.00214823, 0.00179002, 0.00157891, 0.00140318, 0.00120162, 0.000947612, 0.000645371, 0.0003332, 8.76979e-05, 2.15342e-05, 0.000267893, 0.000949333, 0.00213811, 0.00382395, 0.00590563, 0.00821168, 0.0105401, 0.0127, 0.0145411, 0.0159704, 0.0169657, 0.017614])}"
193214
]
194215
},
195-
"execution_count": 128,
216+
"execution_count": 12,
196217
"metadata": {},
197218
"output_type": "execute_result"
198219
}
199220
],
221+
"source": [
222+
"V.sensitivity"
223+
]
224+
},
225+
{
226+
"cell_type": "code",
227+
"execution_count": null,
228+
"metadata": {},
229+
"outputs": [],
200230
"source": [
201231
"%%time\n",
202232
"V.value({\"Dsn\": 1, \"Dsp\": 1, \"D_e\": 1, \"kappa_e\": 1, \"j0n\": 1, \"j0p\": 1})"
@@ -211,63 +241,37 @@
211241
},
212242
{
213243
"cell_type": "code",
214-
"execution_count": 129,
244+
"execution_count": null,
215245
"metadata": {},
216-
"outputs": [
217-
{
218-
"name": "stdout",
219-
"output_type": "stream",
220-
"text": [
221-
"CPU times: user 672 ms, sys: 7.61 ms, total: 679 ms\n",
222-
"Wall time: 675 ms\n"
223-
]
224-
}
225-
],
246+
"outputs": [],
226247
"source": [
227248
"%%time\n",
228249
"sens = V.sensitivity({\"Dsn\": 1, \"Dsp\": 1, \"D_e\": 1, \"kappa_e\": 1, \"j0n\": 1, \"j0p\": 1})"
229250
]
230251
},
231252
{
232253
"cell_type": "code",
233-
"execution_count": 109,
254+
"execution_count": null,
234255
"metadata": {},
235-
"outputs": [
236-
{
237-
"data": {
238-
"text/plain": [
239-
"DM(\n",
240-
"[[00, 00], \n",
241-
" [0.00144585, 0.00372077], \n",
242-
" [0.00652415, 0.00146917], \n",
243-
" [0.00246126, 0.00137044], \n",
244-
" [0.0174035, 0.223563]])"
245-
]
246-
},
247-
"execution_count": 109,
248-
"metadata": {},
249-
"output_type": "execute_result"
250-
}
251-
],
256+
"outputs": [],
252257
"source": [
253258
"sens"
254259
]
255260
},
256261
{
257262
"cell_type": "code",
258-
"execution_count": 84,
263+
"execution_count": 13,
259264
"metadata": {},
260265
"outputs": [
261266
{
262-
"name": "stdout",
263-
"output_type": "stream",
264-
"text": [
265-
"[[0, 0.000422464, 0.000444994, 0.000428074, 0.000407487, 0.000399877, 0.000428391, 0.000542233, 0.000778531, 0.00103886, 0.00122177, 0.0013259, 0.00137903, 0.0014072, 0.00143188, 0.00147267, 0.00155152, 0.00170014, 0.00197684, 0.002514, 0.00366832, 0.00623007, 0.00951514, 0.0100351, 0.00790636, 0.00546389, 0.00381094, 0.00281181, 0.0021213, 0.00151904, 0.00102273, 0.00104159, 0.00145708, 0.00182547, 0.00207236, 0.00222589, 0.00231668, 0.0023674, 0.00239664, 0.00242538, 0.00249296, 0.00270719, 0.00340515, 0.00526309, 0.0081467, 0.0109712, 0.0132931, 0.015046, 0.0162617, 0.0170824]]\n",
266-
"[[0.00485232, 0.00523816, 0.0052792, 0.0052831, 0.00527848, 0.00525259, 0.00517244, 0.00495439, 0.00451041, 0.00397247, 0.00352681, 0.00321071, 0.00300636, 0.00289322, 0.0028568, 0.00288818, 0.00298289, 0.00314032, 0.0033631, 0.00365425, 0.00399468, 0.00423495, 0.00415848, 0.0040753, 0.00417632, 0.00439649, 0.00459593, 0.0047219, 0.00481469, 0.00494027, 0.00510006, 0.00503174, 0.00469119, 0.00435765, 0.00410709, 0.00393143, 0.00381349, 0.00374063, 0.00370672, 0.00371226, 0.0037682, 0.00390221, 0.00414353, 0.00439765, 0.00447031, 0.00444454, 0.00441415, 0.00439328, 0.00437972, 0.00437309]]\n",
267-
"[[0, 0.00526514, 0.00652778, 0.00677179, 0.00659185, 0.00625442, 0.00586091, 0.00544997, 0.00503892, 0.00463729, 0.00425102, 0.00388392, 0.00353832, 0.00321543, 0.00291568, 0.00263888, 0.00238442, 0.00215139, 0.0019387, 0.0017451, 0.00156931, 0.00141004, 0.00126599, 0.00113594, 0.00101868, 0.000913098, 0.000818128, 0.000732787, 0.000656173, 0.000587471, 0.000525967, 0.000471096, 0.000422519, 0.000380349, 0.000345679, 0.000321775, 0.000316733, 0.000348904, 0.000455642, 0.000698224, 0.00113785, 0.00178227, 0.00261286, 0.00374895, 0.00570581, 0.0101298, 0.0222322, 0.0525921, 0.111765, 0.220066]]\n",
268-
"[[0.043932, 0.043935, 0.0439292, 0.0439253, 0.0439234, 0.0439229, 0.0439232, 0.0439241, 0.0439253, 0.0439268, 0.0439285, 0.0439302, 0.043932, 0.0439339, 0.0439358, 0.0439377, 0.0439396, 0.0439414, 0.0439433, 0.0439451, 0.0439469, 0.0439487, 0.0439505, 0.0439521, 0.0439537, 0.0439551, 0.0439564, 0.0439576, 0.0439587, 0.0439598, 0.0439607, 0.0439614, 0.0439618, 0.0439616, 0.0439601, 0.0439554, 0.0439433, 0.0439135, 0.0438452, 0.0437043, 0.0434649, 0.0431603, 0.0428923, 0.0427639, 0.0428367, 0.0431063, 0.0433412, 0.0432045, 0.0434168, 0.0437639]]\n",
269-
"[[0, 0.00453626, 0.00576241, 0.00593085, 0.0059697, 0.00599664, 0.00603969, 0.00614157, 0.00635712, 0.00665648, 0.00696915, 0.00727134, 0.00756004, 0.00783543, 0.00809717, 0.00834377, 0.00857198, 0.00877537, 0.00894056, 0.00903652, 0.00898227, 0.00859883, 0.00788577, 0.00720723, 0.0066758, 0.00634508, 0.00619816, 0.00613976, 0.00608265, 0.00597241, 0.00584504, 0.00594164, 0.00625905, 0.00659515, 0.00691037, 0.00721551, 0.00753136, 0.00789289, 0.00836534, 0.00905537, 0.0100645, 0.0113496, 0.0126403, 0.01357, 0.0139674, 0.0136313, 0.0119871, 0.00958296, 0.00846165, 0.00808178]]\n",
270-
"[[0.00744968, 0.00706049, 0.00707755, 0.00710189, 0.00712197, 0.00715069, 0.00721447, 0.00738035, 0.00773676, 0.0082317, 0.00874522, 0.00923704, 0.00970383, 0.0101478, 0.0105697, 0.0109681, 0.0113379, 0.0116681, 0.0119346, 0.0120797, 0.0119521, 0.0112178, 0.00990156, 0.00874322, 0.00791797, 0.00744991, 0.00728365, 0.00725226, 0.00720922, 0.00706729, 0.00688395, 0.00704026, 0.00754735, 0.00808927, 0.00859748, 0.00908629, 0.00958379, 0.0101323, 0.0108059, 0.0117219, 0.0129838, 0.0145132, 0.015934, 0.0167318, 0.0167981, 0.0161305, 0.0141787, 0.0114777, 0.0101689, 0.00975205]]\n"
267+
"ename": "AttributeError",
268+
"evalue": "'ProcessedVariable' object has no attribute 'symbolic_inputs_dict'",
269+
"output_type": "error",
270+
"traceback": [
271+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
272+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
273+
"\u001b[0;32m<ipython-input-13-ef5967a63575>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0minputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mV\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbolic_inputs_dict\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mh\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1e-6\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mV_down\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mV\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mh\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
274+
"\u001b[0;31mAttributeError\u001b[0m: 'ProcessedVariable' object has no attribute 'symbolic_inputs_dict'"
271275
]
272276
}
273277
],
@@ -285,31 +289,9 @@
285289
},
286290
{
287291
"cell_type": "code",
288-
"execution_count": 82,
292+
"execution_count": null,
289293
"metadata": {},
290-
"outputs": [
291-
{
292-
"data": {
293-
"text/plain": [
294-
"{'Dsn': 1,\n",
295-
" 'j0n': 1,\n",
296-
" 'Dsp': 1,\n",
297-
" 'j0p': 1,\n",
298-
" 'D_e': 1,\n",
299-
" 'kappa_e': 1,\n",
300-
" 0: 1,\n",
301-
" 1: 1,\n",
302-
" 2: 1,\n",
303-
" 3: 1,\n",
304-
" 4: 1,\n",
305-
" 5: 1}"
306-
]
307-
},
308-
"execution_count": 82,
309-
"metadata": {},
310-
"output_type": "execute_result"
311-
}
312-
],
294+
"outputs": [],
313295
"source": [
314296
"inputs"
315297
]

pybamm/solvers/base_solver.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def report(string):
299299
else:
300300
df_dz = casadi.jacobian(func, y_alg)
301301
S_z_mat = S_z.reshape(
302-
(model.len_rhs, p_casadi_stacked.shape[0])
302+
(model.len_alg, p_casadi_stacked.shape[0])
303303
)
304304
S_rhs = (df_dx @ S_x_mat + df_dz @ S_z_mat + df_dp).reshape(
305305
(-1, 1)
@@ -312,7 +312,7 @@ def report(string):
312312
dg_dz = casadi.jacobian(func, y_alg)
313313
dg_dp = casadi.jacobian(func, p_casadi_stacked)
314314
S_z_mat = S_z.reshape(
315-
(model.len_rhs, p_casadi_stacked.shape[0])
315+
(model.len_alg, p_casadi_stacked.shape[0])
316316
)
317317
if model.len_rhs == 0:
318318
S_alg = (dg_dz @ S_z_mat + dg_dp).reshape((-1, 1))
@@ -930,6 +930,12 @@ def _set_up_ext_and_inputs(self, model, external_variables, inputs):
930930
for input_param in model.input_parameters:
931931
name = input_param.name
932932
if name not in inputs:
933+
# Don't allow symbolic inputs if using `solve_sensitivity_equations`
934+
if self.solve_sensitivity_equations is True:
935+
raise pybamm.SolverError(
936+
"Cannot have symbolic inputs if explicitly solving forward"
937+
"sensitivity equations"
938+
)
933939
# Only allow symbolic inputs for CasadiSolver and CasadiAlgebraicSolver
934940
if not isinstance(
935941
self, (pybamm.CasadiSolver, pybamm.CasadiAlgebraicSolver)

0 commit comments

Comments
 (0)