-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
405 lines (298 loc) · 9.48 KB
/
Mesh.cpp
File metadata and controls
405 lines (298 loc) · 9.48 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
#include "Mesh.h"
Mesh::Mesh()
: nPoints_(-2), nFaces_(-2), nPatches_(-2)
{
std::cout << "I am the mesh constructor" << std::endl;
readMesh();
}
void Mesh::readMesh()
{
std::string pathPoints (getExecutablePath()+std::string("constant/")+std::string("polyMesh/points"));
readPoints(pathPoints);
std::string pathFaces (getExecutablePath()+std::string("constant/")+std::string("polyMesh/faces"));
readFaces(pathFaces);
std::string pathOwners(getExecutablePath()+std::string("constant/")+std::string("polyMesh/owner"));
readOwners(pathOwners);
std::string pathNeighbour(getExecutablePath()+std::string("constant/")+std::string("polyMesh/neighbour"));
readNeighbour(pathNeighbour);
std::string pathBoundary(getExecutablePath()+std::string("constant/")+std::string("polyMesh/boundary"));
readBoundary(pathBoundary);
std::cout << "Mesh was read successfully!" << std::endl;
}
int Mesh::getNEntitites(std::ifstream& file)
{
int nPoints ;
bool findNPoints (true);
std::string line;
while (findNPoints)
{
std::getline(file, line );
if ( std::stringstream( line ) >> nPoints)
findNPoints=false;
}
return nPoints;
}
void Mesh::readPoints(std::string path)
{
// Passes the path of points into a ifsteam
std::ifstream in_file(path.c_str());
// Checks if file is to be open correctly
if(!in_file.is_open())
{
std::cerr << "The input file was not open correctly!" << std::endl;
//return 1;
}
std::string line;
// Gets number of points in file
nPoints_ = getNEntitites(in_file);
pointList_.resize(nPoints_);
// Loop over the points to fill the vector
while ( getline(in_file, line ) && line.find("(") == std::string::npos );
{
char c; // Variable to catch "("
double x, y, z; // Variables to store the x,y,z position of the points
for ( int i = 0; i < nPoints_; i++ )
{
std::getline( in_file, line );
std::stringstream(line ) >> c >> x >> y >> z;
pointList_[i]= Point(x,y,z); // Node list
}
}
// Close the file
in_file.close();
}
void Mesh::readFaces(std::string path)
{
// Passes the path of points into a ifsteam
std::ifstream in_file(path.c_str());
// Checks if file is to be open correctly
if(!in_file.is_open())
{
std::cerr << "The input file was not open correctly!" << std::endl;
//return 1;
}
std::string line;
// Gets number of faces in file
nFaces_ = getNEntitites(in_file);
// Resize the vectors according to the number of faces
faceList_.resize(nFaces_);
// Loop over the points to fill the vector
while ( getline(in_file, line ) && line.find("(") == std::string::npos );
{
int nPointsInFace; // catch number of points in each Face
char c; // Variable to catch "("
for ( int i = 0; i < nFaces_; i++ )
{
std::getline( in_file, line );
std::stringstream line_2(line);
line_2 >> nPointsInFace >> c;
std::vector<Point*> listOfPoints (nPointsInFace); // array to store the list of points
int counter(0);
int tmp;
while(line_2 >> tmp)
{
listOfPoints[counter]=&pointList_[tmp];
counter++;
}
faceList_[i]=Face(nPointsInFace,listOfPoints);
}
}
// Close the file
in_file.close();
}
void Mesh::readOwners(std::string path)
{
// Passes the path of points into a ifsteam
std::ifstream in_file(path.c_str());
// Checks if file is to be open correctly
if(!in_file.is_open())
{
std::cerr << "The input file was not open correctly!" << std::endl;
//return 1;
}
std::string line;
// Gets number of owners in file
int nOwners = getNEntitites(in_file);
// Loop over the points to fill the vector
while ( getline(in_file, line ) && line.find("(") == std::string::npos );
{
int tmp(0); // catch number of owner
for ( int i = 0; i < nOwners; i++ )
{
std::getline( in_file, line );
std::stringstream line_2(line);
line_2 >> tmp;
faceList_[i].setOwner(tmp); //Set Face Owner
}
}
// Close the file
in_file.close();
}
void Mesh::readNeighbour(std::string path)
{
// Passes the path of points into a ifsteam
std::ifstream in_file(path.c_str());
// Checks if file is to be open correctly
if(!in_file.is_open())
{
std::cerr << "The input file was not open correctly!" << std::endl;
//return 1;
}
std::string line;
// Gets number of faces in file
int nNeighbours = getNEntitites(in_file);
// Variable to store maximum value of neighbour
int storeMax(0);
// Loop over the points to fill the vector
while ( getline(in_file, line ) && line.find("(") == std::string::npos );
{
int tmp(0); // catch number of owner
for ( int i = 0; i < nNeighbours; i++ )
{
std::getline( in_file, line );
std::stringstream line_2(line);
line_2 >> tmp;
faceList_[i].setNeighbour(tmp); //setFace neighbor
if(tmp > storeMax)
{
storeMax = tmp;
}
}
}
// Close the file
in_file.close();
nInteriorFaces_ = nNeighbours;
nBoundaryFaces_ = nFaces_ - nInteriorFaces_;
nCells_ = storeMax + 1;
}
void Mesh::readBoundary(std::string path)
{
// Passes the path of points into a ifsteam
std::ifstream in_file(path.c_str());
// Checks if file is to be open correctly
if(!in_file.is_open())
{
std::cerr << "The input file was not open correctly!" << std::endl;
}
std::string line;
// Booleans to check file reading
bool endInnerLoop = false;
bool firstCurly = false;
bool secondCurly = false;
bool checkBoundaryName = false;
bool checkType = false;
bool checkNFaces = false;
bool checkStartFace = false;
std::string words;
std::string words2;
int values(-1);
int countCurly (0);
std::string name;
std::string type;
int nFaces;
int startFace;
nPatches_ = getNEntitites(in_file);
// Loop over the points to fill the vector
while ( getline(in_file, line ) && line.find("(") == std::string::npos );
{
for ( int i = 0; i < nPatches_; i++ )
{
endInnerLoop = false;
while(!endInnerLoop && !in_file.eof())
{
std::getline( in_file, line );
std::stringstream line_2(line);
// Extracts word from line
line_2 >> words;
if (words.at(0) == '{')
{
firstCurly = true;
countCurly++;
}
if (firstCurly && words.back() == '}' && countCurly < 2)
{
secondCurly = true;
countCurly = 0;
}
if (firstCurly && secondCurly)
endInnerLoop = true;
if(!checkBoundaryName && !firstCurly)
{
name = words;
checkBoundaryName = true;
}
if(words == "type" && (line_2 >> words2) && words2.back() == ';' )
{
type = words2.substr(0, words2.size()-1);
checkType = true;
words2.clear();
}
if(words == "nFaces" && (line_2 >> values) && ( (line_2 >> words2) && words2.back() == ';') )
{
nFaces = values;
checkNFaces = true;
words2.clear();
}
if(words == "startFace" && (line_2 >> values) && ( (line_2 >> words2) && words2.back() == ';'))
{
startFace = values;
checkStartFace = true;
}
// If the stringstream reaches the end of file some parameters are missing.
// Implement later
if (endInnerLoop)
{
if ( !(checkBoundaryName && checkType && checkNFaces && checkStartFace) )
{
throw std::runtime_error("Some error in the boundaries");
}
}
if (in_file.eof() && !endInnerLoop)
{
throw std::runtime_error("End of file!! You probably have problems with parentheses");
}
}
patchList_.push_back(Patch(name,type, nFaces,startFace)); //add a Patch object to the list
checkBoundaryName = false;
checkType = false;
checkNFaces = false;
checkStartFace = false;
firstCurly = false;
secondCurly = false;
}
}
// Close the file
in_file.close();
}
// Function to get the current working directory of the executable file
std::string Mesh::getExecutablePath()
{
char result[ PATH_MAX ];
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
std::string executablePath ( result, (count > 0) ? count : 0 );
executablePath=executablePath.substr(0, executablePath.find_last_of("/"));
return executablePath+"/";
}
void Mesh::computeFaceWeightingFactor()
{
//std::vector<Cell>& cells = cellList_;
std::vector<Face>& faces = faceList_;
// This is a place holder for when the cellList is implemented
std::vector<Cell> cells(nCells_);
// Loop through interior faces
for (unsigned int i = 0; i < nInteriorFaces_ ; i++ )
{
const int cellOwner = faces[i].getOwner();
const int cellNeighbour = faces[i].getNeighbour();
const vector3 faceCenter = faces[i].getCenterOfMass();
const vector3 d_Cf = cells[cellOwner].getCenterOfMass() - faceCenter;
const vector3 d_fF = cells[cellNeighbour].getCenterOfMass() - faceCenter;
const vector3 e_f = faces[i].getFaceAreaVector()/mag( faces[i].getFaceAreaVector() );
faces[i].setweightingFactor( (d_Cf & e_f) / ( (d_Cf & e_f) + (d_fF & e_f) ));
}
// Loop through boundary the faces
for (unsigned int i = nInteriorFaces_ + 1; i < nFaces_ ; i++ )
{
faces[i].setweightingFactor(1.0);
}
}