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 SAMM files
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "sammMesh.H"
30 #include "IFstream.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 void Foam::sammMesh::addRegularCell
35 (
36  const labelList& labels,
37  const label nCreatedCells
38 )
39 {
40  // Memory 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 Foam::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  << "SAMM type " << typeFlag << " has no registered label. BUG!"
151  << abort(FatalError);
152  }
153 
154  const cellModel& curModel = *(sammShapeLookup[typeFlag]);
155 
156  // get reference to the addressing list
157  const label* addressing = sammAddressingTable[typeFlag];
158 
159  // make a list of labels
160  labelList sammCellLabels(curModel.nPoints(), -1);
161 
162  forAll(sammCellLabels, labelI)
163  {
164  sammCellLabels[labelI] = globalLabels[addressing[labelI]];
165  }
166 
167  cellShapes_[nCreatedCells] = cellShape(curModel, sammCellLabels);
168 }
169 
170 
171 void Foam::sammMesh::readCells()
172 {
173  label nCells = 0;
174  label maxLabel = -1;
175 
176  fileName cellsFileName(casePrefix_ + ".cel");
177 
178  {
179  IFstream cellsFile(cellsFileName);
180 
181  if (cellsFile.good())
182  {
183  label lineLabel, cellLabel = -1, pointLabel, regionLabel, typeFlag;
184 
185  maxLabel = -1;
186  while (!(cellsFile >> lineLabel).eof())
187  {
188  maxLabel = max(maxLabel, lineLabel);
189  for (int i=0; i<8; i++)
190  {
191  cellsFile >> pointLabel;
192  }
193 
194  cellsFile >> regionLabel;
195  cellsFile >> typeFlag;
196 
197  if (lineLabel != cellLabel)
198  {
199  cellLabel = lineLabel;
200  nCells++;
201  }
202  }
203  }
204  else
205  {
207  << "Cannot read file "
208  << cellsFileName
209  << abort(FatalError);
210  }
211  }
212 
213  Info<< "Number of cells = " << nCells << endl << endl;
214 
215  cellShapes_.setSize(nCells);
216 
217  starCellLabelLookup_.setSize(maxLabel+1);
218 
219  // reset point labels to invalid value
220  forAll(starCellLabelLookup_, i)
221  {
222  starCellLabelLookup_[i] = -1;
223  }
224 
225 
226  if (nCells > 0)
227  {
228  IFstream cellsFile(cellsFileName);
229 
230  labelList labels(24, label(-1));
231  label lineLabel, sammLabel, regionLabel, typeFlag;
232 
233  for (label celli = 0; celli < nCells; celli++)
234  {
235  label nLabels = 0;
236 
237  bool addOnToCell = false;
238 
239  do
240  {
241  if (nLabels > 24)
242  {
244  << "Unknown SAMM cell. "
245  << "More than 24 vertices"
246  << abort(FatalError);
247  }
248 
249  if ((cellsFile >> lineLabel).eof())
250  {
252  << "Reached end of cells file before "
253  << "all cells are read in."
254  << abort(FatalError);
255  }
256 
257  // prepare for possible continuation
258  nLabels += 8;
259 
260  for (int i=nLabels-8; i<nLabels; i++)
261  {
262  cellsFile >> sammLabel;
263 
264  if (sammLabel != 0)
265  {
266  // Convert Samm vertex number to point label
267  labels[i] = starPointLabelLookup_[sammLabel];
268 
269  if (labels[i] < 0)
270  {
271  Info<< "Cell file not consistent with vertex file. "
272  << "Samm vertex number " << sammLabel
273  << " does not exist\n";
274  }
275  }
276  else
277  {
278  labels[i] = -1;
279  }
280  }
281 
282  cellsFile >> regionLabel;
283  cellsFile >> typeFlag;
284 
285  // check for continuation line
286  if (!addOnToCell && typeFlag == 255)
287  {
288  addOnToCell = true;
289  }
290  else
291  {
292  addOnToCell = false;
293  }
294 
295  } while (typeFlag == -1 || addOnToCell);
296 
297  starCellLabelLookup_[lineLabel] = celli;
298 
299  if (nLabels == 8)
300  {
301  addRegularCell(labels, celli);
302  }
303  else
304  {
305  addSAMMcell(typeFlag, labels, celli);
306  }
307  }
308  }
309  else
310  {
312  << "No cells in file "
313  << cellsFileName
314  << abort(FatalError);
315  }
316 }
317 
318 
319 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
layerAndWeight max(const layerAndWeight &a, const layerAndWeight &b)
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
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