Esempio n. 1
0
    def test_potential_order_2(self):
        self.ctx.box_size = [2, 2, 2]
        self.ctx.pbc = [True, True, True]
        self.model.get_particle_data().clear()

        class MyPot2(pr.PotentialOrder2):
            def __init__(self, typea, typeb):
                super(MyPot2, self).__init__(typea, typeb)

            def calculate_energy(self, x_ij):
                return np.sqrt(x_ij * x_ij)

            def calculate_force(self, x_ij):
                return .5 * x_ij

            def configure_for_types(self, type1, type2):
                pass

            def get_cutoff_radius(self):
                return 5.0

            def get_maximal_force(self, kbt):
                return kbt

        self.ctx.particle_types.add("A", 1.0, 0)
        self.ctx.particle_types.add("B", 1.0, 0)
        pot = MyPot2(self.ctx.particle_types.id_of("A"), self.ctx.particle_types.id_of("B"))
        self.ctx.potentials.add_external_order2(pot)
        particles = [pr.Particle(0, 0, 0, self.ctx.particle_types.id_of("A")),
                     pr.Particle(1, 1, 1, self.ctx.particle_types.id_of("B"))]
        self.model.get_particle_data().add_particles(particles)
        self.ctx.configure()
Esempio n. 2
0
    def test_potential_order_2(self):
        self.ctx.set_box_size(cmn.Vec(2, 2, 2))
        self.ctx.periodic_boundary = [True, True, True]
        self.model.get_particle_data().clear()

        class MyPot2(pr.PotentialOrder2):
            def __init__(self, typea, typeb):
                super(MyPot2, self).__init__(typea, typeb)

            def calculate_energy(self, x_ij):
                return np.sqrt(x_ij * x_ij)

            def calculate_force(self, x_ij):
                return .5 * x_ij

            def configure_for_types(self, type1, type2):
                pass

            def get_cutoff_radius(self):
                return 5.0

            def get_maximal_force(self, kbt):
                return kbt

        self.ctx.particle_types().add("A", 1.0, 1.0, 0)
        self.ctx.particle_types().add("B", 1.0, 1.0, 0)
        pot = MyPot2("A", "B")
        self.ctx.potentials().add_external_order2(pot)
        particles = [
            pr.Particle(0, 0, 0,
                        self.ctx.particle_types().id_of("A")),
            pr.Particle(1, 1, 1,
                        self.ctx.particle_types().id_of("B"))
        ]
        add_particles_program = self.progs.create_add_particles(particles)
        add_particles_program.perform()
        self.ctx.configure()
        self.progs.create_update_neighbor_list().perform()
        self.progs.create_update_forces().perform()

        np.testing.assert_almost_equal(self.model.get_energy(), np.sqrt(3))

        it = self.model.get_particle_data().entries
        entry = next(it)
        if entry.type == self.ctx.particle_types().id_of("A"):
            np.testing.assert_equal(entry.force, cmn.Vec(.5, .5, .5))
            np.testing.assert_equal(next(it).force, cmn.Vec(-.5, -.5, -.5))
        else:
            np.testing.assert_equal(entry.force, cmn.Vec(-.5, -.5, -.5))
            np.testing.assert_equal(next(it).force, cmn.Vec(.5, .5, .5))

        with np.testing.assert_raises(StopIteration):
            next(it)
Esempio n. 3
0
    def test_potential_order_1(self):
        self.ctx.box_size = [2., 2., 2.]
        self.ctx.pbc = [True, True, True]
        self.model.get_particle_data().clear()

        class MyPot1(pr.PotentialOrder1):
            def __init__(self, type):
                super(MyPot1, self).__init__(type)

            def calculate_energy(self, pos_vec):
                return 5.0

            def calculate_force(self, pos_vec):
                return cmn.Vec(.1, .1, .1)

            def configure_for_type(self, type):
                pass

            def get_relevant_length_scale(self):
                return 5.0

            def get_maximal_force(self, kbt):
                return kbt

        self.ctx.particle_types.add("A", 1.0, 0)
        pot = MyPot1(self.ctx.particle_types.id_of("A"))
        self.ctx.potentials.add_external_order1(pot)
        particles = [pr.Particle(0, 0, .5, self.ctx.particle_types.id_of("A"))]
        self.model.get_particle_data().add_particles(particles)
        self.ctx.configure()
Esempio n. 4
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)
Esempio n. 5
0
    def test_potential_order_1(self):
        self.ctx.set_box_size(cmn.Vec(2, 2, 2))
        self.ctx.periodic_boundary = [True, True, True]
        self.model.get_particle_data().clear()

        class MyPot1(pr.PotentialOrder1):
            def __init__(self, type):
                super(MyPot1, self).__init__(type)

            def calculate_energy(self, pos_vec):
                return 5.0

            def calculate_force(self, pos_vec):
                return cmn.Vec(.1, .1, .1)

            def configure_for_type(self, type):
                pass

            def get_relevant_length_scale(self):
                return 5.0

            def get_maximal_force(self, kbt):
                return kbt

        self.ctx.particle_types().add("A", 1.0, 1.0, 0)
        pot = MyPot1("A")
        self.ctx.potentials().add_external_order1(pot)
        particles = [
            pr.Particle(0, 0, .5,
                        self.ctx.particle_types().id_of("A"))
        ]
        add_particles_program = self.progs.create_add_particles(particles)
        add_particles_program.perform()
        self.ctx.configure()
        updforces = self.progs.create_update_forces()
        updforces.perform()

        np.testing.assert_equal(
            self.model.get_energy(),
            5.0,
            err_msg="the user defined potential returns energy=5.0")

        data = self.model.get_particle_data()
        it = data.entries
        f_vec = next(it).force
        np.testing.assert_equal(f_vec, cmn.Vec(.1, .1, .1))
        with np.testing.assert_raises(StopIteration):
            next(it)