STARCDsurfaceFormat.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) 2011 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 \*---------------------------------------------------------------------------*/
25 
26 #include "STARCDsurfaceFormat.H"
27 #include "ListOps.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 template<class Face>
33 (
34  Ostream& os,
35  const Face& f,
36  const label cellId,
37  const label cellTableId
38 )
39 {
40  os << cellId // includes 1 offset
41  << ' ' << starcdShellShape_ // 3(shell) shape
42  << ' ' << f.size()
43  << ' ' << cellTableId
44  << ' ' << starcdShellType_; // 4(shell)
45 
46  // primitives have <= 8 vertices, but prevent overrun anyhow
47  // indent following lines for ease of reading
48  label count = 0;
49  forAll(f, fp)
50  {
51  if ((count % 8) == 0)
52  {
53  os << nl << " " << cellId;
54  }
55  os << ' ' << f[fp] + 1;
56  count++;
57  }
58  os << endl;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
64 template<class Face>
66 (
67  const fileName& filename
68 )
69 {
70  read(filename);
71 }
72 
73 
74 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
75 
76 template<class Face>
78 (
79  const fileName& filename
80 )
81 {
82  const bool mustTriangulate = this->isTri();
83  this->clear();
84 
85  fileName baseName = filename.lessExt();
86 
87  // read cellTable names (if possible)
88  Map<word> cellTableLookup;
89 
90  {
91  IFstream is(baseName + ".inp");
92  if (is.good())
93  {
94  cellTableLookup = readInpCellTable(is);
95  }
96  }
97 
98 
99  // STAR-CD index of points
100  List<label> pointId;
101 
102  // read points from .vrt file
103  readPoints
104  (
105  IFstream(baseName + ".vrt")(),
106  this->storedPoints(),
107  pointId
108  );
109 
110  // Build inverse mapping (STAR-CD pointId -> index)
111  Map<label> mapPointId(2*pointId.size());
112  forAll(pointId, i)
113  {
114  mapPointId.insert(pointId[i], i);
115  }
116  pointId.clear();
117 
118  //
119  // read .cel file
120  // ~~~~~~~~~~~~~~
121  IFstream is(baseName + ".cel");
122  if (!is.good())
123  {
125  (
126  "fileFormats::STARCDsurfaceFormat::read(const fileName&)"
127  )
128  << "Cannot read file " << is.name()
129  << exit(FatalError);
130  }
131 
132  readHeader(is, "PROSTAR_CELL");
133 
134  DynamicList<Face> dynFaces;
135  DynamicList<label> dynZones;
136  DynamicList<word> dynNames;
137  DynamicList<label> dynSizes;
139 
140  // assume the cellTableIds are not intermixed
141  bool sorted = true;
142  label zoneI = 0;
143 
144  label lineLabel, shapeId, nLabels, cellTableId, typeId;
145  DynamicList<label> vertexLabels(64);
146 
147  while ((is >> lineLabel).good())
148  {
149  is >> shapeId >> nLabels >> cellTableId >> typeId;
150 
151  vertexLabels.clear();
152  vertexLabels.reserve(nLabels);
153 
154  // read indices - max 8 per line
155  for (label i = 0; i < nLabels; ++i)
156  {
157  label vrtId;
158  if ((i % 8) == 0)
159  {
160  is >> lineLabel;
161  }
162  is >> vrtId;
163 
164  // convert original vertex id to point label
165  vertexLabels.append(mapPointId[vrtId]);
166  }
167 
168  if (typeId == starcdShellType_)
169  {
170  // Convert groupID into zoneID
171  Map<label>::const_iterator fnd = lookup.find(cellTableId);
172  if (fnd != lookup.end())
173  {
174  if (zoneI != fnd())
175  {
176  // cellTableIds are intermixed
177  sorted = false;
178  }
179  zoneI = fnd();
180  }
181  else
182  {
183  zoneI = dynSizes.size();
184  lookup.insert(cellTableId, zoneI);
185 
186  Map<word>::const_iterator tableNameIter =
187  cellTableLookup.find(cellTableId);
188 
189  if (tableNameIter == cellTableLookup.end())
190  {
191  dynNames.append
192  (
193  word("cellTable_") + ::Foam::name(cellTableId)
194  );
195  }
196  else
197  {
198  dynNames.append(tableNameIter());
199  }
200 
201  dynSizes.append(0);
202  }
203 
204  SubList<label> vertices(vertexLabels, vertexLabels.size());
205  if (mustTriangulate && nLabels > 3)
206  {
207  face f(vertices);
208 
209  faceList triFaces(f.nTriangles());
210  label nTri = 0;
211  f.triangles(this->points(), nTri, triFaces);
212 
213  forAll(triFaces, faceI)
214  {
215  // a triangular face, but not yet a triFace
216  dynFaces.append
217  (
218  triFace
219  (
220  static_cast<labelUList&>(triFaces[faceI])
221  )
222  );
223  dynZones.append(zoneI);
224  dynSizes[zoneI]++;
225  }
226  }
227  else
228  {
229  dynFaces.append(Face(vertices));
230  dynZones.append(zoneI);
231  dynSizes[zoneI]++;
232  }
233  }
234  }
235  mapPointId.clear();
236 
237  this->sortFacesAndStore(dynFaces.xfer(), dynZones.xfer(), sorted);
238 
239  // add zones, culling empty ones
240  this->addZones(dynSizes, dynNames, true);
241  return true;
242 }
243 
244 
245 template<class Face>
247 (
248  const fileName& filename,
249  const MeshedSurfaceProxy<Face>& surf
250 )
251 {
252  const pointField& pointLst = surf.points();
253  const List<Face>& faceLst = surf.faces();
254  const List<label>& faceMap = surf.faceMap();
255 
256  const List<surfZone>& zones =
257  (
258  surf.surfZones().empty()
259  ? surfaceFormatsCore::oneZone(faceLst)
260  : surf.surfZones()
261  );
262 
263  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
264 
265 
266  fileName baseName = filename.lessExt();
267 
268  writePoints(OFstream(baseName + ".vrt")(), pointLst);
269  OFstream os(baseName + ".cel");
270  writeHeader(os, "CELL");
271 
272  label faceIndex = 0;
273  forAll(zones, zoneI)
274  {
275  const surfZone& zone = zones[zoneI];
276 
277  if (useFaceMap)
278  {
279  forAll(zone, localFaceI)
280  {
281  const Face& f = faceLst[faceMap[faceIndex++]];
282  writeShell(os, f, faceIndex, zoneI + 1);
283  }
284  }
285  else
286  {
287  forAll(zone, localFaceI)
288  {
289  const Face& f = faceLst[faceIndex++];
290  writeShell(os, f, faceIndex, zoneI + 1);
291  }
292  }
293  }
294 
295  // write simple .inp file
296  writeCase
297  (
298  OFstream(baseName + ".inp")(),
299  pointLst,
300  faceLst.size(),
301  zones
302  );
303 }
304 
305 
306 // ************************************************************************* //
Output to file stream.
Definition: OFstream.H:81
const pointField & points
label cellId
virtual bool read(const fileName &)
Read from file.
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
bool useFaceMap() const
Use faceMap?
Read/write the surface shells from pro-STAR vrt/cel files.
An STL-conforming const_iterator.
Definition: HashTable.H:470
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
labelList f(nPoints)
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:242
A class for handling words, derived from string.
Definition: word.H:59
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
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:310
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Various functions to operate on Lists.
label size() const
The surface size is the number of faces.
const pointField & points() const
Return const access to the points.
Input from file stream.
Definition: IFstream.H:81
const List< Face > & faces() const
Return const access to the faces.
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
stressControl lookup("compactNormalStress") >> compactNormalStress
Base class for zones.
Definition: zone.H:57
const List< surfZone > & surfZones() const
Const access to the surface zones.
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:301
#define forAll(list, i)
Definition: UList.H:421
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
error FatalError
A class for handling file names.
Definition: fileName.H:69
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:307
A triangular face using a FixedList of labels corresponding to mesh vertices.
Definition: triFace.H:68
bool read(const char *, int32_t &)
Definition: int32IO.C:87
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
A List obtained as a section of another List.
Definition: SubList.H:53
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:139
UEqn clear()
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:130
label nTriangles() const
Number of triangles after splitting.
Definition: faceI.H:130
label triangles(const pointField &points, label &triI, faceList &triFaces) const
Split into triangles using existing points.
Definition: face.C:835