NASCore.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) 2017-2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "NASCore.H"
29 #include "IOmanip.H"
30 #include "Ostream.H"
31 #include "parsing.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const Foam::Enum
36 <
38 >
40 ({
41  { fieldFormat::SHORT, "short" },
42  { fieldFormat::LONG, "long" },
43  { fieldFormat::FREE, "free" },
44 });
45 
46 
47 const Foam::Enum
48 <
50 >
52 ({
53  { loadFormat::PLOAD2, "PLOAD2" },
54  { loadFormat::PLOAD4, "PLOAD4" },
55 });
56 
57 
58 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
59 
60 Foam::scalar Foam::fileFormats::NASCore::readNasScalar(const std::string& str)
61 {
62  const auto signPos = str.find_last_of("+-");
63 
64  if
65  (
66  signPos == std::string::npos
67  || signPos == 0
68  || str[signPos-1] == 'E' || str[signPos-1] == 'e'
69  || isspace(str[signPos-1])
70  )
71  {
72  // A normal number format
73  return readScalar(str);
74  }
75 
76 
77  // Nastran compact number format.
78  // Eg, "1234-2" instead of "1234E-2"
79 
80  scalar value = 0;
81  int exponent = 0; // Any integer
82 
83  if
84  (
85  readScalar(str.substr(0, signPos), value) // Mantissa
86  && readInt(str.substr(signPos), exponent) // Exponent (with sign)
87  )
88  {
89  // Note: this does not catch underflow/overflow
90  // (especially when scalar is a float)
91  value *= ::pow(10, exponent);
92  }
93  else
94  {
95  FatalIOErrorInFunction("unknown")
97  << exit(FatalIOError);
98 
99  value = 0;
100  }
101 
102  return value;
103 }
104 
105 
107 (
108  const std::string& str,
111 )
112 {
113  const auto beg = pos;
114  const auto end = str.find(',', pos);
115 
116  if (end == std::string::npos)
117  {
118  pos = beg + len; // Continue after field width
119  }
120  else
121  {
122  len = (end - beg); // Efffective width
123  pos = end + 1; // Continue after comma
124  }
125 
126  return str.substr(beg, len);
127 }
128 
129 
131 (
132  Ostream& os,
133  const fieldFormat format
134 )
135 {
136  os.setf(ios_base::scientific);
137 
138  // Capitalise the E marker
139  os.setf(ios_base::uppercase);
140 
141  const label offset = 7;
142 
143  label prec = 16 - offset;
144  switch (format)
145  {
146  case fieldFormat::SHORT :
147  {
148  prec = 8 - offset;
149  break;
150  }
151 
152  case fieldFormat::LONG :
153  case fieldFormat::FREE :
154  {
155  prec = 16 - offset;
156  break;
157  }
158  }
159 
160  os.precision(prec);
161 }
162 
163 
165 (
166  Ostream& os,
167  const word& keyword,
168  const fieldFormat format
169 )
170 {
171  os.setf(ios_base::left);
172 
173  switch (format)
174  {
175  case fieldFormat::SHORT :
176  {
177  os << setw(8) << keyword;
178  break;
179  }
180 
181  case fieldFormat::LONG :
182  {
183  os << setw(8) << word(keyword + '*');
184  break;
185  }
186 
187  case fieldFormat::FREE :
188  {
189  os << keyword;
190  break;
191  }
192  }
193 
194  os.unsetf(ios_base::left);
195 
196  return os;
197 }
198 
199 
201 (
202  const UList<point>& points,
203  const UList<face>& faces,
204  labelList& decompOffsets,
205  DynamicList<face>& decompFaces
206 )
207 {
208  // On-demand face decomposition (triangulation)
209 
210  decompOffsets.resize(faces.size()+1);
211  decompFaces.clear();
212 
213  auto offsetIter = decompOffsets.begin();
214  *offsetIter = 0; // The first offset is always zero
215 
216  for (const face& f : faces)
217  {
218  const label n = f.size();
219 
220  if (n != 3 && n != 4)
221  {
222  // Decompose non-tri/quad into tris
223  f.triangles(points, decompFaces);
224  }
225 
226  // The end offset, which is the next begin offset
227  *(++offsetIter) = decompFaces.size();
228  }
229 
230  return decompFaces.size();
231 }
232 
233 
234 // ************************************************************************* //
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::isspace
bool isspace(char c) noexcept
Test for whitespace (C-locale)
Definition: char.H:65
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
parsing.H
Foam::fileFormats::NASCore::loadFormatNames
static const Enum< loadFormat > loadFormatNames
Selection names for the NASTRAN file field formats.
Definition: NASCore.H:81
Foam::FatalIOError
IOerror FatalIOError
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::fileFormats::NASCore::setPrecision
static void setPrecision(Ostream &os, const fieldFormat format)
Set output stream precision and format flags.
Definition: NASCore.C:131
Foam::fileFormats::NASCore::faceDecomposition
static label faceDecomposition(const UList< point > &points, const UList< face > &faces, labelList &decompOffsets, DynamicList< face > &decompFaces)
Calculate face decomposition for non tri/quad faces.
Definition: NASCore.C:201
n
label n
Definition: TABSMDCalcMethod2.H:31
format
word format(conversionProperties.get< word >("format"))
Foam::fileFormats::NASCore::writeKeyword
static Ostream & writeKeyword(Ostream &os, const word &keyword, const fieldFormat format)
Definition: NASCore.C:165
Foam::fileFormats::NASCore::nextNasField
static std::string nextNasField(const std::string &str, std::string::size_type &pos, std::string::size_type len)
A string::substr() to handle fixed-format and free-format NASTRAN.
Definition: NASCore.C:107
IOmanip.H
Istream and Ostream manipulators taking arguments.
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:75
Foam::parsing::errorNames
const Foam::Enum< errorType > errorNames
Strings corresponding to the errorType.
Foam::parsing::errorType::GENERAL
General parsing error.
NASCore.H
os
OBJstream os(runTime.globalPath()/outputName)
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:121
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream.H
Foam::fileFormats::NASCore::loadFormat
loadFormat
Output load format.
Definition: NASCore.H:74
f
labelList f(nPoints)
Foam::List< label >
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
Foam::fileFormats::NASCore::readNasScalar
static scalar readNasScalar(const std::string &str)
Extract numbers from things like "-2.358-8" (same as "-2.358e-8")
Definition: NASCore.C:60
Foam::face
A face is a list of labels corresponding to mesh vertices.
Definition: face.H:72
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::readInt
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::fileFormats::NASCore::fieldFormatNames
static const Enum< fieldFormat > fieldFormatNames
Selection names for the NASTRAN file field formats.
Definition: NASCore.H:71
Foam::fileFormats::NASCore::fieldFormat
fieldFormat
File field formats.
Definition: NASCore.H:63
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177