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 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
52 
53 Foam::wordList Foam::codedFunctionObject::codeKeys() const
54 {
55  return
56  {
57  "codeData",
58  "codeEnd",
59  "codeExecute",
60  "codeInclude",
61  "codeRead",
62  "codeFields",
63  "codeWrite",
64  "localCode"
65  };
66 }
67 
68 
69 Foam::wordList Foam::codedFunctionObject::codeDictVars() const
70 {
71  return
72  {
73  word::null,
74  word::null,
75  word::null,
76  word::null,
77  "dict",
78  word::null,
79  word::null,
80  word::null,
81  };
82 }
83 
84 
85 void Foam::codedFunctionObject::prepare
86 (
87  dynamicCode& dynCode,
88  const dynamicCodeContext& context
89 ) const
90 {
91  dynCode.setFilterVariable("typeName", codeName());
92 
93  // Compile filtered C template
94  dynCode.addCompileFile(codeTemplateC("codedFunctionObject"));
95 
96  // Copy filtered H template
97  dynCode.addCopyFile(codeTemplateH("codedFunctionObject"));
98 
99  // Make verbose if debugging
100  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
101 
102  if (debug)
103  {
104  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
105  }
106 
107  // Define Make/options
108  dynCode.setMakeOptions
109  (
110  "EXE_INC = -g \\\n"
111  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
112  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
113  + context.options()
114  + "\n\nLIB_LIBS = \\\n"
115  + " -lOpenFOAM \\\n"
116  + " -lfiniteVolume \\\n"
117  + " -lmeshTools \\\n"
118  + context.libs()
119  );
120 }
121 
122 
123 void Foam::codedFunctionObject::clearRedirect() const
124 {
125  redirectFunctionObjectPtr_.clear();
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
130 
132 (
133  const word& name,
134  const Time& time,
135  const dictionary& dict
136 )
137 :
138  functionObject(name, time),
140 {
141  read(dict);
142 }
143 
144 
145 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
146 
148 {}
149 
150 
151 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152 
153 Foam::functionObject& Foam::codedFunctionObject::redirectFunctionObject() const
154 {
155  if (!redirectFunctionObjectPtr_.valid())
156  {
157  dictionary constructDict(codeDict());
158  constructDict.set("type", codeName());
159 
160  redirectFunctionObjectPtr_ = functionObject::New
161  (
162  codeName(),
163  time_,
164  constructDict
165  );
166  }
167  return redirectFunctionObjectPtr_();
168 }
169 
170 
172 {
173  updateLibrary();
174  return redirectFunctionObject().fields();
175 }
176 
177 
179 {
180  updateLibrary();
181  return redirectFunctionObject().execute();
182 }
183 
184 
186 {
187  updateLibrary();
188  return redirectFunctionObject().write();
189 }
190 
191 
193 {
194  updateLibrary();
195  return redirectFunctionObject().end();
196 }
197 
198 
200 {
201  updateLibrary();
202  return functionObject::read(dict) && redirectFunctionObject().read(dict);
203 }
204 
205 
206 // ************************************************************************* //
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:54
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 keyword definitions, which are a keyword followed by any number of values (e....
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
static const word null
An empty word.
Definition: word.H:77
Namespace for OpenFOAM.
addToRunTimeSelectionTable(polyPatch, mergedCyclicPolyPatch, word)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
defineTypeNameAndDebug(combustionModel, 0)
dictionary dict