patchExprDriverTemplates.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) 2019-2021 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
32 template<class Type>
35 (
36  const word& name
37 ) const
38 {
39  bool hasPointData = false;
40 
42 
43  if (hasVariable(name) && variable(name).isType<Type>())
44  {
45  tvar.cref(variable(name));
46  hasPointData = tvar().isPointData();
47  }
48  else if (isGlobalVariable<Type>(name))
49  {
50  tvar.cref(lookupGlobal(name));
51  }
52 
53 
54  if (tvar.valid())
55  {
56  const auto& var = tvar.cref();
57  const Field<Type>& vals = var.cref<Type>();
58 
59  const label len = (hasPointData ? this->pointSize() : this->size());
60 
61  if (returnReduce((vals.size() == len), andOp<bool>()))
62  {
63  // Return a copy of the field
64  return tmp<Field<Type>>::New(vals);
65  }
66 
67  if (!var.isUniform())
68  {
70  << "Variable " << name
71  << " is nonuniform and does not fit the size"
72  << ". Using average" << endl;
73  }
74 
75  return tmp<Field<Type>>::New(this->size(), gAverage(vals));
76  }
77 
78  return nullptr;
79 }
80 
81 
82 template<class Type>
85 {
86  return getField<Type>(name);
87 }
88 
89 
90 template<class Type>
93 {
94  return getField<Type>(name);
95 }
96 
97 
98 template<class Type>
101 {
102  return getField<Type>(name);
103 }
104 
105 
106 template<class Type>
109 {
110  tmp<Field<Type>> tfield = getVariableIfAvailable<Type>(name);
111 
112  if (tfield.valid())
113  {
114  return tfield;
115  }
116 
117  const objectRegistry& obr = this->mesh().thisDb();
118  const label patchIndex = patch_.index();
119 
120 
121  // Field types
122 
126 
127  // Local, temporary storage and/or lookup values
128  bool found = false;
129  tmp<vfieldType> vfield;
130  tmp<sfieldType> sfield;
131  tmp<pfieldType> pfield;
132 
133  for (int checki = 0; !found && checki < 2; ++checki)
134  {
135  // Check 0: object context (first)
136  // Check 1: regular objectRegistry
137  const regIOobject* ioptr =
138  (
139  (checki == 0)
141  : obr.cfindIOobject(name)
142  );
143  if (!ioptr) continue;
144 
145  if (!found)
146  {
147  vfield.cref(dynamic_cast<const vfieldType*>(ioptr));
148  found = vfield.valid();
149  }
150  if (!found)
151  {
152  sfield.cref(dynamic_cast<const sfieldType*>(ioptr));
153  found = sfield.valid();
154  }
155  if (!found)
156  {
157  pfield.cref(dynamic_cast<const pfieldType*>(ioptr));
158  found = pfield.valid();
159  }
160  }
161 
162 
163  // Finally, search files if necessary (and permitted)
164  if (!found && searchFiles())
165  {
166  const word fldType = this->getTypeOfField(name);
167 
168  if (fldType == vfieldType::typeName)
169  {
170  vfield = this->readAndRegister<vfieldType>(name, mesh());
171  }
172  else if (fldType == sfieldType::typeName)
173  {
174  sfield = this->readAndRegister<sfieldType>(name, mesh());
175  }
176  else if (fldType == pfieldType::typeName)
177  {
178  pfield = this->readAndRegister<pfieldType>
179  (
180  name,
182  );
183  }
184  }
185 
186 
187  if (vfield.valid())
188  {
189  return tmp<Field<Type>>::New
190  (
191  vfield().boundaryField()[patchIndex]
192  );
193  }
194  if (sfield.valid())
195  {
196  return tmp<Field<Type>>::New
197  (
198  sfield().boundaryField()[patchIndex]
199  );
200  }
201  if (pfield.valid())
202  {
203  return pfield().boundaryField()[patchIndex].patchInternalField();
204  }
205 
206 
208  << "No field '" << name << "' of type "
209  << pTraits<Type>::typeName << nl << nl;
210 
211  FatalError
212  << vfieldType::typeName << " Fields: "
213  << flatOutput(obr.sortedNames<vfieldType>()) << nl;
214 
215  FatalError
216  << sfieldType::typeName << " Fields: "
217  << flatOutput(obr.sortedNames<sfieldType>()) << nl;
218 
219  FatalError
220  << pfieldType::typeName << " Fields: "
221  << flatOutput(obr.sortedNames<pfieldType>()) << nl;
222 
223  FatalError
224  << exit(FatalError);
225 
226  return tmp<Field<Type>>::New();
227 }
228 
229 
230 template<class Type>
233 (
234  const word& name
235 )
236 {
237  tmp<Field<Type>> tfield = getVariableIfAvailable<Type>(name);
238 
239  if (tfield.valid())
240  {
241  return tfield;
242  }
243 
244  const objectRegistry& obr = this->mesh().thisDb();
245  const label patchIndex = patch_.index();
246 
247 
248  // Field types
249 
252 
253  // Local, temporary storage and/or lookup values
254  bool found = false;
255  tmp<vfieldType> vfield;
256  tmp<pfieldType> pfield;
257 
258  for (int checki = 0; !found && checki < 2; ++checki)
259  {
260  // Check 0: object context (first)
261  // Check 1: regular objectRegistry
262  const regIOobject* ioptr =
263  (
264  (checki == 0)
266  : obr.cfindIOobject(name)
267  );
268  if (!ioptr) continue;
269 
270  if (!found)
271  {
272  vfield.cref(dynamic_cast<const vfieldType*>(ioptr));
273  found = vfield.valid();
274  }
275  if (!found)
276  {
277  pfield.cref(dynamic_cast<const pfieldType*>(ioptr));
278  found = pfield.valid();
279  }
280  }
281 
282 
283  // Finally, search files if necessary (and permitted)
284  if (!found && searchFiles())
285  {
286  const word fldType = this->getTypeOfField(name);
287 
288  if (fldType == vfieldType::typeName)
289  {
290  vfield = this->readAndRegister<vfieldType>(name, mesh());
291  }
292  else if (fldType == pfieldType::typeName)
293  {
294  pfield = this->readAndRegister<pfieldType>
295  (
296  name,
298  );
299  }
300  }
301 
302 
303  if (vfield.valid())
304  {
305  return vfield().boundaryField()[patchIndex].patchInternalField();
306  }
307  if (pfield.valid())
308  {
309  return pfield().boundaryField()[patchIndex].patchInternalField();
310  }
311 
312 
314  << "No field '" << name << "' of type "
315  << pTraits<Type>::typeName << nl << nl;
316 
317  FatalError
318  << vfieldType::typeName << " Fields: "
319  << flatOutput(obr.sortedNames<vfieldType>()) << nl;
320 
321  FatalError
322  << pfieldType::typeName << " Fields: "
323  << flatOutput(obr.sortedNames<pfieldType>()) << nl;
324 
325  FatalError
326  << exit(FatalError);
327 
328  return tmp<Field<Type>>::New();
329 }
330 
331 
332 template<class Type>
335 (
336  const word& name
337 )
338 {
339  tmp<Field<Type>> tfield = getVariableIfAvailable<Type>(name);
340 
341  if (tfield.valid())
342  {
343  return tfield;
344  }
345 
346  const objectRegistry& obr = this->mesh().thisDb();
347  const label patchIndex = patch_.index();
348 
349 
350  // Field types
351 
353 
354  // Local, temporary storage and/or lookup values
355  bool found = false;
356  tmp<vfieldType> vfield;
357 
358  for (int checki = 0; !found && checki < 2; ++checki)
359  {
360  // Check 0: object context (first)
361  // Check 1: regular objectRegistry
362  const regIOobject* ioptr =
363  (
364  (checki == 0)
366  : obr.cfindIOobject(name)
367  );
368  if (!ioptr) continue;
369 
370  if (!found)
371  {
372  vfield.cref(dynamic_cast<const vfieldType*>(ioptr));
373  found = vfield.valid();
374  }
375  }
376 
377 
378  // Finally, search files if necessary (and permitted)
379  if (!found && searchFiles())
380  {
381  const word fldType = this->getTypeOfField(name);
382 
383  if (fldType == vfieldType::typeName)
384  {
385  vfield = this->readAndRegister<vfieldType>(name, mesh());
386  }
387  }
388 
389 
390  if (vfield.valid())
391  {
392  return vfield().boundaryField()[patchIndex].patchNeighbourField();
393  }
394 
395 
397  << "No field '" << name << "' of type "
398  << pTraits<Type>::typeName << nl << nl;
399 
400  FatalError
401  << vfieldType::typeName << " Fields: "
402  << flatOutput(obr.sortedNames<vfieldType>()) << nl;
403 
404  FatalError
405  << exit(FatalError);
406 
407  return tmp<Field<Type>>::New();
408 }
409 
410 
411 template<class Type>
414 (
415  const word& name
416 )
417 {
418  tmp<Field<Type>> tfield = getVariableIfAvailable<Type>(name);
419 
420  if (tfield.valid())
421  {
422  return tfield;
423  }
424 
425  const objectRegistry& obr = this->mesh().thisDb();
426  const label patchIndex = patch_.index();
427 
428 
429  // Field types
430 
432 
433  // Local, temporary storage and/or lookup values
434  bool found = false;
435  tmp<vfieldType> vfield;
436 
437  for (int checki = 0; !found && checki < 2; ++checki)
438  {
439  // Check 0: object context (first)
440  // Check 1: regular objectRegistry
441  const regIOobject* ioptr =
442  (
443  (checki == 0)
445  : obr.cfindIOobject(name)
446  );
447  if (!ioptr) continue;
448 
449  if (!found)
450  {
451  vfield.cref(dynamic_cast<const vfieldType*>(ioptr));
452  found = vfield.valid();
453  }
454  }
455 
456 
457  // Finally, search files if necessary (and permitted)
458  if (!found && searchFiles())
459  {
460  const word fldType = this->getTypeOfField(name);
461 
462  if (fldType == vfieldType::typeName)
463  {
464  vfield = this->readAndRegister<vfieldType>(name, mesh());
465  }
466  }
467 
468 
469  if (vfield.valid())
470  {
471  return vfield().boundaryField()[patchIndex].snGrad();
472  }
473 
474 
476  << "No field '" << name << "' of type "
477  << pTraits<Type>::typeName << nl << nl;
478 
479  FatalError
480  << vfieldType::typeName << " Fields: "
481  << flatOutput(obr.sortedNames<vfieldType>()) << nl;
482 
483  FatalError
484  << exit(FatalError);
485 
486  return tmp<Field<Type>>::New();
487 }
488 
489 
490 template<class Type>
493 (
494  const Field<Type>& field
495 ) const
496 {
497  primitivePatchInterpolation interp(patch_.patch());
498 
499  return interp.faceToPointInterpolate(field);
500 }
501 
502 
503 template<class Type>
506 (
507  const Field<Type>& field
508 ) const
509 {
510  primitivePatchInterpolation interp(patch_.patch());
511 
512  return interp.pointToFaceInterpolate(field);
513 }
514 
515 
516 // ************************************************************************* //
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:153
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
Foam::gAverage
Type gAverage(const FieldField< Field, Type > &f)
Definition: FieldFieldFunctions.C:604
Foam::expressions::patchExpr::parseDriver::getVariableIfAvailable
tmp< Field< Type > > getVariableIfAvailable(const word &fldName) const
Retrieve variable as field if possible.
Foam::PrimitivePatchInterpolation::pointToFaceInterpolate
tmp< Field< Type > > pointToFaceInterpolate(const Field< Type > &pf) const
Interpolate from points to faces.
Definition: PrimitivePatchInterpolation.C:234
Foam::MeshObject< polyMesh, UpdateableMeshObject, pointMesh >::New
static const pointMesh & New(const polyMesh &mesh, Args &&... args)
Get existing or create a new MeshObject.
Definition: MeshObject.C:48
Foam::expressions::patchExpr::parseDriver::getField
tmp< Field< Type > > getField(const word &fldName)
Return named field.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
primitivePatchInterpolation.H
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::refPtr::valid
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: refPtr.H:300
Foam::expressions::patchExpr::parseDriver::getSurfaceField
tmp< Field< Type > > getSurfaceField(const word &fldName)
Retrieve field (surface field)
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::refPtr::cref
const T & cref() const
Definition: refPtrI.H:189
Foam::objectRegistry::cfindIOobject
const regIOobject * cfindIOobject(const word &name, const bool recursive=false) const
Return const pointer to the regIOobject.
Definition: objectRegistry.C:411
Foam::andOp
Definition: ops.H:233
field
rDeltaTY field()
Foam::expressions::patchExpr::parseDriver::patchInternalField
tmp< Field< Type > > patchInternalField(const word &fldName)
Return internal field next to patch.
Foam::expressions::patchExpr::parseDriver::patchNormalField
tmp< Field< Type > > patchNormalField(const word &fldName)
Return surface normal field (snGrad)
Foam::expressions::patchExpr::parseDriver::patchNeighbourField
tmp< Field< Type > > patchNeighbourField(const word &fldName)
Return patchField on the opposite patch of a coupled patch.
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::PrimitivePatchInterpolation::faceToPointInterpolate
tmp< Field< Type > > faceToPointInterpolate(const Field< Type > &ff) const
Interpolate from faces to points.
Definition: PrimitivePatchInterpolation.C:177
Foam::expressions::patchExpr::parseDriver::pointToFace
tmp< Field< Type > > pointToFace(const Field< Type > &field) const
Interpolate point to face values.
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::expressions::exprDriver::cfindContextIOobject
const regIOobject * cfindContextIOobject(const word &name) const
Find named context field, if it exists.
Definition: exprDriverContextI.H:38
Foam::PrimitivePatchInterpolation
Interpolation class within a primitive patch. Allows interpolation from points to faces and vice vers...
Definition: PrimitivePatchInterpolation.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::expressions::patchExpr::parseDriver::getVolField
tmp< Field< Type > > getVolField(const word &fldName)
Retrieve field (vol field)
Foam::expressions::patchExpr::parseDriver::faceToPoint
tmp< Field< Type > > faceToPoint(const Field< Type > &field) const
Interpolate face to point.
Foam::tmp::valid
bool valid() const noexcept
Identical to good(), or bool operator.
Definition: tmp.H:292
Foam::expressions::patchExpr::parseDriver::getPointField
tmp< Field< Type > > getPointField(const word &fldName)
Retrieve field (point field)
Foam::GeometricField< Type, fvPatchField, volMesh >
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::tmp::cref
const T & cref() const
Definition: tmpI.H:213
Foam::refPtr
A class for managing references or pointers (no reference counting)
Definition: PtrList.H:60