codedFixedValuePointPatchField.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) 2012-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 "pointPatchFieldMapper.H"
29 #include "pointFields.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::codedFixedValuePointPatchField<Type>::codeKeys_ =
38  {"code", "codeInclude", "localCode"};
39 
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 template<class Type>
44 const Foam::word Foam::codedFixedValuePointPatchField<Type>::codeTemplateC =
45  "fixedValuePointPatchFieldTemplate.C";
46 
47 template<class Type>
48 const Foam::word Foam::codedFixedValuePointPatchField<Type>::codeTemplateH =
49  "fixedValuePointPatchFieldTemplate.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 pointPatchField
63  dynCode.setFilterVariable("TemplateType", fieldType);
64 
65  // Name for pointPatchField - 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 const
76 {
77  const objectRegistry& obr = this->db();
78 
79  if (obr.foundObject<IOdictionary>("codeDict"))
80  {
81  return obr.lookupObject<IOdictionary>("codeDict");
82  }
83  else
84  {
85  return obr.store
86  (
87  new IOdictionary
88  (
89  IOobject
90  (
91  "codeDict",
92  this->db().time().system(),
93  this->db(),
94  IOobject::MUST_READ_IF_MODIFIED,
95  IOobject::NO_WRITE
96  )
97  )
98  );
99  }
100 }
101 
102 
103 template<class Type>
105 {
106  return const_cast<dlLibraryTable&>(this->db().time().libs());
107 }
108 
109 
110 template<class Type>
112 (
113  dynamicCode& dynCode,
114  const dynamicCodeContext& context
115 ) const
116 {
117  // Take no chances - typeName must be identical to name_
118  dynCode.setFilterVariable("typeName", name_);
119 
120  // Set TemplateType and FieldType filter variables
121  // (for pointPatchField)
122  setFieldTemplates(dynCode);
123 
124  // Compile filtered C template
125  dynCode.addCompileFile(codeTemplateC);
126 
127  // Copy filtered H template
128  dynCode.addCopyFile(codeTemplateH);
129 
130 
131  // Debugging: make BC verbose
132  // dynCode.setFilterVariable("verbose", "true");
133  // Info<<"compile " << name_ << " sha1: "
134  // << context.sha1() << endl;
135 
136  // Define Make/options
137  dynCode.setMakeOptions
138  (
139  "EXE_INC = -g \\\n"
140  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
141  + context.options()
142  + "\n\nLIB_LIBS = \\\n"
143  + " -lOpenFOAM \\\n"
144  + " -lfiniteVolume \\\n"
145  + context.libs()
146  );
147 }
148 
149 
150 template<class Type>
151 const Foam::dictionary&
153 {
154  // Use system/codeDict or in-line
155  return
156  (
157  dict_.found("code")
158  ? dict_
159  : this->dict().subDict(name_)
160  );
161 }
162 
163 
164 template<class Type>
165 const Foam::wordList&
167 {
168  return codeKeys_;
169 }
170 
171 
172 template<class Type>
174 {
175  return
176  "patch "
177  + this->patch().name()
178  + " on field "
179  + this->internalField().name();
180 }
181 
182 
183 template<class Type>
185 {
186  // Remove instantiation of pointPatchField provided by library
187  redirectPatchFieldPtr_.clear();
188 }
189 
190 
191 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
192 
193 template<class Type>
195 (
196  const pointPatch& p,
198 )
199 :
201  codedBase(),
202  redirectPatchFieldPtr_()
203 {}
204 
205 
206 template<class Type>
208 (
210  const pointPatch& p,
212  const pointPatchFieldMapper& mapper
213 )
214 :
215  fixedValuePointPatchField<Type>(ptf, p, iF, mapper),
216  codedBase(),
217  dict_(ptf.dict_),
218  name_(ptf.name_),
219  redirectPatchFieldPtr_()
220 {}
221 
222 
223 template<class Type>
225 (
226  const pointPatch& p,
228  const dictionary& dict
229 )
230 :
232  codedBase(),
233  dict_(dict),
234  name_
235  (
236  dict.found("redirectType")
237  ? dict.lookup("redirectType")
238  : dict.lookup("name")
239  ),
240  redirectPatchFieldPtr_()
241 {
242  updateLibrary(name_);
243 }
244 
245 
246 template<class Type>
248 (
250 )
251 :
253  codedBase(),
254  dict_(ptf.dict_),
255  name_(ptf.name_),
256  redirectPatchFieldPtr_()
257 {}
258 
259 
260 template<class Type>
262 (
265 )
266 :
268  codedBase(),
269  dict_(ptf.dict_),
270  name_(ptf.name_),
271  redirectPatchFieldPtr_()
272 {}
273 
274 
275 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
276 
277 template<class Type>
280 {
281  if (!redirectPatchFieldPtr_.valid())
282  {
283  // Construct a patch
284  // Make sure to construct the patchfield with up-to-date value
285 
286  OStringStream os;
287  writeEntry(os, "type", name_);
288  writeEntry(os, "value", static_cast<const Field<Type>&>(*this));
289  IStringStream is(os.str());
290  dictionary dict(is);
291 
292  redirectPatchFieldPtr_.set
293  (
295  (
296  this->patch(),
297  this->internalField(),
298  dict
299  ).ptr()
300  );
301  }
302  return redirectPatchFieldPtr_();
303 }
304 
305 
306 template<class Type>
308 {
309  if (this->updated())
310  {
311  return;
312  }
313 
314  // Make sure library containing user-defined pointPatchField is up-to-date
315  updateLibrary(name_);
316 
317  const pointPatchField<Type>& fvp = redirectPatchField();
318 
319  const_cast<pointPatchField<Type>&>(fvp).updateCoeffs();
320 
321  // Copy through value
322  this->operator==(fvp);
323 
325 }
326 
327 
328 template<class Type>
330 (
331  const Pstream::commsTypes commsType
332 )
333 {
334  // Make sure library containing user-defined pointPatchField is up-to-date
335  updateLibrary(name_);
336 
337  const pointPatchField<Type>& fvp = redirectPatchField();
338 
339  const_cast<pointPatchField<Type>&>(fvp).evaluate(commsType);
340 
342 }
343 
344 
345 template<class Type>
347 {
349  writeEntry(os, "name", name_);
350 
351  if (dict_.found("codeInclude"))
352  {
353  os.writeKeyword("codeInclude")
354  << token::HASH << token::BEGIN_BLOCK;
355 
356  os.writeQuoted(string(dict_["codeInclude"]), false)
357  << token::HASH << token::END_BLOCK
358  << token::END_STATEMENT << nl;
359  }
360 
361  if (dict_.found("localCode"))
362  {
363  os.writeKeyword("localCode")
364  << token::HASH << token::BEGIN_BLOCK;
365 
366  os.writeQuoted(string(dict_["localCode"]), false)
367  << token::HASH << token::END_BLOCK
368  << token::END_STATEMENT << nl;
369  }
370 
371  if (dict_.found("code"))
372  {
373  os.writeKeyword("code")
374  << token::HASH << token::BEGIN_BLOCK;
375 
376  os.writeQuoted(string(dict_["code"]), false)
377  << token::HASH << token::END_BLOCK
378  << token::END_STATEMENT << nl;
379  }
380 
381  if (dict_.found("codeOptions"))
382  {
383  os.writeKeyword("codeOptions")
384  << token::HASH << token::BEGIN_BLOCK;
385 
386  os.writeQuoted(string(dict_["codeOptions"]), false)
387  << token::HASH << token::END_BLOCK
388  << token::END_STATEMENT << nl;
389  }
390 
391  if (dict_.found("codeLibs"))
392  {
393  os.writeKeyword("codeLibs")
394  << token::HASH << token::BEGIN_BLOCK;
395 
396  os.writeQuoted(string(dict_["codeLibs"]), false)
397  << token::HASH << token::END_BLOCK
398  << token::END_STATEMENT << nl;
399  }
400 }
401 
402 
403 // ************************************************************************* //
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
codedFixedValuePointPatchField(const pointPatch &, const DimensionedField< Type, pointMesh > &)
Construct from patch and internal field.
commsTypes
Types of communications.
Definition: UPstream.H:64
A FixedValue boundary condition for pointField.
const pointPatchField< Type > & redirectPatchField() const
Get reference to the underlying patch.
Foam::pointPatchFieldMapper.
virtual void evaluate(const Pstream::commsTypes commsType=Pstream::commsTypes::blocking)
Evaluate the patch field, sets Updated to false.
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
Abstract base class for point-mesh patch fields.
void write(Ostream &, const label, const dictionary &)
Write with dictionary lookup.
Constructs on-the-fly a new boundary condition (derived from fixedValuePointPatchField) which is then...
Pre-declare SubField and related Field type.
Definition: Field.H:56
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
virtual void write(Ostream &) const
Write.
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
Basic pointPatch represents a set of points from the mesh.
Definition: pointPatch.H:56
Input from memory buffer stream.
Definition: IStringStream.H:49
virtual void updateCoeffs()
Update the coefficients associated with the patch 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.
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