codedFixedValueFvPatchField.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 
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::codedFixedValueFvPatchField<Type>::codeKeys_ =
38  {"code", "codeInclude", "localCode"};
39 
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 template<class Type>
44 const Foam::word Foam::codedFixedValueFvPatchField<Type>::codeTemplateC =
45  "fixedValueFvPatchFieldTemplate.C";
46 
47 template<class Type>
48 const Foam::word Foam::codedFixedValueFvPatchField<Type>::codeTemplateH =
49  "fixedValueFvPatchFieldTemplate.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 (
208  const fvPatch& p,
210  const fvPatchFieldMapper& mapper
211 )
212 :
213  fixedValueFvPatchField<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 (
263 )
264 :
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  OStringStream os;
285  writeEntry(os, "type", name_);
286  writeEntry(os, "value", *this);
287  IStringStream is(os.str());
288  dictionary dict(is);
289 
290  redirectPatchFieldPtr_.set
291  (
293  (
294  this->patch(),
295  this->internalField(),
296  dict
297  ).ptr()
298  );
299  }
300  return redirectPatchFieldPtr_();
301 }
302 
303 
304 template<class Type>
306 {
307  if (this->updated())
308  {
309  return;
310  }
311 
312  // Make sure library containing user-defined fvPatchField is up-to-date
313  updateLibrary(name_);
314 
315  const fvPatchField<Type>& fvp = redirectPatchField();
316 
317  const_cast<fvPatchField<Type>&>(fvp).updateCoeffs();
318 
319  // Copy through value
320  this->operator==(fvp);
321 
323 }
324 
325 
326 template<class Type>
328 (
329  const Pstream::commsTypes commsType
330 )
331 {
332  // Make sure library containing user-defined fvPatchField is up-to-date
333  updateLibrary(name_);
334 
335  const fvPatchField<Type>& fvp = redirectPatchField();
336 
337  const_cast<fvPatchField<Type>&>(fvp).evaluate(commsType);
338 
340 }
341 
342 
343 template<class Type>
345 {
347  os.writeKeyword("name") << name_
348  << token::END_STATEMENT << nl;
349 
350  if (dict_.found("codeInclude"))
351  {
352  os.writeKeyword("codeInclude")
353  << token::HASH << token::BEGIN_BLOCK;
354 
355  os.writeQuoted(string(dict_["codeInclude"]), false)
356  << token::HASH << token::END_BLOCK
357  << token::END_STATEMENT << nl;
358  }
359 
360  if (dict_.found("localCode"))
361  {
362  os.writeKeyword("localCode")
363  << token::HASH << token::BEGIN_BLOCK;
364 
365  os.writeQuoted(string(dict_["localCode"]), false)
366  << token::HASH << token::END_BLOCK
367  << token::END_STATEMENT << nl;
368  }
369 
370  if (dict_.found("code"))
371  {
372  os.writeKeyword("code")
373  << token::HASH << token::BEGIN_BLOCK;
374 
375  os.writeQuoted(string(dict_["code"]), false)
376  << token::HASH << token::END_BLOCK
377  << token::END_STATEMENT << nl;
378  }
379 
380  if (dict_.found("codeOptions"))
381  {
382  os.writeKeyword("codeOptions")
383  << token::HASH << token::BEGIN_BLOCK;
384 
385  os.writeQuoted(string(dict_["codeOptions"]), false)
386  << token::HASH << token::END_BLOCK
387  << token::END_STATEMENT << nl;
388  }
389 
390  if (dict_.found("codeLibs"))
391  {
392  os.writeKeyword("codeLibs")
393  << token::HASH << token::BEGIN_BLOCK;
394 
395  os.writeQuoted(string(dict_["codeLibs"]), false)
396  << token::HASH << token::END_BLOCK
397  << token::END_STATEMENT << nl;
398  }
399 }
400 
401 
402 // ************************************************************************* //
This boundary condition supplies a fixed value constraint, and is the base class for a number of othe...
dictionary dict
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:438
virtual void updateCoeffs()
Update the coefficients associated with the patch field.
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
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.
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.
virtual void evaluate(const Pstream::commsTypes commsType=Pstream::commsTypes::blocking)
Evaluate the patch field, sets Updated to false.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
A class for handling words, derived from string.
Definition: word.H:59
Foam::fvPatchFieldMapper.
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:53
A table of dynamically loaded libraries.
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
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
Constructs on-the-fly a new boundary condition (derived from fixedValueFvPatchField) which is then us...
Input from memory buffer stream.
Definition: IStringStream.H:49
codedFixedValueFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &)
Construct from patch and internal field.
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 write(Ostream &) const
Write.
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
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
const fvPatchField< Type > & redirectPatchField() const
Get reference to the underlying patch.