Loading...
Searching...
No Matches
writeObjects.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2016-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "writeObjects.H"
30#include "Time.H"
31#include "polyMesh.H"
32#include "ListOps.H"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37namespace Foam
38{
39namespace functionObjects
40{
42
44 (
48 );
49}
50}
51
52const Foam::Enum
53<
55>
57({
58 { writeOption::NO_WRITE, "noWrite" },
59 { writeOption::AUTO_WRITE, "autoWrite" },
60 { writeOption::ANY_WRITE, "anyWrite" },
61 { writeOption::LOG, "log" },
62});
63
65(
66 const Foam::Time& runTime,
68)
69{
71 dict.getOrDefault("region", Foam::polyMesh::defaultRegion);
72
73 if (regionName == "__TIME__")
74 {
75 return runTime;
76 }
77
78 return runTime.lookupObject<Foam::objectRegistry>(regionName);
79}
80
81// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82
83Foam::functionObjects::writeObjects::writeObjects
84(
85 const word& name,
86 const Time& runTime,
87 const dictionary& dict
88)
89:
91 obr_(setRegistry(runTime, dict)),
92 writeOption_(ANY_WRITE),
93 objectNames_()
95 read(dict);
96}
97
98
99// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100
102{
104 {
105 return false;
106 }
107
108 if (dict.found("field"))
109 {
110 objectNames_.resize(1);
111 dict.readEntry("field", objectNames_.first());
112 }
113 else if (dict.found("fields"))
114 {
115 dict.readEntry("fields", objectNames_);
116 }
117 else
118 {
119 dict.readEntry("objects", objectNames_);
120 }
121
122 writeOption_ = writeOptionNames_.getOrDefault
123 (
124 "writeOption",
125 dict,
127 );
128
129 return true;
130}
131
134{
135 return true;
136}
137
138
140{
141 if (writeOption_ == writeOption::LOG)
142 {
143 const auto& classes = obr_.classes();
144
145 Log << "Registered objects:\n";
146
147 forAllConstIters(classes, classInfo)
148 {
149 const word& className = classInfo.key();
150 const wordHashSet& objectSet = classInfo();
151
152 Log << " " << className << ":\n";
153
154 for (const auto& objectName : objectSet)
155 {
156 Log << " " << objectName << "\n";
157 }
158 Log << nl;
159 }
160
161 Log << endl;
162
163 return true;
164 }
165
166 Log << type() << " " << name() << " write:" << nl;
167
168 if (!obr_.time().writeTime())
169 {
170 obr_.time().writeTimeDict();
171 }
172
173 // Get selection
174 const wordList selectedNames(obr_.sortedNames<regIOobject>(objectNames_));
175
176 // Warning if anything was missed
177 bitSet missed(objectNames_.size());
178
179 label index = 0;
180 for (const wordRe& select : objectNames_)
181 {
182 if (!ListOps::found(selectedNames, select))
183 {
184 missed.set(index);
185 }
186 ++index;
187 }
188
189 if (missed.any())
190 {
192 << "No corresponding selection for "
193 << flatOutput(subset(missed, objectNames_)) << nl
194 << "Available objects in database:"
195 << nl << obr_.sortedToc()
196 << endl;
197 }
198
199 for (const word& objName : selectedNames)
200 {
201 regIOobject& obj = obr_.lookupObjectRef<regIOobject>(objName);
202
203 switch (writeOption_)
204 {
205 case writeOption::NO_WRITE:
206 {
207 if (obj.writeOpt() != IOobject::NO_WRITE)
208 {
209 continue;
210 }
211
212 break;
213 }
214 case writeOption::AUTO_WRITE:
215 {
216 if (obj.writeOpt() != IOobject::AUTO_WRITE)
217 {
218 continue;
219 }
220
221 break;
222 }
223 case writeOption::ANY_WRITE:
224 {
225 break;
226 }
227 default:
228 {
230 << "Unknown writeOption "
231 << writeOptionNames_[writeOption_]
232 << ". Valid writeOption types are "
233 << writeOptionNames_
234 << exit(FatalError);
235
236 continue;
237 break;
238 }
239 }
240
241 if
242 (
243 obj.writeOpt() == IOobject::AUTO_WRITE
244 && obr_.time().writeTime()
245 )
246 {
247 Log << " automatically written object " << obj.name() << endl;
248 }
249 else
250 {
251 // TBD:
252 // If the object is a temporary field expression wrap with tmp<...>
253
254 // if (obj.db().is_cacheTemporaryObject(obj))
255 // {
256 // const word oldName(obj.name());
257 // obj.IOobject::rename("tmp<" + oldName + ">");
258 //
259 // Log << " writing object " << obj.name() << endl;
260 // obj.write();
261 // obj.IOobject::rename(oldName);
262 // }
263 // else
264 {
265 Log << " writing object " << obj.name() << endl;
266 obj.write();
267 }
268 }
269 }
270
271 return true;
272}
273
274
275// ************************************************************************* //
Various functions to operate on Lists.
#define Log
Definition PDRblock.C:28
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition Enum.H:57
writeOption writeOpt() const noexcept
Get the write option.
@ NO_WRITE
Ignore writing from objectRegistry::writeObject().
@ AUTO_WRITE
Automatically write from objectRegistry::writeObject().
const word & name() const noexcept
Return the object name.
Definition IOobjectI.H:205
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
T & first()
Access first element of the list, position [0].
Definition UList.H:957
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition bitSetI.H:502
bool any() const
True if any bits in this bitset are set.
Definition bitSetI.H:408
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
functionObject(const word &name, const bool withNamePrefix=defaultUseNamePrefix)
Construct from components.
Allows specification of different writing frequency of objects registered to the database.
static const Enum< writeOption > writeOptionNames_
Names for writeOption.
writeOption
Re-enumeration defining the write options, Naming based on the IOobjectOption::writeOption.
virtual bool execute()
Execute the function-object operations (no-op).
virtual bool write()
Write the function-object results.
virtual bool read(const dictionary &)
Read the function-object dictionary.
Registry of regIOobjects.
static word defaultRegion
Return the default region name.
Definition polyMesh.H:406
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
Foam::word regionName(args.getOrDefault< word >("region", Foam::polyMesh::defaultRegion))
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as found_if.
Definition ListOps.H:886
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235
const Foam::objectRegistry & setRegistry(const Foam::Time &runTime, const Foam::dictionary &dict)