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-2023 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 {
38 
40  (
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  {
77  {
78  if (obj.writeOpt() != IOobject::AUTO_WRITE)
79  {
80  return;
81  }
82 
83  break;
84  }
86  {
87  if (obj.writeOpt() != IOobject::NO_WRITE)
88  {
89  return;
90  }
91 
92  break;
93  }
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  && 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, runTime),
146  (
147  runTime.lookupObject<objectRegistry>
148  (
149  dict.lookupOrDefault("region", polyMesh::defaultRegion)
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 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
writeOption writeOpt() const
Definition: IOobject.H:370
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
const word & name() const
Return name.
Definition: IOobject.H:310
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:54
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
bool writeTime() const
Return true if this is a write time.
Definition: TimeStateI.H:58
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
const Time & time_
Reference to time.
const word & name() const
Return the name of this functionObject.
virtual bool read(const dictionary &)
Read and set the functionObject if its data have changed.
Calculates the natural logarithm of the specified scalar field.
Definition: log.H:106
FunctionObject base class for writing a list of objects registered to the database,...
virtual void writeObject(const regIOobject &obj)
Write the requested registered IO object.
virtual bool write()
Write function.
virtual bool read(const dictionary &)
Read the list of objects to be written.
Allows specification of different writing frequency of objects registered to the database.
Definition: writeObjects.H:135
virtual ~writeObjects()
Destructor.
Definition: writeObjects.C:161
static const NamedEnum< writeOption, 3 > writeOptionNames_
Definition: writeObjects.H:149
writeOption
Re-enumeration defining the write options, based on the original.
Definition: writeObjects.H:143
virtual bool execute()
Do nothing.
Definition: writeObjects.C:198
writeObjects(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: writeObjects.C:138
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:204
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:167
Registry of regIOobjects.
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define Log
Report write to Foam::Info if the local log switch is true.
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
error FatalError
static const char nl
Definition: Ostream.H:266
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
dictionary dict