writeObjectsBase.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2016-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "writeObjectsBase.H"
27 #include "Time.H"
28 #include "dictionary.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
36 
38 (
39  const wordRe& name
40 )
41 {
44 }
45 
46 
48 (
49  const wordReList& names
50 )
51 {
52  writeObjectNames_.clear();
53  writeObjectNames_.append(names);
54 }
55 
56 
58 {
59  DynamicList<word> allNames(writeObr_.toc().size());
60  forAll(writeObjectNames_, i)
61  {
62  if (regExp_)
63  {
64  wordList names(writeObr_.names<regIOobject>(writeObjectNames_[i]));
65 
66  if (names.size())
67  {
68  allNames.append(names);
69  }
70  else
71  {
72  Warning
73  << "writeObjects: object " << writeObjectNames_[i]
74  << " not found in database. Available objects:"
75  << nl << writeObr_.sortedToc() << endl;
76  }
77  }
78  else
79  {
80  const word name(writeObjectNames_[i]);
81 
82  if (writeObr_.foundObject<regIOobject>(name))
83  {
84  allNames.append(name);
85  }
86  else
87  {
88  Warning
89  << "writeObjects: object " << name
90  << " not found in database. Available objects:"
91  << nl << writeObr_.sortedToc() << endl;
92  }
93  }
94  }
95 
96  return allNames;
97 }
98 
99 
101 (
102  const regIOobject& obj
103 )
104 {
105  if (log_) Info << " writing object " << obj.name() << endl;
106 
107  obj.write();
108 }
109 
110 
111 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
112 
114 (
115  const objectRegistry& obr,
116  const Switch& log
117 )
118 :
119  writeObr_(obr),
120  log_(log)
121 {}
122 
123 
124 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
125 
127 {}
128 
129 
130 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131 
132 const Foam::wordReList&
134 {
135  return writeObjectNames_;
136 }
137 
138 
140 {
141  regExp_ = dict.lookupOrDefault<Switch>("regExp", true);
142 
143  if (regExp_)
144  {
145  dict.lookup("objects") >> writeObjectNames_;
146  }
147  else
148  {
149  const wordList objectNames(dict.lookup("objects"));
150  writeObjectNames_.setSize(objectNames.size());
151  forAll(objectNames, i)
152  {
153  writeObjectNames_[i] = objectNames[i];
154  }
155  }
156 
157  return true;
158 }
159 
160 
162 {
163  wordList names(objectNames());
164 
165  if (!names.empty())
166  {
167  if (!writeObr_.time().writeTime())
168  {
169  writeObr_.time().writeTimeDict();
170  }
171 
172  forAll(names, i)
173  {
174  writeObject(writeObr_.lookupObject<regIOobject>(names[i]));
175  }
176  }
177 
178  return true;
179 }
180 
181 
182 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
Definition: DynamicListI.H:296
const word & name() const
Return name.
Definition: IOobject.H:310
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:125
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:325
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
Calculates the natural logarithm of the specified scalar field.
Definition: log.H:106
wordReList writeObjectNames_
Object names requested by the user to be written.
writeObjectsBase(const objectRegistry &obr, const Switch &logRef=logFalse)
Construct from objectRegistry and inheriting function object.
virtual void writeObject(const regIOobject &obj)
Write the requested registered IO object.
void resetWriteObjectNames(const wordReList &names)
Reset the list of object names to be written.
const wordReList & writeObjectNames() const
Return const access to the object names requested to be written.
void resetWriteObjectName(const wordRe &name)
Reset the list of object names to be written to a single regular.
virtual wordList objectNames()
Get the list of field names to be written.
static const Switch logFalse
False switch to which log_ defaults.
virtual bool write()
Write function.
virtual bool read(const dictionary &)
Read the list of objects to be written.
Registry of regIOobjects.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
virtual bool write(const bool write=true) const
Write using setting from DB.
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
A class for handling words, derived from string.
Definition: word.H:62
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
messageStream Warning
dictionary dict