Commit 091d8470 authored by Andrew Heather's avatar Andrew Heather
Browse files

ENH: Added a new function object to create a field relative to a field value

    Calculates and outputs a field whose values are offset to a reference
    value obtained by sampling the field at a user-specified location.

    The field values are calculated using:

    \f[
        f_c = s(f_{c,t} - f_p + f_{off})
    \f]

    where
    \vartable
        f_c     | field values at cell
        s       | optional scale factor (default = 1)
        f_{c,t} | current field values at cell at this time
        f_p     | field value at position
        f_{off} | offset field value (default = 0)
    \endvartable

Usage
    Example of function object specification to calculate the reference
field:
    \verbatim
    pRef
    {
        type        reference;
        libs        ("libfieldFunctionObjects.so");
        ...
        field       p;
        result      pRef;
        position    (0 0 0);
        scale       1.2;
        offset      100000;
    }
    \endverbatim
parent 5c60cbe3
......@@ -60,6 +60,7 @@ blendingFactor/blendingFactor.C
pressure/pressure.C
MachNo/MachNo.C
Curle/Curle.C
reference/reference.C
fieldsExpression/fieldsExpression.C
add/add.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 "reference.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(reference, 0);
addToRunTimeSelectionTable
(
functionObject,
reference,
dictionary
);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
bool Foam::functionObjects::reference::calc()
{
Log << type() << " " << name() << " output:" << nl;
bool processed = calcType<scalar>();
processed = processed || calcType<vector>();
processed = processed || calcType<sphericalTensor>();
processed = processed || calcType<symmTensor>();
processed = processed || calcType<tensor>();
Log << endl;
return returnReduce(processed, orOp<bool>());
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::reference::reference
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fieldExpression(name, runTime, dict),
localDict_(dict),
position_(point::zero),
celli_(-1),
interpolationScheme_("cell"),
scale_(1)
{
read(dict);
// Forcing default result name to include the name of the field
setResultName(typeName, word::null);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::reference::~reference()
{}
bool Foam::functionObjects::reference::read(const dictionary& dict)
{
if (fieldExpression::read(dict))
{
localDict_ = dict;
dict.lookup("position") >> position_;
interpolationScheme_ =
dict.lookupOrDefault<word>("interpolationScheme", "cell");
dict.readIfPresent("scale", scale_);
celli_ = mesh_.findCell(position_);
label celli = returnReduce(celli_, maxOp<label>());
if (celli == -1)
{
FatalErrorInFunction
<< "Sample cell could not be found at position " << position_
<< exit(FatalError);
}
Log << type() << " " << name() << nl
<< " field: " << fieldName_ << nl
<< " sample position: " << position_ << nl
<< " scale: " << scale_ << nl
<< endl;
return true;
}
return false;
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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::functionObjects::reference
Group
grpFieldFunctionObjects
Description
Calculates and outputs a field whose values are offset to a reference
value obtained by sampling the field at a user-specified location.
The field values are calculated using:
\f[
f_c = s(f_{c,t} - f_p + f_{off})
\f]
where
\vartable
f_c | field values at cell
s | optional scale factor (default = 1)
f_{c,t} | current field values at cell at this time
f_p | field value at position
f_{off} | offset field value (default = 0)
\endvartable
Usage
Example of function object specification to calculate the reference field:
\verbatim
pRef
{
type reference;
libs ("libfieldFunctionObjects.so");
...
field p;
result pRef;
position (0 0 0);
scale 1.2;
offset 100000;
}
\endverbatim
Where the entries comprise:
\table
Property | Description | Required | Default value
type | Type name: reference | yes |
position | Position to sample | yes |
field | Name of field | yes |
reult | Name of result field | no | reference(<field>)
scale | Scale value | no | 1
offset | Offset value | no | zero
result | Name of result field | no | <function name>
log | Log to standard output | no | yes
\endtable
See also
Foam::functionObjects::fieldExpression
Foam::functionObjects::fvMeshFunctionObject
SourceFiles
reference.C
\*---------------------------------------------------------------------------*/
#ifndef functionObjects_reference_H
#define functionObjects_reference_H
#include "fieldExpression.H"
#include "point.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
/*---------------------------------------------------------------------------*\
Class reference Declaration
\*---------------------------------------------------------------------------*/
class reference
:
public fieldExpression
{
// Private data
//- Local copy of dictionary used for construction
dictionary localDict_;
//- Sample location
point position_;
//- Sample cell
label celli_;
//- Interpolation scheme
word interpolationScheme_;
//- Scale factor
scalar scale_;
// Private Member Functions
//- Calculate the reference field and return true if successful
virtual bool calc();
//- Utility function to calc a given type of field
template<class Type>
bool calcType();
public:
//- Runtime type information
TypeName("reference");
// Constructors
//- Construct from Time and dictionary
reference
(
const word& name,
const Time& runTime,
const dictionary& dict
);
//- Destructor
virtual ~reference();
// Public Member Functions
//- Read the fieldExpression data
virtual bool read(const dictionary& dict);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionObjects
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "referenceTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "interpolation.H"
template<class Type>
bool Foam::functionObjects::reference::calcType()
{
typedef GeometricField<Type, fvPatchField, volMesh> VolFieldType;
const VolFieldType* vfPtr = lookupObjectPtr<VolFieldType>(fieldName_);
if (vfPtr && celli_ != -1)
{
const VolFieldType& vf = *vfPtr;
autoPtr<interpolation<Type>> interpolator
(
interpolation<Type>::New(interpolationScheme_, vf)
);
const dimensioned<Type> value
(
"value",
vf.dimensions(),
interpolator().interpolate(position_, celli_, -1)
);
dimensioned<Type> offset
(
dimensioned<Type>::lookupOrDefault
(
"offset",
localDict_,
vf.dimensions(),
Zero
)
);
Log << " sampled value: " << value.value() << endl;
return store
(
resultName_,
scale_*(vf - value + offset)
);
}
return false;
}
// ************************************************************************* //
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment