STARCDedgeFormat.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-2017 OpenFOAM Foundation
9 Copyright (C) 2016 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 "STARCDedgeFormat.H"
30#include "ListOps.H"
31#include "clock.H"
32#include "bitSet.H"
33#include "StringStream.H"
34
35// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36
37inline void Foam::fileFormats::STARCDedgeFormat::writeLines
38(
39 Ostream& os,
40 const edgeList& edges,
41 label starCellId
42)
43{
44 starCellId = max(1, starCellId); // Enforce 1-based cellId
45
46 for (const edge& e : edges)
47 {
48 os << starCellId
49 << ' ' << starcdLine // 2(line) shape
50 << ' ' << e.size()
51 << ' ' << 401 // arbitrary value
52 << ' ' << starcdLineType; // 5(line)
53
54 os << nl
55 << " " << starCellId << " "
56 << (e[0]+1) << " " << (e[1]+1) << nl;
57
58 ++starCellId;
59 }
60}
61
62
63// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
64
66(
67 Ostream& os,
68 const pointField& pointLst,
69 const label nEdges
70)
71{
72 const word caseName = os.name().nameLessExt();
73
74 os << "! STARCD file written " << clock::dateTime().c_str() << nl
75 << "! " << pointLst.size() << " points, " << nEdges << " lines" << nl
76 << "! case " << caseName << nl
77 << "! ------------------------------" << nl;
78
79// forAll(zoneLst, zoneI)
80// {
81// os << "ctable " << zoneI + 1 << " line" << nl
82// << "ctname " << zoneI + 1 << " "
83// << zoneLst[zoneI].name() << nl;
84// }
85
86 os << "! ------------------------------" << nl
87 << "*set icvo mxv - 1" << nl
88 << "vread " << caseName << ".vrt icvo,,,coded" << nl
89 << "cread " << caseName << ".cel icvo,,,add,coded" << nl
90 << "*set icvo" << nl
91 << "! end" << nl;
92
93 os.flush();
94}
95
96
97// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
98
100(
101 const fileName& filename
102)
103{
104 read(filename);
105}
106
107
108// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
109
111(
112 const fileName& filename
113)
114{
115 clear();
116
117 fileName baseName = filename.lessExt();
118
119 // STARCD index of points
120 List<label> pointId;
121
122 // Read points from .vrt file
123 readPoints
124 (
125 IFstream(starFileName(baseName, STARCDCore::VRT_FILE))(),
126 storedPoints(),
127 pointId
128 );
129
130 // Build inverse mapping (STARCD pointId -> index)
131 Map<label> mapPointId(2*pointId.size());
132 forAll(pointId, i)
133 {
134 mapPointId.insert(pointId[i], i);
135 }
136 pointId.clear();
137
138 // Note which points were really used and which can be culled
139 bitSet usedPoints(points().size());
140
141
142 // Read .cel file
143 // ~~~~~~~~~~~~~~
144 IFstream is(starFileName(baseName, STARCDCore::CEL_FILE));
145 if (!is.good())
146 {
148 << "Cannot read file " << is.name()
149 << exit(FatalError);
150 }
151
152 readHeader(is, STARCDCore::HEADER_CEL);
153
154 DynamicList<edge> dynEdges;
155
156 label ignoredLabel, shapeId, nLabels, cellTableId, typeId;
157 DynamicList<label> vertexLabels(64);
158
159 token tok;
160
161 while (is.read(tok).good() && tok.isLabel())
162 {
163 // const label starCellId = tok.labelToken();
164 is >> shapeId
165 >> nLabels
166 >> cellTableId
167 >> typeId;
168
169 vertexLabels.clear();
170 vertexLabels.reserve(nLabels);
171
172 // Read indices - max 8 per line
173 for (label i = 0; i < nLabels; ++i)
174 {
175 label vrtId;
176 if ((i % 8) == 0)
177 {
178 is >> ignoredLabel; // Skip cellId for continuation lines
179 }
180 is >> vrtId;
181
182 // Convert original vertex id to point label
183 vertexLabels.append(mapPointId[vrtId]);
184 }
185
186 if (typeId == starcdLineType)
187 {
188 if (vertexLabels.size() >= 2)
189 {
190 dynEdges.append(edge(vertexLabels[0], vertexLabels[1]));
191
192 usedPoints.set(vertexLabels[0]);
193 usedPoints.set(vertexLabels[1]);
194 }
195 }
196 }
197
198 mapPointId.clear();
199
200 // Not all points were used, subset/cull them accordingly
201 if (!usedPoints.all())
202 {
203 label nUsed = 0;
204
205 pointField& pts = storedPoints();
206 for (const label pointi : usedPoints)
207 {
208 if (nUsed != pointi)
209 {
210 pts[nUsed] = pts[pointi];
211 }
212
213 // Map prev -> new id
214 mapPointId.set(pointi, nUsed);
215
216 ++nUsed;
217 }
218 pts.resize(nUsed);
219
220 // Renumber edge vertices
221 for (edge& e : dynEdges)
222 {
223 e[0] = mapPointId[e[0]];
224 e[1] = mapPointId[e[1]];
225 }
226 }
227
228 storedEdges().transfer(dynEdges);
229
230 return true;
231}
232
233
235(
236 const fileName& filename,
237 const edgeMesh& mesh,
238 IOstreamOption streamOpt,
239 const dictionary&
240)
241{
242 // ASCII only, allow output compression
243 streamOpt.format(IOstream::ASCII);
244
245 const pointField& pointLst = mesh.points();
246 const edgeList& edgeLst = mesh.edges();
247
248 fileName baseName = filename.lessExt();
249
250 // The .vrt file
251 {
252 OFstream os(starFileName(baseName, STARCDCore::VRT_FILE), streamOpt);
253 writePoints(os, pointLst);
254 }
255
256 // The .cel file
257 {
258 OFstream os(starFileName(baseName, STARCDCore::CEL_FILE), streamOpt);
260 writeLines(os, edgeLst);
261 }
262
263 // Write a simple .inp file. Never compressed
264 writeCase
265 (
266 OFstream(starFileName(baseName, STARCDCore::INP_FILE))(),
267 pointLst,
268 edgeLst.size()
269 );
270}
271
272
273// ************************************************************************* //
Various functions to operate on Lists.
Input/output from string buffers.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
void reserve(const label len)
Definition: DynamicListI.H:333
bool set(const Key &key, const T &obj)
Copy assign a new entry, overwriting existing entries.
Definition: HashTableI.H:202
bool insert(const Key &key, const T &obj)
Copy insert a new entry, not overwriting existing entries.
Definition: HashTableI.H:180
void transfer(HashTable< T, Key, Hash > &rhs)
Transfer contents into this table.
Definition: HashTable.C:719
void clear()
Clear all entries from table.
Definition: HashTable.C:678
Input from file stream, using an ISstream.
Definition: IFstream.H:57
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:113
The IOstreamOption is a simple container for options an IOstream can normally have.
streamFormat format() const noexcept
Get the current stream format.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:530
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
A HashTable to objects of type <T> with a label key.
Definition: Map.H:60
Output to file stream, using an OSstream.
Definition: OFstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:590
bool all() const
True if all bits in this bitset are set or if the set is empty.
Definition: bitSetI.H:461
static std::string dateTime()
Definition: clock.C:60
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Mesh data needed to do the Finite Area discretisation.
Definition: edgeFaMesh.H:56
const edgeList & edges() const noexcept
Return edges.
Definition: edgeMeshI.H:105
An edge is a list of two point labels. The functionality it provides supports the discretisation on a...
Definition: edge.H:66
Read/write the lines from PROSTAR vrt/cel files.
static void writeCase(Ostream &os, const pointField &pointLst, const label nEdges)
A class for handling file names.
Definition: fileName.H:76
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
virtual bool write()
Write the output fields.
A token holds an item read from Istream.
Definition: token.H:69
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
A class for handling words, derived from Foam::string.
Definition: word.H:68
patchWriters clear()
dynamicFvMesh & mesh
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
const labelList nEdges(UPstream::listGatherValues< label >(aMesh.nEdges()))
const pointField & points
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
static void writeHeader(Ostream &os, const word &fieldName)
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
volScalarField & e
Definition: createFields.H:11
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333