LookupField.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) 2024 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 
28 #include "volFields.H"
29 #include "volMesh.H"
30 #include "surfaceFields.H"
31 #include "surfaceMesh.H"
32 #include "pointFields.H"
33 #include "pointMesh.H"
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
38 template<class Type>
40 (
41  const polyPatch& pp,
42  const word& redirectType,
43  const word& entryName,
44  const dictionary& dict,
45  const bool faceValues
46 )
47 :
48  PatchFunction1<Type>(pp, entryName, dict, faceValues),
50 {}
51 
52 
53 template<class Type>
55 (
56  const LookupField<Type>& rhs,
57  const polyPatch& pp
58 )
59 :
60  PatchFunction1<Type>(rhs, pp),
61  lookupBase(rhs)
62 {}
63 
64 
65 template<class Type>
67 (
68  const LookupField<Type>& rhs
69 )
70 :
71  LookupField<Type>(rhs, rhs.patch())
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76 
77 template<class Type>
80 {
81  typedef UniformDimensionedField<Type> UType;
82 
83  const objectRegistry& db = patchFunction1Base::obr();
84  const label patchi = patchFunction1Base::patch().index();
85 
86  if (this->faceValues())
87  {
90 
91  // Try:
92  // - as volField in local scope
93  // - as surfaceField in local scope
94  // - as UniformDimensionedField recursively
95 
96  const regIOobject* ptr = db.cfindIOobject(name_, false);
97 
98  if (ptr)
99  {
100  const auto* vPtr = dynamic_cast<const VType*>(ptr);
101  if (vPtr)
102  {
103  return vPtr->boundaryField()[patchi];
104  }
105 
106  const auto* sPtr = dynamic_cast<const SType*>(ptr);
107  if (sPtr)
108  {
109  return sPtr->boundaryField()[patchi];
110  }
111 
112  const auto* uPtr = dynamic_cast<const UType*>(ptr);
113  if (uPtr)
114  {
115  return Field<Type>(this->size(), uPtr->value());
116  }
117  }
118 
119  // Done db level. Try recursion
120  ptr = db.parent().cfindIOobject(name_, true);
121 
122  if (ptr)
123  {
124  const auto* uPtr = dynamic_cast<const UType*>(ptr);
125  if (uPtr)
126  {
127  return Field<Type>(this->size(), uPtr->value());
128  }
129  }
130 
132  << nl
133  << " failed lookup of " << name_
134  << " (objectRegistry "
135  << db.name()
136  << ")\n available objects of type " << VType::typeName
137  << ':' << nl
138  << db.names<VType>() << nl
139  << " available objects of type " << SType::typeName
140  << ':' << nl
141  << db.names<SType>() << nl
142  << " available objects of type " << UType::typeName
143  << ':' << nl
144  << db.names<UType>() << nl
145  << exit(FatalError);
146  return Field<Type>::null();
147  }
148  else
149  {
150  // Assume pointField
151  typedef GeometricField<Type, pointPatchField, pointMesh> PType;
152 
153  const regIOobject* ptr = db.cfindIOobject(name_, false);
154 
155  if (ptr)
156  {
157  const auto* pPtr = dynamic_cast<const PType*>(ptr);
158  if (pPtr)
159  {
160  return pPtr->boundaryField()[patchi].patchInternalField();
161  }
162  }
163 
164  // Re-do as uniform field. Note: could repeat logic above
165  const auto& obj = db.lookupObject<UType>(name_, true);
166 
167  return Field<Type>(this->size(), obj.value());
168  }
169 }
170 
171 
172 template<class Type>
175 (
176  const scalar x1,
177  const scalar x2
178 ) const
179 {
180  return (x2-x1)*value(0.5*(x1+x2));
181 }
182 
183 
184 template<class Type>
186 (
187  Ostream& os
188 ) const
189 {
191  os.writeEntry(this->name(), type());
192  os.beginBlock(word(this->name() + "Coeffs"));
193  writeEntries(os);
194  os.endBlock();
195 }
196 
197 
198 // ************************************************************************* //
Foam::surfaceFields.
dictionary dict
LookupField(const polyPatch &pp, const word &redirectType, const word &entryName, const dictionary &dict, const bool faceValues=true)
Construct from entry name, dictionary and optional registry.
Definition: LookupField.C:33
const Type & lookupObject(const word &name, const bool recursive=false) const
Lookup and return const reference to the object of the given Type. Fatal if not found or the wrong ty...
type
Types of root.
Definition: Roots.H:52
virtual tmp< Field< Type > > value(const scalar x) const
Return Lookup value.
Definition: LookupField.C:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
wordList names() const
The unsorted names of all objects.
Generic GeometricField class.
const bool writeData(pdfDictionary.get< bool >("writeData"))
PatchFunction1 to lookup volField/surfaceField or pointField from an objectregistry and return its va...
Definition: LookupField.H:85
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: LookupField.C:179
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Base level of Lookup and LookupField classes.
Definition: lookupBase.H:46
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
virtual tmp< Field< Type > > integrate(const scalar x1, const scalar x2) const
Integrate between two values.
Definition: LookupField.C:168
OBJstream os(runTime.globalPath()/outputName)
Dimensioned<Type> registered with the database as a registered IOobject which has the functionality o...
const objectRegistry & parent() const noexcept
Return the parent objectRegistry.
const std::string patch
OpenFOAM patch number as a std::string.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
A class for managing temporary objects.
Definition: HashPtrTable.H:50
Registry of regIOobjects.
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:69
const regIOobject * cfindIOobject(const word &name, const bool recursive=false) const
Return const pointer to the regIOobject.
uindirectPrimitivePatch pp(UIndirectList< face >(mesh.faces(), faceLabels), mesh.points())