-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmesh_cuda.cpp
758 lines (672 loc) · 23.5 KB
/
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <limits>
#include "Mesh_cuda.h"
#include "mesh_cuda_kernel.h"
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "mpi.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif // H5_NO_STD
#endif
#include "H5Cpp.h"
using namespace std;
Sea::Sea(int _nx, int _ny,
int _nt, int _ng, int _r, float _df,
float xmin, float xmax,
float ymin, float ymax,
float _zmin, float _zmax, float * _rho,
float _Q, float _gamma, float _E_He, float _Cv,
float _alpha, float * _beta, float * _gamma_down,
bool _periodic, bool _burning, int _dprint, int _n_print_levels)
: nx(_nx), ny(_ny), ng(_ng), zmin(_zmin), zmax(_zmax), nt(_nt), r(_r), df(_df), gamma(_gamma), E_He(_E_He), Cv(_Cv), alpha0(_alpha), periodic(_periodic), burning(_burning), dprint(_dprint), n_print_levels(_n_print_levels)
{
/**
Implement Sea class
*/
xs = new float[nx];
for (int i = 0; i < nx; i++) {
xs[i] = xmin + (i-ng) * (xmax - xmin) / (nx-2*ng);
}
ys = new float[ny];
for (int i = 0; i < ny; i++) {
ys[i] = ymin + (i-ng) * (ymax - ymin) / (ny-2*ng);
}
nxs[0] = nx;
nys[0] = ny;
for (int i = 1; i < nlevels; i++) {
nxs[i] = int(r * df * nxs[i-1]);
nys[i] = int(r * df * nys[i-1]);
}
dx = xs[1] - xs[0];
dy = ys[1] - ys[0];
// NOTE: need to define this in such a way that it is calculated using layer separation on first compressible grid
int c_in = nlevels-1;
while(models[c_in] == 'C') c_in -= 1;
dz = (zmax - zmin) / (nzs[c_in] - 1.0);
dz *= pow(r, c_in);
float cfl = 0.5; // cfl number?
dt = cfl * min(dx, min(dy, dz));
rho = new float[nzs[0]];
for (int i = 0; i < nzs[0]; i++) {
rho[i] = _rho[i];
}
Q = _Q;
for (int i = 0; i < 3; i++) {
beta[i] = _beta[i];
for (int j = 0; j < 3; j++) {
gamma_down[i*3+j] = _gamma_down[i*3+j];
}
}
// find inverse of gamma
for (int i = 0; i < 3*3; i++) {
gamma_up[i] = gamma_down[i];
}
Sea::invert_mat(gamma_up, 3, 3);
matching_indices = new int[4 * (nlevels-1)];
matching_indices[0] = int(ceil(nx*0.5*(1-df)));
matching_indices[1] = int(ceil(nx*0.5*(1+df)));
matching_indices[2] = int(ceil(ny*0.5*(1-df)));
matching_indices[3] = int(ceil(ny*0.5*(1+df)));
cout << "Matching indices: " << matching_indices[0] << ',' << matching_indices[1] << ',' << matching_indices[2] << ',' << matching_indices[3] << '\n';
cout << "Made a Sea.\n";
}
void Sea::invert_mat(float * M, int m, int n) {
/**
Invert the m x n matrix M in place using Gaussian elimination
*/
float * B = new float[m*n*2];
// initialise augmented matrix
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
B[i*2*n+j] = M[i*n+j];
B[i*2*n+ n+j] = 0.0;
}
B[i*2*n+n+i] = 1.0;
}
for (int k = 0; k < min(m,n); k++) {
// i_max := argmax (i = k ... m, abs(A[i, k]))
int i_max = k;
for (int i = k+1; i < m; i++) {
if (abs(B[i*n*2+k]) > abs(B[i_max*n*2+k])) {
i_max = i;
}
}
if (abs(B[i_max*n*2+k]) < 1.0e-12) {
cout << "Matrix is singular!\n";
}
// swap rows(k, i_max)
for (int i = 0; i < 2*n; i++) {
float temp = B[k*n*2+i];
B[k*n*2+i] = B[i_max*n*2+i];
B[i_max*n*2+i] = temp;
}
for (int i = k+1; i < m; i++) {
float f = B[i*n*2+k] / B[k*n*2+k];
for (int j = k+1; j < n*2; j++) {
B[i*n*2+j] -= B[k*n*2+j] * f;
}
B[i*n*2+k] = 0.0;
}
}
// back substitution
for (int k = 0; k < m; k++) {
for (int i = k+1; i < n; i++) {
float f = B[k*n*2+i] / B[i*2*n+i];
for (int j = k+1; j < 2*n; j++) {
B[k*n*2+j] -= B[i*n*2+j] * f;
}
}
for (int i = k+1; i < 2*n; i++) {
B[k*n*2+i] /= B[k*n*2+k];
}
B[k*n*2+k] = 1.0;
}
// put answer back in M
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[i*n+j] = B[i*2*n+n+j];
}
}
delete[] B;
}
Sea::Sea(char * filename) {
// open file
ifstream inputFile(filename);
stringstream ss;
if (inputFile) {
ss << inputFile.rdbuf();
inputFile.close();
}
size_t t = string(filename).find("_", 10+1);
char * param_filename = filename + t;
init_sea(ss, param_filename);
}
Sea::Sea(stringstream &inputFile, char * filename) {
init_sea(inputFile, filename);
}
void Sea::init_sea(stringstream &inputFile, char * filename)
{
/**
Constructor for Sea class using inputs from file.
Data is validated: an error will be thrown and the program terminated if any of the inputs are found to be invalid.
*/
// open file
//ifstream inputFile(filename);
string variableName;
float value;
float xmin, xmax, ymin, ymax;
while (inputFile >> variableName) {
// mega if/else statement of doom
if (variableName == "nx") {
inputFile >> value;
nx = int(value);
} else if (variableName == "ny") {
inputFile >> value;
ny = int(value);
} else if (variableName == "ng") {
inputFile >> value;
ng = int(value);
} else if (variableName == "nlevels") {
inputFile >> value;
nlevels = int(value);
cout << "nlevels = " << nlevels << '\n';
models = new char[nlevels];
nxs = new int[nlevels];
nys = new int[nlevels];
nzs = new int[nlevels];
vec_dims = new int[nlevels];
} else if (variableName == "models") {
for (int i = 0; i < nlevels; i++) {
inputFile >> models[i];
if (models[i] == 'S' || models[i] == 'M') {
vec_dims[i] = 4;
} else {
vec_dims[i] = 6;
}
}
} else if (variableName == "nzs") {
for (int i = 0; i < nlevels; i++) {
inputFile >> value;
nzs[i] = int(value);
}
} else if (variableName == "nt") {
inputFile >> value;
nt = int(value);
} else if (variableName == "r") {
inputFile >> value;
r = int(value);
} else if (variableName == "df") {
inputFile >> df;
} else if (variableName == "xmin") {
inputFile >> xmin;
} else if (variableName == "xmax") {
inputFile >> xmax;
} else if (variableName == "ymin") {
inputFile >> ymin;
} else if (variableName == "ymax") {
inputFile >> ymax;
} else if (variableName == "zmin") {
inputFile >> zmin;
} else if (variableName == "zmax") {
inputFile >> zmax;
} else if (variableName == "rho") {
int m_in = 0;
while (models[m_in] != 'M') m_in += 1;
rho = new float[nzs[m_in]];
for (int i = 0; i < nzs[m_in]; i++) {
inputFile >> rho[i];
}
} else if (variableName == "Q") {
inputFile >> Q;
} else if (variableName == "gamma") {
inputFile >> gamma;
} else if (variableName == "E_He") {
inputFile >> E_He;
} else if (variableName == "Cv") {
inputFile >> Cv;
} else if (variableName == "alpha") {
inputFile >> alpha0;
} else if (variableName == "beta") {
for (int i = 0; i < 3; i++) {
inputFile >> beta[i];
}
} else if (variableName == "gamma_down") {
for (int i = 0; i < 3*3; i++) {
inputFile >> gamma_down[i];
}
} else if (variableName == "R") {
inputFile >> R;
} else if (variableName == "periodic") {
string tf;
inputFile >> tf;
if (tf == "t" || tf == "T") {
periodic = true;
} else {
periodic = false;
}
} else if (variableName == "burning") {
string tf;
inputFile >> tf;
if (tf == "t" || tf == "T") {
burning = true;
} else {
burning = false;
}
} else if (variableName == "dprint") {
inputFile >> value;
dprint = int(value);
} else if (variableName == "outfile") {
string f;
inputFile >> f;
strncpy(outfile, f.c_str(), sizeof(outfile));
outfile[sizeof(outfile) - 1] = 0;
} else if (variableName == "n_print_levels") {
inputFile >> value;
n_print_levels = int(value);
print_levels = new int[n_print_levels];
} else if (variableName == "print_levels") {
for (int i = 0; i < n_print_levels; i++) {
inputFile >> value;
print_levels[i] = int(value);
}
}
}
// data validation
if (nx < 0 || nx > 1e5) {
printf("Invalid nx: %d\n", nx);
exit(EXIT_FAILURE);
}
if (ny < 0 || ny > 1e5) {
printf("Invalid ny: %d\n", ny);
exit(EXIT_FAILURE);
}
if (nlevels < 0 || nlevels > 1e2) {
printf("Invalid nlevels: %d\n", nlevels);
exit(EXIT_FAILURE);
}
for (int i = 0; i < nlevels; i++) {
if (nzs[i] < 0 || nzs[i] > nzs[nlevels-1]) {
printf("Invalid nzs[%d]: %d\n", i, nzs[i]);
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < nlevels; i++) {
if (models[i] != 'S' && models[i] != 'M' && models[i] != 'C' && models[i] != 'L') {
printf("Invalid model[%d]: %c\n", i, models[i]);
exit(EXIT_FAILURE);
}
}
/*if (models[0] == 'S') {
if (models[1] != 'M') {
printf("Single layer SWE level must be followed by multilayer SWE level.");
exit(EXIT_FAILURE);
}
for (int i = 1; i < nlevels; i++) {
if (models[i] == 'S') {
printf("Can only have one single layer SWE level at coarsest level.");
exit(EXIT_FAILURE);
}
}
}*/
// locate index of first multilayer SWE level
int m_in = 0;
while (models[m_in] != 'M') m_in += 1;
for (int i = m_in+1; i < nlevels; i++) {
if (models[i] != 'M' && models[i] != 'C' && models[i] != 'L') {
printf("Multilayer SWE level can only be followed by multilayer, compressible or Low Mach levels.\n");
printf("Models: %c, %c\n", models[m_in], models[m_in+1]);
exit(EXIT_FAILURE);
}
}
if (ng < 0 || ng > 1e2) {
printf("Invalid ng: %d\n", ng);
exit(EXIT_FAILURE);
}
if (nt < 0 || nt > 1e8) {
printf("Invalid nt: %d\n", nt);
exit(EXIT_FAILURE);
}
if (r < 0 || r > 1e2) {
printf("Invalid r: %d\n", r);
exit(EXIT_FAILURE);
}
if (df < 0.0 || df > 1.0) {
printf("Invalid df: %f\n", df);
exit(EXIT_FAILURE);
}
if (xmin < -1.0e5 || xmin > 1.0e5) {
printf("Invalid xmin: %f\n", xmin);
exit(EXIT_FAILURE);
}
if (xmax < -1.0e5 || xmax > 1.0e5 || xmax < xmin) {
printf("Invalid xmax: %f\n", xmax);
exit(EXIT_FAILURE);
}
if (ymin < -1.0e5 || ymin > 1.0e5) {
printf("Invalid ymin: %f\n", ymin);
exit(EXIT_FAILURE);
}
if (ymax < -1.0e5 || ymax > 1.0e5 || ymax < ymin) {
printf("Invalid ymax: %f\n", ymax);
exit(EXIT_FAILURE);
}
if (zmin < -1.0e5 || zmin > 1.0e5) {
printf("Invalid zmin: %f\n", zmin);
exit(EXIT_FAILURE);
}
if (zmax < -1.0e5 || zmax > 1.0e5 || zmax < zmin) {
printf("Invalid zmax: %f\n", zmax);
exit(EXIT_FAILURE);
}
for (int i = 0; i < nzs[0]; i++) {
if (rho[i] < 0 || rho[i] > 1.0e8) {
printf("Invalid rho[%d]: %f\n", i, rho[i]);
exit(EXIT_FAILURE);
}
}
if (Q < -1.0e8 || Q > 1.0e8) {
printf("Invalid Q: %f\n", Q);
exit(EXIT_FAILURE);
}
if (gamma < 0.0 || gamma > 1.0e2) {
printf("Invalid gamma: %f\n", gamma);
exit(EXIT_FAILURE);
}
if (E_He < 0.0 || E_He > 1.0e8) {
printf("Invalid E_He: %f\n", E_He);
exit(EXIT_FAILURE);
}
if (Cv < 0.0 || Cv > 1.0e2) {
printf("Invalid Cv: %f\n", Cv);
exit(EXIT_FAILURE);
}
for (int i = 0; i < 3; i++) {
if (beta[i] < -1.0 || beta[i] > 1.0) {
printf("Invalid beta[%d]: %f\n", i, beta[i]);
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 3*3; i++) {
if (gamma_down[i] < -1.0e2 || gamma_down[i] > 1.0e2) {
printf("Invalid gamma_down[%d]: %f\n", i, gamma_down[i]);
exit(EXIT_FAILURE);
}
}
if (R < 0.0) {
printf("Invalid R: %f\n", R);
exit(EXIT_FAILURE);
}
if (dprint < 0 || dprint > 1e9) {
printf("Invalid dprint: %d\n", dprint);
exit(EXIT_FAILURE);
}
if (n_print_levels < 0 || n_print_levels > nlevels) {
printf("Invalid n_print_levels: %d\n", n_print_levels);
exit(EXIT_FAILURE);
}
for (int i = 0; i < n_print_levels; i++) {
if (print_levels[i] < 0 || print_levels[i] > nlevels-1) {
printf("Invalid print_level[%d]: %d\n", i, print_levels[i]);
exit(EXIT_FAILURE);
}
}
//inputFile.close();
float M = 1;
alpha0 = sqrt(1 - 2 * M / R);
nxs[0] = nx;
nys[0] = ny;
for (int i = 1; i < nlevels; i++) {
nxs[i] = int(r * df * nxs[i-1]);
nys[i] = int(r * df * nys[i-1]);
}
xs = new float[nxs[m_in]];
for (int i = 0; i < nxs[m_in]; i++) {
xs[i] = xmin + (i-ng) * (xmax - xmin) / (nxs[m_in]-2*ng);
}
ys = new float[nys[m_in]];
for (int i = 0; i < nys[m_in]; i++) {
ys[i] = ymin + (i-ng) * (ymax - ymin) / (nys[m_in]-2*ng);
}
dx = (xmax - xmin) / (nxs[0]-2*ng);
dy = (ymax - ymin) / (nys[0]-2*ng);
// need to define this in such a way that it is calculated using layer separation on first compressible grid
int c_in = nlevels;
if (models[nlevels-1] == 'C') {
while(models[c_in-1] == 'C') c_in -= 1;
}
dz = (zmax - zmin) / (nzs[c_in] - 1.0);
dz *= pow(r, c_in);
float cfl = 0.5; // cfl number?
dt = cfl * min(dx, min(dy, dz));
// find inverse of gamma
for (int i = 0; i < 3*3; i++) {
gamma_up[i] = gamma_down[i];
}
Sea::invert_mat(gamma_up, 3, 3);
cout << "nxs, nys, nzs, vec_dims:\n";
for (int i = 0; i < nlevels; i++) {
cout << nxs[i] << ' ' << nys[i] << ' ' << nzs[i] << ' ' << vec_dims[i] << '\n';
}
Us = new float*[nlevels];
p_const = new float[nzs[m_in]];
//cout << "Made p_const: " << p_const[0] << '\n';
for (int i = 0; i < nlevels; i++) {
Us[i] = new float[nxs[i]*nys[i]*nzs[i]*vec_dims[i]];
for (int j = 0; j < nxs[i]*nys[i]*nzs[i]*vec_dims[i]; j++) {
Us[i][j] = 0.0;
}
}
matching_indices = new int[4 * (nlevels-1)];
for (int i = 0; i < nlevels-1; i++) {
matching_indices[i*4] = int(ceil(nxs[i]*0.5*(1-df)));
matching_indices[i*4+1] = int(floor(nxs[i]*0.5*(1+df)));
matching_indices[i*4+2] = int(ceil(nys[i]*0.5*(1-df)));
matching_indices[i*4+3] = int(floor(nys[i]*0.5*(1+df)));
cout << "Matching indices: " << matching_indices[i*4] << ',' << matching_indices[i*4+1] << ',' << matching_indices[i*4+2] << ',' << matching_indices[i*4+3] << '\n';
cout << "matching_indices vs nxf: " <<
matching_indices[i*4+1] - matching_indices[i*4] << ',' << nxs[i+1] << '\n';
}
strncpy(paramfile, filename, sizeof(paramfile));
cout << "Made a Sea.\n";
}
// copy constructor
Sea::Sea(const Sea &seaToCopy)
: nx(seaToCopy.nx), ny(seaToCopy.ny), ng(seaToCopy.ng), zmin(seaToCopy.zmin), zmax(seaToCopy.zmax), nt(seaToCopy.nt), r(seaToCopy.r), dx(seaToCopy.dx), dy(seaToCopy.dy), dz(seaToCopy.dz), dt(seaToCopy.dt), df(seaToCopy.df), gamma(seaToCopy.gamma), E_He(seaToCopy.E_He), Cv(seaToCopy.Cv), alpha0(seaToCopy.alpha0), R(seaToCopy.R), periodic(seaToCopy.periodic), burning(seaToCopy.burning), dprint(seaToCopy.dprint), n_print_levels(seaToCopy.n_print_levels)
{
/**
copy constructor
*/
xs = new float[nx];
for (int i = 0; i < nx; i++) {
xs[i] = seaToCopy.xs[i];
}
ys = new float[ny];
for (int i = 0; i < ny; i++) {
ys[i] = seaToCopy.ys[i];
}
rho = new float[nzs[0]];
for (int i = 0; i < nzs[0]; i++) {
rho[i] = seaToCopy.rho[i];
}
Q = seaToCopy.Q;
for (int i = 0; i < 3*nx*ny; i++) {
beta[i] = seaToCopy.beta[i];
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gamma_down[i*3+j] = seaToCopy.gamma_down[i*3+j];
gamma_up[i*3+j] = seaToCopy.gamma_up[i*3+j];
}
}
for (int i = 0; i < 2*2; i++) {
matching_indices[i] = seaToCopy.matching_indices[i];
}
}
Sea::~Sea() {
/**
Deconstructor
*/
delete[] xs;
delete[] ys;
delete[] rho;
delete[] models;
delete[] nxs;
delete[] nys;
delete[] nzs;
delete[] vec_dims;
delete[] matching_indices;
delete[] print_levels;
delete[] p_const;
for (int i = 0; i < nlevels; i++) {
delete[] Us[i];
}
}
void Sea::initial_swe_data(float * D0, float * Sx0, float * Sy0) {
/**
Initialise D, Sx, Sy and Q on coarsest multilayer SWE grid.
*/
// find coarsest multilayer SWE grid
// TODO: make sure ensure this exists when initialise object
int m_in = 0;
while (models[m_in] != 'M') m_in += 1;
for (int i = 0; i < nxs[m_in]*nys[m_in]*nzs[m_in]; i++) {
// it's on a SWE grid so know vec_dim = 4
Us[m_in][i*4] = D0[i];
Us[m_in][i*4+1] = Sx0[i];
Us[m_in][i*4+2] = Sy0[i];
Us[m_in][i*4+3] =
0.9 * float(i) / (nxs[m_in]*nys[m_in]*nzs[m_in]);
}
bcs(Us[m_in], nxs[m_in], nys[m_in], nzs[m_in], vec_dims[m_in]);
cout << "Set initial data.\n";
}
void Sea::initial_compressible_data(float * D0, float * Sx0, float * Sy0, float * Sz0, float * tau0) {
/**
Initialise D, Sx, Sy, Sz and tau on coarsest compressible grid.
*/
// find coarsest multilayer SWE grid
// TODO: make sure ensure this exists when initialise object
int c_in = 0;
while (models[c_in] != 'C') c_in += 1;
for (int i = 0; i < nxs[c_in]*nys[c_in]*nzs[c_in]; i++) {
// it's on a compressible grid so know vec_dim = 6
Us[c_in][i*6] = D0[i];
Us[c_in][i*6+1] = Sx0[i];
Us[c_in][i*6+2] = Sy0[i];
Us[c_in][i*6+3] = Sz0[i];
Us[c_in][i*6+4] = tau0[i];
Us[c_in][i*6+5] =
0.9 * float(i) / (nxs[c_in]*nys[c_in]*nzs[c_in]);
}
bcs(Us[c_in], nxs[c_in], nys[c_in], nzs[c_in], vec_dims[c_in]);
cout << "Set initial data.\n";
}
void Sea::print_inputs() {
/**
Print some input and runtime parameters to screen.
*/
cout << "\nINPUT DATA\n" << "----------\n";
cout << "(nx, ny, nz, ng) \t(" << nxs[0] << ',' << nys[0] << ',' << nzs[0] << ',' << ng << ")\n";
cout << "nt \t\t\t" << nt << '\n';
cout << "(r, df, nlevels) \t(" << r << ',' << df << ',' << nlevels << ")\n";
cout << "dprint \t\t\t" << dprint << '\n';
cout << "(dx, dy, dz, dt) \t(" << dx << ',' << dy << ',' << dz << ',' << dt << ")\n";
cout << "rho \t\t\t" << rho[0] << ',' << rho[1]<< ',' << rho[2] << "\n";
cout << "Q \t\t\t" << Q << '\n';
cout << "E_He \t\t\t" << E_He << '\n';
cout << "Cv \t\t\t" << Cv << '\n';
cout << "alpha0, R \t\t" << alpha0 << ", " << R << '\n';
cout << "beta \t\t\t(" << beta[0] << ',' << beta[1] << ',' << beta[2] << ")\n";
cout << "gamma_down \t\t((" << gamma_down[0] << ',' << gamma_down[1] << ',' << gamma_down[2] << "),(" << gamma_down[3] << ',' << gamma_down[4] << ',' << gamma_down[5] << "),(" << gamma_down[6] << ',' << gamma_down[7] << ',' << gamma_down[8] << "))\n";
cout << "burning \t\t" << burning << '\n';
cout << "outfile \t\t" << outfile << "\n";
cout << "print levels \t\t";
for (int i = 0; i < n_print_levels; i++) {
cout << print_levels[i] << ' ';
}
cout << "\n\n";
}
void Sea::bcs(float * grid, int n_x, int n_y, int n_z, int vec_dim) {
/**
Enforce boundary conditions on grid of quantities with dimension vec_dim.
*/
if (false) {
for (int z = 0; z < n_z; z++) {
for (int y = 0; y < n_y; y++){
for (int g = 0; g < ng; g++) {
for (int l = 0; l < vec_dim; l++) {
grid[((z * n_y + y) * n_x + g) * vec_dim + l] =
grid[((z * n_y + y) * n_x + (n_x-2*ng+g)) * vec_dim + l];
grid[((z * n_y + y) * n_x + (n_x-ng+g)) * vec_dim + l] =
grid[((z * n_y + y) * n_x + ng+g) * vec_dim + l];
}
}
}
for (int g = 0; g < ng; g++) {
for (int x = 0; x < n_x; x++){
for (int l = 0; l < vec_dim; l++) {
grid[((z * n_y + g) * n_x + x) * vec_dim + l] =
grid[((z * n_y + n_y-2*ng+g) * n_x + x) * vec_dim + l];
grid[((z * n_y + n_y-ng+g) * n_x + x) * vec_dim + l] =
grid[((z * n_y + ng+g) * n_x + x) * vec_dim + l];
}
}
}
}
} else { // outflow
for (int z = 0; z < n_z; z++) {
for (int y = 0; y < n_y; y++){
for (int g = 0; g < ng; g++) {
for (int l = 0; l < vec_dim; l++) {
grid[((z * n_y + y) * n_x + g) * vec_dim + l] =
grid[((z * n_y + y) * n_x + ng) * vec_dim + l];
grid[((z * n_y + y) * n_x + (n_x-1-g)) * vec_dim + l] =
grid[((z * n_y + y) * n_x + (n_x-1-ng)) * vec_dim + l];
}
}
}
for (int g = 0; g < ng; g++) {
for (int x = 0; x < n_x; x++){
for (int l = 0; l < vec_dim; l++) {
grid[((z * n_y + g) * n_x + x) * vec_dim + l] =
grid[((z * n_y + ng) * n_x + x) * vec_dim + l];
grid[((z * n_y + n_y-1-g) * n_x + x) * vec_dim + l] =
grid[((z * n_y + n_y-1-ng) * n_x + x) * vec_dim + l];
}
}
}
}
}
}
void Sea::run(MPI_Comm comm, MPI_Status * status, int rank, int size,
int tstart) {
/**
run code
*/
int m_in = 0;
while (models[m_in] != 'M') m_in += 1;
// hack for now
float * Qs = new float[nzs[m_in]];
for (int i = 0; i < nzs[m_in]; i++) {
Qs[i] = Q;
}
//cout << "p_const: " << p_const[0] << ' ' << p_const[1] << ' '<< p_const[2] << '\n';
cuda_run(beta, Us, rho, Qs,
nxs, nys, nzs, nlevels, models, vec_dims,
ng, nt, alpha0, R, gamma, E_He, Cv, zmin, dx, dy, dz, dt, burning,
periodic, dprint,
outfile, paramfile, comm, *status, rank, size,
matching_indices, r,
n_print_levels, print_levels, tstart, p_const);
delete[] Qs;
}