codedMixedFvPatchField.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "codedMixedFvPatchField.H"
31 #include "fvPatchFieldMapper.H"
32 #include "volFields.H"
33 #include "dynamicCode.H"
34 #include "dictionaryContent.H"
35 
36 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37 
38 template<class Type>
40 {
41  return this->db().time().libs();
42 }
43 
44 
45 template<class Type>
47 {
48  return
49  "patch "
50  + this->patch().name()
51  + " on field "
52  + this->internalField().name();
53 }
54 
55 
56 template<class Type>
58 {
59  redirectPatchFieldPtr_.reset(nullptr);
60 }
61 
62 
63 template<class Type>
64 const Foam::dictionary&
66 {
67  const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
68  return (ptr ? *ptr : dictionary::null);
69 }
70 
71 
72 template<class Type>
73 const Foam::dictionary&
75 {
76  // Inline "code" or from system/codeDict
77  return
78  (
79  dict_.found("code")
80  ? dict_
81  : codedBase::codeDict(this->db()).subDict(name_)
82  );
83 }
84 
85 
86 template<class Type>
88 (
89  dynamicCode& dynCode,
90  const dynamicCodeContext& context
91 ) const
92 {
93  // Take no chances - typeName must be identical to name_
94  dynCode.setFilterVariable("typeName", name_);
95 
96  // Set TemplateType and FieldType filter variables
97  dynCode.setFieldTemplates<Type>();
98 
99  // Compile filtered C template
100  dynCode.addCompileFile(codeTemplateC);
101 
102  // Copy filtered H template
103  dynCode.addCopyFile(codeTemplateH);
104 
105  #ifdef FULLDEBUG
106  dynCode.setFilterVariable("verbose", "true");
107  DetailInfo
108  <<"compile " << name_ << " sha1: " << context.sha1() << endl;
109  #endif
110 
111  // Define Make/options
112  dynCode.setMakeOptions
113  (
114  "EXE_INC = -g \\\n"
115  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
116  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
117  + context.options()
118  + "\n\nLIB_LIBS = \\\n"
119  " -lOpenFOAM \\\n"
120  " -lfiniteVolume \\\n"
121  " -lmeshTools \\\n"
122  + context.libs()
123  );
124 }
125 
126 
127 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
128 
129 template<class Type>
131 (
132  const fvPatch& p,
134 )
135 :
136  parent_bctype(p, iF),
137  codedBase(),
138  redirectPatchFieldPtr_(nullptr)
139 {}
140 
141 
142 template<class Type>
144 (
145  const codedMixedFvPatchField<Type>& rhs,
146  const fvPatch& p,
148  const fvPatchFieldMapper& mapper
149 )
150 :
151  parent_bctype(rhs, p, iF, mapper),
152  codedBase(),
153  dict_(rhs.dict_), // Deep copy
154  name_(rhs.name_),
155  redirectPatchFieldPtr_(nullptr)
156 {}
157 
158 
159 template<class Type>
161 (
162  const fvPatch& p,
164  const dictionary& dict
165 )
166 :
167  parent_bctype(p, iF, dict),
168  codedBase(),
169  dict_
170  (
171  // Copy dictionary, but without "heavy" data chunks
172  dictionaryContent::copyDict
173  (
174  dict,
175  wordList(), // allow
176  wordList // deny
177  ({
178  "type", // redundant
179  "value", "refValue", "refGradient", "valueFraction"
180  })
181  )
182  ),
183  name_(dict.getCompat<word>("name", {{"redirectType", 1706}})),
184  redirectPatchFieldPtr_(nullptr)
185 {
186  updateLibrary(name_);
187 }
188 
189 
190 template<class Type>
192 (
194 )
195 :
196  parent_bctype(rhs),
197  codedBase(),
198  dict_(rhs.dict_), // Deep copy
199  name_(rhs.name_),
200  redirectPatchFieldPtr_(nullptr)
201 {}
202 
203 
204 template<class Type>
206 (
207  const codedMixedFvPatchField<Type>& rhs,
209 )
210 :
211  parent_bctype(rhs, iF),
212  codedBase(),
213  dict_(rhs.dict_), // Deep copy
214  name_(rhs.name_),
215  redirectPatchFieldPtr_(nullptr)
216 {}
217 
218 
219 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
220 
221 template<class Type>
224 {
225  if (!redirectPatchFieldPtr_)
226  {
227  // Construct a patch
228  // Make sure to construct the patchfield with up-to-date value
229 
230  // Write the data from the mixed b.c.
232  this->parent_bctype::write(os);
233  IStringStream is(os.str());
234  // Construct dictionary from it.
235  dictionary constructDict(is);
236 
237  // Override type
238  constructDict.set("type", name_);
239 
240  redirectPatchFieldPtr_.reset
241  (
242  dynamic_cast<parent_bctype*>
243  (
245  (
246  this->patch(),
247  this->internalField(),
248  constructDict
249  ).ptr()
250  )
251  );
252 
253  // Forward copy of dictionary content to the code template
254  auto* contentPtr =
255  dynamic_cast<dictionaryContent*>(redirectPatchFieldPtr_.get());
256 
257  if (contentPtr)
258  {
259  contentPtr->dict(this->codeContext());
260  }
261  else
262  {
264  << name_ << " Did not derive from dictionaryContent"
265  << nl << nl;
266  }
267  }
268  return *redirectPatchFieldPtr_;
269 }
270 
271 
272 template<class Type>
274 {
275  if (this->updated())
276  {
277  return;
278  }
279 
280  // Make sure library containing user-defined fvPatchField is up-to-date
281  updateLibrary(name_);
282 
283  const parent_bctype& fvp = redirectPatchField();
284  const_cast<parent_bctype&>(fvp).updateCoeffs();
285 
286  // Copy through coefficients
287  this->refValue() = fvp.refValue();
288  this->refGrad() = fvp.refGrad();
289  this->valueFraction() = fvp.valueFraction();
290 
291  this->parent_bctype::updateCoeffs();
292 }
293 
294 
295 template<class Type>
297 (
298  const Pstream::commsTypes commsType
299 )
300 {
301  // Make sure library containing user-defined fvPatchField is up-to-date
302  updateLibrary(name_);
303 
304  const parent_bctype& fvp = redirectPatchField();
305 
306  // - updates the value of fvp (though not used)
307  // - resets the updated() flag
308  const_cast<parent_bctype&>(fvp).evaluate(commsType);
309 
310  // Update the value (using the coefficients) locally
311  parent_bctype::evaluate(commsType);
312 }
313 
314 
315 template<class Type>
317 {
318  this->parent_bctype::write(os);
319  os.writeEntry("name", name_);
320 
321  codedBase::writeCodeDict(os, dict_);
322 }
323 
324 
325 // ************************************************************************* //
Foam::fvPatchField
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: volSurfaceMapping.H:51
Foam::codedMixedFvPatchField::codeDict
virtual const dictionary & codeDict() const
The code dictionary. Inline "code" or from system/codeDict.
Definition: codedMixedFvPatchField.C:74
Foam::dictionary::findDict
dictionary * findDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX)
Find and return a sub-dictionary pointer if present.
Definition: dictionaryI.H:127
volFields.H
Foam::mixedFvPatchField::valueFraction
virtual scalarField & valueFraction()
Definition: mixedFvPatchField.H:250
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:63
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::mixedFvPatchField::refValue
virtual Field< Type > & refValue()
Definition: mixedFvPatchField.H:230
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::codedMixedFvPatchField::prepare
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: codedMixedFvPatchField.C:88
Foam::mixedFvPatchField::refGrad
virtual Field< Type > & refGrad()
Definition: mixedFvPatchField.H:240
Foam::codedMixedFvPatchField::updateCoeffs
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
Definition: codedMixedFvPatchField.C:273
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
fvPatchFieldMapper.H
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::dictionaryContent
A wrapper for dictionary content, without operators that could affect inheritance patterns.
Definition: dictionaryContent.H:49
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::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::dynamicCode::addCompileFile
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:353
Foam::codedMixedFvPatchField::libs
virtual dlLibraryTable & libs() const
Mutable access to the loaded dynamic libraries.
Definition: codedMixedFvPatchField.C:39
Foam::fvPatch
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:65
Foam::codedMixedFvPatchField::description
virtual string description() const
Description (type + name) for the output.
Definition: codedMixedFvPatchField.C:46
dynamicCode.H
Foam::codedMixedFvPatchField::redirectPatchField
const mixedFvPatchField< Type > & redirectPatchField() const
Get reference to the underlying patchField.
Definition: codedMixedFvPatchField.C:223
DetailInfo
#define DetailInfo
Definition: evalEntry.C:37
Foam::dynamicCodeContext::libs
const string & libs() const noexcept
The code libs (LIB_LIBS)
Definition: dynamicCodeContext.H:140
Foam::codedMixedFvPatchField::codeContext
virtual const dictionary & codeContext() const
Additional 'codeContext' dictionary to pass through.
Definition: codedMixedFvPatchField.C:65
Foam::codedMixedFvPatchField::codedMixedFvPatchField
codedMixedFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &)
Construct from patch and internal field.
Definition: codedMixedFvPatchField.C:131
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::codedMixedFvPatchField::clearRedirect
virtual void clearRedirect() const
Clear redirected object(s)
Definition: codedMixedFvPatchField.C:57
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)
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
Foam::mixedFvPatchField
This boundary condition provides a base class for 'mixed' type boundary conditions,...
Definition: mixedFvPatchField.H:123
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::dynamicCode::setFieldTemplates
void setFieldTemplates()
Define a filter variables TemplateType and FieldType.
Definition: dynamicCode.H:286
dictionaryContent.H
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
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::dictionaryContent::dict
const dictionary & dict() const noexcept
Read-access to the content.
Definition: dictionaryContent.H:125
Foam::List< word >
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
Foam::codedMixedFvPatchField::evaluate
virtual void evaluate(const Pstream::commsTypes commsType=Pstream::commsTypes::blocking)
Evaluate the patch field.
Definition: codedMixedFvPatchField.C:297
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::fvPatchFieldMapper
Foam::fvPatchFieldMapper.
Definition: fvPatchFieldMapper.H:47
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
codedMixedFvPatchField.H
Foam::codedMixedFvPatchField::write
virtual void write(Ostream &) const
Write.
Definition: codedMixedFvPatchField.C:316
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
Foam::codedMixedFvPatchField
Constructs on-the-fly a new boundary condition (derived from mixedFvPatchField) which is then used to...
Definition: codedMixedFvPatchField.H:149
Foam::DimensionedField
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Definition: DimensionedField.H:54
Foam::stringOps::evaluate
string evaluate(label fieldWidth, const std::string &s, size_t pos=0, size_t len=std::string::npos)
String evaluation with specified (positive, non-zero) field width.
Definition: stringOpsEvaluate.C:37