Exemple #1
0
 def get_ode_solver(self):
     """
     Build and return an object for solving the Newton Law of motion (derived form the abstract class ode_solver)
     """
     print("")
     
     self.ode_solver = None
     
     if self.ode_solver_name == "euler" :
         self.ode_solver = els.EulerSolver( self.force , self.pset , self.time_step )
         
         print( " setup - Integration method:  EULER "  )
            
     elif self.ode_solver_name == "runge_kutta" :
         self.ode_solver = rks.RungeKuttaSolver( self.force , self.pset , self.time_step )
         
         print( " setup - Integration method:  Runge Kutta "  )
         
     elif self.ode_solver_name == "leap_frog" :
         self.ode_solver = lps.LeapfrogSolver( self.force , self.pset , self.time_step )            
         
         print( " setup - Integration method:  Leap Frog "  )
     
     elif self.ode_solver_name == "stormer_verlet" :
         self.ode_solver = svs.StormerVerletSolver( self.force , self.pset , self.time_step )
     
         print( " setup - Integration method:  Stormer Verlet " )
         
     elif self.ode_solver_name == "midpoint" :
         self.ode_solver = mds.MidpointSolver( self.force , self.pset , self.time_step )
     
         print( " setup - Integration method:  Midpoint " )
     
     print( " setup - Integration method - time step: %e " %  self.time_step )   
     return self.ode_solver
Exemple #2
0
def gas_lj():
    """
    Gas simulation based on the Lennard Jones force
    """

    steps = 1000000
    dt = 0.001

    omicron = 0.05
    epsilon = 1.0

    rand_c = rc.RandCluster()

    pset = ps.ParticlesSet(1000)

    r_min = 2.0**(1. / 6.) * omicron

    rand_c.insert3(X=pset.X,
                   M=pset.M,
                   start_indx=0,
                   n=pset.size,
                   radius=5.0,
                   mass_rng=(0.1, 0.3),
                   r_min=r_min)

    lennard_jones = lj.LenardJones(pset.size,
                                   pset.dim,
                                   pset.M,
                                   Consts=(epsilon, omicron))

    solver = els.EulerSolver(lennard_jones, pset, dt)
    #solver = lps.LeapfrogSolver( lennard_jones , pset , dt )
    #solver = svs.StormerVerletSolver( lennard_jones , pset , dt )
    #solver = rks.RungeKuttaSolver( lennard_jones , pset , dt )
    #solver = mds.MidpointSolver( lennard_jones , pset , dt )

    bound = pb.PeriodicBoundary(bound=(-6.0, 6.0))

    pset.set_boundary(bound)

    a = aogl.AnimatedGl()

    a.ode_solver = solver
    a.pset = pset
    a.steps = steps

    a.build_animation()

    a.start()

    return
    def build_animation(self):

        self.steps = 3000

        self.pset = ps.ParticlesSet(1, 3)

        self.ip = 0

        self.pset.M[:] = 1.0
        self.pset.V[:] = 0.0

        self.t = np.zeros((self.steps))
        self.x = np.zeros((self.steps, self.pset.dim))

        self.xn = np.zeros((self.steps, self.pset.dim))

        constf = cf.ConstForce(self.pset.size,
                               u_force=[0, 0, -10.0],
                               dim=self.pset.dim)
        drag = dr.Drag(self.pset.size, Consts=1.0)

        multi = mf.MultipleForce(self.pset.size)

        multi.append_force(constf)
        multi.append_force(drag)

        multi.set_masses(self.pset.M)

        dt = 0.001

        self.odes = dict()

        self.odes["Euler      "] = els.EulerSolver(multi, self.pset, dt)
        self.odes["Runge Kutta"] = rks.RungeKuttaSolver(multi, self.pset, dt)
        self.odes["Leap Frog  "] = lps.LeapfrogSolver(multi, self.pset, dt)
        self.odes["MidPoint   "] = mps.MidpointSolver(multi, self.pset, dt)
        self.odes["Verlet     "] = svs.StormerVerletSolver(
            multi, self.pset, dt)
