def test_field_at_position(self):
     mesh = SpatialMeshConf((2, 4, 8),
                            (1, 2, 4)).make(BoundaryConditionsConf())
     mesh.electric_field[1:2, 0:2, 0:2] = np.array([[[2, 1, 0], [-3, 1, 0]],
                                                    [[0, -1, 0], [-1, 0,
                                                                  0]]])
     assert_array_equal(mesh.field_at_position([(1, 1, 3)]),
                        [(-1.25, 0.375, 0)])
Example #2
0
 def test_eval_field_from_potential(self):
     mesh = SpatialMeshConf((1.5, 2, 1),
                            (0.5, 1, 1)).make(BoundaryConditionsConf())
     mesh.potential = np.stack([
         np.array([[0., 0, 0], [1, 2, 3], [4, 3, 2], [4, 4, 4]]),
         np.zeros((4, 3))
     ], -1)
     FieldSolver.eval_fields_from_potential(mesh)
     expected = np.array([[[[-2, 0, 0], [0, 0, 0]], [[-4, 0, 0], [0, 0, 0]],
                           [[-6, 0, 0], [0, 0, 0]]],
                          [[[-4, -1, 1], [0, 0, 1]], [[-3, -1, 2],
                                                      [0, 0, 2]],
                           [[-2, -1, 3], [0, 0, 3]]],
                          [[[-3, 1, 4], [0, 0, 4]], [[-2, 1, 3], [0, 0, 3]],
                           [[-1, 1, 2], [0, 0, 2]]],
                          [[[0, 0, 4], [0, 0, 4]], [[-2, 0, 4], [0, 0, 4]],
                           [[-4, 0, 4], [0, 0, 4]]]])
     assert_array_equal(mesh.electric_field, expected)
Example #3
0
def test_large_grid(solver_, backend_):
    try:
        configure_application(solver_, backend_)
        c = Config(TimeGridConf(1, 1, 1), SpatialMeshConf(100, 1),
                   inner_regions=[InnerRegionConf('tube',
                                                  Tube((50, 50, 10), (50, 50, 12), 10, 90),
                                                  10)])
        r = Runner(c.make())
        r.eval_and_write_fields_without_particles()
        assert r.simulation.potential.data.mean() >= 0
        assert r.simulation.potential.data.mean() <= 10
        assert abs(r.simulation.potential.data.max() - 10) < 0.01
        assert abs(r.simulation.potential.data.min()) < 0.01
    finally:
        inject.clear()
Example #4
0
    def test_transfer_solution_to_spat_mesh(self):
        mesh = SpatialMeshConf((4, 6, 9),
                               (1, 2, 3)).make(BoundaryConditionsConf())
        solver = FieldSolver(mesh, [])
        solver.phi_vec = np.array(range(1, 3 * 2 * 2 + 1))
        solver.transfer_solution_to_spat_mesh(mesh)
        assert_array_equal(
            mesh.potential[1:-1, 1:-1, 1:-1],
            [[[1, 7], [4, 10]], [[2, 8], [5, 11]], [[3, 9], [6, 12]]])

        assert_array_equal(
            mesh.potential,
            [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
             [[0, 0, 0, 0], [0, 1, 7, 0], [0, 4, 10, 0], [0, 0, 0, 0]],
             [[0, 0, 0, 0], [0, 2, 8, 0], [0, 5, 11, 0], [0, 0, 0, 0]],
             [[0, 0, 0, 0], [0, 3, 9, 0], [0, 6, 12, 0], [0, 0, 0, 0]],
             [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]])
