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-2023 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 "rawIOField.H"
29 #include "IFstream.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
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 
67 template<class Type>
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  readContents(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  readContents(*isPtr, readAverage);
126  }
127  else
128  {
129  // Error if required but missing
130  if (isReadRequired())
131  {
132  FatalIOErrorInFunction(*isPtr)
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 
154 template<class Type>
156 (
157  const IOobject& io,
158  IOobjectOption::readOption readAverage
159 )
160 :
161  regIOobject(io),
162  hasAverage_(false),
163  average_(Zero)
164 {
165  // Check for MUST_READ_IF_MODIFIED
166  warnNoRereading<rawIOField<Type>>();
168  readContents(readAverage);
169 }
170 
171 
172 template<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  )
187  )
188 {}
189 
190 
191 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
192 
193 template<class Type>
195 {
197  if (rio.readOpt() == IOobjectOption::MUST_READ_IF_MODIFIED)
198  {
199  rio.readOpt(IOobjectOption::MUST_READ);
200  }
201 
202  rawIOField<Type> reader(rio);
203 
204  return Field<Type>(std::move(static_cast<Field<Type>&>(reader)));
205 }
206 
207 
208 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
209 
210 template<class Type>
212 {
213  os << static_cast<const Field<Type>&>(*this);
214  if (hasAverage_)
215  {
216  os << token::NL << average_;
217  }
218  return os.good();
219 }
220 
221 
222 // ************************************************************************* //
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
rawIOField(const rawIOField &)=default
Default copy construct.
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:218
virtual bool writeData(Ostream &os) const
The writeData method for regIOobject write operation.
Definition: rawIOField.C:204
Newline [isspace].
Definition: token.H:130
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
Like IOField but falls back to raw IFstream if no header found. Optionally reads average value...
Definition: rawIOField.H:48
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Generic templated field type.
Definition: Field.H:62
Report an I/O error.
Definition: error.H:370
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Do not request registration (bool: false)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
readOption
Enumeration defining read preferences.