Skip to content

Commit

Permalink
#729 tried adding entropic change functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottmar93 committed Feb 12, 2020
1 parent bbba976 commit 7203297
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 22 deletions.
16 changes: 16 additions & 0 deletions examples/scripts/ecker_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pybamm as pb

pb.set_logging_level("DEBUG")

model = pb.lithium_ion.DFN()
# model.convert_to_format = "python"

chemistry = pb.parameter_sets.Ecker2015
# chemistry = pb.parameter_sets.Marquis2019
parameter_values = pb.ParameterValues(chemistry=chemistry)

sim = pb.Simulation(model, parameter_values=parameter_values)

solver = pb.CasadiSolver(mode="safe")
sim.solve(solver=solver)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def graphite_entropic_change_NULL(sto, c_n_max):
"""
Entered as a function to be consistent with
other PyBaMM parameter sets.
"""
return 0 * sto
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Negative electrode charge transfer coefficient,0.489,,
Negative electrode double-layer capacity [F.m-2],0.2,,
,,,
# Thermal parameters,,,
Negative electrode OCP entropic change [V.K-1],0,,
Negative electrode OCP entropic change [V.K-1],[function]graphite_entropic_change_NULL,,
,,,
# Activation energies,,,
Reference temperature [K],296.15,23C,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def nco_entropic_change_NULL(sto, c_p_max):
"""
Entered as a function to be consistent with other PyBaMM parameter sets.
"""

return 0 * sto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Positive electrode double-layer capacity [F.m-2],0.2,,

,,,
# Thermal parameters,,,
Positive electrode OCP entropic change [V.K-1],0,,
Positive electrode OCP entropic change [V.K-1],[function]nco_entropic_change_NULL,,
,,,
# Activation energies,,,
Reference temperature [K],296.15,23C,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Heat transfer coefficient [W.m-2.K-1],1, dummy value,
# Electrical
Number of electrodes connected in parallel to make a cell,1,,
Number of cells connected in series to make a battery,1,,
Lower voltage cut-off [V],2.8,,
Upper voltage cut-off [V],4.7,,
C-rate,0.0001,,
Lower voltage cut-off [V],0,,
Upper voltage cut-off [V],10,,
C-rate,0.1,,
,,,
# Initial conditions
Initial concentration in negative electrode [mol.m-3], 26356,
Expand Down
49 changes: 32 additions & 17 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,20 @@ def report(string):
# Check for heaviside functions in rhs and algebraic and add discontinuity
# events if these exist.
# Note: only checks for the case of t < X, t <= X, X < t, or X <= t
for symbol in itertools.chain(model.concatenated_rhs.pre_order(),
model.concatenated_algebraic.pre_order()):
for symbol in itertools.chain(
model.concatenated_rhs.pre_order(), model.concatenated_algebraic.pre_order()
):
if isinstance(symbol, pybamm.Heaviside):
if symbol.right.id == pybamm.t.id:
expr = symbol.left
elif symbol.left.id == pybamm.t.id:
expr = symbol.right

model.events.append(pybamm.Event(str(symbol), expr.new_copy(),
pybamm.EventType.DISCONTINUITY))
model.events.append(
pybamm.Event(
str(symbol), expr.new_copy(), pybamm.EventType.DISCONTINUITY
)
)

# Process rhs, algebraic and event expressions
rhs, rhs_eval, jac_rhs = process(model.concatenated_rhs, "RHS")
Expand All @@ -241,7 +245,8 @@ def report(string):
# discontinuity events are evaluated before the solver is called, so don't need
# to process them
discontinuity_events_eval = [
event for event in model.events
event
for event in model.events
if event.event_type == pybamm.EventType.DISCONTINUITY
]

Expand Down Expand Up @@ -448,11 +453,12 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
# make sure they are increasing in time
discontinuities = sorted(discontinuities)
pybamm.logger.info(
'Discontinuity events found at t = {}'.format(discontinuities)
"Discontinuity events found at t = {}".format(discontinuities)
)
# remove any identical discontinuities
discontinuities = [
v for i, v in enumerate(discontinuities)
v
for i, v in enumerate(discontinuities)
if i == len(discontinuities) - 1
or discontinuities[i] < discontinuities[i + 1]
]
Expand All @@ -462,16 +468,18 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
start_indices = [0]
end_indices = []
for dtime in discontinuities:
dindex = np.searchsorted(t_eval, dtime, side='left')
dindex = np.searchsorted(t_eval, dtime, side="left")
end_indices.append(dindex + 1)
start_indices.append(dindex + 1)
if t_eval[dindex] == dtime:
t_eval[dindex] += sys.float_info.epsilon
t_eval = np.insert(t_eval, dindex, dtime - sys.float_info.epsilon)
else:
t_eval = np.insert(t_eval, dindex,
[dtime - sys.float_info.epsilon,
dtime + sys.float_info.epsilon])
t_eval = np.insert(
t_eval,
dindex,
[dtime - sys.float_info.epsilon, dtime + sys.float_info.epsilon],
)
end_indices.append(len(t_eval))

# integrate separatly over each time segment and accumulate into the solution
Expand All @@ -480,16 +488,21 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
old_y0 = model.y0
solution = None
for start_index, end_index in zip(start_indices, end_indices):
pybamm.logger.info("Calling solver for {} < t < {}"
.format(t_eval[start_index], t_eval[end_index - 1]))
pybamm.logger.info(
"Calling solver for {} < t < {}".format(
t_eval[start_index], t_eval[end_index - 1]
)
)
timer.reset()
if solution is None:
solution = self._integrate(
model, t_eval[start_index:end_index], ext_and_inputs)
model, t_eval[start_index:end_index], ext_and_inputs
)
solution.solve_time = timer.time()
else:
new_solution = self._integrate(
model, t_eval[start_index:end_index], ext_and_inputs)
model, t_eval[start_index:end_index], ext_and_inputs
)
new_solution.solve_time = timer.time()
solution.append(new_solution, start_index=0)

Expand All @@ -501,14 +514,16 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
y0_guess = solution.y[:, -1]
if model.algebraic:
model.y0 = self.calculate_consistent_state(
model, t_eval[end_index], y0_guess)
model, t_eval[end_index], y0_guess
)
else:
model.y0 = y0_guess

last_state = solution.y[:, -1]
if len(model.algebraic) > 0:
model.y0 = self.calculate_consistent_state(
model, t_eval[end_index], last_state)
model, t_eval[end_index], last_state
)
else:
model.y0 = last_state

Expand Down

0 comments on commit 7203297

Please sign in to comment.