Skip to content

Commit

Permalink
pybamm-team#1087 fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brosaplanella committed Jun 29, 2020
1 parent 71834c0 commit 5d52fe3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 54 deletions.
92 changes: 44 additions & 48 deletions pybamm/solvers/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,39 +261,14 @@ def initialise_2D(self):
"""
first_dim_nodes = self.mesh.nodes
first_dim_edges = self.mesh.edges
second_dim_pts = self.base_variable.secondary_mesh.nodes
if self.base_eval.size // len(second_dim_pts) == len(first_dim_nodes):
second_dim_nodes = self.base_variable.secondary_mesh.nodes
second_dim_edges = self.base_variable.secondary_mesh.edges
if self.base_eval.size // len(second_dim_nodes) == len(first_dim_nodes):
first_dim_pts = first_dim_nodes
elif self.base_eval.size // len(second_dim_pts) == len(first_dim_edges):
elif self.base_eval.size // len(second_dim_nodes) == len(first_dim_edges):
first_dim_pts = first_dim_edges

# Process r-x or x-z
if self.domain[0] in [
"negative particle",
"positive particle",
] and self.auxiliary_domains["secondary"][0] in [
"negative electrode",
"positive electrode",
]:
self.first_dimension = "r"
self.second_dimension = "x"
self.r_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0] in [
"negative electrode",
"separator",
"positive electrode",
] and self.auxiliary_domains["secondary"] == ["current collector"]:
self.first_dimension = "x"
self.second_dimension = "z"
self.x_sol = first_dim_pts
self.z_sol = second_dim_pts
else:
raise pybamm.DomainError(
"Cannot process 3D object with domain '{}' "
"and auxiliary_domains '{}'".format(self.domain, self.auxiliary_domains)
)

second_dim_pts = second_dim_nodes
first_dim_size = len(first_dim_pts)
second_dim_size = len(second_dim_pts)
entries = np.empty((first_dim_size, second_dim_size, len(self.t_sol)))
Expand All @@ -320,20 +295,12 @@ def initialise_2D(self):
order="F",
)

# Get node and edge values
nodes = self.mesh.nodes
edges = self.mesh.edges
if entries.shape[0] == len(nodes):
space = nodes
elif entries.shape[0] == len(edges):
space = edges

# add points outside first dimension domain for extrapolation to
# boundaries
extrap_space_first_dim_left = np.array([2 * space[0] - space[1]])
extrap_space_first_dim_right = np.array([2 * space[-1] - space[-2]])
space_first_dim = np.concatenate(
[extrap_space_first_dim_left, space, extrap_space_first_dim_right]
extrap_space_first_dim_left = np.array([2 * first_dim_pts[0] - first_dim_pts[1]])
extrap_space_first_dim_right = np.array([2 * first_dim_pts[-1] - first_dim_pts[-2]])
first_dim_pts = np.concatenate(
[extrap_space_first_dim_left, first_dim_pts, extrap_space_first_dim_right]
)
extrap_entries_left = np.expand_dims(2 * entries[0] - entries[1], axis=0)
extrap_entries_right = np.expand_dims(2 * entries[-1] - entries[-2], axis=0)
Expand All @@ -349,7 +316,7 @@ def initialise_2D(self):
extrap_space_second_dim_right = np.array(
[2 * second_dim_pts[-1] - second_dim_pts[-2]]
)
space_second_dim = np.concatenate(
second_dim_pts = np.concatenate(
[
extrap_space_second_dim_left,
second_dim_pts,
Expand All @@ -371,22 +338,51 @@ def initialise_2D(self):
axis=1,
)

# Process r-x or x-z
if self.domain[0] in [
"negative particle",
"positive particle",
] and self.auxiliary_domains["secondary"][0] in [
"negative electrode",
"positive electrode",
]:
self.first_dimension = "r"
self.second_dimension = "x"
self.r_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0] in [
"negative electrode",
"separator",
"positive electrode",
] and self.auxiliary_domains["secondary"] == ["current collector"]:
self.first_dimension = "x"
self.second_dimension = "z"
self.x_sol = first_dim_pts
self.z_sol = second_dim_pts
else:
raise pybamm.DomainError(
"Cannot process 3D object with domain '{}' "
"and auxiliary_domains '{}'".format(self.domain, self.auxiliary_domains)
)



# assign attributes for reference
self.entries = entries
self.dimensions = 2
first_length_scale = self.get_spatial_scale(
self.first_dimension, self.domain[0]
)
first_dim_pts_for_interp = space_first_dim * first_length_scale
first_dim_pts_for_interp = first_dim_pts * first_length_scale

second_length_scale = self.get_spatial_scale(
self.second_dimension, self.auxiliary_domains["secondary"][0]
)
second_dim_pts_for_interp = space_second_dim * second_length_scale
self.second_dim_pts = second_dim_pts * second_length_scale
second_dim_pts_for_interp = second_dim_pts * second_length_scale

# Set first_dim_pts to edges for nicer plotting
self.first_dim_pts = edges * first_length_scale
# Set pts to edges for nicer plotting
self.first_dim_pts = first_dim_edges * first_length_scale
self.second_dim_pts = second_dim_edges * second_length_scale

# set up interpolation
if len(self.t_sol) == 1:
Expand Down
16 changes: 10 additions & 6 deletions tests/unit/test_quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def test_loqs_spme(self):
pybamm.QuickPlot(solution, output_variables)

# check 2D (space) variables update properly for different time units
c_n = solution["Negative particle concentration [mol.m-3]"].entries
c_n = solution["Negative particle concentration [mol.m-3]"]

for unit, scale in zip(["seconds", "minutes", "hours"], [1, 60, 3600]):
quick_plot = pybamm.QuickPlot(
Expand All @@ -342,12 +342,14 @@ def test_loqs_spme(self):
qp_data = quick_plot.plots[
("Negative particle concentration [mol.m-3]",)
][0][1]
np.testing.assert_array_almost_equal(qp_data, c_n[:, :, 0])
c_n_eval = c_n(t_eval[0], r=c_n.first_dim_pts, x=c_n.second_dim_pts)
np.testing.assert_array_almost_equal(qp_data, c_n_eval)
quick_plot.slider_update(t_eval[-1] / scale)
qp_data = quick_plot.plots[
("Negative particle concentration [mol.m-3]",)
][0][1]
np.testing.assert_array_almost_equal(qp_data, c_n[:, :, 1])
c_n_eval = c_n(t_eval[-1], r=c_n.first_dim_pts, x=c_n.second_dim_pts)
np.testing.assert_array_almost_equal(qp_data, c_n_eval)

pybamm.close_plots()

Expand All @@ -369,18 +371,20 @@ def test_plot_1plus1D_spme(self):

# check 2D (x,z space) variables update properly for different time units
# Note: these should be the transpose of the entries in the processed variable
c_e = solution["Electrolyte concentration [mol.m-3]"].entries
c_e = solution["Electrolyte concentration [mol.m-3]"]

for unit, scale in zip(["seconds", "minutes", "hours"], [1, 60, 3600]):
quick_plot = pybamm.QuickPlot(
solution, ["Electrolyte concentration [mol.m-3]"], time_unit=unit
)
quick_plot.plot(0)
qp_data = quick_plot.plots[("Electrolyte concentration [mol.m-3]",)][0][1]
np.testing.assert_array_almost_equal(qp_data.T, c_e[:, :, 0])
c_e_eval = c_e(t_eval[0], x=c_e.first_dim_pts, z=c_e.second_dim_pts)
np.testing.assert_array_almost_equal(qp_data.T, c_e_eval)
quick_plot.slider_update(t_eval[-1] / scale)
qp_data = quick_plot.plots[("Electrolyte concentration [mol.m-3]",)][0][1]
np.testing.assert_array_almost_equal(qp_data.T, c_e[:, :, -1])
c_e_eval = c_e(t_eval[-1], x=c_e.first_dim_pts, z=c_e.second_dim_pts)
np.testing.assert_array_almost_equal(qp_data.T, c_e_eval)

pybamm.close_plots()

Expand Down

0 comments on commit 5d52fe3

Please sign in to comment.