FreeFOAM The Cross-Platform CFD Toolkit
readCells.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/IFstream.H>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 void starMesh::addRegularCell
35 (
36  const labelList& labels,
37  const label nCreatedCells
38 )
39 {
40  // Momory 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 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  (
184  "starMesh::addSAMMcell"
185  "(const labelList& labels, const label nCreatedCells)"
186  ) << "SAMM type " << sammTypeFlag << " is invalid"
187  << abort(FatalError);
188  }
189  }
190 
191  // make a list of labels
192  labelList sammCellLabels(curModelPtr->nPoints(), -1);
193  // get reference to the addressing list
194  const label* addressing = sammAddressingTable[sammTypeFlag];
195 
196  forAll (sammCellLabels, labelI)
197  {
198  sammCellLabels[labelI] = labels[addressing[labelI]];
199  }
200 
201  cellShapes_[nCreatedCells] = cellShape(*curModelPtr, sammCellLabels);
202 
203  // set permutation flag for cell
204  starCellPermutation_[nCreatedCells] = permutationFlag;
205 }
206 
207 
208 void starMesh::readCells()
209 {
210  label nCells = 0;
211  label maxLabel = -1;
212 
213  fileName cellsFileName(casePrefix_ + ".cel");
214 
215  {
216  IFstream cellsFile(cellsFileName);
217 
218  if (cellsFile.good())
219  {
220  label lineLabel, pointLabel, regionLabel, typeFlag;
221 
222  maxLabel = -1;
223  while (!(cellsFile >> lineLabel).eof())
224  {
225  maxLabel = max(maxLabel, lineLabel);
226  for (int i=0; i<8; i++)
227  {
228  cellsFile >> pointLabel;
229  }
230 
231  cellsFile >> regionLabel;
232  cellsFile >> typeFlag;
233 
234  // lines with typeFlag of zero are continuation lines.
235  if (typeFlag != 0)
236  {
237  nCells++;
238  }
239 
240  // backward compatibility: number of trailing rubbish in
241  // STAR is unknown.
242  // Fixed to cope with missing \n on last line.
243  readToNl(cellsFile);
244  }
245  }
246  else
247  {
248  FatalErrorIn("starMesh::readCells()")
249  << "Cannot read file " << cellsFileName
250  << abort(FatalError);
251  }
252  }
253 
254  Info<< "Number of cells = " << nCells << endl << endl;
255 
256  cellShapes_.setSize(nCells);
257  starCellID_.setSize(nCells);
258  starCellPermutation_.setSize(nCells);
259 
260  // reset permutation to invalid value
261  forAll (starCellPermutation_, i)
262  {
263  starCellPermutation_[i] = -1;
264  }
265 
266  starCellLabelLookup_.setSize(maxLabel+1);
267 
268  // reset point labels to invalid value
269  forAll (starCellLabelLookup_, i)
270  {
271  starCellLabelLookup_[i] = -1;
272  }
273 
274  if (nCells > 0)
275  {
276  IFstream cellsFile(cellsFileName);
277 
278  labelList labels(24, -1);
279  label lineLabel, starLabel, regionLabel, typeFlag;
280 
281  for (label cellI = 0; cellI < nCells; cellI++)
282  {
283  label nLabels = 0;
284 
285  label addOnToCell = 0;
286 
287  // reset the labels to -1. Debugging.
288  forAll (labels, i)
289  {
290  labels[i] = -1;
291  }
292 
293  do
294  {
295  if ((cellsFile >> lineLabel).eof())
296  {
297  FatalErrorIn("starMesh::readCells()")
298  << "Reached end of cells file before "
299  << "all cells are read in."
300  << abort(FatalError);
301  }
302 
303  nLabels += 8;
304 
305  for (int i=nLabels-8; i<nLabels; i++)
306  {
307  cellsFile >> starLabel;
308 
309  if (i < 21)
310  {
311  if (starLabel != 0)
312  {
313  // Convert Star vertex number to point label
314  labels[i] = starPointLabelLookup_[starLabel];
315 
316  if (labels[i] < 0)
317  {
318  Info<< "Cells not consistent with vertex file. "
319  << "Star vertex number " << starLabel
320  << " does not exist\n";
321  }
322  }
323  else
324  {
325  labels[i] = -1;
326  }
327  }
328  else
329  {
330  labels[i] = starLabel;
331  }
332  }
333 
334  cellsFile >> regionLabel;
335  cellsFile >> typeFlag;
336 
337  // check for continuation line
338  if (typeFlag == -1)
339  {
340  addOnToCell = 2;
341  }
342 
343  // backward compatibility: number of trailing rubbish in
344  // STAR is unknown.
345  readToNl(cellsFile);
346 
347  addOnToCell--;
348 
349  } while (addOnToCell >= 0);
350 
351  // Record STAR cell number (used for debugging)
352  starCellID_[cellI] = lineLabel;
353 
354  // insert STAR lookup addressing
355  starCellLabelLookup_[lineLabel] = cellI;
356 
357  if (nLabels == 8)
358  {
359  addRegularCell(labels, cellI);
360  }
361  else
362  {
363  addSAMMcell(labels, cellI);
364  }
365 
366  // check cell labels
367  const labelList& curShapeLabels = cellShapes_[cellI];
368 
369  forAll (curShapeLabels, i)
370  {
371  if (curShapeLabels[i] < 0)
372  {
373  FatalErrorIn("starMesh::readCells()")
374  << "Invalid vertex found in cell " << cellI
375  << ". STAR cell no: " << lineLabel
376  << " labels: " << curShapeLabels
377  << abort(FatalError);
378  }
379  }
380  }
381  }
382  else
383  {
384  FatalErrorIn("starMesh::readCells()")
385  << "No cells in file " << cellsFileName
386  << abort(FatalError);
387  }
388 }
389 
390 
391 // ************************ vim: set sw=4 sts=4 et: ************************ //