Пример #1
1
def get_dummy_particles():
    x, y = numpy.mgrid[-5 * dx : box_length + 5 * dx + 1e-10 : dx, -5 * dx : box_height + 5 * dx + 1e-10 : dx]

    xd, yd = x.ravel(), y.ravel()

    md = numpy.ones_like(xd) * m
    hd = numpy.ones_like(xd) * h

    rhod = numpy.ones_like(xd) * ro
    cd = numpy.ones_like(xd) * co
    pd = numpy.zeros_like(xd)

    dummy_fluid = base.get_particle_array(name="dummy_fluid", type=Fluid, x=xd, y=yd, h=hd, rho=rhod, c=cd, p=pd)

    # remove indices within the square

    indices = []

    np = dummy_fluid.get_number_of_particles()
    x, y = dummy_fluid.get("x", "y")

    for i in range(np):
        if -dx / 2 <= x[i] <= box_length + dx / 2:
            if -dx / 2 <= y[i] <= box_height + dx / 2:
                indices.append(i)

    to_remove = base.LongArray(len(indices))
    to_remove.set_data(numpy.array(indices))

    dummy_fluid.remove_particles(to_remove)

    return dummy_fluid
Пример #2
0
    def setUp(self):
        """ The setup consists of points randomly distributed in a
        cube [-1,1] X [-1.1] X [1-,1]

        The smoothing length for the points is proportional to the
        number of particles.

        """

        self.cl_precision = cl_precision = "single"
        self.np = np = 1<<14

        self.x = x = random.random(np) * 2.0 - 1.0
        self.y = y = random.random(np) * 2.0 - 1.0
        self.z = z = random.random(np) * 2.0 - 1.0

        vol_per_particle = numpy.power(8.0/np, 1.0/3.0)

        self.h = h = numpy.ones_like(x) * vol_per_particle

        self.cy_pa = base.get_particle_array(name='test', x=x, y=y, z=z, h=h)

        self.cl_pa = base.get_particle_array(
            name="test", cl_precision=self.cl_precision,
            x=x, y=y, z=z, h=h)

        # the scale factor for the cell sizes
        self.kernel_scale_factor = kernel_scale_factor = 2.0

        self._setup()
Пример #3
0
 def t(self):
     ret = {}
     da = DoubleArray()
     pa = ParticleArray()
     kernel = kernels.CubicSplineKernel(3)
     get_time = time.time
     for N in Ns:
         x = numpy.arange(N)
         z = y = numpy.zeros(N)
         mu = m = rho = numpy.ones(N)
         h = 2*m
         da = DoubleArray(N)
         da2 = DoubleArray(N)
         da.set_data(z)
         da2.set_data(z)
         pa = get_particle_array(x=x, y=y, z=z, h=h, mu=mu, rho=rho, m=m, tmp=z,
                                 tx=z, ty=m, tz=z, nx=m, ny=z, nz=z, u=z, v=z, w=z,
                                 ubar=z, vbar=z, wbar=z, q=m)
         pb = get_particle_array(x=x+0.1**0.5, y=y, z=z, h=h, mu=mu, rho=rho, m=m, tmp=z,
                                 tx=m, ty=z, tz=z, nx=z, ny=m, nz=z, u=z, v=z, w=z,
                                 ubar=z, vbar=z, wbar=z, q=m)
         particles = Particles(arrays=[pa, pb])
         
         func = func_getter.get_func(pa, pb)
         calc = SPHCalc(particles, [pa], pb, kernel, [func], ['tmp']*func.num_outputs)
         print cls.__name__
         t = get_time()
         calc.sph('tmp', 'tmp', 'tmp')
         t = get_time() - t
         
         nam = '%s'%(cls.__name__)
         ret[nam +' /%d'%(N)] = t/N
     return ret
Пример #4
0
    def setUp(self):
        """ Setup for SPHOperationTestCase

        Setup:
        ------
        Create two particle arrays, one Fluid and one Solid.
        Instantiate the class with a default function `SPHRho` with 
        various combinations of the from and on types and check for the 
        filtering of the arrays.

        """

        x = numpy.linspace(0,1,11)
        h = numpy.ones_like(x) * 2 * (x[1] - x[0])

        #create the fluid particle array

        self.fluid = base.get_particle_array(name='fluid', type=Fluid, x=x,h=h)

        #create the solid particle array
        
        self.solid = base.get_particle_array(name="solid", type=Solid, x=x,h=h)

        #create the particles
        
        self.particles = particles = base.Particles(arrays=[self.fluid,
                                                            self.solid])

        #set the kernel
        
        self.kernel = base.CubicSplineKernel()
Пример #5
0
def get_boundary_particles(**kwargs):

    # left boundary
    x = numpy.ones(50)
    for i in range(50):
        x[i] = -0.6 - (i + 1) * dxl

    m = numpy.ones_like(x) * dxl
    h = numpy.ones_like(x) * 2 * dxr
    rho = numpy.ones_like(x)
    u = numpy.zeros_like(x)
    e = numpy.ones_like(x) * 2.5

    p = (0.4) * rho * e
    cs = numpy.sqrt(1.4 * p / rho)

    left = base.get_particle_array(name="left", type=Boundary, x=x, m=m, h=h, rho=rho, u=u, e=e, cs=cs, p=p)

    # right boundary
    for i in range(50):
        x[i] = 0.6 + (i + 1) * dxr

    m = numpy.ones_like(x) * dxl
    h = numpy.ones_like(x) * 2 * dxr
    rho = numpy.ones_like(x) * 0.25
    u = numpy.zeros_like(x)
    e = numpy.ones_like(x) * 1.795

    p = (0.4) * rho * e
    # cs = numpy.sqrt(0.4*e)
    cs = numpy.sqrt(1.4 * p / rho)

    right = base.get_particle_array(name="right", type=Boundary, x=x, m=m, h=h, rho=rho, u=u, e=e, cs=cs, p=p)

    return [left, right]
Пример #6
0
    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])

        tmpx1 = numpy.ones_like(x1)
        tmpx2 = numpy.ones_like(x2)

        self.f1 = base.get_particle_array(name="fluid1", x=x1, y=y1, tmpx=tmpx1)
        self.f2 = base.get_particle_array(name="fluid2", x=x2, y=y2, tmpx=tmpx2)

        self.particles = base.Particles(arrays=[self.f1, self.f2])
        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 = cl.create_some_context()
        self.integrator.setup_integrator(ctx)
        self.queue = calcs[0].queue

        self.dt = 0.1
        self.nsteps = 10
Пример #7
0
def get_boundary_particles():
    """ Get the particles corresponding to the dam and fluids """

    # get the tank
    xt1, yt1 = geom.create_2D_tank(x1=0, y1=0,
                                   x2=tank_length, y2=tank_height,
                                   dx=dx)

    xt2,  yt2 = geom.create_2D_tank(x1=-dx/2, y1=-dx/2,
                                    x2=tank_length + dx/2, y2=tank_height+dx/2,
                                    dx=dx)

    x = numpy.concatenate( (xt1, xt2) )
    y = numpy.concatenate( (yt1, yt2) )

    h = numpy.ones_like(x) * h0
    m = numpy.ones_like(x) * ro*dx*dx*0.5
    rho = numpy.ones_like(x) * ro
    cs = numpy.ones_like(x) * co

    tank = base.get_particle_array(cl_precision="single", name="tank",
                                   type=Solid, x=x,y=y,m=m,rho=rho,h=h,cs=cs)
    np = tank.get_number_of_particles()

    # create the gate
    y1 = numpy.arange(dx/2, tank_height+1e-4, dx/2)
    x1 = numpy.ones_like(y1)*(0.38-dx/2)

    y2 = numpy.arange(dx/2+dx/4, tank_height+1e-4, dx/2)
    x2 = numpy.ones_like(y2)*(0.38-dx)

    y3 = numpy.arange(dx/2, tank_height+1e-4, dx/2)
    x3 = numpy.ones_like(y3)*(0.38-1.5*dx)

    x = numpy.concatenate( (x1, x2, x3) )
    y = numpy.concatenate( (y1, y2, y3) )

    h = numpy.ones_like(x) * h0
    m = numpy.ones_like(x) * 0.5 * dx/2 * dx/2 * ro
    rho = numpy.ones_like(x) * ro
    cs = numpy.ones_like(x) * co
    v = numpy.ones_like(x) * 1.5

    gate = base.get_particle_array(cl_precision="single", name="gate",
                                   x=x, y=y, m=m, rho=rho, h=h, cs=cs,
                                   v=v,
                                   type=Solid)

    np += gate.get_number_of_particles()
    print "Number of solid particles = %d"%(np)

    return [tank, gate]
