Skip to content

Commit

Permalink
#920 tino comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimms committed May 29, 2020
1 parent ad49ca7 commit af679e2
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

## Breaking changes

- Calls to `ProcessedVariable` objects are now made using dimensional time and space ([#1028](https://github.com/pybamm-team/PyBaMM/pull/1028))
- For variables discretised using finite elements the result returned by calling `ProcessedVariable` is now transposed ([#1020](https://github.com/pybamm-team/PyBaMM/pull/1020))
- Renamed "surface area density" to "surface area to volume ratio" ([#975](https://github.com/pybamm-team/PyBaMM/pull/975))
- Replaced "reaction rate" with "exchange-current density" ([#975](https://github.com/pybamm-team/PyBaMM/pull/975))
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/compare-comsol-discharge-curve.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
" # solve model at comsol times\n",
" solver = pybamm.CasadiSolver(mode=\"fast\")\n",
" solution = solver.solve(model, comsol_time, inputs={\"Current function [A]\": current})\n",
" time_in_seconds = solution.t * model.timescale_eval\n",
" time_in_seconds = solution[\"Time [s]\"].entries\n",
" # discharge capacity\n",
" discharge_capacity = solution[\"Discharge capacity [A.h]\"]\n",
" discharge_capacity_sol = discharge_capacity(time_in_seconds)\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/notebooks/rate-capability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,7 +36,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -52,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down
4 changes: 1 addition & 3 deletions examples/scripts/DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@

# solve model
t_eval = np.linspace(0, 3600, 100)
solver = pybamm.CasadiSolver()
solver.rtol = 1e-3
solver.atol = 1e-6
solver = pybamm.CasadiSolver(atol=1e-3, rtol=1e-6)
solution = solver.solve(model, t_eval)

# plot
Expand Down
4 changes: 1 addition & 3 deletions examples/scripts/DFN_ambient_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def ambient_temperature(t):

# solve model
t_eval = np.linspace(0, 3600 / 2, 100)
solver = pybamm.CasadiSolver(mode="fast")
solver.rtol = 1e-3
solver.atol = 1e-6
solver = pybamm.CasadiSolver(mode="fast", atol=1e-3, rtol=1e-3)
solution = solver.solve(model, t_eval)

# plot
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/SPMe_SOC.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
xnsurf = sol["X-averaged negative particle surface concentration"]
time = sol["Time [h]"]
# Coulomb counting
time_secs = sol.t * model.timescale_eval
time_secs = sol["Time [s]"].entries
time_hours = time(time_secs)
dc_time = np.around(time_hours[-1], 3)
# Capacity mAh
Expand Down
20 changes: 6 additions & 14 deletions examples/scripts/SPMe_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,12 @@
time += dt

# plot
voltage = solution["Terminal voltage [V]"]
step_voltage = step_solution["Terminal voltage [V]"]
plt.plot(
solution.t * timescale,
voltage(solution.t * timescale),
"b-",
label="SPMe (continuous solve)",
)
plt.plot(
step_solution.t * timescale,
step_voltage(step_solution.t * timescale),
"ro",
label="SPMe (stepped solve)",
)
time_in_seconds = solution["Time [s]"].entries
step_time_in_seconds = step_solution["Time [s]"].entries
voltage = solution["Terminal voltage [V]"].entries
step_voltage = step_solution["Terminal voltage [V]"].entries
plt.plot(time_in_seconds, voltage, "b-", label="SPMe (continuous solve)")
plt.plot(step_time_in_seconds, step_voltage, "ro", label="SPMe (stepped solve)")
plt.xlabel(r"$t$")
plt.ylabel("Terminal voltage [V]")
plt.legend()
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/compare_comsol/discharge_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
solution = pybamm.CasadiSolver(mode="fast").solve(
model, t, inputs={"Current function [A]": current}
)
time_in_seconds = solution.t * model.timescale_eval
time_in_seconds = solution["Time [s]"].entries

# discharge capacity
discharge_capacity = solution["Discharge capacity [A.h]"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
# load models
models = [
pybamm.lithium_ion.SPM(
{"current collector": "potential pair", "dimensionality": 2}, name="2+1D SPM"
{"current collector": "potential pair", "dimensionality": 1}, name="2+1D SPM"
),
pybamm.lithium_ion.SPMe(
{"current collector": "potential pair", "dimensionality": 1}, name="2+1D SPMe"
),
# pybamm.lithium_ion.SPMe(
# {"current collector": "potential pair", "dimensionality": 2}, name="2+1D SPMe"
# ),
]

# load parameter values and process models
Expand All @@ -36,13 +36,13 @@
param.process_geometry(geometry)
var = pybamm.standard_spatial_vars
var_pts = {
var.x_n: 5,
var.x_s: 5,
var.x_p: 5,
var.r_n: 5,
var.r_p: 5,
var.y: 5,
var.z: 5,
var.x_n: 10,
var.x_s: 10,
var.x_p: 10,
var.r_n: 10,
var.r_p: 10,
var.y: 10,
var.z: 10,
}
mesh = pybamm.Mesh(geometry, model.default_submesh_types, var_pts)
disc = pybamm.Discretisation(mesh, model.default_spatial_methods)
Expand All @@ -56,6 +56,10 @@
solutions[i] = solution

# plot
output_variables = ["Terminal voltage [V]", "Negative current collector potential [V]"]
output_variables = [
"Terminal voltage [V]",
"Negative current collector potential [V]",
"Positive current collector potential [V]",
]
plot = pybamm.QuickPlot(solutions, output_variables)
plot.dynamic_plot()
7 changes: 4 additions & 3 deletions examples/scripts/rate_capability.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
sim = pybamm.Simulation(model, experiment=experiment, solver=pybamm.CasadiSolver())
sim.solve()

time = sim.solution["Time [s]"].entries
capacity = sim.solution["Discharge capacity [A.h]"]
current = sim.solution["Current [A]"]
voltage = sim.solution["Terminal voltage [V]"]

capacities[i] = capacity(sim.solution.t[-1])
currents[i] = current(sim.solution.t[-1])
voltage_av[i] = np.mean(voltage(sim.solution.t))
capacities[i] = capacity(time[-1])
currents[i] = current(time[-1])
voltage_av[i] = np.mean(voltage(time))

plt.figure(1)
plt.scatter(C_rates, capacities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ def __init__(
self.options = options
self.param = pybamm.standard_parameters_lithium_ion

# Default timescale is discharge timescale (used in post process)
self.timescale = self.param.tau_discharge

self.variables = self.get_fundamental_variables()
self.set_algebraic(self.variables)
self.set_boundary_conditions(self.variables)
Expand Down
27 changes: 13 additions & 14 deletions pybamm/solvers/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ def __init__(self, base_variable, solution, known_evals=None, warn=True):
self.warn = warn

# Set timescale
try:
self.timescale = solution.model.timescale_eval
except AttributeError:
if self.warn:
pybamm.logger.warning("No timescale provided. Using default of 1 [s]")
self.timescale = 1
self.timescale = solution.model.timescale_eval
self.t_pts = self.t_sol * self.timescale

# Store spatial variables to get scales
self.spatial_vars = {}
if solution.model:
for var in ["x", "y", "z", "r_n", "r_p"]:
if var and var + " [m]" in solution.model.variables:
if (
var in solution.model.variables
and var + " [m]" in solution.model.variables
):
self.spatial_vars[var] = solution.model.variables[var]
self.spatial_vars[var + " [m]"] = solution.model.variables[
var + " [m]"
Expand Down Expand Up @@ -156,7 +155,7 @@ def fun(t):
self._interpolation_function = fun
else:
self._interpolation_function = interp.interp1d(
self.t_sol * self.timescale,
self.t_pts,
entries,
kind="linear",
fill_value=np.nan,
Expand Down Expand Up @@ -250,7 +249,7 @@ def interp_fun(t, z):
# function of space and time. Note that the order of 't' and 'space'
# is the reverse of what you'd expect
self._interpolation_function = interp.interp2d(
self.t_sol * self.timescale,
self.t_pts,
self.first_dim_pts,
entries_for_interp,
kind="linear",
Expand Down Expand Up @@ -353,7 +352,7 @@ def interp_fun(input):
else:
# function of space and time.
self._interpolation_function = interp.RegularGridInterpolator(
(self.first_dim_pts, self.second_dim_pts, self.t_sol * self.timescale),
(self.first_dim_pts, self.second_dim_pts, self.t_pts),
entries,
method="linear",
fill_value=np.nan,
Expand Down Expand Up @@ -418,7 +417,7 @@ def interp_fun(input):
else:
# function of space and time.
self._interpolation_function = interp.RegularGridInterpolator(
(self.first_dim_pts, self.second_dim_pts, self.t_sol * self.timescale),
(self.first_dim_pts, self.second_dim_pts, self.t_pts),
entries,
method="linear",
fill_value=np.nan,
Expand All @@ -436,10 +435,10 @@ def __call__(self, t=None, x=None, r=None, y=None, z=None, warn=True):
# time) evaluate arbitrarily at the first value of t. Otherwise, raise
# an error
if t is None:
if len(self.t_sol) == 1:
t = self.t_sol * self.timescale
if len(self.t_pts) == 1:
t = self.t_pts
elif self.base_variable.is_constant():
t = self.t_sol[0] * self.timescale
t = self.t_pts[0]
else:
raise ValueError(
"t cannot be None for variable {}".format(self.base_variable)
Expand Down
2 changes: 1 addition & 1 deletion pybamm/solvers/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
if copy_this is None:
# initialize empty inputs and model, to be populated later
self._inputs = pybamm.FuzzyDict()
self._model = None
self._model = pybamm.BaseModel()
self.set_up_time = None
self.solve_time = None
self.has_symbolic_inputs = False
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_solvers/test_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_init(self):
self.assertEqual(sol.y_event, None)
self.assertEqual(sol.termination, "final time")
self.assertEqual(sol.inputs, {})
self.assertEqual(sol.model, None)
self.assertIsInstance(sol.model, pybamm.BaseModel)

with self.assertRaisesRegex(AttributeError, "sub solutions"):
print(sol.sub_solutions)
Expand Down

0 comments on commit af679e2

Please sign in to comment.