def test_init(self): ir = InnerRegion('test', Box()) assert ir.name == 'test' assert_dataclass_eq(ir.shape, Box()) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 assert ir.inverted == False
def test_absorb_charge_inverted(self): particles = ParticleArray([1], -2.0, 1.0, (0, 0, 0), np.zeros(3)) ir = InnerRegion('test', Box(), inverted=True) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 assert_dataclass_eq( particles, ParticleArray([1], -2.0, 1.0, np.zeros((1, 3)), np.zeros((1, 3)))) particles = ParticleArray([1], -2.0, 1.0, (10, 10, 10), np.zeros(3)) ir = InnerRegion('test', Box(), inverted=True) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 1 assert ir.total_absorbed_charge == -2 assert_dataclass_eq( particles, ParticleArray([], -2.0, 1.0, np.zeros((0, 3)), np.zeros((0, 3)))) particles = ParticleArray([1, 2], -2.0, 1.0, [(0, 0, 0), (10, 10, 10)], np.zeros((2, 3))) ir = InnerRegion('test', Box(), inverted=True) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 1 assert ir.total_absorbed_charge == -2 assert_dataclass_eq( particles, ParticleArray([1], -2.0, 1.0, [(0, 0, 0)], np.zeros((1, 3))))
def test_absorb_charge(self): particles = ParticleArray([1], -2.0, 1.0, (0, 0, 0), np.zeros(3)) ir = InnerRegion('test', Box()) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 1 assert ir.total_absorbed_charge == -2 assert particles == ParticleArray([], -2.0, 1.0, np.zeros((0, 3)), np.zeros((0, 3))) particles = ParticleArray([1], -2.0, 1.0, (10, 10, 10), np.zeros(3)) ir = InnerRegion('test', Box()) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 assert particles == ParticleArray([1], -2.0, 1.0, [(10, 10, 10)], np.zeros((1, 3))) particles = ParticleArray([1, 2], -2.0, 1.0, [(0, 0, 0), (10, 10, 10)], np.zeros((2, 3))) ir = InnerRegion('test', Box()) assert ir.total_absorbed_particles == 0 assert ir.total_absorbed_charge == 0 ir.collide_with_particles(particles) assert ir.total_absorbed_particles == 1 assert ir.total_absorbed_charge == -2 assert particles == ParticleArray([2], -2.0, 1.0, [(10, 10, 10)], np.zeros((1, 3)))
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]])
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])
def test_particle_ids(self): p1 = ParticleSource("test", Box(), 120, 5, np.array((1, 0, 0)), 200, -1, 2) a1 = p1.generate_initial_particles() a2 = p1.generate_each_step() assert_array_equal(a1.ids, range(120)) assert_array_equal(a2.ids, range(5))
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])
def test_generate(self): p1 = ParticleSource("test", Box(), 120, 5, np.array((1, 0, 0)), 200, -1, 2) p2 = ParticleSource("test", Box(), 120, 5, np.array((1, 0, 0)), 200, -1, 2) p3 = ParticleSource("test", Box(), 120, 5, np.array((1, 0, 0)), 200, -1, 2) p3._generator.set_state(p1._generator.get_state()) a1 = p1.generate_initial_particles() a2 = p2.generate_initial_particles() a3 = p3.generate_initial_particles() assert a1 != a2 assert a1 == a3 a32 = p3.generate_each_step() assert a1 != a32 a12 = p1.generate_each_step() assert a12 == a32
def test_generate_positions(self): ps = ParticleSource() ps._generator = RandomState(123) p = ps.generate_num_of_particles(100) assert_ae = p.xp.testing.assert_array_equal assert_ae( p.positions, Box().generate_uniform_random_posititons(RandomState(123), 100))
def test_h5(self, tmpdir): fname = tmpdir.join('test_particle_source.h5') p1 = ParticleSource("test", Box(), 120, 5, np.array((1, 0, 0)), 0, -1, 2) with h5py.File(fname, mode="w") as h5file: p1.save_h5(h5file) with h5py.File(fname, mode="r") as h5file: p2 = ParticleSource.load_h5(h5file) assert p1 == p2
def test_generate_for_simulation(self): ps = ParticleSource('test', Box(6, 0), 17, 13, (4, 4, 4), 0, -2, 6) assert_dataclass_eq( ps.generate_initial_particles(), ParticleArray(range(17), -2, 6, np.full((17, 3), 6), np.full((17, 3), 4), False)) assert_dataclass_eq( ps.generate_each_step(), ParticleArray(range(13), -2, 6, np.full((13, 3), 6), np.full((13, 3), 4), False))
def test_init(self): p = ParticleSource() assert p.name == "particle_source" assert_dataclass_eq(p.shape, Box()) assert p.initial_number_of_particles == 0 assert p.particles_to_generate_each_step == 0 assert_array_equal(p.mean_momentum, np.zeros(3)) assert p.temperature == 0 assert p.charge == 0 assert p.mass == 1
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_generate_particles(self): ps = ParticleSource('test', Box((1., 2., 3.), 0), 17, 13, (-2, 3, 1), 0, -2, 6) assert_dataclass_eq( ps.generate_num_of_particles(3), ParticleArray(range(3), -2, 6, [(1, 2, 3)] * 3, [(-2, 3, 1)] * 3, False)) assert_dataclass_eq( ps.generate_num_of_particles(1), ParticleArray([0], -2, 6, [(1, 2, 3)], [(-2, 3, 1)], False)) assert_dataclass_eq( ps.generate_num_of_particles(0), ParticleArray([], -2, 6, np.empty((0, 3)), np.empty((0, 3)), False))
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]])