Skip to content

Commit

Permalink
add unit benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhav-Chopra-GT committed Jun 7, 2022
1 parent e1f52ff commit f53a7f4
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions benchmarks/unit_benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import pybamm
import numpy as np

R = 1e-5


def time_create_expression():
global model
model = pybamm.BaseModel()

R = pybamm.Parameter("Particle radius [m]")
D = pybamm.Parameter("Diffusion coefficient [m2.s-1]")
j = pybamm.Parameter("Interfacial current density [A.m-2]")
F = pybamm.Parameter("Faraday constant [C.mol-1]")
c0 = pybamm.Parameter("Initial concentration [mol.m-3]")

c = pybamm.Variable("Concentration [mol.m-3]", domain="negative particle")
N = -D * pybamm.grad(c)
dcdt = -pybamm.div(N)
model.rhs = {c: dcdt}

lbc = pybamm.Scalar(0)
rbc = -j / F / D
model.boundary_conditions = {
c: {"left": (lbc, "Neumann"), "right": (rbc, "Neumann")}
}

model.initial_conditions = {c: c0}
model.variables = {
"Concentration [mol.m-3]": c,
"Surface concentration [mol.m-3]": pybamm.surf(c),
"Flux [mol.m-2.s-1]": N,
}


def setup_parameterise():
time_create_expression()


def time_parameterise():

global param
param = pybamm.ParameterValues(
{
"Particle radius [m]": 10e-6,
"Diffusion coefficient [m2.s-1]": 3.9e-14,
"Interfacial current density [A.m-2]": 1.4,
"Faraday constant [C.mol-1]": 96485,
"Initial concentration [mol.m-3]": 2.5e4,
}
)
global r
r = pybamm.SpatialVariable(
"r", domain=["negative particle"], coord_sys="spherical polar"
)
global geometry
geometry = {"negative particle": {r: {"min": pybamm.Scalar(0), "max": R}}}
param.process_model(model)
param.process_geometry(geometry)


time_parameterise.setup = setup_parameterise


def setup_discretise():
time_create_expression()
time_parameterise()


def time_discretise():
time_create_expression()
time_parameterise()

submesh_types = {"negative particle": pybamm.Uniform1DSubMesh}
var_pts = {r: 20}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)

spatial_methods = {"negative particle": pybamm.FiniteVolume()}
disc = pybamm.Discretisation(mesh, spatial_methods)
disc.process_model(model)


time_discretise.setup = setup_discretise


def setup_solve():
time_create_expression()
time_parameterise()
time_discretise()


def time_solve():
solver = pybamm.ScipySolver()
t = np.linspace(0, 3600, 600)
solver.solve(model, t)


time_solve.setup = setup_solve

0 comments on commit f53a7f4

Please sign in to comment.