Commit 16714f35 authored by Henry Weller's avatar Henry Weller
Browse files

functionObjects: New abstract base-class 'fieldExpression' for simple field...

functionObjects: New abstract base-class 'fieldExpression' for simple field expression evaluation functionObjects
Updated and simplified 'div', 'grad' and 'mag' functionObjects by deriving from 'fieldExpression'.
Corrected the handling of cached gradients in 'grad'.
parent 6d562d1a
......@@ -38,6 +38,28 @@ namespace functionObjects
}
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
bool Foam::functionObjects::fvMeshFunctionObject::write(const word& fieldName)
{
if (mesh_.foundObject<regIOobject>(fieldName))
{
const regIOobject& field = mesh_.lookupObject<regIOobject>(fieldName);
Info<< type() << " " << name() << " writing:" << nl
<< " field " << field.name() << nl << endl;
field.write();
return true;
}
else
{
return false;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::fvMeshFunctionObject::fvMeshFunctionObject
......
......@@ -73,6 +73,29 @@ protected:
const fvMesh& mesh_;
// Protected member functions
//- Find field in the objectRegistry
template<class FieldType>
bool foundField(const word& fieldName) const;
//- Lookup field from the objectRegistry
template<class FieldType>
const FieldType& lookupField(const word& fieldName) const;
//- Store the given field in the objectRegistry under the given name
template<class FieldType>
bool store
(
word& fieldName,
tmp<FieldType> tfield,
bool cacheable = false
);
//- Write field if present in objectRegistry
virtual bool write(const word& fieldName);
private:
// Private Member Functions
......@@ -113,6 +136,12 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "fvMeshFunctionObjectTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ 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 "fvMeshFunctionObject.H"
#include "volFields.H"
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
template<class FieldType>
bool Foam::functionObjects::fvMeshFunctionObject::foundField
(
const word& fieldName
) const
{
return mesh_.foundObject<FieldType>(fieldName);
}
template<class FieldType>
const FieldType& Foam::functionObjects::fvMeshFunctionObject::lookupField
(
const word& fieldName
) const
{
return mesh_.lookupObject<FieldType>(fieldName);
}
template<class FieldType>
bool Foam::functionObjects::fvMeshFunctionObject::store
(
word& fieldName,
tmp<FieldType> tfield,
bool cacheable
)
{
if (cacheable && fieldName == tfield().name())
{
WarningInFunction
<< "Cannot store cache-able field with the named used in the cache."
<< nl
<< " Either choose a different name or cache the field"
<< " and use the 'writeRegisteredObject' functionObject."
<< endl;
return false;
}
if
(
fieldName.size()
&& mesh_.foundObject<FieldType>(fieldName)
)
{
const_cast<FieldType&>
(
mesh_.lookupObject<FieldType>(fieldName)
) = tfield;
}
else
{
if (fieldName.size() && fieldName != tfield().name())
{
tfield.ref().rename(fieldName);
}
else
{
fieldName = tfield().name();
}
mesh_.objectRegistry::store(tfield.ptr());
}
return true;
}
// ************************************************************************* //
......@@ -32,6 +32,7 @@ surfaceInterpolateFields/surfaceInterpolateFields.C
regionSizeDistribution/regionSizeDistribution.C
histogram/histogram.C
fieldExpression/fieldExpression.C
div/div.C
grad/grad.C
mag/mag.C
......
......@@ -25,6 +25,7 @@ License
#include "div.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -39,42 +40,6 @@ namespace functionObjects
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::volScalarField& Foam::functionObjects::div::divField
(
const word& divName,
const dimensionSet& dims
)
{
if (!mesh_.foundObject<volScalarField>(divName))
{
volScalarField* divFieldPtr
(
new volScalarField
(
IOobject
(
divName,
mesh_.time().timeName(),
mesh_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
mesh_,
dimensionedScalar("zero", dims/dimLength, 0.0)
)
);
mesh_.objectRegistry::store(divFieldPtr);
}
const volScalarField& field = mesh_.lookupObject<volScalarField>(divName);
return const_cast<volScalarField&>(field);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::div::div
......@@ -84,10 +49,8 @@ Foam::functionObjects::div::div
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict)
{
read(dict);
}
fieldExpression(name, runTime, dict)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
......@@ -98,26 +61,12 @@ Foam::functionObjects::div::~div()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::div::read(const dictionary& dict)
{
dict.lookup("fieldName") >> fieldName_;
dict.lookup("resultName") >> resultName_;
if (resultName_ == "none")
{
resultName_ = "fvc::div(" + fieldName_ + ")";
}
return true;
}
bool Foam::functionObjects::div::execute(const bool postProcess)
{
bool processed = false;
calcDiv<surfaceScalarField>(fieldName_, resultName_, processed);
calcDiv<volVectorField>(fieldName_, resultName_, processed);
processed = processed || calc<surfaceScalarField>();
processed = processed || calc<volVectorField>();
if (!processed)
{
......@@ -125,24 +74,7 @@ bool Foam::functionObjects::div::execute(const bool postProcess)
<< "Unprocessed field " << fieldName_ << endl;
}
return true;
}
bool Foam::functionObjects::div::write(const bool postProcess)
{
if (mesh_.foundObject<regIOobject>(resultName_))
{
const regIOobject& field =
mesh_.lookupObject<regIOobject>(resultName_);
Info<< type() << " " << name() << " output:" << nl
<< " writing field " << field.name() << nl << endl;
field.write();
}
return true;
return processed;
}
......
......@@ -43,17 +43,12 @@ SourceFiles
#ifndef functionObjects_div_H
#define functionObjects_div_H
#include "fvMeshFunctionObject.H"
#include "volFieldsFwd.H"
#include "fieldExpression.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class dimensionSet;
namespace functionObjects
{
......@@ -63,40 +58,14 @@ namespace functionObjects
class div
:
public fvMeshFunctionObject
public fieldExpression
{
// Private member data
//- Name of field to process
word fieldName_;
//- Name of result field
word resultName_;
// Private Member Functions
//- Helper function to create/store/return the divergence field
volScalarField& divField
(
const word& gradName,
const dimensionSet& dims
);
//- Helper function to calculate the divergence of different field types
//- Calculate the divergence of either a
// volScalarField or a surfaceScalarField and register the result
template<class FieldType>
void calcDiv
(
const word& fieldName,
const word& resultName,
bool& processed
);
//- Disallow default bitwise copy construct
div(const div&);
//- Disallow default bitwise assignment
void operator=(const div&);
bool calc();
public:
......@@ -122,14 +91,8 @@ public:
// Member Functions
//- Read the div data
virtual bool read(const dictionary&);
//- Calculate the divergence field
virtual bool execute(const bool postProcess = false);
//- Write the divergence field
virtual bool write(const bool postProcess = false);
};
......
......@@ -23,28 +23,24 @@ License
\*---------------------------------------------------------------------------*/
#include "fvMesh.H"
#include "fvcDiv.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class FieldType>
void Foam::functionObjects::div::calcDiv
(
const word& fieldName,
const word& resultName,
bool& processed
)
bool Foam::functionObjects::div::calc()
{
if (mesh_.foundObject<FieldType>(fieldName))
if (foundField<FieldType>(fieldName_))
{
const FieldType& vf = mesh_.lookupObject<FieldType>(fieldName);
volScalarField& field = divField(resultName, vf.dimensions());
field = fvc::div(vf);
processed = true;
return store
(
resultName_,
fvc::div(lookupField<FieldType>(fieldName_))
);
}
else
{
return false;
}
}
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ 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 "fieldExpression.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
defineTypeNameAndDebug(fieldExpression, 0);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::fieldExpression::fieldExpression
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict)
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::fieldExpression::~fieldExpression()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::fieldExpression::read(const dictionary& dict)
{
dict.lookup("field") >> fieldName_;
if (dict.found("result"))
{
dict.lookup("result") >> resultName_;
}
return true;
}
bool Foam::functionObjects::fieldExpression::write(const bool postProcess)
{
return fvMeshFunctionObject::write(resultName_);
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012-2016 OpenFOAM Foundation
\\/ 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::fieldExpression
Group
grpFieldFunctionObjects
Description
SeeAlso
Foam::functionObjects::fvMeshFunctionObject
SourceFiles
fieldExpression.C
\*---------------------------------------------------------------------------*/
#ifndef functionObjects_fieldExpression_H
#define functionObjects_fieldExpression_H
#include "fvMeshFunctionObject.H"
#include "volFieldsFwd.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionObjects
{
/*---------------------------------------------------------------------------*\
Class fieldExpression Declaration
\*---------------------------------------------------------------------------*/
class fieldExpression
:
public fvMeshFunctionObject
{
protected:
// Protected member data
//- Name of field to process
word fieldName_;
//- Name of result field
word resultName_;
private:
// Private Member Functions
//- Disallow default bitwise copy construct
fieldExpression(const fieldExpression&);
//- Disallow default bitwise assignment
void operator=(const fieldExpression&);
public:
//- Runtime type information
TypeName("fieldExpression");
// Constructors
//- Construct from Time and dictionary
fieldExpression
(
const word& name,
const Time& runTime,
const dictionary& dict
);
//- Destructor
virtual ~fieldExpression();
// Member Functions
//- Read the fieldExpression data