Ejemplo n.º 1
0
 def test_box_positions_in(self):
     b = Box((0, 2, 4), (2, 2, 2))
     # check that non-array call works
     assert b.are_positions_inside((1, 3, 5))
     assert not b.are_positions_inside((-1, 3, 5))
     b.xp.testing.assert_array_equal(
         b.are_positions_inside([(1, 3, 5), (0.5, 2.5, 5.5), (0, 2, 4),
                                 (2, 4, 6), (0, 3, 5), (1, 2, 6),
                                 (-1, 3, 5), (3, 3, 5), (1, 0, 5),
                                 (1, 5, 5), (1, 3, 2), (1, 3, 9),
                                 (10, -10, 10), (0, 0, 0)]),
         b.xp.asarray([
             1,
             1,  # in the box
             1,
             1,
             1,
             1,  # on the surface
             0,
             0,
             0,
             0,
             0,
             0,  # far on one axis
             0,
             0
         ]))  # far on all axes
 def __init__(self,
              name='ParticleSource1',
              shape=Box(),
              initial_particles=500,
              particles_to_generate_each_step=500,
              momentum=(0, 0, 6.641e-15),
              temperature=0.0,
              charge=-1.799e-6,
              mass=3.672e-24):
     self.name = name
     self.shape = shape
     self.initial_particles = initial_particles
     self.particles_to_generate_each_step = particles_to_generate_each_step
     self.momentum = np.array(momentum, np.float)
     self.temperature = temperature
     self.charge = charge
     self.mass = mass
Ejemplo n.º 3
0
 def make(self):
     l, r, b, t, n, f = self.content[:6]
     box = Box((r, b, n), (l - r, t - b, f - n))
     return InnerRegionConf(self.name, box, self.content.potential)
Ejemplo n.º 4
0
    def test_generate_positions(self):
        shape = Box()
        num_points = 500000
        decimals = 2
        points = shape.generate_uniform_random_posititons(
            RandomState(0), num_points)
        assert shape.are_positions_inside(points).all()
        assert_array_almost_equal(points.mean(axis=0), (0.5, 0.5, 0.5),
                                  decimals)
        assert_array_almost_equal(np.median(points, axis=0), (0.5, 0.5, 0.5),
                                  decimals)
        assert_array_almost_equal(points.std(axis=0),
                                  (1 / sqrt(12), 1 / sqrt(12), 1 / sqrt(12)),
                                  decimals)
        assert_array_almost_equal(points.min(axis=0), (0, 0, 0), decimals)
        assert_array_almost_equal(points.max(axis=0), (1, 1, 1), decimals)
        assert_array_almost_equal(
            np.cov(points.transpose()),
            [[1 / 12, 0, 0], [0, 1 / 12, 0], [0, 0, 1 / 12]], decimals)
        shape = Cylinder()
        points = shape.generate_uniform_random_posititons(
            RandomState(0), num_points)
        assert shape.are_positions_inside(points).all()
        assert_array_almost_equal(points.mean(axis=0), (0.5, 0, 0), decimals)
        assert_array_almost_equal(np.median(points, axis=0), (0.5, 0, 0),
                                  decimals)
        assert_array_almost_equal(points.std(axis=0), (1 / sqrt(12), .5, .5),
                                  decimals)
        assert_array_almost_equal(points.min(axis=0), (0, -1, -1), decimals)
        assert_array_almost_equal(points.max(axis=0), (1, 1, 1), decimals)
        print(np.cov(points.transpose()))
        assert_array_almost_equal(
            np.cov(points.transpose()),
            [[1 / 12, 0, 0], [0, 1 / 4, 0], [0, 0, 1 / 4]], decimals)

        shape = Cylinder((0, 0, 0), (0, 0, 1), 1)
        points = shape.generate_uniform_random_posititons(
            RandomState(0), num_points)
        assert shape.are_positions_inside(points).all()
        assert_array_almost_equal(points.mean(axis=0), (0, 0, 0.5), decimals)
        assert_array_almost_equal(np.median(points, axis=0), (0, 0, 0.5),
                                  decimals)
        assert_array_almost_equal(points.std(axis=0), (.5, .5, 1 / sqrt(12)),
                                  decimals)
        assert_array_almost_equal(points.min(axis=0), (-1, -1, 0), decimals)
        assert_array_almost_equal(points.max(axis=0), (1, 1, 1), decimals)
        print(np.cov(points.transpose()))
        assert_array_almost_equal(
            np.cov(points.transpose()),
            [[1 / 4, 0, 0], [0, 1 / 4, 0], [0, 0, 1 / 12]], decimals)

        shape = Tube()
        points = shape.generate_uniform_random_posititons(
            RandomState(0), num_points)
        assert shape.are_positions_inside(points).all()
        assert_array_almost_equal(points.mean(axis=0), (0.5, 0, 0), decimals)
        assert_array_almost_equal(np.median(points, axis=0), (0.5, 0, 0),
                                  decimals)
        assert_array_almost_equal(points.std(axis=0),
                                  (1 / sqrt(12), sqrt(1.5), sqrt(1.5)),
                                  decimals)
        assert_array_almost_equal(points.min(axis=0), (0, -2, -2), decimals)
        assert_array_almost_equal(points.max(axis=0), (1, 2, 2), decimals)
        print(np.cov(points.transpose()))
        assert_array_almost_equal(np.cov(points.transpose()),
                                  [[1 / 12, 0, 0], [0, 1.5, 0], [0, 0, 1.5]],
                                  decimals)

        shape = Sphere()
        points = shape.generate_uniform_random_posititons(
            RandomState(0), num_points)
        assert shape.are_positions_inside(points).all()
        assert_array_almost_equal(points.mean(axis=0), (0, 0, 0), decimals)
        assert_array_almost_equal(np.median(points, axis=0), (0, 0, 0),
                                  decimals)
        assert_array_almost_equal(points.std(axis=0),
                                  (sqrt(.2), sqrt(.2), sqrt(.2)), decimals)
        assert_array_almost_equal(points.min(axis=0), (-1, -1, -1), decimals)
        assert_array_almost_equal(points.max(axis=0), (1, 1, 1), decimals)
        print(np.cov(points.transpose()))
        assert_array_almost_equal(np.cov(points.transpose()),
                                  [[.2, 0, 0], [0, .2, 0], [0, 0, .2]],
                                  decimals)
 def make(self):
     l, r, b, t, n, f = self.content[:6]
     box = Box((r, b, n), (l - r, t - b, f - n))
     return ParticleSource._from_content(self.name, box, self.content)
 def __init__(self, name="InnerRegion1", shape=Box(), potential=0):
     self.name = name
     self.shape = shape
     self.potential = float(potential)