diff --git a/src/sampling/Make/files b/src/sampling/Make/files index 22c32f0eeb11f19d3686dc45e44ecb79d113178e..ad272d0e2094b195b38454bccd2e29605b58ce67 100644 --- a/src/sampling/Make/files +++ b/src/sampling/Make/files @@ -80,5 +80,6 @@ meshToMesh0/meshToMesh0.C meshToMesh0/calculateMeshToMesh0Addressing.C meshToMesh0/calculateMeshToMesh0Weights.C +functions/Function1/makeFunction1s.C LIB = $(FOAM_LIBBIN)/libsampling diff --git a/src/sampling/functions/Function1/Sample/SampleFunction1.C b/src/sampling/functions/Function1/Sample/SampleFunction1.C new file mode 100644 index 0000000000000000000000000000000000000000..d617ec7d359060b9e6f612d9da480e8c55afd27e --- /dev/null +++ b/src/sampling/functions/Function1/Sample/SampleFunction1.C @@ -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(); +} + + +// ************************************************************************* // diff --git a/src/sampling/functions/Function1/Sample/SampleFunction1.H b/src/sampling/functions/Function1/Sample/SampleFunction1.H new file mode 100644 index 0000000000000000000000000000000000000000..11e27aadc451a5f28e03043d2bc79e40a45016f9 --- /dev/null +++ b/src/sampling/functions/Function1/Sample/SampleFunction1.H @@ -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 + +// ************************************************************************* // diff --git a/src/sampling/functions/Function1/makeFunction1s.C b/src/sampling/functions/Function1/makeFunction1s.C new file mode 100644 index 0000000000000000000000000000000000000000..7739a872e7e33d929469fd497b28d2c356af26c6 --- /dev/null +++ b/src/sampling/functions/Function1/makeFunction1s.C @@ -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); +} + + +// ************************************************************************* //