codedFunctionObject.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) 2011-2024 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 "codedFunctionObject.H"
27 #include "volFields.H"
28 #include "dictionary.H"
29 #include "Time.H"
30 #include "SHA1Digest.H"
31 #include "dynamicCode.H"
32 #include "dynamicCodeContext.H"
33 #include "stringOps.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
41 
43  (
47  );
48 }
49 
50 
51 const Foam::wordList Foam::codedFunctionObject::codeKeys
52 {
53  "codeData",
54  "codeEnd",
55  "codeExecute",
56  "codeInclude",
57  "codeRead",
58  "codeFields",
59  "codeWrite",
60  "localCode"
61 };
62 
63 const Foam::wordList Foam::codedFunctionObject::codeDictVars
64 {
65  word::null,
66  word::null,
67  word::null,
68  word::null,
69  "dict",
70  word::null,
71  word::null,
72  word::null,
73 };
74 
75 
76 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
77 
78 void Foam::codedFunctionObject::prepare
79 (
80  dynamicCode& dynCode,
81  const dynamicCodeContext& context
82 ) const
83 {
84  dynCode.setFilterVariable("typeName", codeName());
85 
86  // Compile filtered C template
87  dynCode.addCompileFile(codeTemplateC("codedFunctionObject"));
88 
89  // Copy filtered H template
90  dynCode.addCopyFile(codeTemplateH("codedFunctionObject"));
91 
92  // Make verbose if debugging
93  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
94 
95  if (debug)
96  {
97  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
98  }
99 
100  // Define Make/options
101  dynCode.setMakeOptions
102  (
103  "EXE_INC = -g \\\n"
104  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
105  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
106  + context.options()
107  + "\n\nLIB_LIBS = \\\n"
108  + " -lOpenFOAM \\\n"
109  + " -lfiniteVolume \\\n"
110  + " -lmeshTools \\\n"
111  + context.libs()
112  );
113 }
114 
115 
116 void Foam::codedFunctionObject::updateLibrary(const dictionary& dict)
117 {
118  redirectFunctionObjectPtr_.clear();
119 
121 
122  dictionary constructDict(dict);
123  constructDict.set("type", codeName());
124 
125  redirectFunctionObjectPtr_ = functionObject::New
126  (
127  codeName(),
128  time_,
129  constructDict
130  );
131 }
132 
133 
134 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
135 
137 (
138  const word& name,
139  const Time& time,
140  const dictionary& dict
141 )
142 :
143  functionObject(name, time, dict),
144  codedBase(name, dict, codeKeys, codeDictVars)
145 {
146  updateLibrary(dict);
147 }
148 
149 
150 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
151 
153 {}
154 
155 
156 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157 
158 Foam::functionObject& Foam::codedFunctionObject::redirectFunctionObject() const
159 {
160  if (!redirectFunctionObjectPtr_.valid())
161  {
163  << "redirectFunctionObject not allocated" << exit(FatalError);
164  }
165 
166  return redirectFunctionObjectPtr_();
167 }
168 
169 
171 {
172  return redirectFunctionObject().fields();
173 }
174 
175 
177 {
178  return redirectFunctionObject().execute();
179 }
180 
181 
183 {
184  return redirectFunctionObject().write();
185 }
186 
187 
189 {
190  return redirectFunctionObject().end();
191 }
192 
193 
195 {
197  {
199  updateLibrary(dict);
200  return true;
201  }
202  else
203  {
204  return false;
205  }
206 }
207 
208 
209 // ************************************************************************* //
Macros for easy insertion into run-time selection tables.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:52
word codeTemplateC(const word &baseTypeName) const
Definition: codedBase.C:386
void read(const dictionary &dict)
Read the dictionary and update the code.
Definition: codedBase.C:455
const word & codeName() const
Name of the dynamically generated CodedType.
Definition: codedBase.C:374
word codeTemplateH(const word &baseTypeName) const
Definition: codedBase.C:392
bool updateLibrary(const dictionary &dict) const
Update library from given updated dictionary as required.
Definition: codedBase.C:398
Provides a general interface to enable dynamic code compilation.
virtual ~codedFunctionObject()
Destructor.
virtual wordList fields() const
Return the list of fields required.
codedFunctionObject(const word &name, const Time &time, const dictionary &dict)
Construct from Time and dictionary.
virtual bool execute()
Called at each ++ or += of the time-loop.
virtual bool write()
Called at each ++ or += of the time-loop.
virtual bool end()
Called when Time::run() determines that the time-loop exits.
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
virtual bool read(const dictionary &)
Read and set the functionObject if its data have changed.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
defineTypeNameAndDebug(combustionModel, 0)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
error FatalError
dictionary dict