Révision 164 NBody/NBodyGL.py
| NBodyGL.py (revision 164) | ||
|---|---|---|
| 247 | 247 |
|
| 248 | 248 |
// Distribute in sphere |
| 249 | 249 |
MYFLOAT radius=MWCfp*box; |
| 250 |
MYFLOAT theta=MWCfp*PI; |
|
| 251 | 250 |
MYFLOAT phi=MWCfp*PI*(MYFLOAT)2.e0f; |
| 251 |
MYFLOAT theta=acos(1.0e0f-2.e0f*MWCfp); |
|
| 252 | 252 |
// cast to float for sin,cos are NEEDED by Mesa FP64 implementation! |
| 253 | 253 |
MYFLOAT sinTheta=sin((float)theta); |
| 254 | 254 |
clDataX[gid].s0=radius*sinTheta*cos((float)phi)/2.e0f; |
| ... | ... | |
| 379 | 379 |
|
| 380 | 380 |
def display(*args): |
| 381 | 381 |
|
| 382 |
global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations |
|
| 382 |
global MyDataX,MyDataV,clDataX,clDataV,Step,Method,Number,Iterations,Durations,Verbose
|
|
| 383 | 383 |
|
| 384 | 384 |
glClearColor(0.0, 0.0, 0.0, 0.0) |
| 385 | 385 |
glClear(GL_COLOR_BUFFER_BIT) |
| ... | ... | |
| 395 | 395 |
else: |
| 396 | 396 |
CLLaunch=MyRoutines.ImplicitEuler(queue,(Number,1),None,clDataX,clDataV,Step) |
| 397 | 397 |
CLLaunch.wait() |
| 398 |
print("Duration of #%s iteration: %s" % (Iterations,(time.time()-time_start)))
|
|
| 399 |
|
|
| 398 |
Elapsed=time.time()-time_start |
|
| 399 |
Durations=np.append(Durations,Elapsed) |
|
| 400 |
if Verbose: |
|
| 401 |
print("Duration of #%s iteration: %s" % (Iterations,Elapsed))
|
|
| 402 |
else: |
|
| 403 |
sys.stdout.write('.')
|
|
| 404 |
sys.stdout.flush() |
|
| 400 | 405 |
cl.enqueue_copy(queue, MyDataX, clDataX) |
| 401 | 406 |
# print(MyDataX.reshape(Number,4)) |
| 402 | 407 |
MyDataX.reshape(Number,4)[:,3]=1 |
| ... | ... | |
| 415 | 420 |
def halt(): |
| 416 | 421 |
pass |
| 417 | 422 |
|
| 418 |
def keyboard(k, x, y): |
|
| 423 |
def keyboard(k,x,y): |
|
| 424 |
global ViewRZ,SizeOfBox |
|
| 419 | 425 |
LC_Z = as_8_bit( 'z' ) |
| 420 | 426 |
UC_Z = as_8_bit( 'Z' ) |
| 427 |
Plus = as_8_bit( '+' ) |
|
| 428 |
Minus = as_8_bit( '-' ) |
|
| 421 | 429 |
|
| 430 |
Zoom=1 |
|
| 422 | 431 |
if k == LC_Z: |
| 423 | 432 |
ViewRZ += 1.0 |
| 424 | 433 |
elif k == UC_Z: |
| 425 | 434 |
ViewRZ -= 1.0 |
| 435 |
elif k == Plus: |
|
| 436 |
Zoom *= 2.0 |
|
| 437 |
elif k == Minus: |
|
| 438 |
Zoom /= 2.0 |
|
| 426 | 439 |
elif ord(k) == 27: # Escape |
| 427 |
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION) |
|
| 428 |
glutSetOption(GLUT_ACTION_GLUTMAINLOOP_RETURNS,GLUT_ACTION_CONTINUE_EXECUTION) |
|
| 429 | 440 |
glutLeaveMainLoop() |
| 430 | 441 |
return(False) |
| 431 | 442 |
else: |
| 432 | 443 |
return |
| 433 | 444 |
glRotatef(ViewRZ, 0.0, 0.0, 1.0) |
| 445 |
glScalef(Zoom,Zoom,Zoom) |
|
| 434 | 446 |
glutPostRedisplay() |
| 435 | 447 |
|
| 436 |
def special(k, x, y):
|
|
| 448 |
def special(k,x,y):
|
|
| 437 | 449 |
global ViewRX, ViewRY, ViewRZ |
| 438 | 450 |
|
| 439 | 451 |
if k == GLUT_KEY_UP: |
| ... | ... | |
| 455 | 467 |
glMatrixMode(GL_PROJECTION) |
| 456 | 468 |
glLoadIdentity() |
| 457 | 469 |
glOrtho(-SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox, -SizeOfBox, SizeOfBox) |
| 470 |
glutPostRedisplay() |
|
| 458 | 471 |
|
| 459 | 472 |
def reshape(w, h): |
| 460 | 473 |
glViewport(0, 0, w, h) |
| ... | ... | |
| 462 | 475 |
|
| 463 | 476 |
if __name__=='__main__': |
| 464 | 477 |
|
| 465 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox |
|
| 478 |
global Number,Step,clDataX,clDataV,MyDataX,MyDataV,Method,SizeOfBox,Iterations,Verbose
|
|
| 466 | 479 |
|
| 467 | 480 |
# ValueType |
| 468 | 481 |
ValueType='FP32' |
| 469 | 482 |
class MyFloat(np.float32):pass |
| 470 | 483 |
# clType8=cl_array.vec.float8 |
| 471 |
clType4=cl_array.vec.float4 |
|
| 472 | 484 |
# Set defaults values |
| 473 | 485 |
np.set_printoptions(precision=2) |
| 474 | 486 |
# Id of Device : 1 is for first find ! |
| 475 | 487 |
Device=0 |
| 476 |
# Iterations is integer
|
|
| 488 |
# Number of bodies is integer
|
|
| 477 | 489 |
Number=2 |
| 478 | 490 |
# Size of box |
| 479 |
SizeOfBox=MyFloat(1.)
|
|
| 491 |
SizeOfBall=MyFloat(1.)
|
|
| 480 | 492 |
# Initial velocity of particules |
| 481 | 493 |
Velocity=MyFloat(1.) |
| 482 |
# Redo the last process |
|
| 483 |
Iterations=int(np.pi*1024) |
|
| 484 | 494 |
# Step |
| 485 | 495 |
Step=MyFloat(1./256) |
| 486 | 496 |
# Method of integration |
| ... | ... | |
| 491 | 501 |
RNG='MWC' |
| 492 | 502 |
# Viriel Distribution of stress |
| 493 | 503 |
VirielStress=True |
| 504 |
# Verbose |
|
| 505 |
Verbose=False |
|
| 494 | 506 |
|
| 495 | 507 |
HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBall> -v <Velocity> -s <Step> -m <ImplicitEuler|RungeKutta|ExplicitEuler|Heun> -t <FP32|FP64>' |
| 496 | 508 |
|
| ... | ... | |
| 533 | 545 |
elif opt in ("-n", "--number"):
|
| 534 | 546 |
Number=int(arg) |
| 535 | 547 |
elif opt in ("-z", "--size"):
|
| 536 |
SizeOfBox=MyFloat(arg)
|
|
| 548 |
SizeOfBall=MyFloat(arg)
|
|
| 537 | 549 |
elif opt in ("-v", "--velocity"):
|
| 538 | 550 |
Velocity=MyFloat(arg) |
| 539 | 551 |
VirielStress=False |
| ... | ... | |
| 546 | 558 |
elif opt in ("-e", "--viriel"):
|
| 547 | 559 |
VirielStress=True |
| 548 | 560 |
|
| 549 |
SizeOfBox=MyFloat(SizeOfBox*Number)
|
|
| 561 |
SizeOfBall=MyFloat(SizeOfBall*Number)
|
|
| 550 | 562 |
Velocity=MyFloat(Velocity) |
| 551 | 563 |
Step=MyFloat(Step) |
| 552 | 564 |
|
| 553 | 565 |
print("Device choosed : %s" % Device)
|
| 554 | 566 |
print("Number of particules : %s" % Number)
|
| 555 |
print("Size of Ball : %s" % SizeOfBox)
|
|
| 567 |
print("Size of Ball : %s" % SizeOfBall)
|
|
| 556 | 568 |
print("Initial velocity : %s" % Velocity)
|
| 557 | 569 |
print("Step of iteration : %s" % Step)
|
| 558 | 570 |
print("Method of resolution : %s" % Method)
|
| ... | ... | |
| 564 | 576 |
MyCoM = np.zeros(4,dtype=MyFloat) |
| 565 | 577 |
MyDataX = np.zeros(Number*4, dtype=MyFloat) |
| 566 | 578 |
MyDataV = np.zeros(Number*4, dtype=MyFloat) |
| 567 |
# MyCoM = np.zeros(1,dtype=clType4) |
|
| 568 |
# MyDataX = np.zeros(Number, dtype=clType4) |
|
| 569 |
# MyDataV = np.zeros(Number, dtype=clType4) |
|
| 570 | 579 |
MyPotential = np.zeros(Number, dtype=MyFloat) |
| 571 | 580 |
MyKinetic = np.zeros(Number, dtype=MyFloat) |
| 572 | 581 |
|
| ... | ... | |
| 596 | 605 |
except: |
| 597 | 606 |
print("Crash during context creation")
|
| 598 | 607 |
|
| 599 |
print(Marsaglia[RNG],Computing[ValueType]) |
|
| 608 |
# print(Marsaglia[RNG],Computing[ValueType])
|
|
| 600 | 609 |
# Build all routines used for the computing |
| 601 | 610 |
BuildOptions="-cl-mad-enable -cl-kernel-arg-info -cl-fast-relaxed-math -cl-std=CL1.2 -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]) |
| 602 | 611 |
|
| ... | ... | |
| 624 | 633 |
|
| 625 | 634 |
# Set particles to RNG points |
| 626 | 635 |
if InitialRandom: |
| 627 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBox,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32)))
|
|
| 636 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(nprnd(2**32)),np.uint32(nprnd(2**32)))
|
|
| 628 | 637 |
else: |
| 629 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBox,np.uint32(110271),np.uint32(250173))
|
|
| 638 |
MyRoutines.SplutterPoints(queue,(Number,1),None,clDataX,SizeOfBall,np.uint32(110271),np.uint32(250173))
|
|
| 630 | 639 |
|
| 631 | 640 |
print('All particules distributed')
|
| 632 | 641 |
|
| 633 | 642 |
CLLaunch=MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 634 | 643 |
CLLaunch.wait() |
| 635 | 644 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 636 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 645 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 637 | 646 |
|
| 638 | 647 |
if VirielStress: |
| 639 | 648 |
CLLaunch=MyRoutines.SplutterStress(queue,(Number,1),None,clDataX,clDataV,clCoM,MyFloat(0.),np.uint32(110271),np.uint32(250173)) |
| ... | ... | |
| 646 | 655 |
CLLaunch.wait() |
| 647 | 656 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 648 | 657 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 649 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 658 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 650 | 659 |
|
| 651 | 660 |
wall_time_start=time.time() |
| 652 | 661 |
|
| 653 | 662 |
# Launch OpenGL Loop |
| 654 |
global Iterations |
|
| 655 |
# (view_rotx,view_roty,view_rotz)=(0.0, 0.0, 0.0) |
|
| 663 |
global SizeOfBox,ViewRX,ViewRY,ViewRZ,Iterations,Durations |
|
| 664 |
ViewRX,ViewRY,ViewRZ = 0.,0.,0. |
|
| 665 |
SizeOfBox=SizeOfBall |
|
| 656 | 666 |
Iterations=0 |
| 667 |
Durations=np.array([],dtype=MyFloat) |
|
| 657 | 668 |
glutInit(sys.argv) |
| 658 | 669 |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) |
| 670 |
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION) |
|
| 671 |
glutSetOption(GLUT_ACTION_GLUTMAINLOOP_RETURNS,GLUT_ACTION_CONTINUE_EXECUTION) |
|
| 659 | 672 |
glutInitWindowSize(512,512) |
| 660 | 673 |
glutCreateWindow(b'NBodyGL') |
| 661 | 674 |
setup_viewport() |
| ... | ... | |
| 667 | 680 |
glutKeyboardFunc(keyboard) |
| 668 | 681 |
glutMainLoop() |
| 669 | 682 |
|
| 670 |
print("\nWall Duration on device %s for each iteration: %s\n" % (Device,(time.time()-wall_time_start)/Iterations))
|
|
| 671 |
|
|
| 672 | 683 |
MyRoutines.CenterOfMass(queue,(1,1),None,clDataX,clCoM,np.int32(Number)) |
| 673 | 684 |
CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clDataX,clPotential) |
| 674 | 685 |
CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clDataV,clKinetic) |
| ... | ... | |
| 676 | 687 |
cl.enqueue_copy(queue,MyCoM,clCoM) |
| 677 | 688 |
cl.enqueue_copy(queue,MyPotential,clPotential) |
| 678 | 689 |
cl.enqueue_copy(queue,MyKinetic,clKinetic) |
| 679 |
print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 680 |
print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 690 |
print('\nCenter Of Mass: (%s,%s,%s)' % (MyCoM[0],MyCoM[1],MyCoM[2]))
|
|
| 691 |
print('Viriel=%s Potential=%s Kinetic=%s\n'% (np.sum(MyPotential)+2.*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
|
|
| 681 | 692 |
|
| 693 |
print("\nDuration stats on device %s :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n" % (Device,np.mean(Durations),np.median(Durations),np.std(Durations),np.min(Durations),np.max(Durations)))
|
|
| 694 |
|
|
| 695 |
# Contraction of Square*Size*Hertz: Size*Size/Tile |
|
| 696 |
Squertz=np.ones(len(Durations)) |
|
| 697 |
Squertz*=Number*Number |
|
| 698 |
Squertz/=Durations |
|
| 699 |
|
|
| 700 |
print("\nSquertz stats on device %s :\n\tMean:\t%s\n\tMedian:\t%s\n\tStddev:\t%s\n\tMin:\t%s\n\tMax:\t%s\n" % (Device,np.mean(Squertz),np.median(Squertz),np.std(Squertz),np.min(Squertz),np.max(Squertz)))
|
|
| 701 |
|
|
| 682 | 702 |
clDataX.release() |
| 683 | 703 |
clDataV.release() |
| 684 | 704 |
clKinetic.release() |
Formats disponibles : Unified diff