Пример #8
0
    def setUp(self):

        self.np = 25

        self.x1 = x1 = numpy.array([-0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,]
                                   )
        
        self.y1 = y1 = numpy.array([-0.125, -0.125, -0.125, -0.125, -0.125,
                                    0.125, 0.125, 0.125, 0.125, 0.125,
                                    0.375, 0.375, 0.375, 0.375, 0.375,
                                    0.625, 0.625, 0.625, 0.625, 0.625,
                                    0.875, 0.875, 0.875, 0.875, 0.875]
                                   )

        self.z1 = z1 = numpy.zeros_like(x1)

        self.h1 = h1 = numpy.ones_like(x1) * 0.1

        self.x2 = x2 = numpy.array([-0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,
                                    -0.125, 0.125, 0.375, 0.625, 0.875,]
                                   )
        
        self.y2 = y2 = numpy.array([-0.125, -0.125, -0.125, -0.125, -0.125,
                                    0.125, 0.125, 0.125, 0.125, 0.125,
                                    0.375, 0.375, 0.375, 0.375, 0.375,
                                    0.625, 0.625, 0.625, 0.625, 0.625,
                                    0.875, 0.875, 0.875, 0.875, 0.875]
                                   )
       
        self.z2 = z2 = numpy.zeros_like(x2)
        
        self.h2 = h2 = numpy.ones_like(x2) * 0.15

        self.pa1 = pa1 = base.get_particle_array(name='test1',
                                                 x=x1, y=y1, z=z1, h=h1)

        self.pa2 = pa2 = base.get_particle_array(name='test2',
                                                 x=x2, y=y2, z=z2, h=h2)

        self.periodic_domain = periodic_domain = PeriodicDomain(xmin=-0.2,
                                                                xmax=1.0)
Пример #9
0
    def setUp(self):

        self.np = np = 100
        
        x = numpy.random.random(np)
        y = numpy.random.random(np)
        z = numpy.random.random(np)
        m = numpy.ones_like(x)

        cy_pa = base.get_particle_array(name="test", x=x, y=y, z=z, m=m)
        cl_pa = base.get_particle_array(name="test", cl_precision="double",
                                        x=x, y=y, z=z, m=m)

        cy_particles = base.Particles(
            [cy_pa,], locator_type=CYLoctor.NSquareNeighborLocator)

        cl_particles = base.CLParticles( [cl_pa,] )

        cy_solver = solver.Solver(
            dim=3, integrator_type=solver.EulerIntegrator)

        cl_solver = solver.Solver(
            dim=3, integrator_type=solver.EulerIntegrator)

        self.cy_solver = cy_solver
        self.cl_solver = cl_solver

        cy_solver.add_operation(solver.SPHIntegration(

            sph.NBodyForce.withargs(), on_types=[0], from_types=[0],
            updates=['u','v','w'], id='nbody_force')

                                )

        cy_solver.add_operation_step([0,])

        cl_solver.add_operation(solver.SPHIntegration(

            sph.NBodyForce.withargs(), on_types=[0], from_types=[0],
            updates=['u','v','w'], id='nbody_force')

                                )

        cl_solver.add_operation_step([0,])
        cl_solver.set_cl(True)
        
        cy_solver.setup(cy_particles)
        cl_solver.setup(cl_particles)
Пример #10
0
def standard_shock_tube_data(name="", type=0, cl_precision="double",
                             nl=320, nr=80, smoothing_length=None, **kwargs):
    """ Standard 400 particles shock tube problem """
    
    dxl = 0.6/nl
    dxr = dxl*4
    
    x = numpy.ones(nl+nr)
    x[:nl] = numpy.arange(-0.6, -dxl+1e-10, dxl)
    x[nl:] = numpy.arange(dxr, 0.6+1e-10, dxr)

    m = numpy.ones_like(x)*dxl
    h = numpy.ones_like(x)*2*dxr

    if smoothing_length:
        h = numpy.ones_like(x) * smoothing_length
    
    rho = numpy.ones_like(x)
    rho[nl:] = 0.25
    
    u = numpy.zeros_like(x)
    
    e = numpy.ones_like(x)
    e[:nl] = 2.5
    e[nl:] = 1.795

    p = 0.4*rho*e

    cs = numpy.sqrt(1.4*p/rho)

    idx = numpy.arange(nl+nr)
    
    return base.get_particle_array(name=name,x=x,m=m,h=h,rho=rho,p=p,e=e,
                                   cs=cs,type=type, idx=idx,
                                   cl_precision=cl_precision)
Пример #11
0
def get_fluid():
    """ Get the fluid particle array """

    x, y = numpy.mgrid[dx : box_length - 1e-10 : dx, dx : box_height - 1e-10 : dx]

    xf, yf = x.ravel(), y.ravel()

    mf = numpy.ones_like(xf) * m
    hf = numpy.ones_like(xf) * h

    rhof = numpy.ones_like(xf) * ro
    cf = numpy.ones_like(xf) * co
    pf = numpy.zeros_like(xf)

    fluid = base.get_particle_array(name="fluid", type=Fluid, x=xf, y=yf, h=hf, rho=rhof, c=cf, p=pf)

    # remove indices within the square

    indices = []

    np = fluid.get_number_of_particles()
    x, y = fluid.get("x", "y")

    for i in range(np):
        if 1.0 - dx / 2 <= x[i] <= 2.0 + dx / 2:
            if 2.0 - dx / 2 <= y[i] <= 3.0 + dx / 2:
                indices.append(i)

    to_remove = base.LongArray(len(indices))
    to_remove.set_data(numpy.array(indices))

    fluid.remove_particles(to_remove)

    return fluid
Пример #12
0
def test_sph_calc():

    x = numpy.array([0,])
    y = numpy.array([0,])
    z = numpy.array([0,])
    h = numpy.ones_like(x)

    pa = base.get_particle_array(name="test", x=x, y=y, z=z,h=h)
    particles = base.Particles(arrays=[pa,])
    kernel = base.CubicSplineKernel(dim=1)

    vector_force1 = sph.VectorForce.withargs(force=base.Point(1,1,1))
    vector_force2 = sph.VectorForce.withargs(force=base.Point(1,1,1))

    func1 = vector_force1.get_func(pa,pa)
    func2 = vector_force2.get_func(pa,pa)

    calc = sph.SPHCalc(particles=particles, sources=[pa,pa], dest=pa,
                       kernel=kernel, funcs=[func1, func2],
                       updates=['u','v','w'], integrates=True)

    # evaluate the calc. Accelerations are stored in _tmpx, _tmpy and _tmpz

    calc.sph('_tmpx', '_tmpy', '_tmpz')

    tmpx, tmpy, tmpz = pa.get('_tmpx', '_tmpy', '_tmpz')

    # the acceleration should be 2 in each direction

    assert ( abs(tmpx[0] - 2.0) < 1e-16 )
    assert ( abs(tmpy[0] - 2.0) < 1e-16 )
    assert ( abs(tmpz[0] - 2.0) < 1e-16 )
