Skip to content

Commit

Permalink
Merge pull request #801 from pybamm-team/issue-709-simplify-on-creation
Browse files Browse the repository at this point in the history
Issue 709 simplify on creation
  • Loading branch information
valentinsulzer committed Feb 6, 2020
2 parents 93b3b25 + 27c118d commit 017529c
Show file tree
Hide file tree
Showing 31 changed files with 521 additions and 789 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

## Optimizations

- Now simplifying objects that are constant as soon as they are created ([#801](https://github.com/pybamm-team/PyBaMM/pull/801))
- Simplified solver interface ([#800](https://github.com/pybamm-team/PyBaMM/pull/800))
- Added caching for shape evaluation, used during discretisation ([#780](https://github.com/pybamm-team/PyBaMM/pull/780))
- Added an option to skip model checks during discretisation, which could be slow for large models ([#739](https://github.com/pybamm-team/PyBaMM/pull/739))
Expand Down
53 changes: 17 additions & 36 deletions examples/notebooks/change-input-current.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"\n",
"In this notebook we will use the SPM as the example model, and change the input current from the default option. If you are not familiar with running a model in PyBaMM, please see [this](./models/SPM.ipynb) notebook for more details.\n",
"\n",
"In PyBaMM, the current function is set using the parameter \"Current function [A]\". Below we load the SPM with the default parameters, and then change the the current function to 16A."
"In PyBaMM, the current function is set using the parameter \"Current function [A]\". Below we load the SPM with the default parameters, and then change the the current function to be an input parameter, so that we can change it easily later."
]
},
{
Expand All @@ -45,15 +45,15 @@
"# set the default model parameters\n",
"param = model.default_parameter_values\n",
"\n",
"# change the current function\n",
"param[\"Current function [A]\"] = 16"
"# change the current function to be an input parameter\n",
"param[\"Current function [A]\"] = \"[input]\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now solve the model in the ususal way"
"We can now solve the model in the ususal way, with a 16A current"
]
},
{
Expand All @@ -66,12 +66,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "000da8e81b0b4e91984553ba15ba179c",
"model_id": "770ba7f159054c5bad11038ac7be3f72",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.0036363636363636364, step=0.005), Output()…"
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.003612040133779264, step=0.005), Output())…"
]
},
"metadata": {},
Expand All @@ -92,9 +92,9 @@
"\n",
"# Solve the model at the given time points\n",
"solver = model.default_solver\n",
"n = 100\n",
"n = 300\n",
"t_eval = np.linspace(0, 0.02, n)\n",
"solution = solver.solve(model, t_eval)\n",
"solution = solver.solve(model, t_eval, inputs={\"Current function [A]\": 16})\n",
"\n",
"# plot\n",
"quick_plot = pybamm.QuickPlot(solution)\n",
Expand All @@ -114,27 +114,11 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"param[\"Current function [A]\"] = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The model may then be updated and solved again in the usual way."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cc4fd2e7c9ea4129a7acf0e519fea268",
"model_id": "4625a91d401b456d8e43aae201f4a380",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -147,11 +131,8 @@
}
],
"source": [
"# set the parameters for the model and the geometry\n",
"param.update_model(model, disc)\n",
"\n",
"# Solve the model at the given time points\n",
"solution = solver.solve(model, t_eval)\n",
"solution = solver.solve(model, t_eval, inputs={\"Current function [A]\": 0})\n",
"\n",
"# plot\n",
"quick_plot = pybamm.QuickPlot(solution)\n",
Expand All @@ -171,13 +152,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "84f6cac9b14f43a1907322e650f3c9f8",
"model_id": "88c9c957b666420e9cf6f615ff43817c",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -242,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -264,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -295,13 +276,13 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "09ced8a9e10b4e769a9b4cc8573b7fa2",
"model_id": "a8b90dfe860644c68af15c7c07e3adeb",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -366,4 +347,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
106 changes: 47 additions & 59 deletions examples/notebooks/change-settings.ipynb

Large diffs are not rendered by default.

27 changes: 5 additions & 22 deletions examples/notebooks/compare-comsol-discharge-curve.ipynb

Large diffs are not rendered by default.

83 changes: 19 additions & 64 deletions examples/notebooks/models/compare-lithium-ion.ipynb

Large diffs are not rendered by default.

95 changes: 48 additions & 47 deletions examples/notebooks/models/lead-acid.ipynb

Large diffs are not rendered by default.

65 changes: 32 additions & 33 deletions examples/notebooks/parameter-values.ipynb

Large diffs are not rendered by default.

131 changes: 43 additions & 88 deletions examples/notebooks/spatial_methods/finite-volumes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -615,19 +615,7 @@
" ├── @\n",
" │ ├── Sparse Matrix (42, 40)\n",
" │ └── y[0:40]\n",
" └── numpy concatenation\n",
" ├── Column vector of length 0\n",
" ├── *\n",
" │ ├── *\n",
" │ │ ├── 2.0\n",
" │ │ └── 1.0\n",
" │ └── Column vector of length 1\n",
" ├── Column vector of length 40\n",
" └── *\n",
" ├── *\n",
" │ ├── 2.0\n",
" │ └── 2.0\n",
" └── Column vector of length 1\n",
" └── Column vector of length 42\n",
"The value of u on the left-hand boundary is [1.]\n",
"The value of u on the right-hand boundary is [2.]\n"
]
Expand Down Expand Up @@ -663,25 +651,13 @@
"output_type": "stream",
"text": [
"The gradient object is:\n",
"@\n",
"├── Sparse Matrix (41, 42)\n",
"└── +\n",
" ├── @\n",
" │ ├── Sparse Matrix (42, 40)\n",
" │ └── y[0:40]\n",
" └── numpy concatenation\n",
" ├── Column vector of length 0\n",
" ├── *\n",
" │ ├── *\n",
" │ │ ├── -0.024999999999999998\n",
" │ │ └── 3.0\n",
" │ └── Column vector of length 1\n",
" ├── Column vector of length 40\n",
" └── *\n",
" ├── *\n",
" │ ├── 0.02499999999999991\n",
" │ └── 4.0\n",
" └── Column vector of length 1\n",
"+\n",
"├── @\n",
"│ ├── Sparse Matrix (41, 39)\n",
"│ └── @\n",
"│ ├── Sparse Matrix (39, 40)\n",
"│ └── y[0:40]\n",
"└── Column vector of length 41\n",
"The gradient on the left-hand boundary is [3.]\n",
"The gradient of u on the right-hand boundary is [4.]\n"
]
Expand Down Expand Up @@ -714,26 +690,18 @@
"output_type": "stream",
"text": [
"The gradient object is:\n",
"@\n",
"├── Sparse Matrix (41, 42)\n",
"└── +\n",
" ├── @\n",
" │ ├── Sparse Matrix (42, 40)\n",
" │ └── y[0:40]\n",
" └── numpy concatenation\n",
" ├── Column vector of length 0\n",
" ├── *\n",
" │ ├── *\n",
" │ │ ├── 2.0\n",
" │ │ └── 5.0\n",
" │ └── Column vector of length 1\n",
" ├── Column vector of length 40\n",
" └── *\n",
" ├── *\n",
" │ ├── 0.02499999999999991\n",
" │ └── 6.0\n",
" └── Column vector of length 1\n",
"The value of u on the left-hand boundary is [5.]\n",
"+\n",
"├── @\n",
"│ ├── Sparse Matrix (41, 40)\n",
"│ └── @\n",
"│ ├── Sparse Matrix (40, 41)\n",
"│ └── +\n",
"│ ├── @\n",
"│ │ ├── Sparse Matrix (41, 40)\n",
"│ │ └── y[0:40]\n",
"│ └── Column vector of length 41\n",
"└── Column vector of length 41\n",
"The value of u on the left-hand boundary is [0.]\n",
"The gradient on the right-hand boundary is [6.]\n"
]
}
Expand Down Expand Up @@ -797,25 +765,13 @@
"text": [
"@\n",
"├── Sparse Matrix (40, 41)\n",
"└── @\n",
" ├── Sparse Matrix (41, 42)\n",
" └── +\n",
" ├── @\n",
" │ ├── Sparse Matrix (42, 40)\n",
" │ └── y[0:40]\n",
" └── numpy concatenation\n",
" ├── Column vector of length 0\n",
" ├── *\n",
" │ ├── *\n",
" │ │ ├── -0.024999999999999998\n",
" │ │ └── -1.0\n",
" │ └── Column vector of length 1\n",
" ├── Column vector of length 40\n",
" └── *\n",
" ├── *\n",
" │ ├── 0.02499999999999991\n",
" │ └── 1.0\n",
" └── Column vector of length 1\n"
"└── +\n",
" ├── @\n",
" │ ├── Sparse Matrix (41, 39)\n",
" │ └── @\n",
" │ ├── Sparse Matrix (39, 40)\n",
" │ └── y[0:40]\n",
" └── Column vector of length 41\n"
]
}
],
Expand Down Expand Up @@ -878,11 +834,11 @@
"output_type": "stream",
"text": [
"@\n",
"├── Sparse Matrix (40, 42)\n",
"├── Sparse Matrix (40, 41)\n",
"└── +\n",
" ├── Column vector of length 42\n",
" ├── Column vector of length 41\n",
" └── @\n",
" ├── Sparse Matrix (42, 40)\n",
" ├── Sparse Matrix (41, 40)\n",
" └── y[0:40]\n"
]
}
Expand All @@ -904,13 +860,13 @@
"laplacian matrix is:\n",
"\n",
"1/dx^2 *\n",
"[[ 1. -2. 1. ... 0. 0. 0.]\n",
" [ 0. 1. -2. ... 0. 0. 0.]\n",
" [ 0. 0. 1. ... 0. 0. 0.]\n",
"[[-0.025 0.025 0. ... 0. 0. 0. ]\n",
" [ 0. -0.025 0.025 ... 0. 0. 0. ]\n",
" [ 0. 0. -0.025 ... 0. 0. 0. ]\n",
" ...\n",
" [ 0. 0. 0. ... 1. 0. 0.]\n",
" [ 0. 0. 0. ... -2. 1. 0.]\n",
" [ 0. 0. 0. ... 1. -2. 1.]]\n"
" [ 0. 0. 0. ... 0.025 0. 0. ]\n",
" [ 0. 0. 0. ... -0.025 0.025 0. ]\n",
" [ 0. 0. 0. ... 0. -0.025 0.025]]\n"
]
}
],
Expand All @@ -936,7 +892,7 @@
{
"data": {
"text/plain": [
"0.5950588420091663"
"0.4124502999475226"
]
},
"execution_count": 25,
Expand All @@ -957,7 +913,7 @@
{
"data": {
"text/plain": [
"0.3470027389994357"
"0.2925413999473676"
]
},
"execution_count": 26,
Expand Down Expand Up @@ -1035,9 +991,7 @@
"int(v):\n",
"\n",
"@\n",
"├── *\n",
"│ ├── 39.47841760435743\n",
"│ └── Sparse Matrix (1, 10)\n",
"├── Sparse Matrix (1, 10)\n",
"└── *\n",
" ├── /\n",
" │ ├── y[40:50]\n",
Expand Down Expand Up @@ -1083,7 +1037,8 @@
{
"data": {
"text/plain": [
"matrix([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
"matrix([[39.4784176, 39.4784176, 39.4784176, 39.4784176, 39.4784176,\n",
" 39.4784176, 39.4784176, 39.4784176, 39.4784176, 39.4784176]])"
]
},
"execution_count": 30,
Expand All @@ -1092,7 +1047,7 @@
}
],
"source": [
"int_v_over_r_disc.children[0].children[1].evaluate() / micro_mesh.d_edges"
"int_v_over_r_disc.children[0].evaluate() / micro_mesh.d_edges"
]
},
{
Expand Down Expand Up @@ -1282,7 +1237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/compare_comsol/compare_comsol_DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def myinterp(t):
"Positive electrode potential [V]": comsol_phi_p,
"Terminal voltage [V]": comsol_voltage,
}
comsol_solution = pybamm.CasadiSolver(mode="fast").solve(pybamm_model, time)
# Make new solution with same t and y
comsol_solution = pybamm.Solution(pybamm_solution.t, pybamm_solution.y)
comsol_solution.model = comsol_model
# plot
plot = pybamm.QuickPlot(
Expand Down
Loading

0 comments on commit 017529c

Please sign in to comment.