Skip to content

Commit

Permalink
Make solvers and SolverPtr deepcopyable (#2245)
Browse files Browse the repository at this point in the history
Make solvers and SolverPtr deepcopyable, and fix a segfault when creating `SolverPtr`s from Python.
  • Loading branch information
dweindl authored Dec 18, 2023
1 parent e213d59 commit ba4f856
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/tests/test_swig_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,19 @@ def test_expdata_and_expdataview_are_deepcopyable():
ev2 = copy.deepcopy(ev1)
assert ev2._swigptr.this != ev1._swigptr.this
assert ev1 == ev2


def test_solvers_are_deepcopyable():
for solver_type in (amici.CVodeSolver, amici.IDASolver):
for solver1 in (solver_type(), amici.SolverPtr(solver_type())):
solver2 = copy.deepcopy(solver1)
assert solver1.this != solver2.this
assert (
solver1.getRelativeTolerance()
== solver2.getRelativeTolerance()
)
solver2.setRelativeTolerance(100 * solver2.getRelativeTolerance())
assert (
solver1.getRelativeTolerance()
!= solver2.getRelativeTolerance()
)
9 changes: 9 additions & 0 deletions swig/solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,18 @@ def __repr__(self):
%pythoncode %{
def __repr__(self):
return _solver_repr(self)

def __deepcopy__(self, memo):
return self.clone()
%}
};

%extend amici::Solver {
%pythoncode %{
def __deepcopy__(self, memo):
return self.clone()
%}
};

%newobject amici::Solver::clone;
// Process symbols in header
Expand Down
3 changes: 3 additions & 0 deletions swig/std_unique_ptr.i
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ namespace std {
struct unique_ptr {
typedef Type* pointer;

%apply SWIGTYPE *DISOWN { pointer Ptr };
explicit unique_ptr( pointer Ptr );
%clear pointer Ptr;
unique_ptr (unique_ptr&& Right);

template<class Type2, Class Del2> unique_ptr( unique_ptr<Type2, Del2>&& Right );
unique_ptr( const unique_ptr& Right) = delete;

Expand Down

0 comments on commit ba4f856

Please sign in to comment.