-
-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: refactor and speed-ups for Jax BDF Solver (#4456)
* Performance refactor for Jax BDF, "BDF" as default for JaxSolver, bugfixes for calculate_sensitivities, adds JAX vectorised example * update docstring, add changelog entry * feat: adds property for explicit sensitivity attribute, suggested changes from review * examples: adds JIT compiled comparison * Apply suggestions from code review Co-authored-by: Martin Robinson <[email protected]> * fix: post suggestions property alignment * tests: adds calculate_sensitivities check for JaxSolver * tests: move calculate_senstivities unit test for coverage --------- Co-authored-by: Martin Robinson <[email protected]>
- Loading branch information
1 parent
1390ea3
commit 62a7ee8
Showing
9 changed files
with
262 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import pybamm | ||
import time | ||
import numpy as np | ||
|
||
|
||
# This script provides an example for massively vectorised | ||
# model solves using the JAX BDF solver. First, | ||
# we set up the model and process parameters | ||
model = pybamm.lithium_ion.SPM() | ||
model.convert_to_format = "jax" | ||
model.events = [] # remove events (not supported in jax) | ||
geometry = model.default_geometry | ||
param = pybamm.ParameterValues("Chen2020") | ||
param.update({"Current function [A]": "[input]"}) | ||
param.process_geometry(geometry) | ||
param.process_model(model) | ||
|
||
# Discretise and setup solver | ||
mesh = pybamm.Mesh(geometry, model.default_submesh_types, model.default_var_pts) | ||
disc = pybamm.Discretisation(mesh, model.default_spatial_methods) | ||
disc.process_model(model) | ||
t_eval = np.linspace(0, 3600, 100) | ||
solver = pybamm.JaxSolver(atol=1e-6, rtol=1e-6, method="BDF") | ||
|
||
# Set number of vectorised solves | ||
values = np.linspace(0.01, 1.0, 1000) | ||
inputs = [{"Current function [A]": value} for value in values] | ||
|
||
# Run solve for all inputs, with a just-in-time compilation | ||
# occurring on the first solve. All sequential solves will | ||
# use the compiled code, with a large performance improvement. | ||
start_time = time.time() | ||
sol = solver.solve(model, t_eval, inputs=inputs) | ||
print(f"Time taken: {time.time() - start_time}") # 1.3s | ||
|
||
# Rerun the vectorised solve, showing performance improvement | ||
start_time = time.time() | ||
compiled_sol = solver.solve(model, t_eval, inputs=inputs) | ||
print(f"Compiled time taken: {time.time() - start_time}") # 0.42s | ||
|
||
# Plot one of the solves | ||
plot = pybamm.QuickPlot( | ||
sol[5], | ||
[ | ||
"Negative particle concentration [mol.m-3]", | ||
"Electrolyte concentration [mol.m-3]", | ||
"Positive particle concentration [mol.m-3]", | ||
"Current [A]", | ||
"Negative electrode potential [V]", | ||
"Electrolyte potential [V]", | ||
"Positive electrode potential [V]", | ||
"Voltage [V]", | ||
], | ||
time_unit="seconds", | ||
spatial_unit="um", | ||
) | ||
plot.dynamic_plot() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.