Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 989
Expand file tree
/
Copy pathCMarkerProfileReaderFVM.cpp
More file actions
503 lines (359 loc) · 17.2 KB
/
Copy pathCMarkerProfileReaderFVM.cpp
File metadata and controls
503 lines (359 loc) · 17.2 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
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
/*!
* \file CMarkerProfileReaderFVM.cpp
* \brief Class that handles the reading of marker profile files.
* \author T. Economon
* \version 7.4.0 "Blackbird"
*
* SU2 Project Website: https://su2code.github.io
*
* The SU2 Project is maintained by the SU2 Foundation
* (http://su2foundation.org)
*
* Copyright 2012-2022, SU2 Contributors (cf. AUTHORS.md)
*
* SU2 is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* SU2 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with SU2. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../include/CMarkerProfileReaderFVM.hpp"
CMarkerProfileReaderFVM::CMarkerProfileReaderFVM(CGeometry *val_geometry,
CConfig *val_config,
string val_filename,
unsigned short val_kind_marker,
unsigned short val_number_vars,
vector<string> val_columnNames,
vector<string> val_columnValues) {
/*--- Store input values and pointers to class data. ---*/
rank = SU2_MPI::GetRank();
size = SU2_MPI::GetSize();
this->config = val_config;
this->geometry = val_geometry;
dimension = geometry->GetnDim();
filename = val_filename;
markerType = val_kind_marker;
numberOfVars = val_number_vars;
columnNames = val_columnNames;
columnValues = val_columnValues;
/* Attempt to open the specified file. */
ifstream profile_file;
profile_file.open(filename.data(), ios::in);
/* If the file is not found, then we merge the information necessary
and write a template marker profile file. Otherwise, we read and
store the information in the marker profile file. */
if (profile_file.fail()) {
MergeProfileMarkers();
WriteMarkerProfileTemplate();
} else {
ReadMarkerProfile();
}
}
CMarkerProfileReaderFVM::~CMarkerProfileReaderFVM(void) { }
void CMarkerProfileReaderFVM::ReadMarkerProfile() {
/*--- Open the profile file (we have already error checked) ---*/
ifstream profile_file;
profile_file.open(filename.data(), ios::in);
bool nmarkFound = false;
unsigned long skip = 0;
/*--- Identify the markers and data set in the profile file ---*/
string text_line;
/*--- We search the file until we find the keyword NMARK=. Data before NMARK= will be ignored.
This allows for some information in a header that will be ignored by the profile reader. ---*/
while (getline (profile_file, text_line)) {
/*--- read NMARK ---*/
string::size_type position = text_line.find ("NMARK=",0);
if (position != string::npos) {
nmarkFound = true;
text_line.erase (0,6); numberOfProfiles = atoi(text_line.c_str());
numberOfRowsInProfile.resize(numberOfProfiles);
numberOfColumnsInProfile.resize(numberOfProfiles);
for (unsigned short iMarker = 0 ; iMarker < numberOfProfiles; iMarker++) {
/*--- read MARKER_TAG ---*/
getline (profile_file, text_line);
text_line.erase (0,11);
for (unsigned short iChar = 0; iChar < 20; iChar++) {
position = text_line.find( " ", 0 ); if (position != string::npos) text_line.erase (position,1);
position = text_line.find( "\r", 0 ); if (position != string::npos) text_line.erase (position,1);
position = text_line.find( "\n", 0 ); if (position != string::npos) text_line.erase (position,1);
}
profileTags.push_back(text_line.c_str());
/*--- read NROW ---*/
getline (profile_file, text_line);
text_line.erase (0,5); numberOfRowsInProfile[iMarker] = atoi(text_line.c_str());
/*--- read NCOL ---*/
getline (profile_file, text_line);
text_line.erase (0,5); numberOfColumnsInProfile[iMarker] = atoi(text_line.c_str());
/*--- read the column format description. This line is not required, so if we cannot find it, we just continue ---*/
getline (profile_file, text_line);
string::size_type dataheader = text_line.find ("# COORD",0);
if (dataheader == 0) {
skip = 0;
} else {
/*--- no header, but we have read a line, so we have to read one line less data ---*/
skip = 1;
}
/*--- Skip the data. This is read in the next loop. ---*/
for (unsigned long iRow = 0; iRow < (numberOfRowsInProfile[iMarker]-skip); iRow++) getline (profile_file, text_line);
}
}
}
profile_file.close();
if (nmarkFound==false) {
SU2_MPI::Error("While opening profile file, no \"NMARK=\" specification was found", CURRENT_FUNCTION);
}
/*--- Compute array bounds and offsets. Allocate data structure. ---*/
profileData.resize(numberOfProfiles);
for (unsigned short iMarker = 0; iMarker < numberOfProfiles; iMarker++) {
profileData[iMarker].resize(numberOfRowsInProfile[iMarker]*numberOfColumnsInProfile[iMarker], 0.0);
}
/*--- Read all lines in the profile file and extract data. ---*/
profile_file.open(filename.data(), ios::in);
int counter = 0;
while (getline (profile_file, text_line)) {
string::size_type position = text_line.find ("NMARK=",0);
if (position != string::npos) {
for (unsigned short iMarker = 0; iMarker < numberOfProfiles; iMarker++) {
/*--- Skip the tag, nRow and nCol lines. ---*/
getline (profile_file, text_line);
getline (profile_file, text_line);
getline (profile_file, text_line);
/*--- if skip=0 then we can expect column format description ---*/
if (skip == 0) getline (profile_file, text_line);
/*--- Now read the data for each row and store. ---*/
for (unsigned long iRow = 0; iRow < numberOfRowsInProfile[iMarker]; iRow++) {
getline (profile_file, text_line);
istringstream point_line(text_line);
/*--- Store the values (starting with node coordinates) --*/
for (unsigned short iVar = 0; iVar < numberOfColumnsInProfile[iMarker]; iVar++)
point_line >> profileData[iMarker][iRow*numberOfColumnsInProfile[iMarker] + iVar];
/*--- Increment our local row counter. ---*/
counter++;
}
}
}
}
profile_file.close();
}
void CMarkerProfileReaderFVM::MergeProfileMarkers() {
/*--- Local variables needed on all processors ---*/
unsigned long iPoint, jPoint, kPoint;
int iProcessor, nProcessor = size;
unsigned long iVertex, iMarker;
unsigned long Buffer_Send_nPoin[1], *Buffer_Recv_nPoin = nullptr;
unsigned long nLocalPoint = 0, MaxLocalPoint = 0;
unsigned long index, iChar;
char str_buf[MAX_STRING_SIZE];
vector<string> Marker_Tags;
vector<unsigned long> nRowCum_Counter;
if (rank == MASTER_NODE) Buffer_Recv_nPoin = new unsigned long[nProcessor];
/*--- Search all boundaries on the present rank to count the number
of nodes found on profile markers. ---*/
nLocalPoint = 0; numberOfProfiles = 0;
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if (config->GetMarker_All_KindBC(iMarker) == markerType) {
numberOfProfiles++;
for (iVertex = 0; iVertex < geometry->nVertex[iMarker]; iVertex++) {
iPoint = geometry->vertex[iMarker][iVertex]->GetNode();
/*--- Only communicate owned nodes to avoid duplicates. ---*/
if (geometry->nodes->GetDomain(iPoint))
nLocalPoint++;
}
}
}
Buffer_Send_nPoin[0] = nLocalPoint;
/*--- Communicate the total number of nodes on this domain. ---*/
SU2_MPI::Gather(&Buffer_Send_nPoin, 1, MPI_UNSIGNED_LONG,
Buffer_Recv_nPoin, 1, MPI_UNSIGNED_LONG, MASTER_NODE, SU2_MPI::GetComm());
SU2_MPI::Allreduce(&nLocalPoint, &MaxLocalPoint, 1, MPI_UNSIGNED_LONG, MPI_MAX, SU2_MPI::GetComm());
/*--- Send and Recv buffers. ---*/
su2double *Buffer_Send_X = new su2double[MaxLocalPoint];
su2double *Buffer_Recv_X = nullptr;
su2double *Buffer_Send_Y = new su2double[MaxLocalPoint];
su2double *Buffer_Recv_Y = nullptr;
su2double *Buffer_Send_Z = nullptr, *Buffer_Recv_Z = nullptr;
if (dimension == 3) Buffer_Send_Z = new su2double[MaxLocalPoint];
char *Buffer_Send_Str = new char[MaxLocalPoint*MAX_STRING_SIZE];
char *Buffer_Recv_Str = nullptr;
/*--- Prepare the receive buffers in the master node only. ---*/
if (rank == MASTER_NODE) {
Buffer_Recv_X = new su2double[nProcessor*MaxLocalPoint];
Buffer_Recv_Y = new su2double[nProcessor*MaxLocalPoint];
if (dimension == 3) Buffer_Recv_Z = new su2double[nProcessor*MaxLocalPoint];
Buffer_Recv_Str = new char[nProcessor*MaxLocalPoint*MAX_STRING_SIZE];
/*--- Sum total number of nodes to be written and allocate arrays ---*/
unsigned long nGlobal_InletPoint = 0;
for (iProcessor = 0; iProcessor < nProcessor; iProcessor++) {
nGlobal_InletPoint += Buffer_Recv_nPoin[iProcessor];
}
profileCoords.resize(numberOfProfiles);
for (iMarker = 0; iMarker < numberOfProfiles; iMarker++) {
profileCoords[iMarker].resize(dimension);
}
}
/*--- Main communication routine. Loop over each coordinate and perform
the MPI comm. Temporary 1-D buffers are used to send the coordinates at
all nodes on each partition to the master node. These are then unpacked
by the master and sorted by marker tag in one large n-dim. array. ---*/
su2double *Coords_Local; jPoint = 0;
for (iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if (config->GetMarker_All_KindBC(iMarker) == markerType) {
for (iVertex = 0; iVertex < geometry->nVertex[iMarker]; iVertex++) {
iPoint = geometry->vertex[iMarker][iVertex]->GetNode();
/*--- Only communicate owned nodes to avoid duplicates. ---*/
if (geometry->nodes->GetDomain(iPoint)) {
/*--- Retrieve local coordinates at this node. ---*/
Coords_Local = geometry->nodes->GetCoord(iPoint);
/*--- Load local coords into the temporary send buffer. ---*/
Buffer_Send_X[jPoint] = Coords_Local[0];
Buffer_Send_Y[jPoint] = Coords_Local[1];
if (dimension == 3) Buffer_Send_Z[jPoint] = Coords_Local[2];
/*--- If US system, the output should be in inches ---*/
if (config->GetSystemMeasurements() == US) {
Buffer_Send_X[jPoint] *= 12.0;
Buffer_Send_Y[jPoint] *= 12.0;
if (dimension == 3) Buffer_Send_Z[jPoint] *= 12.0;
}
/*--- Store the marker tag for this particular node. ---*/
SPRINTF(&Buffer_Send_Str[jPoint*MAX_STRING_SIZE], "%s",
config->GetMarker_All_TagBound(iMarker).c_str());
/*--- Increment jPoint as the counter. We need this because iPoint
may include halo nodes that we skip over during this loop. ---*/
jPoint++;
}
}
}
}
/*--- Gather the coordinate data on the master node using MPI. ---*/
SU2_MPI::Gather(Buffer_Send_X, (int)MaxLocalPoint, MPI_DOUBLE,
Buffer_Recv_X, (int)MaxLocalPoint, MPI_DOUBLE, MASTER_NODE, SU2_MPI::GetComm());
SU2_MPI::Gather(Buffer_Send_Y, (int)MaxLocalPoint, MPI_DOUBLE,
Buffer_Recv_Y, (int)MaxLocalPoint, MPI_DOUBLE, MASTER_NODE, SU2_MPI::GetComm());
if (dimension == 3) {
SU2_MPI::Gather(Buffer_Send_Z, (int)MaxLocalPoint, MPI_DOUBLE,
Buffer_Recv_Z, (int)MaxLocalPoint, MPI_DOUBLE, MASTER_NODE, SU2_MPI::GetComm());
}
SU2_MPI::Gather(Buffer_Send_Str, (int)MaxLocalPoint*MAX_STRING_SIZE, MPI_CHAR,
Buffer_Recv_Str, (int)MaxLocalPoint*MAX_STRING_SIZE, MPI_CHAR, MASTER_NODE, SU2_MPI::GetComm());
/*--- The master node unpacks and sorts this variable by marker tag. ---*/
if (rank == MASTER_NODE) {
profileTags.clear();
/*--- First, parse the marker tags to count how many total profile markers
we have now on the master. ---*/
for (iProcessor = 0; iProcessor < nProcessor; iProcessor++) {
for (iPoint = 0; iPoint < Buffer_Recv_nPoin[iProcessor]; iPoint++) {
index = (iProcessor*MaxLocalPoint + iPoint)*MAX_STRING_SIZE;
for (iChar = 0; iChar < MAX_STRING_SIZE; iChar++) {
str_buf[iChar] = Buffer_Recv_Str[index + iChar];
}
Marker_Tags.push_back(str_buf);
profileTags.push_back(str_buf);
}
}
/*--- Remove the duplicate profile marker strings. From 1 per point to 1 per marker. ---*/
profileTags.erase(unique(profileTags.begin(),
profileTags.end()),
profileTags.end());
/*--- Store the unique number of markers for writing later. ---*/
numberOfProfiles = profileTags.size();
/*--- Count the number of rows (nodes) per marker. ---*/
numberOfRowsInProfile.resize(numberOfProfiles,0.0);
jPoint = 0;
for (iProcessor = 0; iProcessor < nProcessor; iProcessor++) {
for (iPoint = 0; iPoint < Buffer_Recv_nPoin[iProcessor]; iPoint++) {
for (iMarker = 0; iMarker < numberOfProfiles; iMarker++) {
if (profileTags[iMarker] == Marker_Tags[jPoint]) {
numberOfRowsInProfile[iMarker]++;
}
}
jPoint++;
}
}
/*--- Load up the coordinates, sorted into chunks per marker. ---*/
jPoint = 0; kPoint = 0;
for (iProcessor = 0; iProcessor < nProcessor; iProcessor++) {
for (iPoint = 0; iPoint < Buffer_Recv_nPoin[iProcessor]; iPoint++) {
for (iMarker = 0; iMarker < numberOfProfiles; iMarker++) {
if (profileTags[iMarker] == Marker_Tags[kPoint]) {
/*--- Find our current index for this marker and store coords. ---*/
profileCoords[iMarker][0].push_back(Buffer_Recv_X[jPoint]);
profileCoords[iMarker][1].push_back(Buffer_Recv_Y[jPoint]);
if (dimension == 3)
profileCoords[iMarker][2].push_back(Buffer_Recv_Z[jPoint]);
}
}
/*--- Increment point counter for marker tags and data. ---*/
kPoint++;
jPoint++;
}
/*--- Adjust jPoint to index of next proc's data in the buffers. ---*/
jPoint = (iProcessor+1)*MaxLocalPoint;
}
}
/*--- Immediately release the temporary data buffers. ---*/
delete [] Buffer_Send_X;
delete [] Buffer_Send_Y;
delete [] Buffer_Send_Z;
delete [] Buffer_Send_Str;
if (rank == MASTER_NODE) {
delete [] Buffer_Recv_X;
delete [] Buffer_Recv_Y;
delete [] Buffer_Recv_Z;
delete [] Buffer_Recv_nPoin;
delete [] Buffer_Recv_Str;
}
}
void CMarkerProfileReaderFVM::WriteMarkerProfileTemplate() {
/*--- Count the number of columns that we have for this case.
Here, we have dimension entries for node coordinates and then the
total number of columns of data specified in the constructor. ---*/
const unsigned short nColumns = dimension + numberOfVars;
/*--- Write the profile file. Note that we have already merged
all of the information for the markers and coordinates previously
in the MergeProfileMarkers() routine and only the master writes. ---*/
if (rank == MASTER_NODE) {
ofstream node_file("example_"+filename);
node_file << "NMARK= " << numberOfProfiles << endl;
unsigned short iMarkerCounter = 0;
for (unsigned short iMarker = 0; iMarker < config->GetnMarker_All(); iMarker++) {
if (config->GetMarker_All_KindBC(iMarker) == markerType) {
/*--- Access the default data for this marker. ---*/
string Marker_Tag = profileTags[iMarkerCounter];
/*--- Header information for this marker. ---*/
node_file << "MARKER_TAG= " << Marker_Tag << endl;
node_file << "NROW=" << numberOfRowsInProfile[iMarkerCounter] << endl;
node_file << "NCOL=" << nColumns << endl;
/*--- header line (names of the columns) --- */
node_file << columnNames[iMarkerCounter] << endl;
node_file << setprecision(15);
node_file << std::scientific;
/*--- Loop over the data structure and write the coords and vars. ---*/
for (unsigned long iPoint = 0; iPoint < numberOfRowsInProfile[iMarkerCounter]; iPoint++) {
for (unsigned short iDim = 0; iDim < dimension; iDim++) {
node_file << profileCoords[iMarkerCounter][iDim][iPoint] << "\t";
}
node_file << columnValues[iMarkerCounter] << endl;
}
iMarkerCounter++;
}
} // iMarker
node_file.close();
/*--- Print a message to inform the user about the template file. ---*/
stringstream err;
err << endl;
err << " Could not find the input file for the marker profile." << endl;
err << " Looked for: " << filename << "." << endl;
err << " Created a template profile file with default values" << endl;
err << " named example_" << filename << endl;
err << " You can use this file as a guide for making your own profile" << endl;
err << " specification." << endl << endl;
SU2_MPI::Error(err.str(), CURRENT_FUNCTION);
}
}
You can’t perform that action at this time.
