Skip to content

Commit

Permalink
#2382 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Oct 20, 2022
1 parent ffebfaa commit 8d866d5
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 160 deletions.
2 changes: 1 addition & 1 deletion pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
from .models.base_model import BaseModel
from .models.base_equations import _BaseEquations, _BaseProcessedEquations
from .models.symbolic_equations import _SymbolicEquations
from .models.parameterised_equations import _ParameterisedEquations
from .models.parameterised_equations import _ParameterisedEquations, _ReplacedEquations
from .models.discretised_equations import _DiscretisedEquations
from .models.standard_variables import standard_variables
from .models.event import Event
Expand Down
49 changes: 24 additions & 25 deletions pybamm/expression_tree/operations/replace_symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,17 @@ def process_model(self, unprocessed_model, inplace=True):
"Start replacing symbols in {}".format(unprocessed_model.name)
)

# set up inplace vs not inplace
if inplace:
# any changes to unprocessed_model attributes will change model attributes
# since they point to the same object
model = unprocessed_model
else:
# create a copy of the model
model = unprocessed_model.new_copy()

new_rhs = {}
for variable, equation in unprocessed_model.rhs.items():
pybamm.logger.verbose("Replacing symbols in {!r} (rhs)".format(variable))
new_rhs[self.process_symbol(variable)] = self.process_symbol(equation)
model.rhs = new_rhs

new_algebraic = {}
for variable, equation in unprocessed_model.algebraic.items():
pybamm.logger.verbose(
"Replacing symbols in {!r} (algebraic)".format(variable)
)
new_algebraic[self.process_symbol(variable)] = self.process_symbol(equation)
model.algebraic = new_algebraic

new_initial_conditions = {}
for variable, equation in unprocessed_model.initial_conditions.items():
Expand All @@ -78,17 +67,8 @@ def process_model(self, unprocessed_model, inplace=True):
] = self.process_symbol(equation)
else:
new_initial_conditions[self.process_symbol(variable)] = equation
model.initial_conditions = new_initial_conditions

model.boundary_conditions = self.process_boundary_conditions(unprocessed_model)

new_variables = {}
for variable, equation in unprocessed_model.variables.items():
pybamm.logger.verbose(
"Replacing symbols in {!r} (variables)".format(variable)
)
new_variables[variable] = self.process_symbol(equation)
model.variables = new_variables
new_boundary_conditions = self.process_boundary_conditions(unprocessed_model)

new_events = []
for event in unprocessed_model.events:
Expand All @@ -98,21 +78,40 @@ def process_model(self, unprocessed_model, inplace=True):
event.name, self.process_symbol(event.expression), event.event_type
)
)
model.events = new_events

