Skip to content

Commit

Permalink
#692 structure for external variables in place but need to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Scottmar93 committed Nov 9, 2019
1 parent 9fe3a3d commit 97f9a67
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def process_model(self, model, inplace=True):
elif isinstance(var, pybamm.Variable):
start_vals += [self.y_slices[var.id][0].start]

self.external_start = min(start_vals)
model_disc.external_variables = model.external_variables
model_disc.external_start = min(start_vals)
model_disc.y_length = self.y_length
model_disc.y_slices = self.y_slices

pybamm.logger.info("Discretise initial conditions for {}".format(model.name))
ics, concat_ics = self.process_initial_conditions(model)
Expand Down
4 changes: 3 additions & 1 deletion pybamm/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def step(self, dt, solver=None, external_variables=None):
if solver is None:
solver = self.solver

self._solution = solver.step(self.built_model, dt, external_variables)
self._solution = solver.step(
self.built_model, dt, external_variables=external_variables
)

def plot(self, quick_plot_vars=None):
"""
Expand Down
19 changes: 19 additions & 0 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, method=None, rtol=1e-6, atol=1e-6):
self._atol = atol
self.name = "Base solver"

self.y_pad = None
self.y_ext = None

@property
def method(self):
return self._method
Expand Down Expand Up @@ -147,9 +150,16 @@ def step(self, model, dt, npts=2, log=True, external_variables=None):
self.set_up(model)
self.t = 0.0
set_up_time = timer.time()

# create a y_pad vector of the correct size:
self.y_pad = np.zeros((model.external_start))

else:
set_up_time = None

if external_variables is None:
external_variables = {}

# load external variables into a state vector
self.y_ext = np.zeros((model.y_length, 1))
for var_name, var_vals in external_variables.items():
Expand Down Expand Up @@ -193,6 +203,15 @@ def step(self, model, dt, npts=2, log=True, external_variables=None):
)
return solution

def add_external(self, y):
"""
Pad the state vector and then add the external variables so that
it is of the correct shape for evaluate
"""
if self.y_pad and self.y_ext:
y = np.concatenate(y, self.y_pad) + self.y_ext
return y

def compute_solution(self, model, t_eval):
"""Calculate the solution of the model at specified times. Note: this
does *not* execute the solver setup.
Expand Down
2 changes: 2 additions & 0 deletions pybamm/solvers/dae_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def residuals(t, y, ydot):
pybamm.logger.debug(
"Evaluating residuals for {} at t={}".format(model.name, t)
)
y = self.add_external(y)
y = y[:, np.newaxis]
rhs_eval, known_evals = concatenated_rhs.evaluate(t, y, known_evals={})
# reuse known_evals
Expand All @@ -202,6 +203,7 @@ def residuals(t, y, ydot):
# Create event-dependent function to evaluate events
def event_fun(event):
def eval_event(t, y):
y = self.add_external(y)
return event.evaluate(t, y)

return eval_event
Expand Down
6 changes: 6 additions & 0 deletions pybamm/solvers/ode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ def set_up(self, model):
# Create function to evaluate rhs
def dydt(t, y):
pybamm.logger.debug("Evaluating RHS for {} at t={}".format(model.name, t))
y = self.add_external(y)
y = y[:, np.newaxis]
dy = concatenated_rhs.evaluate(t, y, known_evals={})[0]
return dy[:, 0]

# Create event-dependent function to evaluate events
def event_fun(event):
def eval_event(t, y):
y = self.add_external(y)
return event.evaluate(t, y)

return eval_event
Expand All @@ -138,6 +140,7 @@ def eval_event(t, y):
if jac_rhs is not None:

def jacobian(t, y):
y = self.add_external(y)
return jac_rhs.evaluate(t, y, known_evals={})[0]

else:
Expand Down Expand Up @@ -193,6 +196,7 @@ def set_up_casadi(self, model):

def dydt(t, y):
pybamm.logger.debug("Evaluating RHS for {} at t={}".format(model.name, t))
y = self.add_external(y)
dy = concatenated_rhs_fn(t, y).full()
return dy[:, 0]

Expand All @@ -201,6 +205,7 @@ def event_fun(event):
casadi_event_fn = casadi.Function("event", [t_casadi, y_casadi], [event])

def eval_event(t, y):
y = self.add_external(y)
return casadi_event_fn(t, y)

return eval_event
Expand All @@ -216,6 +221,7 @@ def eval_event(t, y):
)

def jacobian(t, y):
y = self.add_external(y)
return casadi_jac_fn(t, y)

else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def test_external_temperature(self):

t_eval = np.linspace(0, 0.17, 100)

for t in t_eval:
for i, t in enumerate(t_eval):
dt = t_eval[i + 1] - t_eval[i]
T = np.zeros((tot_pts, 1))
external_variables = {"Cell temperature": T}
sim.step(external_variables=external_variables)
sim.step(dt, external_variables=external_variables)


if __name__ == "__main__":
Expand Down

0 comments on commit 97f9a67

Please sign in to comment.