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.found())
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  }
136 
137  return autoPtr<functionObject>(ctorPtr(name, runTime, dict));
138 }
139 
140 
141 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
142 
143 const Foam::word& Foam::functionObject::name() const noexcept
144 {
145  return name_;
146 }
147 
148 
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 
185 
187 {
188  return true;
189 }
190 
191 
193 {
194  return true;
195 }
196 
197 
199 {
200  return false;
201 }
202 
203 
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 
249 
251 {
252  return true;
253 }
254 
255 
257 {
258  return true;
259 }
260 
261 
262 // ************************************************************************* //
Foam::functionObject::unavailableFunctionObject::execute
virtual bool execute()
No nothing.
Definition: functionObject.C:250
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObject::execute
virtual bool execute()=0
Called at each ++ or += of the time-loop.
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::defineDebugSwitchWithName
defineDebugSwitchWithName(pointMVCWeight, "pointMVCWeight", 0)
Foam::functionObject::New
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
Definition: functionObject.C:79
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::functionObject::functionObject
functionObject(const word &name, const bool withNamePrefix=defaultUseNamePrefix)
Construct from components.
Definition: functionObject.C:65
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
Foam::functionObject::unavailableFunctionObject::unavailableFunctionObject
unavailableFunctionObject(const word &name)
Construct with name.
Definition: functionObject.C:221
Foam::functionObject::unavailableFunctionObject::write
virtual bool write()
No nothing.
Definition: functionObject.C:256
Foam::functionObject::defaultUseNamePrefix
static bool defaultUseNamePrefix
Global default for useNamePrefix.
Definition: functionObject.H:373
Foam::functionObject::postProcess
static bool postProcess
Global post-processing mode switch.
Definition: functionObject.H:370
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
Foam::functionObject::outputPrefix
static word outputPrefix
Directory prefix.
Definition: functionObject.H:376
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
FatalErrorInLookup
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:457
Foam::Time::libs
dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: Time.H:505
Foam::log
dimensionedScalar log(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:262
Foam::functionObject::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: functionObject.C:198
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObject::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
Definition: functionObject.C:214
Foam::dlLibraryTable::open
bool open(bool verbose=true)
Definition: dlLibraryTable.C:409
Foam::functionObject::unavailableFunctionObject::carp
void carp(std::string message="") const
Definition: functionObject.C:230
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::dictionary::csearchCompat
const_searcher csearchCompat(const word &keyword, std::initializer_list< std::pair< const char *, int >> compat, enum keyType::option=keyType::REGEX) const
Search dictionary for given keyword and any compatibility names.
Definition: dictionaryCompat.C:34
Foam::functionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: functionObject.C:192
Foam::functionObject::scopedName
word scopedName(const word &name) const
Return a scoped (prefixed) name.
Definition: functionObject.C:51
Time.H
Foam::autoPtr< Foam::functionObject >
Foam::functionObject::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: functionObject.C:210
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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
dlLibraryTable.H
dictionary.H
Foam::functionObject::useNamePrefix
bool useNamePrefix() const noexcept
Return the flag for adding a scoping name prefix.
Definition: functionObject.C:149
Foam::functionObject::filesModified
virtual bool filesModified() const
Did any file get changed during execution?
Definition: functionObject.C:204
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::keyType::LITERAL
String literal.
Definition: keyType.H:81
Foam::IOobject::scopedName
static word scopedName(const std::string &scope, const word &name)
Create scope:name or scope_name string.
Definition: IOobjectI.H:47
functionObject.H
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148