-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtimeintegration.cu
255 lines (217 loc) · 7.1 KB
/
timeintegration.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* @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 "boundary.h"
#include "timeintegration.h"
#include "tree.h"
#include "porosity.h"
#include "pressure.h"
#include "plasticity.h"
#include "soundspeed.h"
#include "parameter.h"
#include "io.h"
#include "xsph.h"
#include "miluph.h"
#include "aneos.h"
#include "linalg.h"
#include "density.h"
#include "rhs.h"
#include "viscosity.h"
#include "float.h"
#include "extrema.h"
#include "sinking.h"
#include "config_parameter.h"
pthread_t fileIOthread;
double L_ini = 0.0;
__device__ double maxPosAbsError;
__device__ double maxVelAbsError;
__device__ int movingparticles = 0;
__device__ int reset_movingparticles = 1;
__device__ double dtNewErrorCheck = 0.0;
#if INTEGRATE_DENSITY
__device__ double maxDensityAbsError;
#endif
#if INTEGRATE_ENERGY
__device__ double maxEnergyAbsError;
#endif
#if PALPHA_POROSITY
__device__ double maxPressureAbsChange;
__device__ double maxAlphaDiff;
#endif
#if FRAGMENTATION
__device__ double maxDamageTimeStep;
#endif
__device__ int errorSmallEnough = FALSE;
__constant__ int isRelaxationRun = FALSE;
__constant__ volatile int *childList;
int *childListd;
/* time variables */
void (*integrator)();
int startTimestep = 0;
int numberOfTimesteps = 1;
double timePerStep;
double dt_host;
double dt_grav;
int gravity_index = 0;
int flag_force_gravity_calc = 0;
double currentTime;
double startTime;
double h5time;
__device__ double dt; // timestep on the device
__device__ double dtmax; // max allowed timestep (either from cmd-line or output timestep)
__device__ double endTimeD, currentTimeD;
__device__ double substep_currentTimeD;
__device__ int blockCount = 0;
__device__ volatile int maxNodeIndex;
__device__ volatile double radius;
// tree computational domain
double *minxPerBlock, *maxxPerBlock;
__device__ double minx, maxx;
#if DIM > 1
double *minyPerBlock, *maxyPerBlock;
__device__ double miny, maxy;
#endif
#if DIM == 3
double *minzPerBlock, *maxzPerBlock;
__device__ double minz, maxz;
#endif
// map [i][j] to [i*DIM*DIM+j] for the tensors
__device__ int stressIndex(int particleIndex, int row, int col)
{
return particleIndex*DIM*DIM+row*DIM+col;
}
#if SOLID
__global__ void symmetrizeStress(void)
{
register int i, j, k, inc;
register double val;
inc = blockDim.x * gridDim.x;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i += inc) {
for (j = 0; j < DIM; j ++) {
for (k = 0; k < j; k++) {
val = 0.5 * (p.S[stressIndex(i,j,k)] + p.S[stressIndex(i,k,j)]);
p.S[stressIndex(i,j,k)] = val;
p.S[stressIndex(i,k,j)] = val;
}
}
}
}
#endif
double calculate_angular_momentum(void)
{
int i;
double l_i = 0.0;
double Lx = 0.0;
double Ly = 0.0;
double Lz = 0.0;
double L = 0.0;
#if DIM > 1
for (i = 0; i < numberOfParticles; i++) {
l_i = 0;
#if DIM > 2
l_i = p_host.m[i]*(p_host.y[i]*p_host.vz[i] - p_host.z[i]*p_host.vy[i]);
Lx += l_i;
l_i = p_host.m[i]*(p_host.z[i]*p_host.vx[i] - p_host.x[i]*p_host.vz[i]);
Ly += l_i;
l_i = p_host.m[i]*(p_host.x[i]*p_host.vy[i] - p_host.y[i]*p_host.vx[i]);
Lz += l_i;
#else
l_i = p_host.m[i]*(p_host.x[i]*p_host.vy[i] - p_host.y[i]*p_host.vx[i]);
Lz += l_i;
#endif
}
L = sqrt(Lx*Lx + Ly*Ly + Lz*Lz);
#endif
return L;
}
void initIntegration()
{
L_ini = calculate_angular_momentum();
if (param.verbose) {
fprintf(stdout, "Initial angular momentum is: %.17e\n", L_ini);
}
dt_host = timePerStep;
// copy constants to device
cudaVerify(cudaMemcpyToSymbol(dt, &dt_host, sizeof(double)));
cudaVerify(cudaMemcpyToSymbol(dtmax, ¶m.maxtimestep, sizeof(double)));
cudaVerify(cudaMemcpyToSymbol(theta, &treeTheta, sizeof(double)));
cudaVerify(cudaMemcpyToSymbol(numParticles, &numberOfParticles, sizeof(int)));
cudaVerify(cudaMemcpyToSymbol(numPointmasses, &numberOfPointmasses, sizeof(int)));
cudaVerify(cudaMemcpyToSymbol(maxNumParticles, &maxNumberOfParticles, sizeof(int)));
cudaVerify(cudaMemcpyToSymbol(numRealParticles, &numberOfRealParticles, sizeof(int)));
cudaVerify(cudaMemcpyToSymbol(numChildren, &numberOfChildren, sizeof(int)));
cudaVerify(cudaMemcpyToSymbol(numNodes, &numberOfNodes, sizeof(int)));
#if FRAGMENTATION
cudaVerify(cudaMemcpyToSymbol(maxNumFlaws, &maxNumFlaws_host, sizeof(int)));
#endif
// memory for tree
cudaVerify(cudaMalloc((void**)&minxPerBlock, sizeof(double)*numberOfMultiprocessors));
cudaVerify(cudaMalloc((void**)&maxxPerBlock, sizeof(double)*numberOfMultiprocessors));
#if DIM > 1
cudaVerify(cudaMalloc((void**)&minyPerBlock, sizeof(double)*numberOfMultiprocessors));
cudaVerify(cudaMalloc((void**)&maxyPerBlock, sizeof(double)*numberOfMultiprocessors));
#endif
#if DIM == 3
cudaVerify(cudaMalloc((void**)&minzPerBlock, sizeof(double)*numberOfMultiprocessors));
cudaVerify(cudaMalloc((void**)&maxzPerBlock, sizeof(double)*numberOfMultiprocessors));
#endif
// set the pointer on the gpu to p_device
cudaVerify(cudaMemcpyToSymbol(p, &p_device, sizeof(struct Particle)));
cudaVerify(cudaMemcpyToSymbol(p_rhs, &p_device, sizeof(struct Particle)));
cudaVerify(cudaMemcpyToSymbol(pointmass, &pointmass_device, sizeof(struct Pointmass)));
cudaVerify(cudaMemcpyToSymbol(pointmass_rhs, &pointmass_device, sizeof(struct Pointmass)));
cudaVerifyKernel((initializeSoundspeed<<<numberOfMultiprocessors*4, NUM_THREADS_512>>>()));
}
//this function is called after every successful integration (not only when output is generated)
void afterIntegrationStep(void)
{
#if PARTICLE_ACCRETION
cudaVerifyKernel((ParticleSinking<<<numberOfMultiprocessors*4, NUM_THREADS_PRESSURE>>>()));
#endif
#if MORE_OUTPUT
cudaVerifyKernel((get_extrema<<<numberOfMultiprocessors*4, NUM_THREADS_PRESSURE>>>()));
#endif
}
void endIntegration(void)
{
int rc = pthread_join(fileIOthread, NULL);
assert(0 == rc);
// free memory
cudaVerify(cudaFree(minxPerBlock));
cudaVerify(cudaFree(maxxPerBlock));
#if DIM > 1
cudaVerify(cudaFree(minyPerBlock));
cudaVerify(cudaFree(maxyPerBlock));
#endif
#if DIM == 3
cudaVerify(cudaFree(minzPerBlock));
cudaVerify(cudaFree(maxzPerBlock));
#endif
cleanupMaterials();
}
/* just do it */
void timeIntegration()
{
initIntegration();
integrator();
endIntegration();
}