CodedField.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) 2020 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 
30 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
35 {
36  return this->patch_.boundaryMesh().mesh().time().libs();
37 }
38 
39 
40 template<class Type>
42 (
43  dynamicCode& dynCode,
44  const dynamicCodeContext& context
45 ) const
46 {
47  if (context.code().empty())
48  {
50  << "No code section in input dictionary for patch "
51  << this->patch_.name()
52  << " name " << name_
53  << exit(FatalIOError);
54  }
55 
56  // Take no chances - typeName must be identical to name_
57  dynCode.setFilterVariable("typeName", name_);
58 
59  // Set TemplateType and FieldType filter variables
60  dynCode.setFieldTemplates<Type>();
61 
62  // Compile filtered C template
63  dynCode.addCompileFile(codeTemplateC);
64 
65  // Copy filtered H template
66  dynCode.addCopyFile(codeTemplateH);
67 
68  // Debugging: make verbose
69  // dynCode.setFilterVariable("verbose", "true");
70  // DetailInfo
71  // <<"compile " << name_ << " sha1: "
72  // << context.sha1() << endl;
73 
74  // Define Make/options
75  dynCode.setMakeOptions
76  (
77  "EXE_INC = -g \\\n"
78  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
79  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
80  + context.options()
81  + "\n\nLIB_LIBS = \\\n"
82  " -lOpenFOAM \\\n"
83  " -lfiniteVolume \\\n"
84  + context.libs()
85  );
86 }
87 
88 
89 template<class Type>
90 const Foam::dictionary&
92 (
93  const dictionary& dict
94 ) const
95 {
96  // Use named subdictionary if present to provide the code. This allows
97  // running with multiple PatchFunction1s
98 
99  return
100  (
101  dict.found("code")
102  ? dict
103  : dict.subDict(name_)
104  );
105 }
106 
107 
108 template<class Type>
109 const Foam::dictionary&
111 {
112  return codeDict(dict_);
113 }
114 
115 
116 template<class Type>
119 {
120  return "CodedField " + name_;
121 }
122 
123 
124 template<class Type>
126 {
127  // remove instantiation of fvPatchField provided by library
128  redirectFunctionPtr_.clear();
129 }
130 
131 
132 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
133 
134 template<class Type>
136 (
137  const polyPatch& pp,
138  const word& redirectType,
139  const word& entryName,
140  const dictionary& dict,
141  const bool faceValues
142 )
143 :
144  PatchFunction1<Type>(pp, entryName, dict, faceValues),
145  codedBase(),
146  dict_(dict),
147  name_(dict.getOrDefault<word>("name", entryName))
148 {
149  updateLibrary(name_);
150 }
151 
152 
153 template<class Type>
155 (
156  const CodedField<Type>& rhs
157 )
158 :
159  CodedField<Type>(rhs, rhs.patch())
160 {}
161 
162 
163 template<class Type>
165 (
166  const CodedField<Type>& rhs,
167  const polyPatch& pp
168 )
169 :
170  PatchFunction1<Type>(rhs, pp),
171  codedBase(),
172  dict_(rhs.dict_),
173  name_(rhs.name_)
174 {}
175 
176 
177 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
178 
179 template<class Type>
182 {
183  if (!redirectFunctionPtr_)
184  {
185  // Construct a PatchFunction1 containing the input code
186  dictionary completeDict(dict_);
187 
188  // Override the type to enforce the PatchFunction1::New constructor
189  // to choose our type
190  completeDict.set("type", name_);
191 
193  dict.add(name_, completeDict);
194 
195  redirectFunctionPtr_.reset
196  (
198  (
199  this->patch(),
200  name_,
201  dict,
202  this->faceValues()
203  )
204  );
205  }
206  return *redirectFunctionPtr_;
207 }
208 
209 
210 template<class Type>
213 (
214  const scalar x
215 ) const
216 {
217  // Ensure library containing user-defined code is up-to-date
218  updateLibrary(name_);
219 
220  return redirectFunction().value(x);
221 }
222 
223 
224 template<class Type>
227 (
228  const scalar x1,
229  const scalar x2
230 ) const
231 {
232  // Ensure library containing user-defined code is up-to-date
233  updateLibrary(name_);
234 
235  return redirectFunction().integrate(x1, x2);
236 }
237 
238 
239 template<class Type>
241 (
242  const FieldMapper& mapper
243 )
244 {
246  if (redirectFunctionPtr_)
247  {
248  redirectFunctionPtr_->autoMap(mapper);
249  }
250 }
251 
252 
253 template<class Type>
255 (
256  const PatchFunction1<Type>& pf1,
257  const labelList& addr
258 )
259 {
260  PatchFunction1<Type>::rmap(pf1, addr);
261  if (redirectFunctionPtr_)
262  {
263  redirectFunctionPtr_->rmap(pf1, addr);
264  }
265 }
266 
267 
268 template<class Type>
270 (
271  Ostream& os
272 ) const
273 {
274  // Should really only output only relevant entries but since using
275  // PatchFunction1-from-subdict upon construction our dictionary contains
276  // only the relevant entries. It would be different if PatchFunction1-from
277  // primitiveEntry when the whole 'value' entry would be present
278  dict_.writeEntry(this->name(), os);
279 }
280 
281 
282 // ************************************************************************* //
Foam::dynamicCodeContext::libs
const string & libs() const
Return the code-libs.
Definition: dynamicCodeContext.H:146
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:62
Foam::dynamicCode
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:59
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::PatchFunction1Types::CodedField::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: CodedField.C:34
Foam::dynamicCodeContext
Encapsulation of dynamic code dictionaries.
Definition: dynamicCodeContext.H:53
Foam::PatchFunction1Types::CodedField::rmap
virtual void rmap(const PatchFunction1< Type > &pf1, const labelList &addr)
Reverse map the given PatchFunction1 onto this PatchFunction1.
Definition: CodedField.C:255
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:67
Foam::FieldMapper
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:49
Foam::FatalIOError
IOerror FatalIOError
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::PatchFunction1Types::CodedField::autoMap
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: CodedField.C:241
Foam::PatchFunction1Types::CodedField::CodedField
CodedField(const polyPatch &pp, const word &redirectType, const word &entryName, const dictionary &dict, const bool faceValues=true)
Construct from entry name and dictionary.
Definition: CodedField.C:136
Foam::dynamicCode::addCopyFile
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:357
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:351
Foam::PatchFunction1Types::CodedField::value
virtual tmp< Field< Type > > value(const scalar x) const
Return CodedField value.
Definition: CodedField.C:213
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:68
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::PatchFunction1Types::CodedField::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedField.C:42
Foam::dynamicCodeContext::code
const string & code() const
Return the code.
Definition: dynamicCodeContext.H:152
dynamicCode.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::dynamicCodeContext::options
const string & options() const
Return the code-options.
Definition: dynamicCodeContext.H:140
Foam::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:59
Foam::PatchFunction1Types::CodedField::integrate
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: CodedField.C:227
Foam::dynamicCode::setFilterVariable
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:386
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
Foam::PatchFunction1Types::CodedField::codeDict
virtual const dictionary & codeDict() const
Definition: CodedField.C:110
Foam::PatchFunction1Types::CodedField::clearRedirect
virtual void clearRedirect() const
Definition: CodedField.C:125
Foam::dynamicCode::setMakeOptions
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:395
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::List< label >
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::PatchFunction1Types::CodedField::writeData
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: CodedField.C:270
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
Foam::PatchFunction1Types::CodedField::description
virtual string description() const
Definition: CodedField.C:118
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::PatchFunction1Types::CodedField
PatchFunction1 with the code supplied by an on-the-fly compiled C++ expression.
Definition: CodedField.H:94