Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Created December 22, 2014 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaityo256/3b5ae8f80d34a4cceed2 to your computer and use it in GitHub Desktop.
Save kaityo256/3b5ae8f80d34a4cceed2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
//----------------------------------------------------------------------
const int N = 20000;
const double L = 10.0;
const int D = 3;
const int X = 0;
const int Y = 1;
const int Z = 2;
double q[D][N],p[D][N];
const double dt = 0.001;
const double C2 = 0.1;
const double CUTOFF2 = L*L*0.9*0.9;
//----------------------------------------------------------------------
double
myrand(void){
return static_cast<double>(rand())/(static_cast<double>(RAND_MAX));
}
//----------------------------------------------------------------------
double
myclock(void){
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec + t.tv_usec*1e-6;
}
//----------------------------------------------------------------------
void
calcforce(void){
for(int i=0;i<N-1;i++){
for(int j=i+1;j<N;j++){
double dx = q[X][j] - q[X][i];
double dy = q[Y][j] - q[Y][i];
double dz = q[Z][j] - q[Z][i];
double r2 = (dx*dx + dy*dy + dz*dz);
double r6 = r2*r2*r2;
double df = ((24.0*r6-48.0)/(r6*r6*r2)+C2*8.0)*dt;
p[X][i] += df*dx;
p[Y][i] += df*dy;
p[Z][i] += df*dz;
p[X][j] -= df*dx;
p[Y][j] -= df*dy;
p[Z][j] -= df*dz;
}
}
}
//----------------------------------------------------------------------
void
measure(void(*pfunc)(),const char *name){
double st = myclock();
pfunc();
double t = myclock()-st;
printf("N=%d, %s %f [sec]\n",N,name,t);
}
//----------------------------------------------------------------------
int
main(void){
for(int i=0;i<N;i++){
p[X][i] = 0.0;
p[Y][i] = 0.0;
p[Z][i] = 0.0;
q[X][i] = L*myrand();
q[Y][i] = L*myrand();
q[Z][i] = L*myrand();
}
measure(&calcforce,"calcforce");
}
//----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment