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-2018 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 // includes 1 offset
45  << ' ' << starcdShell // 3(shell) shape
46  << ' ' << f.size()
47  << ' ' << cellTableId
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;
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  this->clear();
87 
88  fileName baseName = filename.lessExt();
89 
90  // Read cellTable names (if possible)
91  Map<word> cellTableLookup = readInpCellTable
92  (
93  IFstream(starFileName(baseName, STARCDCore::INP_FILE))()
94  );
95 
96 
97  // STARCD index of points
98  List<label> pointId;
99 
100  // read points from .vrt file
101  readPoints
102  (
103  IFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
104  this->storedPoints(),
105  pointId
106  );
107 
108  // Build inverse mapping (STARCD pointId -> index)
109  Map<label> mapPointId(2*pointId.size());
110  forAll(pointId, i)
111  {
112  mapPointId.insert(pointId[i], i);
113  }
114  pointId.clear();
115 
116 
117  // Read .cel file
118  // ~~~~~~~~~~~~~~
119  IFstream is(starFileName(baseName, STARCDCore::CEL_FILE));
120  if (!is.good())
121  {
123  << "Cannot read file " << is.name()
124  << exit(FatalError);
125  }
126 
127  readHeader(is, STARCDCore::HEADER_CEL);
128 
129  DynamicList<Face> dynFaces;
130  DynamicList<label> dynZones;
131  DynamicList<word> dynNames;
132  DynamicList<label> dynSizes;
134 
135  // assume the cellTableIds are not intermixed
136  bool sorted = true;
137  label zoneId = 0;
138 
139  label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
140  DynamicList<label> vertexLabels(64);
141 
142  token tok;
143 
144  while (is.read(tok).good() && tok.isLabel())
145  {
146  // const label starCellId = tok.labelToken();
147  is >> shapeId
148  >> nLabels
149  >> cellTableId
150  >> 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 >> ignoredLabel; // Skip cellId for continuation lines
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 cellTableId to zoneId
172  const auto iterGroup = lookup.cfind(cellTableId);
173  if (iterGroup.found())
174  {
175  if (zoneId != *iterGroup)
176  {
177  // cellTableIds are intermixed
178  sorted = false;
179  }
180  zoneId = *iterGroup;
181  }
182  else
183  {
184  zoneId = dynSizes.size();
185  lookup.insert(cellTableId, zoneId);
186 
187  const auto iterTableName = cellTableLookup.cfind(cellTableId);
188 
189  if (iterTableName.found())
190  {
191  dynNames.append(*iterTableName);
192  }
193  else
194  {
195  dynNames.append("cellTable_" + ::Foam::name(cellTableId));
196  }
197 
198  dynSizes.append(0);
199  }
200 
201  SubList<label> vertices(vertexLabels, vertexLabels.size());
202  if (faceTraits<Face>::isTri() && nLabels > 3)
203  {
204  // face needs triangulation
205  face f(vertices);
206 
207  faceList trias(f.nTriangles());
208  label nTri = 0;
209  f.triangles(this->points(), nTri, trias);
210 
211  for (const face& tri : trias)
212  {
213  // A triangular 'face', convert to 'triFace' etc
214  dynFaces.append(Face(tri));
215  dynZones.append(zoneId);
216  dynSizes[zoneId]++;
217  }
218  }
219  else if (nLabels >= 3)
220  {
221  dynFaces.append(Face(vertices));
222  dynZones.append(zoneId);
223  dynSizes[zoneId]++;
224  }
225  }
226  }
227  mapPointId.clear();
228 
229  this->sortFacesAndStore(dynFaces, dynZones, sorted);
230 
231  // Add zones (retaining empty ones)
232  this->addZones(dynSizes, dynNames);
233  this->addZonesToFaces(); // for labelledTri
234 
235  return true;
236 }
237 
238 
239 template<class Face>
241 (
242  const fileName& filename,
243  const MeshedSurfaceProxy<Face>& surf,
244  const dictionary&
245 )
246 {
247  const UList<point>& pointLst = surf.points();
248  const UList<Face>& faceLst = surf.surfFaces();
249  const UList<label>& faceMap = surf.faceMap();
250 
251  const surfZoneList zones =
252  (
253  surf.surfZones().empty()
254  ? surfaceFormatsCore::oneZone(faceLst)
255  : surf.surfZones()
256  );
257 
258  const bool useFaceMap = (surf.useFaceMap() && zones.size() > 1);
259 
260  fileName baseName = filename.lessExt();
261 
262  writePoints
263  (
264  OFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
265  pointLst
266  );
267  OFstream os(starFileName(baseName, STARCDCore::CEL_FILE));
268  writeHeader(os, STARCDCore::HEADER_CEL);
269 
270  label faceIndex = 0;
271  forAll(zones, zonei)
272  {
273  const surfZone& zone = zones[zonei];
274  const label nLocalFaces = zone.size();
275 
276  if (useFaceMap)
277  {
278  for (label i=0; i<nLocalFaces; ++i)
279  {
280  const Face& f = faceLst[faceMap[faceIndex++]];
281  writeShell(os, f, faceIndex, zonei + 1);
282  }
283  }
284  else
285  {
286  for (label i=0; i<nLocalFaces; ++i)
287  {
288  const Face& f = faceLst[faceIndex++];
289  writeShell(os, f, faceIndex, zonei + 1);
290  }
291  }
292  }
293 
294  // Write simple .inp file
295  writeCase
296  (
297  OFstream(starFileName(baseName, STARCDCore::INP_FILE))(),
298  pointLst,
299  faceLst.size(),
300  zones
301  );
302 }
303 
304 
305 // ************************************************************************* //
Foam::faceMap
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Definition: blockMeshMergeFast.C:94
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::MeshedSurfaceProxy::useFaceMap
bool useFaceMap() const
Use faceMap?
Definition: MeshedSurfaceProxy.H:186
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
Foam::IFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:111
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::fileFormats::STARCDsurfaceFormat::write
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, const dictionary &options=dictionary::null)
Write surface mesh components by proxy.
Definition: STARCDsurfaceFormat.C:241
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: HashTableFwd.H:46
Foam::MeshedSurfaceProxy::points
const pointField & points() const
Return const access to the points.
Definition: MeshedSurfaceProxy.H:160
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:240
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::MeshedSurfaceProxy
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
Definition: MeshedSurface.H:78
Foam::DynamicList::reserve
void reserve(const label nElem)
Reserve allocation space for at least this size.
Definition: DynamicListI.H:254
Foam::MeshedSurfaceProxy::faceMap
const labelUList & faceMap() const
Const access to the faceMap, zero-sized when unused.
Definition: MeshedSurfaceProxy.H:180
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::token::isLabel
bool isLabel() const
Token is LABEL.
Definition: tokenI.H:450
Foam::faceTraits
Traits class for faces.
Definition: faceTraits.H:50
Foam::label
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:62
STARCDsurfaceFormat.H
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::cellModeller::lookup
const cellModel * lookup(const word &modelName)
Deprecated(2017-11) equivalent to cellModel::ptr static method.
Definition: cellModeller.H:46
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:472
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::radiation::lookup
Lookup type of boundary radiation properties.
Definition: lookup.H:63
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
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:121
Foam::vertices
pointField vertices(const blockVertexList &bvl)
Definition: blockVertexList.H:49
Foam::fileFormats::STARCDsurfaceFormat
Read/write the surface shells from PROSTAR vrt/cel files.
Definition: STARCDsurfaceFormat.H:63
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:99
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
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:372
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:74
Foam::List< label >
Foam::surfZone
A surface zone on a MeshedSurface.
Definition: surfZone.H:65
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:115
Foam::ISstream::read
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:158
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:74
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:166
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
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:174