def setUp(self): """ Basic setup for testing the kernel. Setup: ------ The kernel is assumed to be placed at the origon. Particle spacing dx = 0.1 Expected Result: ---------------- The kernel and it's gradient is integrated (summation) over the domain, ensuring no boundary truncation. The result should be 1 and 0 respectively. """ #Set the limits for Summation. self.x = x = numpy.linspace(-1, 1, 21) self.y = numpy.linspace(-1, 1, 21) self.dx = dx = x[1] - x[0] self.hs = 0.2 self.pnt = Point() self.pnts = [Point(i) for i in x] self.setup()
def test_gradient2D(self): """ Test for the normalization of the kernel in 1D. """ print "W8Kernel::py_gradient 2D Normalization. ", print "10 decimal places" #Test using Summation Formula xg = self.xg yg = self.yg dx = self.dx hs = self.hs kernel = self.kernel pnt = self.pnt pnts = self.pnts grad = Point() #Get the 2D Volume nvol = dx * dx val = Point() for p in pnts: kernel.py_gradient(pnt, p, hs, grad) val.x += grad.x * nvol val.y += grad.y * nvol self.assertEqual(grad.z, 0) self.assertAlmostEqual(val.x, 0, 10) self.assertAlmostEqual(val.y, 0, 10)
def __init__(self, output_particle_arrays=[], particle_mass=-1.0, mass_computation_mode=MCM.Compute_From_Density, particle_density=1000.0, density_computation_mode=DCM.Set_Constant, particle_h=0.1, kernel=None, start_point=Point(0, 0, 0), end_point=Point(0, 0, 1), particle_spacing=0.05, end_points_exact=True, tolerance=1e-09, *args, **kwargs): """ """ self.start_point = Point(start_point.x, start_point.y, start_point.z) self.end_point = Point(end_point.x, end_point.y, end_point.z) self.particle_spacing = particle_spacing self.end_points_exact = end_points_exact self.tolerance = tolerance
def test_gradient1D(self): """ Test for the normalization of the kernel in 1D. """ print "W8Kernel::py_gradient 1D Normalization. ", print "4 decimal places" #Test using Summation formula. x = self.x dx = self.dx hs = self.hs val = 0.0 kernel = kernels.W8Kernel(dim=1) pnt = Point() grad = Point() pnts = [Point(i) for i in x] for p in pnts: kernel.py_gradient(pnt, p, hs, grad) val += grad.x * dx self.assertEqual(grad.y, 0) self.assertEqual(grad.z, 0) self.assertAlmostEqual(val, 0, 4)
def test_get_bbox(): cell_size = 0.1 cell = nnps.Cell(IntPoint(0, 0, 0), cell_size=cell_size, narrays=1) centroid = Point() boxmin = Point() boxmax = Point() cell.get_centroid(centroid) cell.get_bounding_box(boxmin, boxmax) assert (abs(boxmin.x - (centroid.x - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.y - (centroid.y - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.z - (centroid.z - 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.x - (centroid.x + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.y - (centroid.y + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.z - (centroid.z + 1.5 * cell_size)) < 1e-10) cell_size = 0.5 cell = nnps.Cell(IntPoint(1, 2, 0), cell_size=cell_size, narrays=1) cell.get_centroid(centroid) cell.get_bounding_box(boxmin, boxmax) assert (abs(boxmin.x - (centroid.x - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.y - (centroid.y - 1.5 * cell_size)) < 1e-10) assert (abs(boxmin.z - (centroid.z - 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.x - (centroid.x + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.y - (centroid.y + 1.5 * cell_size)) < 1e-10) assert (abs(boxmax.z - (centroid.z + 1.5 * cell_size)) < 1e-10)
def compute_particle_mass(parray, kernel, density=1000.0, h=0.1, dim=3): """ Given a particle array, kernel, target density and interaction radius, find the mass of each particle. Note that this method works only when the particle radius is constant. This may also compute incorrect values when the particle cofiguration has voids within. """ centroid = Point(0, 0, 0) dist = DoubleArray(0) indices = LongArray(0) x = parray.get('x') centroid.x = numpy.sum(x)/float(len(x)) y = None z = None logger.debug('particles to compute_particle_mass %d'%(len(x))) if dim > 1: y = parray.get('y') centroid.y = numpy.sum(y)/float(len(y)) if dim > 2: z = parray.get('z') centroid.z = numpy.sum(z)/float(len(z)) else: z = numpy.zeros(len(x), dtype=numpy.float) else: y = numpy.zeros(len(x), dtype=numpy.float) z = y logger.debug('Centroid : %s'%(centroid)) radius = kernel.radius() # find the nearest points in parray of the centroid. brute_force_nnps(pnt=centroid, search_radius=h*radius, xa=x, ya=y, za=z, neighbor_indices=indices, neighbor_distances=dist) k = 0.0 logger.info('Number of neighbors : %d'%(indices.length)) pnt = Point() for i in range(indices.length): pnt.x = x[indices[i]] pnt.y = y[indices[i]] pnt.z = z[indices[i]] k += kernel.py_function(centroid, pnt, h) logger.info('Kernel sum : %f'%(k)) logger.info('Requested density : %f'%(density)) m = float(density/k) logger.info('Computed mass : %f'%(m)) return m
def _testgrad(self, value, places): x = self.x dx = self.dx hs = self.hs val = Point() grad = Point() for p in self.pnts: self.kernel.py_gradient(self.pnt, p, hs, grad) val.x += grad.x * dx self.assertAlmostEqual(val.x, value, places) self.assertEqual(val.y, 0) self.assertEqual(val.z, 0)
def test_get_nearest_particles(self): """Tests the get_nearest_particles. """ parrs = generate_sample_dataset_1() cm = CellManager(arrays_to_bin=parrs, min_cell_size=1., max_cell_size=2.0) nbrl = FixedDestNbrParticleLocator(parrs[0], parrs[1], 1.0, cm, 'h') nbrl4 = FixedDestNbrParticleLocator(parrs[0], parrs[1], 4.0, cm, 'h') nbrl05 = FixedDestNbrParticleLocator(parrs[0], parrs[1], 0.5, cm, 'h') # querying neighbors of dark point 4, with radius 0.5 output_array = nbrl05.py_get_nearest_particles(3) self.assertEqual(output_array.length, 1) self.assertEqual(output_array[0], 0) # querying neighbors of dark point 4, with radius 1 output_array = nbrl.py_get_nearest_particles(3) self.assertEqual(output_array.length, 4) a = list(output_array.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # querying neighbors of dark point 3, with radius 4.0 output_array = nbrl4.py_get_nearest_particles(2) self.assertEqual(output_array.length, 8) a = list(output_array.get_npy_array()) for i in range(8): self.assertEqual(a.count(i), 1) nbrl = FixedDestNbrParticleLocator(parrs[0], parrs[1], 2.0, cm) nbrl.py_update() bnpl = NbrParticleLocatorBase(parrs[0], cm) a2 = LongArray() xa, ya, za = parrs[1].get('x'), parrs[1].get('y'), parrs[1].get('z') pnt = Point(xa[0], ya[0], za[0]) nbrl.py_update() a1 = nbrl.py_get_nearest_particles(0) bnpl.py_get_nearest_particles_to_point(pnt, 2.0, a2) self.assertEqual(set(a1.get_npy_array()), set(a2.get_npy_array())) # test for another point. pnt = Point(xa[3], ya[3], za[3]) a1 = nbrl.py_get_nearest_particles(3) bnpl.py_get_nearest_particles_to_point(pnt, 2.0, a2) self.assertEqual(set(a1.get_npy_array()), set(a2.get_npy_array()))
def test_brute_force_nnps(self): """ Tests the brute-force nnps For a graphical view of the test dataset, refer image test_cell_data1.png. """ nbr_indices = LongArray() nbr_distances = DoubleArray() parrs = generate_sample_dataset_1() xa = parrs[0].get('x') ya = parrs[0].get('y') za = parrs[0].get('z') pnt = Point(0.4, 0.0, 0.4) brute_force_nnps(pnt, 1.0, xa, ya, za, nbr_indices, nbr_distances) self.assertEqual(nbr_indices.length, 4) a = list(nbr_indices.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # now querying for neighbors from dark particles. xa = parrs[1].get('x') ya = parrs[1].get('y') za = parrs[1].get('z') # searching from the center (1., 1., 1.) with different radii. pnt = Point(1, 1, 1) nbr_indices.reset() nbr_distances.reset() brute_force_nnps(pnt, 1.4142135623730951, xa, ya, za, nbr_indices, nbr_distances) self.assertEqual(nbr_indices.length, 3) a = list(nbr_indices.get_npy_array()) self.assertEqual(a.count(1), 1) self.assertEqual(a.count(3), 1) self.assertEqual(a.count(0), 1) # test with exclude_index argument nbr_indices.reset() nbr_distances.reset() brute_force_nnps(pnt, 1.4142135623730951, xa, ya, za, nbr_indices, nbr_distances, exclude_index=1) self.assertEqual(nbr_indices.length, 2) a = list(nbr_indices.get_npy_array()) self.assertEqual(a.count(1), 0) self.assertEqual(a.count(3), 1) self.assertEqual(a.count(0), 1)
def test_moment2D(self): """ Test for the first moment of the function in 2D """ print "W8Kernel::py_function 2D Moment", print " 5 decimal places" #Test using Summation Formula xg = self.xg yg = self.yg dx = self.dx hs = self.hs kernel = self.kernel pnt = self.pnt pnts = self.pnts #Get the 2D Volume nvol = dx * dx val = Point() for p in pnts: val.x += (pnt.x - p.x) * (kernel.py_function(pnt, p, hs) * nvol) val.y += (pnt.y - p.y) * (kernel.py_function(pnt, p, hs) * nvol) self.assertAlmostEqual(val.x, 0, 5) self.assertAlmostEqual(val.y, 0, 5)
def test_(self): print "Testing the expression" x = self.x dx = self.dx hs = self.hs val = 0.0 kernel = kernels.CubicSplineKernel(dim=1) pnt = Point() grad = Point() pnts = [Point(i) for i in x] for p in pnts: kernel.py_gradient(pnt, p, hs, grad) rab = pnt.x = p.x if abs(rab) > 1e-16: val += 2 * dx / (rab * rab) * (p.x - pnt.x) * rab * grad.x self.assertAlmostEqual(val, 0, 10)
def test_moment1D(self): """ Test for the first moment of the kernel """ print "W8Kernel::py_function 1D Moment ", print " 5 decimal places" #Test using Summation formula. x = self.x dx = self.dx hs = self.hs val = 0.0 kernel = kernels.W8Kernel(dim=1) pnt = Point() pnts = [Point(i) for i in x] for p in pnts: val += (pnt.x - p.x) * kernel.py_function(pnt, p, hs) * dx self.assertAlmostEqual(val, 0, 5)
def test_grad_moment1D(self): """ Test for the moment of the kernel """ print "CubicSplineKernel::py_gradient 1D Moment", print "10 decimal places" x = self.x dx = self.dx hs = self.hs val = 0.0 kernel = kernels.CubicSplineKernel(dim=1) pnt = Point() grad = Point() pnts = [Point(i) for i in x] for p in pnts: kernel.py_gradient(pnt, p, hs, grad) val += (pnt.x - p.x) * grad.x * dx self.assertAlmostEqual(val, -1, 10)
def test_constructor(self): """ Tests for the constructor. """ c = CuboidGenerator() self.assertEqual(c.start_point, Point(0, 0, 0)) self.assertEqual(c.end_point, Point(1, 1, 1)) self.assertEqual(c.particle_mass, -1.) self.assertEqual(c.mass_computation_mode, MCM.Compute_From_Density) self.assertEqual(c.particle_density, 1000.) self.assertEqual(c.density_computation_mode, DCM.Set_Constant) self.assertEqual(c.particle_h, 0.1) self.assertEqual(c.kernel, None) self.assertEqual(c.filled, True) self.assertEqual(c.exclude_top, False) self.assertEqual(c.start_point, Point(0, 0, 0)) self.assertEqual(c.end_point, Point(1, 1, 1)) self.assertEqual(c.particle_spacing_x, 0.1) self.assertEqual(c.particle_spacing_y, 0.1) self.assertEqual(c.particle_spacing_z, 0.1) self.assertEqual(c.end_points_exact, True) self.assertEqual(c.tolerance, 1e-09)
def __init__(self, output_particle_arrays=[], particle_mass=-1.0, mass_computation_mode=MCM.Compute_From_Density, particle_density=1000.0, density_computation_mode=DCM.Set_Constant, particle_h=0.1, kernel=None, filled=True, exclude_top=False, start_point=Point(0, 0, 0), end_point=Point(1, 1, 1), particle_spacing_x=0.1, particle_spacing_y=0.1, particle_spacing_z=0.1, end_points_exact=True, tolerance=1e-09, *args, **kwargs): """ Constructor. """ self.filled = filled self.exclude_top = exclude_top self.start_point = Point(start_point.x, start_point.y, start_point.z) self.end_point = Point(end_point.x, end_point.y, end_point.z) self.particle_spacing_x = particle_spacing_x self.particle_spacing_y = particle_spacing_y self.particle_spacing_z = particle_spacing_z self.end_points_exact = end_points_exact self.tolerance = tolerance
def setUp(self): """ Basic setup for testing the kernel. Setup: ------ The kernel is assumed to be placed at the origon. Particle spacing dx = 0.1 Expected Result: ---------------- The kernel and it's gradient is integrated (summation) over the domain, ensuring no boundary truncation. The result should be 1 and 0 respectively. """ #Set the limits for Summation. self.x = x = numpy.linspace(-1, 1, 21) self.y = numpy.linspace(-1, 1, 21) self.dx = dx = x[1] - x[0] self.hs = 2 * dx self.pnt = Point() self.pnts = [Point(i) for i in x] self.xg, self.yg = numpy.meshgrid(self.x, self.y) self.pnt = Point() self.pnts = [] #Create the other points. for i in range(21): xi = self.xg[i] for j in range(21): self.pnts.append(Point(xi[j], self.yg[i][0])) self.setup()
def test_get_centroid(): cell = nnps.Cell(IntPoint(0, 0, 0), cell_size=0.1, narrays=1) centroid = Point() cell.get_centroid(centroid) assert (abs(centroid.x - 0.05) < 1e-10) assert (abs(centroid.y - 0.05) < 1e-10) assert (abs(centroid.z - 0.05) < 1e-10) cell = nnps.Cell(IntPoint(1, 2, 3), cell_size=0.5, narrays=1) cell.get_centroid(centroid) assert (abs(centroid.x - 0.75) < 1e-10) assert (abs(centroid.y - 1.25) < 1e-10) assert (abs(centroid.z - 1.75) < 1e-10)
def __init__(self, input_particle_arrays=[], particle_mass=-1.0, mass_computation_mode=MCM.Compute_From_Density, particle_density=1000.0, density_computation_mode=DCM.Set_Constant, particle_h=0.1, kernel=None, filled=True, start_point=Point(0, 0, 0), end_point=Point(1, 1, 0), particle_spacing_x1=0.1, particle_spacing_x2=0.1, end_points_exact=True, tolerance=1e-09, *args, **kwargs): """ """ ParticleGenerator.__init__(self, input_particle_arrays=input_particle_arrays, particle_mass=particle_mass, mass_computation_mode=mass_computation_mode, particle_density=particle_density, density_computation_mode=density_computation_mode, particle_h=particle_h, kernel=kernel) self.filled = filled self.start_point = Point(start_point.x, start_point.y, start_point.z) self.end_point = Point(end_point.x, end_point.y, end_point.z) self.particle_spacing_x1 = particle_spacing_x1 self.particle_spacing_x2 = particle_spacing_x2 self.end_points_exact = end_points_exact self.tolerance = tolerance
def _testmom(self, value, places): x = self.x dx = self.dx hs = self.hs pnt = self.pnt kernel = self.kernel val = Point() for p in self.pnts: val.x += (pnt.x - p.x) * kernel.py_function(pnt, p, hs) * dx val.y += (pnt.y - p.y) * kernel.py_function(pnt, p, hs) * dx self.assertAlmostEqual(val.x, value, places) self.assertEqual(val.y, value, places) self.assertEqual(val.z, 0)
def test_function2D(self): """ Test for the NOrmalization of the kernel in 2D """ print "W8Kernel::py_function 2D Normalization", print " 4 decimal places" #Test using Summation Formula xg = self.xg yg = self.yg dx = self.dx hs = self.hs val = 0.0 kernel = self.kernel pnt = Point() pnts = [] #Get the 2D Volume nvol = dx * dx for p in self.pnts: val += self.kernel.py_function(pnt, p, hs) * nvol self.assertAlmostEqual(val, 1.0, 4)
def test_constructor(self): """ Tests the constructor. """ lg = LineGenerator() self.assertEqual(lg.start_point, Point(0, 0, 0)) self.assertEqual(lg.end_point, Point(0, 0, 1)) self.assertEqual(lg.particle_spacing, 0.05) self.assertEqual(lg.end_points_exact, True) self.assertEqual(lg.tolerance, 1e-09) lg = LineGenerator(start_point=Point(0, 1, 0), end_point=Point(1, 0, 0), particle_spacing=0.1) self.assertEqual(lg.start_point, Point(0, 1, 0)) self.assertEqual(lg.end_point, Point(1, 0, 0)) self.assertEqual(lg.particle_spacing, 0.1) self.assertEqual(lg.end_points_exact, True) self.assertEqual(lg.tolerance, 1e-09)
def test_constructor(self): """ """ rg = RectangleGenerator() self.assertEqual(rg.filled, True) self.assertEqual(rg.start_point, Point(0, 0, 0)) self.assertEqual(rg.end_point, Point(1, 1, 0)) self.assertEqual(rg.particle_spacing_x1, 0.1) self.assertEqual(rg.particle_spacing_x2, 0.1) self.assertEqual(rg.end_points_exact, True) self.assertEqual(rg.tolerance, 1e-09) rg = RectangleGenerator(start_point=Point(-1, 0, 0), end_point=Point(1, 1, 0), particle_spacing_x1=0.01, particle_spacing_x2=0.01) self.assertEqual(rg.start_point, Point(-1, 0, 0)) self.assertEqual(rg.end_point, Point(1, 1, 0)) self.assertEqual(rg.particle_spacing_x1, 0.01) self.assertEqual(rg.particle_spacing_x2, 0.01)
def test_get_nearest_particles_to_point(self): """Tests the get_nearest_particles_to_point function. For a graphical view of the test dataset, refer image test_cell_data1.png. """ parrs = generate_sample_dataset_1() cm = CellManager(arrays_to_bin=parrs, min_cell_size=1., max_cell_size=2.0) nbrl1 = NbrParticleLocatorBase(parrs[0], cm) # querying neighbors of dark point 4.(refer image) pnt = Point(0.4, 0.0, 0.4) output_array = LongArray() nbrl1.py_get_nearest_particles_to_point(pnt, 0.5, output_array) self.assertEqual(output_array.length, 1) self.assertEqual(output_array[0], 0) output_array.reset() nbrl1.py_get_nearest_particles_to_point(pnt, 1.0, output_array) self.assertEqual(output_array.length, 4) a = list(output_array.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # querying neighbors of dark point 3, with radius 4.0 pnt = Point(1.5, 0.0, -0.5) output_array.reset() nbrl1.py_get_nearest_particles_to_point(pnt, 4.0, output_array) self.assertEqual(output_array.length, 8) a = list(output_array.get_npy_array()) for i in range(8): self.assertEqual(a.count(i), 1) # now querying for neighbors from dark particles. nbrl2 = NbrParticleLocatorBase(parrs[1], cm) # searching from the center (1., 1., 1.) with different radii. pnt.set(1, 1, 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 0.1, output_array) self.assertEqual(output_array.length, 0) nbrl2.py_get_nearest_particles_to_point(pnt, 1.0, output_array) self.assertEqual(output_array.length, 0) nbrl2.py_get_nearest_particles_to_point(pnt, 1.136, output_array) self.assertEqual(output_array.length, 1) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.358, output_array) self.assertEqual(output_array.length, 2) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) self.assertEqual(a.count(3), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.4142135623730951, output_array) self.assertEqual(output_array.length, 3) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) self.assertEqual(a.count(3), 1) self.assertEqual(a.count(0), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.88, output_array) self.assertEqual(output_array.length, 4) a = list(output_array.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # test to make sure the exclude_index parameter is considered. output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.88, output_array, 3) self.assertEqual(output_array.length, 3) a = list(output_array.get_npy_array()) for i in range(3): self.assertEqual(a.count(i), 1)
def create_particles(options): if options.type == "square": # create the square block of particles. start_point = Point(0, 0, 0) end_point = Point(options.square_width, options.square_width, 0) parray = ParticleArray() if rank == 0: rg = RectangleGenerator( start_point=start_point, end_point=end_point, particle_spacing_x1=options.particle_spacing, particle_spacing_x2=options.particle_spacing, density_computation_mode=Dcm.Set_Constant, particle_density=1000.0, mass_computation_mode=Mcm.Compute_From_Density, particle_h=options.particle_radius, kernel=CubicSplineKernel(2), filled=True) tmp = rg.get_particles() parray.append_parray(tmp) if rank != 0: # add some necessary properties to the particle array. parray.add_property({'name': 'x'}) parray.add_property({'name': 'y'}) parray.add_property({'name': 'z'}) parray.add_property({ 'name': 'h', 'default': options.particle_radius }) parray.add_property({'name': 'rho', 'default': 1000.}) parray.add_property({'name': 'pid'}) parray.add_property({'name': '_tmp', 'default': 0.0}) parray.add_property({'name': 'm'}) else: parray.add_property({'name': '_tmp'}) parray.add_property({'name': 'pid', 'default': 0.0}) return [parray] elif options.type == "dam_break": dam_wall = ParticleArray() dam_fluid = ParticleArray() if rank == 0: radius = 0.2 dam_width = 10.0 dam_height = 7.0 solid_particle_h = radius dam_particle_spacing = radius / 9. solid_particle_mass = 1.0 origin_x = origin_y = 0.0 fluid_particle_h = radius fluid_density = 1000. fluid_column_height = 3.0 fluid_column_width = 2.0 fluid_particle_spacing = radius # generate the left wall - a line lg = LineGenerator(particle_mass=solid_particle_mass, mass_computation_mode=Mcm.Set_Constant, density_computation_mode=Dcm.Ignore, particle_h=solid_particle_h, start_point=Point(0, 0, 0), end_point=Point(0, dam_height, 0), particle_spacing=dam_particle_spacing) tmp = lg.get_particles() dam_wall.append_parray(tmp) # generate one half of the base lg.start_point = Point(dam_particle_spacing, 0, 0) lg.end_point = Point(dam_width / 2, 0, 0) tmp = lg.get_particles() dam_wall.append_parray(tmp) # generate particles for the left column of fluid. rg = RectangleGenerator( start_point=Point(origin_x + 2.0 * solid_particle_h, origin_y + 2.0 * solid_particle_h, 0.0), end_point=Point( origin_x + 2.0 * solid_particle_h + fluid_column_width, origin_y + 2.0 * solid_particle_h + fluid_column_height, 0.0), particle_spacing_x1=fluid_particle_spacing, particle_spacing_x2=fluid_particle_spacing, density_computation_mode=Dcm.Set_Constant, mass_computation_mode=Mcm.Compute_From_Density, particle_density=1000., particle_h=fluid_particle_h, kernel=CubicSplineKernel(2), filled=True) dam_fluid = rg.get_particles() # generate the right wall - a line lg = LineGenerator(particle_mass=solid_particle_mass, mass_computation_mode=Mcm.Set_Constant, density_computation_mode=Dcm.Ignore, particle_h=solid_particle_h, start_point=Point(dam_width, 0, 0), end_point=Point(dam_width, dam_height, 0), particle_spacing=dam_particle_spacing) tmp = lg.get_particles() dam_wall.append_parray(tmp) # generate the right half of the base lg.start_point = Point(dam_width / 2. + dam_particle_spacing, 0, 0) lg.end_point = Point(dam_width, 0, 0) tmp = lg.get_particles() dam_wall.append_parray(tmp) for parray in [dam_fluid, dam_wall]: if rank != 0: # add some necessary properties to the particle array. parray.add_property({'name': 'x'}) parray.add_property({'name': 'y'}) parray.add_property({'name': 'z'}) parray.add_property({ 'name': 'h', 'default': options.particle_radius }) parray.add_property({'name': 'rho', 'default': 1000.}) parray.add_property({'name': 'pid'}) parray.add_property({'name': '_tmp', 'default': 0.0}) parray.add_property({'name': 'm'}) else: parray.add_property({'name': '_tmp'}) parray.add_property({'name': 'pid', 'default': 0.0}) return [dam_fluid, dam_wall]
def _get_end_points(self): """ Return changed end points so that start_point to end_point is moving in positive direction for all coords. """ length = self.end_point.x - self.start_point.x depth = self.end_point.y - self.start_point.y width = self.end_point.z - self.start_point.z start_point = Point() end_point = Point() if length < 0: start_point.x = self.end_point.x end_point.x = self.start_point.x else: start_point.x = self.start_point.x end_point.x = self.end_point.x if depth < 0: start_point.y = self.end_point.y end_point.y = self.start_point.y else: start_point.y = self.start_point.y end_point.y = self.end_point.y if width < 0: start_point.z = self.end_point.z end_point.z = self.start_point.z else: start_point.z = self.start_point.z end_point.z = self.end_point.z return start_point, end_point, abs(length), abs(depth), abs(width)
def test_get_nearest_particles_to_point(self): """Tests the get_nearest_particles_to_point function. For a graphical view of the test dataset, refer image test_cell_data1.png. """ parrs = generate_sample_dataset_1() cm = CellManager(arrays_to_bin=parrs, min_cell_size=1.0, max_cell_size=2.0) nbrl1 = NbrParticleLocatorBase(parrs[0], cm) # querying neighbors of dark point 4.(refer image) pnt = Point(0.4, 0.0, 0.4) output_array = LongArray() nbrl1.py_get_nearest_particles_to_point(pnt, 0.5, output_array) self.assertEqual(output_array.length, 1) self.assertEqual(output_array[0], 0) output_array.reset() nbrl1.py_get_nearest_particles_to_point(pnt, 1.0, output_array) self.assertEqual(output_array.length, 4) a = list(output_array.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # querying neighbors of dark point 3, with radius 4.0 pnt = Point(1.5, 0.0, -0.5) output_array.reset() nbrl1.py_get_nearest_particles_to_point(pnt, 4.0, output_array) self.assertEqual(output_array.length, 8) a = list(output_array.get_npy_array()) for i in range(8): self.assertEqual(a.count(i), 1) # now querying for neighbors from dark particles. nbrl2 = NbrParticleLocatorBase(parrs[1], cm) # searching from the center (1., 1., 1.) with different radii. pnt.set(1, 1, 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 0.1, output_array) self.assertEqual(output_array.length, 0) nbrl2.py_get_nearest_particles_to_point(pnt, 1.0, output_array) self.assertEqual(output_array.length, 0) nbrl2.py_get_nearest_particles_to_point(pnt, 1.136, output_array) self.assertEqual(output_array.length, 1) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.358, output_array) self.assertEqual(output_array.length, 2) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) self.assertEqual(a.count(3), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.4142135623730951, output_array) self.assertEqual(output_array.length, 3) a = list(output_array.get_npy_array()) self.assertEqual(a.count(1), 1) self.assertEqual(a.count(3), 1) self.assertEqual(a.count(0), 1) output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.88, output_array) self.assertEqual(output_array.length, 4) a = list(output_array.get_npy_array()) for i in range(4): self.assertEqual(a.count(i), 1) # test to make sure the exclude_index parameter is considered. output_array.reset() nbrl2.py_get_nearest_particles_to_point(pnt, 1.88, output_array, 3) self.assertEqual(output_array.length, 3) a = list(output_array.get_npy_array()) for i in range(3): self.assertEqual(a.count(i), 1)
def _test_summation_density(self): "NNPS :: testing for summation density" fluid, channel = self.particles nnps = self.nnps kernel = self.kernel # get the fluid arrays fx, fy, fh, frho, fV, fm = fluid.get('x', 'y', 'h', 'rho', 'V', 'm', only_real_particles=True) # initialize the fluid density and volume frho[:] = 0.0 fV[:] = 0.0 # compute density on the fluid nbrs = UIntArray() for i in range(fluid.num_real_particles): hi = fh[i] # compute density from the fluid from the source arrays nnps.get_nearest_particles(src_index=0, dst_index=0, d_idx=i, nbrs=nbrs) nnbrs = nbrs.length # the source arrays. First source is also the fluid sx, sy, sh, sm = fluid.get('x', 'y', 'h', 'm', only_real_particles=False) for indexj in range(nnbrs): j = nbrs[indexj] xj = Point(sx[j], sy[j]) hij = 0.5 * (hi + sh[j]) frho[i] += sm[j] * kernel.kernel(fx[i], fy[i], 0.0, sx[j], sy[j], 0.0, hij) fV[i] += kernel.kernel(fx[i], fy[i], 0.0, sx[j], sy[j], 0.0, hij) # compute density from the channel nnps.get_nearest_particles(src_index=1, dst_index=0, d_idx=i, nbrs=nbrs) nnbrs = nbrs.length sx, sy, sh, sm = channel.get('x', 'y', 'h', 'm', only_real_particles=False) for indexj in range(nnbrs): j = nbrs[indexj] hij = 0.5 * (hi + sh[j]) frho[i] += sm[j] * kernel.kernel(fx[i], fy[i], 0.0, sx[j], sy[j], 0.0, hij) fV[i] += kernel.kernel(fx[i], fy[i], 0.0, sx[j], sy[j], 0.0, hij) # check the number density and density by summation voli = 1. / fV[i] self.assertAlmostEqual(voli, self.vol, 6) self.assertAlmostEqual(frho[i], fm[i] / voli, 6)