Пример #13
0
    def setUp(self):
        """ A Dummy fluid solver is created with the following operations

        (i)  -- Equation of State
        (ii) -- Density Rate
        (iii)-- Momentum Equation Pressure Gradient
        (iv) -- Momentum Equation Viscosity 
        
        """
        self.kernel = kernel = base.CubicSplineKernel(dim=2)

        self.solver = s = solver.Solver(dim=2,
                                        integrator_type=solver.EulerIntegrator)

        s.default_kernel = kernel

        self.particles = base.Particles(arrays=[base.get_particle_array()])

        # Create some replacement operations

        self.visc = solver.SPHIntegration(sph.MorrisViscosity,
                                          on_types=[Fluids],
                                          from_types=[Fluids, Solids],
                                          updates=['u', 'v'],
                                          id='visc')

        self.summation_density = solver.SPHOperation(sph.SPHRho,
                                                     on_types=[Fluids, Solids],
                                                     updates=['rho'],
                                                     id='sd')
Пример #14
0
def get_fluid_particles():
    
    xf1, yf1 = geom.create_2D_filled_region(x1=dx, y1=dx,
                                            x2=fluid_column_width,
                                            y2=fluid_column_height,
                                            dx=dx)

    xf2, yf2 = geom.create_2D_filled_region(x1=dx/2, y1=dx/2,
                                            x2=fluid_column_width,
                                            y2=fluid_column_height,
                                            dx=dx)
    

    x = numpy.concatenate((xf1, xf2))
    y = numpy.concatenate((yf1, yf2))

    print 'Number of fluid particles: ', len(x)

    hf = numpy.ones_like(x) * h
    mf = numpy.ones_like(x) * dx * dy * ro * 0.5
    rhof = numpy.ones_like(x) * ro
    csf = numpy.ones_like(x) * co
    
    fluid = base.get_particle_array(cl_precision="single",
                                    name="fluid", type=Fluid,
                                    x=x, y=y, h=hf, m=mf, rho=rhof,
                                    cs=csf)

    return fluid
Пример #15
0
    def setUp(self):
        """ 
        11 particles from 0~1 with dx = 0.1, h = 0.1
        Particle 5 has h = 0.2
                
        x  x  x  x  x  O  x  x  x  x  x    
                       
        With a kernel scale factor of 2, particle 5 should have
        neighbor indices [1,2,3,4,5,6,7,8,9]        

        """

        x = numpy.linspace(0, 1, 11)
        dx = x[1] - x[0]

        self._h = dx + 1e-10

        h = numpy.ones_like(x) * self._h
        h[5] = 2 * self._h

        m = numpy.ones_like(x) * dx
        rho = numpy.ones_like(x)

        pa = base.get_particle_array(type=0,
                                     name="test",
                                     x=x,
                                     h=h,
                                     m=m,
                                     rho=rho)

        self.particles = base.Particles(arrays=[pa],
                                        variable_h=True,
                                        in_parallel=False)
Пример #16
0
    def setUp(self):
        """ 
        11 particles from 0~1 with dx = 0.1, h = 0.1
        Particle 5 has h = 0.2
                
        x  x  x  x  x  O  x  x  x  x  x    
                       
        With a kernel scale factor of 2, particle 5 should have
        neighbor indices [1,2,3,4,5,6,7,8,9]        

        """        

        x = numpy.linspace(0,1,11)
        dx = x[1] - x[0]
        
        self._h = dx + 1e-10

        h = numpy.ones_like(x) * self._h
        h[5] = 2 * self._h
        
        m = numpy.ones_like(x) * dx
        rho = numpy.ones_like(x)

        pa = base.get_particle_array(type=0, name="test",
                                     x=x, h=h, m=m,rho=rho)

        self.particles = base.Particles(arrays=[pa], variable_h=True,
                                        in_parallel=False)
Пример #17
0
def standard_shock_tube_data(name="", type=0):
    """ Standard 400 particles shock tube problem """
    
    dxl = 0.001875
    dxr = dxl*4
    
    x = numpy.ones(400, float)
    x[:320] = numpy.arange(-0.6, -dxl+1e-4, dxl)
    x[320:] = numpy.arange(dxr, 0.6+1e-4, dxr)

    m = numpy.ones_like(x)*dxl
    h = numpy.ones_like(x)*2*dxr

    rho = numpy.ones_like(x)
    rho[320:] = 0.25
    
    u = numpy.zeros_like(x)
    
    e = numpy.ones_like(x)
    e[:320] = 2.5
    e[320:] = 1.795

    p = 0.4*rho*e

    cs = numpy.sqrt(1.4*p/rho)

    idx = numpy.arange(400)
    
    return base.get_particle_array(name=name,x=x,m=m,h=h,rho=rho,p=p,e=e,
                                   cs=cs,type=type, idx=idx)
Пример #18
0
    def setUp(self):
        """ The setup consists of a 2D square ([0,1] X [0,1]) with 25
        particles.


        """

        xc = numpy.arange(0,1.0, 0.2)
        x, y = numpy.meshgrid(xc,xc)

        self.x = x = x.ravel()
        self.y = y = y.ravel()
        self.h = h = numpy.ones_like(x) * 0.25

        dx = dy = 0.2
        self.dx = dx

        self.block_size = 0.5
        self.cell_size = 0.5

        self.pa = pa = base.get_particle_array(name="test", x=x, y=y, h=h)

        self.cm = cm = parallel.ParallelCellManager(arrays_to_bin=[pa,],
                                                    max_radius_scale=2.0,
                                                    dimension=2.0,
                                                    load_balancing=False,
                                                    initialize=False)

        self.block_000_indices = 0,1,2,5,6,7,10,11,12
        self.block_100_indices = 3,4,8,9,13,14
        self.block_010_indices = 15,16,17,20,21,22
        self.block_110_indices = 18,19,23,24
Пример #19
0
def create_particles_3d(**kwargs):

    x, y, z = numpy.mgrid[0.25:0.75+1e-10:dx,
                          0.25:0.75+1e-10:dx,
                          0.25:0.75+1e-10:dx]


    x = x.ravel()
    y = y.ravel()
    z = z.ravel()

    np = len(x)

    u = random.random(np) * 0
    v = random.random(np) * 0
    w = random.random(np) * 0

    m = numpy.ones_like(x) * dx**3

    vol_per_particle = numpy.power(0.5**3/np ,1.0/3.0)
    radius = 2 * vol_per_particle

    print "Using smoothing length: ", radius

    h = numpy.ones_like(x) * radius

    fluid = base.get_particle_array(name="fluid", type=base.Fluid,
                                    x=x, y=y, z=z,
                                    u=u, v=v, w=w,
                                    m=m,h=h)

    print "Number of particles: ", fluid.get_number_of_particles()

    return [fluid,]
Пример #20
0
def create_particles_2d(**kwargs):

    x, y = numpy.mgrid[0.25:0.75+1e-10:dx, 0.25:0.75+1e-10:dx]
    x = x.ravel()
    y = y.ravel()

    np = len(x)
    
    u = numpy.zeros_like(x)
    v = numpy.zeros_like(x)

    m = numpy.ones_like(x) * dx**2

    vol_per_particle = numpy.power(0.5**2/np ,1.0/2.0)
    radius = 2 * vol_per_particle

    print "Using smoothing length: ", radius

    h = numpy.ones_like(x) * radius

    fluid = base.get_particle_array(name="fluid", type=base.Fluid,
                                    x=x, y=y,
                                    u=u, v=v,
                                    m=m,
                                    h=h)

    print "Number of particles: ", fluid.get_number_of_particles()

    return [fluid,]
Пример #21
0
def get_circular_patch(name="", type=0, dx=0.05):
    
    x,y = numpy.mgrid[-1.05:1.05+1e-4:dx, -1.05:1.05+1e-4:dx]
    x = x.ravel()
    y = y.ravel()
 
    m = numpy.ones_like(x)*dx*dx
    h = numpy.ones_like(x)*2*dx
    rho = numpy.ones_like(x)

    p = 0.5*1.0*100*100*(1 - (x**2 + y**2))

    cs = numpy.ones_like(x) * 100.0

    u = 0*x
    v = 0*y

    indices = []

    for i in range(len(x)):
        if numpy.sqrt(x[i]*x[i] + y[i]*y[i]) - 1 > 1e-10:
            indices.append(i)
            
    pa = base.get_particle_array(x=x, y=y, m=m, rho=rho, h=h, p=p, u=u, v=v,
                                 cs=cs,name=name, type=type)

    la = base.LongArray(len(indices))
    la.set_data(numpy.array(indices))

    pa.remove_particles(la)

    pa.set(idx=numpy.arange(len(pa.x)))

    return pa
