codedMixedFvPatchField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "codedMixedFvPatchField.H"
28 #include "fvPatchFieldMapper.H"
29 #include "volFields.H"
30 #include "dynamicCode.H"
31 #include "dynamicCodeContext.H"
32 #include "stringOps.H"
33 
34 // * * * * * * * * * * * * Private Static Data Members * * * * * * * * * * * //
35 
36 template<class Type>
37 const Foam::wordList Foam::codedMixedFvPatchField<Type>::codeKeys_ =
38  {"code", "codeInclude", "localCode"};
39 
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 template<class Type>
44 const Foam::word Foam::codedMixedFvPatchField<Type>::codeTemplateC =
45  "mixedFvPatchFieldTemplate.C";
46 
47 template<class Type>
48 const Foam::word Foam::codedMixedFvPatchField<Type>::codeTemplateH =
49  "mixedFvPatchFieldTemplate.H";
50 
51 
52 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
53 
54 template<class Type>
56 (
57  dynamicCode& dynCode
58 )
59 {
60  word fieldType(pTraits<Type>::typeName);
61 
62  // template type for fvPatchField
63  dynCode.setFilterVariable("TemplateType", fieldType);
64 
65  // Name for fvPatchField - eg, ScalarField, VectorField, ...
66  fieldType[0] = toupper(fieldType[0]);
67  dynCode.setFilterVariable("FieldType", fieldType + "Field");
68 }
69 
70 
71 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
72 
73 template<class Type>
75 {
76  const objectRegistry& obr = this->db();
77 
78  if (obr.foundObject<IOdictionary>("codeDict"))
79  {
80  return obr.lookupObject<IOdictionary>("codeDict");
81  }
82  else
83  {
84  return obr.store
85  (
86  new IOdictionary
87  (
88  IOobject
89  (
90  "codeDict",
91  this->db().time().system(),
92  this->db(),
93  IOobject::MUST_READ_IF_MODIFIED,
94  IOobject::NO_WRITE
95  )
96  )
97  );
98  }
99 }
100 
101 
102 template<class Type>
104 {
105  return const_cast<dlLibraryTable&>(this->db().time().libs());
106 }
107 
108 
109 template<class Type>
111 (
112  dynamicCode& dynCode,
113  const dynamicCodeContext& context
114 ) const
115 {
116  // take no chances - typeName must be identical to name_
117  dynCode.setFilterVariable("typeName", name_);
118 
119  // set TemplateType and FieldType filter variables
120  // (for fvPatchField)
121  setFieldTemplates(dynCode);
122 
123  // compile filtered C template
124  dynCode.addCompileFile(codeTemplateC);
125 
126  // copy filtered H template
127  dynCode.addCopyFile(codeTemplateH);
128 
129 
130  // debugging: make BC verbose
131  // dynCode.setFilterVariable("verbose", "true");
132  // Info<<"compile " << name_ << " sha1: "
133  // << context.sha1() << endl;
134 
135  // define Make/options
136  dynCode.setMakeOptions
137  (
138  "EXE_INC = -g \\\n"
139  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
140  + context.options()
141  + "\n\nLIB_LIBS = \\\n"
142  + " -lOpenFOAM \\\n"
143  + " -lfiniteVolume \\\n"
144  + context.libs()
145  );
146 }
147 
148 
149 template<class Type>
151 const
152 {
153  // use system/codeDict or in-line
154  return
155  (
156  dict_.found("code")
157  ? dict_
158  : this->dict().subDict(name_)
159  );
160 }
161 
162 
163 template<class Type>
165 {
166  return codeKeys_;
167 }
168 
169 
170 template<class Type>
172 {
173  return
174  "patch "
175  + this->patch().name()
176  + " on field "
177  + this->internalField().name();
178 }
179 
180 
181 template<class Type>
183 {
184  // remove instantiation of fvPatchField provided by library
185  redirectPatchFieldPtr_.clear();
186 }
187 
188 
189 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
190 
191 template<class Type>
193 (
194  const fvPatch& p,
196 )
197 :
199  codedBase(),
200  redirectPatchFieldPtr_()
201 {}
202 
203 
204 template<class Type>
206 (
207  const codedMixedFvPatchField<Type>& ptf,
208  const fvPatch& p,
210  const fvPatchFieldMapper& mapper
211 )
212 :
213  mixedFvPatchField<Type>(ptf, p, iF, mapper),
214  codedBase(),
215  dict_(ptf.dict_),
216  name_(ptf.name_),
217  redirectPatchFieldPtr_()
218 {}
219 
220 
221 template<class Type>
223 (
224  const fvPatch& p,
226  const dictionary& dict
227 )
228 :
230  codedBase(),
231  dict_(dict),
232  name_
233  (
234  dict.found("redirectType")
235  ? dict.lookup("redirectType")
236  : dict.lookup("name")
237  ),
238  redirectPatchFieldPtr_()
239 {
240  updateLibrary(name_);
241 }
242 
243 
244 template<class Type>
246 (
248 )
249 :
251  codedBase(),
252  dict_(ptf.dict_),
253  name_(ptf.name_),
254  redirectPatchFieldPtr_()
255 {}
256 
257 
258 template<class Type>
260 (
261  const codedMixedFvPatchField<Type>& ptf,
263 )
264 :
265  mixedFvPatchField<Type>(ptf, iF),
266  codedBase(),
267  dict_(ptf.dict_),
268  name_(ptf.name_),
269  redirectPatchFieldPtr_()
270 {}
271 
272 
273 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
274 
275 template<class Type>
278 {
279  if (!redirectPatchFieldPtr_.valid())
280  {
281  // Construct a patch
282  // Make sure to construct the patchfield with up-to-date value
283 
284  // Write the data from the mixed b.c.
285  OStringStream os;
287  IStringStream is(os.str());
288  // Construct dictionary from it.
289  dictionary dict(is);
290 
291  // Override the type to enforce the fvPatchField::New constructor
292  // to choose our type
293  dict.set("type", name_);
294 
295  redirectPatchFieldPtr_.set
296  (
297  dynamic_cast<mixedFvPatchField<Type>*>
298  (
300  (
301  this->patch(),
302  this->internalField(),
303  dict
304  ).ptr()
305  )
306  );
307  }
308  return redirectPatchFieldPtr_();
309 }
310 
311 
312 template<class Type>
314 {
315  if (this->updated())
316  {
317  return;
318  }
319 
320  // Make sure library containing user-defined fvPatchField is up-to-date
321  updateLibrary(name_);
322 
323  const mixedFvPatchField<Type>& fvp = redirectPatchField();
324 
325  const_cast<mixedFvPatchField<Type>&>(fvp).updateCoeffs();
326 
327  // Copy through coefficients
328  this->refValue() = fvp.refValue();
329  this->refGrad() = fvp.refGrad();
330  this->valueFraction() = fvp.valueFraction();
331 
333 }
334 
335 
336 template<class Type>
338 (
339  const Pstream::commsTypes commsType
340 )
341 {
342  // Make sure library containing user-defined fvPatchField is up-to-date
343  updateLibrary(name_);
344 
345  const mixedFvPatchField<Type>& fvp = redirectPatchField();
346 
347  // - updates the value of fvp (though not used)
348  // - resets the updated() flag
349  const_cast<mixedFvPatchField<Type>&>(fvp).evaluate(commsType);
350 
351  // Update the value (using the coefficients) locally
353 }
354 
355 
356 template<class Type>
358 {
360  os.writeKeyword("name") << name_
361  << token::END_STATEMENT << nl;
362 
363  if (dict_.found("codeInclude"))
364  {
365  os.writeKeyword("codeInclude")
366  << token::HASH << token::BEGIN_BLOCK;
367 
368  os.writeQuoted(string(dict_["codeInclude"]), false)
369  << token::HASH << token::END_BLOCK
370  << token::END_STATEMENT << nl;
371  }
372 
373  if (dict_.found("localCode"))
374  {
375  os.writeKeyword("localCode")
376  << token::HASH << token::BEGIN_BLOCK;
377 
378  os.writeQuoted(string(dict_["localCode"]), false)
379  << token::HASH << token::END_BLOCK
380  << token::END_STATEMENT << nl;
381  }
382 
383  if (dict_.found("code"))
384  {
385  os.writeKeyword("code")
386  << token::HASH << token::BEGIN_BLOCK;
387 
388  os.writeQuoted(string(dict_["code"]), false)
389  << token::HASH << token::END_BLOCK
390  << token::END_STATEMENT << nl;
391  }
392 
393  if (dict_.found("codeOptions"))
394  {
395  os.writeKeyword("codeOptions")
396  << token::HASH << token::BEGIN_BLOCK;
397 
398  os.writeQuoted(string(dict_["codeOptions"]), false)
399  << token::HASH << token::END_BLOCK
400  << token::END_STATEMENT << nl;
401  }
402 
403  if (dict_.found("codeLibs"))
404  {
405  os.writeKeyword("codeLibs")
406  << token::HASH << token::BEGIN_BLOCK;
407 
408  os.writeQuoted(string(dict_["codeLibs"]), false)
409  << token::HASH << token::END_BLOCK
410  << token::END_STATEMENT << nl;
411  }
412 }
413 
414 
415 // ************************************************************************* //
dictionary dict
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:438
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
commsTypes
Types of communications.
Definition: UPstream.H:64
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:61
This boundary condition provides a base class for &#39;mixed&#39; type boundary conditions, i.e. conditions that mix fixed value and patch-normal gradient conditions.
Abstract base class with a fat-interface to all derived classes covering all possible ways in which t...
Definition: fvPatchField.H:66
Macros for easy insertion into run-time selection tables.
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
const mixedFvPatchField< Type > & redirectPatchField() const
Get reference to the underlying patchField.
virtual Field< Type > & refValue()
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:52
void write(Ostream &, const label, const dictionary &)
Write with dictionary lookup.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
A class for handling words, derived from string.
Definition: word.H:59
virtual scalarField & valueFraction()
Foam::fvPatchFieldMapper.
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:53
A table of dynamically loaded libraries.
virtual void evaluate(const Pstream::commsTypes commsType=Pstream::commsTypes::blocking)
Evaluate the patch field.
codedMixedFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &)
Construct from patch and internal field.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
virtual void write(Ostream &) const
Write.
Constructs on-the-fly a new boundary condition (derived from mixedFvPatchField) which is then used to...
Input from memory buffer stream.
Definition: IStringStream.H:49
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
string str() const
Return the string.
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
volScalarField & p
A class for handling character strings derived from std::string.
Definition: string.H:74
Output to memory buffer stream.
Definition: OStringStream.H:49
virtual Field< Type > & refGrad()
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1234
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583