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_reaction_counts_observable(self): fname = os.path.join(self.dir, "test_observables_particle_reaction_counts.h5") sim = Simulation("CPU") sim.context.box_size = [10., 10., 10.] sim.context.particle_types.add("A", .0) sim.context.particle_types.add("B", .0) sim.context.particle_types.add("C", .0) sim.context.reactions.add_conversion("mylabel", "A", "B", .00001) sim.context.reactions.add_conversion("A->B", "A", "B", 1e16) sim.context.reactions.add_fusion("B+C->A", "B", "C", "A", 1e16, 1.0, .5, .5) 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_reaction_counts_observable(self): common.set_logging_level("warn") fname = os.path.join(self.dir, "test_observables_particle_reaction_counts.h5") sim = Simulation() sim.set_kernel("CPU") sim.box_size = common.Vec(10, 10, 10) sim.register_particle_type("A", .0, 5.0) sim.register_particle_type("B", .0, 6.0) sim.register_particle_type("C", .0, 6.0) sim.register_reaction_conversion("mylabel", "A", "B", .00001) sim.register_reaction_conversion("A->B", "A", "B", 1.) sim.register_reaction_fusion("B+C->A", "B", "C", "A", 1.0, 1.0, .5, .5) 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(fname, io.FileAction.CREATE, io.FileFlag.OVERWRITE)) as f: handle.enable_write_to_file(f, u"reactions", int(3)) sim.run_scheme_readdy(True).write_config_to_file( f).with_reaction_scheduler( "GillespieParallel").configure_and_run(n_timesteps, 1) 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) order_1_reactions = data["registered_reactions/order1_reactions"] order_2_reactions = data["registered_reactions/order2_reactions"] mylabel_reaction = get_item("mylabel", order_1_reactions) reaction_idx_mylabel = mylabel_reaction["index"] atob_reaction = get_item("A->B", order_1_reactions) reaction_idx_atob = atob_reaction["index"] # counts of first time step, time is first index np.testing.assert_equal( data["counts/order1/A[id=0]"][0, reaction_idx_mylabel], np.array([0])) np.testing.assert_equal( data["counts/order1/A[id=0]"][0, reaction_idx_atob], np.array([0])) np.testing.assert_equal( data["counts/order2/B[id=1] + C[id=2]"][0, 0], np.array([0])) # counts of second time step np.testing.assert_equal( data["counts/order1/A[id=0]"][1, reaction_idx_mylabel], np.array([0])) np.testing.assert_equal( data["counts/order1/A[id=0]"][1, reaction_idx_atob], np.array([1])) np.testing.assert_equal( data["counts/order2/B[id=1] + C[id=2]"][1, 0], np.array([1])) common.set_logging_level("warn")