rawIOField.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) 2016-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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 "rawIOField.H"
29#include "IFstream.H"
30
31// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32
33template<class Type>
34Foam::rawIOField<Type>::rawIOField(const IOobject& io, const bool readAverage)
35:
37 average_(Zero)
38{
39 // Check for MUST_READ_IF_MODIFIED
40 warnNoRereading<rawIOField<Type>>();
41
42 if
43 (
47 )
48 {
49 bool haveFile = false;
50 bool headerOk = false;
51
52 // Replacement of regIOobject::headerok() since that one complains
53 // if there is no header. TBD - Move up to headerOk()/fileHandler.
54 {
55 const fileName fName(filePath());
56
57 // Try to open raw first
58 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
59
60 if (isPtr && isPtr->good())
61 {
62 haveFile = true;
63
64 ISstream& is = isPtr();
65
66 const token firstToken(is);
67
68 headerOk = is.good() && firstToken.isWord("FoamFile");
69 }
70
71 isPtr.clear();
72
73 if (debug)
74 {
75 Pout<< "rawIOField : object:" << io.name()
76 << " haveFile:" << haveFile
77 << " headerOk:" << headerOk << endl;
78 }
79 }
80
81
82 if (headerOk)
83 {
84 // Read but don't fail upon wrong class. Could extend by providing
85 // wanted typeName. Tbd.
87
88 if (is.good())
89 {
90 is >> static_cast<Field<Type>&>(*this);
91 if (readAverage)
92 {
93 average_ = pTraits<Type>(is);
94 }
95 close();
96 }
97 }
98 else if (haveFile)
99 {
100 // Failed reading - fall back to IFstream
101 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(io.objectPath()));
102
103 if (!isPtr || !isPtr->good())
104 {
106 {
108 << "Trying to read raw field" << endl
109 << exit(FatalIOError);
110 }
111 }
112 else
113 {
114 ISstream& is = isPtr();
115
116 is >> static_cast<Field<Type>&>(*this);
117 if (readAverage)
118 {
119 average_ = pTraits<Type>(is);
120 }
121 }
122 }
123
124 if (debug)
125 {
126 Pout<< "rawIOField : object:" << io.name()
127 << " size:" << this->size() << endl;
128 }
129 }
130}
131
132
133// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134
135template<class Type>
137{
138 os << static_cast<const Field<Type>&>(*this);
139 if (average_ != pTraits<Type>::zero)
140 {
141 os << token::NL << average_;
142 }
143 return os.good();
144}
145
146
147// ************************************************************************* //
Generic templated field type.
Definition: Field.H:82
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:180
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
label size() const noexcept
The number of elements in the UList.
Definition: UListI.H:420
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:176
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
A class for handling file names.
Definition: fileName.H:76
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
Like IOField but falls back to raw IFstream if no header found. Optionally reads average value....
Definition: rawIOField.H:56
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
void close()
Close Istream.
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:432
bool headerOk()
Read and check header info. Does not check the headerClassName.
Definition: regIOobject.C:438
A token holds an item read from Istream.
Definition: token.H:69
@ NL
Newline [isspace].
Definition: token.H:124
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
const fileOperation & fileHandler()
Get current file handler.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
IOerror FatalIOError
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
A non-counting (dummy) refCount.
Definition: refCount.H:59