functionObject.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) 2017-2021 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 "functionObject.H"
30 #include "dictionary.H"
31 #include "dlLibraryTable.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineDebugSwitchWithName(functionObject, "functionObject", 0);
39  defineRunTimeSelectionTable(functionObject, dictionary);
40 }
41 
43 
45 
47 
48 
49 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
50 
52 {
53  if (useNamePrefix_)
54  {
55  return IOobject::scopedName(name_, name);
56  }
57 
58  return name;
59 }
60 
61 
62 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
63 
65 (
66  const word& name,
67  const bool withNamePrefix
68 )
69 :
70  name_(name),
71  useNamePrefix_(withNamePrefix),
72  log(postProcess)
73 {}
74 
75 
76 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
77 
79 (
80  const word& name,
81  const Time& runTime,
82  const dictionary& dict
83 )
84 {
85  const word functionType(dict.get<word>("type"));
86 
87  DebugInfo
88  << "Selecting function " << functionType << endl;
89 
90 
91  // Load any additional libraries
92  {
93  const auto finder =
94  dict.csearchCompat("libs", {{"functionObjectLibs", 1612}});
95 
96  if (finder.good())
97  {
98  runTime.libs().open
99  (
100  dict,
101  finder.ref().keyword(),
102  dictionaryConstructorTablePtr_
103  );
104  }
105  }
106 
107  // This is the simplified version without compatibility messages
108  // runTime.libs().open
109  // (
110  // dict,
111  // "libs",
112  // dictionaryConstructorTablePtr_
113  // );
114 
115  if (!dictionaryConstructorTablePtr_)
116  {
118  << "Cannot load function type " << functionType << nl << nl
119  << "Table of functionObjects is empty" << endl
120  << exit(FatalError);
121  }
122 
123  auto* ctorPtr = dictionaryConstructorTable(functionType);
124 
125  if (!ctorPtr)
126  {
127  // FatalError (not FatalIOError) to ensure it can be caught
128  // as an exception and ignored
130  (
131  "function",
132  functionType,
133  *dictionaryConstructorTablePtr_
134  ) << exit(FatalError);
135  }
137  return autoPtr<functionObject>(ctorPtr(name, runTime, dict));
138 }
139 
140 
141 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
144 {
145  return name_;
146 }
147 
150 {
151  return useNamePrefix_;
152 }
153 
154 
156 {
157  bool old(useNamePrefix_);
158  useNamePrefix_ = on;
159  return old;
160 }
161 
162 
164 {
165 // OR
166 // useNamePrefix_ = Switch("useNamePrefix", dict, defaultUseNamePrefix);
167 
168  useNamePrefix_ =
170  (
171  "useNamePrefix",
172  defaultUseNamePrefix,
174  );
175 
176 
177  if (!postProcess)
178  {
179  log = dict.getOrDefault("log", true);
180  }
181 
182  return true;
183 }
184 
186 bool Foam::functionObject::execute(const label)
187 {
188  return true;
189 }
190 
193 {
194  return true;
195 }
196 
199 {
200  return false;
201 }
202 
205 {
206  return false;
207 }
208 
209 
211 {}
212 
213 
215 {}
216 
217 
218 // * * * * * * * * * * * * unavailableFunctionObject * * * * * * * * * * * * //
219 
221 (
222  const word& name
223 )
224 :
226 {}
227 
228 
230 (
231  std::string message
232 ) const
233 {
234  FatalError
235  << "####" << nl
236  << " " << type() << " not available" << nl
237  << "####" << nl;
238 
239  if (message.size())
240  {
241  FatalError
242  << message.c_str() << nl;
243  }
244 
245  FatalError
246  << exit(FatalError);
247 }
248 
251 {
252  return true;
253 }
254 
255 
257 {
258  return true;
259 }
260 
261 
262 // ************************************************************************* //
dictionary dict
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: Time.H:730
dimensionedScalar log(const dimensionedScalar &ds)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
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
defineDebugSwitchWithName(pointMVCWeight, "pointMVCWeight", 0)
static bool postProcess
Global post-processing mode switch.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual bool execute()=0
Called at each ++ or += of the time-loop.
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
Abstract base-class for Time/database function objects.
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:605
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:157
const word & name() const noexcept
Return the name of this functionObject.
virtual bool end()
Called when Time::run() determines that the time-loop exits.
static word scopedName(const std::string &scope, const word &name)
Create scope:name or scope_name string.
Definition: IOobjectI.H:40
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
bool useNamePrefix() const noexcept
Return the flag for adding a scoping name prefix.
A class for handling words, derived from Foam::string.
Definition: word.H:63
const_searcher csearchCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option matchOpt=keyType::REGEX) const
Search dictionary for given keyword and any compatibility names.
virtual const word & type() const =0
Runtime type information.
unavailableFunctionObject(const word &name)
Construct with name.
virtual bool filesModified() const
Did any file get changed during execution?
String literal.
Definition: keyType.H:82
#define DebugInfo
Report an information message using Foam::Info.
const direction noexcept
Definition: Scalar.H:258
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static bool defaultUseNamePrefix
Global default for useNamePrefix.
static word outputPrefix
Directory prefix.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
void carp(std::string message="") const
Report it is unavailable, emitting a FatalError for try/catch in the caller.
bool open(bool verbose=true)
Open named, but unopened libraries. These names will normally have been added with the append() metho...
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
functionObject(const word &name, const bool withNamePrefix=defaultUseNamePrefix)
Construct from components.
word scopedName(const word &name) const
Return a scoped (prefixed) name.
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Namespace for OpenFOAM.