Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <algorithm>
//----------------------------------------------------------------------
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;
struct point{
double x,y,z;
};
//double q[N][D],p[N][D];
struct point q[N],p[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[j].x - q[i].x;
double dy = q[j].y - q[i].y;
double dz = q[j].z - q[i].z;
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[i].x += df*dx;
p[i].y += df*dy;
p[i].z += df*dz;
p[j].x -= df*dx;
p[j].y -= df*dy;
p[j].z -= 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[i].x = 0.0;
p[i].y = 0.0;
p[i].z = 0.0;
q[i].x = L*myrand();
q[i].y = L*myrand();
q[i].z = L*myrand();
}
measure(&calcforce,"calcforce");
}
//----------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment