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 -------------------------------------------------------------------------------
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 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
33 template<class Type>
34 Foam::rawIOField<Type>::rawIOField(const IOobject& io, const bool readAverage)
35 :
36  regIOobject(io),
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  {
107  FatalIOErrorInFunction(*isPtr)
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 
135 template<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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
rawIOField.H
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::ISstream
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:55
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::rawIOField::rawIOField
rawIOField(const rawIOField &)=default
Default copy construct.
Foam::token::isWord
bool isWord() const noexcept
Token is word-variant (WORD, DIRECTIVE)
Definition: tokenI.H:609
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::rawIOField::writeData
bool writeData(Ostream &os) const
Pure virtual writeData function.
Definition: rawIOField.C:136
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:187
Foam::autoPtr::good
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
IFstream.H
Foam::token::NL
Newline [isspace].
Definition: token.H:124
os
OBJstream os(runTime.globalPath()/outputName)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::readOpt
readOption readOpt() const noexcept
The read option.
Definition: IOobjectI.H:164
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:171
Foam::pTraits< Type >
Foam::regIOobject::filePath
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:430
Foam::word::null
static const word null
An empty word.
Definition: word.H:80
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:186
Foam::autoPtr::clear
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:176
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::regIOobject::readStream
Istream & readStream(const word &, const bool valid=true)
Return Istream and check object type against that given.
Definition: regIOobjectRead.C:129
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:436
Foam::IOobject::MUST_READ
Definition: IOobject.H:185