-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbody.cc
131 lines (111 loc) · 3.24 KB
/
nbody.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <getopt.h>
#include "Integrator.hh"
#include "ForceNaive.hh"
#include "ForceBarnesHut.hh"
using namespace std;
class CSVOutput : public OnStepComplete{
private:
ostream& fd_;
bool isempty_;
int count;
const int every;
public:
CSVOutput(ostream& fd, int every) : fd_(fd), isempty_(true), count(0), every(every){}
virtual void operator() (UniverseState *state){
if(isempty_){
fd_<<"time_idx,body_idx,t,x,y,vx,vy,m"<<endl;
isempty_ = false;
}
if (count % every ==0)
writeToCSV(fd_, *state, count / every);
//cerr<<count<<endl;
count++;
}
virtual ~CSVOutput(){}
};
UniverseState* loadState(const char *filename){
ifstream is;
is.open(filename);
if(is.fail()){
cerr<<"Opening "<<filename<<" failed."<<endl;
exit(EXIT_FAILURE);
}
return UniverseState::createFromFile(is);
};
class MyForceCalculatorFactory : public ForceCalculatorFactory{
public:
MyForceCalculatorFactory() : use_barneshut_(false), theta_(0.0) {}
MyForceCalculatorFactory(double theta) : use_barneshut_(true), theta_(theta){}
virtual ForceCalculator* operator()(Body *bodies, int N){
if(use_barneshut_)
return new ForceBarnesHut(bodies, N, theta_);
else
return new ForceNaive(bodies, N);
}
private:
const bool use_barneshut_;
const double theta_;
};
#define USAGE \
"usage:\n" \
" driver [options]\n" \
"options:\n" \
" -f [filename] Initial conditions (Default: initial.us)\n" \
" -n [nsteps] Number of steps (Default: 60000)\n" \
" -r [nsteps] Record once every nsteps (Default: 1000)\n" \
" -s [step_size] Earth days in step (Default: 0.003)\n" \
" -t [theta] Barnes hut distance ratio parameter (Default: 0.1)\n" \
" -b Use Barneshut rather than naive (Default: 0.1)\n" \
" -h Show this help message\n"
int main(int argc, char **argv){
const char *filename = "initial.us";
int nsteps = 60000;
int r = 1000;
double h = 0.003;
double theta = 0.1;
bool use_barneshut = false;
int option_char;
while ((option_char = getopt(argc, argv, "f:n:r:s:t:bh")) != -1) {
switch (option_char) {
case 'f':
filename = optarg;
break;
case 'n':
nsteps = atoi(optarg);
break;
case 'r':
r = atoi(optarg);
break;
case 's':
h = atof(optarg);
break;
case 't':
theta = atof(optarg);
break;
case 'b':
use_barneshut = true;
break;
case 'h':
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "%s\n", USAGE);
exit(EXIT_FAILURE);
}
}
UniverseState* state = loadState(filename);
CSVOutput csvoutput(cout, r);
MyForceCalculatorFactory *fcfactory;
if(use_barneshut)
fcfactory = new MyForceCalculatorFactory(theta);
else
fcfactory = new MyForceCalculatorFactory();
Integrator integrator(state, &csvoutput, fcfactory);
integrator.run(h, nsteps);
delete fcfactory;
delete state;
}