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-2021 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 {
40  defineTypeNameAndDebug(codedFunctionObject, 0);
41 
43  (
44  functionObject,
45  codedFunctionObject,
46  dictionary
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),
123  codedBase(name, dict),
124  time_(time)
125 {
126  read(dict);
127 }
128 
129 
130 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
131 
133 {}
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 Foam::functionObject& Foam::codedFunctionObject::redirectFunctionObject() const
139 {
140  if (!redirectFunctionObjectPtr_.valid())
141  {
142  dictionary constructDict(codeDict());
143  constructDict.set("type", codeName());
144 
145  redirectFunctionObjectPtr_ = functionObject::New
146  (
147  codeName(),
148  time_,
149  constructDict
150  );
151  }
152  return redirectFunctionObjectPtr_();
153 }
154 
155 
157 {
158  updateLibrary();
159  return redirectFunctionObject().fields();
160 }
161 
162 
164 {
165  updateLibrary();
166  return redirectFunctionObject().execute();
167 }
168 
169 
171 {
172  updateLibrary();
173  return redirectFunctionObject().write();
174 }
175 
176 
178 {
179  updateLibrary();
180  return redirectFunctionObject().end();
181 }
182 
183 
185 {
186  updateLibrary();
187  return redirectFunctionObject().read(dict);
188 }
189 
190 
191 // ************************************************************************* //
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:156
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
codedFunctionObject(const word &name, const Time &time, const dictionary &dict)
Construct from Time and dictionary.
Abstract base-class for Time/database functionObjects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
virtual wordList fields() const
Return the list of fields required.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:53
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
virtual ~codedFunctionObject()
Destructor.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
virtual bool execute()
Called at each ++ or += of the time-loop.
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:1291
messageStream Info
virtual bool write()
Called at each ++ or += of the time-loop.
Namespace for OpenFOAM.