Index: NBody.py
===================================================================
--- NBody.py	(revision 141)
+++ NBody.py	(revision 142)
@@ -18,7 +18,6 @@
     Computing={'FP32':0,'FP64':1}
     return(Marsaglia,Computing)
 
-BlobOpenCL= """
 
 
 
@@ -28,6 +27,8 @@
 
 
 
+
+BlobOpenCL= """
 #define znew  ((z=36969*(z&65535)+(z>>16))<<16)
 #define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
 #define MWC   (znew+wnew)
@@ -90,13 +91,13 @@
     }
 
     barrier(CLK_GLOBAL_MEM_FENCE);
-    // return((MYFLOAT)5e-1f*potential);
+    //return(5e-1f*potential);
     return(potential);
 }
 
-MYFLOAT AtomicPotentialCoM(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,int gid)
+MYFLOAT AtomicPotentialCoM(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,int gid)
 {
-    return(PairPotential(clData[gid].lo,clCoM[0].lo));
+    return(PairPotential(clData[gid].lo,clCoM[0]));
 }
 
 MYFLOAT8 AtomicRungeKutta(__global MYFLOAT8* clDataIn,int gid,MYFLOAT dt)
@@ -239,25 +240,23 @@
     clData[gid].s01234567 = (MYFLOAT8) (x0,y0,z0,0e0f,0e0f,0e0f,0e0f,0e0f);
 }
 
-__kernel void SplutterStress(__global MYFLOAT8* clData, __global MYFLOAT8* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
+__kernel void SplutterStress(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM, MYFLOAT velocity,uint seed_z,uint seed_w)
 {
     int gid = get_global_id(0);
-    int N = get_global_size(0);
+    MYFLOAT N = (MYFLOAT)get_global_size(0);
     uint z=seed_z+(uint)gid;
     uint w=seed_w-(uint)gid;
 
-    MYFLOAT theta=MWCfp*PI;
-    MYFLOAT phi=MWCfp*PI*(MYFLOAT)2e0f;
-    MYFLOAT sinTheta=sin(theta);
-
     if (velocity<SMALL_NUM) {
-       MYFLOAT4 axis=(MYFLOAT4) (1e0f,1e0f,1e0f,0e0f);
-       MYFLOAT4 SpeedVector=sqrt(-AtomicPotentialCoM(clData,clCoM,gid))/(MYFLOAT)N*(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0].lo));
-       // MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0].lo))*sqrt(-AtomicPotentialCoM(clData,clCoM,gid))/sqrt(32e0f);
+       MYFLOAT4 SpeedVector=(MYFLOAT4)normalize(cross(clData[gid].lo,clCoM[0]))*sqrt(-AtomicPotential(clData,gid)*5e-1f);
        clData[gid].hi=SpeedVector;
     }
     else
     {    
+       MYFLOAT theta=MWCfp*PI;
+       MYFLOAT phi=MWCfp*PI*(MYFLOAT)2e0f;
+       MYFLOAT sinTheta=sin(theta);
+
        clData[gid].s4=velocity*sinTheta*cos(phi);
        clData[gid].s5=velocity*sinTheta*sin(phi);
        clData[gid].s6=velocity*cos(theta);
@@ -300,14 +299,14 @@
     clData[gid]=clDataGid;
 }
 
-__kernel void Potential(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,__global MYFLOAT* clPotential)
+__kernel void CoMPotential(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,__global MYFLOAT* clPotential)
 {
     int gid = get_global_id(0);
 
-    clPotential[gid]=PairPotential(clData[gid].lo,clCoM[0].lo);
+    clPotential[gid]=PairPotential(clData[gid].lo,clCoM[0]);
 }
 
-__kernel void PotentialOld(__global MYFLOAT8* clData,__global MYFLOAT* clPotential)
+__kernel void Potential(__global MYFLOAT8* clData,__global MYFLOAT* clPotential)
 {
     int gid = get_global_id(0);
 
@@ -324,17 +323,17 @@
     clPotential[gid]=(MYFLOAT)5e-1f*potential;
 }
 
