Example #1
0
    def test_init_rhs(self, backend):
        mesh = MeshGrid.from_step((4, 3, 3), 1)
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(ArrayOnGrid(mesh),
                                              ArrayOnGrid(mesh))
        assert_array_equal(solver.rhs, np.zeros(3 * 2 * 2))
        pot = ArrayOnGrid(mesh)
        pot.apply_boundary_values(BoundaryConditionsConf(-2))
        solver.init_rhs_vector_in_full_domain(ArrayOnGrid(mesh), pot)
        assert_array_equal(solver.rhs,
                           [6, 4, 6, 6, 4, 6, 6, 4, 6, 6, 4, 6])  # what
        del solver

        mesh = MeshGrid.from_step((4, 4, 5), 1)
        pot = ArrayOnGrid(mesh)
        pot.apply_boundary_values(BoundaryConditionsConf(-2))
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(ArrayOnGrid(mesh), pot)
        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
        del solver

        mesh = MeshGrid.from_step((8, 12, 5), (2, 3, 1))
        pot = ArrayOnGrid(mesh)
        pot.apply_boundary_values(BoundaryConditionsConf(-1))
        solver = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(ArrayOnGrid(mesh), pot)
        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
        ])
        del solver

        mesh = MeshGrid.from_step((4, 6, 9), (1, 2, 3))
        charge = ArrayOnGrid(mesh)
        charge._data = 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 = FieldSolver(mesh, [])
        solver.init_rhs_vector_in_full_domain(charge, ArrayOnGrid(mesh))
        assert_allclose(
            solver.rhs, -np.array([1, 3, 5, -1, 0, -1, 2, 4, 6, 0, -1, 0]) *
            np.pi * 4 * 36)

        solver = FieldSolver(mesh, [])
        nodep = solver.generate_nodes_in_regions(
            [InnerRegion('test', Box((1, 2, 3), (1, 2, 3)), 3)])
        solver.nodes_in_regions, solver.potential_in_regions = nodep
        solver.init_rhs_vector(ArrayOnGrid(mesh), ArrayOnGrid(mesh))
        assert_array_equal(solver.rhs, [3, 3, 0, 3, 3, 0, 3, 3, 0, 3, 3, 0])
Example #2
0
    def test_zero_nondiag_inside_objects(self, backend):
        mesh = MeshGrid.from_step((4, 6, 9), (1, 2, 3))
        solver = FieldSolver(
            mesh, [InnerRegion('test', Box((1, 2, 3), (1, 2, 3)), 3)])

        a = scipy.sparse.coo_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)
        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]])

        a = scipy.sparse.coo_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)
        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 #3
0
 def test_generate_nodes_in_regions(self, backend):
     mesh = MeshGrid.from_step((4, 6, 9), (1, 2, 3))
     solver = FieldSolver(mesh, [])
     inner_regions = [InnerRegion('test', Box((1, 2, 3), (1, 2, 3)), 3)]
     nodes, potential = solver.generate_nodes_in_regions(inner_regions)
     assert_array_equal(nodes, [0, 1, 3, 4, 6, 7, 9, 10])
     assert_array_equal(potential, [3, 3, 3, 3, 3, 3, 3, 3])
Example #4
0
 def test_do_init_warnings(self, capsys, caplog):
     MeshGrid.from_step((12, 12, 12), (5, 5, 7))
     out, err = capsys.readouterr()
     assert out == ""
     assert err == ""
     assert caplog.record_tuples == [
         ('root', logging.WARNING,
          "X step on spatial grid was reduced to 4.000 from 5.000 to fit in a round number of cells."
          ),
         ('root', logging.WARNING,
          "Y step on spatial grid was reduced to 4.000 from 5.000 to fit in a round number of cells."
          ),
         ('root', logging.WARNING,
          "Z step on spatial grid was reduced to 6.000 from 7.000 to fit in a round number of cells."
          ),
     ]
