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]
115 
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 {
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 // ************************************************************************* //
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::fileFormats::STARCDCore::INP_FILE
Definition: STARCDCore.H:79
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fileFormats::STARCDCore::starFileName
static fileName starFileName(const fileName &baseName, const enum fileExt ext)
Resolve base file-name for the given file-type.
Definition: STARCDCore.C:177
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:53
Foam::DynamicList< point >
Foam::IFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:113
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
Foam::fileFormats::STARCDCore::defaultSolidBoundaryName
static const char *const defaultSolidBoundaryName
The name we have chosen for default (unassigned) solid boundaries.
Definition: STARCDCore.H:112
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1004
Foam::fileFormats::STARCDCore::foamToStarFaceAddr
static const Map< FixedList< int, 6 > > foamToStarFaceAddr
Face addressing from OpenFOAM faces to PROSTAR faces.
Definition: STARCDCore.H:133
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:66
Foam::fileFormats::STARCDCore::fileHeader
fileHeader
Enumeration defining the file headers.
Definition: STARCDCore.H:66
STARCDCore.H
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::fileFormats::STARCDCore::VRT_FILE
Definition: STARCDCore.H:77
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::fileFormats::STARCDCore::readHeader
static bool readHeader(IFstream &is, const enum fileHeader header)
Read header and check signature PROSTAR_(CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:122
Foam::ISstream::get
ISstream & get(char &c)
Raw, low-level get character function.
Definition: ISstreamI.H:56
Foam::fileFormats::STARCDCore::readPoints
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
Foam::fileFormats::STARCDCore::fileExt
fileExt
Enumeration defining the file extensions.
Definition: STARCDCore.H:74
Foam::fileFormats::STARCDCore::defaultBoundaryName
static const char *const defaultBoundaryName
The name for default (unassigned) boundaries.
Definition: STARCDCore.H:108
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
Foam::List::transfer
void transfer(List< T > &list)
Definition: List.C:456
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::fileFormats::STARCDCore::CEL_FILE
Definition: STARCDCore.H:76
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::fileFormats::STARCDCore::writeHeader
static void writeHeader(Ostream &os, const enum fileHeader header)
Write header for fileType (CELL|VERTEX|BOUNDARY)
Definition: STARCDCore.C:156
Foam::fileFormats::STARCDCore::BND_FILE
Definition: STARCDCore.H:78
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Vector< scalar >
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::readToNewline
static void readToNewline(ISstream &is)
Definition: STARCDMeshReader.C:46
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
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::ISstream::read
virtual Istream & read(token &t)
Return next token from stream.
Definition: ISstream.C:530
Foam::fileFormats::STARCDCore::starToFoamFaceAddr
static const Map< FixedList< int, 6 > > starToFoamFaceAddr
Face addressing from PROSTAR faces to OpenFOAM faces.
Definition: STARCDCore.H:129
DynamicList.H
Foam::fileFormats::STARCDCore::removeFiles
static void removeFiles(const fileName &baseName)
Remove existing PROSTAR files for the given base file-name.
Definition: STARCDCore.C:186
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::point
vector point
Point is a vector.
Definition: point.H:43
Foam::fileFormats::STARCDCore::writePoints
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
y
scalar y
Definition: LISASMDCalcMethod1.H:14