|
5 | 5 | import warnings |
6 | 6 | from collections import OrderedDict |
7 | 7 |
|
| 8 | +import copy |
8 | 9 | import casadi |
9 | 10 | import numpy as np |
10 | 11 |
|
@@ -366,35 +367,21 @@ def __getitem__(self, key): |
366 | 367 | def default_quick_plot_variables(self): |
367 | 368 | return None |
368 | 369 |
|
369 | | - def new_empty_copy(self): |
370 | | - """ |
371 | | - Create an empty copy of the model with the same name and "parameters" |
372 | | - (convert_to_format, etc), but empty equations and variables. |
373 | | - This is usually then called by :class:`pybamm.ParameterValues`, |
374 | | - :class:`pybamm.Discretisation`, or :class:`pybamm.SymbolReplacer`. |
375 | | - """ |
376 | | - new_model = self.__class__(name=self.name) |
377 | | - new_model.use_jacobian = self.use_jacobian |
378 | | - new_model.convert_to_format = self.convert_to_format |
379 | | - new_model._timescale = self.timescale |
380 | | - new_model._length_scales = self.length_scales |
381 | | - |
382 | | - # Variables from discretisation |
383 | | - new_model.is_discretised = self.is_discretised |
384 | | - new_model.y_slices = self.y_slices |
385 | | - new_model.concatenated_rhs = self.concatenated_rhs |
386 | | - new_model.concatenated_algebraic = self.concatenated_algebraic |
387 | | - new_model.concatenated_initial_conditions = self.concatenated_initial_conditions |
388 | | - |
389 | | - return new_model |
390 | | - |
391 | 370 | def new_copy(self): |
392 | 371 | """ |
393 | | - Creates an identical copy of the model, using the functionality of |
394 | | - :class:`pybamm.SymbolReplacer` but without performing any replacements |
| 372 | + Creates a copy of the model, explicitly copying all the mutable attributes |
| 373 | + to avoid issues with shared objects. |
395 | 374 | """ |
396 | | - replacer = pybamm.SymbolReplacer({}) |
397 | | - return replacer.process_model(self, inplace=False) |
| 375 | + new_model = copy.copy(self) |
| 376 | + new_model._rhs = self.rhs.copy() |
| 377 | + new_model._algebraic = self.algebraic.copy() |
| 378 | + new_model._initial_conditions = self.initial_conditions.copy() |
| 379 | + new_model._boundary_conditions = self.boundary_conditions.copy() |
| 380 | + new_model._variables = self.variables.copy() |
| 381 | + new_model._events = self.events.copy() |
| 382 | + new_model.external_variables = self.external_variables.copy() |
| 383 | + new_model._variables_casadi = self._variables_casadi.copy() |
| 384 | + return new_model |
398 | 385 |
|
399 | 386 | def update(self, *submodels): |
400 | 387 | """ |
@@ -435,13 +422,7 @@ def set_initial_conditions_from(self, solution, inplace=True): |
435 | 422 | if inplace is True: |
436 | 423 | model = self |
437 | 424 | else: |
438 | | - model = self.new_empty_copy() |
439 | | - model.rhs = self.rhs.copy() |
440 | | - model.algebraic = self.algebraic.copy() |
441 | | - model.initial_conditions = self.initial_conditions.copy() |
442 | | - model.boundary_conditions = self.boundary_conditions.copy() |
443 | | - model.variables = self.variables.copy() |
444 | | - model.events = self.events.copy() |
| 425 | + model = self.new_copy() |
445 | 426 |
|
446 | 427 | if isinstance(solution, pybamm.Solution): |
447 | 428 | solution = solution.last_state |
|
0 commit comments