CodedFunction1.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "dynamicCode.H"
29 #include "dynamicCodeContext.H"
30 #include "dictionaryContent.H"
31 
32 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
33 
34 template<class Type>
37 {
38  return this->time().libs();
39 }
40 
41 
42 template<class Type>
45 {
46  return "CodedFunction1 " + redirectName_;
47 }
48 
49 
50 template<class Type>
52 {
53  redirectFunctionPtr_.reset(nullptr);
54 }
55 
56 
57 template<class Type>
58 const Foam::dictionary&
60 {
61  // What else would make sense?
62  return dict_;
63 }
64 
65 
66 template<class Type>
67 const Foam::dictionary&
69 (
70  const dictionary& dict
71 ) const
72 {
73  // Use named subdictionary if present to provide the code.
74  // This allows running with multiple Function1s
75 
76  return
77  (
78  dict.found("code")
79  ? dict
80  : dict.subDict(redirectName_)
81  );
82 }
83 
84 
85 template<class Type>
86 const Foam::dictionary&
88 {
89  return codeDict(dict_);
90 }
91 
92 
93 template<class Type>
95 (
96  dynamicCode& dynCode,
97  const dynamicCodeContext& context
98 ) const
99 {
100  if (context.code().empty())
101  {
103  << "No code section in input dictionary for Function1 "
104  << " name " << redirectName_
105  << exit(FatalIOError);
106  }
107 
108  // Take no chances - typeName must be identical to redirectName_
109  dynCode.setFilterVariable("typeName", redirectName_);
110 
111  // Set TemplateType and FieldType filter variables
112  dynCode.setFieldTemplates<Type>();
113 
114  // Compile filtered C template
115  dynCode.addCompileFile(codeTemplateC);
116 
117  // Copy filtered H template
118  dynCode.addCopyFile(codeTemplateH);
119 
120  #ifdef FULLDEBUG
121  dynCode.setFilterVariable("verbose", "true");
122  DetailInfo
123  <<"compile " << redirectName_ << " sha1: " << context.sha1() << endl;
124  #endif
125 
126  // Define Make/options
127  dynCode.setMakeOptions
128  (
129  "EXE_INC = -g \\\n"
130  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
131  + context.options()
132  + "\n\nLIB_LIBS = \\\n"
133  " -lOpenFOAM \\\n"
134  " -lmeshTools \\\n"
135  + context.libs()
136  );
137 }
138 
139 
140 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
141 
142 template<class Type>
144 (
145  const word& entryName,
146  const dictionary& dict,
147  const objectRegistry* obrPtr
148 )
149 :
150  Function1<Type>(entryName, dict, obrPtr),
151  codedBase(),
152  dict_(dict),
153  redirectName_(dict.getOrDefault<word>("name", entryName))
154 {
155  this->codedBase::setCodeContext(dict_);
156 
157  // No additional code chunks...
158 
159  updateLibrary(redirectName_);
160 }
161 
162 
163 template<class Type>
165 (
166  const CodedFunction1<Type>& rhs
167 )
168 :
169  Function1<Type>(rhs),
170  codedBase(),
171  dict_(rhs.dict_),
172  redirectName_(rhs.redirectName_)
173 {}
174 
175 
176 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
177 
178 template<class Type>
181 {
182  if (!redirectFunctionPtr_)
183  {
184  dictionary constructDict;
185  // Force 'redirectName_' sub-dictionary into existence
186  dictionary& coeffs = constructDict.subDictOrAdd(redirectName_);
187 
188  coeffs = dict_; // Copy input code and coefficients
189  coeffs.remove("name"); // Redundant
190  coeffs.set("type", redirectName_); // Specify our new (redirect) type
191 
192  redirectFunctionPtr_.reset
193  (
195  (
196  redirectName_,
197  constructDict,
198  this->whichDb()
199  )
200  );
201 
202  // Forward copy of codeContext to the code template
203  auto* contentPtr =
204  dynamic_cast<dictionaryContent*>(redirectFunctionPtr_.get());
205 
206  if (contentPtr)
207  {
208  contentPtr->dict(this->codeContext());
209  }
210  else
211  {
213  << redirectName_ << " Did not derive from dictionaryContent"
214  << nl << nl;
215  }
216  }
217  return *redirectFunctionPtr_;
218 }
219 
220 
221 template<class Type>
223 (
224  const scalar x
225 ) const
226 {
227  // Ensure library containing user-defined code is up-to-date
228  updateLibrary(redirectName_);
229 
230  return redirectFunction().value(x);
231 }
232 
233 
234 template<class Type>
236 (
237  Ostream& os
238 ) const
239 {
240  // Should really only output only relevant entries but since using
241  // Function1-from-subdict upon construction our dictionary contains
242  // only the relevant entries.
243  dict_.writeEntry(this->name(), os);
244 }
245 
246 
247 // ************************************************************************* //
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:66
Foam::Function1Types::CodedFunction1::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: CodedFunction1.C:59
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::dictionaryContent
A wrapper for dictionary content, without operators that could affect inheritance patterns.
Definition: dictionaryContent.H:49
dynamicCodeContext.H
Foam::dictionary::subDictOrAdd
dictionary & subDictOrAdd(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary for manipulation.
Definition: dictionary.C:500
Foam::Function1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: propellerInfo.H:291
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:359
Foam::dynamicCodeContext::sha1
const SHA1 & sha1() const noexcept
The SHA1 calculated from options, libs, include, code, etc.
Definition: dynamicCodeContext.H:164
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::Function1Types::CodedFunction1::CodedFunction1
CodedFunction1(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
Definition: CodedFunction1.C:144
Foam::Function1Types::CodedFunction1
Function1 with the code supplied by an on-the-fly compiled C++ expression.
Definition: CodedFunction1.H:124
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
Foam::Function1Types::CodedFunction1::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedFunction1.C:95
Foam::Function1Types::CodedFunction1::codeDict
virtual const dictionary & codeDict() const
Definition: CodedFunction1.C:87
dynamicCode.H
DetailInfo
#define DetailInfo
Definition: evalEntry.C:37
Foam::dynamicCodeContext::libs
const string & libs() const noexcept
The code libs (LIB_LIBS)
Definition: dynamicCodeContext.H:140
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Function1Types::CodedFunction1::value
virtual Type value(const scalar x) const
Return value at current time.
Definition: CodedFunction1.C:223
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::Function1Types::CodedFunction1::description
virtual string description() const
Description (type + name) for the output.
Definition: CodedFunction1.C:44
Foam::Function1Types::CodedFunction1::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedFunction1.C:36
Foam::dynamicCode::setFilterVariable
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:388
Foam::dlLibraryTable::libs
static dlLibraryTable & libs()
Table of global libraries.
Definition: dlLibraryTable.C:80
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::dynamicCode::setFieldTemplates
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:286
dictionaryContent.H
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:397
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::dictionary::remove
bool remove(const word &keyword)
Remove an entry specified by keyword.
Definition: dictionarySearch.C:582
Foam::dictionaryContent::dict
const dictionary & dict() const noexcept
Read-access to the content.
Definition: dictionaryContent.H:125
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::dynamicCodeContext::code
const string & code() const noexcept
The code.
Definition: dynamicCodeContext.H:158
Foam::Function1Types::CodedFunction1::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CodedFunction1.C:236
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::Function1Types::CodedFunction1::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedFunction1.C:51
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::dynamicCodeContext::options
const string & options() const noexcept
The code options (Make/options)
Definition: dynamicCodeContext.H:134