def test_reset(self): """ Tests the reset function. """ l = LongArray(5) l.reset() self.assertEqual(l.length, 0) self.assertEqual(l.alloc, 5) self.assertEqual(len(l.get_npy_array()), 0)
def test_resize(self): """ Tests the resize function. """ l = LongArray(0) l.resize(20) self.assertEqual(l.length, 20) self.assertEqual(len(l.get_npy_array()), 20) self.assertEqual(l.alloc >= l.length, True)
def test_set_data(self): """ Tests the set_data function. """ l = LongArray(5) np = numpy.arange(5) l.set_data(np) for i in range(5): self.assertEqual(l[i], np[i]) self.assertRaises(ValueError, l.set_data, numpy.arange(10))
def test_squeeze_for_zero_length_array(self): # Given. l = LongArray() # When l.squeeze() # Then self.assertEqual(l.length, 0) self.assertEqual(len(l.get_npy_array()), 0) self.assertEqual(l.alloc >= l.length, True) del l # This should work and not segfault.
def test_pickling(self): """ Tests the __reduce__ and __setstate__ functions. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) import pickle l1_dump = pickle.dumps(l1) l1_load = pickle.loads(l1_dump) self.assertEqual((l1_load.get_npy_array() == l1.get_npy_array()).all(), True)
def test_append(self): """ Test the append function. """ l = LongArray(0) l.append(1) l.append(2) l.append(3) self.assertEqual(l.length, 3) self.assertEqual(l[0], 1) self.assertEqual(l[1], 2) self.assertEqual(l[2], 3)
def test_get_npy_array(self): """ Tests the get_npy_array array. """ l = LongArray(3) l[0] = 1 l[1] = 2 l[2] = 3 nparray = l.get_npy_array() self.assertEqual(len(nparray), 3) for i in range(3): self.assertEqual(nparray[0], l[0])
def test_constructor(self): """ Test the constructor. """ l = LongArray(10) self.assertEqual(l.length, 10) self.assertEqual(l.alloc, 10) self.assertEqual(len(l.get_npy_array()), 10) l = LongArray() self.assertEqual(l.length, 0) self.assertEqual(l.alloc, 16) self.assertEqual(len(l.get_npy_array()), 0)
def arange_long(start, stop=-1): """ Creates a LongArray working same as builtin range with upto 2 arguments both expected to be positive """ if stop == -1: arange = LongArray(start) for i in range(start): arange.data[i] = i return arange else: size = stop-start arange = LongArray(size) for i in range(size): arange.data[i] = start + i return arange
def test_set_view(self): # Given src = LongArray() src.extend(numpy.arange(5)) # When. view = LongArray() view.set_view(src, 1, 4) # Then. self.assertEqual(view.length, 3) expect = list(range(1, 4)) self.assertListEqual(view.get_npy_array().tolist(), expect)
def test_set_view_for_empty_array(self): # Given src = LongArray() src.extend(numpy.arange(5)) # When. view = LongArray() view.set_view(src, 1, 1) # Then. self.assertEqual(view.length, 0) expect = [] self.assertListEqual(view.get_npy_array().tolist(), expect)
def test_align_array(self): """ Test the align_array function. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) new_indices = LongArray(10) new_indices.set_data(numpy.asarray([1, 5, 3, 2, 4, 7, 8, 6, 9, 0])) l1.align_array(new_indices) self.assertEqual(numpy.allclose([1, 5, 3, 2, 4, 7, 8, 6, 9, 0], l1.get_npy_array()), True)
def test_set_view_stores_reference_to_parent(self): # Given src = LongArray() src.extend(numpy.arange(5)) # When view = LongArray() view.set_view(src, 1, 4) del src # Then. self.assertEqual(view.length, 3) expect = list(range(1, 4)) self.assertListEqual(view.get_npy_array().tolist(), expect)
def test_remove_particles(self): """ Tests the remove_particles function. """ x = [1, 2, 3, 4.] y = [0., 1., 2., 3.] z = [0., 0., 0., 0.] m = [1., 1., 1., 1.] h = [.1, .1, .1, .1] p = particle_array.ParticleArray(x={'data':x}, y={'data':y}, z={'data':z}, m={'data':m}, h={'data':h}) remove_arr = LongArray(0) remove_arr.append(0) remove_arr.append(1) p.remove_particles(remove_arr) self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True) # now try invalid operations to make sure errors are raised. remove_arr.resize(10) self.assertRaises(ValueError, p.remove_particles, remove_arr) # now try to remove a particle with index more that particle # length. remove_arr = [2] p.remove_particles(remove_arr) # make sure no change occurred. self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True)
def test_reserve(self): """ Tests the reserve function. """ l = LongArray(0) l.reserve(10) self.assertEqual(l.alloc, 16) self.assertEqual(l.length, 0) self.assertEqual(len(l.get_npy_array()), 0) l.reserve(20) self.assertEqual(l.alloc, 20) self.assertEqual(l.length, 0) self.assertEqual(len(l.get_npy_array()), 0)
def test_get_set_indexing(self): """ Test get/set and [] operator. """ l = LongArray(10) l.set(0, 10) l.set(9, 1) self.assertEqual(l.get(0), 10) self.assertEqual(l.get(9), 1) l[9] = 2 self.assertEqual(l[9], 2)
def test_remove_particles(self): if self.backend == 'cuda': pytest.xfail("Scan not supported on CUDA") x = [1, 2, 3, 4.] y = [0., 1., 2., 3.] z = [0., 0., 0., 0.] m = [1., 1., 1., 1.] h = [.1, .1, .1, .1] A = numpy.arange(12) p = particle_array.ParticleArray(x={'data': x}, y={'data': y}, z={'data': z}, m={'data': m}, h={'data': h}, A={ 'data': A, 'stride': 3 }, backend=self.backend) remove_arr = LongArray(0) remove_arr.append(0) remove_arr.append(1) p.remove_particles(remove_arr) self.pull(p) self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True) self.assertEqual(check_array(p.A, numpy.arange(6, 12)), True) # now try invalid operations to make sure errors are raised. remove_arr.resize(10) self.assertRaises(ValueError, p.remove_particles, remove_arr) # now try to remove a particle with index more that particle # length. remove_arr = [2] p.remove_particles(remove_arr) self.pull(p) # make sure no change occurred. self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True) self.assertEqual(check_array(p.A, numpy.arange(6, 12)), True)
def test_squeeze(self): """ Tests the squeeze function. """ l = LongArray(5) l.append(4) self.assertEqual(l.alloc > l.length, True) l.squeeze() self.assertEqual(l.length, 6) self.assertEqual(l.alloc >= l.length, True) self.assertEqual(len(l.get_npy_array()), 6)
def test_extend(self): """ Tests the extend function. """ l1 = LongArray(5) for i in range(5): l1[i] = i l2 = LongArray(5) for i in range(5): l2[i] = 5 + i l1.extend(l2.get_npy_array()) self.assertEqual(l1.length, 10) self.assertEqual(numpy.allclose(l1.get_npy_array(), numpy.arange(10)), True)
def test_update_min_max(self): """ Tests the update_min_max function. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) l1.update_min_max() self.assertEqual(l1.minimum, 0) self.assertEqual(l1.maximum, 9) l1[9] = -1 l1[0] = -20 l1[4] = 200 l1.update_min_max() self.assertEqual(l1.minimum, -20) self.assertEqual(l1.maximum, 200)
def test_squeeze(self): """ Tests the squeeze function. """ l = LongArray(5) l.append(4) self.assertEqual(l.alloc > l.length, True) l.squeeze() self.assertEqual(l.length, 6) self.assertEqual(l.alloc == l.length, True) self.assertEqual(len(l.get_npy_array()), 6)
def arange_long(start, stop=-1): """ Creates a LongArray working same as builtin range with upto 2 arguments both expected to be positive """ if stop == -1: arange = LongArray(start) for i in range(start): arange.data[i] = i return arange else: size = stop - start arange = LongArray(size) for i in range(size): arange.data[i] = start + i return arange
def test_remove_particles(self): """ Tests the remove_particles function. """ x = [1, 2, 3, 4.] y = [0., 1., 2., 3.] z = [0., 0., 0., 0.] m = [1., 1., 1., 1.] h = [.1, .1, .1, .1] p = particle_array.ParticleArray(x={'data': x}, y={'data': y}, z={'data': z}, m={'data': m}, h={'data': h}) remove_arr = LongArray(0) remove_arr.append(0) remove_arr.append(1) p.remove_particles(remove_arr) self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True) # now try invalid operations to make sure errors are raised. remove_arr.resize(10) self.assertRaises(ValueError, p.remove_particles, remove_arr) # now try to remove a particle with index more that particle # length. remove_arr = [2] p.remove_particles(remove_arr) # make sure no change occurred. self.assertEqual(p.get_number_of_particles(), 2) self.assertEqual(check_array(p.x, [3., 4.]), True) self.assertEqual(check_array(p.y, [2., 3.]), True) self.assertEqual(check_array(p.z, [0., 0.]), True) self.assertEqual(check_array(p.m, [1., 1.]), True) self.assertEqual(check_array(p.h, [.1, .1]), True)
def test_align_array(self): """ Test the align_array function. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) new_indices = LongArray(10) new_indices.set_data(numpy.asarray([1, 5, 3, 2, 4, 7, 8, 6, 9, 0])) l1.align_array(new_indices) self.assertEqual( numpy.allclose([1, 5, 3, 2, 4, 7, 8, 6, 9, 0], l1.get_npy_array()), True)
def create_particles(self, **kwargs): fluid_column_height=self.fluid_column_height fluid_column_width=self.fluid_column_width fluid_column_length=self.fluid_column_length container_height = self.container_height container_length = self.container_length container_width = self.container_width obstacle_height = self.obstacle_height obstacle_length = self.obstacle_length obstacle_width = self.obstacle_width obstacle_center_x = self.obstacle_center_x obstacle_center_y = self.obstacle_center_y nboundary_layers = self.nboundary_layers dx = self.dx # get the domain limits ghostlims = nboundary_layers * dx xmin, xmax = 0.0 -ghostlims, container_length + ghostlims ymin, ymax = 0.0 - ghostlims, container_width + ghostlims zmin, zmax = 0.0 - ghostlims, container_height + ghostlims cw2 = 0.5 * container_width #ymin, ymax = -cw2 - ghostlims, cw2 + ghostlims # create all particles #import matplotlib.pyplot as plt #from mpl_toolkits.mplot3d import Axes3D #import matplotlib.pyplot as plt #ig = plt.figure() #ax = Axes3D(fig) eps = 0.1 * dx xx, yy, zz = numpy.mgrid[xmin:xmax+eps:dx, ymin:ymax+eps:dx, zmin:zmax+eps:dx] x = xx.ravel(); y = yy.ravel(); z = zz.ravel() geom=GetGeo() gem=geom.get_data(xn=len(xx),yn=len(xx[0])) gem=np.transpose(gem) ges=np.ones_like(xx) for itr in range(len(xx[0,0])): ges[:,:,itr]=ges[:,:,itr]*gem ge=ges.ravel() ge=ge-np.min(ge)-0.1 ge=ge/np.max(ge)*0.8 #import matplotlib.pyplot as plt #from mpl_toolkits.mplot3d import Axes3D #import matplotlib.pyplot as plt #ig = plt.figure() #ax = Axes3D(fig) # create a dummy particle array from which we'll sort pa = get_particle_array_wcsph(name='block', x=x, y=y, z=z) #pa. # get the individual arrays indices = [] findices = [] oindices = [] obw2 = 0.5 * obstacle_width obl2 = 0.5 * obstacle_length obh = obstacle_height ocx = obstacle_center_x ocy = obstacle_center_y print(obw2,obl2,obh) #ax.scatter3D(x,y,z,alpha=0.05) gggx=xmax-xmin gggy=ymax-ymin for i in range(x.size): xi = x[i]; yi = y[i]; zi = ge[i]-z[i] # fluid if ( (xmin+gggx*0.4< xi <= xmin+gggx*0.6) and \ (ymin+gggy*0.4< yi < ymin+gggy*0.6) and \ (0 < -zi <= 0.3) ): #ax.scatter3D(xi,yi,zi,alpha=0.5) findices.append(i) # obstacle if ( (ocx-obl2 <= xi <= ocx+obl2) and \ (ocy-obw2 <= yi <= ocy+obw2) and \ (0 < -zi <= obh) ): #ax.scatter3D(xi,yi,zi,alpha=0.5) findices.append(i) oindices.append(i) #plt.show() # extract the individual arrays fa = LongArray(len(findices)); fa.set_data(numpy.array(findices)) fluid = pa.extract_particles(fa) fluid.set_name('fluid') if self.with_obstacle: oa = LongArray(len(oindices)); oa.set_data(numpy.array(oindices)) obstacle = pa.extract_particles(oa) obstacle.set_name('obstacle') """ indices = concatenate( (where( y <= -cw2 )[0], where( y >= cw2 )[0], where( x >= container_length )[0], where( x <= 0 )[0], where( z <= 0 )[0]) ) """ # remove duplicates # indices = array(list(set(indices))) indices=np.where(ge>z)[0] print(len(indices)) wa = LongArray(indices.size); wa.set_data(indices) boundary = pa.extract_particles(wa) boundary.set_name('boundary') # create the particles if self.with_obstacle: particles = [fluid, boundary, obstacle] else: particles = [fluid, boundary] # set up particle properties h0 = self.hdx * dx volume = dx**3 m0 = self.rho0 * volume for pa in particles: pa.m[:] = m0 pa.h[:] = h0 pa.rho[:] = self.rho0 nf = fluid.num_real_particles nb = boundary.num_real_particles if self.with_obstacle: no = obstacle.num_real_particles print("3D dam break with %d fluid, %d boundary, %d obstacle particles"%(nf, nb, no)) else: print("3D dam break with %d fluid, %d boundary particles"%(nf, nb)) # load balancing props for the arrays #fluid.set_lb_props(['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm', 'gid', # 'x0', 'y0', 'z0', 'u0', 'v0', 'w0', 'rho0']) fluid.set_lb_props( list(fluid.properties.keys()) ) #boundary.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) #obstacle.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) boundary.set_lb_props( list(boundary.properties.keys()) ) # boundary and obstacle particles can do with a reduced list of properties # to be saved to disk since they are fixed boundary.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid'] ) if self.with_obstacle: obstacle.set_lb_props( list(obstacle.properties.keys()) ) obstacle.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid'] ) return particles
def test_remove(self): """ Tests the remove function. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) rem = [0, 4, 3] l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 7) self.assertEqual(numpy.allclose([7, 1, 2, 8, 9, 5, 6], l1.get_npy_array()), True) l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 4) self.assertEqual(numpy.allclose([6, 1, 2, 5], l1.get_npy_array()), True) rem = [0, 1, 3] l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 1) self.assertEqual(numpy.allclose([2], l1.get_npy_array()), True) l1.remove(numpy.array([0], dtype=numpy.int)) self.assertEqual(l1.length, 0) self.assertEqual(len(l1.get_npy_array()), 0)
def create_particles(self, **kwargs): fluid_column_height = self.fluid_column_height fluid_column_width = self.fluid_column_width fluid_column_length = self.fluid_column_length container_height = self.container_height container_length = self.container_length container_width = self.container_width obstacle_height = self.obstacle_height obstacle_length = self.obstacle_length obstacle_width = self.obstacle_width obstacle_center_x = self.obstacle_center_x obstacle_center_y = self.obstacle_center_y nboundary_layers = self.nboundary_layers dx = self.dx # get the domain limits ghostlims = nboundary_layers * dx xmin, xmax = 0.0 - ghostlims, container_length + ghostlims zmin, zmax = 0.0 - ghostlims, container_height + ghostlims cw2 = 0.5 * container_width ymin, ymax = -cw2 - ghostlims, cw2 + ghostlims # create all particles eps = 0.1 * dx xx, yy, zz = numpy.mgrid[xmin:xmax + eps:dx, ymin:ymax + eps:dx, zmin:zmax + eps:dx] x = xx.ravel() y = yy.ravel() z = zz.ravel() # create a dummy particle array from which we'll sort pa = get_particle_array_wcsph(name='block', x=x, y=y, z=z) # get the individual arrays indices = [] findices = [] oindices = [] obw2 = 0.5 * obstacle_width obl2 = 0.5 * obstacle_length obh = obstacle_height ocx = obstacle_center_x ocy = obstacle_center_y for i in range(x.size): xi = x[i] yi = y[i] zi = z[i] # fluid if ( (0 < xi <= fluid_column_length) and \ (-cw2 < yi < cw2) and \ (0 < zi <= fluid_column_height) ): findices.append(i) # obstacle if ( (ocx-obl2 <= xi <= ocx+obl2) and \ (ocy-obw2 <= yi <= ocy+obw2) and \ (0 < zi <= obh) ): oindices.append(i) # extract the individual arrays fa = LongArray(len(findices)) fa.set_data(numpy.array(findices)) fluid = pa.extract_particles(fa) fluid.set_name('fluid') if self.with_obstacle: oa = LongArray(len(oindices)) oa.set_data(numpy.array(oindices)) obstacle = pa.extract_particles(oa) obstacle.set_name('obstacle') indices = concatenate((where(y <= -cw2)[0], where(y >= cw2)[0], where(x >= container_length)[0], where(x <= 0)[0], where(z <= 0)[0])) # remove duplicates indices = array(list(set(indices))) wa = LongArray(indices.size) wa.set_data(indices) boundary = pa.extract_particles(wa) boundary.set_name('boundary') # create the particles if self.with_obstacle: particles = [fluid, boundary, obstacle] else: particles = [fluid, boundary] # set up particle properties h0 = self.hdx * dx volume = dx**3 m0 = self.rho0 * volume for pa in particles: pa.m[:] = m0 pa.h[:] = h0 pa.rho[:] = self.rho0 nf = fluid.num_real_particles nb = boundary.num_real_particles if self.with_obstacle: no = obstacle.num_real_particles print( "3D dam break with %d fluid, %d boundary, %d obstacle particles" % (nf, nb, no)) else: print("3D dam break with %d fluid, %d boundary particles" % (nf, nb)) # load balancing props for the arrays #fluid.set_lb_props(['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm', 'gid', # 'x0', 'y0', 'z0', 'u0', 'v0', 'w0', 'rho0']) fluid.set_lb_props(list(fluid.properties.keys())) #boundary.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) #obstacle.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) boundary.set_lb_props(list(boundary.properties.keys())) # boundary and obstacle particles can do with a reduced list of properties # to be saved to disk since they are fixed boundary.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid']) if self.with_obstacle: obstacle.set_lb_props(list(obstacle.properties.keys())) obstacle.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid']) return particles
def create_particles(self, **kwargs): fluid_column_height=self.fluid_column_height fluid_column_width=self.fluid_column_width fluid_column_length=self.fluid_column_length container_height = self.container_height container_length = self.container_length container_width = self.container_width obstacle_height = self.obstacle_height obstacle_length = self.obstacle_length obstacle_width = self.obstacle_width obstacle_center_x = self.obstacle_center_x obstacle_center_y = self.obstacle_center_y nboundary_layers = self.nboundary_layers dx = self.dx # get the domain limits ghostlims = nboundary_layers * dx xmin, xmax = 0.0 -ghostlims, container_length + ghostlims zmin, zmax = 0.0 - ghostlims, container_height + ghostlims cw2 = 0.5 * container_width ymin, ymax = -cw2 - ghostlims, cw2 + ghostlims # create all particles eps = 0.1 * dx xx, yy, zz = numpy.mgrid[xmin:xmax+eps:dx, ymin:ymax+eps:dx, zmin:zmax+eps:dx] x = xx.ravel(); y = yy.ravel(); z = zz.ravel() # create a dummy particle array from which we'll sort pa = get_particle_array_wcsph(name='block', x=x, y=y, z=z) # get the individual arrays indices = [] findices = [] oindices = [] obw2 = 0.5 * obstacle_width obl2 = 0.5 * obstacle_length obh = obstacle_height ocx = obstacle_center_x ocy = obstacle_center_y for i in range(x.size): xi = x[i]; yi = y[i]; zi = z[i] # fluid if ( (0 < xi <= fluid_column_length) and \ (-cw2 < yi < cw2) and \ (0 < zi <= fluid_column_height) ): findices.append(i) # obstacle if ( (ocx-obl2 <= xi <= ocx+obl2) and \ (ocy-obw2 <= yi <= ocy+obw2) and \ (0 < zi <= obh) ): oindices.append(i) # extract the individual arrays fa = LongArray(len(findices)); fa.set_data(numpy.array(findices)) fluid = pa.extract_particles(fa) fluid.set_name('fluid') oa = LongArray(len(oindices)); oa.set_data(numpy.array(oindices)) obstacle = pa.extract_particles(oa) obstacle.set_name('obstacle') indices = concatenate( (where( y <= -cw2 )[0], where( y >= cw2 )[0], where( x >= container_length )[0], where( x <= 0 )[0], where( z <= 0 )[0]) ) # remove duplicates indices = array(list(set(indices))) wa = LongArray(indices.size); wa.set_data(indices) boundary = pa.extract_particles(wa) boundary.set_name('boundary') # create the particles particles = [fluid, boundary, obstacle] # set up particle properties h0 = self.hdx * dx volume = dx**3 m0 = self.rho0 * volume for pa in particles: pa.m[:] = m0 pa.h[:] = h0 pa.rho[:] = self.rho0 nf = fluid.num_real_particles nb = boundary.num_real_particles no = obstacle.num_real_particles print "3D dam break with %d fluid, %d boundary, %d obstacle particles"%(nf, nb, no) # load balancing props for the arrays #fluid.set_lb_props(['x', 'y', 'z', 'u', 'v', 'w', 'rho', 'h', 'm', 'gid', # 'x0', 'y0', 'z0', 'u0', 'v0', 'w0', 'rho0']) fluid.set_lb_props( fluid.properties.keys() ) #boundary.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) #obstacle.set_lb_props(['x', 'y', 'z', 'rho', 'h', 'm', 'gid', 'rho0']) boundary.set_lb_props( boundary.properties.keys() ) obstacle.set_lb_props( obstacle.properties.keys() ) # boundary and obstacle particles can do with a reduced list of properties # to be saved to disk since they are fixed boundary.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid'] ) obstacle.set_output_arrays( ['x', 'y', 'z', 'rho', 'm', 'h', 'p', 'tag', 'pid', 'gid'] ) return particles
def test_copy_subset(self): """ Tests the copy_subset function. """ l1 = LongArray(10) l1.set_data(numpy.arange(10)) l2 = LongArray(4) l2[0] = 4 l2[1] = 3 l2[2] = 2 l2[3] = 1 # a valid copy. l1.copy_subset(l2, 5, 9) self.assertEqual(numpy.allclose([0, 1, 2, 3, 4, 4, 3, 2, 1, 9], l1.get_npy_array()), True) # try to copy different sized arrays without any index specification. l1.set_data(numpy.arange(10)) # copy to the last k values of source array. l1.copy_subset(l2, start_index=6) self.assertEqual(numpy.allclose([0, 1, 2, 3, 4, 5, 4, 3, 2, 1], l1.get_npy_array()), True) l1.set_data(numpy.arange(10)) l1.copy_subset(l2, start_index=7) self.assertEqual(numpy.allclose([0, 1, 2, 3, 4, 5, 6, 4, 3, 2], l1.get_npy_array()), True) # some invalid operations. l1.set_data(numpy.arange(10)) self.assertRaises(ValueError, l1.copy_subset, l2, -1, 1) self.assertRaises(ValueError, l1.copy_subset, l2, 3, 2) self.assertRaises(ValueError, l1.copy_subset, l2, 0, 11) self.assertRaises(ValueError, l1.copy_subset, l2, 10, 20) self.assertRaises(ValueError, l1.copy_subset, l2, -1, -1)
def test_remove_with_strides(self): # Given l1 = LongArray(12) l1.set_data(numpy.arange(12)) # When rem = [3, 1] l1.remove(numpy.array(rem, dtype=numpy.int), stride=3) # Then self.assertEqual(l1.length, 6) self.assertEqual( numpy.allclose([0, 1, 2, 6, 7, 8], l1.get_npy_array()), True) # Given l1 = LongArray(12) l1.set_data(numpy.arange(12)) # When rem = [0, 2] l1.remove(numpy.array(rem, dtype=numpy.int), stride=3) # Then self.assertEqual(l1.length, 6) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] self.assertEqual( numpy.allclose([9, 10, 11, 3, 4, 5], l1.get_npy_array()), True)
def test_remove(self): l1 = LongArray(10) l1.set_data(numpy.arange(10)) rem = [0, 4, 3] l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 7) self.assertEqual( numpy.allclose([7, 1, 2, 8, 9, 5, 6], l1.get_npy_array()), True) l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 4) self.assertEqual(numpy.allclose([6, 1, 2, 5], l1.get_npy_array()), True) rem = [0, 1, 3] l1.remove(numpy.array(rem, dtype=numpy.int)) self.assertEqual(l1.length, 1) self.assertEqual(numpy.allclose([2], l1.get_npy_array()), True) l1.remove(numpy.array([0], dtype=numpy.int)) self.assertEqual(l1.length, 0) self.assertEqual(len(l1.get_npy_array()), 0)
def test_reset_works_after_set_view(self): # Given src = LongArray() src.extend(numpy.arange(5)) view = LongArray() view.set_view(src, 1, 3) # When. view.reset() view.extend(numpy.arange(3) * 10) # Then. self.assertEqual(view.length, 3) expect = (numpy.arange(3) * 10).tolist() self.assertListEqual(view.get_npy_array().tolist(), expect)
def test_align_array(self): l1 = LongArray(10) l1.set_data(numpy.arange(10)) new_indices = LongArray(10) new_indices.set_data(numpy.asarray([1, 5, 3, 2, 4, 7, 8, 6, 9, 0])) l1.align_array(new_indices) self.assertEqual( numpy.allclose([1, 5, 3, 2, 4, 7, 8, 6, 9, 0], l1.get_npy_array()), True) # Test case with strides. l1 = LongArray(6) l1.set_data(numpy.arange(6)) new_indices = LongArray(3) new_indices.set_data(numpy.asarray([2, 1, 0])) l1.align_array(new_indices, 2) self.assertEqual( numpy.allclose([4, 5, 2, 3, 0, 1], l1.get_npy_array()), True)
def test_copy_subset(self): l1 = LongArray(10) l1.set_data(numpy.arange(10)) l2 = LongArray(4) l2[0] = 4 l2[1] = 3 l2[2] = 2 l2[3] = 1 # a valid copy. l1.copy_subset(l2, 5, 9) self.assertEqual( numpy.allclose([0, 1, 2, 3, 4, 4, 3, 2, 1, 9], l1.get_npy_array()), True) # try to copy different sized arrays without any index specification. l1.set_data(numpy.arange(10)) # copy to the last k values of source array. l1.copy_subset(l2, start_index=6) self.assertEqual( numpy.allclose([0, 1, 2, 3, 4, 5, 4, 3, 2, 1], l1.get_npy_array()), True) l1.set_data(numpy.arange(10)) l1.copy_subset(l2, start_index=7) self.assertEqual( numpy.allclose([0, 1, 2, 3, 4, 5, 6, 4, 3, 2], l1.get_npy_array()), True) # some invalid operations. l1.set_data(numpy.arange(10)) self.assertRaises(ValueError, l1.copy_subset, l2, -1, 1) self.assertRaises(ValueError, l1.copy_subset, l2, 3, 2) self.assertRaises(ValueError, l1.copy_subset, l2, 0, 11) self.assertRaises(ValueError, l1.copy_subset, l2, 10, 20) self.assertRaises(ValueError, l1.copy_subset, l2, -1, -1)
def test_copy_subset_works_with_strides(self): # Given l1 = LongArray(8) l1.set_data(numpy.arange(8)) l2 = LongArray(4) l2.set_data(numpy.arange(10, 14)) # When l1.copy_subset(l2, 2, 3, stride=2) # Then numpy.testing.assert_array_equal(l1.get_npy_array(), [0, 1, 2, 3, 10, 11, 6, 7]) # When l1.copy_subset(l2, 2, 4, stride=2) # Then numpy.testing.assert_array_equal(l1.get_npy_array(), [0, 1, 2, 3, 10, 11, 12, 13])
def test_copy_values(self): # Given l1 = LongArray(8) l1.set_data(numpy.arange(8)) l2 = LongArray(8) l2.set_data(numpy.zeros(8, dtype=int)) # When indices = LongArray(3) indices.set_data(numpy.array([2, 4, 6])) l1.copy_values(indices, l2) # Then numpy.testing.assert_array_equal(l2.get_npy_array(), [2, 4, 6] + [0] * 5) # When l2.set_data(numpy.zeros(8, dtype=int)) indices.set_data(numpy.array([1, 2, 3])) l1.copy_values(indices, l2, stride=2) # Then numpy.testing.assert_array_equal(l2.get_npy_array(), [2, 3, 4, 5, 6, 7, 0, 0])