Example #5
0
 def __init__(self,
              name,
              electric_or_magnetic,
              field_filename,
              xp=numpy,
              array_class=ArrayOnGrid):
     if not os.path.exists(field_filename):
         raise FileNotFoundError("Field file not found")
     raw = numpy.loadtxt(field_filename, skiprows=1)
     # assume X Y Z Fx Fy Fz columns
     # sort by column 0, then 1, then 2
     # https://stackoverflow.com/a/38194077
     ind = raw[:, 2].argsort()  # First sort doesn't need to be stable.
     raw = raw[ind]
     ind = raw[:, 1].argsort(kind='mergesort')
     raw = raw[ind]
     ind = raw[:, 0].argsort(kind='mergesort')
     raw = raw[ind]
     size = (raw[-1, :3] - raw[0, :3])
     origin = raw[0, :3]
     dist = raw[:, :3] - raw[0, :3]
     step = dist.min(axis=0, where=dist > 0, initial=dist.max())
     grid = MeshGrid.from_step(size, step, origin)
     field = xp.asarray(raw[:, 3:].reshape((*grid.n_nodes, 3)))
     super().__init__(name, electric_or_magnetic,
                      array_class(grid, 3, field))
     self.field_filename = field_filename
Example #6
0
 def test_node_coordinates(self):
     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(
         MeshGrid.from_step((4, 2, 3), (2, 1, 3)).node_coordinates, coords)
     assert_array_equal(
         MeshGrid.from_step((4, 2, 3), (2, 1, 3),
                            (1, 2, 3.14)).node_coordinates,
         coords + [1, 2, 3.14])
Example #7
0
 def test_do_init(self):
     a = ArrayOnGrid(MeshGrid.from_step((12, 12, 12), (4, 4, 6)))
     a.apply_boundary_values(BoundaryConditionsConf(1, 2, 3, 4, 5, 6))
     expected = np.array([[[5., 1., 6.], [5., 1., 6.], [5., 1., 6.],
                           [5., 1., 6.]],
                          [[5., 3., 6.], [5., 0., 6.], [5., 0., 6.],
                           [5., 4., 6.]],
                          [[5., 3., 6.], [5., 0., 6.], [5., 0., 6.],
                           [5., 4., 6.]],
                          [[5., 2., 6.], [5., 2., 6.], [5., 2., 6.],
                           [5., 2., 6.]]])
     assert_array_equal(a.data, expected)
Example #8
0
    def test_transfer_solution_to_spat_mesh(self):
        mesh = MeshGrid.from_step((4, 6, 9), (1, 2, 3))
        solver = FieldSolver(mesh, [])
        solver.phi_vec = np.array(range(1, 3 * 2 * 2 + 1))
        potential = ArrayOnGrid(mesh)
        solver.transfer_solution_to_spat_mesh(potential)
        assert_array_equal(
            potential.data[1:-1, 1:-1, 1:-1],
            [[[1, 7], [4, 10]], [[2, 8], [5, 11]], [[3, 9], [6, 12]]])

        assert_array_equal(
            potential.data,
            [[[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 #9
0
 def test_construct_equation_matrix(self):
     mesh = MeshGrid.from_step((4, 6, 9), (1, 2, 3))
     solver = FieldSolver(mesh, [])
     solver.construct_equation_matrix()
     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 #10
0
 def test_do_init_ranges(self):
     with raises(ValueError):
         MeshGrid.from_step((10, 20), (2, 1, 3))
     with raises(ValueError):
         MeshGrid.from_step(((1, 2), 3), (1, 1, 1))
     with raises(ValueError):
         MeshGrid.from_step((10, 10, 10), [[2, 1, 3], [4, 5, 6], [7, 8, 9]],
                            BoundaryConditionsConf(3.14))
     with raises(ValueError):
         MeshGrid.from_step((10, 10, -30), (2, 1, 3))
     with raises(ValueError):
         MeshGrid.from_step((10, 10, 10), (2, -2, 3))
     mesh = MeshGrid.from_step((10, 10, 10), (17, 2, 3))
     assert tuple(mesh.cell) == (10, 2, 2.5)
Example #11
0
 def test_cell(self):
     assert_array_equal(MeshGrid(10, 11, 7).cell, (1, 1, 1))
     assert_array_equal(
         MeshGrid.from_step(10, [1, 2, 4]).cell, [1, 2, 10 / 3])
Example #12
0
 def test_from_step(self):
     assert_dataclass_eq(MeshGrid.from_step(10, 1, 7), MeshGrid(10, 11, 7))
     assert_dataclass_eq(MeshGrid.from_step([10, 20, 5], [1, 4, 1]),
                         MeshGrid([10, 20, 5], [11, 6, 6]))
     assert_dataclass_eq(MeshGrid.from_step(10, [1, 2, 4]),
                         MeshGrid(10, [11, 6, 4]))