-__kernel void CenterOfMass(__global MYFLOAT8* clData,__global MYFLOAT8* clCoM,int Size)
+__kernel void CenterOfMass(__global MYFLOAT8* clData,__global MYFLOAT4* clCoM,int Size)
 {
     MYFLOAT4 CoM=clData[0].lo; 
-    
+
     for (int i=1;i<Size;i++)
     {
         CoM+=clData[i].lo;
     }
-                 
+
     barrier(CLK_GLOBAL_MEM_FENCE);
-    clCoM[0]=(MYFLOAT8)(CoM.s0,CoM.s1,CoM.s2,0e0f,0e0f,0e0f,0e0f,0e0f)/(MYFLOAT)Size;
+    clCoM[0]=(MYFLOAT4)(CoM.s0,CoM.s1,CoM.s2,0e0f)/(MYFLOAT)Size;
 }
 
 __kernel void Kinetic(__global MYFLOAT8* clData,__global MYFLOAT* clKinetic)
@@ -342,7 +341,7 @@
     int gid = get_global_id(0);
     
     barrier(CLK_GLOBAL_MEM_FENCE);
-    clKinetic[gid]=(MYFLOAT)5e-1f*(MYFLOAT)pow(distance(clData[gid].hi,(MYFLOAT4)(0e0f,0e0f,0e0f,0e0f)),2);
+    clKinetic[gid]=(MYFLOAT)5e-1f*(MYFLOAT)pow(length(clData[gid].hi),2);
 }
 """
 
@@ -354,7 +353,8 @@
     # ValueType
     ValueType='FP32'
     class MyFloat(np.float32):pass
-    clType=cl_array.vec.float8
+    clType8=cl_array.vec.float8
+    clType4=cl_array.vec.float4
     # Set defaults values
     np.set_printoptions(precision=2)  
     # Id of Device : 1 is for first find !
@@ -382,7 +382,7 @@
     # Viriel Distribution of stress
     VirielStress=True
     
-    HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -g [GraphSamples] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|ImplicitEuler|ExplicitEuler> -t <FP32|FP64>'
+    HowToUse='%s -h [Help] -r [InitialRandom] -e [VirielStress] -g [GraphSamples] -c [CheckEnergies] -d <DeviceId> -n <NumberOfParticules> -z <SizeOfBox> -v <Velocity> -s <Step> -i <Iterations> -m <RungeKutta|ImplicitEuler|ExplicitEuler|Heun> -t <FP32|FP64>'
 
     try:
         opts, args = getopt.getopt(sys.argv[1:],"rehgcd:n:z:v:i:s:m:t:",["random","viriel","graph","check","device=","number=","size=","velocity=","iterations=","step=","method=","valuetype="])
@@ -411,10 +411,12 @@
         elif opt in ("-t", "--valuetype"):
             if arg=='FP64':
                 class MyFloat(np.float64): pass
-                clType=cl_array.vec.double8
+                clType8=cl_array.vec.double8
+                clType4=cl_array.vec.double4
             else:
                 class MyFloat(np.float32):pass
-                clType=cl_array.vec.float8
+                clType8=cl_array.vec.float8
+                clType4=cl_array.vec.float4
             ValueType = arg
         elif opt in ("-d", "--device"):
             Device=int(arg)
@@ -458,8 +460,8 @@
     print("Viriel distribution of stress %s" % VirielStress)
 
     # Create Numpy array of CL vector with 8 FP32    
-    MyCoM = np.zeros(1, dtype=clType)
-    MyData = np.zeros(Number, dtype=clType)
+    MyCoM = np.zeros(1,dtype=clType4)
+    MyData = np.zeros(Number, dtype=clType8)
     MyPotential = np.zeros(Number, dtype=MyFloat)
     MyKinetic = np.zeros(Number, dtype=MyFloat)
 
@@ -493,14 +495,6 @@
 #    MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = "-cl-mad-enable -cl-fast-relaxed-math -DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]))
     MyRoutines = cl.Program(ctx, BlobOpenCL).build(options = "-DTRNG=%i -DTYPE=%i" % (Marsaglia[RNG],Computing[ValueType]))
 
-# Initial forced values for exploration
-#    MyData[0][0]=np.float32(-1.)
-#    MyData[0][1]=np.float32(0.)
-#    MyData[0][5]=np.float32(1.)
-#    MyData[1][0]=np.float32(1.)
-#    MyData[1][1]=np.float32(0.)
-#    MyData[1][5]=np.float32(-1.)
-
     mf = cl.mem_flags
     clData = cl.Buffer(ctx, mf.READ_WRITE, MyData.nbytes)
     clPotential = cl.Buffer(ctx, mf.READ_WRITE, MyPotential.nbytes)
@@ -521,8 +515,8 @@
     print('All particules distributed')
 
     MyRoutines.CenterOfMass(queue,(1,1),None,clData,clCoM,np.int32(Number))
-    cl.enqueue_copy(queue, MyCoM, clCoM)
-    print('Center Of Mass:',MyCoM)
+    cl.enqueue_copy(queue,MyCoM,clCoM)
+    print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0][0],MyCoM[0][1],MyCoM[0][2]))
     
     if VirielStress:
         MyRoutines.SplutterStress(queue,(Number,1),None,clData,clCoM,np.float32(0.),np.uint32(110271),np.uint32(250173))
@@ -541,13 +535,12 @@
         #print(t0,t1,tL)
         #print(s0,s1,sL)
         
-    #CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential)
-    CLLaunch=MyRoutines.PotentialOld(queue,(Number,1),None,clData,clPotential)
+    CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential)
     CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic)
     CLLaunch.wait()
     cl.enqueue_copy(queue,MyPotential,clPotential)
     cl.enqueue_copy(queue,MyKinetic,clKinetic)
-    print(np.sum(MyPotential)/2+2*np.sum(MyKinetic),np.sum(MyPotential)/2,np.sum(MyKinetic))
+    print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
  
     if GraphSamples:
         cl.enqueue_copy(queue, MyData, clData)
@@ -567,7 +560,7 @@
             CLLaunch=MyRoutines.RungeKutta(queue,(Number,1),None,clData,Step)
         CLLaunch.wait()
         if CheckEnergies:
-            CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential)
+            CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential)
             CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic)
             CLLaunch.wait()
             cl.enqueue_copy(queue,MyPotential,clPotential)
@@ -583,13 +576,15 @@
             tL=np.append(tL,[MyData[-1][0],MyData[-1][1],MyData[-1][2]])
     print("\nDuration on %s for each %s" % (Device,(time.time()-time_start)/Iterations))
 
-    #CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clCoM,clPotential)
-    CLLaunch=MyRoutines.PotentialOld(queue,(Number,1),None,clData,clPotential)
+    CLLaunch=MyRoutines.Potential(queue,(Number,1),None,clData,clPotential)
     CLLaunch=MyRoutines.Kinetic(queue,(Number,1),None,clData,clKinetic)
     CLLaunch.wait()
     cl.enqueue_copy(queue,MyPotential,clPotential)
     cl.enqueue_copy(queue,MyKinetic,clKinetic)
-    print(np.sum(MyPotential)/2+2*np.sum(MyKinetic),np.sum(MyPotential)/2,np.sum(MyKinetic))
+    print('Viriel=%s Potential=%s Kinetic=%s'% (np.sum(MyPotential)+2*np.sum(MyKinetic),np.sum(MyPotential),np.sum(MyKinetic)))
+    MyRoutines.CenterOfMass(queue,(1,1),None,clData,clCoM,np.int32(Number))
+    cl.enqueue_copy(queue,MyCoM,clCoM)
+    print('Center Of Mass: (%s,%s,%s)' % (MyCoM[0][0],MyCoM[0][1],MyCoM[0][2]))
     
     if GraphSamples:    
         t0=np.transpose(np.reshape(t0,(Iterations+1,3)))
