writeObjects.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "writeObjects.H"
27 #include "Time.H"
28 #include "polyMesh.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(writeObjects, 0);
38 
40  (
41  functionObject,
42  writeObjects,
43  dictionary
44  );
45 }
46 }
47 
48 template<>
49 const char* Foam::NamedEnum
50 <
52  3
53 >::names[] =
54 {
55  "autoWrite",
56  "noWrite",
57  "anyWrite"
58 };
59 
60 const Foam::NamedEnum
61 <
63  3
65 
66 
67 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
68 
69 void Foam::functionObjects::writeObjects::writeObject
70 (
71  const regIOobject& obj
72 )
73 {
74  switch (writeOption_)
75  {
76  case writeOption::AUTO_WRITE:
77  {
78  if (obj.writeOpt() != IOobject::AUTO_WRITE)
79  {
80  return;
81  }
82 
83  break;
84  }
85  case writeOption::NO_WRITE:
86  {
87  if (obj.writeOpt() != IOobject::NO_WRITE)
88  {
89  return;
90  }
91 
92  break;
93  }
94  case writeOption::ANY_WRITE:
95  {
96  break;
97  }
98  default:
99  {
101  << "Unknown writeOption "
102  << writeOptionNames_[writeOption_]
103  << ". Valid writeOption types are" << writeOptionNames_
104  << exit(FatalError);
105  }
106  }
107 
108  if
109  (
111  && writeObr_.time().writeTime()
112  )
113  {
114  Log << " automatically written object " << obj.name() << endl;
115  }
116  else
117  {
118  if (obj.db().cacheTemporaryObject(obj.name()))
119  {
120  // If the object is a temporary field expression wrap with tmp<...>
121  const word name(obj.name());
122  regIOobject& objRef = const_cast<regIOobject&>(obj);
123  objRef.IOobject::rename("tmp<" + name + ">");
125  objRef.IOobject::rename(name);
126  }
127  else
128  {
130  }
131  }
132 }
133 
134 
135 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
136 
138 (
139  const word& name,
140  const Time& runTime,
141  const dictionary& dict
142 )
143 :
144  functionObject(name),
146  (
148  (
150  ),
151  log
152  ),
153  writeOption_(writeOption::ANY_WRITE)
154 {
155  read(dict);
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
160 
162 {}
163 
164 
165 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
166 
168 {
169  if (dict.found("field"))
170  {
171  writeObjectNames_.setSize(1);
172  dict.lookup("field") >> writeObjectNames_[0];
173  }
174  else if (dict.found("fields"))
175  {
176  dict.lookup("fields") >> writeObjectNames_;
177  }
178  else
179  {
181  }
182 
183  if (dict.found("writeOption"))
184  {
185  writeOption_ = writeOptionNames_.read(dict.lookup("writeOption"));
186  }
187  else
188  {
189  writeOption_ = writeOption::ANY_WRITE;
190  }
191 
192  executeAtStart_ = dict.lookupOrDefault<Switch>("executeAtStart", false);
193 
194  return functionObject::read(dict);
195 }
196 
197 
199 {
200  return true;
201 }
202 
203 
205 {
206  Log << type() << " " << name() << " write:" << nl;
207 
209 
210  Log << endl;
211 
212  return true;
213 }
214 
215 
216 // ************************************************************************* //
virtual bool write()
Write function.
Calculates the natural logarithm of the specified scalar field.
Definition: log.H:103
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:643
const word & name() const
Return name.
Definition: IOobject.H:303
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:204
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
static const NamedEnum< writeOption, 3 > writeOptionNames_
Definition: writeObjects.H:148
addToRunTimeSelectionTable(functionObject, Qdot, dictionary)
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:309
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
Abstract base-class for Time/database functionObjects.
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
writeObjects(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: writeObjects.C:138
writeOption
Re-enumeration defining the write options, based on the original.
Definition: writeObjects.H:141
virtual bool read(const dictionary &)
Read and set the functionObject if its data have changed.
static const char nl
Definition: Ostream.H:260
virtual void writeObject(const regIOobject &obj)
Write the requested registered IO object.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
defineTypeNameAndDebug(Qdot, 0)
writeOption writeOpt() const
Definition: IOobject.H:363
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
FunctionObject base class for writing a list of objects registered to the database, on behalf of the inheriting function object, on when those should be written to disk.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
#define Log
Report write to Foam::Info if the local log switch is true.
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
virtual bool execute()
Do nothing.
Definition: writeObjects.C:198
virtual ~writeObjects()
Destructor.
Definition: writeObjects.C:161
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:167
virtual bool read(const dictionary &)
Read the list of objects to be written.
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844