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-2019 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 // * * * * * * * * * * * Protected Static Data Members * * * * * * * * * * * //
37 
38 const Foam::wordList Foam::codedFunctionObject::codeKeys_ =
39  {
40  "codeData",
41  "codeEnd",
42  "codeExecute",
43  "codeInclude",
44  "codeRead",
45  "codeWrite",
46  "localCode"
47  };
48 
49 
50 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54  defineTypeNameAndDebug(codedFunctionObject, 0);
55 
57  (
58  functionObject,
59  codedFunctionObject,
60  dictionary
61  );
62 }
63 
64 
65 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
66 
68 (
69  dynamicCode& dynCode,
70  const dynamicCodeContext& context
71 ) const
72 {
73  dynCode.setFilterVariable("typeName", name_);
74 
75  // Compile filtered C template
76  dynCode.addCompileFile("functionObjectTemplate.C");
77 
78  // Copy filtered H template
79  dynCode.addCopyFile("functionObjectTemplate.H");
80 
81  // Debugging: make BC verbose
82  // dynCode.setFilterVariable("verbose", "true");
83  // Info<<"compile " << name_ << " sha1: "
84  // << context.sha1() << endl;
85 
86  // Define Make/options
87  dynCode.setMakeOptions
88  (
89  "EXE_INC = -g \\\n"
90  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
91  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
92  + context.options()
93  + "\n\nLIB_LIBS = \\\n"
94  + " -lOpenFOAM \\\n"
95  + " -lfiniteVolume \\\n"
96  + " -lmeshTools \\\n"
97  + context.libs()
98  );
99 }
100 
101 
103 {
104  return const_cast<Time&>(time_).libs();
105 }
106 
107 
109 {
110  return "functionObject " + name();
111 }
112 
113 
115 {
116  redirectFunctionObjectPtr_.clear();
117 }
118 
119 
121 {
122  return dict_;
123 }
124 
125 
127 {
128  return codeKeys_;
129 }
130 
131 
132 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
133 
135 (
136  const word& name,
137  const Time& time,
138  const dictionary& dict
139 )
140 :
141  functionObject(name),
142  codedBase(),
143  time_(time),
144  dict_(dict)
145 {
146  read(dict_);
147 }
148 
149 
150 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
151 
153 {}
154 
155 
156 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
157 
159 {
160  if (!redirectFunctionObjectPtr_.valid())
161  {
162  dictionary constructDict(dict_);
163  constructDict.set("type", name_);
164 
165  redirectFunctionObjectPtr_ = functionObject::New
166  (
167  name_,
168  time_,
169  constructDict
170  );
171  }
172  return redirectFunctionObjectPtr_();
173 }
174 
175 
177 {
178  updateLibrary(name_);
179  return redirectFunctionObject().execute();
180 }
181 
182 
184 {
185  updateLibrary(name_);
186  return redirectFunctionObject().write();
187 }
188 
189 
191 {
192  updateLibrary(name_);
193  return redirectFunctionObject().end();
194 }
195 
196 
198 {
199  // The name keyword is "name". "redirectType" is also maintained here
200  // for backwards compatibility, but "name" is taken in preference and
201  // is printed in the error message if neither keyword is present.
202  name_ = word::null;
203  name_ = dict.lookupOrDefault("redirectType", name_);
204  name_ = dict.lookupOrDefault("name", name_);
205  if (name_ == word::null)
206  {
207  dict.lookup("name"); // <-- generate error message with "name" in it
208  }
209 
210  updateLibrary(name_);
211  return redirectFunctionObject().read(dict);
212 }
213 
214 
215 // ************************************************************************* //
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
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:158
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:388
codedFunctionObject(const word &name, const Time &time, const dictionary &dict)
Construct from Time and dictionary.
virtual void clearRedirect() const
Clear any redirected objects.
virtual const wordList & codeKeys() const
Get the keywords associated with source code.
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:356
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
virtual const dictionary & codeDict() const
Get the dictionary to initialize the codeContext.
Macros for easy insertion into run-time selection tables.
virtual dlLibraryTable & libs() const
Get the loaded dynamic libraries.
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
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.
static const word null
An empty word.
Definition: word.H:77
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:53
A table of dynamically loaded libraries.
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual ~codedFunctionObject()
Destructor.
const string & libs() const
Return the code-libs.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Encapsulation of dynamic code dictionaries.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:379
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
const string & options() const
Return the code-options.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:362
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:948
virtual bool write()
Called at each ++ or += of the time-loop.
virtual string description() const
Return a description (type + name) for the output.
A class for handling character strings derived from std::string.
Definition: string.H:74
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583