Beispiel #1
0
 def test_expression(self, backend):
     f = FieldExpression('e1', 'electric', '-1+t', 'x*y-z', 'x+y*z')
     assert_array_equal(f.get_at_points([(1, 2, 3)], 0.), [(-1, -1, 7)])
     assert_array_equal(f.get_at_points([(1, 2, 3)], 5.), [(4, -1, 7)])
     assert_array_equal(f.get_at_points([(3, 2, 1)], 5.), [(4, 5, 5)])
     assert_array_equal(f.get_at_points([(3, 2, 1)], 5.), [(4, 5, 5)])
     assert_array_equal(f.get_at_points([(1, 2, 3), (3, 2, 1)], 5.), [(4, -1, 7), (4, 5, 5)])
Beispiel #2
0
    def test_interpolate_scalar(self):
        a = self.Array(MeshGrid(1, 2), None,
                       [[[0, 1], [0, -1]], [[0, 0], [0, 0]]])
        positions = np.array([(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1),
                              (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1),
                              (0.5, 0.5, 0.5), (0.5, 0, 0.5), (0.5, 1, 0.5),
                              (0, 0, 2)],
                             dtype=float)
        assert_array_equal(a.interpolate_at_positions(positions),
                           [0, 1, 0, -1, 0, 0, 0, 0, 0, 0.25, -0.25, 0])
        assert_array_equal(a.interpolate_at_positions(positions[1:2]), [1.])

        a = self.Array(
            MeshGrid((100, 20, 100), (101, 44, 101)), None,
            np.arange(101 * 44 * 101, dtype=float).reshape((101, 44, 101)))
        positions = np.linspace((0, 13, 100.1), (100.1, 0, 0), 314)
        o, s = np.zeros(3), np.array([100, 20, 100])
        xyz = tuple(
            np.linspace(o[i], o[i] + s[i], a.n_nodes[i]) for i in (0, 1, 2))
        interpolator = RegularGridInterpolator(xyz,
                                               a.data,
                                               bounds_error=False,
                                               fill_value=0)
        r1 = a.interpolate_at_positions(self.xp.asarray(positions))
        assert_array_almost_equal(r1, interpolator(positions))
