Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
35template<class Face>
36inline void Foam::fileFormats::STARCDsurfaceFormat<Face>::writeShell
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
68template<class Face>
70(
71 const fileName& filename
72)
73{
74 read(filename);
75}
76
77
78// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79
80template<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
251
252 return true;
253}
254
255
256template<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()
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// ************************************************************************* //
Various functions to operate on Lists.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
void append(const T &val)
Copy append an element to the end of this list.
void reserve(const label len)
Reserve allocation space for at least this size, allocating new space if required and retaining old c...
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition HashTableI.H:113
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition HashTableI.H:152
void clear()
Remove all entries from table.
Definition HashTable.C:742
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition ISstream.H:147
A simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
@ ASCII
"ascii" (normal default)
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
virtual Istream & read(token &t) override
Return next token from stream.
Definition ISstream.C:535
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void clear()
Clear the list, i.e. set size to zero.
Definition ListI.H:133
A HashTable to objects of type <T> with a label key.
Definition Map.H:54
A proxy for writing MeshedSurface, UnsortedMeshedSurface and surfMesh to various file formats.
const UList< surfZone > & surfZones() const noexcept
Const access to the surface zones.
const UList< Face > & surfFaces() const noexcept
Return const access to the faces.
const labelUList & faceIds() const noexcept
Const access to the faceIds, zero-sized when unused.
const labelUList & faceMap() const noexcept
Const access to the faceMap, zero-sized when unused.
const pointField & points() const noexcept
Return const access to the points.
bool useFaceMap() const noexcept
Can/should use faceMap?
pointField & storedPoints()
Non-const access to global points.
virtual void addZones(const UList< surfZone > &, const bool cullEmpty=false)
Add surface zones.
void sortFacesAndStore(DynamicList< Face > &unsortedFaces, DynamicList< label > &zoneIds, DynamicList< label > &elemIds, bool sorted)
Sort faces by zones and store sorted faces.
bool addZonesToFaces()
Propagate zone information on face regions.
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
static bool isTri()
Face-type only handles triangles. Not true in general.
Definition faceTraits.H:51
A face is a list of labels corresponding to mesh vertices.
Definition face.H:71
static void writeHeader(Ostream &os, const enum fileHeader header)
Write header for fileType (CELL|VERTEX|BOUNDARY).
Definition STARCDCore.C:149
static fileName starFileName(const fileName &baseName, const enum fileExt ext)
Resolve base file-name for the given file-type.
Definition STARCDCore.C:170
static void writePoints(Ostream &os, const UList< point > &points, const scalar scaleFactor=1.0)
Write header and points to (.vrt) file, optionally with scaling.
Definition STARCDCore.C:238
static bool readHeader(IFstream &is, const enum fileHeader header)
Read header and check signature PROSTAR_(CELL|VERTEX|BOUNDARY).
Definition STARCDCore.C:115
static label readPoints(IFstream &is, List< point > &points, List< label > &ids)
Read points from a (.vrt) file, return the max prostar id used.
Definition STARCDCore.C:189
static void writeCase(Ostream &os, const UList< point > &pts, const label nFaces, const UList< surfZone > &zoneLst)
static Map< word > readInpCellTable(ISstream &is)
static void write(const fileName &filename, const MeshedSurfaceProxy< Face > &surf, IOstreamOption streamOpt=IOstreamOption(), const dictionary &=dictionary::null)
Write surface mesh components by proxy.
virtual bool read(const fileName &filename) override
Read from file.
STARCDsurfaceFormat(const fileName &filename)
Construct from file name.
static List< surfZone > oneZone(const Container &container, const word &name="zone0")
Return a surfZone list with a single entry, the size of which corresponds to that of the container.
A class for handling file names.
Definition fileName.H:75
fileName lessExt() const
Return file name without extension (part before last .).
Definition fileNameI.H:238
Lookup type of boundary radiation properties.
Definition lookup.H:60
A surface zone on a MeshedSurface.
Definition surfZone.H:55
A token holds an item read from Istream.
Definition token.H:70
bool isLabel() const noexcept
Integral token is convertible to Foam::label.
Definition tokenI.H:843
label labelToken() const
Return integer type as label value or Error.
Definition tokenI.H:869
Base class for mesh zones.
Definition zone.H:63
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
label cellId
surface1 clear()
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as found_if.
Definition ListOps.H:886
Pair< int > faceMap(const label facePi, const face &faceP, const label faceNi, const face &faceN)
Map< label > invertToMap(const labelUList &values)
Create inverse mapping, which is a lookup table into the given list.
Definition ListOps.C:105
List< surfZone > surfZoneList
List of surfZone.
static void writeHeader(Ostream &os, const word &fieldName)
List< face > faceList
List of faces.
Definition faceListFwd.H:41
pointField vertices(const blockVertexList &bvl)
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
labelList f(nPoints)