Révision 133 NBody/NBody.py

NBody.py (revision 133)
54 54
    return((n-m)/(MYFLOAT)pow(distance(n,m),2));
55 55
}
56 56

  
57
MYFLOAT PairPotential(MYFLOAT4 m,MYFLOAT4 n)
58
{
59
    return((MYFLOAT)-1./distance(n,m));
60
}
61

  
57 62
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html
58 63

  
64

  
59 65
MYFLOAT8 AtomicRungeKutta(__global MYFLOAT8* clDataIn,int gid,MYFLOAT dt)
60 66
{
61
    MYFLOAT4 x0=clDataIn[gid].lo;
62
    MYFLOAT4 v0=clDataIn[gid].hi;
63
    MYFLOAT4 a0=(0.,0.,0.,0.);
64
    for (int i=0;i<get_global_size(0);i++)
67
    MYFLOAT4 x0=(MYFLOAT4)clDataIn[gid].lo;
68
    MYFLOAT4 v0=(MYFLOAT4)clDataIn[gid].hi;
69
    MYFLOAT4 a0=(MYFLOAT4)(0.,0.,0.,0.);
70
    int N = get_global_size(0);    
71
    
72
    for (int i=0;i<N;i++)
65 73
    {
66 74
        if (gid != i)
67 75
        a0+=Interaction(x0,clDataIn[i].lo);
......
69 77
        
70 78
    MYFLOAT4 x1=x0+v0*(MYFLOAT)0.5*dt;
71 79
    MYFLOAT4 v1=v0+a0*(MYFLOAT)0.5*dt;
72
    MYFLOAT4 a1=(0.,0.,0.,0.);
73
    for (int i=0;i<get_global_size(0);i++)
80
    MYFLOAT4 a1=(MYFLOAT4)(0.,0.,0.,0.);
81
    for (int i=0;i<N;i++)
74 82
    {
75 83
        if (gid != i)
76 84
        a1+=Interaction(x1,clDataIn[i].lo);
......
78 86

  
79 87
    MYFLOAT4 x2=x0+v1*(MYFLOAT)0.5*dt;
80 88
    MYFLOAT4 v2=v0+a1*(MYFLOAT)0.5*dt;
81
    MYFLOAT4 a2=(0.,0.,0.,0.);
82
    for (int i=0;i<get_global_size(0);i++)
89
    MYFLOAT4 a2=(MYFLOAT4)(0.,0.,0.,0.);
90
    for (int i=0;i<N;i++)
83 91
    {
84 92
        if (gid != i)
85 93
        a2+=Interaction(x2,clDataIn[i].lo);
......
87 95
    
88 96
    MYFLOAT4 x3=x0+v2*dt;
89 97
    MYFLOAT4 v3=v0+a2*dt;
90
    MYFLOAT4 a3=(0.,0.,0.,0.);
91
    for (int i=0;i<get_global_size(0);i++)
98
    MYFLOAT4 a3=(MYFLOAT)(0.,0.,0.,0.);
99
    for (int i=0;i<N;i++)
92 100
    {
93 101
        if (gid != i)
94 102
        a3+=Interaction(x3,clDataIn[i].lo);
......
105 113
MYFLOAT8 AtomicRungeKutta2(__global MYFLOAT8* clDataIn,int gid,MYFLOAT dt)
106 114
{
107 115
    MYFLOAT4 x[4],v[4],a[4],xf,vf;
116
    int N=get_global_size(0);
108 117

  
109 118
    x[0]=clDataIn[gid].lo;
110 119
    v[0]=clDataIn[gid].hi;
111 120
    a[0]=(0.,0.,0.,0.);
112
    for (int i=0;i<get_global_size(0);i++)
121
    
122
    for (int i=0;i<N;i++)
113 123
    {
114 124
        if (gid != i)
115 125
        a[0]+=Interaction(x[0],clDataIn[i].lo);
......
118 128
    x[1]=x[0]+v[0]*(MYFLOAT)0.5*dt;
119 129
    v[1]=v[0]+a[0]*(MYFLOAT)0.5*dt;
120 130
    a[1]=(0.,0.,0.,0.);
121
    for (int i=0;i<get_global_size(0);i++)
131
    for (int i=0;i<N;i++)
122 132
    {
123 133
        if (gid != i)
124 134
        a[1]+=Interaction(x[1],clDataIn[i].lo);
......
127 137
    x[2]=x[0]+v[1]*(MYFLOAT)0.5*dt;
128 138
    v[2]=v[0]+a[1]*(MYFLOAT)0.5*dt;
129 139
    a[2]=(0.,0.,0.,0.);
130
    for (int i=0;i<get_global_size(0);i++)
140
    for (int i=0;i<N;i++)
131 141
    {
132 142
        if (gid != i)
133 143
        a[2]+=Interaction(x[2],clDataIn[i].lo);
......
136 146
    x[3]=x[0]+v[2]*dt;
137 147
    v[3]=v[0]+a[2]*dt;
138 148
    a[3]=(0.,0.,0.,0.);
139
    for (int i=0;i<get_global_size(0);i++)
149
    for (int i=0;i<N;i++)
140 150
    {
141 151
        if (gid != i)
142 152
        a[3]+=Interaction(x[3],clDataIn[i].lo);
......
160 170
        if (gid != i)
161 171
        a+=Interaction(x,clDataIn[i].lo);
162 172
    }
163
            
164
    xf=x+dt*v;
173
       
165 174
    vf=v+dt*a;
175
    xf=x+dt*vf;
166 176
 
167 177
    return((MYFLOAT8)(xf.s0,xf.s1,xf.s2,xf.s3,vf.s0,vf.s1,vf.s2,vf.s3));
168 178
}
......
171 181
                               uint seed_z,uint seed_w)
172 182
{
173 183
    int gid = get_global_id(0);
184
    MYFLOAT N = (MYFLOAT) get_global_size(0);
174 185
    uint z=seed_z+(uint)gid;
175 186
    uint w=seed_w-(uint)gid;
187
    
176 188
    MYFLOAT theta=MWCfp*PI;
177
    MYFLOAT phi=MWCfp*PI*2.;
189
    MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.;
178 190
    MYFLOAT sinTheta=sin(theta);
179
    clData[gid].s01234567 = (MYFLOAT8) (box*MWCfp,box*MWCfp,box*MWCfp,0.,0.,0.,0.,0.);
180
    clData[gid].s4=velocity*sinTheta*cos(phi);
181
    clData[gid].s5=velocity*sinTheta*sin(phi);
182
    clData[gid].s6=velocity*cos(theta);
191
    clData[gid].s01234567 = (MYFLOAT8) (box*(MYFLOAT)(MWCfp-0.5),box*(MYFLOAT)(MWCfp-0.5),box*(MYFLOAT)(MWCfp-0.5),0.,0.,0.,0.,0.);
192
    MYFLOAT v=sqrt(N*(MYFLOAT)2./distance(clData[gid].lo,(MYFLOAT4) (0.,0.,0.,0.)));
193
    clData[gid].s4=v*sinTheta*cos(phi);
194
    clData[gid].s5=v*sinTheta*sin(phi);
195
    clData[gid].s6=v*cos(theta);
183 196
}
184 197

  
185 198
__kernel void RungeKutta(__global MYFLOAT8* clData,MYFLOAT h)
......
199 212
    barrier(CLK_GLOBAL_MEM_FENCE);
200 213
    clData[gid]=clDataGid;
201 214
}
215

  
216
__kernel void Potential(__global MYFLOAT8* clData,__global MYFLOAT* clPotential)
217
{
218
    int gid = get_global_id(0);
219

  
220
    MYFLOAT potential=0.;
221
    MYFLOAT4 x=clData[gid].lo; 
222
    
223
    for (int i=0;i<get_global_size(0);i++)
224
    {
225
        if (gid != i)
226
        potential+=PairPotential(x,clData[i].lo);
227
    }
228
                 
229
    barrier(CLK_GLOBAL_MEM_FENCE);
230
    clPotential[gid]=(MYFLOAT)0.5*potential;
231
}
232

  
233
__kernel void Kinetic(__global MYFLOAT8* clData,__global MYFLOAT* clKinetic)
234
{
235
    int gid = get_global_id(0);
236
    
237
    clKinetic[gid]=(MYFLOAT)0.5*(pow(clData[gid].s4,2)+pow(clData[gid].s5,2)+pow(clData[gid].s6,2));
238
}
202 239
"""
203 240

  
241
def Energy(MyData):
242
    return(sum(pow(MyData,2)))
243

  
204 244
if __name__=='__main__':
205 245
    
206 246
    # ValueType
......
212 252
    # Id of Device : 1 is for first find !
213 253
    Device=1
214 254
    # Iterations is integer
215
    Number=1024
255
    Number=4
216 256
    # Size of box
217 257
    SizeOfBox=MyFloat(1.)
218 258
    # Initial velocity of particules
219 259
    Velocity=MyFloat(1.)
220 260
    # Redo the last process
221
    Iterations=1000
261
    Iterations=100
222 262
    # Step
223
    Step=MyFloat(0.1)
263
    Step=MyFloat(0.01)
224 264
    # Method of integration
225 265
    Method='RungeKutta'
226 266
    # InitialRandom
227 267
    InitialRandom=False
228 268
    # RNG Marsaglia Method
229 269
    RNG='MWC'
270
    # CheckEnergies
271
    CheckEnergies=False
230 272
    
231
    HowToUse='%s -h [Help] -r [InitialRandom] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|Euler> -t <FP32|FP64>'
273
    HowToUse='%s -h [Help] -r [InitialRandom] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|Euler> -t <FP32|FP64>'
232 274

  
233 275
    try:
234
        opts, args = getopt.getopt(sys.argv[1:],"rhd:n:z:v:i:s:m:t:",["random","device=","number=","size=","velocity=","iterations=","step=","method=","valuetype="])
276
        opts, args = getopt.getopt(sys.argv[1:],"rhcd:n:z:v:i:s:m:t:",["random","check","device=","number=","size=","velocity=","iterations=","step=","method=","valuetype="])
235 277
    except getopt.GetoptError:
236 278
        print(HowToUse % sys.argv[0])
237 279
        sys.exit(2)
......
277 319
            Iterations=int(arg)
278 320
        elif opt in ("-r", "--random"):
279 321
            InitialRandom=True
280

  
322
        elif opt in ("-c", "--check"):
323
            CheckEnergies=True
324
            
281 325
    SizeOfBox=MyFloat(SizeOfBox)
282 326
    Velocity=MyFloat(Velocity)
283 327
    Step=MyFloat(Step)
......
289 333
    print("Number of iterations % s" % Iterations)
290 334
    print("Step of iteration % s" % Step)
291 335
    print("Method of resolution % s" % Method)
292
    print("Seed for RNG % s" % InitialRandom)
336
    print("Initial Random for RNG Seed % s" % InitialRandom)
337
    print("Check for Energie % s" % CheckEnergies)
293 338
    print("ValueType is % s" % ValueType)
294 339

  
295 340
    # Create Numpy array of CL vector with 8 FP32    
296 341
    MyData = np.zeros(Number, dtype=clType)
342
    MyPotential = np.zeros(Number, dtype=MyFloat)
343
    MyKinetic = np.zeros(Number, dtype=MyFloat)
297 344

  
298 345
    Marsaglia,Computing=DictionariesAPI()
299 346

  
......
334 381

  
335 382
    mf = cl.mem_flags
336 383
    clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
384
    clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes)
385
    clKinetic = cl.Buffer(ctx, mf.READ_WRITE, MyKinetic.nbytes)
337 386
    #clData = cl.Buffer(ctx, mf.WRITE_ONLY|mf.COPY_HOST_PTR,hostbuf=MyData)
338 387

  
339 388
    print('At this points, all particles are at the same place.')
......
348 397

  
349 398
    print('All particules distributed')
350 399
 
400
    CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential)
401
    CLLaunch.wait()
402
    if CheckEnergies:
403
        cl.enqueue_copy(queue,MyPotential,clPotential)
404
        CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic)
405
        CLLaunch.wait()
406
        cl.enqueue_copy(queue,MyKinetic,clKinetic)
407
        # print(np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic),MyPotential,MyKinetic)
408
        print(np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))
409
 
351 410
    cl.enqueue_copy(queue, MyData, clData)
352 411
    t0=np.array([[MyData[0][0],MyData[0][1],MyData[0][2]]])
353 412
    t1=np.array([[MyData[1][0],MyData[1][1],MyData[1][2]]])
......
361 420
            CLLaunch=MyRoutines.Euler(queue,(Number,1),None,clData,Step)
362 421
        CLLaunch.wait()
363 422
        cl.enqueue_copy(queue, MyData, clData)
423
        if CheckEnergies:
424
            CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential)
425
            CLLaunch.wait()
426
            cl.enqueue_copy(queue,MyPotential,clPotential)
427
            CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic)
428
            CLLaunch.wait()
429
            cl.enqueue_copy(queue,MyKinetic,clKinetic)
430
            # print(np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic),MyPotential,MyKinetic)
431
            print(np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic))
432

  
364 433
        t0=np.append(t0,[MyData[0][0],MyData[0][1],MyData[0][2]])
365 434
        t1=np.append(t1,[MyData[1][0],MyData[1][1],MyData[1][2]])
366 435
        tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]])
......
370 439
    # t1=np.transpose(np.reshape(t1,(Iterations+1,3)))
371 440
    # tL=np.transpose(np.reshape(tL,(Iterations+1,3)))
372 441
    
373
    # import matplotlib.pyplot as plt
374
    # from mpl_toolkits.mplot3d import Axes3D
442
#    import matplotlib.pyplot as plt
443
#    from mpl_toolkits.mplot3d import Axes3D
444
#    
445
#    fig = plt.figure()
446
#    ax = fig.gca(projection='3d')
447
#    ax.scatter(t0[0],t0[1],t0[2], marker='^',color='blue')
448
#    ax.scatter(t1[0],t1[1],t1[2], marker='o',color='red')
449
#    ax.scatter(tL[0],tL[1],tL[2], marker='D',color='green')
450
#   
451
#    ax.set_xlabel('X Label')
452
#    ax.set_ylabel('Y Label')
453
#    ax.set_zlabel('Z Label')
454
#
455
#    plt.show()
375 456
    
376
    # fig = plt.figure()
377
    # ax = fig.gca(projection='3d')
378
    # ax.scatter(t0[0],t0[1],t0[2], marker='^',color='blue')
379
    # ax.scatter(t1[0],t1[1],t1[2], marker='o',color='red')
380
    # ax.scatter(tL[0],tL[1],tL[2], marker='D',color='green')
381
    
382
    # ax.set_xlabel('X Label')
383
    # ax.set_ylabel('Y Label')
384
    # ax.set_zlabel('Z Label')
385

  
386
    # plt.show()
387
    
388 457
    clData.release()
458
    clKinetic.release()
459
    clPotential.release()

Formats disponibles : Unified diff