Skip to content

Commit 067c504

Browse files
#1100 explicit foward sensitivity working
1 parent c79f0ca commit 067c504

File tree

3 files changed

+328
-287
lines changed

3 files changed

+328
-287
lines changed

examples/notebooks/DFN-sensitivity.ipynb

+46-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
{
4444
"cell_type": "code",
45-
"execution_count": 14,
45+
"execution_count": 2,
4646
"metadata": {},
4747
"outputs": [],
4848
"source": [
@@ -64,7 +64,7 @@
6464
},
6565
{
6666
"cell_type": "code",
67-
"execution_count": 15,
67+
"execution_count": 3,
6868
"metadata": {},
6969
"outputs": [],
7070
"source": [
@@ -85,7 +85,7 @@
8585
},
8686
{
8787
"cell_type": "code",
88-
"execution_count": 16,
88+
"execution_count": 4,
8989
"metadata": {},
9090
"outputs": [],
9191
"source": [
@@ -106,27 +106,27 @@
106106
},
107107
{
108108
"cell_type": "code",
109-
"execution_count": 19,
109+
"execution_count": 6,
110110
"metadata": {},
111111
"outputs": [],
112112
"source": [
113-
"solver = pybamm.CasadiSolver(mode=\"fast\", sensitivity=True)\n",
113+
"solver = pybamm.CasadiSolver(mode=\"fast\", sensitivity=\"casadi\")\n",
114114
"sim = pybamm.Simulation(model, parameter_values=param, solver=solver)\n",
115115
"solution = sim.solve(t_eval=np.linspace(0,3600), inputs={\"Dsn\": 1, \"Dsp\": 1})"
116116
]
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": 20,
120+
"execution_count": 7,
121121
"metadata": {},
122122
"outputs": [
123123
{
124124
"data": {
125125
"text/plain": [
126-
"1.7811190900000042"
126+
"0.015949444000000312"
127127
]
128128
},
129-
"execution_count": 20,
129+
"execution_count": 7,
130130
"metadata": {},
131131
"output_type": "execute_result"
132132
}
@@ -142,6 +142,44 @@
142142
"Since we have not specified the parameter values when solving, the resulting solution contains _symbolic_ variables, such as the voltage"
143143
]
144144
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 13,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"name": "stdout",
152+
"output_type": "stream",
153+
"text": [
154+
"CPU times: user 3.73 s, sys: 194 ms, total: 3.92 s\n",
155+
"Wall time: 3.91 s\n"
156+
]
157+
},
158+
{
159+
"data": {
160+
"text/plain": [
161+
"{'all': DM(sparse: 1500-by-100, 73500 nnz\n",
162+
" (30, 0) -> -8.80308e-05\n",
163+
" (31, 0) -> -9.35556e-05\n",
164+
" (32, 0) -> -0.000107887\n",
165+
" ...\n",
166+
" (1497, 98) -> 0.0170617\n",
167+
" (1498, 98) -> 0.021474\n",
168+
" (1499, 98) -> 0.0260439),\n",
169+
" 'Dsn': DM([00, 00, 00, ..., 0.0170617, 0.021474, 0.0260439]),\n",
170+
" 'Dsp': DM([00, 00, 00, ..., 00, 00, 00])}"
171+
]
172+
},
173+
"execution_count": 13,
174+
"metadata": {},
175+
"output_type": "execute_result"
176+
}
177+
],
178+
"source": [
179+
"%%time\n",
180+
"solution[\"X-averaged negative particle concentration\"].sensitivity"
181+
]
182+
},
145183
{
146184
"cell_type": "code",
147185
"execution_count": 17,

pybamm/solvers/base_solver.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ def set_up(self, model, inputs=None):
204204
model.convert_to_format = "casadi"
205205

206206
# Only allow solving sensitivity equations with the casadi format for now
207-
if self.sensitivity is True and model.convert_to_format != "casadi":
207+
if (
208+
self.sensitivity is "explicit forward"
209+
and model.convert_to_format != "casadi"
210+
):
208211
raise NotImplementedError(
209212
"model should be converted to casadi format in order to solve "
210213
"sensitivity equations"
@@ -231,7 +234,7 @@ def set_up(self, model, inputs=None):
231234
p_casadi[name] = casadi.MX.sym(name, value.shape[0])
232235
p_casadi_stacked = casadi.vertcat(*[p for p in p_casadi.values()])
233236
# sensitivity vectors
234-
if self.sensitivity is True:
237+
if self.sensitivity == "explicit forward":
235238
S_x = casadi.MX.sym("S_x", model.len_rhs * p_casadi_stacked.shape[0])
236239
S_z = casadi.MX.sym("S_z", model.len_alg * p_casadi_stacked.shape[0])
237240
y_and_S = casadi.vertcat(y_diff, S_x, y_alg, S_z)
@@ -286,7 +289,7 @@ def report(string):
286289
report(f"Converting {name} to CasADi")
287290
func = func.to_casadi(t_casadi, y_casadi, inputs=p_casadi)
288291
# Add sensitivity vectors to the rhs and algebraic equations
289-
if self.sensitivity is True:
292+
if self.sensitivity == "explicit forward":
290293
if name == "rhs" and model.len_rhs > 0:
291294
report("Creating sensitivity equations for rhs using CasADi")
292295
df_dx = casadi.jacobian(func, y_diff)
@@ -408,7 +411,7 @@ def report(string):
408411
)[0]
409412
init_eval = InitialConditions(initial_conditions, model)
410413

411-
if self.sensitivity is True:
414+
if self.sensitivity == "explicit forward":
412415
init_eval.y_dummy = np.zeros(
413416
(
414417
model.len_rhs_and_alg * (np.vstack(list(inputs.values())).size + 1),
@@ -456,7 +459,7 @@ def report(string):
456459
):
457460
# can use DAE solver to solve model with algebraic equations only
458461
if len(model.rhs) > 0:
459-
if self.sensitivity is True:
462+
if self.sensitivity == "explicit forward":
460463
# Copy mass matrix blocks diagonally
461464
single_mass_matrix_inv = model.mass_matrix_inv.entries.toarray()
462465
n_inputs = p_casadi_stacked.shape[0]
@@ -946,7 +949,7 @@ def _set_up_ext_and_inputs(self, model, external_variables, inputs):
946949
name = input_param.name
947950
if name not in inputs:
948951
# Don't allow symbolic inputs if using `sensitivity`
949-
if self.sensitivity is True:
952+
if self.sensitivity == "explicit forward":
950953
raise pybamm.SolverError(
951954
"Cannot have symbolic inputs if explicitly solving forward"
952955
"sensitivity equations"

0 commit comments

Comments
 (0)