Loading...
Searching...
No Matches
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-2024 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// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32
33template<class Type>
34void Foam::rawIOField<Type>::readIOcontents
35(
36 Istream& is,
37 IOobjectOption::readOption readAverage
38)
39{
40 is >> static_cast<Field<Type>&>(*this);
41
42 if (IOobjectOption::isReadRequired(readAverage))
43 {
44 is >> average_;
45 hasAverage_ = true;
46 }
47 else if (IOobjectOption::isReadOptional(readAverage))
48 {
49 // Slightly heavy handed
50 const bool oldThrowingIOerr = FatalIOError.throwing(true);
51
52 try
53 {
54 is >> average_;
55 hasAverage_ = true;
56 }
57 catch (const Foam::IOerror& err)
58 {
59 average_ = Zero;
60 hasAverage_ = false;
61 }
62 FatalIOError.throwing(oldThrowingIOerr);
63 }
64}
65
66
67template<class Type>
68bool Foam::rawIOField<Type>::readIOcontents
69(
70 IOobjectOption::readOption readAverage
71)
72{
73 if (isReadRequired() || isReadOptional())
74 {
75 bool haveFile = false;
76 bool haveHeader = false;
77
78 // Replacement of regIOobject::headerOk() since that one complains
79 // if there is no header. TBD - Move up to headerOk()/fileHandler.
80 {
81 const fileName fName(filePath());
82
83 // Try to open raw first
84 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(fName));
85
86 if (isPtr && isPtr->good())
87 {
88 haveFile = true;
89
90 auto& is = *isPtr;
91
92 const token firstToken(is);
93
94 haveHeader = is.good() && firstToken.isWord("FoamFile");
95 }
96
97 if (debug)
98 {
99 Pout<< "rawIOField : object:" << name()
100 << " haveFile:" << haveFile
101 << " haveHeader:" << haveHeader << endl;
102 }
103 }
104
105
106 if (haveHeader)
107 {
108 // Read but don't fail upon wrong class. Could extend by providing
109 // wanted typeName. Tbd.
110 Istream& is = readStream(word::null);
111
112 if (is.good())
113 {
114 readIOcontents(is, readAverage);
115 close();
116 }
117 }
118 else if (haveFile)
119 {
120 // Failed reading header - fall back to IFstream
121 autoPtr<ISstream> isPtr(fileHandler().NewIFstream(objectPath()));
122
123 if (isPtr && isPtr->good())
124 {
125 readIOcontents(*isPtr, readAverage);
126 }
127 else
128 {
129 // Error if required but missing
130 if (isReadRequired())
131 {
133 << "Trying to read raw field" << endl
134 << exit(FatalIOError);
135 }
136 }
137 }
138
139 if (debug)
140 {
141 Pout<< "rawIOField : object:" << name()
142 << " size:" << this->size() << endl;
143 }
144
145 return true;
146 }
147
148 return false;
149}
150
151
152// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
153
154template<class Type>
156(
157 const IOobject& io,
159)
160:
162 hasAverage_(false),
163 average_(Zero)
164{
165 // Check for MUST_READ_IF_MODIFIED
167
168 readIOcontents(readAverage);
169}
170
171
172template<class Type>
174(
175 const IOobject& io,
176 const bool readAverage
177)
178:
179 rawIOField<Type>
180 (
181 io,
182 (
183 readAverage
184 ? IOobjectOption::readOption::MUST_READ
185 : IOobjectOption::readOption::NO_READ
186 )
188{}
189
190
191// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
192
193template<class Type>
195{
197 if (rio.readOpt() == IOobjectOption::READ_MODIFIED)
198 {
199 rio.readOpt(IOobjectOption::MUST_READ);
200 }
201 rio.resetHeader();
202
203 rawIOField<Type> reader(rio);
204
205 return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
206}
207
208
209// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
210
211template<class Type>
213{
215 if (hasAverage_)
216 {
217 os << token::NL << average_;
218 }
219 return os.good();
220}
221
222
223// ************************************************************************* //
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
constexpr Field() noexcept
Default construct.
Definition FieldI.H:24
Report an I/O error.
Definition error.H:370
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
@ NO_REGISTER
Do not request registration (bool: false).
readOption readOpt() const noexcept
Get the read option.
readOption
Enumeration defining read preferences.
@ NO_READ
Nothing to be read.
@ MUST_READ
Reading required.
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
void warnNoRereading() const
Helper: warn that type does not support re-reading.
void resetHeader(const word &newName=word::null)
Clear various bits (headerClassName, note, sizeof...) that would be obtained when reading from a file...
Definition IOobject.C:644
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
bool throwing() const noexcept
Return the current exception throwing state (on or off).
Definition error.H:223
Like IOField but falls back to raw IFstream if no header found. Optionally reads average value....
Definition rawIOField.H:52
rawIOField(const rawIOField &)=default
Default copy construct.
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition rawIOField.C:205
static Field< Type > readContents(const IOobject &io)
Read and return contents. The IOobject will not be registered.
Definition rawIOField.C:187
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition regIOobject.C:43
@ NL
Newline [isspace].
Definition token.H:143
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
const auto & io
auto & name
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125