-
Notifications
You must be signed in to change notification settings - Fork 9
/
rk4_pointmass.cu
237 lines (217 loc) · 10 KB
/
rk4_pointmass.cu
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @author Christoph Schaefer [email protected] and Thomas I. Maindl
*
* @section LICENSE
* Copyright (c) 2019 Christoph Schaefer
*
* This file is part of miluphcuda.
*
* miluphcuda is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* miluphcuda is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with miluphcuda. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "rk4_pointmass.h"
#include "miluph.h"
#include "timeintegration.h"
#include "parameter.h"
#include "memory_handling.h"
#include "rhs.h"
#include "pressure.h"
#include "boundary.h"
#include "config_parameter.h"
extern __device__ double dt;
/* the runge-kutta 4nd order integrator with fixed timestep */
void rk4_nbodies()
{
int rkstep;
// alloc mem for multiple rhs and copy immutables
int allocate_immutables = 0;
for (rkstep = 0; rkstep < 4; rkstep++) {
copy_pointmass_immutables_device_to_device(&rk4_pointmass_device[rkstep], &pointmass_device);
}
copy_pointmass_variables_device_to_device(&rk4_pointmass_device[RKFIRST], &pointmass_device);
copy_pointmass_variables_device_to_device(&rk4_pointmass_device[RKSTART], &pointmass_device);
cudaVerify(cudaDeviceSynchronize());
// calculate first right hand side with rk[RKFIRST]_device
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk4_pointmass_device[RKFIRST], sizeof(struct Pointmass)));
cudaVerifyKernel((rhs_pointmass<<<numberOfMultiprocessors, NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// remember values of first step
copy_pointmass_variables_device_to_device(&rk4_pointmass_device[RKSTART], &rk4_pointmass_device[RKFIRST]);
copy_pointmass_derivatives_device_to_device(&rk4_pointmass_device[RKSTART], &rk4_pointmass_device[RKFIRST]);
cudaVerify(cudaDeviceSynchronize());
// set rk[RKFIRST] variables
cudaVerifyKernel((rhs_pointmass<<<numberOfMultiprocessors,NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerifyKernel((rk4_integrateFirstStep<<<numberOfMultiprocessors, NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// get derivatives for second step
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk4_pointmass_device[RKFIRST], sizeof(struct Pointmass)));
cudaVerifyKernel((rhs_pointmass<<<numberOfMultiprocessors,NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// rk4_integrate second step
cudaVerifyKernel((rk4_integrateSecondStep<<<numberOfMultiprocessors, NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// get derivatives for third step
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk4_pointmass_device[RKSECOND], sizeof(struct Pointmass)));
cudaVerifyKernel((rhs_pointmass<<<numberOfMultiprocessors,NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
// rk4_integrate third step
cudaVerifyKernel((rk4_integrateThirdStep<<<numberOfMultiprocessors, NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
// get derivatives for the fourth (and last) step
// this happens at t = t0 + h
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk4_pointmass_device[RKTHIRD], sizeof(struct Pointmass)));
cudaVerifyKernel((rhs_pointmass<<<numberOfMultiprocessors,NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
cudaVerify(cudaMemcpyToSymbol(pointmass, &pointmass_device, sizeof(struct Pointmass)));
// rk4_integrate fourth step
cudaVerifyKernel((rk4_integrateFourthStep<<<numberOfMultiprocessors, NUM_THREADS_RK4_INTEGRATE_STEP>>>()));
}
// acceleration due to the point masses
__global__ void rhs_pointmass()
{
int i, inc;
int j;
int d;
double r;
double rrr;
double dr[DIM];
inc = blockDim.x * gridDim.x;
// loop for point masses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i += inc) {
pointmass.ax[i] = 0.0;
#if DIM > 1
pointmass.ay[i] = 0.0;
#if DIM > 2
pointmass.az[i] = 0.0;
#endif
#endif
for (j = 0; j < numPointmasses; j++) {
if (i == j) continue;
r = 0.0;
dr[0] = pointmass.x[j] - pointmass.x[i];
#if DIM > 1
dr[1] = pointmass.y[j] - pointmass.y[i];
#if DIM > 2
dr[2] = pointmass.z[j] - pointmass.z[i];
#endif
#endif
for (d = 0; d < DIM; d++) {
r += dr[d]*dr[d];
}
r = sqrt(r);
rrr = r*r*r;
pointmass.ax[i] += gravConst * pointmass.m[j] * dr[0]/(rrr);
#if DIM > 1
pointmass.ay[i] += gravConst * pointmass.m[j] * dr[1]/(rrr);
#if DIM > 2
pointmass.az[i] += gravConst * pointmass.m[j] * dr[2]/(rrr);
#endif
#endif
}
if (pointmass_rhs.feels_particles[i]) {
pointmass.ax[i] += pointmass_rhs.feedback_ax[i];
#if DIM > 1
pointmass.ay[i] += pointmass_rhs.feedback_ay[i];
#if DIM > 2
pointmass.az[i] += pointmass_rhs.feedback_az[i];
#endif
#endif
}
}
}
__global__ void rk4_integrateFirstStep()
{
int i;
// loop for the point masses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
rk4_pointmass[RKFIRST].x[i] = rk4_pointmass[RKSTART].x[i] + dt * B21 * rk4_pointmass[RKFIRST].vx[i];
#if DIM > 1
rk4_pointmass[RKFIRST].y[i] = rk4_pointmass[RKSTART].y[i] + dt * B21 * rk4_pointmass[RKFIRST].vy[i];
#endif
#if DIM == 3
rk4_pointmass[RKFIRST].z[i] = rk4_pointmass[RKSTART].z[i] + dt * B21 * rk4_pointmass[RKFIRST].vz[i];
#endif
rk4_pointmass[RKFIRST].vx[i] = rk4_pointmass[RKSTART].vx[i] + dt * B21 * rk4_pointmass[RKFIRST].ax[i];
#if DIM > 1
rk4_pointmass[RKFIRST].vy[i] = rk4_pointmass[RKSTART].vy[i] + dt * B21 * rk4_pointmass[RKFIRST].ay[i];
#endif
#if DIM == 3
rk4_pointmass[RKFIRST].vz[i] = rk4_pointmass[RKSTART].vz[i] + dt * B21 * rk4_pointmass[RKFIRST].az[i];
#endif
}
}
__global__ void rk4_integrateSecondStep()
{
int i;
// loop for pointmasses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
rk4_pointmass[RKSECOND].vx[i] = rk4_pointmass[RKSTART].vx[i] + dt * B21 * rk4_pointmass[RKFIRST].ax[i];
#if DIM > 1
rk4_pointmass[RKSECOND].vy[i] = rk4_pointmass[RKSTART].vy[i] + dt * B21 * rk4_pointmass[RKFIRST].ay[i];
#endif
#if DIM == 3
rk4_pointmass[RKSECOND].vz[i] = rk4_pointmass[RKSTART].vz[i] + dt * B21 * rk4_pointmass[RKFIRST].az[i];
#endif
rk4_pointmass[RKSECOND].x[i] = rk4_pointmass[RKSTART].x[i] + dt * B21 * rk4_pointmass[RKFIRST].vx[i];
#if DIM > 1
rk4_pointmass[RKSECOND].y[i] = rk4_pointmass[RKSTART].y[i] + dt * B21 * rk4_pointmass[RKFIRST].vy[i];
#endif
#if DIM == 3
rk4_pointmass[RKSECOND].z[i] = rk4_pointmass[RKSTART].z[i] + dt * B21 * rk4_pointmass[RKFIRST].vz[i];
#endif
}
}
__global__ void rk4_integrateThirdStep()
{
int i;
// loop for pointmasses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
rk4_pointmass[RKTHIRD].vx[i] = rk4_pointmass[RKSTART].vx[i] + dt * rk4_pointmass[RKSECOND].ax[i];
#if DIM > 1
rk4_pointmass[RKTHIRD].vy[i] = rk4_pointmass[RKSTART].vy[i] + dt * rk4_pointmass[RKSECOND].ay[i];
#endif
#if DIM == 3
rk4_pointmass[RKTHIRD].vz[i] = rk4_pointmass[RKSTART].vz[i] + dt * rk4_pointmass[RKSECOND].az[i];
#endif
rk4_pointmass[RKTHIRD].x[i] = rk4_pointmass[RKSTART].x[i] + dt * rk4_pointmass[RKSECOND].vx[i];
#if DIM > 1
rk4_pointmass[RKTHIRD].y[i] = rk4_pointmass[RKSTART].y[i] + dt * rk4_pointmass[RKSECOND].vy[i];
#endif
#if DIM == 3
rk4_pointmass[RKTHIRD].z[i] = rk4_pointmass[RKSTART].z[i] + dt * rk4_pointmass[RKSECOND].vz[i];
#endif
}
}
__global__ void rk4_integrateFourthStep()
{
int i;
int d;
// loop pointmasses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
pointmass.vx[i] = rk4_pointmass[RKSTART].vx[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].ax[i] + B32 * rk4_pointmass[RKFIRST].ax[i] + B32 * rk4_pointmass[RKSECOND].ax[i] + C1 * rk4_pointmass[RKTHIRD].ax[i]);
pointmass.ax[i] = 1./6.0 *(C1 * rk4_pointmass[RKSTART].ax[i] + B32 * rk4_pointmass[RKFIRST].ax[i] + B32 * rk4_pointmass[RKSECOND].ax[i] + C1 * rk4_pointmass[RKTHIRD].ax[i]);
#if DIM > 1
pointmass.vy[i] = rk4_pointmass[RKSTART].vy[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].ay[i] + B32 * rk4_pointmass[RKFIRST].ay[i] + B32 * rk4_pointmass[RKSECOND].ay[i] + C1 * rk4_pointmass[RKTHIRD].ay[i]);
pointmass.ay[i] = 1./6.0 *(C1 * rk4_pointmass[RKSTART].ay[i] + B32 * rk4_pointmass[RKFIRST].ay[i] + B32 * rk4_pointmass[RKSECOND].ay[i] + C1 * rk4_pointmass[RKTHIRD].ay[i]);
#endif
#if DIM > 2
pointmass.vz[i] = rk4_pointmass[RKSTART].vz[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].az[i] + B32 * rk4_pointmass[RKFIRST].az[i] + B32 * rk4_pointmass[RKSECOND].az[i] + C1 * rk4_pointmass[RKTHIRD].az[i]);
pointmass.az[i] = 1./6.0 *(C1 * rk4_pointmass[RKSTART].az[i] + B32 * rk4_pointmass[RKFIRST].az[i] + B32 * rk4_pointmass[RKSECOND].az[i] + C1 * rk4_pointmass[RKFIRST].az[i]);
#endif
pointmass.x[i] = rk4_pointmass[RKSTART].x[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].vx[i] + B32 * rk4_pointmass[RKFIRST].vx[i] + B32 * rk4_pointmass[RKSECOND].vx[i] + C1 * rk4_pointmass[RKTHIRD].vx[i]);
#if DIM > 1
pointmass.y[i] = rk4_pointmass[RKSTART].y[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].vy[i] + B32 * rk4_pointmass[RKFIRST].vy[i] + B32 * rk4_pointmass[RKSECOND].vy[i] + C1 * rk4_pointmass[RKTHIRD].vy[i]);
#endif
#if DIM > 2
pointmass.z[i] = rk4_pointmass[RKSTART].z[i] + dt/6.0 * (C1 * rk4_pointmass[RKSTART].vz[i] + B32 * rk4_pointmass[RKFIRST].vz[i] + B32 * rk4_pointmass[RKSECOND].vz[i] + C1 * rk4_pointmass[RKTHIRD].vz[i]);
#endif
}
}