diff --git a/src/functionObjects/field/Make/files b/src/functionObjects/field/Make/files index 1ca07b3766057c5fd3b4b02b4b14d9657ae02e50..18ec9b28dbf1d0b667be96ac98e4c59eef182636 100644 --- a/src/functionObjects/field/Make/files +++ b/src/functionObjects/field/Make/files @@ -2,6 +2,8 @@ AMIWeights/AMIWeights.C columnAverage/columnAverage.C +continuityError/continuityError.C + fieldAverage/fieldAverage.C fieldAverage/fieldAverageItem/fieldAverageItem.C fieldAverage/fieldAverageItem/fieldAverageItemIO.C diff --git a/src/functionObjects/field/continuityError/continuityError.C b/src/functionObjects/field/continuityError/continuityError.C new file mode 100644 index 0000000000000000000000000000000000000000..1bffa5a6c6a2fd11141c02babfded6722fa3672b --- /dev/null +++ b/src/functionObjects/field/continuityError/continuityError.C @@ -0,0 +1,144 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 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 "continuityError.H" +#include "volFields.H" +#include "fvcDiv.H" +#include "addToRunTimeSelectionTable.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace functionObjects +{ + defineTypeNameAndDebug(continuityError, 0); + addToRunTimeSelectionTable(functionObject, continuityError, dictionary); +} +} + + +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +void Foam::functionObjects::continuityError::writeFileHeader(Ostream& os) +{ + writeHeader(os, "Continuity error"); + + writeCommented(os, "Time"); + writeCommented(os, "Local"); + writeCommented(os, "Global"); + writeCommented(os, "Cumulative"); + + os << endl; +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::functionObjects::continuityError::continuityError +( + const word& name, + const Time& runTime, + const dictionary& dict +) +: + fvMeshFunctionObject(name, runTime, dict), + writeFile(mesh_, name, typeName, dict), + phiName_("phi"), + cumulative_(getProperty<scalar>("cumulative")) +{ + if (read(dict)) + { + writeFileHeader(file()); + } +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::functionObjects::continuityError::read(const dictionary& dict) +{ + if (fvMeshFunctionObject::read(dict) && writeFile::read(dict)) + { + dict.readIfPresent("phi", phiName_); + + return true; + } + + return false; +} + + +bool Foam::functionObjects::continuityError::execute() +{ + return true; +} + + +bool Foam::functionObjects::continuityError::write() +{ + const auto phiPtr = mesh_.lookupObjectPtr<surfaceScalarField>(phiName_); + + if (!phiPtr) + { + WarningInFunction + << "Unable to find flux field " << phiName_ + << endl; + + return false; + } + + const volScalarField error(fvc::div(*phiPtr)); + const scalar deltaT = mesh_.time().deltaTValue(); + + scalar local = deltaT*mag(error)().weightedAverage(mesh_.V()).value(); + scalar global = deltaT*error.weightedAverage(mesh_.V()).value(); + cumulative_ += global; + + Ostream& os = file(); + + writeTime(os); + + os << local << tab + << global << tab + << cumulative_ << endl; + + Log << type() << " " << name() << " write:" << nl + << " local = " << local << nl + << " global = " << global << nl + << " cumulative = " << cumulative_ << nl + << endl; + + setResult("local", local); + setResult("global", global); + setResult("cumulative", cumulative_); + + setProperty<scalar>("cumulative", cumulative_); + + return true; +} + + +// ************************************************************************* // diff --git a/src/functionObjects/field/continuityError/continuityError.H b/src/functionObjects/field/continuityError/continuityError.H new file mode 100644 index 0000000000000000000000000000000000000000..7c64dfff5db83ed6764817bd84a692f1ca773b3a --- /dev/null +++ b/src/functionObjects/field/continuityError/continuityError.H @@ -0,0 +1,162 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2019 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::continuityError + +Group + grpFieldFunctionObjects + +Description + Calculates the continuity error for a flux field + +Usage + Example of function object specification: + \verbatim + continuityError1 + { + type continuityError; + libs ("libfieldFunctionObjects.so"); + ... + writeToFile yes; + log yes; + phi phi; + } + \endverbatim + + Where the entries comprise: + \table + Property | Description | Required | Default value + type | type name: continuityError | yes | + writeToFile | write min/max data to file | no | yes + log | write min/max data to standard output | no | yes + phi | name of flux field | no | phi + \endtable + + Output data is written to the file \<timeDir\>/continuityError.dat + +See also + Foam::functionObjects::fvMeshFunctionObject + Foam::functionObjects::writeFile + +SourceFiles + continuityError.C + continuityErrorTemplates.C + +\*---------------------------------------------------------------------------*/ + +#ifndef functionObjects_continuityError_H +#define functionObjects_continuityError_H + +#include "Switch.H" +#include "fvMeshFunctionObject.H" +#include "writeFile.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace functionObjects +{ + +/*---------------------------------------------------------------------------*\ + Class continuityError Declaration +\*---------------------------------------------------------------------------*/ + +class continuityError +: + public fvMeshFunctionObject, + public writeFile +{ +private: + + // Private Member Functions + + //- No copy construct + continuityError(const continuityError&) = delete; + + //- No copy assignment + void operator=(const continuityError&) = delete; + + +protected: + + // Protected data + + //- Name of the flux field; default = "phi + word phiName_; + + //- Cumulative error + scalar cumulative_; + + + // Protected Member Functions + + //- Output file header information + virtual void writeFileHeader(Ostream& os); + + +public: + + //- Runtime type information + TypeName("continuityError"); + + + // Constructors + + //- Construct from Time and dictionary + continuityError + ( + const word& name, + const Time& runTime, + const dictionary& dict + ); + + + //- Destructor + virtual ~continuityError() = default; + + + // Member Functions + + //- Read the field min/max data + virtual bool read(const dictionary&); + + //- Execute, currently does nothing + virtual bool execute(); + + //- Write the continuityError + virtual bool write(); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace functionObjects +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/functionObjects/utilities/Make/files b/src/functionObjects/utilities/Make/files index 8a2d7f9e4b71010ab99b2b3399dd46edf7613df1..a274f616e1c035a8fa081082769e91b75a0b2e86 100644 --- a/src/functionObjects/utilities/Make/files +++ b/src/functionObjects/utilities/Make/files @@ -10,7 +10,7 @@ vtkWrite/vtkWriteUpdate.C removeRegisteredObject/removeRegisteredObject.C -residuals/residuals.C +solverInfo/solverInfo.C runTimeControl/runTimeControl.C runTimeControl/runTimeCondition/runTimeCondition/runTimeCondition.C diff --git a/src/functionObjects/utilities/residuals/residuals.C b/src/functionObjects/utilities/solverInfo/solverInfo.C similarity index 76% rename from src/functionObjects/utilities/residuals/residuals.C rename to src/functionObjects/utilities/solverInfo/solverInfo.C index c06a00837e8321194fc58b9437cb1b99ccae1c6f..6b0c736b85b051d6eeb0676b448ab26ed17e1465 100644 --- a/src/functionObjects/utilities/residuals/residuals.C +++ b/src/functionObjects/utilities/solverInfo/solverInfo.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,7 +23,7 @@ License \*---------------------------------------------------------------------------*/ -#include "residuals.H" +#include "solverInfo.H" #include "addToRunTimeSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -32,12 +32,12 @@ namespace Foam { namespace functionObjects { - defineTypeNameAndDebug(residuals, 0); + defineTypeNameAndDebug(solverInfo, 0); addToRunTimeSelectionTable ( functionObject, - residuals, + solverInfo, dictionary ); } @@ -46,7 +46,7 @@ namespace functionObjects // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // -void Foam::functionObjects::residuals::writeFileHeader(Ostream& os) +void Foam::functionObjects::solverInfo::writeFileHeader(Ostream& os) { if (!fieldSet_.updateSelection()) { @@ -59,7 +59,7 @@ void Foam::functionObjects::residuals::writeFileHeader(Ostream& os) } else { - writeHeader(os, "Residuals"); + writeHeader(os, "Solver information"); } writeCommented(os, "Time"); @@ -79,9 +79,12 @@ void Foam::functionObjects::residuals::writeFileHeader(Ostream& os) } -void Foam::functionObjects::residuals::createField(const word& fieldName) +void Foam::functionObjects::solverInfo::createResidualField +( + const word& fieldName +) { - if (!writeFields_) + if (!writeResidualFields_) { return; } @@ -109,7 +112,10 @@ void Foam::functionObjects::residuals::createField(const word& fieldName) } -void Foam::functionObjects::residuals::writeField(const word& fieldName) const +void Foam::functionObjects::solverInfo::writeResidualField +( + const word& fieldName +) const { const word residualName("initialResidual:" + fieldName); @@ -143,7 +149,7 @@ void Foam::functionObjects::residuals::writeField(const word& fieldName) const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::functionObjects::residuals::residuals +Foam::functionObjects::solverInfo::solverInfo ( const word& name, const Time& runTime, @@ -153,28 +159,23 @@ Foam::functionObjects::residuals::residuals fvMeshFunctionObject(name, runTime, dict), writeFile(obr_, name, typeName, dict), fieldSet_(mesh_), - writeFields_(false), + writeResidualFields_(false), initialised_(false) { read(dict); } -// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // - -Foam::functionObjects::residuals::~residuals() -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -bool Foam::functionObjects::residuals::read(const dictionary& dict) +bool Foam::functionObjects::solverInfo::read(const dictionary& dict) { if (fvMeshFunctionObject::read(dict)) { fieldSet_.read(dict); - writeFields_ = dict.lookupOrDefault("writeFields", false); + writeResidualFields_ = + dict.lookupOrDefault("writeResidualFields", false); return true; } @@ -183,7 +184,7 @@ bool Foam::functionObjects::residuals::read(const dictionary& dict) } -bool Foam::functionObjects::residuals::execute() +bool Foam::functionObjects::solverInfo::execute() { // Note: delaying initialisation until after first iteration so that // we can find wildcard fields @@ -191,15 +192,15 @@ bool Foam::functionObjects::residuals::execute() { writeFileHeader(file()); - if (writeFields_) + if (writeResidualFields_) { for (const word& fieldName : fieldSet_.selectionNames()) { - initialiseField<scalar>(fieldName); - initialiseField<vector>(fieldName); - initialiseField<sphericalTensor>(fieldName); - initialiseField<symmTensor>(fieldName); - initialiseField<tensor>(fieldName); + initialiseResidualField<scalar>(fieldName); + initialiseResidualField<vector>(fieldName); + initialiseResidualField<sphericalTensor>(fieldName); + initialiseResidualField<symmTensor>(fieldName); + initialiseResidualField<tensor>(fieldName); } } @@ -210,17 +211,17 @@ bool Foam::functionObjects::residuals::execute() } -bool Foam::functionObjects::residuals::write() +bool Foam::functionObjects::solverInfo::write() { writeTime(file()); for (const word& fieldName : fieldSet_.selectionNames()) { - writeResidual<scalar>(fieldName); - writeResidual<vector>(fieldName); - writeResidual<sphericalTensor>(fieldName); - writeResidual<symmTensor>(fieldName); - writeResidual<tensor>(fieldName); + writeSolverInfo<scalar>(fieldName); + writeSolverInfo<vector>(fieldName); + writeSolverInfo<sphericalTensor>(fieldName); + writeSolverInfo<symmTensor>(fieldName); + writeSolverInfo<tensor>(fieldName); } file() << endl; diff --git a/src/functionObjects/utilities/residuals/residuals.H b/src/functionObjects/utilities/solverInfo/solverInfo.H similarity index 73% rename from src/functionObjects/utilities/residuals/residuals.H rename to src/functionObjects/utilities/solverInfo/solverInfo.H index f0e6858a6151d2f065aa467b5a760c05f6ecf9f3..ecaf36acf258c2d6fd187ac15e572875351e4c78 100644 --- a/src/functionObjects/utilities/residuals/residuals.H +++ b/src/functionObjects/utilities/solverInfo/solverInfo.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -22,37 +22,44 @@ License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Class - Foam::functionObjects::residuals + Foam::functionObjects::solverInfo Group grpUtilitiesFunctionObjects Description - Writes out the initial residual for specified fields. + Writes solver information for a list of user-specified fields + + Information written to file includes: + - residual fields + - solver type + - initial residual + - final residual + - number of solver iterations + - convergecnce flag Usage Example of function object specification: \verbatim - residuals + solverInfo { - type residuals; + type solverInfo; libs ("libutilityFunctionObjects.so"); ... fields (U p); + writeResidualFields yes; } \endverbatim Where the entries comprise: \table Property | Description | Required | Default value - type | Type name: residua ls | yes | + type | Type name: solverInfo | yes | fields | List of fields to process | yes | - writeFields | Write the residual fields | no | no + writeResidualFields | Write the residual fields | no | no \endtable - Output data is written to the dir postProcessing/residuals/\<timeDir\>/ - For vector/tensor fields, e.g. U, where an equation is solved for each - component, the largest residual of each component is written. + Output data is written to the dir postProcessing/solverInfo/\<timeDir\>/ See also Foam::functionObject @@ -61,12 +68,12 @@ See also Foam::functionObjects::timeControl SourceFiles - residuals.C + solverInfo.C \*---------------------------------------------------------------------------*/ -#ifndef functionObjects_residuals_H -#define functionObjects_residuals_H +#ifndef functionObjects_solverInfo_H +#define functionObjects_solverInfo_H #include "fvMeshFunctionObject.H" #include "writeFile.H" @@ -80,10 +87,10 @@ namespace functionObjects { /*---------------------------------------------------------------------------*\ - Class residuals Declaration + Class solverInfo Declaration \*---------------------------------------------------------------------------*/ -class residuals +class solverInfo : public fvMeshFunctionObject, public writeFile @@ -92,11 +99,11 @@ protected: // Protected data - //- Fields to write residuals + //- Fields to process solverFieldSelection fieldSet_; //- Flag to write the residual as a vol field - bool writeFields_; + bool writeResidualFields_; //- Initialisation flag bool initialised_; @@ -108,10 +115,10 @@ protected: void writeFileHeader(Ostream& os); //- Create and store a residual field on the mesh database - void createField(const word& fieldName); + void createResidualField(const word& fieldName); //- Write a residual field - void writeField(const word& fieldName) const; + void writeResidualField(const word& fieldName) const; //- Output file header information per primitive type value template<class Type> @@ -119,11 +126,11 @@ protected: //- Initialise a residual field template<class Type> - void initialiseField(const word& fieldName); + void initialiseResidualField(const word& fieldName); - //- Calculate the residual + //- Calculate the solver information template<class Type> - void writeResidual(const word& fieldName); + void writeSolverInfo(const word& fieldName); private: @@ -131,22 +138,22 @@ private: // Private member functions //- No copy construct - residuals(const residuals&) = delete; + solverInfo(const solverInfo&) = delete; //- No copy assignment - void operator=(const residuals&) = delete; + void operator=(const solverInfo&) = delete; public: //- Runtime type information - TypeName("residuals"); + TypeName("solverInfo"); // Constructors //- Construct from Time and dictionary - residuals + solverInfo ( const word& name, const Time& runTime, @@ -155,7 +162,7 @@ public: //- Destructor - virtual ~residuals(); + virtual ~solverInfo() = default; // Member Functions @@ -166,7 +173,7 @@ public: //- Execute, currently does nothing virtual bool execute(); - //- Write the residuals + //- Write the solverInfo virtual bool write(); }; @@ -179,7 +186,7 @@ public: // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository - #include "residualsTemplates.C" + #include "solverInfoTemplates.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/functionObjects/utilities/residuals/residualsTemplates.C b/src/functionObjects/utilities/solverInfo/solverInfoTemplates.C similarity index 90% rename from src/functionObjects/utilities/residuals/residualsTemplates.C rename to src/functionObjects/utilities/solverInfo/solverInfoTemplates.C index bfba247840c1a6f881293b4aa48d00cd030db6ee..1b171eb7d812ab8446065b56de655d1a5adf9e06 100644 --- a/src/functionObjects/utilities/residuals/residualsTemplates.C +++ b/src/functionObjects/utilities/solverInfo/solverInfoTemplates.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2015-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -23,7 +23,7 @@ License \*---------------------------------------------------------------------------*/ -#include "residuals.H" +#include "solverInfo.H" #include "volFields.H" #include "ListOps.H" #include "zeroGradientFvPatchField.H" @@ -31,7 +31,7 @@ License // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // template<class Type> -void Foam::functionObjects::residuals::writeFileHeader +void Foam::functionObjects::solverInfo::writeFileHeader ( Ostream& os, const word& fieldName @@ -52,7 +52,7 @@ void Foam::functionObjects::residuals::writeFileHeader { if (component(validComponents, cmpt) != -1) { - const word cmptName(pTraits<Type>::componentNames[cmpt]); + const word cmptName(pTraits<Type>::componentNames[cmpt]); const word fieldBase(fieldName + cmptName); writeTabbed(os, fieldBase + "_initial"); @@ -67,7 +67,10 @@ void Foam::functionObjects::residuals::writeFileHeader template<class Type> -void Foam::functionObjects::residuals::initialiseField(const word& fieldName) +void Foam::functionObjects::solverInfo::initialiseResidualField +( + const word& fieldName +) { typedef GeometricField<Type, fvPatchField, volMesh> volFieldType; @@ -91,7 +94,7 @@ void Foam::functionObjects::residuals::initialiseField(const word& fieldName) fieldName + word(pTraits<Type>::componentNames[cmpt]) ); - createField(resultName); + createResidualField(resultName); } } } @@ -100,7 +103,7 @@ void Foam::functionObjects::residuals::initialiseField(const word& fieldName) template<class Type> -void Foam::functionObjects::residuals::writeResidual(const word& fieldName) +void Foam::functionObjects::solverInfo::writeSolverInfo(const word& fieldName) { typedef GeometricField<Type, fvPatchField, volMesh> volFieldType; typedef typename pTraits<Type>::labelType labelType; @@ -121,7 +124,7 @@ void Foam::functionObjects::residuals::writeResidual(const word& fieldName) const Type& initialResidual = sp0.initialResidual(); const Type& finalResidual = sp0.finalResidual(); const labelType nIterations = sp0.nIterations(); - const bool converged = sp0.converged(); + const Switch converged(sp0.converged()); const labelType validComponents(mesh_.validComponents<Type>()); @@ -140,13 +143,13 @@ void Foam::functionObjects::residuals::writeResidual(const word& fieldName) << token::TAB << rf << token::TAB << n; - const word cmptName(pTraits<Type>::componentNames[cmpt]); + const word cmptName(pTraits<Type>::componentNames[cmpt]); const word resultName(fieldName + cmptName); setResult(resultName + "_initial", ri); setResult(resultName + "_final", rf); setResult(resultName + "_iters", n); - writeField(resultName); + writeResidualField(resultName); } }