From 16714f3569cf90243e1fd63e46ad11f189fc92ad Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Sat, 21 May 2016 13:58:08 +0100 Subject: [PATCH] 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'. --- .../fvMeshFunctionObject.C | 22 +++ .../fvMeshFunctionObject.H | 29 ++++ .../fvMeshFunctionObjectTemplates.C | 100 ++++++++++++++ .../functionObjects/field/Make/files | 1 + .../functionObjects/field/div/div.C | 80 +---------- .../functionObjects/field/div/div.H | 47 +------ .../functionObjects/field/div/divTemplates.C | 26 ++-- .../field/fieldExpression/fieldExpression.C | 81 +++++++++++ .../field/fieldExpression/fieldExpression.H | 126 ++++++++++++++++++ .../functionObjects/field/grad/grad.C | 38 +----- .../functionObjects/field/grad/grad.H | 49 +------ .../field/grad/gradTemplates.C | 94 +++---------- .../functionObjects/field/mag/mag.C | 46 +------ .../functionObjects/field/mag/mag.H | 43 +----- .../functionObjects/field/mag/magTemplates.C | 82 +++--------- 15 files changed, 435 insertions(+), 429 deletions(-) create mode 100644 src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObjectTemplates.C create mode 100644 src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.C create mode 100644 src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.H diff --git a/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.C b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.C index f5e0b45852..84bb6e7e03 100644 --- a/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.C +++ b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.C @@ -38,6 +38,28 @@ namespace functionObjects } +// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // + +bool Foam::functionObjects::fvMeshFunctionObject::write(const word& fieldName) +{ + if (mesh_.foundObject(fieldName)) + { + const regIOobject& field = mesh_.lookupObject(fieldName); + + Info<< type() << " " << name() << " writing:" << nl + << " field " << field.name() << nl << endl; + + field.write(); + + return true; + } + else + { + return false; + } +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::functionObjects::fvMeshFunctionObject::fvMeshFunctionObject diff --git a/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.H b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.H index 6e2477fc47..b090c7c4b5 100644 --- a/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.H +++ b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObject.H @@ -73,6 +73,29 @@ protected: const fvMesh& mesh_; + // Protected member functions + + //- Find field in the objectRegistry + template + bool foundField(const word& fieldName) const; + + //- Lookup field from the objectRegistry + template + const FieldType& lookupField(const word& fieldName) const; + + //- Store the given field in the objectRegistry under the given name + template + bool store + ( + word& fieldName, + tmp 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 // ************************************************************************* // diff --git a/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObjectTemplates.C b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObjectTemplates.C new file mode 100644 index 0000000000..2a466a789b --- /dev/null +++ b/src/finiteVolume/fvMesh/fvMeshFunctionObject/fvMeshFunctionObjectTemplates.C @@ -0,0 +1,100 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +\*---------------------------------------------------------------------------*/ + +#include "fvMeshFunctionObject.H" +#include "volFields.H" + +// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // + +template +bool Foam::functionObjects::fvMeshFunctionObject::foundField +( + const word& fieldName +) const +{ + return mesh_.foundObject(fieldName); +} + + +template +const FieldType& Foam::functionObjects::fvMeshFunctionObject::lookupField +( + const word& fieldName +) const +{ + return mesh_.lookupObject(fieldName); +} + + +template +bool Foam::functionObjects::fvMeshFunctionObject::store +( + word& fieldName, + tmp 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(fieldName) + ) + { + const_cast + ( + mesh_.lookupObject(fieldName) + ) = tfield; + } + else + { + if (fieldName.size() && fieldName != tfield().name()) + { + tfield.ref().rename(fieldName); + } + else + { + fieldName = tfield().name(); + } + + mesh_.objectRegistry::store(tfield.ptr()); + } + + return true; +} + + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/field/Make/files b/src/postProcessing/functionObjects/field/Make/files index f25f7d5172..a314c43ad0 100644 --- a/src/postProcessing/functionObjects/field/Make/files +++ b/src/postProcessing/functionObjects/field/Make/files @@ -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 diff --git a/src/postProcessing/functionObjects/field/div/div.C b/src/postProcessing/functionObjects/field/div/div.C index a3e6bf114d..c8aefb1693 100644 --- a/src/postProcessing/functionObjects/field/div/div.C +++ b/src/postProcessing/functionObjects/field/div/div.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(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(divName); - - return const_cast(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(fieldName_, resultName_, processed); - calcDiv(fieldName_, resultName_, processed); + processed = processed || calc(); + processed = processed || calc(); 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(resultName_)) - { - const regIOobject& field = - mesh_.lookupObject(resultName_); - - Info<< type() << " " << name() << " output:" << nl - << " writing field " << field.name() << nl << endl; - - field.write(); - } - - return true; + return processed; } diff --git a/src/postProcessing/functionObjects/field/div/div.H b/src/postProcessing/functionObjects/field/div/div.H index 54e514a42a..f490a9cac5 100644 --- a/src/postProcessing/functionObjects/field/div/div.H +++ b/src/postProcessing/functionObjects/field/div/div.H @@ -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 - 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); }; diff --git a/src/postProcessing/functionObjects/field/div/divTemplates.C b/src/postProcessing/functionObjects/field/div/divTemplates.C index bdfea97710..e791e25b2c 100644 --- a/src/postProcessing/functionObjects/field/div/divTemplates.C +++ b/src/postProcessing/functionObjects/field/div/divTemplates.C @@ -23,28 +23,24 @@ License \*---------------------------------------------------------------------------*/ -#include "fvMesh.H" #include "fvcDiv.H" // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -void Foam::functionObjects::div::calcDiv -( - const word& fieldName, - const word& resultName, - bool& processed -) +bool Foam::functionObjects::div::calc() { - if (mesh_.foundObject(fieldName)) + if (foundField(fieldName_)) { - const FieldType& vf = mesh_.lookupObject(fieldName); - - volScalarField& field = divField(resultName, vf.dimensions()); - - field = fvc::div(vf); - - processed = true; + return store + ( + resultName_, + fvc::div(lookupField(fieldName_)) + ); + } + else + { + return false; } } diff --git a/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.C b/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.C new file mode 100644 index 0000000000..205d6a0fd6 --- /dev/null +++ b/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.C @@ -0,0 +1,81 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +\*---------------------------------------------------------------------------*/ + +#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_); +} + + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.H b/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.H new file mode 100644 index 0000000000..767dd7ce1b --- /dev/null +++ b/src/postProcessing/functionObjects/field/fieldExpression/fieldExpression.H @@ -0,0 +1,126 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +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 + virtual bool read(const dictionary&); + + //- Calculate the fieldExpressionergence field + virtual bool execute(const bool postProcess = false) = 0; + + //- Write the fieldExpressionergence field + virtual bool write(const bool postProcess = false); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace functionObjects +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/field/grad/grad.C b/src/postProcessing/functionObjects/field/grad/grad.C index 4736c4c04d..1ee5d82f8c 100644 --- a/src/postProcessing/functionObjects/field/grad/grad.C +++ b/src/postProcessing/functionObjects/field/grad/grad.C @@ -24,7 +24,6 @@ License \*---------------------------------------------------------------------------*/ #include "grad.H" -#include "volFields.H" #include "addToRunTimeSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -48,7 +47,7 @@ Foam::functionObjects::grad::grad const dictionary& dict ) : - fvMeshFunctionObject(name, runTime, dict) + fieldExpression(name, runTime, dict) { read(dict); } @@ -62,26 +61,12 @@ Foam::functionObjects::grad::~grad() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -bool Foam::functionObjects::grad::read(const dictionary& dict) -{ - dict.lookup("fieldName") >> fieldName_; - dict.lookup("resultName") >> resultName_; - - if (resultName_ == "none") - { - resultName_ = "fvc::grad(" + fieldName_ + ")"; - } - - return true; -} - - bool Foam::functionObjects::grad::execute(const bool postProcess) { bool processed = false; - calcGrad(fieldName_, resultName_, processed); - calcGrad(fieldName_, resultName_, processed); + processed = processed || calc(); + processed = processed || calc(); if (!processed) { @@ -93,21 +78,4 @@ bool Foam::functionObjects::grad::execute(const bool postProcess) } -bool Foam::functionObjects::grad::write(const bool postProcess) -{ - if (obr_.foundObject(resultName_)) - { - const regIOobject& field = - obr_.lookupObject(resultName_); - - Info<< type() << " " << name() << " output:" << nl - << " writing field " << field.name() << nl << endl; - - field.write(); - } - - return true; -} - - // ************************************************************************* // diff --git a/src/postProcessing/functionObjects/field/grad/grad.H b/src/postProcessing/functionObjects/field/grad/grad.H index a58d781935..68bd4450f6 100644 --- a/src/postProcessing/functionObjects/field/grad/grad.H +++ b/src/postProcessing/functionObjects/field/grad/grad.H @@ -43,17 +43,12 @@ SourceFiles #ifndef functionObjects_grad_H #define functionObjects_grad_H -#include "fvMeshFunctionObject.H" -#include "volFieldsFwd.H" +#include "fieldExpression.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { - -// Forward declaration of classes -class dimensionSet; - namespace functionObjects { @@ -63,43 +58,13 @@ namespace functionObjects class grad : - public fvMeshFunctionObject + public fieldExpression { - // Private data - - //- Name of field to process - word fieldName_; - - //- Name of result field - word resultName_; - - // Private Member Functions - //- Helper function to create/store/return the gradient field + //- Calculate the magnitude of the field and register the result template - GeometricField - < - typename outerProduct::type, - fvPatchField, - volMesh - >& - gradField(const word& gradName, const dimensionSet& dims); - - //- Helper function to calculate the gradient of different field types - template - void calcGrad - ( - const word& fieldName, - const word& resultName, - bool& processed - ); - - //- Disallow default bitwise copy construct - grad(const grad&); - - //- Disallow default bitwise assignment - void operator=(const grad&); + bool calc(); public: @@ -125,14 +90,8 @@ public: // Member Functions - //- Read the grad data - virtual bool read(const dictionary&); - //- Calculate the gradient field virtual bool execute(const bool postProcess = false); - - //- Write the gradient field - virtual bool write(const bool postProcess = false); }; diff --git a/src/postProcessing/functionObjects/field/grad/gradTemplates.C b/src/postProcessing/functionObjects/field/grad/gradTemplates.C index 0c06b01c61..026b1a3c70 100644 --- a/src/postProcessing/functionObjects/field/grad/gradTemplates.C +++ b/src/postProcessing/functionObjects/field/grad/gradTemplates.C @@ -23,97 +23,37 @@ License \*---------------------------------------------------------------------------*/ -#include "fvMesh.H" #include "fvcGrad.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // template -Foam::GeometricField -< - typename Foam::outerProduct::type, - Foam::fvPatchField, - Foam::volMesh ->& -Foam::functionObjects::grad::gradField -( - const word& gradName, - const dimensionSet& dims -) +bool Foam::functionObjects::grad::calc() { - Info<< "gradField" << endl; + typedef GeometricField VolFieldType; + typedef GeometricField SurfaceFieldType; - typedef typename outerProduct::type gradType; - typedef GeometricField vfGradType; - - if (!mesh_.foundObject(gradName)) + if (foundField(fieldName_)) { - vfGradType* gradFieldPtr + return store ( - new vfGradType - ( - IOobject - ( - gradName, - mesh_.time().timeName(), - mesh_, - IOobject::NO_READ, - IOobject::NO_WRITE - ), - mesh_, - dimensioned - ( - "zero", - dims/dimLength, - Zero - ) - ) + resultName_, + fvc::grad(lookupField(fieldName_)), + true ); - - mesh_.objectRegistry::store(gradFieldPtr); } - - const vfGradType& field = mesh_.lookupObject(gradName); - - return const_cast(field); -} - - -template -void Foam::functionObjects::grad::calcGrad -( - const word& fieldName, - const word& resultName, - bool& processed -) -{ - typedef GeometricField vfType; - typedef GeometricField sfType; - - typedef typename outerProduct::type gradType; - typedef GeometricField vfGradType; - - if (mesh_.foundObject(fieldName)) + else if (foundField(fieldName_)) { - const vfType& vf = mesh_.lookupObject(fieldName); - - vfGradType& field = gradField(resultName, vf.dimensions()); - - // De-reference the tmp to avoid a clash with the cached grad field - field = fvc::grad(vf)(); - - processed = true; + return store + ( + resultName_, + fvc::grad(lookupField(fieldName_)), + true + ); } - else if (mesh_.foundObject(fieldName)) + else { - const sfType& sf = mesh_.lookupObject(fieldName); - - vfGradType& field = gradField(resultName, sf.dimensions()); - - // De-reference the tmp to avoid a clash with the cached grad field - field = fvc::grad(sf)(); - - processed = true; + return false; } } diff --git a/src/postProcessing/functionObjects/field/mag/mag.C b/src/postProcessing/functionObjects/field/mag/mag.C index ac8e426e9c..c2e519d267 100644 --- a/src/postProcessing/functionObjects/field/mag/mag.C +++ b/src/postProcessing/functionObjects/field/mag/mag.C @@ -24,7 +24,6 @@ License \*---------------------------------------------------------------------------*/ #include "mag.H" -#include "volFields.H" #include "addToRunTimeSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -48,7 +47,7 @@ Foam::functionObjects::mag::mag const dictionary& dict ) : - fvMeshFunctionObject(name, runTime, dict) + fieldExpression(name, runTime, dict) { read(dict); } @@ -62,29 +61,15 @@ Foam::functionObjects::mag::~mag() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -bool Foam::functionObjects::mag::read(const dictionary& dict) -{ - dict.lookup("fieldName") >> fieldName_; - dict.lookup("resultName") >> resultName_; - - if (resultName_ == "none") - { - resultName_ = "mag(" + fieldName_ + ")"; - } - - return true; -} - - bool Foam::functionObjects::mag::execute(const bool postProcess) { bool processed = false; - calc(fieldName_, resultName_, processed); - calc(fieldName_, resultName_, processed); - calc(fieldName_, resultName_, processed); - calc(fieldName_, resultName_, processed); - calc(fieldName_, resultName_, processed); + processed = processed || calc(); + processed = processed || calc(); + processed = processed || calc(); + processed = processed || calc(); + processed = processed || calc(); if (!processed) { @@ -92,24 +77,7 @@ bool Foam::functionObjects::mag::execute(const bool postProcess) << "Unprocessed field " << fieldName_ << endl; } - return true; -} - - -bool Foam::functionObjects::mag::write(const bool postProcess) -{ - if (obr_.foundObject(resultName_)) - { - const regIOobject& field = - obr_.lookupObject(resultName_); - - Info<< type() << " " << name() << " output:" << nl - << " writing field " << field.name() << nl << endl; - - field.write(); - } - - return true; + return processed; } diff --git a/src/postProcessing/functionObjects/field/mag/mag.H b/src/postProcessing/functionObjects/field/mag/mag.H index e3f09e7ed8..a77951827e 100644 --- a/src/postProcessing/functionObjects/field/mag/mag.H +++ b/src/postProcessing/functionObjects/field/mag/mag.H @@ -43,17 +43,12 @@ SourceFiles #ifndef functionObjects_mag_H #define functionObjects_mag_H -#include "fvMeshFunctionObject.H" -#include "volFieldsFwd.H" +#include "fieldExpression.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { - -// Forward declaration of classes -class dimensionSet; - namespace functionObjects { @@ -63,37 +58,13 @@ namespace functionObjects class mag : - public fvMeshFunctionObject + public fieldExpression { - // Private data - - //- Name of field to process - word fieldName_; - - //- Name of result field - word resultName_; - - // Private Member Functions - //- Helper function to create/store/return the mag field - template - FieldType& magField(const word& magName, const dimensionSet& dims); - - //- Helper function to calculate the magnitude of different field types + //- Calculate the magnitude of the field and register the result template - void calc - ( - const word& fieldName, - const word& resultName, - bool& processed - ); - - //- Disallow default bitwise copy construct - mag(const mag&); - - //- Disallow default bitwise assignment - void operator=(const mag&); + bool calc(); public: @@ -119,14 +90,8 @@ public: // Member Functions - //- Read the mag data - virtual bool read(const dictionary&); - //- Calculate the magnitude field virtual bool execute(const bool postProcess = false); - - //- Write the magnitude field - virtual bool write(const bool postProcess = false); }; diff --git a/src/postProcessing/functionObjects/field/mag/magTemplates.C b/src/postProcessing/functionObjects/field/mag/magTemplates.C index 49386a701b..5b78efb93a 100644 --- a/src/postProcessing/functionObjects/field/mag/magTemplates.C +++ b/src/postProcessing/functionObjects/field/mag/magTemplates.C @@ -23,80 +23,36 @@ License \*---------------------------------------------------------------------------*/ -#include "fvMesh.H" -#include "Time.H" #include "volFields.H" #include "surfaceFields.H" -template -FieldType& Foam::functionObjects::mag::magField -( - const word& magName, - const dimensionSet& dims -) -{ - if (!mesh_.foundObject(magName)) - { - FieldType* magFieldPtr - ( - new FieldType - ( - IOobject - ( - magName, - mesh_.time().timeName(), - mesh_, - IOobject::NO_READ, - IOobject::NO_WRITE - ), - mesh_, - dimensionedScalar("zero", dims, 0.0) - ) - ); - - mesh_.objectRegistry::store(magFieldPtr); - } - - const FieldType& f = mesh_.lookupObject(magName); - - return const_cast(f); -} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -void Foam::functionObjects::mag::calc -( - const word& fieldName, - const word& resultName, - bool& processed -) +bool Foam::functionObjects::mag::calc() { - typedef GeometricField vfType; - typedef GeometricField sfType; + typedef GeometricField VolFieldType; + typedef GeometricField SurfaceFieldType; - if (mesh_.foundObject(fieldName)) + if (foundField(fieldName_)) { - const vfType& vf = mesh_.lookupObject(fieldName); - - volScalarField& field = - magField(resultName_, vf.dimensions()); - - field = Foam::mag(vf); - - processed = true; + return store + ( + resultName_, + Foam::mag(lookupField(fieldName_)) + ); } - else if (mesh_.foundObject(fieldName)) + else if (foundField(fieldName_)) { - const sfType& sf = mesh_.lookupObject(fieldName); - - surfaceScalarField& field = - magField(resultName_, sf.dimensions()); - - field = Foam::mag(sf); - - processed = true; + return store + ( + resultName_, + Foam::mag(lookupField(fieldName_)) + ); + } + else + { + return false; } }