Ejemplo n.º 1
0
    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]))
Ejemplo n.º 2
0
    def test_factory_programs(self):
        kernel = pr.SingleCPUKernel()
        kernel.get_kernel_context().particle_types().add("A", 1.0, 1.0)
        factory = kernel.get_action_factory()

        add_particles = factory.create_add_particles([pr.Particle(0, 0, 0, 0), pr.Particle(1, 1, 1, 0)])
        integrator = factory.create_euler_integrator(1)
        forces = factory.create_update_forces()
        neighbor_list = factory.create_update_neighbor_list()
        reactions = factory.create_reactions_uncontrolled_approximation(1)

        add_particles.perform()

        state_model = kernel.get_kernel_state_model()
        positions = state_model.get_particle_positions()
        np.testing.assert_equal(positions[0], cmn.Vec(0, 0, 0))
        np.testing.assert_equal(positions[1], cmn.Vec(1, 1, 1))

        it = kernel.get_kernel_state_model().get_particle_data().entries
        assert next(it).pos == cmn.Vec(0, 0, 0)
        assert next(it).pos == cmn.Vec(1, 1, 1)

        with np.testing.assert_raises(StopIteration):
            next(it)
Ejemplo n.º 3
0
    def test_center_of_mass_observable(self):
        common.set_logging_level("warn")
        fname = os.path.join(self.dir, "test_observables_com.h5")

        simulation = Simulation()
        simulation.set_kernel("SingleCPU")

        box_size = common.Vec(10, 10, 10)
        simulation.kbt = 2
        simulation.periodic_boundary = [True, True, True]
        simulation.box_size = box_size
        simulation.register_particle_type("A", .2, 1.)
        simulation.register_particle_type("B", .2, 1.)
        simulation.add_particle("A", common.Vec(-2.5, 0, 0))
        simulation.add_particle("B", common.Vec(0, 0, 0))
        n_time_steps = 50
        callback_com = []

        def com_callback(vec):
            callback_com.append(vec)

        handle = simulation.register_observable_center_of_mass(
            1, ["A", "B"], com_callback)
        with closing(
                io.File(fname, io.FileAction.CREATE,
                        io.FileFlag.OVERWRITE)) as f:
            handle.enable_write_to_file(f, u"com", 3)
            simulation.run(n_time_steps, 0.02)
            handle.flush()

        with h5py.File(fname, "r") as f2:
            com = f2["readdy/observables/com/data"][:]
            for t in range(n_time_steps):
                np.testing.assert_equal(com[t]["x"], callback_com[t][0])
                np.testing.assert_equal(com[t]["y"], callback_com[t][1])
                np.testing.assert_equal(com[t]["z"], callback_com[t][2])
Ejemplo n.º 4
0
    def chain_decay(self, kernel):
        sim = Simulation()
        sim.set_kernel(kernel)
        sim.box_size = common.Vec(10, 10, 10)
        np.testing.assert_equal(sim.kernel_supports_topologies(), True)

        typeid_b = sim.register_particle_type("B", 1.0, 1.0,
                                              ParticleTypeFlavor.NORMAL)
        sim.register_particle_type("Topology A", 1.0, 1.0,
                                   ParticleTypeFlavor.TOPOLOGY)
        sim.configure_topology_bond_potential("Topology A", "Topology A", 10,
                                              10)

        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(particles)

        for i in range(int(n_elements - 1)):
            topology.get_graph().add_edge(i, i + 1)

        topology.add_reaction(self._get_decay_reaction(typeid_b))
        topology.add_reaction(self._get_split_reaction())

        # 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_scheme_readdy(
            True).evaluate_topology_reactions().configure_and_run(
                int(500), float(1.0))

        np.testing.assert_equal(0, len(sim.current_topologies()))
Ejemplo n.º 5
0
 def test_unconnected_graph(self):
     context = Context()
     context.topologies.add_type("TA")
     context.box_size = [10., 10., 10.]
     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(0, 0, 0)) for _ in range(4)]
     top = sim.add_topology("TA", particles)
     graph = top.get_graph()
     graph.add_edge(0, 1)
     graph.add_edge(1, 2)
     with (np.testing.assert_raises(ValueError)):
         top.configure()
