ENH: Added new sample Function1

This commit is contained in:
Andrew Heather
2021-10-13 21:31:43 +01:00
committed by Mark Olesen
parent bc2b469f9d
commit b9b011f5ea
4 changed files with 398 additions and 0 deletions

View File

@ -0,0 +1,185 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "SampleFunction1.H"
#include "volFields.H"
#include "interpolation.H"
#include "pointIOField.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type>
void Foam::Function1Types::Sample<Type>::setSampleCell() const
{
const polyMesh& mesh = this->template mesh<polyMesh>();
const auto& points = static_cast<const pointIOField&>(mesh.points());
if (pointEventNo_ < points.eventNo())
{
pointEventNo_ = points.eventNo();
celli_ = this->template mesh<fvMesh>().findCell(position_);
if (!returnReduce(celli_ != -1, orOp<bool>()))
{
FatalErrorInFunction
<< "Sample cell could not be found at position "
<< position_ << nl
<< exit(FatalError);
}
if (debug)
{
Pout<< "Position: " << position_
<< " celli:" << celli_
<< " eventNo:" << pointEventNo_
<< " points eventNo:" << points.eventNo()
<< endl;
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Sample<Type>::Sample
(
const word& entryName,
const dictionary& dict,
const objectRegistry* obrPtr
)
:
Function1<Type>(entryName, dict, obrPtr),
fieldName_(dict.get<word>("field")),
position_(dict.get<point>("position")),
interpolationScheme_
(
dict.getOrDefault<word>("interpolationScheme", "cell")
),
celli_(-1),
pointEventNo_(-1)
{}
template<class Type>
Foam::Function1Types::Sample<Type>::Sample(const Sample& s)
:
Function1<Type>(s),
fieldName_(s.fieldName_),
position_(s.position_),
interpolationScheme_(s.interpolationScheme_),
celli_(s.celli_),
pointEventNo_(s.pointEventNo_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Type Foam::Function1Types::Sample<Type>::value(const scalar x) const
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
const auto& mesh = this->template mesh<fvMesh>();
const auto* fieldPtr = mesh.template cfindObject<VolFieldType>(fieldName_);
if (!fieldPtr)
{
FatalErrorInFunction
<< "Unable to find field " << fieldName_ << " on the mesh database"
<< ". Valid " << VolFieldType::typeName << " fields are:"
<< mesh.names(VolFieldType::typeName)
<< exit(FatalError);
}
// Might trigger parallel comms (e.g. volPointInterpolation, if
// result is not yet cached) so have all processors do it
autoPtr<interpolation<Type>> interpolator
(
interpolation<Type>::New(interpolationScheme_, *fieldPtr)
);
Type result = pTraits<Type>::min;
setSampleCell();
if (celli_ != -1)
{
result = interpolator().interpolate(position_, celli_, -1);
}
reduce(result, maxOp<Type>());
DebugInfo << "sampled value: " << result << endl;
return result;
}
template<class Type>
Type Foam::Function1Types::Sample<Type>::integrate
(
const scalar x1,
const scalar x2
) const
{
NotImplemented;
return Zero;
}
template<class Type>
void Foam::Function1Types::Sample<Type>::writeEntries(Ostream& os) const
{
os.writeEntry("field", fieldName_);
os.writeEntry("position", position_);
os.writeEntryIfDifferent<word>
(
"interpolationScheme", "cell", interpolationScheme_
);
}
template<class Type>
void Foam::Function1Types::Sample<Type>::writeData(Ostream& os) const
{
Function1<Type>::writeData(os);
os.endEntry();
os.beginBlock(word(this->name() + "Coeffs"));
writeEntries(os);
os.endBlock();
}
// ************************************************************************* //

View File

@ -0,0 +1,166 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::Function1Types::sample
Description
Minimal example by using \c system/controlDict.functions:
\verbatim
\verbatim
<entryName> sample;
<entryName>Coeffs
{
field <field name>;
position (0 0 0);
interpolationScheme cell;
}
\endverbatim
SourceFiles
sampleFunction1.C
\*---------------------------------------------------------------------------*/
#ifndef Function1Types_Sample_H
#define Function1Types_Sample_H
#include "Function1.H"
#include "point.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace Function1Types
{
/*---------------------------------------------------------------------------*\
Class Sample Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class Sample
:
public Function1<Type>
{
// Private Data
//- Name of field to sample
const word fieldName_;
//- Sample position
const point position_;
//- Interpolation scheme name; default = "cell"
const word interpolationScheme_;
//- Sample cell
mutable label celli_;
//- Points event number used to determine if celli should be updated
mutable label pointEventNo_;
// Private Member Functions
//- Set the sample celli; error on not found
void setSampleCell() const;
//- No copy assignment
void operator=(const Sample<Type>&) = delete;
public:
//- Runtime type information
TypeName("sample");
// Constructors
//- Construct from entry name, dictionary and optional registry
Sample
(
const word& entryName,
const dictionary& dict,
const objectRegistry* obrPtr = nullptr
);
//- Construct from components
Sample
(
const word& entryName,
const List<Tuple2<Type, Type>>& coeffs,
const objectRegistry* obrPtr = nullptr
);
//- Copy constructor
explicit Sample(const Sample& poly);
//- Construct and return a clone
virtual tmp<Function1<Type>> clone() const
{
return tmp<Function1<Type>>(new Sample<Type>(*this));
}
//- Destructor
virtual ~Sample() = default;
// Member Functions
//- Return Sample value
virtual Type value(const scalar x) const;
//- Integrate between two (scalar) values
virtual Type integrate(const scalar x1, const scalar x2) const;
//- Write as primitive (inline) format
virtual void writeData(Ostream& os) const;
//- Write coefficient entries in dictionary format
void writeEntries(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Function1Types
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "SampleFunction1.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,46 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "SampleFunction1.H"
#include "fieldTypes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#define makeFunction1s(Type) \
makeFunction1Type(Sample, Type);
namespace Foam
{
makeFunction1s(scalar);
makeFunction1s(vector);
makeFunction1s(sphericalTensor);
makeFunction1s(symmTensor);
makeFunction1s(tensor);
}
// ************************************************************************* //