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 -------------------------------------------------------------------------------
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 "STARCDCore.H"
30 #include "DynamicList.H"
31 #include "OSspecific.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const Foam::Enum
36 <
38 >
39 Foam::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 
47 const Foam::Enum
48 <
50 >
51 Foam::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 
88 namespace 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 /*---------------------------------------------------------------------------*\
110 Line 1:
111  PROSTAR_(BOUNDARY|CELL|VERTEX) [newline]
112 
113 Line 2:
114  <version> 0 0 0 0 0 0 0 [newline]
116 Body:
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 // ************************************************************************* //
static bool readHeader(IFstream &is, const enum fileHeader header)
Read header and check signature PROSTAR_(CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:115
A class for handling file names.
Definition: fileName.H:72
static void writeHeader(Ostream &os, const word &fieldName)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
static const Map< FixedList< int, 6 > > starToFoamFaceAddr
Face addressing from PROSTAR faces to OpenFOAM faces.
Definition: STARCDCore.H:141
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
static void readToNewline(ISstream &is)
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
A token holds an item read from Istream.
Definition: token.H:65
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
fileExt
Enumeration defining the file extensions.
Definition: STARCDCore.H:74
static const char *const defaultBoundaryName
The name for default (unassigned) boundaries.
Definition: STARCDCore.H:114
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
scalar y
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition: ISstream.H:141
void fileHeader(std::ostream &os, const std::string &title, bool binary)
Emit header for legacy file (vtk DataFile Version 2.0)
const pointField & points
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const char *const defaultSolidBoundaryName
The name we have chosen for default (unassigned) solid boundaries.
Definition: STARCDCore.H:121
static const Map< FixedList< int, 6 > > foamToStarFaceAddr
Face addressing from OpenFOAM faces to PROSTAR faces.
Definition: STARCDCore.H:148
static void removeFiles(const fileName &baseName)
Remove existing PROSTAR files for the given base file-name.
Definition: STARCDCore.C:179
errorManip< error > abort(error &err)
Definition: errorManip.H:139
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:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
OBJstream os(runTime.globalPath()/outputName)
Input from file stream, using an ISstream.
Definition: IFstream.H:49
virtual Istream & read(token &t) override
Return next token from stream.
Definition: ISstream.C:535
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 writeHeader(Ostream &os, const enum fileHeader header)
Write header for fileType (CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:149
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
vector point
Point is a vector.
Definition: point.H:37
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
label labelToken() const
Return label value.
Definition: tokenI.H:615
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:599
messageStream Info
Information stream (stdout output on master, null elsewhere)
static fileName starFileName(const fileName &baseName, const enum fileExt ext)
Resolve base file-name for the given file-type.
Definition: STARCDCore.C:170
volScalarField & p
ISstream & get(char &c)
Raw, low-level get character function.
Definition: ISstreamI.H:49
fileHeader
Enumeration defining the file headers.
Definition: STARCDCore.H:64
Namespace for OpenFOAM.
A HashTable to objects of type <T> with a label key.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: POSIX.C:1404