Skip to content

Commit e528e80

Browse files
bigfootedpcarruscagdanielmayer
authored
Update LUT routine (#1792)
Co-authored-by: Pedro Gomes <38071223+pcarruscag@users.noreply.github.com> Co-authored-by: Daniel Mayer <daniel.mayer4@us.bosch.com>
1 parent 124795b commit e528e80

8 files changed

Lines changed: 162 additions & 110 deletions

File tree

Common/include/containers/CFileReaderLUT.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@ class CFileReaderLUT {
6363

6464
std::vector<unsigned long> hull;
6565

66-
std::string SkipToFlag(std::ifstream* file_stream, const std::string& flag);
66+
/*! \brief Searches for the position of flag in file_stream and
67+
* sets the stream position of file_stream to that position.
68+
*/
69+
void SkipToFlag(std::ifstream& file_stream, const std::string& current_line, const std::string& flag) const;
70+
71+
/*! \brief Extracts the next non-empty characters from file_stream and stores them into line.
72+
*/
73+
bool GetNextNonEmptyLine(std::ifstream& file_stream, std::string& line) const;
74+
75+
/*! \brief Extracts characters from file_stream, removes trailing control characters,
76+
* and stores them into line.
77+
*/
78+
bool GetStrippedLine(std::ifstream& file_stream, std::string& line) const;
6779

6880
public:
6981
CFileReaderLUT(){};
@@ -83,5 +95,5 @@ class CFileReaderLUT {
8395

8496
inline const std::vector<unsigned long>& GetHull() const { return hull; };
8597

86-
void ReadRawDRG(const std::string& file_name);
98+
void ReadRawLUT(const std::string& file_name);
8799
};

Common/src/CConfig.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ void CConfig::SetConfig_Options() {
10951095
/*!\brief KIND_TRANS_MODEL \n DESCRIPTION: Specify transition model OPTIONS: see \link Trans_Model_Map \endlink \n DEFAULT: NONE \ingroup Config*/
10961096
addEnumOption("KIND_TRANS_MODEL", Kind_Trans_Model, Trans_Model_Map, TURB_TRANS_MODEL::NONE);
10971097

1098-
/*!\brief KIND_SPECIES_MODEL \n DESCRIPTION: Specify scalar transport model \n Options: see \link Scalar_Model_Map \endlink \n DEFAULT: NONE \ingroup Config*/
1098+
/*!\brief KIND_SCALAR_MODEL \n DESCRIPTION: Specify scalar transport model \n Options: see \link Scalar_Model_Map \endlink \n DEFAULT: NONE \ingroup Config*/
10991099
addEnumOption("KIND_SCALAR_MODEL", Kind_Species_Model, Species_Model_Map, SPECIES_MODEL::NONE);
11001100

11011101
/*!\brief KIND_SGS_MODEL \n DESCRIPTION: Specify subgrid scale model OPTIONS: see \link SGS_Model_Map \endlink \n DEFAULT: NONE \ingroup Config*/
@@ -5339,7 +5339,7 @@ void CConfig::SetPostprocessing(SU2_COMPONENT val_software, unsigned short val_i
53395339
}
53405340

53415341
/*--- Checks for additional species transport. ---*/
5342-
if (Kind_Species_Model != SPECIES_MODEL::NONE) {
5342+
if (Kind_Species_Model == SPECIES_MODEL::SPECIES_TRANSPORT) {
53435343
if (Kind_Solver != MAIN_SOLVER::INC_NAVIER_STOKES &&
53445344
Kind_Solver != MAIN_SOLVER::INC_RANS &&
53455345
Kind_Solver != MAIN_SOLVER::DISC_ADJ_INC_NAVIER_STOKES &&

Common/src/containers/CFileReaderLUT.cpp

Lines changed: 113 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333

3434
using namespace std;
3535

36-
void CFileReaderLUT::ReadRawDRG(const string& file_name) {
37-
version_reader = "1.0.0";
36+
void CFileReaderLUT::ReadRawLUT(const string& file_name) {
37+
version_reader = "1.0.1";
3838

3939
/*--- Store MPI rank. ---*/
4040
rank = SU2_MPI::GetRank();
@@ -46,8 +46,6 @@ void CFileReaderLUT::ReadRawDRG(const string& file_name) {
4646

4747
ifstream file_stream;
4848

49-
int ixColon;
50-
5149
bool eoHeader = false;
5250
bool eoData = false;
5351
bool eoConnectivity = false;
@@ -59,106 +57,104 @@ void CFileReaderLUT::ReadRawDRG(const string& file_name) {
5957
SU2_MPI::Error(string("There is no look-up-table file called ") + file_name, CURRENT_FUNCTION);
6058
}
6159

62-
/* Read header */
63-
line = SkipToFlag(&file_stream, "<header>");
60+
/*--- Read header ---*/
61+
SkipToFlag(file_stream, line, "<Header>");
62+
63+
while (GetNextNonEmptyLine(file_stream, line) && !eoHeader) {
6464

65-
while (getline(file_stream, line) && !eoHeader) {
66-
/* number of points in LUT */
67-
if (line.compare("[version]") == 0) {
68-
getline(file_stream, line);
65+
/*--- number of points in LUT ---*/
66+
if (line.compare("[Version]") == 0) {
67+
GetNextNonEmptyLine(file_stream, line);
6968
version_lut = line;
7069
}
7170

72-
/* number of points in LUT */
73-
if (line.compare("[number of points]") == 0) {
74-
getline(file_stream, line);
71+
/*--- number of points in LUT ---*/
72+
if (line.compare("[Number of points]") == 0) {
73+
GetNextNonEmptyLine(file_stream, line);
7574
n_points = stoi(line);
7675
}
7776

78-
/* number of triangles in LUT */
79-
if (line.compare("[number of triangles]") == 0) {
80-
getline(file_stream, line);
77+
/*--- number of triangles in LUT ---*/
78+
if (line.compare("[Number of triangles]") == 0) {
79+
GetNextNonEmptyLine(file_stream, line);
8180
n_triangles = stoi(line);
8281
}
8382

84-
/* number of points on the hull */
85-
if (line.compare("[number of hull points]") == 0) {
86-
getline(file_stream, line);
83+
/*--- number of points on the hull ---*/
84+
if (line.compare("[Number of hull points]") == 0) {
85+
GetNextNonEmptyLine(file_stream, line);
8786
n_hull_points = stoi(line);
8887
}
8988

90-
/* number of variables in LUT */
91-
if (line.compare("[number of variables]") == 0) {
92-
getline(file_stream, line);
89+
/*--- number of variables in LUT ---*/
90+
if (line.compare("[Number of variables]") == 0) {
91+
GetNextNonEmptyLine(file_stream, line);
9392
n_variables = stoi(line);
9493
}
9594

96-
/* variable names */
97-
if (line.compare("[variable names]") == 0) {
98-
getline(file_stream, line);
99-
stream_names_var.str(line);
100-
while (stream_names_var) {
101-
stream_names_var >> word;
102-
ixColon = (int)word.find(":");
95+
/*--- variable names ---*/
96+
if (line.compare("[Variable names]") == 0) {
97+
98+
for (unsigned long i = 0; i < n_variables; i++){
10399

104-
names_var.push_back(word.substr(ixColon + 1, word.size() - 1));
100+
/*--- grab a single line ---*/
101+
GetNextNonEmptyLine(file_stream, line);
102+
names_var.push_back(line.substr(line.find(":")+1));
105103
}
106-
names_var.pop_back(); // removes last redundant element
107104
}
108105

109-
// check if end of header is reached
110-
if (line.compare("</header>") == 0) eoHeader = true;
106+
/*--- check if end of header is reached ---*/
107+
if (line.compare("</Header>") == 0) eoHeader = true;
111108
}
112109

113-
// check version_lut
110+
/*--- check version_lut ---*/
114111
if (version_lut.compare(version_reader) != 0)
115-
SU2_MPI::Error("Version conflict between Dragon reader and Dragon library file.", CURRENT_FUNCTION);
112+
SU2_MPI::Error("Version conflict between LUT reader and LUT library file.", CURRENT_FUNCTION);
116113

117-
// check header quantities
114+
/*--- check header quantities ---*/
118115
if (n_points == 0 || n_triangles == 0 || n_variables == 0 || n_hull_points == 0)
119116
SU2_MPI::Error(
120-
"Number of points, triangles, hull points, or variables in Dragon "
117+
"Number of points, triangles, hull points, or variables in lookup table "
121118
"library header is zero.",
122119
CURRENT_FUNCTION);
123120

124-
// check if number of variables is consistent
121+
/*--- check if number of variables is consistent ---*/
125122
if (n_variables != names_var.size())
126123
SU2_MPI::Error(
127124
"Number of read variables does not match number of "
128-
"variables specified in Dragon "
125+
"variables specified in lookup table "
129126
"library header.",
130127
CURRENT_FUNCTION);
131128

132-
/* now that n_variables, n_points, n_hull_points and n_variables is available,
133-
* allocate memory */
134-
if (rank == MASTER_NODE) cout << "allocating memory for the data" << endl;
129+
/*--- now that n_variables, n_points, n_hull_points and n_variables are available, allocate memory ---*/
130+
if (rank == MASTER_NODE) cout << "allocating memory for the data, size = ( " << GetNVariables() << " , " << GetNPoints() << " )" << endl;
135131
table_data.resize(GetNVariables(), GetNPoints());
136132

137-
138-
if (rank == MASTER_NODE) cout << "allocating memory for the triangles" << endl;
133+
if (rank == MASTER_NODE) cout << "allocating memory for the triangles, size = "<< GetNTriangles() << endl;
139134
triangles.resize(GetNTriangles(), 3);
140135

141-
if (rank == MASTER_NODE) cout << "allocating memory for the hull points" << endl;
136+
if (rank == MASTER_NODE) cout << "allocating memory for the hull points, size = " << GetNHullPoints() << endl;
142137
hull.resize(GetNHullPoints());
143138

144-
/* flush any cout */
139+
/*--- flush any cout ---*/
145140
if (rank == MASTER_NODE) cout << endl;
146141

147-
// read data block
142+
/*--- read data block ---*/
148143
if (rank == MASTER_NODE) cout << "loading data block" << endl;
149144

150-
line = SkipToFlag(&file_stream, "<data>");
145+
SkipToFlag(file_stream, line, "<Data>");
151146

152147
unsigned long pointCounter = 0;
153-
while (getline(file_stream, line) && !eoData) {
154-
// check if end of data is reached
155-
if (line.compare("</data>") == 0) eoData = true;
148+
while (GetNextNonEmptyLine(file_stream, line) && !eoData) {
149+
150+
/*--- check if end of data is reached ---*/
151+
if (line.compare("</Data>") == 0) eoData = true;
156152

157153
if (!eoData) {
158-
// one line contains values for one point for all variables
154+
/*--- one line contains values for one point for all variables ---*/
159155
istringstream streamDataLine(line);
160156

161-
// add next line to table array
157+
/*--- add next line to table array ---*/
162158
for (unsigned long iVar = 0; iVar < n_variables; iVar++) {
163159
streamDataLine >> word;
164160
passivedouble tmp = stod(word);
@@ -171,28 +167,32 @@ void CFileReaderLUT::ReadRawDRG(const string& file_name) {
171167
if (n_points != pointCounter - 1)
172168
SU2_MPI::Error(
173169
"Number of read points does not match number of points "
174-
"specified in Dragon library header.",
170+
"specified in lookup table library header.",
175171
CURRENT_FUNCTION);
176172

177-
// read connectivity
173+
/*--- read connectivity ---*/
178174
if (rank == MASTER_NODE) cout << "loading connectivity block" << endl;
179175

180-
line = SkipToFlag(&file_stream, "<connectivity>");
176+
SkipToFlag(file_stream, line, "<Connectivity>");
181177

182178
unsigned long triCounter = 0;
183-
while (getline(file_stream, line) && !eoConnectivity) {
184-
// check if end of data is reached
185-
if (line.compare("</connectivity>") == 0) eoConnectivity = true;
179+
while (GetNextNonEmptyLine(file_stream, line) && !eoConnectivity) {
180+
if (!line.empty() && (line[line.length()-1] == '\n' || line[line.length()-1] == '\r' )) {
181+
line.erase(line.length()-1);
182+
}
183+
/*--- check if end of data is reached ---*/
184+
if (line.compare("</Connectivity>") == 0) eoConnectivity = true;
186185

187186
if (!eoConnectivity) {
188-
// one line contains values for one triangle (3 points)
187+
/*--- one line contains values for one triangle (3 points) ---*/
189188
istringstream streamTriLine(line);
190189

191-
// add next line to triangles
190+
/*--- add next line to triangles ---*/
192191
for (int iPoint = 0; iPoint < 3; iPoint++) {
192+
193193
streamTriLine >> word;
194-
// Dragon table index starts with 1, convert to c++ indexing starting
195-
// with 0:
194+
195+
/*--- lookup table index starts with 1, convert to c++ indexing starting with 0: ---*/
196196
triangles[triCounter][iPoint] = stol(word) - 1;
197197
}
198198
}
@@ -202,27 +202,29 @@ void CFileReaderLUT::ReadRawDRG(const string& file_name) {
202202
if (n_triangles != triCounter - 1)
203203
SU2_MPI::Error(
204204
"Number of read triangles does not match number of points "
205-
"specified in Dragon library header.",
205+
"specified in lookup table library header.",
206206
CURRENT_FUNCTION);
207207

208-
// read hull points
208+
/*--- read hull points ---*/
209209
if (rank == MASTER_NODE) cout << "loading hull block" << endl;
210210

211-
line = SkipToFlag(&file_stream, "<hull>");
211+
SkipToFlag(file_stream, line, "<Hull>");
212212

213213
unsigned long hullCounter = 0;
214-
while (getline(file_stream, line) && !eoHull) {
215-
// check if end of data is reached
216-
if (line.compare("</hull>") == 0) eoHull = true;
214+
while (GetNextNonEmptyLine(file_stream, line) && !eoHull) {
215+
if (!line.empty() && (line[line.length()-1] == '\n' || line[line.length()-1] == '\r' )) {
216+
line.erase(line.length()-1);
217+
}
218+
/*--- check if end of data is reached ---*/
219+
if (line.compare("</Hull>") == 0) eoHull = true;
217220

218221
if (!eoHull) {
219-
// one line contains one point ID for one point on the hull
222+
/*--- one line contains one point ID for one point on the hull ---*/
220223
istringstream streamHullLine(line);
221224

222225
streamHullLine >> word;
223226

224-
// Dragon table indices start with 1, convert to c++ indexing starting
225-
// with 0:
227+
/*--- Lookup table indices start with 1, convert to c++ indexing starting with 0: ---*/
226228
hull[hullCounter] = stol(word) - 1;
227229
}
228230
hullCounter++;
@@ -231,22 +233,53 @@ void CFileReaderLUT::ReadRawDRG(const string& file_name) {
231233
if (n_hull_points != hullCounter - 1)
232234
SU2_MPI::Error(
233235
"Number of read hull points does not match number of points "
234-
"specified in Dragon library header.",
236+
"specified in lookup table library header.",
235237
CURRENT_FUNCTION);
236238

237239
file_stream.close();
238240

239241
}
240242

241-
string CFileReaderLUT::SkipToFlag(ifstream* file_stream, const string& flag) {
242-
string line;
243-
getline(*file_stream, line);
243+
void CFileReaderLUT::SkipToFlag(ifstream& file_stream, const string& current_line, const string& flag) const {
244+
string next_line = "";
245+
246+
/*--- compare current line to flag and return if equal ---*/
247+
if (current_line.compare(flag) == 0) return;
244248

245-
while (line.compare(flag) != 0 && !(*file_stream).eof()) {
246-
getline(*file_stream, line);
249+
/*--- else, search for flag ---*/
250+
while (next_line.find(flag) == string::npos && !(file_stream).eof()) {
251+
GetStrippedLine(file_stream, next_line);
247252
}
248253

249-
if ((*file_stream).eof()) SU2_MPI::Error("Flag not found in file", CURRENT_FUNCTION);
254+
/*--- throw error if end of file reached ---*/
255+
if ((file_stream).eof()) SU2_MPI::Error("Flag " + flag + " not found in file", CURRENT_FUNCTION);
256+
}
257+
258+
bool CFileReaderLUT::GetNextNonEmptyLine(ifstream& file_stream, string& line) const {
250259

251-
return line;
260+
/*--- get next line and save return value ---*/
261+
bool return_value = GetStrippedLine(file_stream, line);
262+
263+
/*--- skip empty lines ---*/
264+
while (line.empty() && !(file_stream).eof()){
265+
return_value = GetStrippedLine(file_stream, line);
266+
}
267+
268+
/*--- return true if line is not empty, else return false ---*/
269+
return return_value;
252270
}
271+
272+
bool CFileReaderLUT::GetStrippedLine(ifstream& file_stream, string& line) const {
273+
274+
/*--- get next line and save return value ---*/
275+
getline(file_stream, line);
276+
277+
/*--- find last non-control-character character ---*/
278+
size_t end = line.find_last_not_of(" \n\r\t\f\v");
279+
280+
/*--- clean up line ---*/
281+
line = (end == string::npos) ? "" : line.substr(0, end + 1);
282+
283+
/*--- return true if line is not empty, else return false ---*/
284+
return !line.empty();
285+
}

Common/src/containers/CLookUpTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void CLookUpTable::LoadTableRaw(const string& var_file_name_lut) {
7878
if (rank == MASTER_NODE)
7979
cout << "Loading lookup table, filename = " << var_file_name_lut << " ..." << endl;
8080

81-
file_reader.ReadRawDRG(var_file_name_lut);
81+
file_reader.ReadRawLUT(var_file_name_lut);
8282

8383
n_points = file_reader.GetNPoints();
8484
n_triangles = file_reader.GetNTriangles();

0 commit comments

Comments
 (0)