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 -------------------------------------------------------------------------------
11 License
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 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(writeObjects, 0);
42 
44  (
45  functionObject,
46  writeObjects,
47  dictionary
48  );
49 }
50 }
51 
52 const Foam::Enum
53 <
55 >
57 ({
58  { writeOption::NO_WRITE, "noWrite" },
59  { writeOption::AUTO_WRITE, "autoWrite" },
60  { writeOption::ANY_WRITE, "anyWrite" },
61 });
62 
64 (
65  const Foam::Time& runTime,
66  const Foam::dictionary& dict
67 )
68 {
69  const Foam::word regionName =
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 
82 Foam::functionObjects::writeObjects::writeObjects
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 
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 writeOption::NO_WRITE:
177  {
178  if (obj.writeOpt() != IOobject::NO_WRITE)
179  {
180  continue;
181  }
182 
183  break;
184  }
185  case writeOption::AUTO_WRITE:
186  {
187  if (obj.writeOpt() != IOobject::AUTO_WRITE)
188  {
189  continue;
190  }
191 
192  break;
193  }
194  case writeOption::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  (
214  obj.writeOpt() == IOobject::AUTO_WRITE
215  && obr_.time().writeTime()
216  )
217  {
218  Log << " automatically written object " << obj.name() << endl;
219  }
220  else
221  {
222  // TBD:
223  // If the object is a temporary field expression wrap with tmp<...>
224 
225  // if (obj.db().is_cacheTemporaryObject(obj))
226  // {
227  // const word oldName(obj.name());
228  // obj.IOobject::rename("tmp<" + oldName + ">");
229  //
230  // Log << " writing object " << obj.name() << endl;
231  // obj.write();
232  // obj.IOobject::rename(oldName);
233  // }
234  // else
235  {
236  Log << " writing object " << obj.name() << endl;
237  obj.write();
238  }
239  }
240  }
241 
242  return true;
243 }
244 
245 
246 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:504
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:128
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as found_if.
Definition: ListOps.H:826
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
T & first()
Access first element of the list, position [0].
Definition: UList.H:853
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Ignore writing from objectRegistry::writeObject()
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name...
Definition: Enum.C:173
Macros for easy insertion into run-time selection tables.
Various functions to operate on Lists.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
static word defaultRegion
Return the default region name.
Definition: polyMesh.H:405
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
writeOption
Re-enumeration defining the write options, Naming based on the IOobjectOption::writeOption.
Definition: writeObjects.H:147
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
Foam::word regionName(args.getOrDefault< word >("region", Foam::polyMesh::defaultRegion))
static const Enum< writeOption > writeOptionNames_
Definition: writeObjects.H:154
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Automatically write from objectRegistry::writeObject()
#define Log
Definition: PDRblock.C:28
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
const Foam::objectRegistry & setRegistry(const Foam::Time &runTime, const Foam::dictionary &dict)
Definition: writeObjects.C:57
Registry of regIOobjects.
virtual bool execute()
Do nothing.
Definition: writeObjects.C:122
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:93
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225