SampleFunction1.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) 2021-2022 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 "SampleFunction1.H"
29 #include "volFields.H"
30 #include "interpolation.H"
31 #include "pointIOField.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 template<class Type>
37 {
38  const polyMesh& mesh = this->template mesh<polyMesh>();
39 
40  const auto& points = static_cast<const pointIOField&>(mesh.points());
41 
42  if (pointEventNo_ < points.eventNo())
43  {
44  pointEventNo_ = points.eventNo();
45 
46  celli_ = mesh.findCell(position_);
47 
48  if (returnReduceAnd(celli_ < 0))
49  {
51  << "Sample cell could not be found at position "
52  << position_ << nl
53  << exit(FatalError);
54  }
55 
56  if (debug)
57  {
58  Pout<< "Position: " << position_
59  << " celli:" << celli_
60  << " eventNo:" << pointEventNo_
61  << " points eventNo:" << points.eventNo()
62  << endl;
63  }
64  }
65 }
66 
67 
68 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
69 
70 template<class Type>
72 (
73  const word& entryName,
74  const dictionary& dict,
75  const objectRegistry* obrPtr
76 )
77 :
78  Function1<Type>(entryName, dict, obrPtr),
79  fieldName_(dict.get<word>("field")),
80  position_(dict.get<point>("position")),
81  interpolationScheme_
82  (
83  dict.getOrDefault<word>("interpolationScheme", "cell")
84  ),
85  celli_(-1),
86  pointEventNo_(-1)
87 {}
88 
89 
90 template<class Type>
92 :
93  Function1<Type>(s),
94  fieldName_(s.fieldName_),
95  position_(s.position_),
96  interpolationScheme_(s.interpolationScheme_),
97  celli_(s.celli_),
98  pointEventNo_(s.pointEventNo_)
99 {}
100 
101 
102 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
103 
104 template<class Type>
105 Type Foam::Function1Types::Sample<Type>::value(const scalar x) const
106 {
107  const auto& mesh = this->template mesh<fvMesh>();
108 
109  const auto* fieldPtr =
110  mesh.template cfindObject<VolumeField<Type>>(fieldName_);
111 
112  if (!fieldPtr)
113  {
115  << "Unable to find field " << fieldName_ << " on the mesh database"
116  << ". Valid " << VolumeField<Type>::typeName << " fields are:"
117  << mesh.template sortedNames<VolumeField<Type>>()
118  << exit(FatalError);
119  }
120 
121 
122  // Might trigger parallel comms (e.g. volPointInterpolation, if
123  // result is not yet cached) so have all processors do it
124  autoPtr<interpolation<Type>> interpolator
125  (
126  interpolation<Type>::New(interpolationScheme_, *fieldPtr)
127  );
128 
129  Type result = pTraits<Type>::min;
130 
131  setSampleCell();
132 
133  if (celli_ != -1)
134  {
135  result = interpolator().interpolate(position_, celli_, -1);
136  }
137 
138  reduce(result, maxOp<Type>());
139 
140  DebugInfo << "sampled value: " << result << endl;
142  return result;
143 }
144 
145 
146 template<class Type>
148 (
149  const scalar x1,
150  const scalar x2
151 ) const
152 {
154 
155  return Zero;
156 }
157 
158 
159 template<class Type>
161 {
162  os.writeEntry("field", fieldName_);
163  os.writeEntry("position", position_);
164 
165  os.writeEntryIfDifferent<word>
166  (
167  "interpolationScheme", "cell", interpolationScheme_
168  );
169 }
170 
171 
172 template<class Type>
174 {
176  os.endEntry();
177 
178  os.beginBlock(word(this->name() + "Coeffs"));
179  writeEntries(os);
180  os.endBlock();
181 }
182 
183 
184 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
Top level data entry class for use in dictionaries. Provides a mechanism to specify a variable as a c...
dictionary dict
virtual Type integrate(const scalar x1, const scalar x2) const
Integrate between two (scalar) values.
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:598
vectorIOField pointIOField
pointIOField is a vectorIOField.
Definition: pointIOField.H:38
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
virtual void writeData(Ostream &os) const
Write in dictionary format.
Definition: Function1.C:167
bool returnReduceAnd(const bool value, const label comm=UPstream::worldComm)
Perform logical (and) MPI Allreduce on a copy. Uses UPstream::reduceAnd.
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
const pointField & points
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual Type value(const scalar x) const
Return Sample value.
#define DebugInfo
Report an information message using Foam::Info.
virtual void writeEntries(Ostream &os) const
Write coefficient entries in dictionary format.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
Abstract base class for volume field interpolation.
virtual void writeData(Ostream &os) const
Write as primitive (inline) format.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Registry of regIOobjects.
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
Sample(const word &entryName, const dictionary &dict, const objectRegistry *obrPtr=nullptr)
Construct from entry name, dictionary and optional registry.
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127