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-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->patch_.boundaryMesh().mesh().time().libs();
39 }
40 
41 
42 template<class Type>
45 {
46  return "CodedField " + 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 PatchFunction1s
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 patch "
104  << this->patch_.name()
105  << " name " << redirectName_
106  << exit(FatalIOError);
107  }
108 
109  // Take no chances - typeName must be identical to redirectName_
110  dynCode.setFilterVariable("typeName", redirectName_);
111 
112  // Set TemplateType and FieldType filter variables
113  dynCode.setFieldTemplates<Type>();
114 
115  // Compile filtered C template
116  dynCode.addCompileFile(codeTemplateC);
117 
118  // Copy filtered H template
119  dynCode.addCopyFile(codeTemplateH);
120 
121  #ifdef FULLDEBUG
122  dynCode.setFilterVariable("verbose", "true");
123  DetailInfo
124  <<"compile " << redirectName_ << " sha1: " << context.sha1() << endl;
125  #endif
126 
127  // Define Make/options
128  dynCode.setMakeOptions
129  (
130  "EXE_INC = -g \\\n"
131  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
132  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
133  + context.options()
134  + "\n\nLIB_LIBS = \\\n"
135  " -lOpenFOAM \\\n"
136  " -lfiniteVolume \\\n"
137  " -lmeshTools \\\n"
138  + context.libs()
139  );
140 }
141 
142 
143 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
144 
145 template<class Type>
147 (
148  const polyPatch& pp,
149  const word& redirectType,
150  const word& entryName,
151  const dictionary& dict,
152  const bool faceValues
153 )
154 :
155  PatchFunction1<Type>(pp, entryName, dict, faceValues),
156  codedBase(),
157  dict_(dict),
158  redirectName_(dict.getOrDefault<word>("name", entryName))
159 {
160  this->codedBase::setCodeContext(dict_);
161 
162  // No additional code chunks...
163 
164  updateLibrary(redirectName_);
165 }
166 
167 
168 template<class Type>
170 (
171  const CodedField<Type>& rhs
172 )
173 :
174  CodedField<Type>(rhs, rhs.patch())
175 {}
176 
177 
178 template<class Type>
180 (
181  const CodedField<Type>& rhs,
182  const polyPatch& pp
183 )
184 :
185  PatchFunction1<Type>(rhs, pp),
186  codedBase(),
187  dict_(rhs.dict_),
188  redirectName_(rhs.redirectName_)
189 {}
190 
191 
192 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
193 
194 template<class Type>
197 {
198  if (!redirectFunctionPtr_)
199  {
200  dictionary constructDict;
201  // Force 'redirectName_' sub-dictionary into existence
202  dictionary& coeffs = constructDict.subDictOrAdd(redirectName_);
203 
204  coeffs = dict_; // Copy input code and coefficients
205  coeffs.remove("name"); // Redundant
206  coeffs.set("type", redirectName_); // Specify our new (redirect) type
207 
208  redirectFunctionPtr_.reset
209  (
211  (
212  this->patch(),
213  redirectName_,
214  constructDict,
215  this->faceValues()
216  )
217  );
218 
219  // Forward copy of codeContext to the code template
220  auto* contentPtr =
221  dynamic_cast<dictionaryContent*>(redirectFunctionPtr_.get());
222 
223  if (contentPtr)
224  {
225  contentPtr->dict(this->codeContext());
226  }
227  else
228  {
230  << redirectName_ << " Did not derive from dictionaryContent"
231  << nl << nl;
232  }
233  }
234  return *redirectFunctionPtr_;
235 }
236 
237 
238 template<class Type>
241 (
242  const scalar x
243 ) const
244 {
245  // Ensure library containing user-defined code is up-to-date
246  updateLibrary(redirectName_);
247 
248  return redirectFunction().value(x);
249 }
250 
251 
252 template<class Type>
255 (
256  const scalar x1,
257  const scalar x2
258 ) const
259 {
260  // Ensure library containing user-defined code is up-to-date
261  updateLibrary(redirectName_);
262 
263  return redirectFunction().integrate(x1, x2);
264 }
265 
266 
267 template<class Type>
269 (
270  const FieldMapper& mapper
271 )
272 {
274  if (redirectFunctionPtr_)
275  {
276  redirectFunctionPtr_->autoMap(mapper);
277  }
278 }
279 
280 
281 template<class Type>
283 (
284  const PatchFunction1<Type>& pf1,
285  const labelList& addr
286 )
287 {
288  PatchFunction1<Type>::rmap(pf1, addr);
289  if (redirectFunctionPtr_)
290  {
291  redirectFunctionPtr_->rmap(pf1, addr);
292  }
293 }
294 
295 
296 template<class Type>
298 (
299  Ostream& os
300 ) const
301 {
302  // Should really only output only relevant entries but since using
303  // PatchFunction1-from-subdict upon construction our dictionary contains
304  // only the relevant entries. It would be different if PatchFunction1-from
305  // primitiveEntry when the whole 'value' entry would be present
306  dict_.writeEntry(this->name(), os);
307 }
308 
309 
310 // ************************************************************************* //
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::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:36
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:283
Foam::codedBase
Base class for function objects and boundary conditions using dynamic code that provides methods for ...
Definition: codedBase.H:66
Foam::FieldMapper
Abstract base class to hold the Field mapping addressing and weights.
Definition: FieldMapper.H:49
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::PatchFunction1Types::CodedField::autoMap
virtual void autoMap(const FieldMapper &mapper)
Map (and resize as needed) from self given a mapping object.
Definition: CodedField.C:269
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::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:147
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::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
Foam::PatchFunction1Types::CodedField::value
virtual tmp< Field< Type > > value(const scalar x) const
Return CodedField value.
Definition: CodedField.C:241
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:68
Foam::PatchFunction1Types::CodedField::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedField.C:95
dynamicCode.H
Foam::PatchFunction1Types::CodedField::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: CodedField.C:59
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::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::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:60
Foam::PatchFunction1Types::CodedField::integrate
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: CodedField.C:255
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::PatchFunction1Types::CodedField::codeDict
virtual const dictionary & codeDict() const
Definition: CodedField.C:87
Foam::PatchFunction1Types::CodedField::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: CodedField.C:51
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::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
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
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:298
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::PatchFunction1Types::CodedField::description
virtual string description() const
Description (type + name) for the output.
Definition: CodedField.C:44
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:124
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