Skip to content

Commit

Permalink
#668 add evaluate function
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 17, 2019
1 parent 1028e78 commit daf6a79
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 152 deletions.
2 changes: 1 addition & 1 deletion examples/scripts/compare_comsol/compare_comsol_DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
# Make Comsol 'model' for comparison
whole_cell = ["negative electrode", "separator", "positive electrode"]
comsol_t = comsol_variables["time"]
L_x = param.process_symbol(pybamm.standard_parameters_lithium_ion.L_x).evaluate()
L_x = param.evaluate(pybamm.standard_parameters_lithium_ion.L_x)


def get_interp_fun(variable, domain):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ def get_processed_potentials(self, solution, mesh, param_values, V_av, I_av):
"""
# Get required processed parameters
param = self.param
l_cn = param_values.process_symbol(param.l_cn).evaluate()
l_cp = param_values.process_symbol(param.l_cp).evaluate()
l_y = param_values.process_symbol(param.l_y).evaluate()
l_z = param_values.process_symbol(param.l_z).evaluate()
sigma_cn_prime = param_values.process_symbol(param.sigma_cn_prime).evaluate()
sigma_cp_prime = param_values.process_symbol(param.sigma_cp_prime).evaluate()
alpha = param_values.process_symbol(param.alpha).evaluate()
pot_scale = param_values.process_symbol(param.potential_scale).evaluate()
U_ref = param_values.process_symbol(param.U_p_ref - param.U_n_ref).evaluate()
l_cn = param_values.evaluate(param.l_cn)
l_cp = param_values.evaluate(param.l_cp)
l_y = param_values.evaluate(param.l_y)
l_z = param_values.evaluate(param.l_z)
sigma_cn_prime = param_values.evaluate(param.sigma_cn_prime)
sigma_cp_prime = param_values.evaluate(param.sigma_cp_prime)
alpha = param_values.evaluate(param.alpha)
pot_scale = param_values.evaluate(param.potential_scale)
U_ref = param_values.evaluate(param.U_p_ref - param.U_n_ref)

# Process psi and W, and their (average) values at the negative tab
psi = pybamm.ProcessedVariable(
Expand Down
20 changes: 20 additions & 0 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,23 @@ def update_scalars(self, symbol):
x.function.interpolate()

return symbol

def evaluate(self, symbol):
"""
Process and evaluate a symbol.
Parameters
----------
symbol : :class:`pybamm.Symbol`
Symbol or Expression tree to evaluate
Returns
-------
number of array
The evaluated symbol
"""
processed_symbol = self.process_symbol(symbol)
if processed_symbol.is_constant() and processed_symbol.evaluates_to_number():
return processed_symbol.evaluate()
else:
raise ValueError("symbol must evaluate to a constant scalar")
4 changes: 2 additions & 2 deletions results/2plus1D/compare_lithium_ion_2plus1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
plt.ylabel("Terminal voltage [V]")
plt.legend()
# add C-rate, delta, and alpha to title
delta = param.process_symbol(pybamm.standard_parameters_lithium_ion.delta).evaluate()
alpha = param.process_symbol(pybamm.standard_parameters_lithium_ion.alpha).evaluate()
delta = param.evaluate(pybamm.standard_parameters_lithium_ion.delta)
alpha = param.evaluate(pybamm.standard_parameters_lithium_ion.alpha)
plt.title(
r"C-rate = {:3d}, $\alpha$ = {:.6f} , $\delta$ = {:.6f}".format(
C_rate, alpha, delta
Expand Down
4 changes: 1 addition & 3 deletions results/2plus1D/compare_spmecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
# add current collector Ohmic losses to average SPMEe to get SPMeCC voltage
if model.name == "Average SPMe":
current = pybamm.ProcessedVariable(model.variables["Current [A]"], t, y)(t)
delta = param.process_symbol(
pybamm.standard_parameters_lithium_ion.delta
).evaluate()
delta = param.evaluate(pybamm.standard_parameters_lithium_ion.delta)
R_cc = param.process_symbol(
cc_model.variables["Effective current collector resistance [Ohm]"]
).evaluate(
Expand Down
6 changes: 2 additions & 4 deletions results/2plus1D/spm_2plus1D_tab_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# adjust current to correspond to a typical current density of 24 [A.m-2]
C_rate = 1
param["Typical current [A]"] = (
C_rate * 24 * param.process_symbol(pybamm.geometric_parameters.A_cc).evaluate()
C_rate * 24 * param.evaluate(pybamm.geometric_parameters.A_cc)
)
param.process_model(model)
param.process_geometry(geometry)
Expand All @@ -39,9 +39,7 @@
var.z: 5,
}
submesh_types = model.default_submesh_types
submesh_types[
"current collector"
] = pybamm.ScikitExponential2DSubMesh
submesh_types["current collector"] = pybamm.ScikitExponential2DSubMesh
# depnding on number of points in y-z plane may need to increase recursion depth...
sys.setrecursionlimit(10000)
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
Expand Down
2 changes: 1 addition & 1 deletion results/2plus1D/spmecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
time = pybamm.ProcessedVariable(cell_model.variables["Time [h]"], t, y)(t)
voltage = pybamm.ProcessedVariable(cell_model.variables["Terminal voltage [V]"], t, y)
current = pybamm.ProcessedVariable(cell_model.variables["Current [A]"], t, y)(t)
delta = param.process_symbol(pybamm.standard_parameters_lithium_ion.delta).evaluate()
delta = param.evaluate(pybamm.standard_parameters_lithium_ion.delta)
R_cc = param.process_symbol(
cc_model.variables["Effective current collector resistance [Ohm]"]
).evaluate(t=cc_solution.t, y=cc_solution.y)[0][0]
Expand Down
2 changes: 1 addition & 1 deletion results/2plus1D/user_mesh_spm_1plus1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# load parameter values and process model and geometry
param = model.default_parameter_values
C_rate = 1
current_1C = 24 * param.process_symbol(pybamm.geometric_parameters.A_cc).evaluate()
current_1C = 24 * param.evaluate(pybamm.geometric_parameters.A_cc)
param.update(
{
"Typical current [A]": C_rate * current_1C,
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_models/standard_output_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def __init__(self, model, param, disc, solution, operating_condition):
self.r_p_edge = disc.mesh["positive particle"][0].edges

# Useful parameters
self.l_n = param.process_symbol(pybamm.geometric_parameters.l_n).evaluate()
self.l_p = param.process_symbol(pybamm.geometric_parameters.l_p).evaluate()
self.l_n = param.evaluate(pybamm.geometric_parameters.l_n)
self.l_p = param.evaluate(pybamm.geometric_parameters.l_p)

if isinstance(self.model, pybamm.lithium_ion.BaseModel):
current_param = pybamm.standard_parameters_lithium_ion.current_with_time
Expand Down Expand Up @@ -635,9 +635,9 @@ def test_velocity_vs_current(self):
t, x_n, x_p = self.t, self.x_n, self.x_p

beta_n = pybamm.standard_parameters_lead_acid.beta_n
beta_n = self.param.process_symbol(beta_n).evaluate()
beta_n = self.param.evaluate(beta_n)
beta_p = pybamm.standard_parameters_lead_acid.beta_p
beta_p = self.param.process_symbol(beta_p).evaluate()
beta_p = self.param.evaluate(beta_p)

np.testing.assert_array_almost_equal(
self.v_box(t, x_n), beta_n * self.i_e(t, x_n)
Expand Down
Loading

0 comments on commit daf6a79

Please sign in to comment.