def test_particle_positions_observable(self): fname = os.path.join(self.dir, "test_observables_particle_positions.h5") context = Context() context.box_size = [13., 13., 13.] context.particle_types.add("A", .1) sim = Simulation("SingleCPU", context) sim.add_particle("A", common.Vec(0, 0, 0)) # every time step, add one particle sim.register_observable_n_particles( 1, ["A"], lambda n: sim.add_particle("A", common.Vec(1.5, 2.5, 3.5))) handle = sim.register_observable_particle_positions(1, []) n_timesteps = 19 with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"particle_positions", int(3)) sim.run(n_timesteps, 0) handle.flush() with h5py.File(fname, "r") as f2: data = f2["readdy/observables/particle_positions/data"][:] np.testing.assert_equal(len(data), n_timesteps + 1) for t, positions in enumerate(data): # we begin with two particles np.testing.assert_equal(len(positions), t + 2) np.testing.assert_equal(positions[0]["x"], 0) np.testing.assert_equal(positions[0]["y"], 0) np.testing.assert_equal(positions[0]["z"], 0) for i in range(1, len(positions)): np.testing.assert_equal(positions[i]["x"], 1.5) np.testing.assert_equal(positions[i]["y"], 2.5) np.testing.assert_equal(positions[i]["z"], 3.5)
def test_virial_observable_SCPU(self): fname = os.path.join(self.dir, "test_observables_virial_scpu.h5") context = Context() context.box_size = [13., 13., 13.] context.particle_types.add("A", .1) context.potentials.add_harmonic_repulsion("A", "A", 10., .5) sim = Simulation("SingleCPU", context) for _ in range(10000): pos = common.Vec(*(13 * np.random.random(size=3) - .5 * 13)) sim.add_particle("A", pos) virials = [] def virial_callback(virial): virials.append(np.ndarray((3, 3), buffer=virial)) handle = sim.register_observable_virial(1, virial_callback) with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"virial", int(3)) sim.run(10, .1) handle.flush() with h5py.File(fname, "r") as f2: h5virials = f2["readdy/observables/virial/data"] for v, v2 in zip(virials, h5virials): np.testing.assert_almost_equal(v, v2)
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_unbonded_edge(self): context = Context() context.box_size = [10., 10., 10.] context.topologies.add_type("TA") context.particle_types.add("T", 1.0, flavor=ParticleTypeFlavor.TOPOLOGY) context.particle_types.add("D", 1.0, flavor=ParticleTypeFlavor.TOPOLOGY) context.topologies.configure_bond_potential( "T", "T", BondedPotentialConfiguration(10., 11., "harmonic")) sim = Simulation("SingleCPU", context) np.testing.assert_equal(sim.kernel_supports_topologies(), True) particles = [ sim.create_topology_particle("T", common.Vec(0, 0, 0)) for _ in range(3) ] particles.append(sim.create_topology_particle("D", common.Vec(0, 0, 0))) top = sim.add_topology("TA", particles) graph = top.get_graph() graph.add_edge(0, 1) graph.add_edge(1, 2) graph.add_edge(2, 3) with (np.testing.assert_raises(ValueError)): top.configure()
def test_reaction_counts_observable(self): fname = os.path.join(self.dir, "test_observables_particle_reaction_counts.h5") context = Context() context.box_size = [10., 10., 10.] context.particle_types.add("A", .0) context.particle_types.add("B", .0) context.particle_types.add("C", .0) context.reactions.add_conversion("mylabel", "A", "B", .00001) context.reactions.add_conversion("A->B", "A", "B", 1e16) context.reactions.add_fusion("B+C->A", "B", "C", "A", 1e16, 1.0, .5, .5) sim = Simulation("CPU", context) sim.add_particle("A", common.Vec(0, 0, 0)) sim.add_particle("B", common.Vec(1.0, 1.0, 1.0)) sim.add_particle("C", common.Vec(1.1, 1.0, 1.0)) n_timesteps = 1 handle = sim.register_observable_reaction_counts(1) with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"reactions", int(3)) loop = sim.create_loop(1) loop.use_reaction_scheduler("Gillespie") loop.write_config_to_file(f) loop.run(1) import readdy.util.io_utils as io_utils reactions = io_utils.get_reactions(fname) with h5py.File(fname, "r") as f2: data = f2["readdy/observables/reactions"] time_series = f2["readdy/observables/reactions/time"] np.testing.assert_equal(time_series, np.array(range(0, n_timesteps + 1))) def get_item(name, collection): return next(x for x in collection if x["name"] == name) mylabel_id = get_item("mylabel", reactions.values())["id"] atob_id = get_item("A->B", reactions.values())["id"] fusion_id = get_item("B+C->A", reactions.values())["id"] # counts of first time step, time is first index np.testing.assert_equal(data["counts/" + str(mylabel_id)][0], np.array([0])) np.testing.assert_equal(data["counts/" + str(atob_id)][0], np.array([0])) np.testing.assert_equal(data["counts/" + str(fusion_id)][0], np.array([0])) # counts of second time step np.testing.assert_equal(data["counts/" + str(mylabel_id)][1], np.array([0])) np.testing.assert_equal(data["counts/" + str(atob_id)][1], np.array([1])) np.testing.assert_equal(data["counts/" + str(fusion_id)][1], np.array([1]))
def test_sanity(self): context = Context() context.box_size = [10., 10., 10.] context.topologies.add_type("TA") context.particle_types.add("T", 1.0, flavor=ParticleTypeFlavor.TOPOLOGY) context.topologies.configure_bond_potential( "T", "T", BondedPotentialConfiguration(10., 11., "harmonic")) sim = Simulation("SingleCPU", context) np.testing.assert_equal(sim.kernel_supports_topologies(), True) particles = [ sim.create_topology_particle("T", common.Vec(x, 0, 0)) for x in range(4) ] top = sim.add_topology("TA", particles) graph = top.graph graph.add_edge(0, 1) graph.add_edge(1, 2) graph.add_edge(2, 3) np.testing.assert_equal(len(graph.get_vertices()), 4) for v in graph.vertices: if v.particle_index == 0: np.testing.assert_equal(top.position_of_vertex(v), common.Vec(0, 0, 0)) np.testing.assert_equal(len(v.neighbors()), 1) np.testing.assert_equal( 1 in [vv.get().particle_index for vv in v], True) if v.particle_index == 1: np.testing.assert_equal(top.position_of_vertex(v), common.Vec(1, 0, 0)) np.testing.assert_equal(len(v.neighbors()), 2) np.testing.assert_equal( 0 in [vv.get().particle_index for vv in v], True) np.testing.assert_equal( 2 in [vv.get().particle_index for vv in v], True) if v.particle_index == 2: np.testing.assert_equal(top.position_of_vertex(v), common.Vec(2, 0, 0)) np.testing.assert_equal(len(v.neighbors()), 2) np.testing.assert_equal( 1 in [vv.get().particle_index for vv in v], True) np.testing.assert_equal( 3 in [vv.get().particle_index for vv in v], True) if v.particle_index == 3: np.testing.assert_equal(top.position_of_vertex(v), common.Vec(3, 0, 0)) np.testing.assert_equal(len(v.neighbors()), 1) np.testing.assert_equal( 2 in [vv.get().particle_index for vv in v], True) top.configure() sim.run(0, 1)
def test_n_particles_observable(self): fname = os.path.join(self.dir, "test_observables_n_particles.h5") context = Context() box_size = [10., 10., 10.] context.kbt = 2 context.pbc = [True, True, True] context.box_size = box_size context.particle_types.add("A", .2) context.particle_types.add("B", .2) simulation = Simulation("SingleCPU", context) simulation.add_particle("A", common.Vec(-2.5, 0, 0)) simulation.add_particle("B", common.Vec(0, 0, 0)) n_time_steps = 50 callback_n_particles_a_b = [] callback_n_particles_all = [] def callback_ab(value): callback_n_particles_a_b.append(value) simulation.add_particle("A", common.Vec(-1, -1, -1)) def callback_all(hist): callback_n_particles_all.append(hist) simulation.add_particle("A", common.Vec(-1, -1, -1)) simulation.add_particle("B", common.Vec(-1, -1, -1)) handle_a_b_particles = simulation.register_observable_n_particles( 1, ["A", "B"], callback_ab) handle_all = simulation.register_observable_n_particles( 1, [], callback_all) with closing(io.File.create(fname)) as f: handle_a_b_particles.enable_write_to_file(f, u"n_a_b_particles", int(3)) handle_all.enable_write_to_file(f, u"n_particles", int(5)) simulation.run(n_time_steps, 0.02) handle_all.flush() handle_a_b_particles.flush() with h5py.File(fname, "r") as f2: n_a_b_particles = f2["readdy/observables/n_a_b_particles/data"][:] n_particles = f2["readdy/observables/n_particles/data"][:] time_series = f2["readdy/observables/n_a_b_particles/time"] np.testing.assert_equal(time_series, np.array(range(0, n_time_steps + 1))) for t in range(n_time_steps): np.testing.assert_equal(n_a_b_particles[t][0], callback_n_particles_a_b[t][0]) np.testing.assert_equal(n_a_b_particles[t][1], callback_n_particles_a_b[t][1]) np.testing.assert_equal(n_particles[t][0], callback_n_particles_all[t][0])
def test_particles_observable(self): fname = os.path.join(self.dir, "test_observables_particles.h5") context = Context() context.box_size = [13., 13., 13.] context.particle_types.add("A", .1) context.particle_types.add("B", .1) sim = Simulation("SingleCPU", context) sim.add_particle("A", common.Vec(0, 0, 0)) sim.add_particle("B", common.Vec(0, 0, 0)) # every time step, add one particle sim.register_observable_n_particles( 1, ["A"], lambda n: sim.add_particle("A", common.Vec(1.5, 2.5, 3.5))) handle = sim.register_observable_particles(1) n_timesteps = 19 with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"particles", int(3)) loop = sim.create_loop(0) loop.write_config_to_file(f) loop.run(n_timesteps) handle.flush() from readdy.util.io_utils import get_particle_types particle_types = get_particle_types(fname) with h5py.File(fname, "r") as f2: types = f2["readdy/observables/particles/types"][:] ids = f2["readdy/observables/particles/ids"][:] positions = f2["readdy/observables/particles/positions"][:] for t in range(n_timesteps): np.testing.assert_equal(len(types[t]), t + 3) np.testing.assert_equal(len(ids[t]), t + 3) np.testing.assert_equal(len(positions[t]), t + 3) np.testing.assert_equal(types[t][0], particle_types["A"]["type_id"]) np.testing.assert_equal(positions[t][0][0], 0) np.testing.assert_equal(positions[t][0][1], 0) np.testing.assert_equal(positions[t][0][2], 0) np.testing.assert_equal(positions[t][1][0], 0) np.testing.assert_equal(positions[t][1][1], 0) np.testing.assert_equal(positions[t][1][2], 0) np.testing.assert_equal(types[t][1], particle_types["B"]["type_id"]) for others in range(2, len(types[t])): np.testing.assert_equal(types[t][others], particle_types["A"]["type_id"]) np.testing.assert_equal(positions[t][others][0], 1.5) np.testing.assert_equal(positions[t][others][1], 2.5) np.testing.assert_equal(positions[t][others][2], 3.5)
def test_radial_distribution_observable(self): fname = os.path.join(self.dir, "test_observables_radial_distribution.h5") context = Context() context.kbt = 2 context.pbc = [True, True, True] box_size = [10., 10., 10.] context.box_size = box_size context.particle_types.add("A", .2) context.particle_types.add("B", .2) context.potentials.add_harmonic_repulsion("A", "B", 10, 2.) simulation = Simulation("SingleCPU", context) simulation.add_particle("A", common.Vec(-2.5, 0, 0)) simulation.add_particle("B", common.Vec(0, 0, 0)) bin_borders = np.arange(0, 5, .01) density = 1. / (box_size[0] * box_size[1] * box_size[2]) n_time_steps = 50 callback_centers = [] callback_rdf = [] def rdf_callback(pair): callback_centers.append(pair[0]) callback_rdf.append(pair[1]) handle = simulation.register_observable_radial_distribution( 1, bin_borders, ["A"], ["B"], density, rdf_callback) with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"radial_distribution", int(3)) simulation.run(n_time_steps, 0.02) handle.flush() with h5py.File(fname, "r") as f2: bin_centers = f2[ "readdy/observables/radial_distribution/bin_centers"][:] distribution = f2[ "readdy/observables/radial_distribution/distribution"][:] for t in range(n_time_steps): np.testing.assert_equal(bin_centers, np.array(callback_centers[t])) np.testing.assert_equal(distribution[t], np.array(callback_rdf[t]))
def chain_decay(self, kernel): context = Context() context.box_size = [10., 10., 10.] context.topologies.add_type("TA") context.particle_types.add("B", 1.0, ParticleTypeFlavor.NORMAL) context.particle_types.add("Topology A", 1.0, ParticleTypeFlavor.TOPOLOGY) context.topologies.configure_bond_potential( "Topology A", "Topology A", BondedPotentialConfiguration(10, 10, "harmonic")) context.topologies.add_structural_reaction("TA", self._get_decay_reaction()) context.topologies.add_structural_reaction("TA", self._get_split_reaction()) sim = Simulation(kernel, context) np.testing.assert_equal(sim.kernel_supports_topologies(), True) n_elements = 50. particles = [ sim.create_topology_particle( "Topology A", common.Vec(-5. + i * 10. / n_elements, 0, 0)) for i in range(int(n_elements)) ] topology = sim.add_topology("TA", particles) for i in range(int(n_elements - 1)): topology.get_graph().add_edge(i, i + 1) # h = sim.register_observable_n_particles(1, [], lambda x: print("n particles=%s" % x)) np.testing.assert_equal(1, len(sim.current_topologies)) sim.run(500, 1.) np.testing.assert_equal(0, len(sim.current_topologies))
def test_histogram_along_axis_observable(self): fname = os.path.join(self.dir, "test_observables_hist_along_axis.h5") context = Context() context.kbt = 2 context.pbc = [True, True, True] box_size = [10., 10., 10.] context.box_size = box_size context.particle_types.add("A", .2) context.particle_types.add("B", .2) context.potentials.add_harmonic_repulsion("A", "B", 10, 2.) simulation = Simulation("SingleCPU", context) simulation.add_particle("A", common.Vec(-2.5, 0, 0)) simulation.add_particle("B", common.Vec(0, 0, 0)) bin_borders = np.arange(0, 5, .01) n_time_steps = 50 callback_hist = [] def hist_callback(hist): callback_hist.append(hist) handle = simulation.register_observable_histogram_along_axis( 2, bin_borders, 0, ["A", "B"], hist_callback) with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"hist_along_x_axis", int(3)) simulation.run(n_time_steps, 0.02) handle.flush() with h5py.File(fname, "r") as f2: histogram = f2["readdy/observables/hist_along_x_axis/data"][:] time_series = f2["readdy/observables/hist_along_x_axis/time"] np.testing.assert_equal(time_series, np.array(range(0, n_time_steps + 1))[::2]) for t in range(n_time_steps // 2): np.testing.assert_equal(histogram[t], np.array(callback_hist[t]))
def test_write_trajectory_as_observable(self): traj_fname = os.path.join(self.dir, "traj_as_obs.h5") context = Context() context.box_size = [5., 5., 5.] context.particle_types.add("A", 0.0) 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(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]))
def test_reactions_observable(self): fname = os.path.join(self.dir, "test_observables_particle_reactions.h5") context = Context() context.box_size = [10., 10., 10.] context.particle_types.add("A", .0) context.particle_types.add("B", .0) context.particle_types.add("C", .0) context.reactions.add_conversion("mylabel", "A", "B", .00001) context.reactions.add_conversion("A->B", "A", "B", 1.) context.reactions.add_fusion("B+C->A", "B", "C", "A", 1.0, 1.0, .5, .5) sim = Simulation("CPU", context) sim.add_particle("A", common.Vec(0, 0, 0)) sim.add_particle("B", common.Vec(1.0, 1.0, 1.0)) sim.add_particle("C", common.Vec(1.1, 1.0, 1.0)) n_timesteps = 1 handle = sim.register_observable_reactions(1) with closing(io.File.create(fname)) as f: handle.enable_write_to_file(f, u"reactions", int(3)) loop = sim.create_loop(1) loop.write_config_to_file(f) loop.run(n_timesteps) type_str_to_id = { k: x["type_id"] for k, x in ioutils.get_particle_types(fname).items() } with h5py.File(fname, "r") as f2: data = f2["readdy/observables/reactions"] time_series = f2["readdy/observables/reactions/time"] np.testing.assert_equal(time_series, np.array(range(0, n_timesteps + 1))) def get_item(name, collection): return next(x for x in collection if x["name"] == name) import readdy.util.io_utils as io_utils reactions = io_utils.get_reactions(fname) mylabel_reaction = get_item("mylabel", reactions.values()) np.testing.assert_allclose(mylabel_reaction["rate"], .00001) np.testing.assert_equal(mylabel_reaction["n_educts"], 1) np.testing.assert_equal(mylabel_reaction["n_products"], 1) np.testing.assert_equal(mylabel_reaction["educt_types"], [type_str_to_id["A"], 0]) np.testing.assert_equal(mylabel_reaction["product_types"], [type_str_to_id["B"], 0]) atob_reaction = get_item("A->B", reactions.values()) np.testing.assert_equal(atob_reaction["rate"], 1.) np.testing.assert_equal(atob_reaction["n_educts"], 1) np.testing.assert_equal(atob_reaction["n_products"], 1) np.testing.assert_equal(mylabel_reaction["educt_types"], [type_str_to_id["A"], 0]) np.testing.assert_equal(mylabel_reaction["product_types"], [type_str_to_id["B"], 0]) fusion_reaction = get_item("B+C->A", reactions.values()) np.testing.assert_equal(fusion_reaction["rate"], 1.) np.testing.assert_equal(fusion_reaction["educt_distance"], 1.) np.testing.assert_equal(fusion_reaction["n_educts"], 2) np.testing.assert_equal(fusion_reaction["n_products"], 1) np.testing.assert_equal(fusion_reaction["educt_types"], [type_str_to_id["B"], type_str_to_id["C"]]) np.testing.assert_equal(fusion_reaction["product_types"], [type_str_to_id["A"], 0]) records = data["records"][:] np.testing.assert_equal(len(records), 2) # records of 1st time step for record in records[1]: np.testing.assert_equal( record["reaction_type"] == 0 or record["reaction_type"] == 1, True) if record["reaction_type"] == 0: np.testing.assert_equal(record["position"], np.array([.0, .0, .0])) np.testing.assert_equal(record["reaction_id"], atob_reaction["id"]) elif record["reaction_type"] == 1: # fusion np.testing.assert_allclose(record["position"], np.array([1.05, 1.0, 1.0])) np.testing.assert_equal(record["reaction_id"], fusion_reaction["id"])