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  "codeWrite",
63  "localCode"
64  };
65 }
66 
67 
68 void Foam::codedFunctionObject::prepare
69 (
70  dynamicCode& dynCode,
71  const dynamicCodeContext& context
72 ) const
73 {
74  dynCode.setFilterVariable("typeName", codeName());
75 
76  // Compile filtered C template
77  dynCode.addCompileFile(codeTemplateC("codedFunctionObject"));
78 
79  // Copy filtered H template
80  dynCode.addCopyFile(codeTemplateH("codedFunctionObject"));
81 
82  // Debugging: make verbose
83  if (debug)
84  {
85  dynCode.setFilterVariable("verbose", "true");
86  Info<<"compile " << codeName() << " sha1: "
87  << context.sha1() << endl;
88  }
89 
90  // Define Make/options
91  dynCode.setMakeOptions
92  (
93  "EXE_INC = -g \\\n"
94  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
95  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
96  + context.options()
97  + "\n\nLIB_LIBS = \\\n"
98  + " -lOpenFOAM \\\n"
99  + " -lfiniteVolume \\\n"
100  + " -lmeshTools \\\n"
101  + context.libs()
102  );
103 }
104 
105 
106 void Foam::codedFunctionObject::clearRedirect() const
107 {
108  redirectFunctionObjectPtr_.clear();
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
115 (
116  const word& name,
117  const Time& time,
118  const dictionary& dict
119 )
120 :
121  functionObject(name),
122  codedBase(name, dict),
123  time_(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().execute();
159 }
160 
161 
163 {
164  updateLibrary();
165  return redirectFunctionObject().write();
166 }
167 
168 
170 {
171  updateLibrary();
172  return redirectFunctionObject().end();
173 }
174 
175 
177 {
178  updateLibrary();
179  return redirectFunctionObject().read(dict);
180 }
181 
182 
183 // ************************************************************************* //
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
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
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:68
Macros for easy insertion into run-time selection tables.
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:1271
messageStream Info
virtual bool write()
Called at each ++ or += of the time-loop.
Namespace for OpenFOAM.