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-2025 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 const Foam::NamedEnum
49 <
51  3
53 {
54  "autoWrite",
55  "noWrite",
56  "anyWrite"
57 };
58 
59 
60 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
61 
62 void Foam::functionObjects::writeObjects::writeObject
63 (
64  const regIOobject& obj
65 )
66 {
67  switch (writeOption_)
68  {
70  {
71  if (obj.writeOpt() != IOobject::AUTO_WRITE)
72  {
73  return;
74  }
75 
76  break;
77  }
79  {
80  if (obj.writeOpt() != IOobject::NO_WRITE)
81  {
82  return;
83  }
84 
85  break;
86  }
88  {
89  break;
90  }
91  }
92 
93  if
94  (
95  obj.writeOpt() == IOobject::AUTO_WRITE
96  && time_.writeTime()
97  )
98  {
99  Log << " automatically written object " << obj.name() << endl;
100  }
101  else
102  {
103  if (obj.db().cacheTemporaryObject(obj.name()))
104  {
105  // If the object is a temporary field expression wrap with tmp<...>
106  const word name(obj.name());
107  regIOobject& objRef = const_cast<regIOobject&>(obj);
108  objRef.IOobject::rename("tmp<" + name + ">");
110  objRef.IOobject::rename(name);
111  }
112  else
113  {
115  }
116  }
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121 
123 (
124  const word& name,
125  const Time& runTime,
126  const dictionary& dict
127 )
128 :
129  functionObject(name, runTime),
131  (
132  runTime.lookupObject<objectRegistry>
133  (
134  dict.lookupOrDefault("region", polyMesh::defaultRegion)
135  ),
136  log
137  ),
138  writeOption_(writeOption::ANY_WRITE)
139 {
140  read(dict);
141 }
142 
143 
144 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
145 
147 {}
148 
149 
150 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
151 
153 {
154  if (dict.found("field"))
155  {
156  writeObjectNames_.setSize(1);
157  dict.lookup("field") >> writeObjectNames_[0];
158  }
159  else if (dict.found("fields"))
160  {
161  dict.lookup("fields") >> writeObjectNames_;
162  }
163  else
164  {
166  }
167 
168  if (dict.found("writeOption"))
169  {
170  writeOption_ = writeOptionNames_.read(dict.lookup("writeOption"));
171  }
172  else
173  {
174  writeOption_ = writeOption::ANY_WRITE;
175  }
176 
177  executeAtStart_ = dict.lookupOrDefault<Switch>("executeAtStart", false);
178 
179  return functionObject::read(dict);
180 }
181 
182 
184 {
185  return true;
186 }
187 
188 
190 {
191  Log << type() << " " << name() << " write:" << nl;
192 
194 
195  Log << endl;
196 
197  return true;
198 }
199 
200 
201 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:55
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
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:139
virtual ~writeObjects()
Destructor.
Definition: writeObjects.C:146
static const NamedEnum< writeOption, 3 > writeOptionNames_
Definition: writeObjects.H:153
writeOption
Re-enumeration defining the write options, based on the original.
Definition: writeObjects.H:147
virtual bool execute()
Do nothing.
Definition: writeObjects.C:183
writeObjects(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: writeObjects.C:123
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:189
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:152
Registry of regIOobjects.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
A class for handling words, derived from string.
Definition: word.H:62
#define Log
Report write to Foam::Info if the local log switch is true.
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
static const char nl
Definition: Ostream.H:267
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