Пример #22
0
    def setUp(self):

        # create a dummy particle array
        self.pa = pa = base.get_particle_array(name="test",
                                               cl_precision="single",
                                               type=1)

        # create a simple solver with one operation
        self.s = s = solver.Solver(3, solver.EulerIntegrator)
        
        # add the velocity gradient operation 
        s.add_operation(solver.SPHOperation(

            sph.VelocityGradient3D.withargs(), on_types=[1,],
            from_types=[1,], id="vgrad")

                             )
        # add the stress rate function
        s.add_operation(solver.SPHIntegration(

            sph.HookesDeviatoricStressRate3D.withargs(),
            on_types=[1,], id="stress_rate")

                             )
        particles = base.Particles(arrays=[pa,])
        s.setup(particles)

        self.integrator = s.integrator
Пример #23
0
    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()
Пример #24
0
    def setUp(self):
        """ The setup consists of a 2D square ([0,1] X [0,1]) with 25
        particles.


        """

        xc = numpy.arange(0,1.0, 0.2)
        x, y = numpy.meshgrid(xc,xc)

        self.x = x = x.ravel()
        self.y = y = y.ravel()
        self.h = h = numpy.ones_like(x) * 0.25

        dx = dy = 0.2
        self.dx = dx

        self.block_size = 0.5
        self.cell_size = 0.5

        self.pa = pa = base.get_particle_array(name="test", x=x, y=y, h=h)

        self.cm = cm = parallel.ParallelCellManager(arrays_to_bin=[pa,],
                                                    max_radius_scale=2.0,
                                                    dimension=2.0,
                                                    load_balancing=False,
                                                    initialize=False)

        self.block_000_indices = 0,1,2,5,6,7,10,11,12
        self.block_100_indices = 3,4,8,9,13,14
        self.block_010_indices = 15,16,17,20,21,22
        self.block_110_indices = 18,19,23,24
Пример #25
0
    def setUp(self):
        """ A Dummy fluid solver is created with the following operations

        (i)  -- Equation of State
        (ii) -- Density Rate
        (iii)-- Momentum Equation Pressure Gradient
        (iv) -- Momentum Equation Viscosity 
        
        """
        self.kernel = kernel = base.CubicSplineKernel(dim = 2)

        self.solver = s = solver.Solver(dim=2,
                                        integrator_type=solver.EulerIntegrator)

        s.default_kernel = kernel

        self.particles = base.Particles(arrays=[base.get_particle_array()])

        # Create some replacement operations
        
        self.visc = solver.SPHIntegration(

            sph.MorrisViscosity, on_types=[Fluids],
            from_types = [Fluids, Solids], updates=['u','v'], id='visc'
            
            )

        self.summation_density = solver.SPHOperation(

            sph.SPHRho, on_types=[Fluids, Solids],
            updates=['rho'], id='sd'

            )
Пример #26
0
def get_boundary_particles():
    """ Get the particles corresponding to the dam and fluids """
    
    xb1, yb1 = geom.create_2D_tank(x1=0, y1=0,
                                   x2=container_width, y2=container_height,
                                   dx=dx)
    
    xb2, yb2 = geom.create_2D_tank(x1=-dx/2, y1=-dx/2,
                                   x2=container_width, y2=container_height,
                                   dx=dx)

    xb = numpy.concatenate((xb1, xb2))
    yb = numpy.concatenate((yb1, yb2))

    hb = numpy.ones_like(xb)*h
    mb = numpy.ones_like(xb)*dx*dy*ro*0.5
    rhob = numpy.ones_like(xb) * ro

    cb = numpy.ones_like(xb)*co

    boundary = base.get_particle_array(cl_precision="single",
                                       name="boundary", type=Solid, 
                                       x=xb, y=yb, h=hb, rho=rhob, cs=cb,
                                       m=mb)

    print 'Number of Boundary particles: ', len(xb)

    return boundary
Пример #27
0
    def test_load_output(self):
        
        self.setup_solver()
        s = self.solver
        s.particles = self.particles
        d = tempfile.mkdtemp()
        s.output_directory = d
        s.detailed_output = True
        s.fname = 'temp_solver'
        s.dt = 0

        x = numpy.arange(10)
        pa = base.get_particle_array(name='pa', x=x)
        #pa = base.ParticleArray(name='pa')
        pa.add_property(dict(name='q', data=-x))
        s.particles.arrays[0] = pa
        
        old_props = {}
        for name,prop in s.particles.arrays[0].properties.iteritems():
            old_props[name] = prop.get_npy_array().copy()

        s.dump_output(s.dt)
        pa.q = pa.x = pa.z
        ret = s.load_output('?')
        self.assertEqual(ret, ["0"])
        s.load_output('0')

        try:
            for name,prop in s.particles.arrays[0].properties.iteritems():
                self.assertTrue(numpy.allclose(prop,old_props[name]),
                                msg='prop:%s\nold:%s, new:%s'%(name,old_props[name], pa.get(name)))
        finally:
            shutil.rmtree(d, True)
Пример #28
0
    def setUp(self):
        """ The setup consists of four particles placed at the
        vertices of a unit square.

        The function tested is

        ..math::

        \frac{DU_a}{Dt} = \frac{1}{2}\sum_{b=1}^{N}m_b\left[
        \left(\frac{p_a}{\rho_a^2} + \frac{p_b}{\rho_b^2}\right)\,(v_a -
        v_b)\right]\,\nabla_a \cdot W_{ab}

        The mass of each particle is 1

        """
        
        self.precision = "single"

        self.np = 4

        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)
        
        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test", x=x,  y=y, z=z,
                                               m=m, u=u, p=p,
                                               tmpx=tmpx, tmpy=tmpy, tmpz=tmpz,
                                               cl_precision=self.precision)

        env = sph.EnergyEquationNoVisc.withargs()
        ewv = sph.EnergyEquation.withargs(alpha=1.0, beta=1.0,
                                          gamma=1.4, eta=0.1)


        self.env = env.get_func(pa,pa)
        self.ewv = ewv.get_func(pa,pa)
        
        self.env.kernel = base.CubicSplineKernel(dim=2)
        self.env.nbr_locator = \
                             base.Particles.get_neighbor_particle_locator(pa,
                                                                          pa)

        self.ewv.kernel = base.CubicSplineKernel(dim=2)
        self.ewv.nbr_locator = \
                             base.Particles.get_neighbor_particle_locator(pa,
                                                                          pa)

        self.setup_cl()
Пример #29
0
    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.precision = "single"

        self.np = 4

        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)
        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test",
                                               x=x,
                                               y=y,
                                               z=z,
                                               m=m,
                                               tmpx=tmpx,
                                               tmpy=tmpy,
                                               tmpz=tmpz,
                                               cl_precision=self.precision)

        self.func = func = sph.NBodyForce.get_func(pa, pa)

        self.eps = func.eps

        if solver.HAS_CL:
            self.ctx = ctx = cl.create_some_context()
            self.q = q = cl.CommandQueue(ctx)

            pa.setup_cl(ctx, q)

            pysph_root = solver.get_pysph_root()

            template = solver.cl_read(
                path.join(pysph_root, "sph/funcs/external_force.clt"),
                function_name=func.cl_kernel_function_name,
                precision=self.precision)

            prog_src = solver.create_program(template, func)

            self.prog = cl.Program(ctx,
                                   prog_src).build(solver.get_cl_include())
