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-2020 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 =
69  is.good()
70  && firstToken.isWord()
71  && firstToken.wordToken() == "FoamFile";
72  }
73 
74  isPtr.clear();
75 
76  if (debug)
77  {
78  Pout<< "rawIOField : object:" << io.name()
79  << " haveFile:" << haveFile
80  << " headerOk:" << headerOk << endl;
81  }
82  }
83 
84 
85  if (headerOk)
86  {
87  // Read but don't fail upon wrong class. Could extend by providing
88  // wanted typeName. Tbd.
90 
91  if (is.good())
92  {
93  is >> static_cast<Field<Type>&>(*this);
94  if (readAverage)
95  {
96  average_ = pTraits<Type>(is);
97  }
98  close();
99  }
100  }
101  else if (haveFile)
102  {
103  // Failed reading - fall back to IFstream
104  autoPtr<ISstream> isPtr(fileHandler().NewIFstream(io.objectPath()));
105 
106  if (!isPtr || !isPtr->good())
107  {
109  {
110  FatalIOErrorInFunction(*isPtr)
111  << "Trying to read raw field" << endl
112  << exit(FatalIOError);
113  }
114  }
115  else
116  {
117  ISstream& is = isPtr();
118 
119  is >> static_cast<Field<Type>&>(*this);
120  if (readAverage)
121  {
122  average_ = pTraits<Type>(is);
123  }
124  }
125  }
126 
127  if (debug)
128  {
129  Pout<< "rawIOField : object:" << io.name()
130  << " size:" << this->size() << endl;
131  }
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 template<class Type>
140 {
141  os << static_cast<const Field<Type>&>(*this);
142  if (average_ != pTraits<Type>::zero)
143  {
144  os << token::NL << average_;
145  }
146  return os.good();
147 }
148 
149 
150 // ************************************************************************* //
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:104
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:70
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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:1354
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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 or DIRECTIVE word.
Definition: tokenI.H:583
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:139
Foam::IOobject::READ_IF_PRESENT
Definition: IOobject.H:122
IFstream.H
Foam::token::NL
Newline [isspace].
Definition: token.H:119
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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:71
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:182
Foam::pTraits< Type >
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:165
Foam::regIOobject::filePath
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:447
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
Foam::autoPtr::clear
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:181
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
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:140
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:224
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:209
Foam::regIOobject::headerOk
bool headerOk()
Read and check header info.
Definition: regIOobject.C:453
Foam::IOobject::MUST_READ
Definition: IOobject.H:120