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 SAMM files
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "sammMesh.H"
30 #include <OpenFOAM/IFstream.H>
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 void sammMesh::addRegularCell
35 (
36  const labelList& labels,
37  const label nCreatedCells
38 )
39 {
40  // Momory management
41  static labelList labelsHex(8);
42  static labelList labelsWedge(7);
43  static labelList labelsPrism(6);
44  static labelList labelsPyramid(5);
45  static labelList labelsTet(4);
46  static labelList labelsTetWedge(5);
47 
48  if // Tetrahedron
49  (
50  labels[2] == labels[3]
51  && labels[4] == labels[5]
52  && labels[5] == labels[6]
53  && labels[6] == labels[7]
54  )
55  {
56  labelsTet[0] = labels[0];
57  labelsTet[1] = labels[1];
58  labelsTet[2] = labels[2];
59  labelsTet[3] = labels[4];
60  cellShapes_[nCreatedCells] = cellShape(*tetPtr_, labelsTet);
61  }
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  labelsPyramid[0] = labels[0];
71  labelsPyramid[1] = labels[1];
72  labelsPyramid[2] = labels[2];
73  labelsPyramid[3] = labels[3];
74  labelsPyramid[4] = labels[4];
75  cellShapes_[nCreatedCells] = cellShape(*pyrPtr_, labelsPyramid);
76  }
77 
78  else if // Tet Wedge
79  (
80  labels[2] == labels[3]
81  && labels[4] == labels[5]
82  && labels[6] == labels[7]
83  )
84  {
85  labelsTetWedge[0] = labels[0];
86  labelsTetWedge[1] = labels[1];
87  labelsTetWedge[2] = labels[2];
88  labelsTetWedge[3] = labels[4];
89  labelsTetWedge[4] = labels[6];
90  cellShapes_[nCreatedCells] = cellShape(*tetWedgePtr_, labelsTetWedge);
91  }
92 
93  else if // Triangular prism
94  (
95  labels[2] == labels[3]
96  && labels[6] == labels[7]
97  )
98  {
99  labelsPrism[0] = labels[0];
100  labelsPrism[1] = labels[1];
101  labelsPrism[2] = labels[2];
102  labelsPrism[3] = labels[4];
103  labelsPrism[4] = labels[5];
104  labelsPrism[5] = labels[6];
105  cellShapes_[nCreatedCells] = cellShape(*prismPtr_, labelsPrism);
106  }
107 
108  else if // Wedge
109  (
110  labels[4] == labels[7]
111  )
112  {
113  labelsWedge[0] = labels[7];
114  labelsWedge[1] = labels[6];
115  labelsWedge[2] = labels[5];
116  labelsWedge[3] = labels[3];
117  labelsWedge[4] = labels[2];
118  labelsWedge[5] = labels[1];
119  labelsWedge[6] = labels[0];
120  cellShapes_[nCreatedCells] = cellShape(*wedgePtr_, labelsWedge);
121  }
122 
123  else // Hex
124  {
125  labelsHex[0] = labels[0];
126  labelsHex[1] = labels[1];
127  labelsHex[2] = labels[2];
128  labelsHex[3] = labels[3];
129  labelsHex[4] = labels[4];
130  labelsHex[5] = labels[5];
131  labelsHex[6] = labels[6];
132  labelsHex[7] = labels[7];
133  cellShapes_[nCreatedCells] = cellShape(*hexPtr_, labelsHex);
134  }
135 }
136 
137 
138 void sammMesh::addSAMMcell
139 (
140  const label typeFlag,
141  const labelList& globalLabels,
142  const label nCreatedCells
143 )
144 {
145 
146  // grab the shape from the table
147  if (!sammShapeLookup[typeFlag] || !sammAddressingTable[typeFlag])
148  {
150  (
151  "sammMesh::addRegularCell(const labelList& labels, "
152  "const label nCreatedCells)"
153  ) << "SAMM type " << typeFlag << " has no registered label. BUG!"
154  << abort(FatalError);
155  }
156 
157  const cellModel& curModel = *(sammShapeLookup[typeFlag]);
158 
159  // get reference to the addressing list
160  const label* addressing = sammAddressingTable[typeFlag];
161 
162  // make a list of labels
163  labelList sammCellLabels(curModel.nPoints(), -1);
164 
165  forAll (sammCellLabels, labelI)
166  {
167  sammCellLabels[labelI] = globalLabels[addressing[labelI]];
168  }
169 
170  cellShapes_[nCreatedCells] = cellShape(curModel, sammCellLabels);
171 }
172 
173 
174 void sammMesh::readCells()
175 {
176  label nCells = 0;
177  label maxLabel = -1;
178 
179  fileName cellsFileName(casePrefix_ + ".cel");
180 
181  {
182  IFstream cellsFile(cellsFileName);
183 
184  if (cellsFile.good())
185  {
186  label lineLabel, cellLabel = -1, pointLabel, regionLabel, typeFlag;
187 
188  maxLabel = -1;
189  while (!(cellsFile >> lineLabel).eof())
190  {
191  maxLabel = max(maxLabel, lineLabel);
192  for (int i=0; i<8; i++)
193  {
194  cellsFile >> pointLabel;
195  }
196 
197  cellsFile >> regionLabel;
198  cellsFile >> typeFlag;
199 
200  if (lineLabel != cellLabel)
201  {
202  cellLabel = lineLabel;
203  nCells++;
204  }
205  }
206  }
207  else
208  {
209  FatalErrorIn("sammMesh::readCells()")
210  << "Cannot read file "
211  << cellsFileName
212  << abort(FatalError);
213  }
214  }
215 
216  Info<< "Number of cells = " << nCells << endl << endl;
217 
218  cellShapes_.setSize(nCells);
219 
220  starCellLabelLookup_.setSize(maxLabel+1);
221 
222  // reset point labels to invalid value
223  forAll (starCellLabelLookup_, i)
224  {
225  starCellLabelLookup_[i] = -1;
226  }
227 
228 
229  if (nCells > 0)
230  {
231  IFstream cellsFile(cellsFileName);
232 
233  labelList labels(24, -1);
234  label lineLabel, sammLabel, regionLabel, typeFlag;
235 
236  for (label cellI = 0; cellI < nCells; cellI++)
237  {
238  label nLabels = 0;
239 
240  bool addOnToCell = false;
241 
242  do
243  {
244  if (nLabels > 24)
245  {
246  FatalErrorIn("sammMesh::readCells()")
247  << "Unknown SAMM cell. "
248  << "More than 24 vertices"
249  << abort(FatalError);
250  }
251 
252  if ((cellsFile >> lineLabel).eof())
253  {
254  FatalErrorIn("sammMesh::readCells()")
255  << "Reached end of cells file before "
256  << "all cells are read in."
257  << abort(FatalError);
258  }
259 
260  // prepare for possible continuation
261  nLabels += 8;
262 
263  for (int i=nLabels-8; i<nLabels; i++)
264  {
265  cellsFile >> sammLabel;
266 
267  if (sammLabel != 0)
268  {
269  // Convert Samm vertex number to point label
270  labels[i] = starPointLabelLookup_[sammLabel];
271 
272  if (labels[i] < 0)
273  {
274  Info<< "Cell file not consistent with vertex file. "
275  << "Samm vertex number " << sammLabel
276  << " does not exist\n";
277  }
278  }
279  else
280  {
281  labels[i] = -1;
282  }
283  }
284 
285  cellsFile >> regionLabel;
286  cellsFile >> typeFlag;
287 
288  // check for continuation line
289  if (!addOnToCell && typeFlag == 255)
290  {
291  addOnToCell = true;
292  }
293  else
294  {
295  addOnToCell = false;
296  }
297 
298  } while (typeFlag == -1 || addOnToCell);
299 
300  starCellLabelLookup_[lineLabel] = cellI;
301 
302  if (nLabels == 8)
303  {
304  addRegularCell(labels, cellI);
305  }
306  else
307  {
308  addSAMMcell(typeFlag, labels, cellI);
309  }
310  }
311  }
312  else
313  {
314  FatalErrorIn("sammMesh::readCells()")
315  << "No cells in file "
316  << cellsFileName
317  << abort(FatalError);
318  }
319 }
320 
321 
322 // ************************ vim: set sw=4 sts=4 et: ************************ //