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-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 "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 void Foam::codedFunctionObject::prepare
70 (
71  dynamicCode& dynCode,
72  const dynamicCodeContext& context
73 ) const
74 {
75  dynCode.setFilterVariable("typeName", codeName());
76 
77  // Compile filtered C template
78  dynCode.addCompileFile(codeTemplateC("codedFunctionObject"));
79 
80  // Copy filtered H template
81  dynCode.addCopyFile(codeTemplateH("codedFunctionObject"));
82 
83  // Debugging: make verbose
84  if (debug)
85  {
86  dynCode.setFilterVariable("verbose", "true");
87  Info<<"compile " << codeName() << " sha1: "
88  << context.sha1() << endl;
89  }
90 
91  // Define Make/options
92  dynCode.setMakeOptions
93  (
94  "EXE_INC = -g \\\n"
95  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
96  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
97  + context.options()
98  + "\n\nLIB_LIBS = \\\n"
99  + " -lOpenFOAM \\\n"
100  + " -lfiniteVolume \\\n"
101  + " -lmeshTools \\\n"
102  + context.libs()
103  );
104 }
105 
106 
107 void Foam::codedFunctionObject::clearRedirect() const
108 {
109  redirectFunctionObjectPtr_.clear();
110 }
111 
112 
113 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
114 
116 (
117  const word& name,
118  const Time& time,
119  const dictionary& dict
120 )
121 :
122  functionObject(name, time),
124 {
125  read(dict);
126 }
127 
128 
129 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
130 
132 {}
133 
134 
135 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136 
137 Foam::functionObject& Foam::codedFunctionObject::redirectFunctionObject() const
138 {
139  if (!redirectFunctionObjectPtr_.valid())
140  {
141  dictionary constructDict(codeDict());
142  constructDict.set("type", codeName());
143 
144  redirectFunctionObjectPtr_ = functionObject::New
145  (
146  codeName(),
147  time_,
148  constructDict
149  );
150  }
151  return redirectFunctionObjectPtr_();
152 }
153 
154 
156 {
157  updateLibrary();
158  return redirectFunctionObject().fields();
159 }
160 
161 
163 {
164  updateLibrary();
165  return redirectFunctionObject().execute();
166 }
167 
168 
170 {
171  updateLibrary();
172  return redirectFunctionObject().write();
173 }
174 
175 
177 {
178  updateLibrary();
179  return redirectFunctionObject().end();
180 }
181 
182 
184 {
185  updateLibrary();
186  return functionObject::read(dict) && redirectFunctionObject().read(dict);
187 }
188 
189 
190 // ************************************************************************* //
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:160
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
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
messageStream Info
defineTypeNameAndDebug(combustionModel, 0)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
dictionary dict