Révision 120 NBody/NBody.py
| NBody.py (revision 120) | ||
|---|---|---|
| 38 | 38 |
return((n-m)/(float)pow(distance(n,m),2)); |
| 39 | 39 |
} |
| 40 | 40 |
|
| 41 |
// Elements from : http://doswa.com/2009/01/02/fourth-order-runge-kutta-numerical-integration.html |
|
| 42 |
|
|
| 41 | 43 |
float8 RungeKutta(__global float8* clDataIn,int gid,float dt) |
| 42 | 44 |
{
|
| 43 | 45 |
float4 x[4],v[4],a[4],xf,vf; |
| ... | ... | |
| 141 | 143 |
# Set defaults values |
| 142 | 144 |
np.set_printoptions(precision=2) |
| 143 | 145 |
# Id of Device : 1 is for first find ! |
| 144 |
Device=2
|
|
| 146 |
Device=1
|
|
| 145 | 147 |
# Iterations is integer |
| 146 |
Number=10 |
|
| 148 |
Number=1024
|
|
| 147 | 149 |
# Size of box |
| 148 | 150 |
SizeOfBox=1. |
| 149 | 151 |
# Initial velocity of particules |
| 150 | 152 |
Velocity=1. |
| 151 | 153 |
# Redo the last process |
| 152 |
Redo=10
|
|
| 154 |
Iterations=1000
|
|
| 153 | 155 |
# Step |
| 154 | 156 |
Step=0.1 |
| 155 | 157 |
|
| 156 |
HowToUse='%s -d <DeviceId> -n <NumberOfSegments> -z <SizeOfBox> -v <Velocity> -s <Step>'
|
|
| 158 |
HowToUse='%s -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations>'
|
|
| 157 | 159 |
|
| 158 | 160 |
try: |
| 159 |
opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:r:s:",["device=","number=","size=","velocity=","redo=","step="])
|
|
| 161 |
opts, args = getopt.getopt(sys.argv[1:],"hd:n:z:v:i:s:",["device=","number=","size=","velocity=","iterations=","step="])
|
|
| 160 | 162 |
except getopt.GetoptError: |
| 161 | 163 |
print HowToUse % sys.argv[0] |
| 162 | 164 |
sys.exit(2) |
| ... | ... | |
| 188 | 190 |
Velocity=np.float32(arg) |
| 189 | 191 |
elif opt in ("-s", "--step"):
|
| 190 | 192 |
Step=np.float32(arg) |
| 191 |
elif opt in ("-r", "--redo"):
|
|
| 192 |
Redo=int(arg)
|
|
| 193 |
elif opt in ("-i", "--iterations"):
|
|
| 194 |
Iterations=int(arg)
|
|
| 193 | 195 |
|
| 194 | 196 |
print "Device choosed : %s" % Device |
| 195 |
print "Number of segments : %s" % Number
|
|
| 197 |
print "Number of particules : %s" % Number
|
|
| 196 | 198 |
print "Size of Box : %s" % SizeOfBox |
| 197 | 199 |
print "Initial velocity % s" % Velocity |
| 198 |
print "Redo the last process % s" % Redo
|
|
| 200 |
print "Number of iterations % s" % Iterations
|
|
| 199 | 201 |
print "Step of iteration % s" % Step |
| 200 | 202 |
|
| 201 | 203 |
MyData = np.zeros(Number, dtype=cl_array.vec.float8) |
| ... | ... | |
| 251 | 253 |
tL=np.array([[MyData[-1][0],MyData[-1][1],MyData[-1][2]]]) |
| 252 | 254 |
|
| 253 | 255 |
time_start=time.time() |
| 254 |
for i in xrange(Redo):
|
|
| 256 |
for i in xrange(Iterations):
|
|
| 255 | 257 |
CLLaunch=MyRoutines.Evolution(queue,(Number,1),None,clDataNxt,clData,np.float32(Step)) |
| 256 | 258 |
CLLaunch.wait() |
| 257 | 259 |
cl.enqueue_copy(queue, MyData, clDataNxt) |
| 258 |
# print MyData[0] |
|
| 259 | 260 |
t0=np.append(t0,[MyData[0][0],MyData[0][1],MyData[0][2]]) |
| 260 | 261 |
t1=np.append(t1,[MyData[1][0],MyData[1][1],MyData[1][2]]) |
| 261 | 262 |
tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]]) |
| 262 | 263 |
CLLaunch=MyRoutines.Commit(queue,(Number,1),None,clDataNxt,clData) |
| 263 | 264 |
CLLaunch.wait() |
| 264 |
print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Redo)
|
|
| 265 |
print "\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations)
|
|
| 265 | 266 |
|
| 266 |
t0=np.transpose(np.reshape(t0,(Redo+1,3)))
|
|
| 267 |
t1=np.transpose(np.reshape(t1,(Redo+1,3)))
|
|
| 268 |
tL=np.transpose(np.reshape(tL,(Redo+1,3)))
|
|
| 267 |
t0=np.transpose(np.reshape(t0,(Iterations+1,3)))
|
|
| 268 |
t1=np.transpose(np.reshape(t1,(Iterations+1,3)))
|
|
| 269 |
tL=np.transpose(np.reshape(tL,(Iterations+1,3)))
|
|
| 269 | 270 |
|
| 270 | 271 |
import matplotlib.pyplot as plt |
| 271 | 272 |
from mpl_toolkits.mplot3d import Axes3D |
| 272 | 273 |
|
| 273 | 274 |
fig = plt.figure() |
| 274 | 275 |
ax = fig.gca(projection='3d') |
| 275 |
print tL[2] |
|
| 276 | 276 |
ax.scatter(t0[0],t0[1],t0[2], marker='^',color='blue') |
| 277 | 277 |
ax.scatter(t1[0],t1[1],t1[2], marker='o',color='red') |
| 278 | 278 |
ax.scatter(tL[0],tL[1],tL[2], marker='D',color='green') |
Formats disponibles : Unified diff