Ejemplo n.º 6
0
    def test_radial_distribution_observable(self):
        fname = os.path.join(self.dir, "test_observables_radial_distribution.h5")

        simulation = Simulation("SingleCPU")

        box_size = [10.,10.,10.]
        simulation.context.kbt = 2
        simulation.context.pbc = [True, True, True]
        simulation.context.box_size = box_size
        simulation.context.particle_types.add("A", .2)
        simulation.context.particle_types.add("B", .2)
        simulation.context.potentials.add_harmonic_repulsion("A", "B", 10, 2.)
        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]))
Ejemplo n.º 7
0
    def test_forces_observable(self):
        fname = os.path.join(self.dir, "test_observables_particle_forces.h5")
        sim = Simulation()
        sim.set_kernel("CPU")
        sim.box_size = common.Vec(13, 13, 13)
        sim.register_particle_type("A", .1, .1)
        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_forces(1, [])
        n_timesteps = 19
        with closing(
                io.File(fname, io.FileAction.CREATE,
                        io.FileFlag.OVERWRITE)) as f:
            handle.enable_write_to_file(f, u"forces", int(3))
            sim.run_scheme_readdy(True).configure(1).run(n_timesteps)
            handle.flush()

        with h5py.File(fname, "r") as f2:
            data = f2["readdy/observables/forces/data"][:]
            time_series = f2["readdy/observables/forces/time"]
            np.testing.assert_equal(len(data), n_timesteps + 1)
            np.testing.assert_equal(time_series,
                                    np.array(range(0, n_timesteps + 1)))
            for t, forces in enumerate(data):
                # we begin with two particles
                np.testing.assert_equal(len(forces), t + 2)
                np.testing.assert_equal(forces[0]["x"], 0)
                np.testing.assert_equal(forces[0]["y"], 0)
                np.testing.assert_equal(forces[0]["z"], 0)
                for i in range(1, len(forces)):
                    np.testing.assert_equal(forces[i]["x"], 0)
                    np.testing.assert_equal(forces[i]["y"], 0)
                    np.testing.assert_equal(forces[i]["z"], 0)
Ejemplo n.º 8
0
 def test_kernel_context_shortest_difference(self):
     self.ctx.box_size = [2, 2, 2]
     self.ctx.pbc = [True, True, True]
     self.ctx.configure()
     diff = self.ctx.shortest_difference_fun
     np.testing.assert_equal(diff(cmn.Vec(0, 0, 0), cmn.Vec(1, 0, 0)), cmn.Vec(1, 0, 0),
                             err_msg="both vectors were already inside the domain")
     np.testing.assert_equal(diff(cmn.Vec(0, 0, 0), cmn.Vec(-1.5, 0, 0)), cmn.Vec(.5, 0, 0),
                             err_msg="The latter vector was outside the domain, thus had to get a "
                                     "position update to (.5, 0, 0)")
Ejemplo n.º 9
0
 def test_kernel_context_shortest_difference(self):
     self.ctx.set_box_size(cmn.Vec(2, 2, 2))
     self.ctx.periodic_boundary = [True, True, True]
     diff = self.ctx.get_shortest_difference_fun()
     np.testing.assert_equal(
         diff(cmn.Vec(0, 0, 0), cmn.Vec(1, 0, 0)),
         cmn.Vec(1, 0, 0),
         err_msg="both vectors were already inside the domain")
     np.testing.assert_equal(
         diff(cmn.Vec(0, 0, 0), cmn.Vec(-1.5, 0, 0)),
         cmn.Vec(.5, 0, 0),
         err_msg=
         "The latter vector was outside the domain, thus had to get a "
         "position update to (.5, 0, 0)")