Example #5
0
 def test_config(self, capsys):
     mesh = SpatialMeshConf((4, 2, 3), (2, 1, 3)).make(BoundaryConditionsConf(3.14))
     assert mesh.mesh.node_coordinates.shape == (3, 3, 2, 3)
     assert mesh.charge_density.shape == (3, 3, 2)
     assert mesh.potential.shape == (3, 3, 2)
     assert mesh.electric_field.shape == (3, 3, 2, 3)
     coords = np.array([[[[0., 0., 0.], [0., 0., 3.]], [[0., 1., 0.], [0., 1., 3.]],
                         [[0., 2., 0.], [0., 2., 3.]]],
                        [[[2., 0., 0.], [2., 0., 3.]], [[2., 1., 0.], [2., 1., 3.]],
                         [[2., 2., 0.], [2., 2., 3.]]],
                        [[[4., 0., 0.], [4., 0., 3.]], [[4., 1., 0.], [4., 1., 3.]],
                         [[4., 2., 0.], [4., 2., 3.]]]])
     assert_array_equal(mesh.mesh.node_coordinates, coords)
     assert_array_equal(mesh.charge_density, np.zeros((3, 3, 2)))
     potential = np.full((3, 3, 2), 3.14)
     assert_array_equal(mesh.potential, potential)
     assert_array_equal(mesh.electric_field, np.zeros((3, 3, 2, 3)))
     out, err = capsys.readouterr()
     assert out == ""
     assert err == ""
Example #6
0
 def test_construct_equation_matrix(self):
     mesh = SpatialMeshConf((4, 6, 9),
                            (1, 2, 3)).make(BoundaryConditionsConf())
     solver = FieldSolver(mesh, [])
     solver.construct_equation_matrix(mesh, [])
     d = -2 * (2 * 2 * 3 * 3 + 3 * 3 + 2 * 2)
     x = 2 * 2 * 3 * 3
     y = 3 * 3
     z = 2 * 2
     assert_array_equal(solver.A.toarray(),
                        [[d, x, 0, y, 0, 0, z, 0, 0, 0, 0, 0],
                         [x, d, x, 0, y, 0, 0, z, 0, 0, 0, 0],
                         [0, x, d, 0, 0, y, 0, 0, z, 0, 0, 0],
                         [y, 0, 0, d, x, 0, 0, 0, 0, z, 0, 0],
                         [0, y, 0, x, d, x, 0, 0, 0, 0, z, 0],
                         [0, 0, y, 0, x, d, 0, 0, 0, 0, 0, z],
                         [z, 0, 0, 0, 0, 0, d, x, 0, y, 0, 0],
                         [0, z, 0, 0, 0, 0, x, d, x, 0, y, 0],
                         [0, 0, z, 0, 0, 0, 0, x, d, 0, 0, y],
                         [0, 0, 0, z, 0, 0, y, 0, 0, d, x, 0],
                         [0, 0, 0, 0, z, 0, 0, y, 0, x, d, x],
                         [0, 0, 0, 0, 0, z, 0, 0, y, 0, x, d]])
