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-2020 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  fileName baseName = filename.lessExt();
90 
91  // Read cellTable names (if possible)
92  Map<word> cellTableLookup = readInpCellTable
93  (
94  IFstream(starFileName(baseName, 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(baseName, STARCDCore::VRT_FILE))(),
105  this->storedPoints(),
106  pointId
107  );
108 
109  // Build inverse mapping (STARCD pointId -> index)
110  Map<label> mapPointId(2*pointId.size());
111  forAll(pointId, i)
112  {
113  mapPointId.insert(pointId[i], i);
114  }
115  pointId.clear();
116 
117 
118  // Read .cel file
119  // ~~~~~~~~~~~~~~
120  IFstream is(starFileName(baseName, STARCDCore::CEL_FILE));
121  if (!is.good())
122  {
124  << "Cannot read file " << is.name() << nl
125  << exit(FatalError);
126  }
127 
128  readHeader(is, STARCDCore::HEADER_CEL);
129 
130  DynamicList<label> dynElemId; // STARCD element id (1-based)
131  DynamicList<Face> dynFaces;
132 
133  DynamicList<label> dynZones;
134  DynamicList<word> dynNames;
135  DynamicList<label> dynSizes;
137 
138  // Assume the cellTableIds are not intermixed
139  bool sorted = true;
140  label zoneId = 0;
141 
142  // Element id gets trashed with decompose into a triangle!
143  bool ignoreElemId = false;
144 
145  label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
146  DynamicList<label> vertexLabels(64);
147 
148  token tok;
149 
150  while (is.read(tok).good() && tok.isLabel())
151  {
152  // First token is the element id (1-based)
153  label elemId = tok.labelToken();
154 
155  is >> shapeId
156  >> nLabels
157  >> cellTableId
158  >> typeId;
159 
160  vertexLabels.clear();
161  vertexLabels.reserve(nLabels);
162 
163  // Read indices - max 8 per line
164  for (label i = 0; i < nLabels; ++i)
165  {
166  label vrtId;
167  if ((i % 8) == 0)
168  {
169  is >> ignoredLabel; // Skip cellId for continuation lines
170  }
171  is >> vrtId;
172 
173  // Convert original vertex id to point label
174  vertexLabels.append(mapPointId[vrtId]);
175  }
176 
177  if (typeId == starcdShellType)
178  {
179  // Convert cellTableId to zoneId
180  const auto iterGroup = lookup.cfind(cellTableId);
181  if (iterGroup.found())
182  {
183  if (zoneId != *iterGroup)
184  {
185  // cellTableIds are intermixed
186  sorted = false;
187  }
188  zoneId = *iterGroup;
189  }
190  else
191  {
192  zoneId = dynSizes.size();
193  lookup.insert(cellTableId, zoneId);
194 
195  const auto iterTableName = cellTableLookup.cfind(cellTableId);
196 
197  if (iterTableName.found())
198  {
199  dynNames.append(*iterTableName);
200  }
201  else
202  {
203  dynNames.append("cellTable_" + ::Foam::name(cellTableId));
204  }
205 
206  dynSizes.append(0);
207  }
208 
209  SubList<label> vertices(vertexLabels, vertexLabels.size());
210  if (faceTraits<Face>::isTri() && nLabels > 3)
211  {
212  // The face needs triangulation
213  ignoreElemId = true;
214  dynElemId.clear();
215 
216  face f(vertices);
217 
218  faceList trias(f.nTriangles());
219  label nTri = 0;
220  f.triangles(this->points(), nTri, trias);
221 
222  for (const face& tri : trias)
223  {
224  // A triangular 'face', convert to 'triFace' etc
225  dynFaces.append(Face(tri));
226  dynZones.append(zoneId);
227  dynSizes[zoneId]++;
228  }
229  }
230  else if (nLabels >= 3)
231  {
232  --elemId; // Convert 1-based -> 0-based
233  dynElemId.append(elemId);
234 
235  dynFaces.append(Face(vertices));
236  dynZones.append(zoneId);
237  dynSizes[zoneId]++;
238  }
239  }
240  }
241  mapPointId.clear();
242 
243 
244  if (ignoreElemId)
245  {
246  dynElemId.clear();
247  }
248 
249 
250  this->sortFacesAndStore(dynFaces, dynZones, dynElemId, sorted);
251 
252  // Add zones (retaining empty ones)
253  this->addZones(dynSizes, dynNames);
254  this->addZonesToFaces(); // for labelledTri
255 
256  return true;
257 }
258 
259 
260 template<class Face>
262 (
263  const fileName& filename,
264  const MeshedSurfaceProxy<Face>& surf,
265  IOstreamOption streamOpt,
266  const dictionary&
267 )
268 {
269  // ASCII only, allow output compression
270  streamOpt.format(IOstream::ASCII);
271 
272  const UList<point>& pointLst = surf.points();
273  const UList<Face>& faceLst = surf.surfFaces();
274  const UList<label>& faceMap = surf.faceMap();
275  const UList<label>& elemIds = surf.faceIds();
276 
277  const surfZoneList zones =
278  (
279  surf.surfZones().empty()
280  ? surfaceFormatsCore::oneZone(faceLst)
281  : surf.surfZones()
282  );
283 
284  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
285 
286  // Possible to use faceIds?
287  // - cannot if there are negative ids (eg, encoded solid/side)
288  const bool useOrigFaceIds =
289  (
290  !useFaceMap
291  && elemIds.size() == faceLst.size()
292  && !ListOps::found(elemIds, lessOp1<label>(0))
293  );
294 
295 
296  fileName baseName = filename.lessExt();
297 
298  // The .vrt file
299  {
300  OFstream os(starFileName(baseName, STARCDCore::VRT_FILE), streamOpt);
301  writePoints(os, pointLst);
302  }
303 
304  // The .cel file
305  OFstream os(starFileName(baseName, STARCDCore::CEL_FILE), streamOpt);
306  writeHeader(os, STARCDCore::HEADER_CEL);
307 
308  label faceIndex = 0;
309  label zoneIndex = 0;
310  label elemId = 0;
311  for (const surfZone& zone : zones)
312  {
313  for (label nLocal = zone.size(); nLocal--; ++faceIndex)
314  {
315  const label facei =
316  (useFaceMap ? faceMap[faceIndex] : faceIndex);
317 
318  const Face& f = faceLst[facei];
319 
320  if (useOrigFaceIds)
321  {
322  elemId = elemIds[facei];
323  }
324 
325  writeShell(os, f, elemId, zoneIndex);
326  ++elemId;
327  }
328 
329  ++zoneIndex;
330  }
331 
332  // Simple .inp file - always UNCOMPRESSED
333  {
334  OFstream os(starFileName(baseName, STARCDCore::INP_FILE));
335 
336  writeCase
337  (
338  os,
339  pointLst,
340  faceLst.size(),
341  zones
342  );
343  }
344 }
345 
346 
347 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeTopological.C:94
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Can/should use faceMap?
Definition: MeshedSurfaceProxy.H:203
Foam::fileFormats::STARCDsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &=dictionary::null)
Write surface mesh components by proxy.
Definition: STARCDsurfaceFormat.C:262
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::DynamicList< label >
Foam::IFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:113
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:54
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:286
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:171
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
Foam::DynamicList::reserve
void reserve(const label len)
Definition: DynamicListI.H:333
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:82
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:191
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
STARCDsurfaceFormat.H
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::vertices
pointField vertices(const blockVertexList &bvl)
Definition: blockVertexList.H:49
os
OBJstream os(runTime.globalPath()/outputName)
Foam::fileFormats::STARCDsurfaceFormat
Read/write the surface shells from PROSTAR vrt/cel files.
Definition: STARCDsurfaceFormat.H:69
cellId
label cellId
Definition: interrogateWallPatches.H:67
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::MeshedSurfaceProxy::faceIds
const labelUList & faceIds() const
Const access to the faceIds, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:197
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::fileFormats::STARCDsurfaceFormat::read
virtual bool read(const fileName &filename)
Read from file.
Definition: STARCDsurfaceFormat.C:82
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< label >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:56
Foam::UList
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:103
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
Foam::lessOp1
Definition: ops.H:245
Foam::ISstream::read
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:530
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
ListOps.H
Various functions to operate on Lists.
Foam::MeshedSurfaceProxy::surfFaces
const UList< Face > & surfFaces() const
Return const access to the faces.
Definition: MeshedSurfaceProxy.H:177
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::fileFormats::STARCDsurfaceFormat::STARCDsurfaceFormat
STARCDsurfaceFormat(const fileName &filename)
Construct from file name.
Definition: STARCDsurfaceFormat.C:70
faceTraits.H
Foam::MeshedSurfaceProxy::surfZones
const UList< surfZone > & surfZones() const
Const access to the surface zones.
Definition: MeshedSurfaceProxy.H:185