def eval(self, solver): if not ((solver.count % self.count) == 0): return particles = solver.particles time = solver.t nnps = particles.nnps_manager locator_cache = nnps.particle_locator_cache num_locs = len(locator_cache) locators = locator_cache.values() fname_base = os.path.join(self.path+"/neighbors_"+str(self.rank)) cell_manager = particles.cell_manager cell_size = cell_manager.cell_size neighbor_idx = {} for i in range(num_locs): loc = locators[i] dest = loc.dest src = loc.source particle_indices = dest.get('idx') x, y, z = dest.get("x", "y", "z") neighbor_idx[dest.name + '-' + src.name] = {} d = neighbor_idx[dest.name + '-' + src.name] nrp = dest.num_real_particles for j in range(nrp): neighbors = loc.py_get_nearest_particles(j) temp = dest.extract_particles(neighbors) particle_idx = particle_indices[j] pnt = base.Point(x[j], y[j], z[j]) cid = py_find_cell_id(pnt, cell_size) idx = temp.get_carray("idx") d[particle_idx] = {'neighbors':idx, 'cid':cid} fname = fname_base + "_" + dest.name + "_" + str(solver.count) # save particle neighbor information. f = open(fname, 'w') pickle.dump(neighbor_idx, f) f.close() fname_cells = os.path.join(self.path+"/cells_"+str(self.rank)) fname_cells += "_" + str(solver.count) # ask the cell manager to save the particle representation cell_manager.get_particle_representation(fname_cells)
def eval(self, solver, count): if not ((count % self.count) == 0): return particles = solver.particles time = solver.t nnps = particles.nnps_manager locator_cache = nnps.particle_locator_cache num_locs = len(locator_cache) locators = locator_cache.values() fname_base = os.path.join(self.path + "/neighbors_" + str(self.rank)) cell_manager = particles.cell_manager cell_size = cell_manager.cell_size for i in range(num_locs): loc = locators[i] dest = loc.dest particle_indices = dest.get('idx') x, y, z = dest.get("x", "y", "z") neighbor_idx = {} nrp = dest.num_real_particles for j in range(nrp): neighbors = loc.py_get_nearest_particles(j) temp = dest.extract_particles(neighbors) particle_idx = particle_indices[j] pnt = base.Point(x[j], y[j], z[j]) cid = py_find_cell_id(pnt, cell_size) idx = temp.get_carray("idx") neighbor_idx[particle_idx] = {'neighbors': idx, 'cid': cid} fname = fname_base + "_" + dest.name + "_" + str(time) f = open(fname, 'w') pickle.dump(neighbor_idx, f) f.close() fname_cells = os.path.join(self.path + "/cells_" + str(self.rank)) fname_cells += "_" + str(time) cell_manager.get_particle_representation(fname_cells)
def test(self): """Dynamic OpenCL binning test. """ # Create a ParticleArray with double precision pa = solver.shock_tube_solver.standard_shock_tube_data( name="test", cl_precision="double", type=base.Fluid) # get the coordinate array x = pa.get('x') # set the scale factor and cell size. Remember that in the # OpenCL DomainManager, we want the bin size to be (k+1)*maxH scale_fac = 2.0 h0 = pa.h[0] cell_size = (scale_fac + 1) * h0 # create a shock tube solver s = solver.ShockTubeSolver(dim=1, integrator_type=solver.EulerIntegrator) # create a Particles instance with a fixed cell size provided. particles = base.Particles(arrays=[pa,], min_cell_size=cell_size, max_cell_size=cell_size) s.setup(particles) s.set_final_time(0.15) s.set_time_step(3e-4) integrator = s.integrator cell_manager = particles.cell_manager # Setup the OpenCL domain manager ctx = solver.create_some_context() domain_manager = base.LinkedListManager(arrays=[pa,], context=ctx) assert ( domain_manager.with_cl == True ) # bin the particles on the OpenCL device domain_manager.update() # the cell sizes for Cython and OpenCL should be the same assert (domain_manager.cell_size == cell_manager.cell_size) cell_size = domain_manager.cell_size t = 0.0 tf = 0.15 dt = 3e-4 np = 400 while t < tf: # update the particles particles.update() # integrate integrator.integrate(dt) # call the cell manager's update cell_manager.update() # now bin the updated data using OpenCL domain_manager.update() domain_manager.enqueue_copy() head = domain_manager.head["test"] next = domain_manager.Next["test"] cellids = domain_manager.cellids["test"] # test the bin structure for each particle for i in range(np): # find the index of the particle pnt = base.Point(x[i]) index = py_find_cell_id(pnt, cell_size) # get the particles in the cell cell = cell_manager.cells_dict[index] cy_nbrs = cell.index_lists[0].get_npy_array() cy_nbrs.sort() # get the particle's index with OpenCL cl_index = cellids[i] # get the particles in the the cell with OpenCL cl_nbrs = ll_cell_neighbors( cellids[i], head, next ) cl_nbrs.sort() # the lenght of the neighbors should be the same nclnbrs = len(cl_nbrs) ncynbrs = len(cy_nbrs) assert ( nclnbrs == ncynbrs ) # test each neighbor for j in range(ncynbrs): assert ( cl_nbrs[j] == cy_nbrs[j] ) t += dt