Commit 3cb12d2d authored by mattijs's avatar mattijs
Browse files

ENH: PatchFunction1: have 'sampled' type.

type sampled is the equivalent of 'mappedField' bc
functionality - it samples a registered field on a
different region/patch/cells.
parent 192e2eeb
......@@ -224,6 +224,9 @@ $(derivedFvPatchFields)/plenumPressure/plenumPressureFvPatchScalarField.C
$(derivedFvPatchFields)/interfaceCompression/interfaceCompressionFvPatchScalarField.C
$(derivedFvPatchFields)/swirlFanVelocity/swirlFanVelocityFvPatchField.C
$(derivedFvPatchFields)/mappedField/Sampled/makeSampledPatchFunction1s.C
fvsPatchFields = fields/fvsPatchFields
$(fvsPatchFields)/fvsPatchField/fvsPatchFields.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "fvMesh.H"
#include "volFields.H"
#include "interpolationCell.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
template<class Type>
Type Foam::PatchFunction1Types::Sampled<Type>::getAverage
(
const dictionary& dict,
const bool mandatory
)
{
if (mandatory)
{
return dict.get<Type>("average");
}
return Zero;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::PatchFunction1Types::Sampled<Type>::Sampled
(
const polyPatch& pp,
const word& type,
const word& entryName,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1<Type>(pp, entryName, dict, faceValues),
mappedPatchBase(pp, dict),
fieldName_(dict.get<word>("field")),
setAverage_(dict.lookupOrDefault("setAverage", false)),
average_(getAverage(dict, setAverage_)),
interpolationScheme_(interpolationCell<Type>::typeName)
{
if (this->mode() == mappedPatchBase::NEARESTCELL)
{
dict.readEntry("interpolationScheme", interpolationScheme_);
}
}
template<class Type>
Foam::PatchFunction1Types::Sampled<Type>::Sampled
(
const Sampled<Type>& ut
)
:
PatchFunction1<Type>(ut),
mappedPatchBase(ut),
fieldName_(ut.fieldName_),
setAverage_(ut.setAverage_),
average_(ut.average_),
interpolationScheme_(ut.interpolationScheme_)
{}
template<class Type>
Foam::PatchFunction1Types::Sampled<Type>::Sampled
(
const Sampled<Type>& ut,
const polyPatch& pp
)
:
PatchFunction1<Type>(ut, pp),
mappedPatchBase(pp, ut),
fieldName_(ut.fieldName_),
setAverage_(ut.setAverage_),
average_(ut.average_),
interpolationScheme_(ut.interpolationScheme_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
const Foam::GeometricField<Type, Foam::fvPatchField, Foam::volMesh>&
Foam::PatchFunction1Types::Sampled<Type>::sampleField() const
{
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
if (this->sameRegion())
{
const polyMesh& thisMesh =
this->mappedPatchBase::patch_.boundaryMesh().mesh();
return thisMesh.template lookupObject<fieldType>(fieldName_);
}
else
{
const fvMesh& nbrMesh = refCast<const fvMesh>(this->sampleMesh());
return nbrMesh.template lookupObject<fieldType>(fieldName_);
}
}
template<class Type>
bool Foam::PatchFunction1Types::Sampled<Type>::haveSampleField() const
{
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
if (this->sameRegion())
{
const polyMesh& thisMesh =
this->mappedPatchBase::patch_.boundaryMesh().mesh();
return thisMesh.template foundObject<fieldType>(fieldName_);
}
else
{
const fvMesh& nbrMesh = refCast<const fvMesh>(this->sampleMesh());
return nbrMesh.template foundObject<fieldType>(fieldName_);
}
}
template<class Type>
Foam::tmp<Foam::Field<Type>>
Foam::PatchFunction1Types::Sampled<Type>::value
(
const scalar x
) const
{
typedef GeometricField<Type, fvPatchField, volMesh> fieldType;
// Since we're inside initEvaluate/evaluate there might be processor
// comms underway. Change the tag we use.
int oldTag = UPstream::msgType();
UPstream::msgType() = oldTag + 1;
const fvMesh& thisMesh = refCast<const fvMesh>
(
this->mappedPatchBase::patch_.boundaryMesh().mesh()
);
const fvMesh& nbrMesh = refCast<const fvMesh>(this->sampleMesh());
// Result of obtaining remote values
auto tnewValues = tmp<Field<Type>>::New();
auto& newValues = tnewValues.ref();
if (!haveSampleField())
{
// Restore tag
UPstream::msgType() = oldTag;
newValues.setSize(this->mappedPatchBase::patch_.size());
newValues = Zero;
return this->transform(tnewValues);
}
switch (this->mode())
{
case mappedPatchBase::NEARESTCELL:
{
const mapDistribute& distMap = this->map();
if (interpolationScheme_ != interpolationCell<Type>::typeName)
{
// Send back sample points to the processor that holds the cell
vectorField samples(this->samplePoints());
distMap.reverseDistribute
(
(
this->sameRegion()
? thisMesh.nCells()
: nbrMesh.nCells()
),
point::max,
samples
);
auto interpolator =
interpolation<Type>::New
(
interpolationScheme_,
sampleField()
);
const auto& interp = *interpolator;
newValues.setSize(samples.size(), pTraits<Type>::max);
forAll(samples, celli)
{
if (samples[celli] != point::max)
{
newValues[celli] = interp.interpolate
(
samples[celli],
celli
);
}
}
}
else
{
newValues = sampleField();
}
distMap.distribute(newValues);
break;
}
case mappedPatchBase::NEARESTPATCHFACE:
case mappedPatchBase::NEARESTPATCHFACEAMI:
{
const label nbrPatchID =
nbrMesh.boundaryMesh().findPatchID(this->samplePatch());
if (nbrPatchID < 0)
{
FatalErrorInFunction
<< "Unable to find sample patch " << this->samplePatch()
<< " in region " << this->sampleRegion()
<< " for patch " << this->mappedPatchBase::patch_.name() << nl
<< abort(FatalError);
}
const fieldType& nbrField = sampleField();
newValues = nbrField.boundaryField()[nbrPatchID];
this->distribute(newValues);
break;
}
case mappedPatchBase::NEARESTFACE:
{
Field<Type> allValues(nbrMesh.nFaces(), Zero);
const fieldType& nbrField = sampleField();
for (const fvPatchField<Type>& pf : nbrField.boundaryField())
{
label faceStart = pf.patch().start();
forAll(pf, facei)
{
allValues[faceStart++] = pf[facei];
}
}
this->distribute(allValues);
newValues.transfer(allValues);
break;
}
default:
{
FatalErrorInFunction
<< "Unknown sampling mode: " << this->mode() << nl
<< abort(FatalError);
}
}
// Enforce average. Either by scaling (if scaling factor > 0.5) or by
// offsetting.
if (setAverage_ && returnReduce(newValues.size(), sumOp<label>()))
{
Type averagePsi;
if (this->faceValues_)
{
const scalarField magSf
(
mag(this->mappedPatchBase::patch_.faceAreas())
);
averagePsi = gSum(magSf*newValues)/gSum(magSf);
}
else
{
averagePsi = gAverage(newValues);
}
if (mag(averagePsi)/mag(average_) > 0.5)
{
newValues *= mag(average_)/mag(averagePsi);
}
else
{
newValues += (average_ - averagePsi);
}
}
// Restore tag
UPstream::msgType() = oldTag;
return this->transform(tnewValues);
}
template<class Type>
Foam::tmp<Foam::Field<Type>>
Foam::PatchFunction1Types::Sampled<Type>::integrate
(
const scalar x1,
const scalar x2
) const
{
NotImplemented;
return tmp<Field<Type>>(nullptr);
}
template<class Type>
void Foam::PatchFunction1Types::Sampled<Type>::writeData
(
Ostream& os
) const
{
PatchFunction1<Type>::writeData(os);
os.writeKeyword(this->name()) << type();
os << token::END_STATEMENT << nl;
mappedPatchBase::write(os);
os.writeEntry("field", fieldName_);
if (setAverage_)
{
os.writeEntry("setAverage", "true");
os.writeEntry("average", average_);
}
os.writeEntry("interpolationScheme", interpolationScheme_);
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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::PatchFunction1Types::Sampled
Description
PatchFunction1 to sample an existing field.
It is the exact functionality from mappedField boundary condition
with the following differences:
- the field name is specified. So any derived fields will have the
same field name to sample.
- if used with uniformFixedValue boundary condition there is the problem
that that re-evaluates instead of reading/mapping.
Usage
\table
Property | Description | Required | Default value
field | Field name | yes |
sampleMode | how to obtain samples | yes |
sampleRegion | mesh to sample | no | ""
samplePatch | patch to sample | no | ""
offsetMode | how to offset samples | no | uniform
offset | uniform offset vector | no | (0 0 0)
interpolationScheme | interpolation method | yes | cell
setAverage | optional average adjustment | no | false
average | optional average value | no |
\endtable
Example of the boundary condition specification:
\verbatim
<patchName>
{
// Field to sample
field U;
// Geometric/mapping info (equivalent of 'mappedPatch' patch type)
sampleMode nearestCell;
offset (0 -0.001 0);
// Field specific info (equivalent of 'mappedField' patch field type)
interpolationScheme cell;
}
\endverbatim
SourceFiles
Sampled.C
\*---------------------------------------------------------------------------*/
#ifndef PatchFunction1Types_Sampled_H
#define PatchFunction1Types_Sampled_H
#include "PatchFunction1.H"
#include "mappedPatchBase.H"
#include "volFieldsFwd.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace PatchFunction1Types
{
/*---------------------------------------------------------------------------*\
Class Sampled Declaration
\*---------------------------------------------------------------------------*/
template<class Type>
class Sampled
:
public PatchFunction1<Type>,
public mappedPatchBase
{
// Private Member Functions
//- Selective retrieval of "average" entry from the dictionary
static Type getAverage(const dictionary& dict, bool mandatory);
protected:
// Protected data
//- Name of the field
word fieldName_;
//- If true adjust the mapped field to maintain average value average_
const bool setAverage_;
//- Average value the mapped field is adjusted to maintain if
//- setAverage_ is set true
const Type average_;
//- Interpolation scheme to use for nearestcell mode
word interpolationScheme_;
// Private Member Functions
//- Field to sample. Either on my or nbr mesh
bool haveSampleField() const;
//- No copy assignment
void operator=(const Sampled<Type>&) = delete;
public:
// Runtime type information
TypeName("sampled");
// Constructors
//- Construct from entry name and dictionary
Sampled
(
const polyPatch& pp,
const word& type,
const word& entryName,
const dictionary& dict,
const bool faceValues = true
);
//- Copy constructor
explicit Sampled(const Sampled<Type>& ut);
//- Copy constructor setting patch
explicit Sampled
(
const Sampled<Type>& ut,
const polyPatch& pp
);
//- Construct and return a clone
virtual tmp<PatchFunction1<Type>> clone() const
{
return tmp<PatchFunction1<Type>>
(
new Sampled<Type>(*this)
);
}
//- Construct and return a clone setting patch
virtual tmp<PatchFunction1<Type>> clone(const polyPatch& pp) const
{
return tmp<PatchFunction1<Type>>
(
new Sampled<Type>(*this, pp)
);
}
//- Destructor
virtual ~Sampled() = default;
// Member Functions
//- Field to sample. Either on my or nbr mesh
const GeometricField<Type, fvPatchField, volMesh>& sampleField() const;
// Evaluation
//- Return sampled value
virtual tmp<Field<Type>> value(const scalar) const;
//- Integrate between two values
virtual tmp<Field<Type>> integrate
(
const scalar x1,
const scalar x2
) const;
// I-O
//- Write in dictionary format
virtual void writeData(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace PatchFunction1Types
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "Sampled.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\