Skip to content

Commit

Permalink
#871 get 3D working ok
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 11, 2020
1 parent cf400bf commit 98f5434
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
16 changes: 14 additions & 2 deletions examples/scripts/SPMe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pybamm.set_logging_level("DEBUG")

# load model
model = pybamm.lithium_ion.SPMe()
model = pybamm.lithium_ion.DFN()
model.convert_to_format = "python"

# create geometry
Expand All @@ -32,6 +32,18 @@

# plot
plot = pybamm.QuickPlot(
solution, ["Negative particle concentration"], spatial_format="um"
solution,
[
"Negative particle concentration",
"Negative particle surface concentration [mol.m-3]",
"Electrolyte concentration [mol.m-3]",
"Positive particle surface concentration [mol.m-3]",
"Current [A]",
"Negative electrode potential [V]",
"Electrolyte potential [V]",
"Positive electrode potential [V]",
"Terminal voltage [V]",
],
spatial_format="um",
)
plot.dynamic_plot()
56 changes: 48 additions & 8 deletions pybamm/quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
from collections import defaultdict


class LoopList(list):
"A list which loops over itself when accessing an index so that it never runs out."

def __getitem__(self, i):
# implement looping by calling "(i) modulo (length of list)"
return super().__getitem__(i % len(self))


def ax_min(data):
"Calculate appropriate minimum axis value for plotting"
data_min = np.nanmin(data)
Expand Down Expand Up @@ -111,8 +119,9 @@ def __init__(
self.labels = labels

# Set colors, linestyles, figsize
self.colors = colors or ["r", "b", "k", "g", "m", "c"]
self.linestyles = linestyles or ["-", ":", "--", "-."]
# call LoopList to make sure list index never runs out
self.colors = LoopList(colors or ["r", "b", "k", "g", "m", "c"])
self.linestyles = LoopList(linestyles or ["-", ":", "--", "-."])
self.figsize = figsize or (15, 8)

# Spatial scales (default to 1 if information not in model)
Expand Down Expand Up @@ -424,13 +433,20 @@ def plot(self, t):
ax.set_xlabel("Time [h]", fontsize=fontsize)
for i, variable_list in enumerate(variable_lists):
for j, variable in enumerate(variable_list):
if len(variable_list) == 1:
# single variable -> use linestyle to differentiate model
linestyle = self.linestyles[i]
else:
# multiple variables -> use linestyle to differentiate
# variables (color differentiates models)
linestyle = self.linestyles[j]
full_t = self.ts[i]
(self.plots[key][i][j],) = ax.plot(
full_t * self.time_scale,
variable(full_t, warn=False),
lw=2,
color=self.colors[i],
linestyle=self.linestyles[j],
linestyle=linestyle,
)
y_min, y_max = self.axis[key][2:]
(self.time_lines[key],) = ax.plot(
Expand All @@ -447,12 +463,19 @@ def plot(self, t):
)
for i, variable_list in enumerate(variable_lists):
for j, variable in enumerate(variable_list):
if len(variable_list) == 1:
# single variable -> use linestyle to differentiate model
linestyle = self.linestyles[i]
else:
# multiple variables -> use linestyle to differentiate
# variables (color differentiates models)
linestyle = self.linestyles[j]
(self.plots[key][i][j],) = ax.plot(
self.first_dimensional_spatial_variable[key],
variable(t, **spatial_vars, warn=False),
lw=2,
color=self.colors[i],
linestyle=self.linestyles[j],
linestyle=linestyle,
)
elif variable_lists[0][0].dimensions == 2:
# 2D plot: plot as a function of x and y at time t
Expand All @@ -469,9 +492,10 @@ def plot(self, t):
# there can only be one entry in the variable list
variable = variable_lists[0][0]
self.plots[key][0][0] = ax.contourf(
self.second_dimensional_spatial_variable[key],
self.first_dimensional_spatial_variable[key],
variable(t, **spatial_vars, warn=False),
self.second_dimensional_spatial_variable[key],
variable(t, **spatial_vars, warn=False).T,
levels=100,
)

# Set either y label or legend entries
Expand Down Expand Up @@ -537,10 +561,10 @@ def update(self, val):
"""
t = self.sfreq.val
t_dimensionless = t / self.time_scale
for key, plot in self.plots.items():
for k, (key, plot) in enumerate(self.plots.items()):
if self.variables[key][0][0].dimensions == 0:
self.time_lines[key].set_xdata([t])
if self.variables[key][0][0].dimensions == 1:
elif self.variables[key][0][0].dimensions == 1:
for i, variable_lists in enumerate(self.variables[key]):
for j, variable in enumerate(variable_lists):
plot[i][j].set_ydata(
Expand All @@ -550,5 +574,21 @@ def update(self, val):
warn=False
)
)
elif self.variables[key][0][0].dimensions == 2:
if len(self.variables) == 1:
ax = self.ax
else:
ax = self.ax.flat[k]
# 2D plot: plot as a function of x and y at time t
# Read dictionary of spatial variables
spatial_vars = self.spatial_variable_dict[key]
# there can only be one entry in the variable list
variable = self.variables[key][0][0]
self.plots[key][0][0] = ax.contourf(
self.first_dimensional_spatial_variable[key],
self.second_dimensional_spatial_variable[key],
variable(t_dimensionless, **spatial_vars, warn=False).T,
levels=100,
)

self.fig.canvas.draw_idle()
22 changes: 11 additions & 11 deletions tests/unit/test_quick_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ def test_simple_ode_model(self):
):
pybamm.QuickPlot(solution, ["NaN variable"])

def test_spm_simulation(self):
# SPM
model = pybamm.lithium_ion.SPM()
sim = pybamm.Simulation(model)

t_eval = np.linspace(0, 10, 2)
sim.solve(t_eval)
# def test_spm_simulation(self):
# # SPM
# model = pybamm.lithium_ion.SPM()
# sim = pybamm.Simulation(model)

# mixed simulation and solution input
# solution should be extracted from the simulation
quick_plot = pybamm.QuickPlot([sim, sim.solution])
quick_plot.plot(0)
# t_eval = np.linspace(0, 10, 2)
# sim.solve(t_eval)

# # mixed simulation and solution input
# # solution should be extracted from the simulation
# quick_plot = pybamm.QuickPlot([sim, sim.solution])
# quick_plot.plot(0)
#
# def test_loqs_spm_base(self):
# t_eval = np.linspace(0, 10, 2)

Expand Down

0 comments on commit 98f5434

Please sign in to comment.