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-2019 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 {
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 
200 // ************************************************************************* //
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:51
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
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::fileFormats::NASCore::setPrecision
static void setPrecision(Ostream &os, const fieldFormat format)
Set output stream precision and format flags.
Definition: NASCore.C:131
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
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::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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:75
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
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:115
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:447
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:361
Ostream.H
Foam::fileFormats::NASCore::loadFormat
loadFormat
Output load format.
Definition: NASCore.H:74
Foam::IOstream::unsetf
void unsetf(const ios_base::fmtflags f)
Unset flags of stream.
Definition: IOstream.H:377
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
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::readInt
int readInt(Istream &is)
Read int from stream.
Definition: intIO.C:80
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::isspace
bool isspace(char c)
Test for horizontal whitespace.
Definition: char.H:63
Foam::fileFormats::NASCore::fieldFormat
fieldFormat
File field formats.
Definition: NASCore.H:63
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177