Exemple #1
0
    def test_reaction_info(self):
        p_types = ioutils.get_particle_types(self.fname)
        reactions = ioutils.get_reactions(self.fname)
        self.assertEqual(len(reactions), 3)

        self.assertTrue("mylabel" in reactions)
        mylabel = reactions["mylabel"]
        self.assertEqual(mylabel["n_educts"], 1)
        self.assertEqual(mylabel["n_products"], 1)
        self.assertAlmostEqual(mylabel["rate"], 0.00001)
        self.assertEqual(mylabel["educt_types"][0], p_types["A"])
        self.assertEqual(mylabel["product_types"][0], p_types["B"])

        self.assertTrue("A->B" in reactions)
        atob = reactions["A->B"]
        self.assertEqual(atob["n_educts"], 1)
        self.assertEqual(atob["n_products"], 1)
        self.assertEqual(atob["rate"], 1.)
        self.assertEqual(atob["educt_types"][0], p_types["A"])
        self.assertEqual(atob["product_types"][0], p_types["B"])

        self.assertTrue("B+C->A" in reactions)
        fusion = reactions["B+C->A"]
        self.assertEqual(fusion["n_educts"], 2)
        self.assertEqual(fusion["n_products"], 1)
        self.assertAlmostEqual(fusion["rate"], 0.4)
        self.assertAlmostEqual(fusion["educt_distance"], 0.2)
        correct_educts = (fusion["educt_types"][0] == p_types["B"]
                          and fusion["educt_types"][1] == p_types["C"])
        correct_educts = correct_educts or (
            fusion["educt_types"][1] == p_types["B"]
            and fusion["educt_types"][0] == p_types["C"])
        self.assertTrue(correct_educts)
        self.assertEqual(fusion["product_types"][0], p_types["A"])
Exemple #2
0
    def test_reaction_info(self):
        p_types = ioutils.get_particle_types(self.fname)
        reactions = ioutils.get_reactions(self.fname)
        self.assertEqual(len(reactions), 3)

        self.assertTrue("mylabel" in reactions)
        mylabel = reactions["mylabel"]
        self.assertEqual(mylabel["n_educts"], 1)
        self.assertEqual(mylabel["n_products"], 1)
        self.assertAlmostEqual(mylabel["rate"], 0.00001)
        self.assertEqual(mylabel["educt_types"][0], p_types["A"])
        self.assertEqual(mylabel["product_types"][0], p_types["B"])

        self.assertTrue("A->B" in reactions)
        atob = reactions["A->B"]
        self.assertEqual(atob["n_educts"], 1)
        self.assertEqual(atob["n_products"], 1)
        self.assertEqual(atob["rate"], 1.)
        self.assertEqual(atob["educt_types"][0], p_types["A"])
        self.assertEqual(atob["product_types"][0], p_types["B"])

        self.assertTrue("B+C->A" in reactions)
        fusion = reactions["B+C->A"]
        self.assertEqual(fusion["n_educts"], 2)
        self.assertEqual(fusion["n_products"], 1)
        self.assertAlmostEqual(fusion["rate"], 0.4)
        self.assertAlmostEqual(fusion["educt_distance"], 0.2)
        correct_educts = (fusion["educt_types"][0] == p_types["B"] and fusion["educt_types"][1] == p_types["C"])
        correct_educts = correct_educts or (fusion["educt_types"][1] == p_types["B"] and fusion["educt_types"][0] == p_types["C"])
        self.assertTrue(correct_educts)
        self.assertEqual(fusion["product_types"][0], p_types["A"])
Exemple #3
0
 def __init__(self, filename, name=""):
     """
     attempts to open the given trajectory
     :param filename: the file name
     :param name: the trajectory name inside the file, as given in the simulation
     """
     assert _os.path.exists(
         filename), "The file '{}' did not exist!".format(filename)
     self._filename = filename
     self._name = name
     self._diffusion_constants = _io_utils.get_diffusion_constants(filename)
     self._particle_types = _io_utils.get_particle_types(filename)
     self._reactions = []
     self._inverse_types_map = {
         v: k
         for k, v in self.particle_types.items()
     }
     self._general = GeneralInformation(filename)
     for _, reaction in _io_utils.get_reactions(filename).items():
         info = ReactionInfo(reaction["name"], reaction["id"],
                             reaction["n_educts"], reaction["n_products"],
                             reaction["rate"], reaction["educt_distance"],
                             reaction["product_distance"],
                             reaction["educt_types"],
                             reaction["product_types"],
                             self._inverse_types_map)
         self._reactions.append(info)
Exemple #4
0
    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]))
Exemple #5
0
    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]))
Exemple #6
0
 def __init__(self, filename, name=""):
     """
     attempts to open the given trajectory
     :param filename: the file name
     :param name: the trajectory name inside the file, as given in the simulation
     """
     assert _os.path.exists(filename), "The file '{}' did not exist!".format(filename)
     self._filename = filename
     self._name = name
     self._diffusion_constants = _io_utils.get_diffusion_constants(filename)
     self._particle_types = _io_utils.get_particle_types(filename)
     self._reactions = []
     self._inverse_types_map = {v: k for k, v in self.particle_types.items()}
     self._general = GeneralInformation(filename)
     for _, reaction in _io_utils.get_reactions(filename).items():
         info = ReactionInfo(reaction["name"], reaction["id"], reaction["n_educts"],
                             reaction["n_products"], reaction["rate"], reaction["educt_distance"],
                             reaction["product_distance"], reaction["educt_types"], reaction["product_types"],
                             self._inverse_types_map)
         self._reactions.append(info)
Exemple #7
0
    def test_reactions_observable(self):
        fname = os.path.join(self.dir, "test_observables_particle_reactions.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", 1.)
        sim.context.reactions.add_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_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"])
Exemple #8
0
 def test_reaction_info_all(self):
     reactions_all = ioutils.get_reactions(self.fname)
     np.testing.assert_equal(len(reactions_all), 3)