-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.cpp
294 lines (257 loc) · 7.87 KB
/
config.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
#include "config.h"
#include "rgrid/darrayscatter.h"
#include "kutils/config.h"
#include <fstream>
using namespace rgrid;
using namespace std;
void Config::readConfig(string file) {
#ifdef USE_MPI
MPI_File fh;
MPI_Offset filesize;
if (0 != MPI_File_open(MPI_COMM_WORLD, file.c_str(), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh))
throw logic_error("(MPI) Couldn't open \"" + file + "\"");
if (0 != MPI_File_get_size(fh, &filesize))
throw logic_error("(MPI) Couldn't get \"" + file + "\" size");
vector<char> buf(filesize);
MPI_File_read_all(fh, &buf[0], filesize, MPI_CHAR, MPI_STATUS_IGNORE);
MPI_File_close(&fh);
std::istringstream ss(std::string(&buf[0],static_cast<size_t>(filesize)));
istream& is = ss;
#else
fstream fs;
fs.open(file.c_str(), ios_base::in);
if (!fs.is_open()) throw logic_error("Couldn't open \"" + file + "\"");
istream& is = fs;
#endif
kconf.parseStream(is);
int_t order = kconf.get<int_t>("order");
if (!(order > 0 && order % 2 == 0)) throw logic_error("Wrong order");
ho = order / 2;
fdc.calc(ho);
std::vector<int> a;
kconf.getEntry("nodes").getIntArray(a);
// ghost nodes -1 and nx are boundary nodes on the grid
// in input nx and ny is number of nodes without ghost
nx = a.at(0);
ny = a.at(1);
std::vector<double> b;
b.clear();
kconf.getEntry("origin").getDoubleArray(b);
oy = b.at(0);
b.clear();
kconf.getEntry("space_step").getDoubleArray(b);
dx = b.at(0);
dy = b.at(1);
steps = kconf.get<int_t>("time_steps");
dt = kconf.get<real_t>("time_step");
ex = nx * dx;
ey = (ny + 1) * dy + oy;
saveStep = kconf.get<int_t>("save_every");
max_pml = kconf.get<real_t>("pml_max");
pml_len = kconf.get<real_t>("pml_nodes");
std::vector<std::string> s;
std::string boundary;
boundary = kconf.get<std::string>("rad_boundary");
isPmlRad = (boundary == "absorb");
boundary = kconf.get<std::string>("top_boundary");
isPmlTop = (boundary == "absorb");
boundary = kconf.get<std::string>("bot_boundary");
isPmlBot = (boundary == "absorb");
a.clear();
kconf.getEntry("global_parts").getIntArray(a);
gx = a.at(0);
gy = a.at(1);
a.clear();
kconf.getEntry("local_parts").getIntArray(a);
lx = a.at(0);
ly = a.at(1);
string sourcesFile = kconf.get<std::string>("sources_position");
string receiversFile = kconf.get<std::string>("receivers_position");
string KFile = kconf.get<std::string>("bulk_modulus");
s.clear();
kconf.getEntry("density").getStringArray(s);
string rhoXFile = s.at(0);
string rhoYFile = s.at(1);
rcvsOut = kconf.get<std::string>("receivers_output");
#ifndef USE_MPI
fs.close();
#endif
readSources(sourcesFile);
readReceivers(receiversFile);
readK(KFile);
readRhoX(rhoXFile);
readRhoY(rhoYFile);
}
void Config::readSources(string file) {
std::istringstream ss;
#ifdef USE_MPI
{
MPI_File fh;
MPI_Offset filesize;
if (0 != MPI_File_open(MPI_COMM_WORLD, file.c_str(), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh))
throw logic_error("(MPI) Couldn't open \"" + file + "\"");
if (0 != MPI_File_get_size(fh, &filesize))
throw logic_error("(MPI) Couldn't get \"" + file + "\" size");
vector<char> buf(filesize);
MPI_File_read_all(fh, &buf[0], filesize, MPI_CHAR, MPI_STATUS_IGNORE);
MPI_File_close(&fh);
ss.str(std::string(&buf[0],static_cast<size_t>(filesize)));
}
#else
{
fstream fs;
fs.open(file.c_str(), ios_base::in);
if (!fs.is_open()) throw logic_error("Couldn't open \"" + file + "\"");
std::string contents((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
fs.close();
ss.str(contents);
}
#endif
while (1) {
Source e;
string file_src;
ss >> file_src >> e.y;
if (ss.eof()) break;
if (!(oy + dy <= e.y && e.y <= ey - dy)) throw logic_error("Wrong source position Y");
// find nodes
e.j = static_cast<int_t>((e.y - oy) / dy) - 1;
// distance from node with indexes i,j,k to current node
real_t yl = e.y - oy - (e.j + 1) * dy;
real_t yr = dy - yl;
real_t volume = dy;
e.c[L] = yr / volume;
e.c[R] = yl / volume;
istringstream ss_src;
#ifdef USE_MPI
{
string& file = file_src;
MPI_File fh;
MPI_Offset filesize;
if (0 != MPI_File_open(MPI_COMM_WORLD, file.c_str(), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh))
throw logic_error("(MPI) Couldn't open \"" + file + "\"");
if (0 != MPI_File_get_size(fh, &filesize))
throw logic_error("(MPI) Couldn't get \"" + file + "\" size");
vector<char> buf(filesize);
MPI_File_read_all(fh, &buf[0], filesize, MPI_CHAR, MPI_STATUS_IGNORE);
MPI_File_close(&fh);
ss_src.str(std::string(&buf[0],static_cast<size_t>(filesize)));
}
#else
{
string& file = file_src;
fstream fs;
fs.open(file.c_str(), ios_base::in);
if (!fs.is_open()) throw logic_error("Couldn't open \"" + file + "\"");
std::string contents((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
fs.close();
ss_src.str(contents);
}
#endif
for (int_t i = 0; i != steps; ++i) {
real_t val;
ss_src >> val;
e.val.push_back(val);
}
src.push_back(e);
}
}
void Config::readReceivers(string file) {
std::istringstream ss;
#ifdef USE_MPI
{
MPI_File fh;
MPI_Offset filesize;
if (0 != MPI_File_open(MPI_COMM_WORLD, file.c_str(), MPI_MODE_RDONLY, MPI_INFO_NULL, &fh))
throw logic_error("(MPI) Couldn't open \"" + file + "\"");
if (0 != MPI_File_get_size(fh, &filesize))
throw logic_error("(MPI) Couldn't get \"" + file + "\" size");
vector<char> buf(filesize);
MPI_File_read_all(fh, &buf[0], filesize, MPI_CHAR, MPI_STATUS_IGNORE);
MPI_File_close(&fh);
ss.str(std::string(&buf[0],static_cast<size_t>(filesize)));
}
#else
{
fstream fs;
fs.open(file.c_str(), ios_base::in);
if (!fs.is_open()) throw logic_error("Couldn't open \"" + file + "\"");
std::string contents((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
fs.close();
ss.str(contents);
}
#endif
while (1) {
Receiver e;
ss >> e.x >> e.y;
if (ss.eof()) break;
if (!(0 <= e.x && e.x <= ex)) throw logic_error("Wrong receiver position X");
if (!(oy <= e.y && e.y <= ey)) throw logic_error("Wrong receiver position Y");
//e.i = static_cast<int_t>((e.x - ox) / dx) - 1;
e.i = static_cast<int_t>(e.x / dx);
e.j = static_cast<int_t>((e.y - oy) / dy) - 1;
// distance from node with indexes i,j,k to current node
real_t xl = e.x - e.i * dx;
real_t yl = e.y - oy - (e.j + 1) * dy;
if (e.i == nx) { xl = dx; } // e.x == ex
if (e.j == ny) { e.j -= 1; yl = dy; } // e.y == ey
real_t xr = dx - xl;
real_t yr = dy - yl;
real_t volume = dx * dy;
e.c[L][L] = xr * yr / volume;
e.c[L][R] = xr * yl / volume;
e.c[R][L] = xl * yr / volume;
e.c[R][R] = xl * yl / volume;
rcv.push_back(e);
}
}
void Config::readK(string file) {
K.setSizes(
Dim3D<int_t>(nx, ny, 1),
Dim3D<int_t>(gx, gy, 1),
Dim3D<int_t>(lx, ly, 1),
Dim3D<int_t>(ho, ho, 0),
1);
K.loadData(file);
K.externalSyncStart();
K.internalSync();
K.fillGhost();
K.externalSyncEnd();
}
void Config::readRhoX(string file) {
// last block in global partitioning takes one additional node
Dim3D<vector<int_t> > globalWidth = K.getWidth();
globalWidth[X].at(globalWidth[X].size()-1) -= 1;
Dim3D<vector<int_t> > localWidth = K.getLocalContainer().getWidth();
// so if last block on current process we add node here
if (K.getInternalPos(X) == gx - 1) {
localWidth[X].at(localWidth[X].size()-1) -= 1;
}
rhox.setSizes(
globalWidth,
localWidth,
Dim3D<int_t>(ho, ho, 0),
1);
rhox.loadData(file);
rhox.externalSyncStart();
rhox.internalSync();
rhox.fillGhost();
rhox.externalSyncEnd();
}
void Config::readRhoY(string file) {
Dim3D<vector<int_t> > globalWidth = K.getWidth();
globalWidth[Y].at(globalWidth[Y].size()-1) -= 1;
Dim3D<vector<int_t> > localWidth = K.getLocalContainer().getWidth();
if (K.getInternalPos(Y) == gy - 1) {
localWidth[Y].at(localWidth[Y].size()-1) -= 1;
}
rhoy.setSizes(
globalWidth,
localWidth,
Dim3D<int_t>(ho, ho, 0),
1);
rhoy.loadData(file);
rhoy.externalSyncStart();
rhoy.internalSync();
rhoy.fillGhost();
rhoy.externalSyncEnd();
}