def setUp(self): if not solver.HAS_CL: try: import nose.plugins.skip as skip reason = "PyOpenCL not installed!" raise skip.SkipTest(reason) except ImportError: pass self.np = np = 101 self.x = x = numpy.linspace(0, 1, np) self.m = m = numpy.ones_like(x) * (x[1] - x[0]) self.h = h = 2*self.m pa = base.get_particle_array(name="test", cl_precision="single", x=x, m=m, h=h) particles = base.CLParticles([pa,]) kernel = base.CubicSplineKernel(dim=1) func = sph.GravityForce.withargs(gx=-1, gy=-1, gz=-1).get_func(pa,pa) self.calc = sph.CLCalc(particles, sources=[pa,], dest=pa, updates=['u','v','w'], kernel=kernel, funcs=[func,]) if solver.HAS_CL: self.context = solver.create_some_context()
def setUp(self): """ The setup consists of four particles placed at the vertices of a unit square. The force function to be tested is: ..math:: f_i = \sum_{j=1}^{4} \frac{m_j}{|x_j - x_i|^3 + \eps}(x_j - x_i) The mass of each particle is 1 """ self.np = 4 # define the particle properties here x = numpy.array([0, 0, 1, 1], numpy.float64) y = numpy.array([0, 1, 1, 0], numpy.float64) z = numpy.zeros_like(x) m = numpy.ones_like(x) u = numpy.array([1, 0, 0, -1], numpy.float64) p = numpy.array([0, 0, 1, 1], numpy.float64) self.kernel = base.CubicSplineKernel(dim=2) # create a ParticleArray with double precision self.pa = pa = base.get_particle_array(name="test", x=x, y=y, z=z, m=m, u=u, p=p) # create a particles instance self.particles = base.Particles([pa,]) self.cl_particles = base.CLParticles( arrays=[self.pa,], domain_manager_type=CLDomain.DomainManager, cl_locator_type=CLLocator.AllPairNeighborLocator) # define the function here #self.func = func = sph.NBodyForce.get_func(pa, pa) if solver.HAS_CL: self.ctx = ctx = solver.create_some_context() self.q = q = cl.CommandQueue(ctx) self.setup()
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
def setUp(self): """ The setup consists of two fluid particle arrays, each having one particle. The fluids are acted upon by an external vector force and gravity. Comparison is made with the PySPH integration of the system. """ x1 = numpy.array([-0.5,]) y1 = numpy.array([1.0, ]) x2 = numpy.array([0.5,]) y2 = numpy.array([1.0,]) self.f1 = base.get_particle_array(name="fluid1", x=x1, y=y1) self.f2 = base.get_particle_array(name="fluid2", x=x2, y=y2) self.particles = base.CLParticles( arrays=[self.f1,self.f2], domain_manager_type=CLDomain.DomainManager, cl_locator_type=CLLocator.AllPairNeighborLocator ) self.kernel = kernel = base.CubicSplineKernel(dim=2) gravity = solver.SPHIntegration( sph.GravityForce.withargs(gy=-10.0), on_types=[Fluid], updates=['u','v'], id='gravity' ) force = solver.SPHIntegration( sph.GravityForce.withargs(gx = -10.0), on_types=[Fluid], updates=['u','v'], id='force' ) position = solver.SPHIntegration( sph.PositionStepping, on_types=[Fluid], updates=['x','y'], id='step', ) gravity.calc_type = sph.CLCalc force.calc_type = sph.CLCalc position.calc_type = sph.CLCalc gravity_calcs = gravity.get_calcs(self.particles, kernel) force_calcs = force.get_calcs(self.particles, kernel) position_calcs = position.get_calcs(self.particles, kernel) self.calcs = calcs = [] calcs.extend(gravity_calcs) calcs.extend(force_calcs) calcs.extend(position_calcs) self.integrator = CLIntegrator(self.particles, calcs) self.ctx = ctx = solver.create_some_context() self.queue = calcs[0].queue self.dt = 0.1 self.nsteps = 10
np = 2**20 # number of times to bin nbins = 3 # generate the point set x = random.random(np) y = random.random(np) z = random.random(np) vol_per_particle = numpy.power(1.0/np, 1.0/3.0) h = numpy.ones_like(x) * 2 * vol_per_particle precision = "single" ctx = solver.create_some_context() pa = base.get_particle_array(name="test", cl_precision=precision, x=x, y=y, z=z, h=h) t1 = time() for i in range(nbins): particles = base.Particles([pa,]) pa.set_dirty(True) cython_time = time() - t1 t1 = time() cl_particles = base.CLParticles( arrays=[pa,],