def setUp(self): nx = ny = nz = 10 n = nx * nx * nz self.particles = HydroParticleCreator(num=n, dim=3) # create lattice particles in a unit box L = 1. dx = L / nx dy = L / ny dz = L / nz self.volume = dx * dy * dz part = 0 for i in range(nx): for j in range(ny): for k in range(nz): self.particles['position-x'][part] = (i + 0.5) * dx self.particles['position-y'][part] = (j + 0.5) * dy self.particles['position-z'][part] = (k + 0.5) * dz part += 1 # create unit square domain, reflective boundary condition minx = np.array([0., 0., 0.]) maxx = np.array([1., 1., 1.]) self.domain_manager = DomainManager(initial_radius=0.1, search_radius_factor=1.25) self.domain_manager.set_domain_limits(DomainLimits(minx, maxx, dim=3)) self.domain_manager.register_fields(self.particles) self.domain_manager.set_boundary_condition(Reflective()) self.domain_manager.initialize() self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize()
def test_compute_states(self): """Test reconstruction to left and right of face.""" # domain manager domain_manager = DomainManager(0.2) # create mesh mesh = Mesh() mesh.register_fields(self.particles) mesh.initialize() # hard code mesh faces (1 face) mesh.faces.resize(1) mesh.faces["pair-i"][0] = 0 # left particle mesh.faces["pair-j"][0] = 1 # right particle # construct left/right faces self.reconstruction.compute_states(self.particles, mesh, False, domain_manager, dt=1.0) self.assertTrue(True) # left and right states should be the same for field in self.reconstruction.left_states.properties.keys(): self.assertAlmostEqual(self.reconstruction.left_states[field][0], self.particles[field][0]) self.assertAlmostEqual(self.reconstruction.right_states[field][0], self.particles[field][1])
def test_second_pass_reflection_serial(self): # create particle in center of lower left quadrant particles = HydroParticleCreator(num=1, dim=2) particles['position-x'][0] = 0.25 particles['position-y'][0] = 0.25 # create unit square domain self.domain_manager.set_domain(DomainLimits()) self.domain_manager.register_fields(particles) self.domain_manager.set_boundary_condition(Reflective()) mesh = Mesh() mesh.register_fields(particles) mesh.initialize() # set infinte radius flag FIX add variable instead of magic number particles['radius'][0] = -1 self.domain_manager.setup_for_ghost_creation(particles) self.domain_manager.create_ghost_particles(particles) # two ghost particles created self.assertTrue(particles.get_carray_size() == 3) particles['radius'][0] = 0.6 self.domain_manager.update_search_radius(particles) self.domain_manager.create_ghost_particles(particles) # no new ghost should be created self.assertTrue(particles.get_carray_size() == 3)
def setUp(self): n = 100 self.particles = HydroParticleCreator(num=n, dim=2) # create uniform random particles in a unit box np.random.seed(0) self.particles["position-x"][:] = np.random.uniform(size=n) self.particles["position-y"][:] = np.random.uniform(size=n) # create unit square domain, reflective boundary condition minx = np.array([0., 0.]) maxx = np.array([1., 1.]) self.domain_manager = DomainManager(initial_radius=0.1, search_radius_factor=1.25) self.domain_manager.set_domain_limits(DomainLimits(minx, maxx)) self.domain_manager.register_fields(self.particles) self.domain_manager.set_boundary_condition(Reflective()) self.domain_manager.initialize() self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize()
def setUp(self): """Test reconstruction to left and right of face.""" # create 2 particles with constant values self.particles = HydroParticleCreator(num=2, dim=2) # mesh class self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize() # equation of state class self.eos = IdealGas(gamma=1.4) # reconstruction class self.reconstruction = PieceWiseConstant() self.reconstruction.set_fields_for_reconstruction( self.particles) self.reconstruction.initialize() # riemann class self.riemann = HLL(boost=False) self.riemann.fields_to_add(self.particles) self.riemann.initialize()
class TestMeshSetup2d(unittest.TestCase): """Tests for the Reconstruction class.""" def setUp(self): self.mesh = Mesh(param_dim=2) def test_register_fields(self): # create 2d particles particles = HydroParticleCreator(num=1, dim=2) fields = list(particles.properties) # check if correct fields where registered self.mesh.register_fields(particles) reg_fields_2d = ["volume", "dcom-x", "dcom-y", "w-x", "w-y"] for field in reg_fields_2d: self.assertTrue(field in particles.properties.keys()) # check right number of fields self.assertEqual(particles.properties.keys().sort(), (fields + reg_fields_2d).sort()) # check named groups added correctly self.assertEqual(particles.named_groups["w"], ["w-x", "w-y"]) self.assertEqual(particles.named_groups["dcom"], ["dcom-x", "dcom-y"]) def test_register_fields_errors(self): # register fields but wrong dimensions particles = HydroParticleCreator(num=1, dim=4) self.assertRaises(RuntimeError, self.mesh.register_fields, particles) def test_initialize_errors(self): # fields not registered, throws run time error self.assertRaises(RuntimeError, self.mesh.initialize) def test_initialize(self): # create 2d particles particles = HydroParticleCreator(num=1, dim=2) self.mesh.register_fields(particles) self.mesh.initialize() # fields to create in 2d face_vars_2d = [ "area", "pair-i", "pair-j", "com-x", "com-y", "velocity-x", "velocity-y", "normal-x", "normal-y" ] # check if correct fields registered for field in face_vars_2d: self.assertTrue(field in self.mesh.faces.properties.keys()) # check right number of fields self.assertEqual(self.mesh.faces.properties.keys().sort(), face_vars_2d.sort())
class TestMeshSetup3d(TestMeshSetup2d): def setUp(self): self.dim = 3 self.mesh = Mesh() def test_register_fields(self): particles = HydroParticleCreator(num=1, dim=self.dim) fields = list(particles.carrays) # check if correct fields where registered self.mesh.register_fields(particles) reg_fields_3d = [ "volume", "dcom-x", "dcom-y", "dcom-z", "w-x", "w-y", "w-z" ] for field in reg_fields_3d: self.assertTrue(field in list(particles.carrays.keys())) # check right number of fields self.assertEqual( list(particles.carrays.keys()).sort(), (fields + reg_fields_3d).sort()) # check named groups added correctly self.assertEqual(particles.carray_named_groups["w"], ["w-x", "w-y", "w-z"]) self.assertEqual(particles.carray_named_groups["dcom"], ["dcom-x", "dcom-y", "dcom-z"]) def test_initialize(self): particles = HydroParticleCreator(num=1, dim=self.dim) self.mesh.register_fields(particles) self.mesh.initialize() # fields to create in 2d face_vars_3d = [ "area", "pair-i", "pair-j", "com-x", "com-y", "com-z", "velocity-x", "velocity-y", "velocity-z", "normal-x", "normal-y", "normal-z" ] # check if correct fields registered for field in face_vars_3d: self.assertTrue(field in list(self.mesh.faces.carrays.keys())) # check right number of fields self.assertEqual( list(self.mesh.faces.carrays.keys()).sort(), face_vars_3d.sort())
def test_compute_states(self): # create 2 particles with constant values for field in self.particles.carray_named_groups["primitive"]: self.particles[field][:] = 1.0 self.recon.add_fields(self.particles) self.recon.initialize() # domain manager domain_manager = DomainManager(0.2) # create mesh mesh = Mesh() mesh.register_fields(self.particles) mesh.initialize() # hard code mesh faces (1 face) mesh.faces.resize(1) mesh.faces["pair-i"][0] = 0 # left particle mesh.faces["pair-j"][0] = 1 # right particle # construct left/right faces self.recon.compute_states(self.particles, mesh, False, domain_manager, dt=1.0) # left and right states should be the same for field in self.recon.left_states.carrays.keys(): self.assertAlmostEqual(self.recon.left_states[field][0], self.particles[field][0]) self.assertAlmostEqual(self.recon.right_states[field][0], self.particles[field][1])
def test_setup_for_ghost_creation_reflection(self): # create particle in center of lower left quadrant particles = HydroParticleCreator(num=1, dim=2) particles['position-x'][0] = 0.25 particles['position-y'][0] = 0.25 # create unit square domain self.domain_manager.set_domain(DomainLimits()) self.domain_manager.register_fields(particles) self.domain_manager.set_boundary_condition(Reflective()) mesh = Mesh() mesh.register_fields(particles) mesh.initialize() # set infinte radius flag FIX add variable instead of magic number particles['radius'][0] = -1 self.domain_manager.setup_for_ghost_creation(particles) self.domain_manager.create_ghost_particles(particles) # two ghost particles created self.assertTrue(particles.get_carray_size() == 3) # two ghost particles should of been created # first particle reflected across x-min self.assertEqual(particles['position-x'][1], -0.25) self.assertEqual(particles['position-y'][1], 0.25) # second particle reflected across y-min self.assertEqual(particles['position-x'][2], 0.25) self.assertEqual(particles['position-y'][2], -0.25) # should have particle in flagged buffer self.assertFalse(self.domain_manager.ghost_complete()) # update search radius to remove particle from being flagged particles['radius'][1:] = 0.3 self.domain_manager.update_search_radius(particles) self.assertTrue(self.domain_manager.ghost_complete())
def test_check_initial_radius(self): # create particle in center of lower left quadrant particles = HydroParticleCreator(num=1, dim=2) particles['position-x'][0] = 0.125 particles['position-y'][0] = 0.125 # create unit square domain self.domain_manager.set_domain(DomainLimits()) self.domain_manager.register_fields(particles) self.domain_manager.set_boundary_condition(Reflective()) mesh = Mesh() mesh.register_fields(particles) mesh.initialize() # set infinte radius flag FIX add variable instead of magic number particles['radius'][0] = -1 self.domain_manager.setup_for_ghost_creation(particles) self.assertTrue(particles['radius'][0] == 0.25) self.assertTrue(particles['old_radius'][0] == 0.25)
class TestMesh2dUniformBox(unittest.TestCase): def setUp(self): n = 100 self.particles = HydroParticleCreator(num=n, dim=2) # create uniform random particles in a unit box np.random.seed(0) self.particles["position-x"][:] = np.random.uniform(size=n) self.particles["position-y"][:] = np.random.uniform(size=n) # create unit square domain, reflective boundary condition minx = np.array([0., 0.]) maxx = np.array([1., 1.]) self.domain_manager = DomainManager(initial_radius=0.1, search_radius_factor=1.25) self.domain_manager.set_domain_limits(DomainLimits(minx, maxx)) self.domain_manager.register_fields(self.particles) self.domain_manager.set_boundary_condition(Reflective()) self.domain_manager.initialize() self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize() def test_volume(self): """ Test if particle volumes in a square are created correctly. Create grid of particles in a unit box, total volume is 1.0. """ # generate voronoi mesh self.mesh.build_geometry(self.particles, self.domain_manager) # calculate voronoi volumes of all real particles real_indices = self.particles["tag"] == ParticleTAGS.Real tot_vol = np.sum(self.particles["volume"][real_indices]) # total mass should be equal to the volume of the box self.assertAlmostEqual(tot_vol, 1.0)
class TestHLLFlux(unittest.TestCase): """Tests for the Reconstruction class.""" def setUp(self): """Test reconstruction to left and right of face.""" # create 2 particles with constant values self.particles = HydroParticleCreator(num=2, dim=2) # mesh class self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize() # equation of state class self.eos = IdealGas(gamma=1.4) # reconstruction class self.reconstruction = PieceWiseConstant() self.reconstruction.set_fields_for_reconstruction( self.particles) self.reconstruction.initialize() # riemann class self.riemann = HLL(boost=False) self.riemann.fields_to_add(self.particles) self.riemann.initialize() def test_left_state(self): lt = self.reconstruction.left_states rt = self.reconstruction.right_states lt.resize(1); rt.resize(1) # both particles have the same value lt["density"][:] = 1.0; rt["density"][:] = 1.0 lt["velocity-x"][:] = 2.0; rt["velocity-x"][:] = 2.0 lt["velocity-y"][:] = 0.0; rt["velocity-y"][:] = 0.0 lt["pressure"][:] = 0.4; rt["pressure"][:] = 0.4 faces = self.mesh.faces faces.resize(1) # the face is perpendicular to the x-axis and has velocity zero faces["normal-x"][0] = 1; faces["normal-y"][0] = 0 faces["velocity-x"][0] = 0; faces["velocity-y"][0] = 0 ans = {"mass": 2.0, "momentum-x": 4.4, "momentum-y": 0.0, "energy": 6.8} self.riemann.compute_fluxes(self.particles, self.mesh, self.reconstruction, self.eos) for field in list(self.riemann.fluxes.carrays.keys()): self.assertAlmostEqual(self.riemann.fluxes[field][0], ans[field]) # reverse fluid flow lt["velocity-x"][:] = -2.; rt["velocity-x"][:] = -2. ans = {"mass": -2.0, "momentum-x": 4.4, "momentum-y": 0.0, "energy": -6.8} self.riemann.compute_fluxes(self.particles, self.mesh, self.reconstruction, self.eos) # left and right states should be the same for field in list(self.riemann.fluxes.carrays.keys()): self.assertAlmostEqual(self.riemann.fluxes[field][0], ans[field])
def setUp(self): self.dim = 3 self.mesh = Mesh()
class TestMesh2dLatticeBox(unittest.TestCase): def setUp(self): nx = ny = 10 n = nx * nx self.particles = HydroParticleCreator(num=n, dim=2) # create lattice particles in a unit box L = 1. dx = L / nx dy = L / ny self.volume = dx * dy part = 0 for i in range(nx): for j in range(ny): self.particles['position-x'][part] = (i + 0.5) * dx self.particles['position-y'][part] = (j + 0.5) * dy part += 1 # create unit square domain, reflective boundary condition minx = np.array([0., 0.]) maxx = np.array([1., 1.]) self.domain_manager = DomainManager(initial_radius=0.1, search_radius_factor=1.25) self.domain_manager.set_domain_limits(DomainLimits(minx, maxx)) self.domain_manager.register_fields(self.particles) self.domain_manager.set_boundary_condition(Reflective()) self.domain_manager.initialize() self.mesh = Mesh() self.mesh.register_fields(self.particles) self.mesh.initialize() def test_volume(self): """ Test if particle volumes in a square are created correctly. Create grid of particles in a unit box, total volume is 1.0. """ # generate voronoi mesh self.mesh.build_geometry(self.particles, self.domain_manager) # sum voronoi volumes of all real particles real_indices = self.particles["tag"] == ParticleTAGS.Real tot_vol = np.sum(self.particles["volume"][real_indices]) # total mass should be equal to the volume of the box self.assertAlmostEqual(tot_vol, 1.0) def test_center_of_mass(self): """ Test if particle center of mass positions are created correctly. Particles are placed in a uniform lattice. Therefore the center of mass is the same as particle positions. """ # generate voronoi mesh self.mesh.build_geometry(self.particles, self.domain_manager) dim = len(self.particles.carray_named_groups["position"]) axis = "xyz"[:dim] # check if particle position is the same as center of mass for i in range(self.particles.get_carray_size()): if self.particles["tag"][i] == ParticleTAGS.Real: for ax in axis: self.assertAlmostEqual(0., self.particles["dcom-" + ax][i]) def test_particle_volume(self): """ Test if particle center of mass positions are created correctly. Particles are placed in a uniform lattice. Therefore the center of mass is the same as particle positions. """ # generate voronoi mesh self.mesh.build_geometry(self.particles, self.domain_manager) # check if particle position is the same as center of mass for i in range(self.particles.get_carray_size()): if self.particles["tag"][i] == ParticleTAGS.Real: self.assertAlmostEqual(self.volume, self.particles["volume"][i])
def setUp(self): self.mesh = Mesh(param_dim=2)