Skip to content

Commit

Permalink
bug: make sure symbol always has required mesh attribute (#4644)
Browse files Browse the repository at this point in the history
* bug: make sure symbol always has required mesh attribute

* #4637 add to changelog
  • Loading branch information
martinjrobins authored Dec 3, 2024
1 parent 3bf7403 commit 879e746
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Bug fixes

- Remove internal use of deprecated `set_parameters` function in the `Simulation` class which caused warnings. ([#4638](https://github.com/pybamm-team/PyBaMM/pull/4638))
- Provide default value for `Symbol.mesh` attribute to avoid errors when adding variables after discretisation. ([#4644](https://github.com/pybamm-team/PyBaMM/pull/4644))

# [v24.11.2](https://github.com/pybamm-team/PyBaMM/tree/v24.11.2) - 2024-11-27

Expand Down
4 changes: 1 addition & 3 deletions docs/source/examples/notebooks/models/pouch-cell-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -311,8 +311,6 @@
" pybamm.t,\n",
" name=\"voltage_comsol\",\n",
")\n",
"comsol_voltage.mesh = None\n",
"comsol_voltage.secondary_mesh = None\n",
"comsol_phi_s_cn = get_interp_fun_curr_coll(\"phi_s_cn\")\n",
"comsol_phi_s_cp = get_interp_fun_curr_coll(\"phi_s_cp\")\n",
"comsol_current = get_interp_fun_curr_coll(\"current\")\n",
Expand Down
3 changes: 0 additions & 3 deletions examples/scripts/compare_comsol/compare_comsol_DFN.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ def get_interp_fun(variable_name, domain):
comsol_t, np.array(comsol_variables["voltage"]), pybamm.t
)

comsol_voltage.mesh = None
comsol_voltage.secondary_mesh = None

# Create comsol model with dictionary of Matrix variables
comsol_model = pybamm.lithium_ion.BaseModel()
comsol_model._geometry = pybamm_model.default_geometry
Expand Down
3 changes: 3 additions & 0 deletions src/pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def __init__(
# Set domains (and hence id)
self.domains = self.read_domain_or_domains(domain, auxiliary_domains, domains)

# mesh required for solution and processed variables classes
self.mesh = None

self._saved_evaluates_on_edges: dict = {}
self._print_name = None

Expand Down
2 changes: 0 additions & 2 deletions src/pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ def generic_deserialise(cls, instance, properties):
for var in instance._variables.values():
if var.domain != []:
var.mesh = properties["mesh"][var.domain]
else:
var.mesh = None

if var.domains["secondary"] != []:
var.secondary_mesh = properties["mesh"][var.domains["secondary"]]
Expand Down
16 changes: 0 additions & 16 deletions tests/unit/test_solvers/test_processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def test_processed_variable_0D(self, hermite_interp):
t = pybamm.t
y = pybamm.StateVector(slice(0, 1))
var = t * y
var.mesh = None
model = pybamm.BaseModel()
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
Expand All @@ -124,7 +123,6 @@ def test_processed_variable_0D(self, hermite_interp):

# scalar value
var = y
var.mesh = None
t_sol = np.array([0])
y_sol = np.array([1])[:, np.newaxis]
yp_sol = np.array([1])[:, np.newaxis]
Expand Down Expand Up @@ -180,7 +178,6 @@ def test_processed_variable_0D_discrete_data(self, hermite_interp):
data = pybamm.DiscreteTimeData(data_t, data_v, "test_data")
var = (y - data) ** order
expected_entries = (y_sol - data_v) ** order
var.mesh = None
model = pybamm.BaseModel()
var_casadi = to_casadi(var, y_sol)
processed_var = pybamm.process_variable(
Expand All @@ -206,7 +203,6 @@ def test_processed_variable_0D_discrete_data(self, hermite_interp):
var = (y - data) ** order
expected = (y_sol_interp - data_v) ** order
expected_entries = (y_sol - data_v_interp) ** order
var.mesh = None
model = pybamm.BaseModel()
var_casadi = to_casadi(var, y_sol)
processed_var = pybamm.process_variable(
Expand All @@ -227,7 +223,6 @@ def test_processed_variable_0D_no_sensitivity(self, hermite_interp):
t = pybamm.t
y = pybamm.StateVector(slice(0, 1))
var = t * y
var.mesh = None
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
yp_sol = self._get_yps(y_sol, hermite_interp)
Expand All @@ -246,7 +241,6 @@ def test_processed_variable_0D_no_sensitivity(self, hermite_interp):
y = pybamm.StateVector(slice(0, 1))
a = pybamm.InputParameter("a")
var = t * y * a
var.mesh = None
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
inputs = {"a": np.array([1.0])}
Expand Down Expand Up @@ -593,8 +587,6 @@ def test_processed_var_0D_interpolation(self, hermite_interp):
y = pybamm.StateVector(slice(0, 1))
var = y
eqn = t * y
var.mesh = None
eqn.mesh = None

t_sol = np.linspace(0, 1, 1000)
y_sol = np.array([5 * t_sol])
Expand Down Expand Up @@ -631,10 +623,7 @@ def test_processed_var_0D_interpolation(self, hermite_interp):
@pytest.mark.parametrize("hermite_interp", _hermite_args)
def test_processed_var_0D_fixed_t_interpolation(self, hermite_interp):
y = pybamm.StateVector(slice(0, 1))
var = y
eqn = 2 * y
var.mesh = None
eqn.mesh = None

t_sol = np.array([10])
y_sol = np.array([[100]])
Expand Down Expand Up @@ -1191,7 +1180,6 @@ def test_process_spatial_variable_names(self):
t = pybamm.t
y = pybamm.StateVector(slice(0, 1))
var = t * y
var.mesh = None
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
var_casadi = to_casadi(var, y_sol)
Expand Down Expand Up @@ -1235,12 +1223,8 @@ def solution_setup(t_sol, sign):
return sol

# without spatial dependence
t = pybamm.t
y = pybamm.StateVector(slice(0, 1))
var = y
eqn = t * y
var.mesh = None
eqn.mesh = None

sign1 = +1
t_sol1 = np.linspace(0, 1, 100)
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_solvers/test_processed_variable_computed.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def test_processed_variable_0D(self):
# without space
y = pybamm.StateVector(slice(0, 1))
var = y
var.mesh = None
t_sol = np.array([0])
y_sol = np.array([1])[:, np.newaxis]
var_casadi = to_casadi(var, y_sol)
Expand Down Expand Up @@ -117,7 +116,6 @@ def test_processed_variable_0D_no_sensitivity(self):
t = pybamm.t
y = pybamm.StateVector(slice(0, 1))
var = t * y
var.mesh = None
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
var_casadi = to_casadi(var, y_sol)
Expand All @@ -136,7 +134,6 @@ def test_processed_variable_0D_no_sensitivity(self):
y = pybamm.StateVector(slice(0, 1))
a = pybamm.InputParameter("a")
var = t * y * a
var.mesh = None
t_sol = np.linspace(0, 1)
y_sol = np.array([np.linspace(0, 5)])
inputs = {"a": np.array([1.0])}
Expand Down

0 comments on commit 879e746

Please sign in to comment.