Skip to content

Commit

Permalink
Serialize DirectionMover
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Oct 25, 2023
1 parent 85f5269 commit 1baf8c6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
15 changes: 14 additions & 1 deletion modules/core/include/DirectionMover.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* \file IMP/core/DirectionMover.h
* \brief A mover that transforms a Direction.
*
* Copyright 2007-2022 IMP Inventors. All rights reserved.
* Copyright 2007-2023 IMP Inventors. All rights reserved.
*
*/

Expand All @@ -16,6 +16,9 @@
#include <IMP/Particle.h>
#include <IMP/Object.h>
#include <IMP/Model.h>
#include <cereal/access.hpp>
#include <cereal/types/base_class.hpp>
#include <cereal/types/polymorphic.hpp>


IMPCORE_BEGIN_NAMESPACE
Expand All @@ -36,12 +39,22 @@ class IMPCOREEXPORT DirectionMover : public MonteCarloMover {
void initialize(ParticleIndex pi, double max_rotation,
double reflect_probability);

friend class cereal::access;

template<class Archive> void serialize(Archive &ar) {
ar(cereal::base_class<MonteCarloMover>(this), last_direction_,
max_angle_, reflect_prob_, pi_);
}
IMP_OBJECT_SERIALIZE_DECL(DirectionMover);

public:
DirectionMover(Model *m, ParticleIndex pi, Float max_rotation,
Float reflect_probability);

DirectionMover(Direction d, Float max_rotation,
Float reflect_probability);

DirectionMover() {}

//! Set the maximum rotation in radians.
void set_maximum_rotation(Float mr);
Expand Down
2 changes: 1 addition & 1 deletion modules/core/pyext/swig.i-in
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ IMP_SWIG_OBJECT_INSTANCE( IMP::core, AttributeSingletonScore, AttributeSingleton
IMP_SWIG_OBJECT_SERIALIZE( IMP::core, BallMover, BallMovers);
IMP_SWIG_OBJECT_SERIALIZE( IMP::core, SerialMover, SerialMovers);
IMP_SWIG_OBJECT_SERIALIZE( IMP::core, SubsetMover, SubsetMovers);
IMP_SWIG_OBJECT( IMP::core, DirectionMover, DirectionMovers);
IMP_SWIG_OBJECT_SERIALIZE( IMP::core, DirectionMover, DirectionMovers);
IMP_SWIG_OBJECT( IMP::core, SurfaceMover, SurfaceMovers);
IMP_SWIG_OBJECT_INSTANCE( IMP::core, BoundingBox3DSingletonScore, BoundingBox3DSingletonScore, BoundingBox3DSingletonScores);
IMP_SWIG_OBJECT_INSTANCE( IMP::core, BoundingSphere3DSingletonScore, BoundingSphere3DSingletonScore, BoundingSphere3DSingletonScores);
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/DirectionMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ ModelObjectsTemp DirectionMover::do_get_inputs() const {
return ParticlesTemp(1, get_model()->get_particle(pi_));
}

IMP_OBJECT_SERIALIZE_IMPL(IMP::core::DirectionMover);

IMPCORE_END_NAMESPACE
30 changes: 30 additions & 0 deletions modules/core/test/test_direction_mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import IMP.core
import IMP.atom
import IMP.test
import pickle


class Tests(IMP.test.TestCase):
Expand Down Expand Up @@ -62,6 +63,35 @@ def test_inputs(self):
self.assertSetEqual(set([d.get_particle()]), set(mv.get_inputs()))
mv.set_was_used(True)

def test_pickle(self):
"""Test (un-)pickle of DirectionMover"""
m = IMP.Model()
d = IMP.core.Direction.setup_particle(IMP.Particle(m),
IMP.algebra.Vector3D(0, 0, 1))
d.set_direction_is_optimized(True)
mvr = IMP.core.DirectionMover(d, .1, 1.)
mvr.set_name("foo")
dump = pickle.dumps(mvr)

newmvr = pickle.loads(dump)
self.assertEqual(newmvr.get_name(), "foo")
self.assertSetEqual(set([d.get_particle()]), set(newmvr.get_inputs()))

def test_pickle_polymorphic(self):
"""Test (un-)pickle of DirectionMover via polymorphic pointer"""
m = IMP.Model()
d = IMP.core.Direction.setup_particle(IMP.Particle(m),
IMP.algebra.Vector3D(0, 0, 1))
d.set_direction_is_optimized(True)
mvr = IMP.core.DirectionMover(d, .1, 1.)
mvr.set_name("foo")
sm = IMP.core.SerialMover([mvr])
dump = pickle.dumps(sm)

newsm = pickle.loads(dump)
newmvr, = newsm.get_movers()
self.assertEqual(newmvr.get_name(), "foo")


if __name__ == '__main__':
IMP.test.main()

0 comments on commit 1baf8c6

Please sign in to comment.