Пример #30
0
    def setUp(self):
        """ The setup consists of four particles placed at the
        vertices of a unit square. The pressure gradient term to be
        tested is

        ..math::

                \frac{\nablaP}{\rho}_i = \sum_{j=1}^{4}
                -m_j(\frac{Pa}{\rho_a^2} + \frac{Pb}{\rho_b^2})\nabla W_{ab}

        The mass of each particle is 1

        """
        
        self.precision = "single"

        self.np = 4

        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)
        
        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test", x=x,  y=y, z=z,
                                               m=m, u=u, p=p,
                                               tmpx=tmpx, tmpy=tmpy, tmpz=tmpz,
                                               cl_precision=self.precision)

        grad_func = sph.SPHPressureGradient.withargs()
        mom_func = sph.MomentumEquation.withargs(alpha=1.0, beta=1.0,
                                                 gamma=1.4, eta=0.1)


        self.grad_func = grad_func.get_func(pa,pa)
        self.mom_func = mom_func.get_func(pa,pa)
        
        self.grad_func.kernel = base.CubicSplineKernel(dim=2)
        self.grad_func.nbr_locator = \
                              base.Particles.get_neighbor_particle_locator(pa,
                                                                           pa)

        self.mom_func.kernel = base.CubicSplineKernel(dim=2)
        self.mom_func.nbr_locator = \
                             base.Particles.get_neighbor_particle_locator(pa,
                                                                          pa)

        self.setup_cl()
Пример #31
0
    def setUp(self):
        """The setup consists of ten particles with the following cellids:

        cellids = [7, 5, 6, 1, 8, 2, 5, 3, 8, 7]

        Nine cells are created by a manager that uses cells for
        binning. The constant cell size of the domain manager is used
        to create this special structure.
        
        """
        self.cl_precision = cl_precision = "single"

        self.x = x = numpy.array( [0.3, 0.6, 0.1, 0.3, 0.6,
                                   0.6, 0.6, 0.1, 0.6, 0.3] )

        self.y = y = numpy.array( [0.6, 0.3, 0.6, 0.1, 0.6,
                                   0.1, 0.3, 0.3, 0.6, 0.6] )

        self.np = len(x)

        # Construct the ParticleArray to be used with the Cython manager
        self.cy_pa = base.get_particle_array(
            name='test', cl_precision=cl_precision,
            x=x, y=y)

        # Construct the ParticleArray to be used with the OpenCL manager
        self.cl_pa = base.get_particle_array(
            name="test", cl_precision=self.cl_precision,
            x=x, y=y)

        # constant cell size to use
        self.const_cell_size = 0.25

        # constants based on the initial data
        self.mx, self.my = 0.1, 0.1
        self.Mx, self.My = 0.6, 0.6

        self.ncells = 9
        self.ncellsp1 = 10

        # perform the individual manager setup
        self._setup()
Пример #32
0
    def setUp(self):
        """
        Setup a default simulation in 2D. The setup consists of four particles
        on a circle of radius 2/pi. The particles are constrained to move
        along the circle with forcing functions defined in 
        pysph/sph/funcs/external_forces.pyx 
        
        With the choice of radius and unit magnitude of velocity, the particles
        move by pi/2 radians in 1 second. 

        """
        self.r = r = 2. / numpy.pi
        self.x = x = numpy.array([1.0, 0.0, -1.0, 0.0])
        self.y = y = numpy.array([0.0, 1.0, 0.0, -1.0])

        x *= r
        y *= r

        p = numpy.zeros_like(x)
        e = numpy.zeros_like(x)
        h = numpy.ones_like(x)

        self.pa = pa = base.get_particle_array(x=x,
                                               y=y,
                                               p=p,
                                               e=e,
                                               h=h,
                                               name='tmp')

        self.particles = particles = base.Particles(arrays=[pa])
        self.kernel = base.CubicSplineKernel(dim=2)

        circlex = solver.SPHIntegration(sph.MoveCircleX,
                                        from_types=[Fluids],
                                        on_types=[Fluids],
                                        updates=['x', 'y'],
                                        id='circlex')

        circley = solver.SPHIntegration(sph.MoveCircleY,
                                        from_types=[Fluids],
                                        on_types=[Fluids],
                                        updates=['x', 'y'],
                                        id='circley')

        self.calcx = circlex.get_calcs(particles, self.kernel)
        self.calcy = circley.get_calcs(particles, self.kernel)

        self.calcs = []
        self.calcs.extend(self.calcx)
        self.calcs.extend(self.calcy)

        self.integrator = Integrator(particles=particles, calcs=self.calcs)

        self.setup()
Пример #33
0
def test_sph_calc():

    x = numpy.array([
        0,
    ])
    y = numpy.array([
        0,
    ])
    z = numpy.array([
        0,
    ])
    h = numpy.ones_like(x)

    pa = base.get_particle_array(name="test",
                                 x=x,
                                 y=y,
                                 z=z,
                                 h=h,
                                 _tmpx=z,
                                 _tmpy=z,
                                 _tmpz=z)
    particles = base.Particles(arrays=[
        pa,
    ])
    kernel = base.CubicSplineKernel(dim=1)

    vector_force1 = sph.VectorForce.withargs(force=base.Point(1, 1, 1))
    vector_force2 = sph.VectorForce.withargs(force=base.Point(1, 1, 1))

    func1 = vector_force1.get_func(pa, pa)
    func2 = vector_force2.get_func(pa, pa)

    calc = sph.SPHCalc(particles=particles,
                       sources=[pa, pa],
                       dest=pa,
                       kernel=kernel,
                       funcs=[func1, func2],
                       updates=['u', 'v', 'w'],
                       integrates=True)

    # evaluate the calc. Accelerations are stored in _tmpx, _tmpy and _tmpz

    calc.sph('_tmpx', '_tmpy', '_tmpz')

    tmpx, tmpy, tmpz = pa.get('_tmpx', '_tmpy', '_tmpz')

    # the acceleration should be 2 in each direction

    assert (abs(tmpx[0] - 2.0) < 1e-16)
    assert (abs(tmpy[0] - 2.0) < 1e-16)
    assert (abs(tmpz[0] - 2.0) < 1e-16)
Пример #34
0
def get_fluid_particles(name="fluid"):
    
    x, y = get_2D_staggered_grid(base.Point(dx, dx), base.Point(dx/2, dx/2), 
                                 base.Point(1.0,2.0), dx)

    hf = numpy.ones_like(x) * h
    mf = numpy.ones_like(x) * dx * dy * ro
    rhof = numpy.ones_like(x) * ro
    csf = numpy.ones_like(x) * co
    
    fluid = base.get_particle_array(name=name, type=Fluid,
                                    x=x, y=y, h=hf, m=mf, rho=rhof, cs=csf)

    return fluid
Пример #35
0
    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.precision = "single"

        self.np = 4

        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)
        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test", x=x,  y=y, z=z,
                                               m=m, tmpx=tmpx, tmpy=tmpy,
                                               tmpz=tmpz,
                                               cl_precision=self.precision)

        self.func = func = sph.NBodyForce.get_func(pa, pa)

        self.eps = func.eps

        if solver.HAS_CL:
            self.ctx = ctx = cl.create_some_context()
            self.q = q = cl.CommandQueue(ctx)

            pa.setup_cl(ctx, q)
            
            pysph_root = solver.get_pysph_root()
            
            template = solver.cl_read(
                path.join(pysph_root, "sph/funcs/external_force.clt"),
                function_name=func.cl_kernel_function_name,
                precision=self.precision)

            prog_src = solver.create_program(template, func)

            self.prog = cl.Program(ctx, prog_src).build(solver.get_cl_include())
Пример #36
0
    def setUp(self):
        """ Setup for SPHOperationTestCase

        Setup:
        ------
        Create two particle arrays, one Fluid and one Solid.
        Instantiate the class with a default function `SPHRho` with 
        various combinations of the from and on types and check for the 
        filtering of the arrays.

        """

        x = numpy.linspace(0, 1, 11)
        h = numpy.ones_like(x) * 2 * (x[1] - x[0])

        #create the fluid particle array

        self.fluid = base.get_particle_array(name='fluid',
                                             type=Fluid,
                                             x=x,
                                             h=h)

        #create the solid particle array

        self.solid = base.get_particle_array(name="solid",
                                             type=Solid,
                                             x=x,
                                             h=h)

        #create the particles

        self.particles = particles = base.Particles(
            arrays=[self.fluid, self.solid])

        #set the kernel

        self.kernel = base.CubicSplineKernel()
