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"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
39namespace functionObjects
40{
42
44 (
48 );
49}
50}
51
52const Foam::Enum
53<
55>
57({
58 { writeOption::AUTO_WRITE, "autoWrite" },
59 { writeOption::NO_WRITE, "noWrite" },
60 { writeOption::ANY_WRITE, "anyWrite" },
61});
62
64(
65 const Foam::Time& runTime,
67)
68{
70 dict.getOrDefault("region", Foam::polyMesh::defaultRegion);
71
72 if (regionName == "__TIME__")
73 {
74 return runTime;
75 }
76
77 return runTime.lookupObject<Foam::objectRegistry>(regionName);
78}
79
80// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
81
83(
84 const word& name,
85 const Time& runTime,
86 const dictionary& dict
87)
88:
90 obr_(setRegistry(runTime, dict)),
91 writeOption_(ANY_WRITE),
92 objectNames_()
93{
94 read(dict);
95}
96
97
98// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99
101{
103
104 if (dict.found("field"))
105 {
106 objectNames_.resize(1);
107 dict.readEntry("field", objectNames_.first());
108 }
109 else if (dict.found("fields"))
110 {
111 dict.readEntry("fields", objectNames_);
112 }
113 else
114 {
115 dict.readEntry("objects", objectNames_);
116 }
117
118 writeOption_ = writeOptionNames_.getOrDefault
119 (
120 "writeOption",
121 dict,
122 writeOption::ANY_WRITE
123 );
124
125 return true;
126}
127
128
130{
131 return true;
132}
133
134
136{
137 Log << type() << " " << name() << " write:" << nl;
138
139 if (!obr_.time().writeTime())
140 {
141 obr_.time().writeTimeDict();
142 }
143
144 // Get selection
145 const wordList selectedNames(obr_.sortedNames<regIOobject>(objectNames_));
146
147 // Warning if anything was missed
148 bitSet missed(objectNames_.size());
149
150 label index = 0;
151 for (const wordRe& select : objectNames_)
152 {
153 if (!ListOps::found(selectedNames, select))
154 {
155 missed.set(index);
156 }
157 ++index;
158 }
159
160 if (missed.any())
161 {
163 << "No corresponding selection for "
164 << flatOutput(subset(missed, objectNames_)) << nl
165 << "Available objects in database:"
166 << nl << obr_.sortedToc()
167 << endl;
168 }
169
170 for (const word& objName : selectedNames)
171 {
172 regIOobject& obj = obr_.lookupObjectRef<regIOobject>(objName);
173
174 switch (writeOption_)
175 {
176 case AUTO_WRITE:
177 {
178 if (obj.writeOpt() != IOobject::AUTO_WRITE)
179 {
180 continue;
181 }
182
183 break;
184 }
185 case NO_WRITE:
186 {
187 if (obj.writeOpt() != IOobject::NO_WRITE)
188 {
189 continue;
190 }
191
192 break;
193 }
194 case ANY_WRITE:
195 {
196 break;
197 }
198 default:
199 {
201 << "Unknown writeOption "
202 << writeOptionNames_[writeOption_]
203 << ". Valid writeOption types are "
204 << writeOptionNames_
205 << exit(FatalError);
206
207 continue;
208 break;
209 }
210 }
211
212 if
213 (
215 && obr_.time().writeTime()
216 )
217 {
218 Log << " automatically written object " << obj.name() << endl;
219 }
220 else
221 {
222 Log << " writing object " << obj.name() << endl;
223
224 obj.write();
225 }
226 }
227
228 return true;
229}
230
231
232// ************************************************************************* //
Various functions to operate on Lists.
#define Log
Definition: PDRblock.C:35
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:61
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:66
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:590
bool any() const
True if any bits in this bitset are set.
Definition: bitSetI.H:469
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Abstract base-class for Time/database function objects.
Allows specification of different writing frequency of objects registered to the database.
Definition: writeObjects.H:142
static const Enum< writeOption > writeOptionNames_
Definition: writeObjects.H:156
writeOption
Re-enumeration defining the write options, based on the original.
Definition: writeObjects.H:150
virtual bool execute()
Do nothing.
Definition: writeObjects.C:129
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:135
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:100
Registry of regIOobjects.
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:321
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
virtual bool write(const bool valid=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:83
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
Foam::word regionName(Foam::polyMesh::defaultRegion)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#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:679
Namespace for OpenFOAM.
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: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
const Foam::objectRegistry & setRegistry(const Foam::Time &runTime, const Foam::dictionary &dict)
Definition: writeObjects.C:64