-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfluxfunction.cpp
231 lines (187 loc) · 7.96 KB
/
fluxfunction.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
/*
* This file is part of Vlasiator.
* Copyright 2010-2016 Finnish Meteorological Institute
*
* For details of usage, see the COPYING file and read the "Rules of the Road"
* at http://www.physics.helsinki.fi/vlasiator/
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <mpi.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "particles/field.h"
#include "particles/readfields.h"
using namespace std;
// Calculate fluxfunction by integrating along -y/z boundary first,
// and then going along y/z-direction.
std::vector<double> computeFluxUp(Field& B, int outerBoundary, int innerBoundaryRE) {
// Create fluxfunction-field to be the same shape as B
std::vector<double> flux(B.dimension[0]->cells * B.dimension[1]->cells * B.dimension[2]->cells);
bool eqPlane = B.dimension[1]->cells > 1;
int yCoord = eqPlane ? 1 : 2;
long double tmp_flux=0.;
long double bottom_flux=0.;
// First, fill the y/z=3 cells
// Then integrate in y/z direction
for(int x = B.dimension[0]->cells - (outerBoundary+1); x > outerBoundary + 1; x--) {
int y = eqPlane ? outerBoundary : 0;
int z = eqPlane ? 0 : outerBoundary;
Vec3d bval = B.getCell(x,y,z);
bottom_flux -= bval[yCoord] * B.dx[0];
flux[B.dimension[0]->cells * outerBoundary + x] = tmp_flux;
tmp_flux = bottom_flux
for(int i=outerBoundary + 1; i < B.dimension[yCoord]->cells - outerBoundary; i++) {
y = eqPlane ? i : 0;
z = eqPlane ? 0 : i;
bval = B.getCell(x,y,z);
tmp_flux -= bval[0] * B.dx[yCoord];
flux[B.dimension[0]->cells * i + x] = tmp_flux;
}
}
return flux;
}
// Calculate fluxfunction by integrating along +y/z boundary first,
// and then going along negative y/z-direction.
std::vector<double> computeFluxDown(Field& B, int outerBoundary, int innerBoundaryRE) {
// Create fluxfunction-field to be the same shape as B
std::vector<double> flux(B.dimension[0]->cells * B.dimension[1]->cells * B.dimension[2]->cells);
bool eqPlane = B.dimension[1]->cells > 1;
int yCoord = eqPlane ? 1 : 2;
long double tmp_flux=0.;
long double top_flux=0.;
// Calculate flux-difference between bottom and top edge
// of +x boundary (so that values are consistent with computeFluxUp)
for(int i = outerBoundary; i < B.dimension[yCoord]->cells - outerBoundary; i++) {
int y = eqPlane ? i : 0;
int z = eqPlane ? 0 : i;
Vec3d bval = B.getCell(B.dimension[0]->cells - (outerBoundary + 1), y, z);
tmp_flux -= bval[0]*B.dx[yCoord];
}
// First, fill the y/z = max - 4 cells
// Then integrate in -y/z direction
for(int x = B.dimension[0]->cells - (outerBoundary + 2); x >= outerBoundary; x--) {
int y = eqPlane ? B.dimension[1]->cells - (outerBoundary + 1) : 0;
int z = eqPlane ? 0 : B.dimension[2]->cells - (outerBoundary + 1);
Vec3d bval = B.getCell(x, y, z);
top_flux -= bval[yCoord] * B.dx[0];
flux[B.dimension[0]->cells * (B.dimension[yCoord]->cells - (outerBoundary + 1)) + x] = top_flux;
tmp_flux = top_flux
for(int i=B.dimension[yCoord]->cells - (outerBoundary + 2); i > 0; i--) {
y = eqPlane ? i : 0;
z = eqPlane ? 0 : i;
bval = B.getCell(x, y, z);
tmp_flux += bval[0] * B.dx[yCoord];
flux[B.dimension[0]->cells * i + x] = tmp_flux;
}
}
return flux;
}
// Calculate fluxfunction by integrating along -x from the right boundary
std::vector<double> computeFluxLeft(Field& B, int outerBoundary, int innerBoundaryRE) {
// Create fluxfunction-field to be the same shape as B
std::vector<double> flux(B.dimension[0]->cells * B.dimension[1]->cells * B.dimension[2]->cells);
bool eqPlane = B.dimension[1]->cells > 1;
int yCoord = eqPlane ? 1 : 2;
long double tmp_flux=0.;
long double right_flux=0.;
// First calculate flux difference to bottom right corner
// Now, for each row, integrate in -z-direction.
for(int i=0; i < B.dimension[yCoord]->cells - outerBoundary; i++) {
int y = eqPlane ? i : 0;
int z = eqPlane ? 0 : i;
Vec3d bval = B.getCell(B.dimension[0]->cells - (outerBoundary + 1), y, z);
right_flux -= bval[0] * B.dx[yCoord];
flux[B.dimension[0]->cells * i + B.dimension[0]->cells - (outerboundary + 1)] = tmp_flux;
tmp_flux = right_flux;
for(int x = B.dimension[0]->cells - (outerBoundary + 2); x >= outerBoundary; x--) {
bval = B.getCell(x,y,z);
tmp_flux -= bval[yCoord] * B.dx[0];
flux[B.dimension[0]->cells * i + x] = tmp_flux;
}
}
return flux;
}
// Get a median of 3 values (branch-free!)
static double median3(double a, double b, double c) {
return max(min(a,b), min(max(a,b),c));
}
int main(int argc, char** argv) {
// MPI::Init(argc, argv);
if(argc < 3) {
cerr << "Syntax: fluxfunction input.vlsv output.bin" << endl;
cerr << "Output will be two files: output.bin and output.bin.bov." << endl;
cerr << "Point visit to the BOV file." << endl;
return 1;
}
string inFile(argv[1]);
string outFile(argv[2]);
// TODO: Don't uselessly read E, we really only care about B.
Field E,B,V;
readfields(inFile.c_str(),E,B,V,false);
// Make sure we are working with a 2D simulation here.
if(B.dimension[0]->cells > 1 && B.dimension[1]->cells > 1 && B.dimension[2]->cells > 1) {
cerr << "This is a 3D simulation output. Flux function calculation only makes sense for 2D data."
<< endl;
exit(1);
}
cerr << "File read, calculating flux function..." << endl;
std::vector<double> fluxUp, fluxDown, fluxLeft;
fluxUp = computeFluxUp(B, 2, 5);
fluxDown = computeFluxDown(B, 2, 5);
fluxLeft = computeFluxLeft(B, 2, 5);
for(unsigned int i=0; i<fluxUp.size(); i++) {
// Calc median flux value;
double a = fluxUp[i];
double b = fluxDown[i];
double c = fluxLeft[i];
fluxUp[i] = median3(a,b,c);
}
cerr << "Done. Writing output..." << endl;
// Write output as a visit-compatible BOV file
int fd = open(outFile.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0644);
if(!fd || fd == -1) {
cerr << "Error: cannot open output file " << outFile << ": " << strerror(errno) << endl;
return 1;
}
size_t size=B.dimension[0]->cells*B.dimension[1]->cells*B.dimension[2]->cells*sizeof(double);
// Write binary blob
for(ssize_t remain=size; remain > 0; ) {
remain -= write(fd, ((char*) &(fluxUp[0]))+remain-size, remain);
}
close(fd);
// Write BOV header
string outBov = outFile + ".bov";
FILE* f=fopen(outBov.c_str(), "w");
if(!f) {
cerr<< "Error: unable to write BOV ascii file " << outBov << ":" << strerror(errno) << endl;
return 1;
}
fprintf(f, "TIME: %lf\n", B.time);
fprintf(f, "DATA_FILE: %s\n", outFile.c_str());
fprintf(f, "DATA_SIZE: %i %i %i\n", B.dimension[0]->cells, B.dimension[1]->cells, B.dimension[2]->cells);
fprintf(f, "DATA_FORMAT: DOUBLE\nVARIABLE: fluxfunction\nDATA_ENDIAN: LITTLE\nCENTERING: zonal\n");
fprintf(f, "BRICK_ORIGIN: %lf %lf %lf\n", B.dimension[0]->min, B.dimension[1]->min, B.dimension[2]->min);
fprintf(f, "BRICK_SIZE: %lf %lf %lf\n", B.dimension[0]->max - B.dimension[0]->min, B.dimension[1]->max - B.dimension[1]->min, B.dimension[2]->max - B.dimension[2]->min);
fprintf(f, "DATA_COMPONENTS: 1\n");
fclose(f);
return 0;
}