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::AUTO_WRITE, "autoWrite" },
59  { writeOption::NO_WRITE, "noWrite" },
60  { writeOption::ANY_WRITE, "anyWrite" },
61 });
62 
63 
64 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
65 
66 Foam::functionObjects::writeObjects::writeObjects
67 (
68  const word& name,
69  const Time& runTime,
70  const dictionary& dict
71 )
72 :
74  obr_
75  (
76  runTime.lookupObject<objectRegistry>
77  (
78  dict.getOrDefault("region", polyMesh::defaultRegion)
79  )
80  ),
81  writeOption_(ANY_WRITE),
82  objectNames_()
83 {
84  read(dict);
85 }
86 
87 
88 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
89 
91 {
93 
94  if (dict.found("field"))
95  {
96  objectNames_.resize(1);
97  dict.readEntry("field", objectNames_.first());
98  }
99  else if (dict.found("fields"))
100  {
101  dict.readEntry("fields", objectNames_);
102  }
103  else
104  {
105  dict.readEntry("objects", objectNames_);
106  }
107 
108  writeOption_ = writeOptionNames_.getOrDefault
109  (
110  "writeOption",
111  dict,
112  writeOption::ANY_WRITE
113  );
114 
115  return true;
116 }
117 
118 
120 {
121  return true;
122 }
123 
124 
126 {
127  Log << type() << " " << name() << " write:" << nl;
128 
129  if (!obr_.time().writeTime())
130  {
131  obr_.time().writeTimeDict();
132  }
133 
134  // Get selection
135  const wordList selectedNames(obr_.sortedNames<regIOobject>(objectNames_));
136 
137  // Warning if anything was missed
138  bitSet missed(objectNames_.size());
139 
140  label index = 0;
141  for (const wordRe& select : objectNames_)
142  {
143  if (!ListOps::found(selectedNames, select))
144  {
145  missed.set(index);
146  }
147  ++index;
148  }
149 
150  if (missed.any())
151  {
153  << "No corresponding selection for "
154  << flatOutput(subset(missed, objectNames_)) << nl
155  << "Available objects in database:"
156  << nl << obr_.sortedToc()
157  << endl;
158  }
159 
160  for (const word& objName : selectedNames)
161  {
162  regIOobject& obj = obr_.lookupObjectRef<regIOobject>(objName);
163 
164  switch (writeOption_)
165  {
166  case AUTO_WRITE:
167  {
168  if (obj.writeOpt() != IOobject::AUTO_WRITE)
169  {
170  continue;
171  }
172 
173  break;
174  }
175  case NO_WRITE:
176  {
177  if (obj.writeOpt() != IOobject::NO_WRITE)
178  {
179  continue;
180  }
181 
182  break;
183  }
184  case ANY_WRITE:
185  {
186  break;
187  }
188  default:
189  {
191  << "Unknown writeOption "
192  << writeOptionNames_[writeOption_]
193  << ". Valid writeOption types are "
194  << writeOptionNames_
195  << exit(FatalError);
196 
197  continue;
198  break;
199  }
200  }
201 
202  if
203  (
205  && obr_.time().writeTime()
206  )
207  {
208  Log << " automatically written object " << obj.name() << endl;
209  }
210  else
211  {
212  Log << " writing object " << obj.name() << endl;
213 
214  obj.write();
215  }
216  }
217 
218  return true;
219 }
220 
221 
222 // ************************************************************************* //
Foam::IOobject::NO_WRITE
Definition: IOobject.H:195
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObjects::writeObjects::write
virtual bool write()
Write the registered objects.
Definition: writeObjects.C:125
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::IOobject::AUTO_WRITE
Definition: IOobject.H:194
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::bitSet
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:63
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::subset
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
Foam::bitSet::set
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition: bitSetI.H:574
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::functionObjects::writeObjects::writeOptionNames_
static const Enum< writeOption > writeOptionNames_
Definition: writeObjects.H:156
polyMesh.H
Foam::IOobject::writeOpt
writeOption writeOpt() const noexcept
The write option.
Definition: IOobjectI.H:179
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
Foam::functionObjects::writeObjects::writeOption
writeOption
Re-enumeration defining the write options, based on the original.
Definition: writeObjects.H:149
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
writeObjects.H
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:132
Foam::Enum::getOrDefault
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:179
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::functionObjects::writeObjects::execute
virtual bool execute()
Do nothing.
Definition: writeObjects.C:119
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObjects::writeObjects::read
virtual bool read(const dictionary &)
Read the writeObjects data.
Definition: writeObjects.C:90
Foam::List< word >
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::ListOps::found
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
True if there is a value in the list that satisfies the predicate.
Definition: ListOpsTemplates.C:1156
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
ListOps.H
Various functions to operate on Lists.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328