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 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])