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  { writeOption::LOG, "log" },
62 });
63 
65 (
66  const Foam::Time& runTime,
67  const Foam::dictionary& dict
68 )
69 {
70  const Foam::word regionName =
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 
83 Foam::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_()
94 {
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,
126  writeOption::ANY_WRITE
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 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:132
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
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:608
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as found_if.
Definition: ListOps.H:858
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:862
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:406
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
writeOption
Re-enumeration defining the write options, Naming based on the IOobjectOption::writeOption.
Definition: writeObjects.H:178
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.
List< word > wordList
List of word.
Definition: fileName.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_
Names for writeOption.
Definition: writeObjects.H:189
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
const Foam::objectRegistry & setRegistry(const Foam::Time &runTime, const Foam::dictionary &dict)
Definition: writeObjects.C:58
Registry of regIOobjects.
virtual bool execute()
Do nothing.
Definition: writeObjects.C:126
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:94
Namespace for OpenFOAM.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225