Skip to content

Commit

Permalink
#920 fix most integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimms committed May 28, 2020
1 parent 347654c commit 98c06cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
24 changes: 7 additions & 17 deletions pybamm/plotting/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,9 @@ def set_output_variables(self, output_variables, solutions):
) = self.get_spatial_var(variable_tuple, first_variable, "first")
self.spatial_variable_dict[variable_tuple] = {
spatial_var_name: spatial_var_value
* spatial_scale
/ self.spatial_factor
}
self.first_scaled_spatial_variable[variable_tuple] = (
spatial_var_value * spatial_scale
spatial_var_value * self.spatial_factor
)
self.first_spatial_scale[variable_tuple] = spatial_scale

Expand All @@ -343,18 +341,14 @@ def set_output_variables(self, output_variables, solutions):
second_spatial_scale,
) = self.get_spatial_var(variable_tuple, first_variable, "second")
self.spatial_variable_dict[variable_tuple] = {
first_spatial_var_name: first_spatial_var_value
* first_spatial_scale
/ self.spatial_factor,
second_spatial_var_name: second_spatial_var_value
* second_spatial_scale
/ self.spatial_factor,
first_spatial_var_name: first_spatial_var_value,
second_spatial_var_name: second_spatial_var_value,
}
self.first_scaled_spatial_variable[variable_tuple] = (
first_spatial_var_value * first_spatial_scale
first_spatial_var_value * self.spatial_factor
)
self.second_scaled_spatial_variable[variable_tuple] = (
second_spatial_var_value * second_spatial_scale
second_spatial_var_value * self.spatial_factor
)
if first_spatial_var_name == "r" and second_spatial_var_name == "x":
self.is_x_r[variable_tuple] = True
Expand Down Expand Up @@ -383,15 +377,11 @@ def get_spatial_var(self, key, variable, dimension):
else:
domain = variable.auxiliary_domains["secondary"][0]

# Remove subscript "n" or "p" so spatial_var_name can be used in the
# call to a `ProcessedVariable`
if spatial_var_name in ["r_n", "r_p"]:
spatial_var_name = "r"

if domain == "current collector":
domain += " {}".format(spatial_var_name)

# Get scale
# Get scale to go from dimensionless to dimensional in the units
# specified by spatial_unit
try:
spatial_scale = self.spatial_scales[domain]
except KeyError:
Expand Down
33 changes: 18 additions & 15 deletions pybamm/solvers/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def initialise_0D(self):
else:
entries[idx] = self.base_variable.evaluate(t, u, inputs=inputs)

# No discretisation provided, or variable has no domain (function of t only)
# set up interpolation
if len(self.t_sol) == 1:
# Variable is just a scalar value, but we need to create a callable
# function to be consitent with other processed variables
Expand Down Expand Up @@ -205,11 +205,8 @@ def initialise_1D(self, fixed_t=False):
# assign attributes for reference (either x_sol or r_sol)
self.entries = entries
self.dimensions = 1
if self.domain[0] == "negative particle":
self.first_dimension = "r_n"
self.r_sol = space
elif self.domain[0] == "positive particle":
self.first_dimension = "r_p"
if self.domain[0] in ["negative particle", "positive particle"]:
self.first_dimension = "r"
self.r_sol = space
elif self.domain[0] in [
"negative electrode",
Expand All @@ -225,7 +222,10 @@ def initialise_1D(self, fixed_t=False):
self.first_dimension = "x"
self.x_sol = space

self.first_dim_pts = space * self.get_spatial_scale(self.first_dimension)
# assign attributes for reference
self.first_dim_pts = space * self.get_spatial_scale(
self.first_dimension, self.domain[0]
)
self.internal_boundaries = self.mesh[0].internal_boundaries

# set up interpolation
Expand Down Expand Up @@ -278,10 +278,7 @@ def initialise_2D(self):
"negative electrode",
"positive electrode",
]:
if self.domain[0] == "negative particle":
self.first_dimension = "r_n"
elif self.domain[0] == "positive particle":
self.first_dimension = "r_p"
self.first_dimension = "r"
self.second_dimension = "x"
self.r_sol = first_dim_pts
self.x_sol = second_dim_pts
Expand Down Expand Up @@ -330,7 +327,7 @@ def initialise_2D(self):
self.entries = entries
self.dimensions = 2
self.first_dim_pts = first_dim_pts * self.get_spatial_scale(
self.first_dimension
self.first_dimension, self.domain[0]
)
self.second_dim_pts = second_dim_pts * self.get_spatial_scale(
self.second_dimension
Expand Down Expand Up @@ -481,8 +478,15 @@ def call_2D(self, t, x, r, y, z):
second_dim = second_dim[:, np.newaxis]
return self._interpolation_function((first_dim, second_dim, t))

def get_spatial_scale(self, name):
def get_spatial_scale(self, name, domain=None):
"Returns the spatial scale for a named spatial variable"
# Different scale in negative and positive particles
if domain == "negative particle":
name = "r_n"
elif domain == "positive particle":
name = "r_p"

# Try to get length scale
if name + " [m]" in self.spatial_vars and name in self.spatial_vars:
scale = (
self.spatial_vars[name + " [m]"] / self.spatial_vars[name]
Expand All @@ -505,8 +509,7 @@ def data(self):
def eval_dimension_name(name, x, r, y, z):
if name == "x":
out = x
elif name in ["r_n", "r_p"]:
name = "r" # remove subscript to match input name in case of error
elif name == "r":
out = r
elif name == "y":
out = y
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/test_models/standard_output_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,16 @@ def __init__(self, model, param, disc, solution, operating_condition):

def test_velocity_boundaries(self):
"""Test the boundary values of the current densities"""
L = self.v_box.first_dim_pts[-1]
np.testing.assert_array_almost_equal(self.v_box(self.t, 0), 0, decimal=4)
np.testing.assert_array_almost_equal(self.v_box(self.t, 1), 0, decimal=4)
np.testing.assert_array_almost_equal(self.v_box(self.t, L), 0, decimal=4)

def test_vertical_velocity(self):
"""Test the boundary values of the current densities"""
L = self.v_box.first_dim_pts[-1]
np.testing.assert_array_equal(self.dVbox_dz(self.t, 0), 0)
np.testing.assert_array_less(self.dVbox_dz(self.t, 0.5), 0)
np.testing.assert_array_equal(self.dVbox_dz(self.t, 1), 0)
np.testing.assert_array_less(self.dVbox_dz(self.t, 0.5 * L), 0)
np.testing.assert_array_equal(self.dVbox_dz(self.t, L), 0)

def test_velocity_vs_current(self):
"""Test the boundary values of the current densities"""
Expand Down

0 comments on commit 98c06cb

Please sign in to comment.