readCells.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  Create intermediate mesh from Prostar files
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "starMesh.H"
30 #include "IFstream.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 void Foam::starMesh::addRegularCell
35 (
36  const labelList& labels,
37  const label nCreatedCells
38 )
39 {
40  // Memory management
41  static labelList labelsHex(8);
42  static labelList labelsPrism(6);
43  static labelList labelsPyramid(5);
44  static labelList labelsTet(4);
45  static labelList labelsTetWedge(5);
46 
47  label regularTypeFlag = -1;
48 
49  // grab the shape from the table
50  const cellModel* curModelPtr = reinterpret_cast<cellModel*>(0);
51 
52  if // Tetrahedron
53  (
54  labels[2] == labels[3]
55  && labels[4] == labels[5]
56  && labels[5] == labels[6]
57  && labels[6] == labels[7]
58  )
59  {
60  regularTypeFlag = 0;
61  curModelPtr = tetPtr_;
62  }
63  else if // Square-based pyramid
64  (
65  labels[4] == labels[5]
66  && labels[5] == labels[6]
67  && labels[6] == labels[7]
68  )
69  {
70  regularTypeFlag = 1;
71  curModelPtr = pyrPtr_;
72  }
73  else if // Tet Wedge
74  (
75  labels[2] == labels[3]
76  && labels[4] == labels[5]
77  && labels[6] == labels[7]
78  )
79  {
80  regularTypeFlag = 2;
81  curModelPtr = tetWedgePtr_;
82  }
83  else if // Triangular prism
84  (
85  labels[2] == labels[3]
86  && labels[6] == labels[7]
87  )
88  {
89  regularTypeFlag = 3;
90  curModelPtr = prismPtr_;
91  }
92  else if // Wedge
93  (
94  labels[4] == labels[7]
95  )
96  {
97  regularTypeFlag = 4;
98  curModelPtr = wedgePtr_;
99  }
100  else // Hex
101  {
102  regularTypeFlag = 5;
103  curModelPtr = hexPtr_;
104  }
105 
106  labelList regularCellLabels(curModelPtr->nPoints(), -1);
107  // get reference to the addressing list
108  const label* addressing = regularAddressingTable[regularTypeFlag];
109 
110  forAll(regularCellLabels, labelI)
111  {
112  regularCellLabels[labelI] = labels[addressing[labelI]];
113  }
114 
115  cellShapes_[nCreatedCells] = cellShape(*curModelPtr, regularCellLabels);
116 }
117 
118 
119 void Foam::starMesh::addSAMMcell
120 (
121  const labelList& labels,
122  const label nCreatedCells
123 )
124 {
125  // get type, reg and permutation flag
126  label typeFlag = labels[21];
127 // label regularityFlag = labels[22]; // Not used.
128  label permutationFlag = labels[23];
129 
130  // grab the shape from the table
131  label sammTypeFlag = -1;
132  const cellModel* curModelPtr = reinterpret_cast<cellModel*>(0);
133 
134  switch (typeFlag)
135  {
136  case 1:
137  {
138  sammTypeFlag = 1;
139  curModelPtr = sammTrim1Ptr_;
140  break;
141  }
142 
143  case 2:
144  {
145  sammTypeFlag = 2;
146  curModelPtr = sammTrim2Ptr_;
147  break;
148  }
149 
150  case 7:
151  {
152  if (labels[0] != -1)
153  {
154  sammTypeFlag = 3;
155  curModelPtr = sammTrim3Ptr_;
156  }
157  else
158  {
159  sammTypeFlag = 5;
160  curModelPtr = sammTrim5Ptr_;
161  }
162 
163  break;
164  }
165 
166  case 8:
167  {
168  sammTypeFlag = 4;
169  curModelPtr = sammTrim4Ptr_;
170  break;
171  }
172 
173  case 85:
174  {
175  sammTypeFlag = 8;
176  curModelPtr = sammTrim8Ptr_;
177  break;
178  }
179 
180  default:
181  {
183  << "SAMM type " << sammTypeFlag << " is invalid"
184  << abort(FatalError);
185  }
186  }
187 
188  // make a list of labels
189  labelList sammCellLabels(curModelPtr->nPoints(), -1);
190  // get reference to the addressing list
191  const label* addressing = sammAddressingTable[sammTypeFlag];
192 
193  forAll(sammCellLabels, labelI)
194  {
195  sammCellLabels[labelI] = labels[addressing[labelI]];
196  }
197 
198  cellShapes_[nCreatedCells] = cellShape(*curModelPtr, sammCellLabels);
199 
200  // set permutation flag for cell
201  starCellPermutation_[nCreatedCells] = permutationFlag;
202 }
203 
204 
205 void Foam::starMesh::readCells()
206 {
207  label nCells = 0;
208  label maxLabel = -1;
209 
210  fileName cellsFileName(casePrefix_ + ".cel");
211 
212  {
213  IFstream cellsFile(cellsFileName);
214 
215  if (cellsFile.good())
216  {
217  label lineLabel, pointLabel, regionLabel, typeFlag;
218 
219  maxLabel = -1;
220  while (!(cellsFile >> lineLabel).eof())
221  {
222  maxLabel = max(maxLabel, lineLabel);
223  for (int i=0; i<8; i++)
224  {
225  cellsFile >> pointLabel;
226  }
227 
228  cellsFile >> regionLabel;
229  cellsFile >> typeFlag;
230 
231  // lines with typeFlag of zero are continuation lines.
232  if (typeFlag != 0)
233  {
234  nCells++;
235  }
236 
237  // backward compatibility: number of trailing rubbish in
238  // STAR is unknown.
239  // Fixed to cope with missing \n on last line.
240  readToNl(cellsFile);
241  }
242  }
243  else
244  {
246  << "Cannot read file " << cellsFileName
247  << abort(FatalError);
248  }
249  }
250 
251  Info<< "Number of cells = " << nCells << endl << endl;
252 
253  cellShapes_.setSize(nCells);
254  starCellID_.setSize(nCells);
255  starCellPermutation_.setSize(nCells);
256 
257  // reset permutation to invalid value
258  forAll(starCellPermutation_, i)
259  {
260  starCellPermutation_[i] = -1;
261  }
262 
263  starCellLabelLookup_.setSize(maxLabel+1);
264 
265  // reset point labels to invalid value
266  forAll(starCellLabelLookup_, i)
267  {
268  starCellLabelLookup_[i] = -1;
269  }
270 
271  if (nCells > 0)
272  {
273  IFstream cellsFile(cellsFileName);
274 
275  labelList labels(24, label(-1));
276  label lineLabel, starLabel, regionLabel, typeFlag;
277 
278  for (label celli = 0; celli < nCells; celli++)
279  {
280  label nLabels = 0;
281 
282  label addOnToCell = 0;
283 
284  // reset the labels to -1. Debugging.
285  forAll(labels, i)
286  {
287  labels[i] = -1;
288  }
289 
290  do
291  {
292  if ((cellsFile >> lineLabel).eof())
293  {
295  << "Reached end of cells file before "
296  << "all cells are read in."
297  << abort(FatalError);
298  }
299 
300  nLabels += 8;
301 
302  for (int i=nLabels-8; i<nLabels; i++)
303  {
304  cellsFile >> starLabel;
305 
306  if (i < 21)
307  {
308  if (starLabel != 0)
309  {
310  // Convert Star vertex number to point label
311  labels[i] = starPointLabelLookup_[starLabel];
312 
313  if (labels[i] < 0)
314  {
315  Info<< "Cells not consistent with vertex file. "
316  << "Star vertex number " << starLabel
317  << " does not exist\n";
318  }
319  }
320  else
321  {
322  labels[i] = -1;
323  }
324  }
325  else
326  {
327  labels[i] = starLabel;
328  }
329  }
330 
331  cellsFile >> regionLabel;
332  cellsFile >> typeFlag;
333 
334  // check for continuation line
335  if (typeFlag == -1)
336  {
337  addOnToCell = 2;
338  }
339 
340  // backward compatibility: number of trailing rubbish in
341  // STAR is unknown.
342  readToNl(cellsFile);
343 
344  addOnToCell--;
345 
346  } while (addOnToCell >= 0);
347 
348  // Record STAR cell number (used for debugging)
349  starCellID_[celli] = lineLabel;
350 
351  // insert STAR lookup addressing
352  starCellLabelLookup_[lineLabel] = celli;
353 
354  if (nLabels == 8)
355  {
356  addRegularCell(labels, celli);
357  }
358  else
359  {
360  addSAMMcell(labels, celli);
361  }
362 
363  // check cell labels
364  const labelList& curShapeLabels = cellShapes_[celli];
365 
366  forAll(curShapeLabels, i)
367  {
368  if (curShapeLabels[i] < 0)
369  {
371  << "Invalid vertex found in cell " << celli
372  << ". STAR cell no: " << lineLabel
373  << " labels: " << curShapeLabels
374  << abort(FatalError);
375  }
376  }
377  }
378  }
379  else
380  {
382  << "No cells in file " << cellsFileName
383  << abort(FatalError);
384  }
385 }
386 
387 
388 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static const labelSphericalTensor labelI(1)
Identity labelTensor.
List< label > labelList
A List of labels.
Definition: labelList.H:56
errorManip< error > abort(error &err)
Definition: errorManip.H:131
void setSize(const label)
Reset size of List.
Definition: List.C:281
messageStream Info