-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinitialize.cpp
More file actions
208 lines (171 loc) · 6.6 KB
/
Copy pathinitialize.cpp
File metadata and controls
208 lines (171 loc) · 6.6 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "classes.h"
#include "particle.h"
#include "initialize.h"
#include "Particle_IC_Constructor.h"
//--------------------------------------------------------------------------------
// Function : Init_matrix
// Description : Initialize the matrix by different PROB_NUM
// Note :
// Input : mat : matrix
// pars : particles
// Output : if the initializaion is done or not.
//--------------------------------------------------------------------------------
bool Init_matrix( matrix &mat, particle *pars )
{
bool init_stat = false;
#if ( PROB_NUM == PROB_SINWAVE )
init_stat = Init_SinWave( mat, pars );
#elif ( PROB_NUM == PROB_TWOBODY )
init_stat = Init_TwoBody( mat, pars );
#elif ( PROB_NUM == PROB_NBODY )
init_stat = Init_NBody( mat, pars );
#else
printf("No such a problem number %d.\n", PROB_NUM);
#endif
return init_stat;
} // FUNCTION : Init_matrix( matrix &mat )
//--------------------------------------------------------------------------------
// Function : Init_SinWave
// Description : Matrix value set as f(x, y) = -2 * sin(x) * sin(y)
// Note :
// Input : mat : matrix
// Output : if the initializaion is done or not.
//--------------------------------------------------------------------------------
bool Init_SinWave( matrix &mat, particle *pars )
{
for( int idx = 0; idx < N_CELLS; idx++ )
{
#if ( N_DIMS == 2 )
const int i = idx / BOX_N;
const int j = idx % BOX_N;
const double x = BOX_DX * i;
const double y = BOX_DX * j;
mat.set_value(idx, -2. * sin(x) * sin(y));
#endif
#if ( N_DIMS == 3 )
const int dim_2 = BOX_N * BOX_N;
const int i = idx / dim_2;
const int j = (idx % dim_2) / BOX_N;
const int k = (idx % dim_2) % BOX_N;
const double x = BOX_DX*i;
const double y = BOX_DX*j;
const double z = BOX_DX*k;
mat.set_value( idx, -3.*sin(x)*sin(y)*sin(z) );
#endif
} // for( int idx = 0; idx < N_CELLS; idx++ )
return true;
} // FUNCTION : Init_SinWave
//--------------------------------------------------------------------------------
// Function : Init_TwoBody
// Description : Problem number 2
// Note :
// Input : mat : matrix
// Output : if the initializaion is done or not.
//--------------------------------------------------------------------------------
bool Init_TwoBody( matrix &mat, particle *pars )
{
if ( N_PARS != 2 )
{
printf("Number of particles need to be 2 in this simulation problem.\n");
return false;
}
mat.reset();
// particles
double *pos = new double[N_DIMS];
double *vel = new double[N_DIMS];
for ( int p = 0; p < N_PARS; p++ )
{
if (p == 0)
{
pos[0] = BOX_L/2.0 ;
pos[1] = BOX_L/2.0;
vel[0] = 0.0;
vel[1] = 0.0;
} else
{
pos[0] = BOX_L/2.0 + 1.0;
pos[1] = BOX_L/2.0;
vel[0] = 0.0;
vel[1] = pow(2*PI,-0.5);
}
#if ( N_DIMS == 3 )
pos[2] = BOX_L / 2.0;
vel[2] = 0.0;
#endif
pars[p].Par_SetMass( 1.0 );
if(p==1)pars[p].Par_SetMass(0.0);
pars[p].Par_SetPos( pos );
pars[p].Par_SetVel( vel );
pars[p].Par_AddMassToCell( mat );
} // for ( int p = 0; p < N_PARS; p++ )
delete[] pos;
delete[] vel;
return true;
} /// FUNCTION : Init_TwoBody
//--------------------------------------------------------------------------------
// Function : Init_NBody
// Description : Problem number 3
// Note :
// Input : mat : matrix
// Output : if the initializaion is done or not.
//--------------------------------------------------------------------------------
bool Init_NBody( matrix &mat, particle *pars )
{
if ( N_PARS != 10000 )
{
printf("Number of particles need to be 10000 in this simulation problem.\n");
return false;
}
mat.reset();
// Initialize Particle IC Constructor
double Newton_G = 1.0; //Gravitational Constant
double Rho0 = 1.0; // peak density [unit: M mass of sun/kpc^3]
double R0 = 1.0 ; // scale radius [unit: kpc]
double MaxR = 2.0; // maximum radius for particles [unit: kpc]
int MassProfNBin = 1000 ; // number of radial bins in the mass profile table [1000]
double Alpha =1 ; // alpha parameter for Eiasto model [1]
int r_col = 0 ; // number of the column of the radius of density profile, when model is "UNKNOWN" [0]
int rho_col =1 ; // number of the column of the density of density profile, when model is "UNKNOWN" [1]
double truncation = 0 ; // whether to turn on a smoothy truncation function of density near MaxR [0]
Particle_IC_Constructor constructor_Models;
constructor_Models.init("Plummer",Alpha,Newton_G,Rho0,R0,MassProfNBin,MaxR,truncation,0.7,r_col,rho_col,"NONE");
// particles
double *pos = new double[N_DIMS];
double *vel = new double[N_DIMS];
double d_temp = BOX_L/100.;
//compute mass
double par_m =constructor_Models.set_mass( MaxR/R0 )/N_PARS;
for ( int p = 0; p < N_PARS; p++ )
{
#if ( N_DIMS == 2 )
pos[0] = ( p/100 ) * d_temp;
pos[1] = ( p%100 ) * d_temp;
vel[0] = 0.0;
vel[1] = 0.0;
#endif
#if ( N_DIMS == 3 )
double r = constructor_Models.set_radius();
double v = constructor_Models.set_vel(r)/pow(4*PI,0.5);
//random direction
double cos_theta1 = constructor_Models.randomReal(-1,1);
double phi1 = constructor_Models.randomReal(0,2*PI);
double sin_theta1 = (1-cos_theta1*cos_theta,0.5);
double cos_theta2 = constructor_Models.randomReal(-1,1);
double phi2 = constructor_Models.randomReal(0,2*PI);
double sin_theta2 = (1-cos_theta2*cos_theta,0.5);
pos[0] = r*sin_theta1*cos(phi1)+BOX_L/2.0;
pos[1] = r*sin_theta1*sin(phi1)+BOX_L/2.0;
pos[2] = r*cos_theta1+BOX_L/2.0;
vel[0] = v*sin_theta2*cos(phi2);
vel[1] = v*sin_theta2*sin(phi2);
vel[2] = v*cos_theta2;
#endif
pars[p].Par_SetMass( par_m );
pars[p].Par_SetPos( pos );
pars[p].Par_SetVel( vel );
pars[p].Par_AddMassToCell( mat );
} // for ( int p = 0; p < N_PARS; p++ )
delete[] pos;
delete[] vel;
return true;
} /// FUNCTION : Init_NBody