-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdumpToFile.cpp
More file actions
179 lines (141 loc) · 5.53 KB
/
dumpToFile.cpp
File metadata and controls
179 lines (141 loc) · 5.53 KB
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
// ***********************************************************************************
// Idefix MHD astrophysical code
// Copyright(C) Geoffroy R. J. Lesur <geoffroy.lesur@univ-grenoble-alpes.fr>
// and other code contributors
// Licensed under CeCILL 2.1 License, see COPYING for more information
// ***********************************************************************************
#include <string>
#include <cstdio>
#include "../idefix.hpp"
#include "dataBlock.hpp"
#include "version.hpp"
#include "fluid.hpp"
#define NAMESIZE 16
#define HEADERSIZE 128
void DataBlock::WriteVariable(FILE* fileHdl, int ndim, int *dim,
char *name, void* data) {
int ntot = 1; // Number of elements to be written
int size = sizeof(real); // Elements always assumed to be real
// Write field name
fwrite (name, sizeof(char), NAMESIZE, fileHdl);
// Write dimensions of array
fwrite(&ndim, 1, sizeof(int), fileHdl);
for(int n = 0 ; n < ndim ; n++) {
fwrite(dim+n, 1, sizeof(int), fileHdl);
ntot = ntot * dim[n];
}
// Write raw data
fwrite(data, ntot, size, fileHdl);
}
// dump the current dataBlock to a file (mainly used for debug purposes)
void DataBlock::DumpToFile(std::string filebase) {
FILE *fileHdl;
int dims[5];
char fieldName[NAMESIZE+1]; // +1 is just in case
static int n=0;
// TODO(lesurg) Make datablock a friend of hydro to get the Riemann flux?
//IdefixArray4D<real>::host_mirror_type locFlux = Kokkos::create_mirror_view(Kokkos::HostSpace(),
// this->hydro->FluxRiemann);
//Kokkos::deep_copy(locFlux, this->FluxRiemann);
#if MHD == YES
IdefixArray4D<real>::host_mirror_type locJ;
if(hydro->haveCurrent) {
locJ = Kokkos::create_mirror_view(Kokkos::HostSpace(), this->hydro->J);
Kokkos::deep_copy(locJ, this->hydro->J);
}
#endif
// Data format:
// Similar to dump files, but no aggregation through cores
// and include boundaries
// Header [HEADER SIZE]
// Field Name [NAMESIZE]
// # of dimensions (integet)
// dimensions (n*integer)
// raw data (real)
// FieldName [NAMESIZE]...
// FieldName [NAMESIZE] = "eof"
std::string dot = std::string(".");
std::string ext = std::string("idfx");
std::string filename = filebase + dot + std::to_string(n)
+ dot + std::to_string(idfx::prank) + dot + ext;
n++;
fileHdl = fopen(filename.c_str(),"wb");
// Write Header
char header[HEADERSIZE];
std::snprintf(header, HEADERSIZE, "Idefix %s Debug DataBlock", IDEFIX_VERSION);
fwrite (header, sizeof(char), HEADERSIZE, fileHdl);
// Write Vc
IdefixArray4D<real>::host_mirror_type locVc = Kokkos::create_mirror_view(this->hydro->Vc);
Kokkos::deep_copy(locVc,this->hydro->Vc);
dims[0] = this->np_tot[IDIR];
dims[1] = this->np_tot[JDIR];
dims[2] = this->np_tot[KDIR];
dims[3] = NVAR;
std::snprintf(fieldName,NAMESIZE,"Vc");
WriteVariable(fileHdl, 4, dims, fieldName, locVc.data());
if (this->gravity->haveSelfGravityPotential) {
IdefixArray3D<real>::host_mirror_type locPot = Kokkos::create_mirror_view(this->gravity->phiP);
Kokkos::deep_copy(locPot, this->gravity->phiP);
dims[3] = 1;
std::snprintf(fieldName,NAMESIZE,"Pot");
WriteVariable(fileHdl, 4, dims, fieldName, locPot.data());
}
// Write Flux
/*
nx1=this->np_tot[IDIR];
nx2=this->np_tot[JDIR];
nx3=this->np_tot[KDIR];
nv=NVAR;
fwrite(&nx1, sizeof(real),1,fileHdl);
fwrite(&nx2, sizeof(real),1,fileHdl);
fwrite(&nx3, sizeof(real),1,fileHdl);
fwrite(&nv, sizeof(real),1,fileHdl);
fwrite(locFlux.data(), sizeof(real), nx1*nx2*nx3*nv, fileHdl);
*/
// Write Vs
#if MHD == YES
// Write Vs
IdefixArray4D<real>::host_mirror_type locVs = Kokkos::create_mirror_view(Kokkos::HostSpace(),
this->hydro->Vs);
Kokkos::deep_copy(locVs,this->hydro->Vs);
dims[0] = this->np_tot[IDIR]+IOFFSET;
dims[1] = this->np_tot[JDIR]+JOFFSET;
dims[2] = this->np_tot[KDIR]+KOFFSET;
dims[3] = DIMENSIONS;
std::snprintf(fieldName,NAMESIZE,"Vs");
WriteVariable(fileHdl, 4, dims, fieldName, locVs.data());
// Write EMFs
dims[0] = this->np_tot[IDIR];
dims[1] = this->np_tot[JDIR];
dims[2] = this->np_tot[KDIR];
std::snprintf(fieldName,NAMESIZE,"Ex3");
IdefixArray3D<real>::host_mirror_type locE = Kokkos::create_mirror_view(Kokkos::HostSpace(),
this->hydro->emf->ez);
Kokkos::deep_copy(locE,this->hydro->emf->ez);
WriteVariable(fileHdl, 3, dims, fieldName, locE.data());
#if DIMENSIONS == 3
std::snprintf(fieldName,NAMESIZE,"Ex1");
Kokkos::deep_copy(locE,this->hydro->emf->ex);
WriteVariable(fileHdl, 3, dims, fieldName, locE.data());
std::snprintf(fieldName,NAMESIZE,"Ex2");
Kokkos::deep_copy(locE,this->hydro->emf->ey);
WriteVariable(fileHdl, 3, dims, fieldName, locE.data());
#endif
if(hydro->haveCurrent) {
IdefixArray4D<real>::host_mirror_type locJ = Kokkos::create_mirror_view(Kokkos::HostSpace(),
this->hydro->J);
Kokkos::deep_copy(locJ,this->hydro->J);
dims[0] = this->np_tot[IDIR];
dims[1] = this->np_tot[JDIR];
dims[2] = this->np_tot[KDIR];
dims[3] = 3;
std::snprintf(fieldName,NAMESIZE,"J");
WriteVariable(fileHdl, 4, dims, fieldName, locJ.data());
}
#endif
// Write end of file
std::snprintf(fieldName,NAMESIZE,"eof");
dims[0] = 1;
WriteVariable(fileHdl, 1, dims, fieldName, locVc.data());
fclose(fileHdl);
}