STARCDCore.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-2015 OpenFOAM Foundation
9 Copyright (C) 2016-2018 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 "STARCDCore.H"
30#include "DynamicList.H"
31#include "OSspecific.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35const Foam::Enum
36<
38>
39Foam::fileFormats::STARCDCore::fileHeaders_
40({
41 { fileHeader::HEADER_CEL, "PROSTAR_CELL" },
42 { fileHeader::HEADER_VRT, "PROSTAR_VERTEX" },
43 { fileHeader::HEADER_BND, "PROSTAR_BOUNDARY" },
44});
45
46
47const Foam::Enum
48<
50>
51Foam::fileFormats::STARCDCore::fileExtensions_
52({
53 { fileExt::CEL_FILE, "cel" },
54 { fileExt::VRT_FILE, "vrt" },
55 { fileExt::BND_FILE, "bnd" },
56 { fileExt::INP_FILE, "inp" },
57});
58
59
61 "Default_Boundary_Region";
62
64 "Default_Boundary_Solid";
65
66
69{
70 { starcdHex, { 4, 5, 2, 3, 0, 1 } },
71 { starcdPrism, { 0, 1, 4, 5, 2, -1 } },
72 { starcdTet, { 5, 4, 2, 0, -1, -1 } },
73 { starcdPyr, { 0, 4, 3, 5, 2, -1 } }
74};
75
76
79{
80 { starcdHex, { 4, 5, 2, 3, 0, 1 } },
81 { starcdPrism, { 0, 1, 4, -1, 2, 3 } },
82 { starcdTet, { 3, -1, 2, -1, 1, 0 } },
83 { starcdPyr, { 0, -1, 4, 2, 1, 3 } }
84};
85
86// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
87
88namespace Foam
89{
90
91 // Read and discard to newline
92 static inline void readToNewline(ISstream& is)
93 {
94 char ch = '\n';
95 do
96 {
97 is.get(ch);
98 }
99 while ((is) && ch != '\n');
100 }
101
102} // End namespace Foam
103
104
105// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
106
107// Read two-line header
108//
109/*---------------------------------------------------------------------------*\
110Line 1:
111 PROSTAR_(BOUNDARY|CELL|VERTEX) [newline]
112
113Line 2:
114 <version> 0 0 0 0 0 0 0 [newline]
115
116Body:
117 ...
118
119\*---------------------------------------------------------------------------*/
120
122(
123 IFstream& is,
124 const enum fileHeader header
125)
126{
127 if (!is.good())
128 {
130 << abort(FatalError);
131 }
132
133 word magic;
134 is >> magic;
135 readToNewline(is);
136
137 label majorVersion;
138 is >> majorVersion;
139 readToNewline(is);
140
141 // Add other checks ...
142 if (magic != fileHeaders_[header])
143 {
144 Info<< "Header mismatch " << fileHeaders_[header]
145 << " " << is.name()
146 << nl;
147
148 return false;
149 }
150
151 return true;
152}
153
154
156(
157 Ostream& os,
158 const enum fileHeader header
159)
160{
161 os << fileHeaders_[header] << nl
162 << 4000
163 << ' ' << 0
164 << ' ' << 0
165 << ' ' << 0
166 << ' ' << 0
167 << ' ' << 0
168 << ' ' << 0
169 << ' ' << 0
170 << nl;
171}
172
173
174// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
175
177(
178 const fileName& base,
179 const enum fileExt ext
180)
181{
182 return base + '.' + fileExtensions_[ext];
183}
184
185
187{
188 Foam::rm(starFileName(base, VRT_FILE));
189 Foam::rm(starFileName(base, CEL_FILE));
190 Foam::rm(starFileName(base, BND_FILE));
191 Foam::rm(starFileName(base, INP_FILE));
192}
193
194
196(
197 IFstream& is,
199 List<label>& ids
200)
201{
202 label maxId = 0;
203 token tok;
204
205 if (!is.good())
206 {
208 << "Cannot read file " << is.name()
209 << exit(FatalError);
210 }
211
212 readHeader(is, HEADER_VRT);
213
214 // Reuse memory if possible
215 DynamicList<point> dynPoints(std::move(points));
216 DynamicList<label> dynPointId(std::move(ids)); // STARCD index of points
217
218 dynPoints.clear();
219 dynPointId.clear();
220
221 {
222 scalar x, y, z;
223
224 while (is.read(tok).good() && tok.isLabel())
225 {
226 const label starVertexId = tok.labelToken();
227
228 is >> x >> y >> z;
229
230 maxId = max(maxId, starVertexId);
231
232 dynPoints.append(point(x, y, z));
233 dynPointId.append(starVertexId);
234 }
235 }
236
237 points.transfer(dynPoints);
238 ids.transfer(dynPointId);
239
240 return maxId;
241}
242
243
245(
246 Ostream& os,
247 const UList<point>& points,
248 const scalar scaleFactor
249)
250{
251 writeHeader(os, HEADER_VRT);
252
253 // Set the precision of the points data to 10
254 os.precision(10);
255
256 // Force decimal point for Fortran input
257 os.setf(std::ios::showpoint);
258
259 label starVertId = 1; // 1-based vertex labels
260
261 for (const point& p : points)
262 {
263 // Convert [m] -> [mm] etc
264 os
265 << starVertId << ' '
266 << scaleFactor * p.x() << ' '
267 << scaleFactor * p.y() << ' '
268 << scaleFactor * p.z() << nl;
269
270 ++starVertId;
271 }
272
273 os.flush();
274}
275
276
277// ************************************************************************* //
scalar y
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
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
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
ISstream & get(char &c)
Raw, low-level get character function.
Definition: ISstreamI.H:56
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 transfer(List< T > &list)
Definition: List.C:447
A HashTable to objects of type <T> with a label key.
Definition: Map.H:60
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
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:94
void removeFiles() const
Remove all files from mesh instance()
Definition: faMesh.C:718
static const Map< FixedList< int, 6 > > starToFoamFaceAddr
Face addressing from PROSTAR faces to OpenFOAM faces.
Definition: STARCDCore.H:129
static void writeHeader(Ostream &os, const enum fileHeader header)
Write header for fileType (CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:156
static const Map< FixedList< int, 6 > > foamToStarFaceAddr
Face addressing from OpenFOAM faces to PROSTAR faces.
Definition: STARCDCore.H:133
static fileName starFileName(const fileName &baseName, const enum fileExt ext)
Resolve base file-name for the given file-type.
Definition: STARCDCore.C:177
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:245
static bool readHeader(IFstream &is, const enum fileHeader header)
Read header and check signature PROSTAR_(CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:122
static const char *const defaultBoundaryName
The name for default (unassigned) boundaries.
Definition: STARCDCore.H:108
fileHeader
Enumeration defining the file headers.
Definition: STARCDCore.H:67
fileExt
Enumeration defining the file extensions.
Definition: STARCDCore.H:75
static const char *const defaultSolidBoundaryName
The name we have chosen for default (unassigned) solid boundaries.
Definition: STARCDCore.H:112
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:196
A class for handling file names.
Definition: fileName.H:76
A token holds an item read from Istream.
Definition: token.H:69
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
label labelToken() const
Return label value.
Definition: tokenI.H:513
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
const pointField & points
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1012
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
static void readToNewline(ISstream &is)
static void writeHeader(Ostream &os, const word &fieldName)
messageStream Info
Information stream (stdout output on master, null elsewhere)
vector point
Point is a vector.
Definition: point.H:43
errorManip< error > abort(error &err)
Definition: errorManip.H:144
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