-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_mesh_cuda.cpp
359 lines (284 loc) · 13.7 KB
/
run_mesh_cuda.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
Includes main function to run mesh cuda simulation.
Compile with 'make mesh'.
Run with `mpirun -np N ./mesh [input file]` where N is the number of processors to use and input file is an optional argument providing the file path to the input file to use. If no input file is provided, will default to use mesh_input.txt.
*/
#include <stdio.h>
#include <cmath>
#include <limits>
#include "Mesh_cuda.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <algorithm>
#include "mpi.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "mesh_output.h"
using namespace std;
void multiscale_test(Sea *sea);
void acoustic_wave(Sea *sea);
int main(int argc, char *argv[]) {
// MPI variables
MPI_Comm comm;
MPI_Status status;
int rank, size;//, source, tag;
// Initialise MPI and compute number of processes and local rank
comm = MPI_COMM_WORLD;
MPI_Init(&argc, &argv);
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
if (rank == 0) {
printf("Running on %d process(es)\n", size);
}
char input_filename[200];
if (argc == 1) {
// no input arguments - default input file.
string fname = "mesh_input.txt";
strcpy(input_filename, fname.c_str());
} else {
strcpy(input_filename, argv[1]);
}
if (string(input_filename).find("checkpoint") != string::npos) {
start_from_checkpoint(input_filename, comm, status,
rank, size);
} else {
// input file is a parameter file
Sea sea(input_filename);
//multiscale_test(&sea);
acoustic_wave(&sea);
if (rank == 0) {
sea.print_inputs();
}
sea.run(comm, &status, rank, size, 0);
}
MPI_Finalize();
}
void multiscale_test(Sea *sea) {
/*
Initial data for
*/
// locate index of first multilayer SWE level
int m_in = 0;
while (sea -> models[m_in] != 'M') m_in += 1;
// locate index of first compressible level
//int c_in = m_in;
//while (sea -> models[c_in] != 'C') c_in += 1;
int c_in = sea -> nlevels;
if (sea -> models[sea -> nlevels-1] == 'C') {
while(sea -> models[c_in-1] == 'C') c_in -= 1;
}
float * D0 = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
float * Sx0 = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
float * Sy0 = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
// set multiscale test initial data
for (int y = 0; y < sea->nys[m_in]; y++) {
for (int x = 0; x < sea->nxs[m_in]; x++) {
D0[y * sea->nxs[m_in] + x] = -0.5 *
log(1.0 - 2.0 / (sea->zmax+2*sea->dz/pow(2, m_in)));// - 0.1 *
//exp(-(pow(sea->xs[x]-5.0, 2)+pow(sea->ys[y]-5.0, 2)) * 2.0);
D0[(sea->nys[m_in] + y) * sea->nxs[m_in] + x] = 1.1 + 0.001 * sin(2.0 * sea->xs[x] * M_PI / (sea->xs[sea->nxs[m_in]-1-sea->ng] - sea->xs[sea->ng]));
D0[(2*sea->nys[m_in] + y) * sea->nxs[m_in] + x] = -0.5 *
log(1.0 - 2.0 / sea->zmin);
for (int z = 0; z < sea->nzs[m_in]; z++) {
Sx0[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] = 0.0;
Sy0[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] = 0.0;
}
}
}
sea->initial_swe_data(D0, Sx0, Sy0);
// clean up arrays
delete[] D0;
delete[] Sx0;
delete[] Sy0;
float * D0c = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sx0c = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sy0c = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sz0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * tau0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
for (int z = 0; z < sea->nzs[c_in]; z++) {
for (int y = 0; y < sea->nys[c_in]; y++) {
for (int x = 0; x < sea->nxs[c_in]; x++) {
float max_v = 0.3;
float r = sqrt(
(x - 0.5*sea->nxs[c_in])*(x - 0.5*sea->nxs[c_in]) +
(y - 0.5*sea->nys[c_in])*(y - 0.5*sea->nys[c_in]));
float v = 0.0;
if (r < 0.05 * sea->nxs[c_in]) {
v = 20.0 * max_v * r / sea->nxs[c_in];
} else if (r < 0.1 * sea->nxs[c_in]) {
v = 2.0 * 20.0 * max_v * 0.05 - 20.0 * max_v * r / sea->nxs[c_in];
}
float D = sea->Us[c_in][((z*sea->nys[c_in] + y) * sea->nxs[c_in] + x) * sea->vec_dims[c_in]];
D0c[(z * sea->nys[c_in]+ y) * sea->nxs[c_in] + x] = D;
if (r > 0.0) {
// Sx
Sx0c[(z * sea->nys[c_in]+ y) * sea->nxs[c_in] + x]
= - D * v * (y - 0.5*sea->nys[c_in]) / r;
Sy0c[(z * sea->nys[c_in]+ y) * sea->nxs[c_in] + x]
= D * v * (x - 0.5*sea->nxs[c_in]) / r;
} else {
Sx0c[(z * sea->nys[c_in]+ y) * sea->nxs[c_in] + x] = 0.0;
Sy0c[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
}
Sz0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
tau0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
}
}
}
sea->initial_compressible_data(D0c, Sx0c, Sy0c, Sz0, tau0);
// clean up arrays
delete[] D0c;
delete[] Sx0c;
delete[] Sy0c;
delete[] Sz0;
delete[] tau0;
}
void acoustic_wave(Sea *sea) {
/*
Isentropic smooth flow in 1d, as described in section 6.1.1 of Marti & Muller 15, 4.6 of Zhang & MacFadyen 06.
*/
// TODO: need to fix main cuda_run script so that it restricts/prolongs out from coarsest compressible grid rather than from coarsest multilayer grid for this example.
// locate index of first multilayer SWE level
int m_in = 0;
while (sea -> models[m_in] != 'M') m_in += 1;
// locate index of first compressible level
int c_in = sea -> nlevels;
if (sea -> models[sea -> nlevels-1] == 'C') {
while(sea -> models[c_in-1] == 'C') c_in -= 1;
}
float * D0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sx0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sy0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * Sz0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * tau0 = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float * ps = new float[sea->nxs[c_in]*sea->nys[c_in]*sea->nzs[c_in]];
float L = sea->nxs[c_in] * 0.3;
float rho_ref = 0.5;
float alpha = 0.05;
float K = 500;
float s = sqrt(sea->gamma - 1.0);
float z_surface = sea->zmax+4*sea->dz/pow(2, c_in);
float M = 1;
float gamma_surf = (1.0 - M * z_surface / (sea->R*sea->R*sea->alpha0*sea->alpha0)) / sea->alpha0;
// set acoustic wave test initial data
for (int z = 0; z < sea->nzs[c_in]; z++) {
float gamma_z = (1.0 - M * (sea->zmin + sea->dz/pow(2, c_in) * (sea->nzs[c_in] - z - 1)) / (sea->R*sea->R*sea->alpha0*sea->alpha0)) / sea->alpha0;
float rho_z = rho_ref * (1.0 + alpha) *
pow(gamma_z - gamma_surf, 1.0/sea->gamma);
float rhoh_temp = rho_z + sea->gamma * K*pow(rho_z,sea->gamma) / (sea->gamma - 1.0);
float cs_temp = sqrt(sea->gamma * K*pow(rho_z,sea->gamma) / rhoh_temp);
float J = -log((s + cs_temp) / (s - cs_temp)) / s;
for (int y = 0; y < sea->nys[c_in]; y++) {
for (int x = 0; x < sea->nxs[c_in]; x++) {
float r = sqrt((x-0.5*sea->nxs[c_in])*(x-0.5*sea->nxs[c_in]) +
(y-0.5*sea->nys[c_in])*(y-0.5*sea->nys[c_in]));
// HACK - unflatten
float f = 0.0;//abs(r) < L ? pow(r*r / (L*L) - 1.0, 4) : 0.0;
float rho = rho_ref * (1.0 + alpha * f) *
pow(gamma_z - gamma_surf, 1.0/sea->gamma);
float p = K * pow(rho, sea->gamma);
//cout << "z: " << z << " p: " << p << '\n';
float rhoh = rho + sea->gamma * p / (sea->gamma - 1.0);
float cs = sqrt(sea->gamma * p / rhoh);
float a = log((s + cs) / (s - cs)) / s;
float u = (exp(2.0 * (J + a)) - 1.0) / (1.0 + exp(2.0 * (J + a)));
// HACK
u = 0.0;
float W = 1.0 / sqrt(1.0 - u*u);
D0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = rho * W;
if (r < 1.0) {
Sx0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
Sy0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
} else {
Sx0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] =
rhoh * u * W * (x-0.5 * sea->nxs[c_in]) / r;
Sy0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] =
rhoh * u * W * (y-0.5 * sea->nys[c_in]) / r;
}
Sz0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = 0.0;
tau0[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] =
rhoh*W*W - p - rho * W;
ps[(z * sea->nys[c_in] + y) * sea->nxs[c_in] + x] = p;
//cout << rho * W << ' ' << rhoh * u * W << ' ' << rhoh*W*W - p - rho * W << '\n';
}
}
}
sea->initial_compressible_data(D0, Sx0, Sy0, Sz0, tau0);
// clean up arrays
delete[] D0;
delete[] Sx0;
delete[] Sy0;
delete[] Sz0;
delete[] tau0;
float * D0s = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
float * Sx0s = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
float * Sy0s = new float[sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]];
sea->p_const[0] = 0.0;
float gamma_z = (1.0 - M * (sea->zmin*0.6+sea->zmax*0.4) / (sea->R*sea->R*sea->alpha0*sea->alpha0)) / sea->alpha0;
sea->p_const[1] = K * pow(rho_ref, sea->gamma) * (gamma_z - gamma_surf);
gamma_z = (1.0 - M * sea->zmin/ (sea->R*sea->R*sea->alpha0*sea->alpha0)) / sea->alpha0;
sea->p_const[2] = K * pow(rho_ref, sea->gamma) * (gamma_z - gamma_surf);
cout << "p1 = " << sea->p_const[1] << " p2 = " << sea->p_const[2] << '\n';
L /= pow(2.0, c_in - m_in);
// set multiscale test initial data
for (int y = 0; y < sea->nys[m_in]; y++) {
for (int x = 0; x < sea->nxs[m_in]; x++) {
float r = sqrt((x-0.5*sea->nxs[m_in])*(x-0.5*sea->nxs[m_in]) +
(y-0.5*sea->nys[m_in])*(y-0.5*sea->nys[m_in]));
// HACK - unflatten
float f = 0.0;//abs(r) < L ? pow(r*r / (L*L) - 1.0, 4) : 0.0;
D0s[y * sea->nxs[m_in] + x] = -0.5 *
log(1.0 - 2.0 / z_surface);
float height = sea->R*sea->R*sea->alpha0*sea->alpha0 / M * (1 - sea->alpha0 * (sea->p_const[1] / (K * pow(rho_ref * (1.0 + alpha * f), sea->gamma)) + gamma_surf));
//sqrt(z_surface*z_surface/sqrt(1-2/z_surface) - sea->p_const[1] /
//(K * pow(rho_ref * (1.0 + alpha * f), sea->gamma)));
//cout << "heights: " << z_surface << ' ' << height << ' ' << sea->zmin << '\t';
D0s[(sea->nys[m_in] + y) * sea->nxs[m_in] + x] = -0.5 *
log(1.0 - 2.0 / height);
D0s[(2*sea->nys[m_in] + y) * sea->nxs[m_in] + x] = -0.5 *
log(1.0 - 2.0 / sea->zmin);
for (int z = 1; z < sea->nzs[m_in]; z++) {
float rho = (sea->p_const[z] / K, 1.0/sea->gamma);
float rhoh = rho + sea->gamma * sea->p_const[z] / (sea->gamma - 1.0);
float cs = sqrt(sea->gamma * sea->p_const[z] / rhoh);
height = sea->R*sea->R*sea->alpha0*sea->alpha0 / M * (1 - sea->alpha0 * (sea->p_const[z] / (K * pow(rho_ref * (1.0 + alpha * f), sea->gamma)) + gamma_surf));
gamma_z = (1.0 - M * height / (sea->R*sea->R*sea->alpha0*sea->alpha0)) / sea->alpha0;
float rho_z = rho_ref * (1.0 + alpha) *
pow(gamma_z - gamma_surf, 1.0/sea->gamma);
float rhoh_temp = rho_z + sea->gamma * K*pow(rho_z,sea->gamma) / (sea->gamma - 1.0);
float cs_temp = sqrt(sea->gamma * K*pow(rho_z,sea->gamma) / rhoh_temp);
float J = -log((s + cs_temp) / (s - cs_temp)) / s;
float a = log((s + cs) / (s - cs)) / s;
float u = (exp(2.0 * (J + a)) - 1.0) / (1.0 + exp(2.0 * (J + a)));
// HACK
u = 0.0;
float W = 1.0 / sqrt(1.0 - u*u);
if (r < 1.0) {
Sx0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] = 0.0;
Sy0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] = 0.0;
} else {
D0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] *= W;
Sx0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] =
D0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] *
u * W * (x-0.5 * sea->nxs[m_in]) / r;
Sy0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] =
D0s[(z * sea->nys[m_in] + y) * sea->nxs[m_in] + x] *
u * W * (y-0.5 * sea->nys[m_in]) / r;
}
}
// set top layer to be 0
Sx0s[y * sea->nxs[m_in] + x] = 0.0;
Sy0s[y * sea->nxs[m_in] + x] = 0.0;
//cout << "D0s: " << D0s[y * sea->nxs[m_in] + x] << ' ' << D0s[(1 * sea->nys[m_in] + y) * sea->nxs[m_in] + x] << ' ' << D0s[(2 * sea->nys[m_in] + y) * sea->nxs[m_in] + x] << '\n';
}
}
sea->initial_swe_data(D0s, Sx0s, Sy0s);
//for (int i = 0; i < sea->nxs[m_in]*sea->nys[m_in]*sea->nzs[m_in]; i++) {
//cout << sea->Us[0][i*4+2] << '\n';
//}
// clean up arrays
delete[] D0s;
delete[] Sx0s;
delete[] Sy0s;
}