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