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-2018 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 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 template<class Type>
37 const Foam::word Foam::codedFixedValuePointPatchField<Type>::codeTemplateC
38  = "fixedValuePointPatchFieldTemplate.C";
39 
40 template<class Type>
41 const Foam::word Foam::codedFixedValuePointPatchField<Type>::codeTemplateH
42  = "fixedValuePointPatchFieldTemplate.H";
43 
44 
45 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
46 
47 template<class Type>
49 (
50  dynamicCode& dynCode
51 )
52 {
53  word fieldType(pTraits<Type>::typeName);
54 
55  // Template type for pointPatchField
56  dynCode.setFilterVariable("TemplateType", fieldType);
57 
58  // Name for pointPatchField - eg, ScalarField, VectorField, ...
59  fieldType[0] = toupper(fieldType[0]);
60  dynCode.setFilterVariable("FieldType", fieldType + "Field");
61 }
62 
63 
64 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
65 
66 template<class Type>
68 const
69 {
70  const objectRegistry& obr = this->db();
71 
72  if (obr.foundObject<IOdictionary>("codeDict"))
73  {
74  return obr.lookupObject<IOdictionary>("codeDict");
75  }
76  else
77  {
78  return obr.store
79  (
80  new IOdictionary
81  (
82  IOobject
83  (
84  "codeDict",
85  this->db().time().system(),
86  this->db(),
87  IOobject::MUST_READ_IF_MODIFIED,
88  IOobject::NO_WRITE
89  )
90  )
91  );
92  }
93 }
94 
95 
96 template<class Type>
98 {
99  return const_cast<dlLibraryTable&>(this->db().time().libs());
100 }
101 
102 
103 template<class Type>
105 (
106  dynamicCode& dynCode,
107  const dynamicCodeContext& context
108 ) const
109 {
110  // Take no chances - typeName must be identical to name_
111  dynCode.setFilterVariable("typeName", name_);
112 
113  // Set TemplateType and FieldType filter variables
114  // (for pointPatchField)
115  setFieldTemplates(dynCode);
116 
117  // Compile filtered C template
118  dynCode.addCompileFile(codeTemplateC);
119 
120  // Copy filtered H template
121  dynCode.addCopyFile(codeTemplateH);
122 
123 
124  // Debugging: make BC verbose
125  // dynCode.setFilterVariable("verbose", "true");
126  // Info<<"compile " << name_ << " sha1: "
127  // << context.sha1() << endl;
128 
129  // Define Make/options
130  dynCode.setMakeOptions
131  (
132  "EXE_INC = -g \\\n"
133  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
134  + context.options()
135  + "\n\nLIB_LIBS = \\\n"
136  + " -lOpenFOAM \\\n"
137  + " -lfiniteVolume \\\n"
138  + context.libs()
139  );
140 }
141 
142 
143 template<class Type>
145 const
146 {
147  // Use system/codeDict or in-line
148  return
149  (
150  dict_.found("code")
151  ? dict_
152  : this->dict().subDict(name_)
153  );
154 }
155 
156 
157 template<class Type>
159 {
160  return
161  "patch "
162  + this->patch().name()
163  + " on field "
164  + this->internalField().name();
165 }
166 
167 
168 template<class Type>
170 {
171  // Remove instantiation of pointPatchField provided by library
172  redirectPatchFieldPtr_.clear();
173 }
174 
175 
176 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
177 
178 template<class Type>
180 (
181  const pointPatch& p,
183 )
184 :
186  codedBase(),
187  redirectPatchFieldPtr_()
188 {}
189 
190 
191 template<class Type>
193 (
195  const pointPatch& p,
197  const pointPatchFieldMapper& mapper
198 )
199 :
200  fixedValuePointPatchField<Type>(ptf, p, iF, mapper),
201  codedBase(),
202  dict_(ptf.dict_),
203  name_(ptf.name_),
204  redirectPatchFieldPtr_()
205 {}
206 
207 
208 template<class Type>
210 (
211  const pointPatch& p,
213  const dictionary& dict,
214  const bool valueRequired
215 )
216 :
217  fixedValuePointPatchField<Type>(p, iF, dict, valueRequired),
218  codedBase(),
219  dict_(dict),
220  name_
221  (
222  dict.found("redirectType")
223  ? dict.lookup("redirectType")
224  : dict.lookup("name")
225  ),
226  redirectPatchFieldPtr_()
227 {
228  updateLibrary(name_);
229 }
230 
231 
232 template<class Type>
234 (
236 )
237 :
239  codedBase(),
240  dict_(ptf.dict_),
241  name_(ptf.name_),
242  redirectPatchFieldPtr_()
243 {}
244 
245 
246 template<class Type>
248 (
251 )
252 :
254  codedBase(),
255  dict_(ptf.dict_),
256  name_(ptf.name_),
257  redirectPatchFieldPtr_()
258 {}
259 
260 
261 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
262 
263 template<class Type>
266 {
267  if (!redirectPatchFieldPtr_.valid())
268  {
269  // Construct a patch
270  // Make sure to construct the patchfield with up-to-date value
271 
272  OStringStream os;
273  os.writeKeyword("type") << name_ << token::END_STATEMENT
274  << nl;
275  static_cast<const Field<Type>&>(*this).writeEntry("value", os);
276  IStringStream is(os.str());
277  dictionary dict(is);
278 
279  redirectPatchFieldPtr_.set
280  (
282  (
283  this->patch(),
284  this->internalField(),
285  dict
286  ).ptr()
287  );
288  }
289  return redirectPatchFieldPtr_();
290 }
291 
292 
293 template<class Type>
295 {
296  if (this->updated())
297  {
298  return;
299  }
300 
301  // Make sure library containing user-defined pointPatchField is up-to-date
302  updateLibrary(name_);
303 
304  const pointPatchField<Type>& fvp = redirectPatchField();
305 
306  const_cast<pointPatchField<Type>&>(fvp).updateCoeffs();
307 
308  // Copy through value
309  this->operator==(fvp);
310 
312 }
313 
314 
315 template<class Type>
317 (
318  const Pstream::commsTypes commsType
319 )
320 {
321  // Make sure library containing user-defined pointPatchField is up-to-date
322  updateLibrary(name_);
323 
324  const pointPatchField<Type>& fvp = redirectPatchField();
325 
326  const_cast<pointPatchField<Type>&>(fvp).evaluate(commsType);
327 
329 }
330 
331 
332 template<class Type>
334 {
336  os.writeKeyword("name") << name_
337  << token::END_STATEMENT << nl;
338 
339  if (dict_.found("codeInclude"))
340  {
341  os.writeKeyword("codeInclude")
342  << token::HASH << token::BEGIN_BLOCK;
343 
344  os.writeQuoted(string(dict_["codeInclude"]), false)
345  << token::HASH << token::END_BLOCK
346  << token::END_STATEMENT << nl;
347  }
348 
349  if (dict_.found("localCode"))
350  {
351  os.writeKeyword("localCode")
352  << token::HASH << token::BEGIN_BLOCK;
353 
354  os.writeQuoted(string(dict_["localCode"]), false)
355  << token::HASH << token::END_BLOCK
356  << token::END_STATEMENT << nl;
357  }
358 
359  if (dict_.found("code"))
360  {
361  os.writeKeyword("code")
362  << token::HASH << token::BEGIN_BLOCK;
363 
364  os.writeQuoted(string(dict_["code"]), false)
365  << token::HASH << token::END_BLOCK
366  << token::END_STATEMENT << nl;
367  }
368 
369  if (dict_.found("codeOptions"))
370  {
371  os.writeKeyword("codeOptions")
372  << token::HASH << token::BEGIN_BLOCK;
373 
374  os.writeQuoted(string(dict_["codeOptions"]), false)
375  << token::HASH << token::END_BLOCK
376  << token::END_STATEMENT << nl;
377  }
378 
379  if (dict_.found("codeLibs"))
380  {
381  os.writeKeyword("codeLibs")
382  << token::HASH << token::BEGIN_BLOCK;
383 
384  os.writeQuoted(string(dict_["codeLibs"]), false)
385  << token::HASH << token::END_BLOCK
386  << token::END_STATEMENT << nl;
387  }
388 }
389 
390 
391 // ************************************************************************* //
dictionary dict
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
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.
void writeEntry(const word &keyword, Ostream &os) const
Write the field as a dictionary entry.
Definition: Field.C:726
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:57
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
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:1217