# Set external variables
model.external_variables = [
new_external_variables = [
self.process_symbol(var) for var in unprocessed_model.external_variables
]

# Process timescale
model._timescale = self.process_symbol(unprocessed_model.timescale)
new_timescale = self.process_symbol(unprocessed_model.timescale)

# Process length scales
new_length_scales = {}
for domain, scale in unprocessed_model.length_scales.items():
new_length_scales[domain] = self.process_symbol(scale)
model._length_scales = new_length_scales

parameterised_equations = pybamm._ReplacedEquations(
self,
new_rhs,
new_algebraic,
new_initial_conditions,
new_boundary_conditions,
unprocessed_model.variables,
new_events,
new_external_variables,
new_timescale,
new_length_scales,
)

# inplace vs not inplace
if inplace:
model = unprocessed_model
model._equations = parameterised_equations
else:
# create a copy of the model
model = unprocessed_model.new_copy(equations=parameterised_equations)

pybamm.logger.info("Finish replacing symbols in {}".format(model.name))

Expand Down
29 changes: 21 additions & 8 deletions pybamm/models/base_equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ def boundary_conditions(self):
def variables(self):
return self._variables

def variable_names(self):
return list(self._variables.keys())

@functools.cached_property
def variables_and_events(self):
"""
Expand Down Expand Up @@ -439,11 +436,27 @@ def __init__(self, unprocessed_variables, variables_update_function):
self.variables_update_function = variables_update_function

def __getitem__(self, key):
if key not in self:
self.update(
{key: self.variables_update_function(self.unprocessed_variables[key])}
)
return super().__getitem__(key)
try:
return super().__getitem__(key)
except KeyError:
item = self.variables_update_function(self.unprocessed_variables[key])
self.update({key: item})
return item

def search(self, *args, **kwargs):
return self.unprocessed_variables.search(*args, **kwargs)

def __len__(self):
return len(self.unprocessed_variables)

def __iter__(self):
return iter(self.unprocessed_variables)

def __contains__(self, key):
return key in self.unprocessed_variables

def keys(self):
return self.unprocessed_variables.keys()

def copy(self):
return self.__class__(
Expand Down
123 changes: 31 additions & 92 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
import pybamm
from pybamm.expression_tree.operations.latexify import Latexify

_EQUATION_ATTRIBUTES = [
"rhs",
"algebraic",
"initial_conditions",
"boundary_conditions",
"variables",
"external_variables",
"events",
"len_rhs",
"len_alg",
"len_rhs_and_alg",
"bounds",
"mass_matrix",
"mass_matrix_inv",
"concatenated_rhs",
"concatenated_algebraic",
"concatenated_initial_conditions",
]


class BaseModel:
"""
Expand Down Expand Up @@ -65,72 +84,28 @@ def name(self):
def name(self, value):
self._name = value

@property
def rhs(self):
return self._equations.rhs

@rhs.setter
def rhs(self, rhs):
self._equations.rhs = rhs

@property
def algebraic(self):
return self._equations.algebraic

@algebraic.setter
def algebraic(self, algebraic):
self._equations.algebraic = algebraic

@property
def initial_conditions(self):
return self._equations.initial_conditions

@initial_conditions.setter
def initial_conditions(self, initial_conditions):
self._equations.initial_conditions = initial_conditions

@property
def boundary_conditions(self):
return self._equations.boundary_conditions

@boundary_conditions.setter
def boundary_conditions(self, boundary_conditions):
self._equations.boundary_conditions = boundary_conditions

@property
def variables(self):
return self._equations.variables
def __getattr__(self, name):
if name in _EQUATION_ATTRIBUTES:
return getattr(self._equations, name)
else:
return self.__getattribute__(name)

@variables.setter
def variables(self, variables):
self._equations.variables = variables
def __setattr__(self, name, value):
if name in _EQUATION_ATTRIBUTES:
self._equations.__setattr__(name, value)
else:
super().__setattr__(name, value)

def variable_names(self):
return list(self._equations._variables.keys())

@property
def external_variables(self):
return self._equations.external_variables

@external_variables.setter
def external_variables(self, variables):
self._equations.external_variables = variables

@property
def variables_and_events(self):
"""
Returns variables and events in a single dictionary
"""
return self._equations.variables_and_events

@property
def events(self):
return self._equations.events

@events.setter
def events(self, events):
self._equations.events = events

@property
def param(self):
return self._param
Expand All @@ -150,7 +125,7 @@ def options(self, options):
@property
def timescale(self):
"""Timescale of model, to be used for non-dimensionalising time when solving"""
return self._equations._timescale
return self._equations.timescale

@timescale.setter
def timescale(self, value):
Expand All @@ -160,7 +135,7 @@ def timescale(self, value):
@property
def length_scales(self):
"Length scales of model"
return self._equations._length_scales
return self._equations.length_scales

@length_scales.setter
def length_scales(self, values):
Expand Down Expand Up @@ -212,42 +187,6 @@ def input_parameters(self):
def print_parameter_info(self):
self._equations.print_parameter_info()

@property
def bounds(self):
return self._equations._bounds

@property
def len_rhs(self):
return self._equations._len_rhs

@property
def len_alg(self):
return self._equations._len_alg

@property
def len_rhs_and_alg(self):
return self._equations._len_rhs_and_alg

@property
def mass_matrix(self):
return self._equations._mass_matrix

@property
def mass_matrix_inv(self):
return self._equations._mass_matrix_inv

@property
def concatenated_rhs(self):
return self._equations._concatenated_rhs

@property
def concatenated_algebraic(self):
return self._equations._concatenated_algebraic

@property
def concatenated_initial_conditions(self):
return self._equations._concatenated_initial_conditions

def new_copy(self, equations=None):
"""
Creates a copy of the model, explicitly copying all the mutable attributes
Expand Down
24 changes: 24 additions & 0 deletions pybamm/models/discretised_equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
from scipy.sparse import block_diag, csc_matrix, csr_matrix
from scipy.sparse.linalg import inv

_READ_ONLY_ATTRIBUTES = [
"len_rhs",
"len_alg",
"len_rhs_and_alg",
"bounds",
"mass_matrix",
"mass_matrix_inv",
"concatenated_rhs",
"concatenated_algebraic",
"concatenated_initial_conditions",
]


class _DiscretisedEquations(pybamm._BaseProcessedEquations):
"""
Expand Down Expand Up @@ -103,5 +115,17 @@ def create_mass_matrix(self):
self._mass_matrix = mass_matrix
self._mass_matrix_inv = rhs_mass_matrix_inv

def __getattr__(self, name):
if name in _READ_ONLY_ATTRIBUTES:
return getattr(self, "_" + name)
else:
return self.__getattribute__(name)

def __setattr__(self, name, value):
if name in _READ_ONLY_ATTRIBUTES:
raise AttributeError("can't set attribute")
else:
super().__setattr__(name, value)

def variables_update_function(self, variable):
return self._discretisation.process_symbol(variable)
2 changes: 1 addition & 1 deletion pybamm/models/full_battery_models/base_battery_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def build_model(self):
self.set_degradation_variables()
self.set_summary_variables()

self._built = True
self._equations._built = True
pybamm.logger.info("Finish building {}".format(self.name))

@property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,3 @@ def __init__(self, options=None, name="Doyle-Fuller-Newman half cell model"):
"Instantaneous power [W.m-2]": i_cell * i_typ * voltage_dim,
"Pore-wall flux [mol.m-2.s-1]": j_w,
}

def new_copy(self, build=False):
new_model = self.__class__(name=self.name, options=self.options)
new_model.use_jacobian = self.use_jacobian
new_model.convert_to_format = self.convert_to_format
new_model._timescale = self.timescale
new_model._length_scales = self.length_scales
return new_model
Loading

0 comments on commit 8d866d5

Please sign in to comment.