Example #7
0
    def test_zero_nondiag_inside_objects(self):
        mesh = SpatialMeshConf((4, 6, 9),
                               (1, 2, 3)).make(BoundaryConditionsConf())
        solver = FieldSolver(mesh, [])
        region = InnerRegion('test', Box((1, 2, 3), (1, 2, 3)), 3)

        a = csr_matrix(np.full((12, 12), 2))
        assert_array_equal(a.toarray(), [[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                                         [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]])
        result = solver.zero_nondiag_for_nodes_inside_objects(
            a, mesh, [region])
        assert_array_equal(result.toarray(),
                           [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                            [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                            [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                            [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
                            [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                            [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                            [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
                            [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]])

        # TODO: check algorithm if on-diagonal zeros should turn into ones
        a = csr_matrix(
            np.array([[4, 0, 3, 0, 0, 0, 0, 2, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 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, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 6, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]))
        result = solver.zero_nondiag_for_nodes_inside_objects(
            a, mesh, [region])
        assert_array_equal(result.toarray(),
                           [[1, 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, 0, 0, 0, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
                            [0, 0, 2, 0, 0, 3, 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, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Example #8
0
    def test_init_rhs(self):
        mesh = SpatialMeshConf((4, 3, 3)).make(BoundaryConditionsConf())
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(mesh)
        assert_array_equal(solver.rhs, np.zeros(3 * 2 * 2))

        mesh = SpatialMeshConf((4, 3, 3)).make(BoundaryConditionsConf(-2))
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(mesh)
        assert_array_equal(solver.rhs,
                           [6, 4, 6, 6, 4, 6, 6, 4, 6, 6, 4, 6])  # what

        mesh = SpatialMeshConf((4, 4, 5)).make(BoundaryConditionsConf(-2))
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(mesh)
        assert_array_equal(solver.rhs, [
            6, 4, 6, 4, 2, 4, 6, 4, 6, 4, 2, 4, 2, 0, 2, 4, 2, 4, 4, 2, 4, 2,
            0, 2, 4, 2, 4, 6, 4, 6, 4, 2, 4, 6, 4, 6
        ])  # what

        mesh = SpatialMeshConf((8, 12, 5),
                               (2, 3, 1)).make(BoundaryConditionsConf(-1))
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(mesh)
        assert_array_equal(solver.rhs, [
            49, 40, 49, 45, 36, 45, 49, 40, 49, 13, 4, 13, 9, 0, 9, 13, 4, 13,
            13, 4, 13, 9, 0, 9, 13, 4, 13, 49, 40, 49, 45, 36, 45, 49, 40, 49
        ])

        mesh = SpatialMeshConf((4, 6, 9),
                               (1, 2, 3)).make(BoundaryConditionsConf())
        solver = FieldSolver(mesh, [])
        mesh.charge_density = np.array([[[0, 0, 0, 0], [0, 0, 0, 0],
                                         [0, 0, 0, 0], [0, 0, 0, 0]],
                                        [[0, 0, 0, 0], [0, 1, 2, 0],
                                         [0, -1, 0, 0], [0, 0, 0, 0]],
                                        [[0, 0, 0, 0], [0, 3, 4, 0],
                                         [0, 0, -1, 0], [0, 0, 0, 0]],
                                        [[0, 0, 0, 0], [0, 5, 6, 0],
                                         [0, -1, 0, 0], [0, 0, 0, 0]],
                                        [[0, 0, 0, 0], [0, 0, 0, 0],
                                         [0, 0, 0, 0], [0, 0, 0, 0]]])
        solver.init_rhs_vector_in_full_domain(mesh)
        assert_allclose(
            solver.rhs, -np.array([1, 3, 5, -1, 0, -1, 2, 4, 6, 0, -1, 0]) *
            np.pi * 4 * 36)

        mesh = SpatialMeshConf((4, 6, 9),
                               (1, 2, 3)).make(BoundaryConditionsConf())
        solver = FieldSolver(mesh, [])
        region = InnerRegion('test', Box((1, 2, 3), (1, 2, 3)), 3)
        solver.init_rhs_vector(mesh, [region])
        assert_array_equal(solver.rhs, [3, 3, 0, 3, 3, 0, 3, 3, 0, 3, 3, 0])
 def test_weight_particles_charge_to_mesh(self):
     mesh = SpatialMeshConf((2, 4, 8),
                            (1, 2, 4)).make(BoundaryConditionsConf())
     particle_arrays = [ParticleArray(1, -2, 4, [(1, 1, 3)], [(0, 0, 0)])]
     mesh.weight_particles_charge_to_mesh(particle_arrays)
     assert_array_equal(
         mesh.charge_density,
         np.array([[[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]]]))
     particle_arrays = [
         ParticleArray([1, 2], -2, 4, [(1, 1, 3), (1, 1, 3)],
                       np.zeros((2, 3)))
     ]
     mesh.clear_old_density_values()
     mesh.weight_particles_charge_to_mesh(particle_arrays)
     assert_array_equal(
         mesh.charge_density,
         np.array([[[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]]]))
     mesh.clear_old_density_values()
     particle_arrays = [ParticleArray(1, -2, 4, [(2, 4, 8)], [(0, 0, 0)])]
     mesh.weight_particles_charge_to_mesh(particle_arrays)
     assert_array_equal(
         mesh.charge_density,
         np.array([[[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]]]))
     particle_arrays = [ParticleArray(1, -2, 4, [(1, 2, 8.1)], [(0, 0, 0)])]
     with pytest.raises(ValueError,
                        match="Position is out of meshgrid bounds"):
         mesh.weight_particles_charge_to_mesh(particle_arrays)