Beispiel #1
0
    def test_particle_positions_observable(self):
        fname = os.path.join(self.dir, "test_observables_particle_positions.h5")
        sim = Simulation("SingleCPU")
        sim.context.box_size = [13., 13., 13.]
        sim.context.particle_types.add("A", .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_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)
Beispiel #2
0
    def test_particle_positions_observable(self):
        fname = os.path.join(self.dir,
                             "test_observables_particle_positions.h5")
        sim = Simulation()
        sim.set_kernel("SingleCPU")
        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_particle_positions(1, [])
        n_timesteps = 19
        with closing(
                io.File(fname, io.FileAction.CREATE,
                        io.FileFlag.OVERWRITE)) as f:
            handle.enable_write_to_file(f, u"particle_positions", int(3))
            sim.run_scheme_readdy(True).configure(0).run(n_timesteps)
            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)
        common.set_logging_level("warn")
Beispiel #3
0
class TwoParticlesMiniExample(object):
    def __init__(self):
        KernelProvider.get().load_from_dir(
            platform_utils.get_readdy_plugin_dir())
        self.simulation = Simulation()
        self.simulation.set_kernel("CPU")

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111, projection='3d')
        plt.ioff()
        self.fig.show()
        self.prev_pos = {}
        self.current_plot = None

        self.T = 4000000

    def ppos_callback(self, pos):
        plt.cla()
        self.ax.set_xlim([-1, 1])
        self.ax.set_ylim([-1, 1])
        self.ax.set_zlim([-1, 1])
        r = [-.75, .75]
        for s, e in combinations(np.array(list(product(r, r, r))), 2):
            if np.sum(np.abs(s - e)) == r[1] - r[0]:
                self.ax.plot3D(*zip(s, e), color="b")
        pA = self.simulation.get_particle_positions("A")
        pB = self.simulation.get_particle_positions("B")
        if len(pA) == 1 and len(pB) == 1:
            A = pA[0]
            B = pB[0]
            self.ax.scatter([A[0]], [A[1]], [A[2]], color="g", s=100)
            self.ax.scatter([B[0]], [B[1]], [B[2]], color="r", s=100)
            self.ax.plot3D([A[0], B[0]], [A[1], B[1]], [A[2], B[2]], color="r")
        pC = self.simulation.get_particle_positions("C")
        if len(pC) == 1:
            C = pC[0]
            self.ax.scatter([C[0]], [C[1]], [C[2]], color="b", s=100)
        plt.pause(.001)

    def start(self):
        box_size = Vec(2.0, 2.0, 2.0)
        depth = 2.
        desired_dist = .25
        force_constant = 4 * depth / (desired_dist * desired_dist)
        no_interaction_dist = 1.5
        self.simulation.kbt = 0.01
        self.simulation.periodic_boundary = [False, False, False]
        self.simulation.box_size = box_size
        self.simulation.register_particle_type("A", .1, .1)
        self.simulation.register_particle_type("B", .01, .1)
        self.simulation.register_particle_type("C", .5, .1)
        self.simulation.register_potential_piecewise_weak_interaction(
            "A", "B", force_constant, desired_dist, depth, no_interaction_dist
        )  # (force constant, desired dist, depth, no interaction dist)
        self.simulation.register_reaction_fusion("fusion", "A", "B", "C",
                                                 1000., .3, .5, .5)
        self.simulation.register_reaction_fission("fission", "C", "A", "B",
                                                  1000., .25, .5, .5)
        self.simulation.register_potential_box("A", 100.,
                                               Vec(-.75, -.75, -.75),
                                               Vec(1.5, 1.5, 1.5), False)
        self.simulation.register_potential_box("B", 100.,
                                               Vec(-.75, -.75, -.75),
                                               Vec(1.5, 1.5, 1.5), False)
        self.simulation.register_potential_box("C", 100.,
                                               Vec(-.75, -.75, -.75),
                                               Vec(1.5, 1.5, 1.5), False)
        self.simulation.add_particle("A", Vec(-.0, -.0, -.0))
        self.simulation.add_particle("B", Vec(0.1, 0.1, 0.1))
        self.simulation.register_observable_particle_positions(
            1, [], self.ppos_callback)

        self.simulation.run(self.T, .0001)
class TwoParticlesMiniExample(object):
    def __init__(self):
        KernelProvider.get().load_from_dir(platform_utils.get_readdy_plugin_dir())
        self.simulation = Simulation()
        self.simulation.set_kernel("CPU")

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111, projection='3d')
        plt.ioff()
        self.fig.show()
        self.prev_pos = {}
        self.current_plot = None

        self.T = 4000000

    def ppos_callback(self, pos):
        plt.cla()
        self.ax.set_xlim([-1, 1])
        self.ax.set_ylim([-1, 1])
        self.ax.set_zlim([-1, 1])
        r = [-.75, .75]
        for s, e in combinations(np.array(list(product(r, r, r))), 2):
            if np.sum(np.abs(s - e)) == r[1] - r[0]:
                self.ax.plot3D(*zip(s, e), color="b")
        pA = self.simulation.get_particle_positions("A")
        pB = self.simulation.get_particle_positions("B")
        if len(pA) == 1 and len(pB) == 1:
            A = pA[0]; B = pB[0]
            self.ax.scatter([A[0]], [A[1]], [A[2]], color="g", s=100)
            self.ax.scatter([B[0]], [B[1]], [B[2]], color="r", s=100)
            self.ax.plot3D([A[0], B[0]], [A[1], B[1]], [A[2], B[2]], color="r")
        pC = self.simulation.get_particle_positions("C")
        if len(pC) == 1:
            C = pC[0]
            self.ax.scatter([C[0]], [C[1]], [C[2]], color="b", s=100)
        plt.pause(.001)

    def start(self):
        box_size = Vec(2.0, 2.0, 2.0)
        depth = 2.
        desired_dist = .25
        force_constant = 4 * depth / (desired_dist * desired_dist)
        no_interaction_dist = 1.5
        self.simulation.kbt = 0.01
        self.simulation.periodic_boundary = [False, False, False]
        self.simulation.box_size = box_size
        self.simulation.register_particle_type("A", .1, .1)
        self.simulation.register_particle_type("B", .01, .1)
        self.simulation.register_particle_type("C", .5, .1)
        self.simulation.register_potential_piecewise_weak_interaction("A", "B", force_constant, desired_dist, depth,
                                                                          no_interaction_dist)  # (force constant, desired dist, depth, no interaction dist)
        self.simulation.register_reaction_fusion("fusion", "A", "B", "C", 1000., .3, .5, .5)
        self.simulation.register_reaction_fission("fission", "C", "A", "B", 1000., .25, .5, .5)
        self.simulation.register_potential_box("A", 100., Vec(-.75, -.75, -.75), Vec(1.5, 1.5, 1.5), False)
        self.simulation.register_potential_box("B", 100., Vec(-.75, -.75, -.75), Vec(1.5, 1.5, 1.5), False)
        self.simulation.register_potential_box("C", 100., Vec(-.75, -.75, -.75), Vec(1.5, 1.5, 1.5), False)
        self.simulation.add_particle("A", Vec(-.0, -.0, -.0))
        self.simulation.add_particle("B", Vec(0.1, 0.1, 0.1))
        self.simulation.register_observable_particle_positions(1, [], self.ppos_callback)

        self.simulation.run(self.T, .0001)