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_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_export_h5(self): f = BytesIO() p1 = ParticleSource() with h5py.File(f, mode="w") as h5file: p1.export_h5(h5file.create_group(p1.name)) with h5py.File(f, mode="r") as h5file: p2 = ParticleSource.import_h5(h5file[p1.name]) assert_dataclass_eq(p1, p2)
def test_generate_momentums(self, backend): ps = ParticleSource(mean_momentum=(3, -2, 0), temperature=10, mass=.3) ps._generator = RandomState(123) p = ps.generate_num_of_particles(1000000) assert_almost_ae = p.xp.testing.assert_array_almost_equal assert_almost_ae(p.momentums.mean(axis=0), (3, -2, 0), 2) assert_almost_ae(((p.momentums - p.xp.array([3, -2, 0])).std(axis=0)), np.full(3, sqrt(3)), 2)
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 sim_full(): return Simulation( TimeGrid(200, 2, 20), MeshGrid(5, 51), particle_sources=[ ParticleSource('a', Box()), ParticleSource('c', Cylinder()), ParticleSource('d', Tube(start=(0, 0, 0), end=(0, 0, 1))) ], inner_regions=[ InnerRegion('1', Box(), 1), InnerRegion('2', Sphere(), -2), InnerRegion('3', Cylinder(), 0), InnerRegion('4', Tube(), 4) ], particle_interaction_model=Model.binary, electric_fields=[FieldUniform('x', 'electric', (-2, -2, 1))], magnetic_fields=[ FieldExpression('y', 'magnetic', '0', '0', '3*x + sqrt(y) - z**2') ])
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))