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-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 <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 {
69 
70  const word& fieldName(fieldNames_[fieldIndex]);
71  const label fileIndex = timeStartIndex_ + timeIndex*timeIncrement_;
72 
73  fileName fieldFileName(replaceMask(fieldFileNames_[fieldIndex], fileIndex));
74  ensightReadFile is(baseDir_/fieldFileName, streamFormat_);
75 
76  if (!is.good())
77  {
79  << "Cannot read file " << is.name()
80  << " for field " << fieldName
81  << exit(FatalError);
82  }
83 
84  // Check that data type is as expected
85  string primitiveType;
86  is.read(primitiveType);
87 
88 
89  DebugInfo << "primitiveType: " << primitiveType << endl;
90 
91  if (primitiveType != pTraits<Type>::typeName)
92  {
94  << "Expected '" << pTraits<Type>::typeName
95  << "' values but found type " << primitiveType << nl
96  << " This may be okay, but could also indicate an error"
97  << nl << nl;
98  }
99 
100  scalar value;
101  string strValue;
102  label iValue;
103 
104  // Read header info: part index, e.g. part 1
105  is.read(strValue);
106  is.read(iValue);
107 
108  // Allocate storage for data as a list per component
110  label n = surfPtr_->size();
111  forAll(values, cmptI)
112  {
113  values[cmptI].setCapacity(n);
114  }
115 
116  // Read data file using schema generated while reading the surface
117  forAll(schema_, i)
118  {
119  DebugInfo
120  << "Reading face type "
121  << schema_[i].first() << " data" << endl;
122 
123  const label nFace = schema_[i].second();
124 
125  if (nFace != 0)
126  {
127  is.read(strValue);
128 
129  for
130  (
131  direction cmptI=0;
132  cmptI < pTraits<Type>::nComponents;
133  ++cmptI
134  )
135  {
136  for (label faceI = 0; faceI < nFace; ++faceI)
137  {
138  is.read(value);
139  values[cmptI].append(value);
140  }
141  }
142  }
143  }
144 
145  auto tfield = tmp<Field<Type>>::New(n, Zero);
146  auto& field = tfield.ref();
147 
148  for (direction cmpti=0; cmpti < pTraits<Type>::nComponents; ++cmpti)
149  {
150  field.replace(cmpti, values[cmpti]);
151  values[cmpti].clear();
152  }
153 
154  return tfield;
155 }
156 
157 
158 // ************************************************************************* //
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::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
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:113
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::ensightReadFile
Ensight output with specialized read() for strings, integers and floats. Correctly handles binary rea...
Definition: ensightReadFile.H:49
DebugInFunction
#define DebugInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:388
field
rDeltaTY field()
Foam::FatalError
error FatalError
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
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:453
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
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::ensightSurfaceReader::readField
tmp< Field< Type > > readField(const label timeIndex, const label fieldIndex) const
Helper function to return a field.
timeIndex
label timeIndex
Definition: getTimeIndex.H:30
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:340
Foam::ensightReadFile::read
virtual Istream & read(char *buf, std::streamsize count)
Binary read.
Definition: ensightReadFile.C:45