forked from CrazyAncestor/Multigrid_Poisson_Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve.cpp
198 lines (157 loc) · 5.36 KB
/
evolve.cpp
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
#include "classes.h"
#include "particle.h"
#include "solver.h"
#include "evolve.h"
//--------------------------------------------------------------------------------
// Function : Evolve_GetDt
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
double Evolve_GetDt( particle *pars, const double t_now, const int counter, bool &out_stat, bool &end_stat )
{
double min_dt = 0.01;
double out_time = counter * OUT_DT;
if ( t_now + min_dt >= END_TIME )
{
min_dt = END_TIME - t_now;
out_stat = true;
end_stat = true;
} else if ( t_now + min_dt >= out_time )
{
min_dt = out_time - t_now;
out_stat = true;
end_stat = false;
} else
{
out_stat = false;
end_stat = false;
}
return min_dt;
}
//--------------------------------------------------------------------------------
// Function : Evolve_UpdateParticle
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
void Evolve_UpdateParticle( matrix &dens, particle *pars, const double dt )
{
#if ( EVOLVE == EULER )
Evolve_Euler( dens, pars, dt );
#elif ( EVOLVE == KDK )
Evolve_KDK( dens, pars, dt );
#elif ( EVOLVE == DKD )
Evolve_DKD( dens, pars, dt );
#elif ( EVOLVE == RK4 )
Evolve_RK4( dens, pars, dt );
#endif
} // FUNCTION : Evolve_UpdateParticle
//--------------------------------------------------------------------------------
// Function : Evolve_Euler
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
void Evolve_Euler( matrix &dens, particle *pars, const double dt )
{
double **force;
force = new double *[N_DIMS];
for ( int d = 0; d < N_DIMS; d++ ) force[d] = new double [N_CELLS];
// 1. solve acc
const double dx = BOX_DX;
matrix pot( BOX_N, dx );
pot.init_potential();
matrix solved_pot = Solver_Potential( pot, dens );
Solver_Force( solved_pot, force );
// 2. update
double *vel = new double[N_DIMS];
double *acc = new double[N_DIMS];
for ( int p = 0; p < N_PARS; p++ )
{
pars[p].Par_GetVel( vel );
pars[p].Par_SumAcc( acc, force ); // get the velocity
//printf("P = %d, ax = %.5e ,ay = %.5e", p, acc[0], acc[1]);
pars[p].Par_UpdateAll( vel, acc, dt );
} // for ( int p = 0; p < N_PARS; p++ )
//printf("\n");
delete[] vel;
delete[] acc;
for ( int d = 0; d < N_DIMS; d++ ) delete[] force[d];
delete[] force;
} // FUNCTION : Evolve_Euler
//--------------------------------------------------------------------------------
// Function : Evolve_KDK
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
void Evolve_KDK( matrix &dens, particle *pars, const double dt )
{
} // FUNCTION : Evolve_KDK
//--------------------------------------------------------------------------------
// Function : Evolve_DKD
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
void Evolve_DKD( matrix &dens, particle *pars, const double dt )
{
double **force;
force = new double *[N_DIMS];
for ( int d = 0; d < N_DIMS; d++ ) force[d] = new double [N_CELLS];
double *pos = new double[N_DIMS];
double *vel = new double[N_DIMS];
double *acc = new double[N_DIMS];
const double dx = BOX_DX;
// 1. kick
for ( int p = 0; p < N_PARS; p++ )
{
pars[p].Par_GetPos( pos );
pars[p].Par_GetVel( vel );
//printf("P = %d, px = %.5e ,py = %.5e ", p, pos[0], pos[1]);
for ( int d = 0; d < N_DIMS; d++ ) pos[d] += vel[d] * 0.5 * dt;
//printf("=> px = %.5e ,py = %.5e ", pos[0], pos[1]);
pars[p].Par_SetPos( pos );
} // for ( int p = 0; p < N_PARS; p++ )
//printf("\n");
// 2. drift
matrix pot( BOX_N, dx );
pot.init_potential();
matrix solved_pot = Solver_Potential( pot, dens );
Solver_Force( solved_pot, force );
for ( int p = 0; p < N_PARS; p++ )
{
pars[p].Par_SumAcc( acc, force ); // get the velocity
pars[p].Par_GetVel( vel );
for ( int d = 0; d < N_DIMS; d++ ) vel[d] += acc[d] * dt;
pars[p].Par_SetVel( vel );
} // for ( int p = 0; p < N_PARS; p++ )
// 3. kick
for ( int p = 0; p < N_PARS; p++ )
{
pars[p].Par_GetPos( pos );
pars[p].Par_GetVel( vel );
for ( int d = 0; d < N_DIMS; d++ ) pos[d] += vel[d] * 0.5 * dt;
pars[p].Par_SetPos( pos );
} // for ( int p = 0; p < N_PARS; p++ )
delete[] vel;
delete[] acc;
for ( int d = 0; d < N_DIMS; d++ ) delete[] force[d];
delete[] force;
} // FUNCTION : Evolve_DKD
//--------------------------------------------------------------------------------
// Function : Evolve_RK4
// Description :
// Note :
// Input :
// Output :
//--------------------------------------------------------------------------------
void Evolve_RK4( matrix &dens, particle *pars, const double dt )
{
} // FUNCTION : Evolve_RK4