Пример #37
0
def create_particles(**kwargs):
    x = numpy.zeros(0)
    y = numpy.zeros(0)
    u = numpy.zeros(0)
    v = numpy.zeros(0)
    m = numpy.zeros(0)

    rad = 0.0

    for j in range(1, n+1):
        npnts = 4*j
        dtheta = 2*pi/npnts

        theta = numpy.arange(0, 2*pi-1e-10, dtheta)
        rad = rad + dr

        _x = rad*cos(theta)
        _y = rad*sin(theta)

        _u = -cos(theta)
        _v = -sin(theta)

        if j == 1:
            _m = numpy.ones_like(_x) * m1
        else:
            _m = numpy.ones_like(_x) * (2.0*j - 1.0)/(j) * m1

        x = numpy.concatenate( (x, _x) )
        y = numpy.concatenate( (y, _y) )
        m = numpy.concatenate( (m, _m) )
        u = numpy.concatenate( (u, _u) )
        v = numpy.concatenate( (v, _v) )

    rho = numpy.ones_like(x) * 1.0
    h = numpy.ones_like(x) * h0
    p = numpy.ones_like(x) * 0.0
    e = numpy.ones_like(x) * 0.0
    rhop = numpy.ones_like(x)
    div = numpy.zeros_like(x)
    q = numpy.zeros_like(x)

    fluid = base.get_particle_array(name="fluid", type=base.Fluid,
                                    x=x,y=y,m=m,rho=rho, h=h,
                                    u=u,v=v,p=p,e=e,
                                    rhop=rhop, q=q, div=div)

    print "Number of fluid particles = ", fluid.get_number_of_particles()

    return fluid
Пример #38
0
    def setUp(self):

        parrays = []

        for i in range(np):
            x = numpy.array([x0[i]])
            y = numpy.array([y0[i]])
            z = numpy.array([z0[i]])
            h = numpy.array([h0[i]])

            mi = numpy.array([m[i]])

            name = 'array' + str(i)
            pa = base.get_particle_array(name=name, x=x, y=y, z=z, h=h, m=mi)

            parrays.append(pa)

        self.parrays = parrays

        self.particles = base.Particles(arrays=parrays)

        kernel = base.CubicSplineKernel(dim=3)

        self.solver = s = solver.Solver(dim=3,
                                        integrator_type=solver.EulerIntegrator)

        # NBodyForce operation

        s.add_operation(
            solver.SPHIntegration(sph.NBodyForce.withargs(eps=eps),
                                  on_types=[Fluids],
                                  from_types=[Fluids],
                                  updates=['u', 'v', 'w'],
                                  id='nbody_force'))

        # position stepping

        s.add_operation_step([Fluids])

        # time step and final time

        s.set_final_time(tf)
        s.set_time_step(dt)

        # setup the integrator

        s.setup_integrator(self.particles)
Пример #39
0
    def setUp(self):
        """ The setup consists of four particles placed at the
        vertices of a unit square.

        The function tested is

        ..math::

        p_a = (\gamma - 1.0)\rho_a U_a
        cs_a = \sqrt( (\gamma - 1.0) U_a )


        """
        
        self.precision = "single"

        self.np = 4

        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)

        e = numpy.array([1, 2, 1, 2], numpy.float64)
        rho = numpy.array([2, 1, 2, 1], numpy.float64)
        
        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test", x=x,  y=y, z=z,
                                               m=m, e=e, rho=rho,
                                               tmpx=tmpx, tmpy=tmpy, tmpz=tmpz,
                                               cl_precision=self.precision)

        ideal = sph.IdealGasEquation.withargs(gamma=1.4)

        self.ideal = ideal.get_func(pa,pa)
        
        self.ideal.nbr_locator = \
                               base.Particles.get_neighbor_particle_locator(pa,
                                                                            pa)

        self.setup_cl()
Пример #40
0
def get_circular_patch(name="", type=0, dx=0.025):

    x, y = numpy.mgrid[-1.05:1.05 + 1e-4:dx, -1.05:1.05 + 1e-4:dx]
    x = x.ravel()
    y = y.ravel()

    m = numpy.ones_like(x) * dx * dx
    h = numpy.ones_like(x) * 2 * dx
    rho = numpy.ones_like(x)

    p = 0.5 * 1.0 * 100 * 100 * (1 - (x**2 + y**2))

    cs = numpy.ones_like(x) * 100.0

    u = -100 * x
    v = 100 * y

    indices = []

    for i in range(len(x)):
        if numpy.sqrt(x[i] * x[i] + y[i] * y[i]) - 1 > 1e-10:
            indices.append(i)

    pa = base.get_particle_array(x=x,
                                 y=y,
                                 m=m,
                                 rho=rho,
                                 h=h,
                                 p=p,
                                 u=u,
                                 v=v,
                                 cs=cs,
                                 name=name,
                                 type=type)

    la = base.LongArray(len(indices))
    la.set_data(numpy.array(indices))

    pa.remove_particles(la)

    pa.set(idx=numpy.arange(len(pa.x)))

    print 'Number of particles: ', len(pa.x)

    return pa
Пример #41
0
def get_fluid():
    """ Get the fluid particle array """

    x, y = numpy.mgrid[dx:box_length - 1e-10:dx, dx:box_height - 1e-10:dx]

    xf, yf = x.ravel(), y.ravel()

    mf = numpy.ones_like(xf) * m
    hf = numpy.ones_like(xf) * h

    rhof = numpy.ones_like(xf) * ro
    cf = numpy.ones_like(xf) * co
    pf = numpy.zeros_like(xf)

    fluid = base.get_particle_array(name="fluid",
                                    type=Fluid,
                                    x=xf,
                                    y=yf,
                                    h=hf,
                                    rho=rhof,
                                    c=cf,
                                    p=pf)

    # remove indices within the square

    indices = []

    np = fluid.get_number_of_particles()
    x, y = fluid.get('x', 'y')

    for i in range(np):
        if 1.0 - dx / 2 <= x[i] <= 2.0 + dx / 2:
            if 2.0 - dx / 2 <= y[i] <= 3.0 + dx / 2:
                indices.append(i)

    to_remove = base.LongArray(len(indices))
    to_remove.set_data(numpy.array(indices))

    fluid.remove_particles(to_remove)

    return fluid
Пример #42
0
def get_dummy_particles():
    x, y = numpy.mgrid[-5 * dx:box_length + 5 * dx + 1e-10:dx,
                       -5 * dx:box_height + 5 * dx + 1e-10:dx]

    xd, yd = x.ravel(), y.ravel()

    md = numpy.ones_like(xd) * m
    hd = numpy.ones_like(xd) * h

    rhod = numpy.ones_like(xd) * ro
    cd = numpy.ones_like(xd) * co
    pd = numpy.zeros_like(xd)

    dummy_fluid = base.get_particle_array(name="dummy_fluid",
                                          type=Fluid,
                                          x=xd,
                                          y=yd,
                                          h=hd,
                                          rho=rhod,
                                          c=cd,
                                          p=pd)

    # remove indices within the square

    indices = []

    np = dummy_fluid.get_number_of_particles()
    x, y = dummy_fluid.get('x', 'y')

    for i in range(np):
        if -dx / 2 <= x[i] <= box_length + dx / 2:
            if -dx / 2 <= y[i] <= box_height + dx / 2:
                indices.append(i)

    to_remove = base.LongArray(len(indices))
    to_remove.set_data(numpy.array(indices))

    dummy_fluid.remove_particles(to_remove)

    return dummy_fluid