Exemple #4
0
def fountain():
    """
    Fountain demo
    """
    steps = 10000000
    dt = 0.005
    pcnt = 100000

    fl = True
    if test_pyopencl():
        print("OpenCL is installed and enabled ")
        print(" Try, at least, 200000 particles ")

        while fl:
            try:
                print(" ")
                pcnt = int(raw_input('How many particles: '))
            except:
                print("Please insert a number! ")
            else:
                fl = False

    pset = ps.ParticlesSet(pcnt, dtype=np.float32)

    pset.M[:] = 0.1
    pset.X[:, 2] = 0.7 * np.random.rand(pset.size)

    grav = cf.ConstForce(pset.size, dim=pset.dim, u_force=(0.0, 0.0, -10.0))

    occx = None
    if test_pyopencl():
        occx = occ.OpenCLcontext(
            pset.size, pset.dim,
            (occ.OCLC_X | occ.OCLC_V | occ.OCLC_A | occ.OCLC_M))
        drag = dr.DragOCL(pset.size,
                          dim=pset.dim,
                          Consts=0.01,
                          ocl_context=occx)
    else:
        drag = dr.Drag(pset.size, dim=pset.dim, Consts=0.01)

    multi = mf.MultipleForce(pset.size, dim=pset.dim)

    multi.append_force(grav)
    multi.append_force(drag)

    multi.set_masses(pset.M)

    #solver = mds.MidpointSolver( multi , pset , dt )
    if test_pyopencl():
        solver = els.EulerSolverOCL(multi, pset, dt, ocl_context=occx)
    else:
        solver = els.EulerSolver(multi, pset, dt)

    solver.update_force()

    default_pos.sim_time = solver.get_sim_time()

    bd = (-100.0, 100.0, -100.0, 100.0, 0.0, 100.0)
    bound = db.DefaultBoundary(bd, dim=3, defualt_pos=default_pos)

    pset.set_boundary(bound)

    a = aogl.AnimatedGl()

    a.ode_solver = solver
    a.pset = pset
    a.steps = steps

    a.draw_particles.set_draw_model(a.draw_particles.DRAW_MODEL_VECTOR)

    a.init_rotation(-80, [0.7, 0.05, 0])

    a.build_animation()
    a.start()

    return
