STARCDsurfaceFormat.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-2018 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  << "Cannot read file " << is.name()
126  << exit(FatalError);
127  }
128 
129  readHeader(is, "PROSTAR_CELL");
130 
131  DynamicList<Face> dynFaces;
132  DynamicList<label> dynZones;
133  DynamicList<word> dynNames;
134  DynamicList<label> dynSizes;
136 
137  // assume the cellTableIds are not intermixed
138  bool sorted = true;
139  label zoneI = 0;
140 
141  label lineLabel, shapeId, nLabels, cellTableId, typeId;
142  DynamicList<label> vertexLabels(64);
143 
144  while ((is >> lineLabel).good())
145  {
146  is >> shapeId >> nLabels >> cellTableId >> typeId;
147 
148  vertexLabels.clear();
149  vertexLabels.reserve(nLabels);
150 
151  // read indices - max 8 per line
152  for (label i = 0; i < nLabels; ++i)
153  {
154  label vrtId;
155  if ((i % 8) == 0)
156  {
157  is >> lineLabel;
158  }
159  is >> vrtId;
160 
161  // convert original vertex id to point label
162  vertexLabels.append(mapPointId[vrtId]);
163  }
164 
165  if (typeId == starcdShellType_)
166  {
167  // Convert groupID into zoneID
168  Map<label>::const_iterator fnd = lookup.find(cellTableId);
169  if (fnd != lookup.end())
170  {
171  if (zoneI != fnd())
172  {
173  // cellTableIds are intermixed
174  sorted = false;
175  }
176  zoneI = fnd();
177  }
178  else
179  {
180  zoneI = dynSizes.size();
181  lookup.insert(cellTableId, zoneI);
182 
183  Map<word>::const_iterator tableNameIter =
184  cellTableLookup.find(cellTableId);
185 
186  if (tableNameIter == cellTableLookup.end())
187  {
188  dynNames.append
189  (
190  word("cellTable_") + ::Foam::name(cellTableId)
191  );
192  }
193  else
194  {
195  dynNames.append(tableNameIter());
196  }
197 
198  dynSizes.append(0);
199  }
200 
201  SubList<label> vertices(vertexLabels, vertexLabels.size());
202  if (mustTriangulate && nLabels > 3)
203  {
204  face f(vertices);
205 
206  faceList triFaces(f.nTriangles());
207  label nTri = 0;
208  f.triangles(this->points(), nTri, triFaces);
209 
210  forAll(triFaces, facei)
211  {
212  // a triangular face, but not yet a triFace
213  dynFaces.append
214  (
215  triFace
216  (
217  static_cast<labelUList&>(triFaces[facei])
218  )
219  );
220  dynZones.append(zoneI);
221  dynSizes[zoneI]++;
222  }
223  }
224  else
225  {
226  dynFaces.append(Face(vertices));
227  dynZones.append(zoneI);
228  dynSizes[zoneI]++;
229  }
230  }
231  }
232  mapPointId.clear();
233 
234  this->sortFacesAndStore(dynFaces.xfer(), dynZones.xfer(), sorted);
235 
236  // add zones, culling empty ones
237  this->addZones(dynSizes, dynNames, true);
238  return true;
239 }
240 
241 
242 template<class Face>
244 (
245  const fileName& filename,
246  const MeshedSurfaceProxy<Face>& surf
247 )
248 {
249  const pointField& pointLst = surf.points();
250  const List<Face>& faceLst = surf.faces();
251  const List<label>& faceMap = surf.faceMap();
252 
253  const List<surfZone>& zones =
254  (
255  surf.surfZones().empty()
256  ? surfaceFormatsCore::oneZone(faceLst)
257  : surf.surfZones()
258  );
259 
260  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
261 
262 
263  fileName baseName = filename.lessExt();
264 
265  writePoints(OFstream(baseName + ".vrt")(), pointLst);
266  OFstream os(baseName + ".cel");
267  writeHeader(os, "CELL");
268 
269  label faceIndex = 0;
270  forAll(zones, zoneI)
271  {
272  const surfZone& zone = zones[zoneI];
273 
274  if (useFaceMap)
275  {
276  forAll(zone, localFacei)
277  {
278  const Face& f = faceLst[faceMap[faceIndex++]];
279  writeShell(os, f, faceIndex, zoneI + 1);
280  }
281  }
282  else
283  {
284  forAll(zone, localFacei)
285  {
286  const Face& f = faceLst[faceIndex++];
287  writeShell(os, f, faceIndex, zoneI + 1);
288  }
289  }
290  }
291 
292  // write simple .inp file
293  writeCase
294  (
295  OFstream(baseName + ".inp")(),
296  pointLst,
297  faceLst.size(),
298  zones
299  );
300 }
301 
302 
303 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
A class for handling file names.
Definition: fileName.H:69
Read/write the surface shells from pro-STAR vrt/cel files.
An STL-conforming const_iterator.
Definition: HashTable.H:481
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:75
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:110
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tUEqn clear()
Output to file stream.
Definition: OFstream.H:82
A surface zone on a MeshedSurface.
Definition: surfZone.H:62
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
virtual bool read(const fileName &)
Read from file.
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
label triangles(const pointField &points, label &triI, faceList &triFaces) const
Split into triangles using existing points.
Definition: face.C:837
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
void reserve(const label)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:140
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Various functions to operate on Lists.
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
static void write(const fileName &, const MeshedSurfaceProxy< Face > &)
Write surface mesh components by proxy.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:142
pointField vertices(const blockVertexList &bvl)
bool useFaceMap() const
Use faceMap?
A List obtained as a section of another List.
Definition: SubList.H:53
stressControl lookup("compactNormalStress") >> compactNormalStress
const pointField & points
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
Base class for zones.
Definition: zone.H:57
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
const List< Face > & faces() const
Return const access to the faces.
A class for handling words, derived from string.
Definition: word.H:59
A triangular face using a FixedList of labels corresponding to mesh vertices.
Definition: triFace.H:68
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
Xfer< List< T > > xfer()
Transfer contents to the Xfer container as a plain List.
Definition: DynamicListI.H:283
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:292
const List< surfZone > & surfZones() const
Const access to the surface zones.
static const char nl
Definition: Ostream.H:265
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:73
Input from file stream.
Definition: IFstream.H:81
labelList f(nPoints)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
label size() const
The surface size is the number of faces.
const pointField & points() const
Return const access to the points.
const List< label > & faceMap() const
Const access to the faceMap, zero-sized when unused.
label cellId
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileName.C:268
label nTriangles() const
Number of triangles after splitting.
Definition: faceI.H:130
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:224
A HashTable to objects of type <T> with a label key.
Definition: Map.H:49