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-2024 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 "SpanStream.H"
29 #include "ensightPTraits.H"
30 
31 // * * * * * * * * * * Protected Static Member Functions * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const std::string& buffer,
37  Type& value
38 )
39 {
40  ISpanStream is(buffer.data(), buffer.size());
41  is >> value;
42 }
43 
44 
45 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
46 
47 template<class Type>
49 (
50  const fileName& dataFile,
51  const word& fieldName,
52  const label timeIndex
53 ) const
54 {
55  auto tfield = tmp<Field<Type>>::New(surfPtr_->nFaces(), Zero);
56  auto& field = tfield.ref();
57 
58  if (!masterOnly_ || UPstream::master(UPstream::worldComm))
59  {
60  // Use previously detected ascii/binary format
61  ensightReadFile is(dataFile, readFormat_);
62 
63  if (!is.good())
64  {
66  << "Cannot read file " << is.name()
67  << " for field " << fieldName
68  << exit(FatalError);
69  }
70 
71  // If transient single-file
72  is.seekTime(timeIndex);
73 
74 
75  // Check that data type is as expected
76  // (assuming OpenFOAM generated the data set)
77  string primitiveType;
78  is.read(primitiveType);
79 
80  DebugInfo << "primitiveType: " << primitiveType << endl;
81 
82  if
83  (
84  debug
85  && primitiveType != ensightPTraits<Type>::typeName
86  && primitiveType != pTraits<Type>::typeName
87  )
88  {
90  << "Expected <" << ensightPTraits<Type>::typeName
91  << "> values for <" << pTraits<Type>::typeName
92  << "> but found " << primitiveType << nl
93  << " This may be okay, but could indicate an error"
94  << nl << nl;
95  }
96 
97  string strValue;
98  label intValue;
99 
100  // Read header info: part index, e.g. part 1
101  is.read(strValue);
102  is.read(intValue);
103 
104  label begFace = 0;
105 
106  // Loop through different element types when reading the field values
107  for (const faceInfoTuple& facesInfo : faceTypeInfo_)
108  {
109  // [faceType, faceCount]
110  const label endFace = begFace + facesInfo.second();
111 
112  DebugInfo
113  << "Reading <" << pTraits<Type>::typeName << "> face type "
114  << ensightFaces::elemNames[facesInfo.first()]
115  << " data:" << facesInfo.second() << endl;
116 
117  if (begFace < endFace)
118  {
119  // The element type, optionally with 'undef'
120  is.read(strValue);
121 
122  if (strValue.contains("undef"))
123  {
124  // Skip undef entry
125  scalar value;
126  is.read(value);
127  }
128 
129  // Ensight fields are written component-wise
130  // (can be in different order than OpenFOAM uses)
131 
132  for (direction d = 0; d < pTraits<Type>::nComponents; ++d)
133  {
134  const direction cmpt =
136 
137  for (label facei = begFace; facei < endFace; ++facei)
138  {
139  scalar value;
140  is.read(value);
141  setComponent(field[facei], cmpt) = value;
142  }
143  }
144 
145  begFace = endFace;
146  }
147  }
148  }
149 
150  if (masterOnly_ && UPstream::parRun())
151  {
153  }
155  return tfield;
156 }
157 
158 
159 template<class Type>
161 (
162  const label timeIndex,
163  const label fieldIndex
164 ) const
165 {
166  if (fieldIndex < 0 || fieldIndex >= fieldNames_.size())
167  {
169  << "Invalid timeIndex:" << timeIndex
170  << " should be in range [0.." << fieldNames_.size() << ')' << nl
171  << "Possibly used incorrect field lookup name. Known field names: "
172  << flatOutput(fieldNames_) << nl
173  << exit(FatalError);
174  }
175 
176  const word& fieldName = fieldNames_[fieldIndex];
177 
178  const label fileIndex =
179  (
180  (timeIndex >= 0 && timeIndex < fileNumbers_.size())
181  ? fileNumbers_[timeIndex]
182  : (timeStartIndex_ + timeIndex*timeIncrement_)
183  );
184 
185  const fileName dataFile
186  (
187  baseDir_
188  / ensightCase::expand_mask(fieldFileNames_[fieldIndex], fileIndex)
189  );
190 
191  if (debug)
192  {
193  Pout<< "Read <" << pTraits<Type>::typeName << "> field, file="
194  << dataFile << endl;
195  }
196 
197  return readField<Type>(dataFile, fieldName, timeIndex);
198 }
199 
200 
201 // ************************************************************************* //
virtual Istream & read(char *buf, std::streamsize count) override
Binary read.
rDeltaTY field()
uint8_t direction
Definition: direction.H:46
Input/output streams with (internal or external) character storage.
A class for handling file names.
Definition: fileName.H:72
bool seekTime(const label timeIndex)
Transient single-file: seek to the file position corresponding to the given time index.
bool contains(char c) const noexcept
True if string contains given character (cf. C++23)
Definition: string.H:411
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
static const char * elemNames[nTypes]
The ensight &#39;Face&#39; element type names.
Definition: ensightFaces.H:94
Ensight names and component order for base types.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
static StringType expand_mask(const StringType &input, const label index)
Replace the &#39;*&#39; mask chars with zero-padded integer value.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1061
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
::Foam::direction nComponents(const expressions::valueTypeCode) noexcept
The number of components associated with given valueTypeCode.
Definition: exprTraits.C:40
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:421
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
static void readFrom(const std::string &buffer, Type &value)
Helper function to return Type from string.
virtual const fileName & name() const override
Read/write access to the name of the stream.
Definition: ISstream.H:147
A class for handling words, derived from Foam::string.
Definition: word.H:63
A variant of IFstream with specialised handling for Ensight reading of strings, integers and floats (...
#define DebugInfo
Report an information message using Foam::Info.
int debug
Static debugging option.
tmp< Field< Type > > readField(const fileName &dataFile, const word &fieldName, const label timeIndex=0) const
Helper function to return a field.
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
A class for managing temporary objects.
Definition: HashPtrTable.H:50
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components)
Definition: label.H:144
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: ISpanStream.H:251
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
label timeIndex
Definition: getTimeIndex.H:24
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127