Skip to content

Commit

Permalink
#1100 working on examples
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Jul 13, 2020
1 parent 4c6bf59 commit b67a457
Show file tree
Hide file tree
Showing 6 changed files with 1,718 additions and 1,150 deletions.
204 changes: 93 additions & 111 deletions examples/notebooks/DFN-sensitivity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"model = pybamm.lithium_ion.DFN()"
"model = pybamm.lithium_ion.SPMe()"
]
},
{
Expand All @@ -64,56 +64,33 @@
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7.900401128126567"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ce_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
"csn_ref = param[\"Maximum concentration in negative electrode [mol.m-3]\"]\n",
"T_ref = param[\"Reference temperature [K]\"]\n",
"param.evaluate(param[\"Negative electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csn_ref, T_ref))"
]
},
{
"cell_type": "code",
"execution_count": 122,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"param = model.default_parameter_values\n",
"# Get reference values for evaluating functions\n",
"c_e_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
"ce_ref = param[\"Typical electrolyte concentration [mol.m-3]\"]\n",
"csn_ref = param[\"Maximum concentration in negative electrode [mol.m-3]\"]\n",
"csp_ref = param[\"Maximum concentration in positive electrode [mol.m-3]\"]\n",
"T_ref = param[\"Reference temperature [K]\"]\n",
"# Evaluate functions at reference values\n",
"Dsn_ref = param[\"Negative electrode diffusivity [m2.s-1]\"](0.5, T_ref).evaluate()\n",
"Dsp_ref = param[\"Positive electrode diffusivity [m2.s-1]\"](0.5, T_ref).evaluate()\n",
"De_ref = param[\"Electrolyte diffusivity [m2.s-1]\"](c_e_ref, T_ref).evaluate()\n",
"kappae_ref = param[\"Electrolyte conductivity [S.m-1]\"](c_e_ref, T_ref).evaluate()\n",
"De_ref = param[\"Electrolyte diffusivity [m2.s-1]\"](ce_ref, T_ref).evaluate()\n",
"kappae_ref = param[\"Electrolyte conductivity [S.m-1]\"](ce_ref, T_ref).evaluate()\n",
"j0n_ref = param.evaluate(param[\"Negative electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csn_ref, T_ref))\n",
"j0p_ref = param.evaluate(param[\"Positive electrode exchange-current density [A.m-2]\"](ce_ref, 0.5 * csp_ref, T_ref))"
]
},
{
"cell_type": "code",
"execution_count": 123,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"param[\"Negative electrode diffusivity [m2.s-1]\"] = Dsn_ref * pybamm.InputParameter(\"Dsn\")\n",
"# param[\"Positive electrode diffusivity [m2.s-1]\"] = Dsp_ref * pybamm.InputParameter(\"Dsp\")\n",
"param[\"Positive electrode diffusivity [m2.s-1]\"] = Dsp_ref * pybamm.InputParameter(\"Dsp\")\n",
"# param[\"Electrolyte diffusivity [m2.s-1]\"] = De_ref * pybamm.InputParameter(\"D_e\")\n",
"# param[\"Electrolyte conductivity [S.m-1]\"] = kappae_ref * pybamm.InputParameter(\"kappa_e\")\n",
"# param[\"Negative electrode exchange-current density [A.m-2]\"] = j0n_ref * pybamm.InputParameter(\"j0n\")\n",
Expand All @@ -129,13 +106,33 @@
},
{
"cell_type": "code",
"execution_count": 124,
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"solver = pybamm.CasadiSolver(extra_options_setup={\"ad_weight\": 0})\n",
"solver = pybamm.CasadiSolver(mode=\"fast\", solve_sensitivity_equations=True)\n",
"sim = pybamm.Simulation(model, parameter_values=param, solver=solver)\n",
"solution = sim.solve(t_eval=np.linspace(0,3600,5))"
"solution = sim.solve(t_eval=np.linspace(0,3600), inputs={\"Dsn\": 1, \"Dsp\": 1})"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.7811190900000042"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solution.solve_time"
]
},
{
Expand All @@ -147,16 +144,47 @@
},
{
"cell_type": "code",
"execution_count": 125,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"solver = pybamm.CasadiSolver(mode=\"fast\")\n",
"sim = pybamm.Simulation(model, parameter_values=param, solver=solver)\n",
"solution = sim.solve(t_eval=np.linspace(0,3600), inputs={\"Dsn\": 1, \"Dsp\": 1})"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.005656635999997661"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"solution.solve_time"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<pybamm.solvers.processed_symbolic_variable.ProcessedSymbolicVariable at 0x1401c88d0>"
"<pybamm.solvers.processed_variable.ProcessedVariable at 0x13da41210>"
]
},
"execution_count": 125,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -175,28 +203,30 @@
},
{
"cell_type": "code",
"execution_count": 128,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 144 ms, sys: 2.01 ms, total: 146 ms\n",
"Wall time: 146 ms\n"
]
},
{
"data": {
"text/plain": [
"DM([[3.77171, 3.67082, 3.61315, 3.58406, 3.16798]])"
"{'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",
" '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])}"
]
},
"execution_count": 128,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"V.sensitivity"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"V.value({\"Dsn\": 1, \"Dsp\": 1, \"D_e\": 1, \"kappa_e\": 1, \"j0n\": 1, \"j0p\": 1})"
Expand All @@ -211,63 +241,37 @@
},
{
"cell_type": "code",
"execution_count": 129,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 672 ms, sys: 7.61 ms, total: 679 ms\n",
"Wall time: 675 ms\n"
]
}
],
"outputs": [],
"source": [
"%%time\n",
"sens = V.sensitivity({\"Dsn\": 1, \"Dsp\": 1, \"D_e\": 1, \"kappa_e\": 1, \"j0n\": 1, \"j0p\": 1})"
]
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DM(\n",
"[[00, 00], \n",
" [0.00144585, 0.00372077], \n",
" [0.00652415, 0.00146917], \n",
" [0.00246126, 0.00137044], \n",
" [0.0174035, 0.223563]])"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"sens"
]
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[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",
"[[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",
"[[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",
"[[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",
"[[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",
"[[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"
"ename": "AttributeError",
"evalue": "'ProcessedVariable' object has no attribute 'symbolic_inputs_dict'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\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",
"\u001b[0;31mAttributeError\u001b[0m: 'ProcessedVariable' object has no attribute 'symbolic_inputs_dict'"
]
}
],
Expand All @@ -285,31 +289,9 @@
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Dsn': 1,\n",
" 'j0n': 1,\n",
" 'Dsp': 1,\n",
" 'j0p': 1,\n",
" 'D_e': 1,\n",
" 'kappa_e': 1,\n",
" 0: 1,\n",
" 1: 1,\n",
" 2: 1,\n",
" 3: 1,\n",
" 4: 1,\n",
" 5: 1}"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"inputs"
]
Expand Down
10 changes: 8 additions & 2 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def report(string):
else:
df_dz = casadi.jacobian(func, y_alg)
S_z_mat = S_z.reshape(
(model.len_rhs, p_casadi_stacked.shape[0])
(model.len_alg, p_casadi_stacked.shape[0])
)
S_rhs = (df_dx @ S_x_mat + df_dz @ S_z_mat + df_dp).reshape(
(-1, 1)
Expand All @@ -312,7 +312,7 @@ def report(string):
dg_dz = casadi.jacobian(func, y_alg)
dg_dp = casadi.jacobian(func, p_casadi_stacked)
S_z_mat = S_z.reshape(
(model.len_rhs, p_casadi_stacked.shape[0])
(model.len_alg, p_casadi_stacked.shape[0])
)
if model.len_rhs == 0:
S_alg = (dg_dz @ S_z_mat + dg_dp).reshape((-1, 1))
Expand Down Expand Up @@ -930,6 +930,12 @@ def _set_up_ext_and_inputs(self, model, external_variables, inputs):
for input_param in model.input_parameters:
name = input_param.name
if name not in inputs:
# Don't allow symbolic inputs if using `solve_sensitivity_equations`
if self.solve_sensitivity_equations is True:
raise pybamm.SolverError(
"Cannot have symbolic inputs if explicitly solving forward"
"sensitivity equations"
)
# Only allow symbolic inputs for CasadiSolver and CasadiAlgebraicSolver
if not isinstance(
self, (pybamm.CasadiSolver, pybamm.CasadiAlgebraicSolver)
Expand Down
Loading

0 comments on commit b67a457

Please sign in to comment.