ensightSurfaceReaderTemplates.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) 2015-2018 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 <iomanip>
29 #include <sstream>
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const label nSkip,
37  IStringStream& is,
38  Type& value
39 ) const
40 {
41  skip(nSkip, is);
42 
43  is >> value;
44 }
45 
46 
47 template<class Type>
49 (
50  const label nSkip,
51  const string& buffer,
52  Type& value
53 ) const
54 {
55  IStringStream is(buffer);
56 
57  readFromLine(nSkip, is, value);
58 }
59 
60 
61 template<class Type>
63 (
64  const label timeIndex,
65  const label fieldIndex
66 ) const
67 {
68  if (debug)
69  {
71  }
72 
73  const word& fieldName(fieldNames_[fieldIndex]);
74  const label fileIndex = timeStartIndex_ + timeIndex*timeIncrement_;
75 
76  fileName fieldFileName(fieldFileNames_[fieldIndex]);
77 
78  std::ostringstream oss;
79  label nMask = 0;
80  for (size_t chari = 0; chari < fieldFileName.size(); ++chari)
81  {
82  if (fieldFileName[chari] == '*')
83  {
84  nMask++;
85  }
86  }
87 
88  const std::string maskStr(nMask, '*');
89  oss << std::setfill('0') << std::setw(nMask) << fileIndex;
90  const word indexStr = oss.str();
91  fieldFileName.replace(maskStr, indexStr);
92 
93 
94  ensightReadFile is(baseDir_/fieldFileName, streamFormat_);
95 
96  if (!is.good())
97  {
99  << "Cannot read file " << is.name()
100  << " for field " << fieldName
101  << exit(FatalError);
102  }
103 
104  // Check that data type is as expected
105  string primitiveType;
106  is.read(primitiveType);
107 
108 
109  if (debug)
110  {
111  Info<< "primitiveType: " << primitiveType << endl;
112  }
113 
114  if (primitiveType != pTraits<Type>::typeName)
115  {
117  << "Expected '" << pTraits<Type>::typeName
118  << "' values but found type " << primitiveType << nl
119  << " This may be okay, but could also indicate an error"
120  << nl << nl;
121  }
122 
123  scalar value;
124  string strValue;
125  label iValue;
126 
127  // Read header info: part index, e.g. part 1
128  is.read(strValue);
129  is.read(iValue);
130 
131  // Allocate storage for data as a list per component
133  label n = surfPtr_->size();
134  forAll(values, cmptI)
135  {
136  values[cmptI].setCapacity(n);
137  }
138 
139  // Read data file using schema generated while reading the surface
140  forAll(schema_, i)
141  {
142  if (debug)
143  {
144  const string& faceType = schema_[i].first();
145  Info<< "Reading face type " << faceType << " data" << endl;
146  }
147 
148  const label nFace = schema_[i].second();
149 
150  if (nFace != 0)
151  {
152  is.read(strValue);
153 
154  for
155  (
156  direction cmptI=0;
157  cmptI < pTraits<Type>::nComponents;
158  ++cmptI
159  )
160  {
161  for (label faceI = 0; faceI < nFace; ++faceI)
162  {
163  is.read(value);
164  values[cmptI].append(value);
165  }
166  }
167  }
168  }
169 
170  auto tfield = tmp<Field<Type>>::New(n, Zero);
171  auto& field = tfield.ref();
172 
173  for (direction cmpti=0; cmpti < pTraits<Type>::nComponents; ++cmpti)
174  {
175  field.replace(cmpti, values[cmpti]);
176  values[cmpti].clear();
177  }
178 
179  return tfield;
180 }
181 
182 
183 // ************************************************************************* //
Foam::string::replace
string & replace(const std::string &s1, const std::string &s2, size_type pos=0)
Definition: string.C:121
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:316
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::IFstream::name
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:111
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
n
label n
Definition: TABSMDCalcMethod2.H:31
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::ensightReadFile
Ensight output with specialized read() for strings, integers and floats. Correctly handles binary rea...
Definition: ensightReadFile.H:49
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
field
rDeltaTY field()
timeIndex
label timeIndex
Definition: getTimeIndex.H:4
Foam::FatalError
error FatalError
Foam::IStringStream
Input from string buffer, using a ISstream.
Definition: StringStream.H:112
Foam::setw
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::ensightSurfaceReader::readFromLine
void readFromLine(const label nSkip, IStringStream &is, Type &value) const
Helper function to return Type after skipping n tokens.
Definition: ensightSurfaceReaderTemplates.C:35
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::pTraits
Traits class for primitives.
Definition: pTraits.H:52
Foam::setfill
Omanip< char > setfill(char fillch)
Definition: IOmanip.H:175
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::ensightSurfaceReader::readField
tmp< Field< Type > > readField(const label timeIndex, const label fieldIndex) const
Helper function to return a field.
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:306
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::ensightReadFile::read
virtual Istream & read(char *buf, std::streamsize count)
Binary read.
Definition: ensightReadFile.C:45