Exemple #5
0
def my_test() :
    
    
    pset = ps.ParticlesSet( 10 )
    
    lo = log.Logger( pset , 10 )
    
    for i in range( 105 ) :
        pset.X[:] =  float(i) 
        lo.log()
        
        print( lo.get_particles_log( 3 ) )
    
    exit()
        
    t = tr.Transformations()
    
    t.set_points_tuple_size(1)
        
    t.rotate( np.radians(90) , 1 , 0 , 0 )
    #t.rotX( np.radians(90) )
    
    t.append_point( list( [1,0,0] ) )
    t.append_point( np.array( [1,1,0] ) )
    t.append_point( np.array( [1,1,1] ) )
    t.append_point( np.array( [0,1,1] ) )    
    
    t.push_matrix()
    t.identity()
    t.translation( 10 , 2 , 2 )
    #t.rotate( np.radians(20) , 1 , 1 , 1 )
    
    t.append_point( [1,1,1] )
    t.append_point( np.matrix( [0,1,1] ).T ) 
    
    t.pop_matrix()
    
    t.append_point( np.array( [1,0,0] ) )
    t.append_point( [1,1,0] )
    t.append_point( np.array( [1,1,1] ) )
    t.append_point( [0,1,1] )   
    
    #print( t.transform(pt[0] , pt[1] , pt[2] ) )
    
    print("")
    
    for (p) in t :
        print( p )
    
    exit()
    
    n = 10
    dt = 0.005
    #dt = 0.0023453
    
    steps = 1000000
    
    G = 0.001
    #G = 6.67384e-11
    
    FLOOR = -10
    CEILING = 10
    


    #ff = fc.FileCluster()
    #ff.open( options.path_name )
    
    pset = ps.ParticlesSet( n , label=True )
    
    pset.label[8] = "tttt"
    pset.label[9] = "tzzzttt"
    
    pset.add_property_by_name("ciao",dim=1 , model="list")
    
    pset.get_by_name("ciao")[3] = 100
    pset.get_by_name("X")[3,:] = 101
    
    sz = 15
    pset.resize( sz )
    
    tree = ot.OcTree()
    
    pset.get_by_name("X")[:] = np.random.rand(sz,3)
    pset.get_by_name("M")[:] = 1.0
    
    pset.update_centre_of_mass()
    
    print(" C O M pset")
    print( pset.centre_of_mass() )
    print("")
    
    csrt = ct.ConstrainedX( pset )
    
    cfit = cfi.ConstrainedForceInteractions( pset )
    
    cfit.add_connections( [[12,3],[4,4],[6,8],[1,1]] )
    cfit.remove_connections( [[12,3]] )
    
    print( cfit.dense )
    print( cfit.sparse )
    print( cfit.items )
    
    cc = np.array( [[1,2,3],[3,3,3]] )
    cc = np.array( [[1,2,3],[3,3,5]] )
    
    csrt.add_x_constraint( [2,5] , cc )
    csrt.add_x_constraint( [7,10] , cc )
    
    print( csrt.get_cx_indicies() )
    print( csrt.cX )
    
    csrt.remove_x_constraint( [2,10] )
    
    print( csrt.get_cx_indicies() )
    print( csrt.cX )
    
    exit() 
    
    tree.set_global_boundary()
    
    a = time.time()
    tree.build_tree( pset )
    b = time.time()
    
    print( "Tot time: % f" %(b-a) )
    
    C = np.array([0.5,0.4,0.3])
    R = 0.05
    
    a = time.time()
    for ix in range( pset.size ):
        nl = tree.search_neighbour( pset.X[ix,:] , R )
    b = time.time()
    
    print( "Tot time octree : % f" %(b-a) )
    
    nl = np.sort( nl )
    
    print("")
    print("nl:")
    print( nl )
    
    print("")
    print("dd:")
    
    a = time.time()
    for ix in range( pset.size ):
        dd = np.sqrt(  np.sum( (pset.X[ix,:] - pset.X)**2 , 1 ) ) 
        din, = np.where( dd <= R )
    b = time.time()
    
    print( "Tot time numpy : % f" %(b-a) )
    
    print( din )
    
    print(" C O M")
    print( tree.centre_of_mass )
    print("")
    
    print ( np.all( nl == din ) )
    
    #tree.print_tree()
    
    
    #print( pset.get_by_name( "ciao" ) )
    #print( pset.get_by_name( "X" ) )
    #print("")
    #print( pset.X )
    #print( pset.label )
    
    
    exit()
    return
    
    #ff.insert3( pset )
    #ff.close()  
        
    #pset.unit = 149597870700.0
    #pset.mass_unit = 5.9736e24
    
    
    cs = clu.RandCluster()
    
    cs.insert3( pset.X , M=pset.M , V=pset.V ,
                n = n/2 , centre=(-1.5,1,0.5) , mass_rng=(0.5,5.0) ,
                vel_rng=(0,0) , vel_mdl="bomb" )
    
    cs.insert3( pset.X , M=pset.M , V=pset.V ,
                start_indx=int(n/2) , n = int(n/2) , centre=(1.5,-0.5,0.5) ,
                vel_rng=(0.2,0.4) , vel_mdl="const" , vel_dir=[-1.0,0.0,0.0] )
    #
    
    grav = gr.Gravity( pset.size , Consts=G )
    #grav = cf.ConstForce(n , u_force=[0,0,-1.0] )
    #grav = MyField( pset.size , dim=3 )
    #grav = ls.LinearSpring( pset.size , Consts=10e8 )
    
    grav.set_masses( pset.M )
    
    
    bound = None
    #bound = pb.PeriodicBoundary( (-50.0 , 50.0) )
    #bound = rb.ReboundBoundary(  (-10.0 , 10.0)  )
    
    pset.set_boundary( bound )
    grav.update_force( pset )
    
    solver = els.EulerSolver( grav , pset , dt )
    #solver = lps.LeapfrogSolver( grav , pset , dt )
    #solver = svs.StormerVerletSolver( grav , pset , dt )
    #solver = rks.RungeKuttaSolver( grav , pset , dt )    
        
    a = aogl.AnimatedGl()
    # a = anim.AnimatedScatter()
        
    
    a.xlim = ( FLOOR , CEILING )
    a.ylim = ( FLOOR , CEILING )
    a.zlim = ( FLOOR , CEILING )
    
    a.ode_solver = solver
    a.pset = pset
    a.steps = steps
    
    a.build_animation()
    
    a.start()
    
    return