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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2024 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "STARCDsurfaceFormat.H"
30 #include "ListOps.H"
31 #include "faceTraits.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 template<class Face>
37 (
38  Ostream& os,
39  const Face& f,
40  const label cellId,
41  const label cellTableId
42 )
43 {
44  os << (cellId + 1)
45  << ' ' << starcdShell // 3(shell) shape
46  << ' ' << f.size()
47  << ' ' << (cellTableId + 1)
48  << ' ' << starcdShellType; // 4(shell)
49 
50  // Primitives have <= 8 vertices, but prevent overrun anyhow
51  // indent following lines for ease of reading
52  label count = 0;
53  for (const label pointi : f)
54  {
55  if ((count % 8) == 0)
56  {
57  os << nl << " " << (cellId + 1);
58  }
59  os << ' ' << (pointi + 1);
60  ++count;
61  }
62  os << nl;
63 }
64 
65 
66 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
67 
68 template<class Face>
70 (
71  const fileName& filename
72 )
73 {
74  read(filename);
75 }
76 
77 
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79 
80 template<class Face>
82 (
83  const fileName& filename
84 )
85 {
86  // Clear everything
87  this->clear();
88 
89  const fileName prefix(filename.lessExt());
90 
91  // Read cellTable names (if possible)
92  Map<word> cellTableLookup = readInpCellTable
93  (
94  IFstream(starFileName(prefix, STARCDCore::INP_FILE))()
95  );
96 
97 
98  // STARCD index of points
99  List<label> pointId;
100 
101  // read points from .vrt file
102  readPoints
103  (
104  IFstream(starFileName(prefix, STARCDCore::VRT_FILE))(),
105  this->storedPoints(),
106  pointId
107  );
108 
109  // Build inverse mapping (STARCD pointId -> index)
110  Map<label> mapPointId(invertToMap(pointId));
111  pointId.clear();
112 
113 
114  // Read .cel file
115  // ~~~~~~~~~~~~~~
116  IFstream is(starFileName(prefix, STARCDCore::CEL_FILE));
117  if (!is.good())
118  {
120  << "Cannot read file " << is.name() << nl
121  << exit(FatalError);
122  }
123 
124  readHeader(is, STARCDCore::HEADER_CEL);
125 
126  DynamicList<label> dynElemId; // STARCD element id (1-based)
127  DynamicList<Face> dynFaces;
128 
129  DynamicList<label> dynZones;
130  DynamicList<word> dynNames;
131  DynamicList<label> dynSizes;
133 
134  // Assume the cellTableIds are not intermixed
135  bool sorted = true;
136  label zoneId = 0;
137 
138  // Element id gets trashed with decompose into a triangle!
139  bool ignoreElemId = false;
140 
141  label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
142  DynamicList<label> vertexLabels(64);
143 
144  token tok;
145 
146  while (is.read(tok).good() && tok.isLabel())
147  {
148  // First token is the element id (1-based)
149  label elemId = tok.labelToken();
150 
151  is >> shapeId
152  >> nLabels
153  >> cellTableId
154  >> typeId;
155 
156  vertexLabels.clear();
157  vertexLabels.reserve(nLabels);
158 
159  // Read indices - max 8 per line
160  for (label i = 0; i < nLabels; ++i)
161  {
162  label vrtId;
163  if ((i % 8) == 0)
164  {
165  is >> ignoredLabel; // Skip cellId for continuation lines
166  }
167  is >> vrtId;
168 
169  // Convert original vertex id to point label
170  vertexLabels.append(mapPointId[vrtId]);
171  }
172 
173  if (typeId == starcdShellType)
174  {
175  // Convert cellTableId to zoneId
176  const auto iterGroup = lookup.cfind(cellTableId);
177  if (iterGroup.good())
178  {
179  if (zoneId != *iterGroup)
180  {
181  // cellTableIds are intermixed
182  sorted = false;
183  }
184  zoneId = *iterGroup;
185  }
186  else
187  {
188  zoneId = dynSizes.size();
189  lookup.insert(cellTableId, zoneId);
190 
191  const auto iterTableName = cellTableLookup.cfind(cellTableId);
192 
193  if (iterTableName.good())
194  {
195  dynNames.append(*iterTableName);
196  }
197  else
198  {
199  dynNames.append("cellTable_" + ::Foam::name(cellTableId));
200  }
201 
202  dynSizes.append(0);
203  }
204 
205  SubList<label> vertices(vertexLabels, vertexLabels.size());
206  if (faceTraits<Face>::isTri() && nLabels > 3)
207  {
208  // The face needs triangulation
209  ignoreElemId = true;
210  dynElemId.clear();
211 
212  face f(vertices);
213 
214  faceList trias(f.nTriangles());
215  label nTri = 0;
216  f.triangles(this->points(), nTri, trias);
217 
218  for (const face& tri : trias)
219  {
220  // A triangular 'face', convert to 'triFace' etc
221  dynFaces.append(Face(tri));
222  dynZones.append(zoneId);
223  dynSizes[zoneId]++;
224  }
225  }
226  else if (nLabels >= 3)
227  {
228  --elemId; // Convert 1-based -> 0-based
229  dynElemId.append(elemId);
230 
231  dynFaces.append(Face(vertices));
232  dynZones.append(zoneId);
233  dynSizes[zoneId]++;
234  }
235  }
236  }
237  mapPointId.clear();
238 
239 
240  if (ignoreElemId)
241  {
242  dynElemId.clear();
243  }
244 
245 
246  this->sortFacesAndStore(dynFaces, dynZones, dynElemId, sorted);
247 
248  // Add zones (retaining empty ones)
249  this->addZones(dynSizes, dynNames);
250  this->addZonesToFaces(); // for labelledTri
252  return true;
253 }
254 
255 
256 template<class Face>
258 (
259  const fileName& filename,
260  const MeshedSurfaceProxy<Face>& surf,
261  IOstreamOption streamOpt,
262  const dictionary&
263 )
264 {
265  // ASCII only, allow output compression
266  streamOpt.format(IOstreamOption::ASCII);
267 
268  const UList<point>& pointLst = surf.points();
269  const UList<Face>& faceLst = surf.surfFaces();
270  const UList<label>& faceMap = surf.faceMap();
271  const UList<label>& elemIds = surf.faceIds();
272 
273  const surfZoneList zones =
274  (
275  surf.surfZones().empty()
276  ? surfaceFormatsCore::oneZone(faceLst)
277  : surf.surfZones()
278  );
279 
280  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
281 
282  // Possible to use faceIds?
283  // - cannot if there are negative ids (eg, encoded solid/side)
284  const bool useOrigFaceIds =
285  (
286  !useFaceMap
287  && elemIds.size() == faceLst.size()
288  && !ListOps::found(elemIds, lessOp1<label>(0))
289  );
290 
291 
292  const fileName prefix(filename.lessExt());
293 
294  // The .vrt file
295  {
296  OFstream os(starFileName(prefix, STARCDCore::VRT_FILE), streamOpt);
297  writePoints(os, pointLst);
298  }
299 
300  // The .cel file
301  OFstream os(starFileName(prefix, STARCDCore::CEL_FILE), streamOpt);
302  writeHeader(os, STARCDCore::HEADER_CEL);
303 
304  label faceIndex = 0;
305  label zoneIndex = 0;
306  label elemId = 0;
307  for (const surfZone& zone : zones)
308  {
309  for (label nLocal = zone.size(); nLocal--; ++faceIndex)
310  {
311  const label facei =
312  (useFaceMap ? faceMap[faceIndex] : faceIndex);
313 
314  const Face& f = faceLst[facei];
315 
316  if (useOrigFaceIds)
317  {
318  elemId = elemIds[facei];
319  }
320 
321  writeShell(os, f, elemId, zoneIndex);
322  ++elemId;
323  }
324 
325  ++zoneIndex;
326  }
327 
328  // Simple .inp file - always UNCOMPRESSED
329  {
330  OFstream os(starFileName(prefix, STARCDCore::INP_FILE));
331 
332  writeCase
333  (
334  os,
335  pointLst,
336  faceLst.size(),
337  zones
338  );
339  }
340 }
341 
342 
343 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
A class for handling file names.
Definition: fileName.H:72
Read/write the surface shells from PROSTAR vrt/cel files.
static void writeHeader(Ostream &os, const word &fieldName)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
STARCDsurfaceFormat(const fileName &filename)
Construct from file name.
Output to file stream as an OSstream, normally using std::ofstream for the actual output...
Definition: OFstream.H:71
A surface zone on a MeshedSurface.
Definition: surfZone.H:52
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A token holds an item read from Istream.
Definition: token.H:65
A simple container for options an IOstream can normally have.
Lookup type of boundary radiation properties.
Definition: lookup.H:57
const pointField & points() const noexcept
Return const access to the points.
virtual bool read(const fileName &filename) override
Read from file.
const labelUList & faceMap() const noexcept
Const access to the faceMap, zero-sized when unused.
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 &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:152
List< face > faceList
List of faces.
Definition: faceListFwd.H:39
pointField vertices(const blockVertexList &bvl)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition: ISstream.H:147
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const pointField & points
bool useFaceMap() const noexcept
Can/should use faceMap?
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition: HashTableI.H:113
Base class for mesh zones.
Definition: zone.H:59
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:130
void clear()
Remove all entries from table.
Definition: HashTable.C:749
const UList< surfZone > & surfZones() const noexcept
Const access to the surface zones.
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicList.H:584
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &=dictionary::null)
Write surface mesh components by proxy.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats...
Definition: MeshedSurface.H:75
Input from file stream as an ISstream, normally using std::ifstream for the actual input...
Definition: IFstream.H:51
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
labelList f(nPoints)
virtual Istream & read(token &t) override
Return next token from stream.
Definition: ISstream.C:535
const labelUList & faceIds() const noexcept
Const access to the faceIds, zero-sized when unused.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
surface1 clear()
label cellId
label labelToken() const
Return label value.
Definition: tokenI.H:633
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:238
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
const UList< Face > & surfFaces() const noexcept
Return const access to the faces.
Map< label > invertToMap(const labelUList &values)
Create inverse mapping, which is a lookup table into the given list.
Definition: ListOps.C:107
bool found
streamFormat format() const noexcept
Get the current stream format.
A HashTable to objects of type <T> with a label key.