Beispiel #3
0
    def test_distribute_scalar(self):
        a = self.Array(MeshGrid(1, 2))
        a.distribute_at_positions(8, self.xp.full((1000, 3), 0.5))
        assert_array_equal(a.data, np.full((2, 2, 2), 1000))
        a.distribute_at_positions(8, self.xp.full((1000, 3), 0.5))
        assert_array_equal(a.data, np.full((2, 2, 2), 2000))
        a.reset()
        a.distribute_at_positions(1, self.xp.full((1000, 3), 0.1))
        assert_array_almost_equal(
            a.data, np.array([[[729, 81], [81, 9]], [[81, 9], [9, 1]]]))
        a = self.Array(MeshGrid((2, 4, 8), (3, 3, 3)))
        a.distribute_at_positions(-2, [(1, 1, 3)])
        assert_array_equal(
            a.data, [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
                     [[-0.25 / 8, -0.75 / 8, 0], [-0.25 / 8, -0.75 / 8, 0],
                      [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]])

        a.reset()
        a.distribute_at_positions(-2, [(1, 1, 3), (1, 1, 3)])
        assert_array_equal(
            a.data, [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
                     [[-0.25 / 4, -0.75 / 4, 0], [-0.25 / 4, -0.75 / 4, 0],
                      [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, 0]]])
        a.reset()
        a.distribute_at_positions(-2, [(2, 4, 8)])
        assert_array_equal(a.data, [[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
                                    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
                                    [[0, 0, 0], [0, 0, 0], [0, 0, -0.25]]])
        a.reset()
Beispiel #4
0
 def test_update_positions(self):
     p = ParticleArray([123], -1.0, 2.0, [(0., 0., 1.)], [(1., 0., 3.)])
     p.update_positions(10.0)
     assert_array_equal(p.positions, [(5., 0., 16.)])
     p = ParticleArray((1, 2), -1.0, 2.0, [(0., 0., 1.), (1, 2, 3)],
                       [(1., 0., 3.), (-1, -0.5, 0)])
     p.update_positions(10.0)
     assert_array_equal(p.positions, [(5., 0., 16.), (-4, -0.5, 3)])
Beispiel #5
0
    def test_update_momentums(self):
        p = ParticleArray(123, -1.0, 2.0, (0., 0., 1.), (1., 0., 3.))
        p.boris_update_momentums(0.1, (-1.0, 2.0, 3.0), (0, 0, 0))
        assert_array_equal(p.momentums, [(1.1, -0.2, 2.7)])

        p = ParticleArray(123, -1.0, 2.0, (0., 0., 1.), (1., 0., 3.))
        p.boris_update_momentums(2, (-1.0, 2.0, 3.0),
                                 (2 * speed_of_light, 0, 0))
        assert_array_equal(p.momentums, [(3, -2, -5)])
Beispiel #6
0
 def test_init(self):
     a = self.Array(MeshGrid(10, 11))
     assert a.value_shape == ()
     assert_array_equal(a.data, np.zeros([11] * 3))
     assert type(a._data) == self.xp.ndarray
     data = self.xp.ones((5, 5, 5, 3))
     a = self.Array(MeshGrid(1, 5), 3, data)
     assert a.value_shape == (3, )
     assert_array_equal(a.data, np.ones((5, 5, 5, 3)))
     assert type(a._data) == self.xp.ndarray
     assert a._data is not data  # data gets copied don't create ArrayOnGrid every cycle
     with raises(ValueError):
         self.Array(MeshGrid(1, 5), data=self.xp.ones((5, 5, 5, 3)))
Beispiel #7
0
 def test_interpolate_vector(self):
     a = self.Array(MeshGrid((2, 4, 8), (3, 3, 3)), 3,
                    self.xp.full((3, 3, 3, 3), 100))
     a._data[1:2, 0:2, 0:2] = self.xp.array([[[2, 1, 0], [-3, 1, 0]],
                                             [[0, -1, 0], [-1, 0, 0]]])
     assert_array_equal(
         a.interpolate_at_positions(np.array([(1, 1, 3)], dtype=float)),
         [(-1.25, 0.375, 0)])
     assert_array_equal(
         a.interpolate_at_positions(
             self.xp.array([(1, 1, 3), (2, 1, 3), (1.5, 1, 3)],
                           dtype=float)), [(-1.25, 0.375, 0),
                                           (100, 100, 100),
                                           (49.375, 50.1875, 50)])
Beispiel #8
0
    def test_sum(self):
        f = FieldSum.factory([FieldUniform('u1', 'electric', np.array((3.14, 2.7, -0.5)))])
        assert type(f) is FieldUniform
        assert_array_equal(f.get_at_points([(1, 2, 3)], 0.), (3.14, 2.7, -0.5))
        assert_array_equal(f.get_at_points([(1, 2, 3)], 5.), (3.14, 2.7, -0.5))
        assert_array_equal(f.get_at_points([(3, 2, 1)], 5.), (3.14, 2.7, -0.5))
        f = FieldSum.factory(
            [FieldUniform('u1', 'electric', np.array((3.14, 2.7, -0.5))), None, FieldZero('u1', 'electric')])
        assert type(f) is FieldUniform

        f = FieldSum.factory([FieldUniform('u1', 'electric', np.array((3.14, 2.7, -0.5))),
                              FieldExpression('e1', 'electric', '-1+t', 'x*y-z', 'x+y*z')])
        assert type(f) is FieldSum
        assert_array_almost_equal(f.get_at_points([(1, 2, 3)], 0.), [(2.14, 1.7, 6.5)])
        assert_array_almost_equal(f.get_at_points([(1, 2, 3)], 5.), [(7.14, 1.7, 6.5)])
        assert_array_almost_equal(f.get_at_points([(3, 2, 1)], 5.), [(7.14, 7.7, 4.5)])

        f = FieldSum.factory([], 'electric')
        assert type(f) is FieldZero
        with raises(ValueError):
            FieldSum.factory([])

        f = FieldZero('zero', 'magnetic') + None
        assert type(f) is FieldZero
        with raises(ValueError):
            FieldZero('zero', 'magnetic') + FieldZero('zero', 'electric')

        f = FieldSum('electric', [FieldUniform('u1', 'electric', np.array((3.14, 2.7, -0.5)))]) + \
            FieldExpression('e1', 'electric', '-1+t', 'x*y-z', 'x+y*z') + None
        assert type(f) is FieldSum
        assert len(f.fields) == 2
        assert_array_almost_equal(f.get_at_points([(1, 2, 3)], 0.), [(2.14, 1.7, 6.5)])
        assert_array_almost_equal(f.get_at_points([(1, 2, 3)], 5.), [(7.14, 1.7, 6.5)])
        assert_array_almost_equal(f.get_at_points([(3, 2, 1)], 5.), [(7.14, 7.7, 4.5)])
Beispiel #9
0
 def test_from_file(self, backend):
     f = FieldFromCSVFile('f1', 'electric', 'tests/test_field.csv')
     assert_array_equal(f.get_at_points([(0, 0, 0), (1, 1, 1), (1, 0, 1), (.5, .5, .5)], 0),
                        [(1, 1, 1), (-1, -1, -1), (3, 2, 1), (1, 1, 1)])
     assert_array_equal(f.get_at_points([(0, 0, 0), (1, 1, 1), (1, 0, 1), (.5, .5, .5)], 10.),
                        [(1, 1, 1), (-1, -1, -1), (3, 2, 1), (1, 1, 1)])
     assert_array_almost_equal(f.get_at_points([(.5, 1., .3), (0, .5, .7)], 5), [(0., .5, 1.), (1, 1.5, 2)])
     assert_array_equal(f.get_at_points([(-1, 1., .3), (1, 1, 10)], 3), [(0, 0, 0), (0, 0, 0)])
Beispiel #10
0
 def test_update_momentums_function(self):
     assert_array_equal(
         ParticleArray._boris_update_momentums(-1, 2, (1, 0, 3), 0.1,
                                               (-1.0, 2.0, 3.0), (0, 0, 0)),
         (1.1, -0.2, 2.7))
     assert_array_equal(
         ParticleArray._boris_update_momentums(-1, 2, (1, 0, 3), 2,
                                               (-1.0, 2.0, 3.0),
                                               (2 * speed_of_light, 0, 0)),
         (3, -2, -5))
     assert_array_equal(
         ParticleArray._boris_update_momentums(-1, 2, (1, 0, 3), 2,
                                               (-1.0, 2.0, 3.0),
                                               (2 * speed_of_light, 0, 0)),
         (3, -2, -5))
     assert_array_equal(
         ParticleArray._boris_update_momentums(
             charge=-1,
             mass=2,
             momentum_arr=self.xp.array([(1, 0, 3)] * 10),
             dt=2,
             el_field_arr=[(-1.0, 2.0, 3.0)] * 10,
             mgn_field_arr=[(2 * speed_of_light, 0, 0)] * 10),
         self.xp.array([(3, -2, -5)] * 10))
Beispiel #11
0
 def test_zero(self, backend):
     f = FieldZero('z', 'magnetic')
     assert_array_equal(f.get_at_points((1, 2, 3), 0.), (0, 0, 0))
     assert_array_equal(f.get_at_points((1, 2, 3), 5.), (0, 0, 0))
     assert_array_equal(f.get_at_points((3, 2, 1), 5.), (0, 0, 0))
Beispiel #12
0
 def test_uniform(self):
     f = FieldUniform('u1', 'electric', np.array((3.14, 2.7, -0.5)))
     assert_array_equal(f.get_at_points((1, 2, 3), 0.), (3.14, 2.7, -0.5))
     assert_array_equal(f.get_at_points((1, 2, 3), 5.), (3.14, 2.7, -0.5))
     assert_array_equal(f.get_at_points((3, 2, 1), 5.), (3.14, 2.7, -0.5))
Beispiel #13
0
 def test_reset(self):
     a = self.Array(MeshGrid(1, 5), (), self.xp.ones((5, 5, 5)))
     assert_array_equal(a.data, np.ones((5, 5, 5)))
     a.reset()
     assert_array_equal(a.data, np.zeros((5, 5, 5)))
Beispiel #14
0
 def test_update_momentums_no_mgn(self):
     p = ParticleArray(123, -1.0, 2.0, (0., 0., 1.), (1., 0., 3.))
     p.boris_update_momentum_no_mgn(0.1, (-1.0, 2.0, 3.0))
     assert_array_equal(p.momentums, [(1.1, -0.2, 2.7)])
Beispiel #15
0
 def test_field_at_point(self):
     p = ParticleArray([1], -16.0, 2.0, [(0., 0., 1.)], [(1., 0., 3.)])
     assert_array_equal(p.field_at_points((2., 0., 1.)), [(-4, 0, 0)])
     assert_array_equal(p.field_at_points((2., 0., 1.)),
                        self.xp.array([(-4, 0, 0)]))
     assert_array_equal(p.field_at_points(self.xp.array((2., 0., 1.))),
                        [(-4, 0, 0)])
     assert_array_equal(p.field_at_points((0., 0., 1.)),
                        self.xp.array([[0, 0, 0]]))
     p = ParticleArray((1, 2), -16.0, 2.0, [(0, 0, 1), (0, 0, 0)],
                       self.xp.zeros((2, 3)))
     assert_array_equal(p.field_at_points((0, 0, 0.5)), [(0, 0, 0)])
     assert_array_equal(p.field_at_points((0, 0, 2)), [(0, 0, -20)])
     assert_array_equal(p.field_at_points((0., 0., 0)),
                        self.xp.array([[0, 0, 16]]))
     assert_array_equal(
         p.field_at_points([(0, 0, 0.5), (0, 0, 2), (0, 0, 2)]),
         [(0, 0, 0), (0, 0, -20), (0, 0, -20)])