Ejemplo n.º 10
0
    def test_custom_state_model(self):
        class CustomStateModel(pr.Model):
            def get_particle_positions(self):
                return [cmn.Vec(-1, -1, -1)]

        class CustomKernel(pr.SingleCPUKernel):
            def __init__(self):
                super(CustomKernel, self).__init__()
                self._model = CustomStateModel(self.get_kernel_context(), self.get_topology_action_factory())

            def get_kernel_state_model(self):
                return self._model

        kernel = CustomKernel()
        pos = kernel.get_kernel_state_model().get_particle_positions()
        assert len(pos) == 1
        assert pos[0] == cmn.Vec(-1, -1, -1)
Ejemplo n.º 11
0
 def test_sanity(self):
     sim = Simulation()
     sim.set_kernel("SingleCPU")
     sim.box_size = common.Vec(10, 10, 10)
     np.testing.assert_equal(sim.kernel_supports_topologies(), True)
     sim.register_topology_type("TA")
     sim.register_particle_type("T",
                                1.0,
                                flavor=ParticleTypeFlavor.TOPOLOGY)
     sim.configure_topology_bond_potential("T", "T", 10., 11.)
     particles = [
         sim.create_topology_particle("T", common.Vec(x, 0, 0))
         for x in range(4)
     ]
     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)
     np.testing.assert_equal(len(graph.get_vertices()), 4)
     for v in graph.get_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_scheme_readdy(True).configure_and_run(0, 1)
Ejemplo n.º 12
0
    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))
Ejemplo n.º 13
0
    def test_write_flat_trajectory(self):
        traj_fname = os.path.join(self.dir, "flat_traj.h5")
        simulation = Simulation()
        simulation.set_kernel("SingleCPU")
        simulation.box_size = common.Vec(5, 5, 5)
        simulation.register_particle_type("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_flat_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_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]))
Ejemplo n.º 14
0
    def test_reactions_observable(self):
        common.set_logging_level("warn")
        fname = os.path.join(self.dir,
                             "test_observables_particle_reactions.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_reactions(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).configure_and_run(n_timesteps, 1)

        type_str_to_id = ioutils.get_particle_types(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)

            order_1_reactions = data["registered_reactions/order1_reactions"]

            mylabel_reaction = get_item("mylabel", order_1_reactions)
            np.testing.assert_equal(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", order_1_reactions)
            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])

            order_2_reactions = data["registered_reactions/order2_reactions"]

            fusion_reaction = get_item("B+C->A", order_2_reactions)
            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_index"], 1)
                elif record["reaction_type"] == 1:
                    # fusion
                    np.testing.assert_equal(record["position"],
                                            np.array([1.05, 1.0, 1.0]))
                    np.testing.assert_equal(record["reaction_index"], 0)

        common.set_logging_level("warn")
Ejemplo n.º 15
0
 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))
Ejemplo n.º 16
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"])
Ejemplo n.º 17
0
 def get_particle_positions(self):
     return [cmn.Vec(-1, -1, -1)]
Ejemplo n.º 18
0
 def callback_ab(value):
     callback_n_particles_a_b.append(value)
     simulation.add_particle("A", common.Vec(-1, -1, -1))
Ejemplo n.º 19
0
 def test_kernel_context_dist_squared_fun(self):
     self.ctx.box_size = [2., 2., 2.]
     self.ctx.pbc = [True, True, True]
     dist = self.ctx.dist_squared_fun
     np.testing.assert_equal(dist(cmn.Vec(0, 0, 0), cmn.Vec(1, 0, 0)), 1.0)
     np.testing.assert_equal(dist(cmn.Vec(-.5, 0, 0), cmn.Vec(-1.5, 0, 0)), 1.0)
Ejemplo n.º 20
0
 def test_kernel_context_box_size(self):
     self.ctx.set_box_size(cmn.Vec(5, 5, 5))
     np.testing.assert_equal(self.ctx.get_box_size(), cmn.Vec(5, 5, 5))
Ejemplo n.º 21
0
 def calculate_force(self, pos_vec):
     return cmn.Vec(.1, .1, .1)
Ejemplo n.º 22
0
    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")
Ejemplo n.º 23
0
 def callback(_):
     simulation.add_particle("A", common.Vec(0, 0, 0))