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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
35 {
36  const dlLibraryTable& timeLibs =
37  this->patch_.boundaryMesh().mesh().time().libs();
38 
39  return const_cast<dlLibraryTable&>(timeLibs);
40 }
41 
42 
43 template<class Type>
45 (
46  dynamicCode& dynCode,
47  const dynamicCodeContext& context
48 ) const
49 {
50  if (context.code().empty())
51  {
53  << "No code section in input dictionary for patch "
54  << this->patch_.name()
55  << " name " << name_
56  << exit(FatalIOError);
57  }
58 
59  // Take no chances - typeName must be identical to name_
60  dynCode.setFilterVariable("typeName", name_);
61 
62  // Set TemplateType and FieldType filter variables
63  dynCode.setFieldTemplates<Type>();
64 
65  // Compile filtered C template
66  dynCode.addCompileFile(codeTemplateC);
67 
68  // Copy filtered H template
69  dynCode.addCopyFile(codeTemplateH);
70 
71  // Debugging: make verbose
72  // dynCode.setFilterVariable("verbose", "true");
73  // DetailInfo
74  // <<"compile " << name_ << " sha1: "
75  // << context.sha1() << endl;
76 
77  // Define Make/options
78  dynCode.setMakeOptions
79  (
80  "EXE_INC = -g \\\n"
81  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
82  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
83  + context.options()
84  + "\n\nLIB_LIBS = \\\n"
85  " -lOpenFOAM \\\n"
86  " -lfiniteVolume \\\n"
87  + context.libs()
88  );
89 }
90 
91 
92 template<class Type>
93 const Foam::dictionary&
95 (
96  const dictionary& dict
97 ) const
98 {
99  // Use named subdictionary if present to provide the code. This allows
100  // running with multiple PatchFunction1s
101 
102  return
103  (
104  dict.found("code")
105  ? dict
106  : dict.subDict(name_)
107  );
108 }
109 
110 
111 template<class Type>
112 const Foam::dictionary&
114 {
115  return codeDict(dict_);
116 }
117 
118 
119 template<class Type>
122 {
123  return "CodedField " + name_;
124 }
125 
126 
127 template<class Type>
129 {
130  // remove instantiation of fvPatchField provided by library
131  redirectFunctionPtr_.clear();
132 }
133 
134 
135 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
136 
137 template<class Type>
139 (
140  const polyPatch& pp,
141  const word& type,
142  const word& entryName,
143  const dictionary& dict,
144  const bool faceValues
145 )
146 :
147  PatchFunction1<Type>(pp, entryName, dict, faceValues),
148  codedBase(),
149  dict_(dict),
150  name_(dict.getOrDefault<word>("name", entryName))
151 {
152  updateLibrary(name_);
153 }
154 
155 
156 template<class Type>
158 (
159  const CodedField<Type>& rhs
160 )
161 :
163  codedBase(),
164  dict_(rhs.dict_),
165  name_(rhs.name_)
166 {}
167 
168 
169 template<class Type>
171 (
172  const CodedField<Type>& rhs,
173  const polyPatch& pp
174 )
175 :
176  PatchFunction1<Type>(rhs, pp),
177  codedBase(),
178  dict_(rhs.dict_),
179  name_(rhs.name_)
180 {}
181 
182 
183 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
184 
185 template<class Type>
188 {
189  if (!redirectFunctionPtr_)
190  {
191  // Construct a PatchFunction1 containing the input code
192  dictionary completeDict(dict_);
193 
194  // Override the type to enforce the PatchFunction1::New constructor
195  // to choose our type
196  completeDict.set("type", name_);
197 
199  dict.add(name_, completeDict);
200 
201  redirectFunctionPtr_.reset
202  (
204  (
205  this->patch(),
206  name_,
207  dict,
208  this->faceValues_
209  )
210  );
211  }
212  return *redirectFunctionPtr_;
213 }
214 
215 
216 template<class Type>
219 (
220  const scalar x
221 ) const
222 {
223  // Ensure library containing user-defined code is up-to-date
224  updateLibrary(name_);
225 
226  return redirectFunction().value(x);
227 }
228 
229 
230 template<class Type>
233 (
234  const scalar x1,
235  const scalar x2
236 ) const
237 {
238  // Ensure library containing user-defined code is up-to-date
239  updateLibrary(name_);
240 
241  return redirectFunction().integrate(x1, x2);
242 }
243 
244 
245 template<class Type>
247 (
248  const FieldMapper& mapper
249 )
250 {
252  if (redirectFunctionPtr_)
253  {
254  redirectFunctionPtr_->autoMap(mapper);
255  }
256 }
257 
258 
259 template<class Type>
261 (
262  const PatchFunction1<Type>& pf1,
263  const labelList& addr
264 )
265 {
266  PatchFunction1<Type>::rmap(pf1, addr);
267  if (redirectFunctionPtr_)
268  {
269  redirectFunctionPtr_->rmap(pf1, addr);
270  }
271 }
272 
273 
274 template<class Type>
276 (
277  Ostream& os
278 ) const
279 {
280  // Should really only output only relevant entries but since using
281  // PatchFunction1-from-subdict upon construction our dictionary contains
282  // only the relevant entries. It would be different if PatchFunction1-from
283  // primitiveEntry when the whole 'value' entry would be present
284  dict_.writeEntry(this->name(), os);
285 }
286 
287 
288 // ************************************************************************* //
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
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:261
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::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:247
Foam::PatchFunction1Types::CodedField::value
virtual tmp< Field< Type > > value(const scalar x) const
Return CodedField value.
Definition: CodedField.C:219
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:67
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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::PatchFunction1
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
Definition: PatchFunction1.H:63
Foam::PatchFunction1Types::CodedField::CodedField
CodedField(const polyPatch &pp, const word &type, const word &entryName, const dictionary &dict, const bool faceValues=true)
Construct from entry name and dictionary.
Definition: CodedField.C:139
Foam::PatchFunction1Types::CodedField::integrate
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: CodedField.C:233
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
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:276
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
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