Пример #43
0
def standard_shock_tube_data(name="", type=0):
    """ Standard 400 particles shock tube problem """

    dxl = 0.001875
    dxr = dxl * 4

    x = numpy.ones(400, float)
    x[:320] = numpy.arange(-0.6, -dxl + 1e-4, dxl)
    x[320:] = numpy.arange(dxr, 0.6 + 1e-4, dxr)

    m = numpy.ones_like(x) * dxl
    h = numpy.ones_like(x) * 2 * dxr

    rho = numpy.ones_like(x)
    rho[320:] = 0.25

    u = numpy.zeros_like(x)

    e = numpy.ones_like(x)
    e[:320] = 2.5
    e[320:] = 1.795

    p = 0.4 * rho * e

    cs = numpy.sqrt(1.4 * p / rho)

    idx = numpy.arange(400)

    return base.get_particle_array(name=name,
                                   x=x,
                                   m=m,
                                   h=h,
                                   rho=rho,
                                   p=p,
                                   e=e,
                                   cs=cs,
                                   type=type,
                                   idx=idx)
Пример #44
0
    def setUp(self):
        """ Create some default particle properties """

        x = numpy.linspace(0, 1, 11)
        m = numpy.ones_like(x) * (x[1] - x[0])
        rho = numpy.ones_like(x)
        h = 2 * m

        pa = base.get_particle_array(cl_precision="single",
                                     x=x,
                                     h=h,
                                     m=m,
                                     rho=rho)

        self.pa = pa

        platforms = cl.get_platforms()
        devices = platforms[0].get_devices()

        self.ctx = ctx = cl.Context(devices)
        queue = cl.CommandQueue(ctx, devices[0])

        self.queue = queue
Пример #45
0
def get_fluid_particles():

    x, y = get_2D_staggered_grid(base.Point(dx, dx),
                                 base.Point(dx / 2, dx / 2),
                                 base.Point(1.0, 2.0), dx)

    print 'Number of fluid particles: ', len(x)

    hf = numpy.ones_like(x) * h
    mf = numpy.ones_like(x) * dx * dy * ro * 0.5
    rhof = numpy.ones_like(x) * ro
    csf = numpy.ones_like(x) * co

    fluid = base.get_particle_array(name="fluid",
                                    type=Fluid,
                                    x=x,
                                    y=y,
                                    h=hf,
                                    m=mf,
                                    rho=rhof,
                                    cs=csf)

    return fluid
Пример #46
0
        print("Device compute units:", device.max_compute_units)

        precision_types = ['single']

        device_extensions = device.get_info(cl.device_info.EXTENSIONS)
        if 'cl_khr_fp64' in device_extensions:
            precision_types.append('double')

        for prec in precision_types:

            print "\n========================================================"
            print "Summation Density Comparison using %s precision" % (prec)

            pa = base.get_particle_array(cl_precision=prec,
                                         name="test",
                                         x=x,
                                         h=h,
                                         m=m,
                                         rho=rho)

            particles = base.Particles(arrays=[
                pa,
            ],
                                       locator_type=NSquareNeighborLocator)

            kernel = base.CubicSplineKernel(dim=1)

            # create the function
            func = sph.SPHRho.get_func(pa, pa)

            # create the CLCalc object
            cl_calc = sph.CLCalc(particles=particles,
Пример #47
0
app.process_command_line()

#global variables
h = 2.097e-2
dx = dy = h/(1.3)
g = -9.81

xf = numpy.array([0])
yf = numpy.array([0.3])
hf = numpy.array([h])
mf = numpy.array([1.0])
vf = numpy.array([0.0])
cf = numpy.array([25.0])
rhof = numpy.array([1.0])

fluid = base.get_particle_array(name="fluid", type=Fluid, x=xf, y=yf,
                                h=hf, m=mf, rho=rhof, v=vf, cs=cf)

#generate the boundary
l = base.Line(base.Point(-.5), 1.0, 0)
g = base.Geometry('line', [l], False)
g.mesh_geometry(dx)
boundary = g.get_particle_array(re_orient=True)

boundary.m[:] = 1.0

particles = base.Particles(arrays=[fluid, boundary])
app.particles = particles


kernel = base.HarmonicKernel(dim=2, n=3)
Пример #48
0
    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,
        ])

        tmpx1 = numpy.ones_like(x1)
        tmpx2 = numpy.ones_like(x2)

        self.f1 = base.get_particle_array(name="fluid1",
                                          x=x1,
                                          y=y1,
                                          tmpx=tmpx1)
        self.f2 = base.get_particle_array(name="fluid2",
                                          x=x2,
                                          y=y2,
                                          tmpx=tmpx2)

        self.particles = base.Particles(arrays=[self.f1, self.f2])
        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 = cl.create_some_context()
        self.integrator.setup_integrator(ctx)
        self.queue = calcs[0].queue

        self.dt = 0.1
        self.nsteps = 10
Пример #49
0
vf = numpy.array([0.0])
cf = numpy.array([25.0])
rhof = numpy.array([1.0])

#define the staggered grid of boundary particles
xb = numpy.array([-dx, 0, dx, -dx / 2, dx / 2])
yb = numpy.array([0, 0, 0, -dy / 2, -dy / 2])
hb = numpy.ones_like(xb) * h
mb = numpy.ones_like(xb)
rhob = numpy.ones_like(xb)

fluid = base.get_particle_array(name="fluid",
                                type=0,
                                x=xf,
                                y=yf,
                                h=hf,
                                m=mf,
                                rho=rhof,
                                v=vf,
                                cf=cf)

boundary = base.get_particle_array(name="boundary",
                                   type=1,
                                   x=xb,
                                   y=yb,
                                   h=hb,
                                   m=mb,
                                   rho=rhob)

particles = base.Particles(arrays=[boundary, fluid])
app.particles = particles
Пример #50
0
def get_boundary_particles():
    """ Get the particles corresponding to the dam and fluids """

    #left wall
    ylw = get_1D_grid(0, container_height, dy)
    xlw = numpy.zeros_like(ylw)
    nb1 = len(ylw)

    #bottom
    xbs = get_1D_grid(dx, container_width + dx, dx)
    ybs = numpy.zeros_like(xbs)
    nb3 = len(xbs)

    max_xb = numpy.max(xbs)

    #staggered left wall
    yslw = get_1D_grid(-dx / 2, container_height, dx)
    xslw = numpy.ones_like(yslw) * -dx / 2
    nb4 = len(yslw)

    #staggered bottom
    xsb = get_1D_grid(dx / 2, container_width + dx + dx, dx)
    ysb = numpy.ones_like(xsb) * -dy / 2
    nb6 = len(xsb)

    max_xsb = numpy.max(xsb)

    #right wall
    yrw = numpy.arange(dx, container_height, dx)
    xrw = numpy.ones_like(yrw) * max_xb
    nb2 = len(yrw)

    #staggered right wall
    ysrw = numpy.arange(dy / 2, container_height, dy)
    xsrw = numpy.ones_like(ysrw) * max_xsb
    nb5 = len(ysrw)

    nb = nb1 + nb2 + nb3 + nb4 + nb5 + nb6

    print "Number of Boundary Particles: ", nb

    xb = numpy.zeros(nb, float)
    yb = numpy.zeros(nb, float)

    idx = 0

    xb[:nb1] = xlw
    yb[:nb1] = ylw

    idx += nb1

    xb[idx:idx + nb2] = xrw
    yb[idx:idx + nb2] = yrw

    idx += nb2

    xb[idx:idx + nb3] = xbs
    yb[idx:idx + nb3] = ybs
    idx += nb3

    xb[idx:idx + nb4] = xslw
    yb[idx:idx + nb4] = yslw
    idx += nb4

    xb[idx:idx + nb5] = xsrw
    yb[idx:idx + nb5] = ysrw
    idx += nb5

    xb[idx:] = xsb
    yb[idx:] = ysb

    hb = numpy.ones_like(xb) * h
    mb = numpy.ones_like(xb) * dx * dy * ro * 0.5
    rhob = numpy.ones_like(xb) * ro

    cb = numpy.ones_like(xb) * co

    boundary = base.get_particle_array(name="boundary",
                                       type=Solid,
                                       x=xb,
                                       y=yb,
                                       h=hb,
                                       rho=rhob,
                                       cs=cb,
                                       m=mb)

    return boundary
