codedFunctionObject.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
53 (
54  dynamicCode& dynCode,
55  const dynamicCodeContext& context
56 ) const
57 {
58  // Set additional rewrite rules
59  dynCode.setFilterVariable("typeName", redirectType_);
60  dynCode.setFilterVariable("codeRead", codeRead_);
61  dynCode.setFilterVariable("codeExecute", codeExecute_);
62  dynCode.setFilterVariable("codeEnd", codeEnd_);
63  dynCode.setFilterVariable("codeData", codeData_);
64  dynCode.setFilterVariable("codeTimeSet", codeTimeSet_);
65  //dynCode.setFilterVariable("codeWrite", codeWrite_);
66 
67  // compile filtered C template
68  dynCode.addCompileFile("functionObjectTemplate.C");
69  dynCode.addCompileFile("FilterFunctionObjectTemplate.C");
70 
71  // copy filtered H template
72  dynCode.addCopyFile("FilterFunctionObjectTemplate.H");
73  dynCode.addCopyFile("functionObjectTemplate.H");
74  dynCode.addCopyFile("IOfunctionObjectTemplate.H");
75 
76  // debugging: make BC verbose
77  // dynCode.setFilterVariable("verbose", "true");
78  // Info<<"compile " << redirectType_ << " sha1: "
79  // << context.sha1() << endl;
80 
81  // define Make/options
82  dynCode.setMakeOptions
83  (
84  "EXE_INC = -g \\\n"
85  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
86  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
87  + context.options()
88  + "\n\nLIB_LIBS = \\\n"
89  + " -lOpenFOAM \\\n"
90  + " -lfiniteVolume \\\n"
91  + " -lmeshTools \\\n"
92  + context.libs()
93  );
94 }
95 
96 
98 {
99  return const_cast<Time&>(time_).libs();
100 }
101 
102 
104 {
105  return "functionObject " + name();
106 }
107 
108 
110 {
111  redirectFunctionObjectPtr_.clear();
112 }
113 
114 
116 {
117  return dict_;
118 }
119 
120 
121 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
122 
124 (
125  const word& name,
126  const Time& time,
127  const dictionary& dict,
128  bool readNow
129 )
130 :
132  codedBase(),
133  time_(time),
134  dict_(dict)
135 {
136  if (readNow)
137  {
138  read(dict_);
139  }
140 }
141 
142 
143 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
144 
146 {}
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
153 {
154  if (!redirectFunctionObjectPtr_.valid())
155  {
156  dictionary constructDict(dict_);
157  constructDict.set("type", redirectType_);
158 
159  redirectFunctionObjectPtr_ = functionObject::New
160  (
161  redirectType_,
162  time_,
163  constructDict
164  );
165  }
166  return redirectFunctionObjectPtr_();
167 }
168 
169 
171 {
172  updateLibrary(redirectType_);
173  return redirectFunctionObject().start();
174 }
175 
176 
177 bool Foam::codedFunctionObject::execute(const bool forceWrite)
178 {
179  updateLibrary(redirectType_);
180  return redirectFunctionObject().execute(forceWrite);
181 }
182 
183 
185 {
186  updateLibrary(redirectType_);
187  return redirectFunctionObject().end();
188 }
189 
190 
192 {
193  updateLibrary(redirectType_);
194  return redirectFunctionObject().timeSet();
195 }
196 
197 
199 {
200  dict.lookup("redirectType") >> redirectType_;
201 
202  const entry* dataPtr = dict.lookupEntryPtr
203  (
204  "codeData",
205  false,
206  false
207  );
208  if (dataPtr)
209  {
210  codeData_ = stringOps::trim(dataPtr->stream());
211  stringOps::inplaceExpand(codeData_, dict);
213  (
214  codeData_,
215  dataPtr->startLineNumber(),
216  dict.name()
217  );
218  }
219 
220  const entry* readPtr = dict.lookupEntryPtr
221  (
222  "codeRead",
223  false,
224  false
225  );
226  if (readPtr)
227  {
228  codeRead_ = stringOps::trim(readPtr->stream());
229  stringOps::inplaceExpand(codeRead_, dict);
231  (
232  codeRead_,
233  readPtr->startLineNumber(),
234  dict.name()
235  );
236  }
237 
238  const entry* execPtr = dict.lookupEntryPtr
239  (
240  "codeExecute",
241  false,
242  false
243  );
244  if (execPtr)
245  {
246  codeExecute_ = stringOps::trim(execPtr->stream());
247  stringOps::inplaceExpand(codeExecute_, dict);
249  (
250  codeExecute_,
251  execPtr->startLineNumber(),
252  dict.name()
253  );
254  }
255 
256  const entry* endPtr = dict.lookupEntryPtr
257  (
258  "codeEnd",
259  false,
260  false
261  );
262  if (endPtr)
263  {
264  codeEnd_ = stringOps::trim(endPtr->stream());
265  stringOps::inplaceExpand(codeEnd_, dict);
267  (
268  codeEnd_,
269  endPtr->startLineNumber(),
270  dict.name()
271  );
272  }
273 
274  const entry* timeSetPtr = dict.lookupEntryPtr
275  (
276  "codeTimeSet",
277  false,
278  false
279  );
280  if (timeSetPtr)
281  {
282  codeTimeSet_ = stringOps::trim(timeSetPtr->stream());
283  stringOps::inplaceExpand(codeTimeSet_, dict);
285  (
286  codeTimeSet_,
287  timeSetPtr->startLineNumber(),
288  dict.name()
289  );
290  }
291 
292  updateLibrary(redirectType_);
293  return redirectFunctionObject().read(dict);
294 }
295 
296 
298 {}
299 
300 
302 {}
303 
304 
305 // ************************************************************************* //
Foam::codedFunctionObject::timeSet
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
Definition: codedFunctionObject.C:191
Foam::dynamicCodeContext::libs
const string & libs() const
Return the code-libs.
Definition: dynamicCodeContext.H:102
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
volFields.H
Foam::codedFunctionObject::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: codedFunctionObject.C:53
Foam::codedFunctionObject::description
virtual string description() const
Definition: codedFunctionObject.C:103
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:49
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
Foam::codedFunctionObject::codeDict
virtual const dictionary & codeDict() const
Definition: codedFunctionObject.C:115
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::codedFunctionObject::libs
virtual dlLibraryTable & libs() const
Get the loaded dynamic libraries.
Definition: codedFunctionObject.C:97
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:49
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:53
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::codedFunctionObject::redirectFunctionObject
functionObject & redirectFunctionObject() const
Dynamically compiled functionObject.
Definition: codedFunctionObject.C:152
Foam::codedFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: codedFunctionObject.C:198
codedFunctionObject.H
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::dynamicCodeContext::addLineDirective
static void addLineDirective(string &, const label lineNum, const fileName &name)
Helper: add #line directive.
Definition: dynamicCodeContext.C:128
dynamicCodeContext.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:355
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:58
SHA1Digest.H
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:349
Foam::codedFunctionObject::codedFunctionObject
codedFunctionObject(const codedFunctionObject &)
Disallow default bitwise copy construct.
Foam::codedFunctionObject::start
virtual bool start()
Called at the start of the time-loop.
Definition: codedFunctionObject.C:170
Foam::codedFunctionObject::~codedFunctionObject
virtual ~codedFunctionObject()
Destructor.
Definition: codedFunctionObject.C:145
Foam::functionObject::New
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
Definition: functionObject.C:51
dynamicCode.H
Foam::codedFunctionObject::movePoints
virtual void movePoints(const polyMesh &)
Move points.
Definition: codedFunctionObject.C:301
Foam::codedFunctionObject::clearRedirect
virtual void clearRedirect() const
Definition: codedFunctionObject.C:109
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::entry::startLineNumber
virtual label startLineNumber() const =0
Return line number of first token in dictionary.
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::codedFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: codedFunctionObject.H:101
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::dynamicCodeContext::options
const string & options() const
Return the code-options.
Definition: dynamicCodeContext.H:96
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::dynamicCode::setFilterVariable
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:384
Foam::dictionary::lookupEntryPtr
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:343
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:393
Foam::codedFunctionObject::execute
virtual bool execute(const bool forceWrite)
Called at each ++ or += of the time-loop. forceWrite overrides the.
Definition: codedFunctionObject.C:177
Foam::codedFunctionObject::updateMesh
virtual void updateMesh(const mapPolyMesh &)
Update mesh.
Definition: codedFunctionObject.C:297
dictionary.H
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
stringOps.H
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::stringOps::inplaceExpand
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:86
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::codedFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: codedFunctionObject.C:184
Foam::dictionary::set
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:856
Foam::stringOps::trim
string trim(const string &)
Return string trimmed of leading and trailing whitespace.
Definition: stringOps.C:922