def test_write_trajectory(self): traj_fname = os.path.join(self.dir, "traj.h5") simulation = Simulation() simulation.set_kernel("SingleCPU") simulation.box_size = common.Vec(5, 5, 5) simulation.register_particle_type("A", 0.0) simulation.register_reaction_conversion("A->A", "A", "A", 1.) def callback(_): simulation.add_particle("A", common.Vec(0, 0, 0)) simulation.register_observable_n_particles(1, ["A"], callback) traj_handle = simulation.register_observable_trajectory(0) with closing(io.File.create(traj_fname, io.FileFlag.OVERWRITE)) as f: traj_handle.enable_write_to_file(f, u"", 3) simulation.run_scheme_readdy(True).write_config_to_file( f).configure(1).run(20) r = TrajectoryReader(traj_fname) trajectory_items = r[:] for idx, items in enumerate(trajectory_items): np.testing.assert_equal(len(items), idx + 1) for item in items: np.testing.assert_equal(item.t, idx) np.testing.assert_equal(item.position, np.array([.0, .0, .0])) with h5py.File(traj_fname) as f: np.testing.assert_equal( "A", f["readdy/config/particle_types"][0]["name"])
def test_write_trajectory_as_observable(self): common.set_logging_level("error") traj_fname = os.path.join(self.dir, "traj_as_obs.h5") simulation = Simulation() simulation.set_kernel("SingleCPU") simulation.box_size = common.Vec(5, 5, 5) simulation.register_particle_type("A", 0.0, 0.0) def callback(_): simulation.add_particle("A", common.Vec(0, 0, 0)) simulation.register_observable_n_particles(1, ["A"], callback) traj_handle = simulation.register_observable_trajectory(1) with closing( io.File(traj_fname, io.FileAction.CREATE, io.FileFlag.OVERWRITE)) as f: traj_handle.enable_write_to_file(f, u"", int(3)) simulation.run_scheme_readdy(True).configure(1).run(20) r = TrajectoryReader(traj_fname) trajectory_items = r[:] for idx, items in enumerate(trajectory_items): np.testing.assert_equal(len(items), idx + 1) for item in items: np.testing.assert_equal(item.t, idx) np.testing.assert_equal(item.position, np.array([.0, .0, .0]))
def test_write_trajectory(self): traj_fname = os.path.join(self.dir, "traj.h5") context = Context() context.box_size = [5., 5., 5.] context.particle_types.add("A", 0.0) context.reactions.add_conversion("A->A", "A", "A", 1.) simulation = Simulation("SingleCPU", context) def callback(_): simulation.add_particle("A", common.Vec(0, 0, 0)) simulation.register_observable_n_particles(1, ["A"], callback) traj_handle = simulation.register_observable_trajectory(0) with closing(io.File.create(traj_fname, io.FileFlag.OVERWRITE)) as f: traj_handle.enable_write_to_file(f, u"", 3) loop = simulation.create_loop(1.) loop.write_config_to_file(f) loop.run(20) r = TrajectoryReader(traj_fname) trajectory_items = r[:] for idx, items in enumerate(trajectory_items): np.testing.assert_equal(len(items), idx + 1) for item in items: np.testing.assert_equal(item.t, idx) np.testing.assert_equal(item.position, np.array([.0, .0, .0])) with h5py.File(traj_fname, 'r') as f: np.testing.assert_equal( "A", f["readdy/config/particle_types"][0]["name"])
def test_write_trajectory(self): traj_fname = os.path.join(self.dir, "traj.h5") simulation = Simulation() simulation.set_kernel("SingleCPU") simulation.box_size = common.Vec(5,5,5) simulation.register_particle_type("A", 0.0) simulation.register_reaction_conversion("A->A", "A", "A", 1.) def callback(_): simulation.add_particle("A", common.Vec(0, 0, 0)) simulation.register_observable_n_particles(1, ["A"], callback) traj_handle = simulation.register_observable_trajectory(0) with closing(io.File.create(traj_fname, io.FileFlag.OVERWRITE)) as f: traj_handle.enable_write_to_file(f, u"", 3) simulation.run_scheme_readdy(True).write_config_to_file(f).configure(1).run(20) r = TrajectoryReader(traj_fname) trajectory_items = r[:] for idx, items in enumerate(trajectory_items): np.testing.assert_equal(len(items), idx+1) for item in items: np.testing.assert_equal(item.t, idx) np.testing.assert_equal(item.position, np.array([.0, .0, .0])) with h5py.File(traj_fname) as f: np.testing.assert_equal("A", f["readdy/config/particle_types"][0]["name"])
def test_write_trajectory_as_observable(self): traj_fname = os.path.join(self.dir, "traj_as_obs.h5") simulation = Simulation("SingleCPU") simulation.context.box_size = [5., 5., 5.] simulation.context.particle_types.add("A", 0.0) def callback(_): simulation.add_particle("A", common.Vec(0, 0, 0)) simulation.register_observable_n_particles(1, ["A"], callback) traj_handle = simulation.register_observable_trajectory(1) with closing(io.File.create(traj_fname, io.FileFlag.OVERWRITE)) as f: traj_handle.enable_write_to_file(f, u"", int(3)) simulation.run(20, 1) r = TrajectoryReader(traj_fname) trajectory_items = r[:] for idx, items in enumerate(trajectory_items): np.testing.assert_equal(len(items), idx+1) for item in items: np.testing.assert_equal(item.t, idx) np.testing.assert_equal(item.position, np.array([.0, .0, .0]))