Пример #51
0
    h = numpy.ones_like(x) * 0.2
    m = numpy.ones_like(x) * 0.1
    rho = numpy.ones_like(x)
    fval = x * x

#create the particles on processor 1
if rank == 1:
    x = numpy.linspace(1.1, 2, 10)
    h = numpy.ones_like(x) * 0.2
    m = numpy.ones_like(x) * 0.1
    rho = numpy.ones_like(x)
    fval = x * x

#create the particles in parallel without load balancing
kernel = CubicSplineKernel(dim=1)
pa = get_particle_array(x=x, h=h, m=m, fval=fval, rho=rho)
particles = Particles([pa], in_parallel=True, load_balancing=False)

#make sure the particles need updating
particles.update()

#choose the function and the sph calc
func = sph.SPHRho(pa, pa)
calc = sph.SPHCalc(particles=particles,
                   kernel=kernel,
                   func=func,
                   updates=['rho'],
                   integrates=False)

tmpx = pa.get('tmpx', only_real_particles=False)
logger.debug('tempx for all particles %s' % (tmpx))
Пример #52
0
    def setUp(self):
        """ The setup consists of four particles placed at the
        vertices of a unit square.

        The function tested is

        ..math::

        \frac{DU_a}{Dt} = \frac{1}{2}\sum_{b=1}^{N}m_b\left[
        \left(\frac{p_a}{\rho_a^2} + \frac{p_b}{\rho_b^2}\right)\,(v_a -
        v_b)\right]\,\nabla_a \cdot W_{ab}

        The mass of each particle is 1

        """

        self.precision = "single"

        self.np = 4

        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)

        tmpx = numpy.zeros_like(x)
        tmpy = numpy.zeros_like(x)
        tmpz = numpy.zeros_like(x)

        self.pa = pa = base.get_particle_array(name="test",
                                               x=x,
                                               y=y,
                                               z=z,
                                               m=m,
                                               u=u,
                                               p=p,
                                               tmpx=tmpx,
                                               tmpy=tmpy,
                                               tmpz=tmpz,
                                               cl_precision=self.precision)

        env = sph.EnergyEquationNoVisc.withargs()
        ewv = sph.EnergyEquation.withargs(alpha=1.0,
                                          beta=1.0,
                                          gamma=1.4,
                                          eta=0.1)

        self.env = env.get_func(pa, pa)
        self.ewv = ewv.get_func(pa, pa)

        self.env.kernel = base.CubicSplineKernel(dim=2)
        self.env.nbr_locator = \
                             base.Particles.get_neighbor_particle_locator(pa,
                                                                          pa)

        self.ewv.kernel = base.CubicSplineKernel(dim=2)
        self.ewv.nbr_locator = \
                             base.Particles.get_neighbor_particle_locator(pa,
                                                                          pa)

        self.setup_cl()
Пример #53
0
h = h = numpy.ones_like(x) * 0.25

dx = dy = 0.2
dx = dx

block_size = 0.5
cell_size = 0.5

block_000_indices = 0, 1, 2, 5, 6, 7, 10, 11, 12
block_100_indices = 3, 4, 8, 9, 13, 14
block_010_indices = 15, 16, 17, 20, 21, 22
block_110_indices = 18, 19, 23, 24

name = "rank" + str(pid)

pa = base.get_particle_array(name="test", x=x, y=y, h=h)

pa.x += 1.0 * pid
pa.x += 1e-10

pa.y += 1.0 * (pid % 2)
pa.y += 1e-10

# create the cell manager
cm = parallel.ParallelCellManager(arrays_to_bin=[
    pa,
],
                                  max_radius_scale=2.0,
                                  dimension=2.0,
                                  load_balancing=False,
                                  initialize=False,
Пример #54
0
    def setUp(self):
        """ The setup consists of four particles placed at the
        vertices of a unit square. 
        
        """

        self.precision = "single"

        self.np = 4

        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.pa = pa = base.get_particle_array(name="test",
                                               x=x,
                                               y=y,
                                               z=z,
                                               m=m,
                                               u=u,
                                               p=p,
                                               cl_precision=self.precision)

        self.particles = particles = base.Particles([
            pa,
        ])

        sphrho_func = sph.SPHRho.withargs()
        density_rate_func = sph.SPHDensityRate.withargs()

        self.sphrho_func = sphrho_func.get_func(pa, pa)
        self.density_rate_func = density_rate_func.get_func(pa, pa)

        self.sphrho_func.kernel = base.CubicSplineKernel(dim=2)
        self.density_rate_func.kernel = base.CubicSplineKernel(dim=2)

        self.rho_calc = sph.SPHCalc(particles, [
            pa,
        ],
                                    pa,
                                    base.CubicSplineKernel(dim=2), [
                                        self.sphrho_func,
                                    ],
                                    updates=['rho'])

        self.rho_calc_cl = sph.CLCalc(particles, [
            pa,
        ],
                                      pa,
                                      base.CubicSplineKernel(dim=2), [
                                          self.sphrho_func,
                                      ],
                                      updates=['rho'])

        if solver.HAS_CL:
            self.ctx = ctx = cl.create_some_context()
            self.q = q = cl.CommandQueue(ctx)
            self.rho_calc_cl.setup_cl(ctx)
Пример #55
0
    def setUp(self):
        """ A simple simulation in 2D with boundary particles spaced with a
        distance dp. The fluid particles are in a row just above as shown
        in the fgure. 

                   dx
                  o   o   o   o   o
            x x x x x x x x x x x x x x x  

        Y
        |  Z
        | /
        |/___ X
            

        Expected Behavior:
        -------------------
        The Monaghan Boundary force is defned such that a particle moving 
        parallel to the wall experiences a constant force.
        Each fluid particle can be thought of successive instances of a 
        moving particle and hence each of them should experience the same
        force.

        """

        #fluid particle spacing
        self.dx = dx = 0.1

        #solid particle spacing
        self.dp = dp = 0.05

        #the fluid properties
        xf = numpy.array([-.2, -.1, 0.0, 0.1, 0.2])
        yf = numpy.array([dp, dp, dp, dp, dp])
        hf = numpy.ones_like(xf) * 2 * dx
        mf = numpy.ones_like(xf) * dx
        cs = numpy.ones_like(xf)
        rhof = numpy.ones_like(xf)

        self.fluid = base.get_particle_array(x=xf,
                                             y=yf,
                                             h=hf,
                                             m=mf,
                                             rho=rhof,
                                             cs=cs,
                                             ax=mf,
                                             ay=mf,
                                             az=mf,
                                             name='fluid',
                                             type=Fluid)

        l = base.Line(base.Point(-0.35), 0.70, 0.0)
        g = base.Geometry('line', lines=[l], is_closed=False)
        g.mesh_geometry(dp)
        self.solid = g.get_particle_array(re_orient=True)

        self.particles = particles = base.Particles(
            arrays=[self.fluid, self.solid])

        self.kernel = kernel = base.CubicSplineKernel(dim=2)

        self.solver = solver.Solver(kernel.dim, solver.EulerIntegrator)

        self.solver.add_operation(
            solver.SPHIntegration(sph.MonaghanBoundaryForce.withargs(delp=dp),
                                  from_types=[Solid],
                                  on_types=[Fluid],
                                  updates=['u', 'v', 'w'],
                                  id='boundary'))

        self.solver.setup_integrator(particles)

        self.integrator = self.solver.integrator
Пример #56
0
_rho, _p

These variables should not be communicated with MPI  

"""

import pysph.base.api as base
import numpy

from mpi4py import MPI
comm = MPI.COMM_WORLD
num_procs = comm.Get_size()
pid = comm.Get_rank()

x = numpy.linspace(0, 1, 11)
pa = base.get_particle_array(name='test' + str(pid), x=x)

pa.add_property({'name': '_rho'})
pa.add_property({'name': '_p'})

if pid == 0:

    # make sure the sending data defines the underscored variables
    assert pa.properties.has_key('_rho')
    assert pa.properties.has_key('_p')

    comm.send(obj=pa, dest=1)

if pid == 1:

    pa2 = comm.recv(source=0)