From 46a4446587b42cb23835a89f6789f22ee33f0664 Mon Sep 17 00:00:00 2001 From: Chris Greenshields Date: Sun, 17 May 2015 16:48:55 +0100 Subject: [PATCH] Function object that writes out the initial residual for specified fields For vector/tensor fields, e.g. U, the largest residual of all components is written out --- .../functionObjects/utilities/Make/files | 3 + .../utilities/residuals/IOresiduals.H | 49 +++++ .../utilities/residuals/residuals.C | 153 ++++++++++++++ .../utilities/residuals/residuals.H | 196 ++++++++++++++++++ .../residuals/residualsFunctionObject.C | 42 ++++ .../residuals/residualsFunctionObject.H | 54 +++++ .../utilities/residuals/residualsTemplates.C | 58 ++++++ 7 files changed, 555 insertions(+) create mode 100644 src/postProcessing/functionObjects/utilities/residuals/IOresiduals.H create mode 100644 src/postProcessing/functionObjects/utilities/residuals/residuals.C create mode 100644 src/postProcessing/functionObjects/utilities/residuals/residuals.H create mode 100644 src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.C create mode 100644 src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.H create mode 100644 src/postProcessing/functionObjects/utilities/residuals/residualsTemplates.C diff --git a/src/postProcessing/functionObjects/utilities/Make/files b/src/postProcessing/functionObjects/utilities/Make/files index 43e5bc68eb..4c38b4ba67 100644 --- a/src/postProcessing/functionObjects/utilities/Make/files +++ b/src/postProcessing/functionObjects/utilities/Make/files @@ -21,6 +21,9 @@ dsmcFields/dsmcFieldsFunctionObject.C pressureTools/pressureTools.C pressureTools/pressureToolsFunctionObject.C +residuals/residuals.C +residuals/residualsFunctionObject.C + scalarTransport/scalarTransport.C scalarTransport/scalarTransportFunctionObject.C diff --git a/src/postProcessing/functionObjects/utilities/residuals/IOresiduals.H b/src/postProcessing/functionObjects/utilities/residuals/IOresiduals.H new file mode 100644 index 0000000000..1f74ab27a3 --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/IOresiduals.H @@ -0,0 +1,49 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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 . + +Typedef + Foam::IOresiduals + +Description + Instance of the generic IOOutputFilter for residuals. + +\*---------------------------------------------------------------------------*/ + +#ifndef IOresiduals_H +#define IOresiduals_H + +#include "residuals.H" +#include "IOOutputFilter.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + typedef IOOutputFilter IOresiduals; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/utilities/residuals/residuals.C b/src/postProcessing/functionObjects/utilities/residuals/residuals.C new file mode 100644 index 0000000000..64ac3690b6 --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/residuals.C @@ -0,0 +1,153 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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 "residuals.H" +#include "volFields.H" +#include "dictionary.H" +#include "Time.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(residuals, 0); +} + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::residuals::residuals +( + const word& name, + const objectRegistry& obr, + const dictionary& dict, + const bool loadFromFiles +) +: + functionObjectFile(obr, name, typeName), + name_(name), + obr_(obr), + active_(true), + fieldSet_() +{ + // Check if the available mesh is an fvMesh otherwise deactivate + if (!isA(obr_)) + { + active_ = false; + WarningIn + ( + "residuals::residuals" + "(" + "const word&, " + "const objectRegistry&, " + "const dictionary&, " + "const bool" + ")" + ) << "No fvMesh available, deactivating " << name_ + << endl; + } + + read(dict); +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::residuals::~residuals() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::residuals::read(const dictionary& dict) +{ + if (active_) + { + dict.lookup("fields") >> fieldSet_; + } +} + + +void Foam::residuals::writeFileHeader(const label i) +{ + if (Pstream::master()) + { + writeHeader(file(), "Residuals"); + writeCommented(file(), "Time"); + + forAll(fieldSet_, fieldI) + { + writeTabbed(file(), fieldSet_[fieldI]); + } + + file() << endl; + } +} + + +void Foam::residuals::execute() +{ + // Do nothing - only valid on write +} + + +void Foam::residuals::end() +{ + // Do nothing - only valid on write +} + + +void Foam::residuals::timeSet() +{ + // Do nothing - only valid on write +} + + +void Foam::residuals::write() +{ + if (active_) + { + functionObjectFile::write(); + + if (Pstream::master()) + { + file()<< obr_.time().value(); + + forAll(fieldSet_, fieldI) + { + const word& fieldName = fieldSet_[fieldI]; + + writeResidual(fieldName); + writeResidual(fieldName); + writeResidual(fieldName); + writeResidual(fieldName); + writeResidual(fieldName); + } + + file() << endl; + } + } +} + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/utilities/residuals/residuals.H b/src/postProcessing/functionObjects/utilities/residuals/residuals.H new file mode 100644 index 0000000000..cb3d69cabd --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/residuals.H @@ -0,0 +1,196 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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::residuals + +Group + grpUtilitiesFunctionObjects + +Description + This function object writes out the initial residual for specified fields. + + Example of function object specification: + \verbatim + residuals + { + type residuals; + outputControl timeStep; + outputInterval 1; + fields + ( + U + p + ); + } + \endverbatim + + Output data is written to the dir postProcessing/residuals/\/ + For vector/tensor fields, e.g. U, where an equation is solved for each + component, the largest residual of each component is written out. + +SeeAlso + Foam::functionObject + Foam::OutputFilterFunctionObject + +SourceFiles + residuals.C + IOresiduals.H + +\*---------------------------------------------------------------------------*/ + +#ifndef residuals_H +#define residuals_H + +#include "functionObjectFile.H" +#include "primitiveFieldsFwd.H" +#include "volFieldsFwd.H" +#include "HashSet.H" +#include "OFstream.H" +#include "Switch.H" +#include "NamedEnum.H" +#include "solverPerformance.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of classes +class objectRegistry; +class dictionary; +class polyMesh; +class mapPolyMesh; + +/*---------------------------------------------------------------------------*\ + Class residuals Declaration +\*---------------------------------------------------------------------------*/ + +class residuals +: + public functionObjectFile +{ +protected: + + // Protected data + + //- Name of this set of residuals + // Also used as the name of the output directory + word name_; + + const objectRegistry& obr_; + + //- on/off switch + bool active_; + + //- Fields to write residuals + wordList fieldSet_; + + + // Private Member Functions + + //- Disallow default bitwise copy construct + residuals(const residuals&); + + //- Disallow default bitwise assignment + void operator=(const residuals&); + + //- Output file header information + virtual void writeFileHeader(const label i); + + +public: + + //- Runtime type information + TypeName("residuals"); + + + // Constructors + + //- Construct for given objectRegistry and dictionary. + // Allow the possibility to load fields from files + residuals + ( + const word& name, + const objectRegistry&, + const dictionary&, + const bool loadFromFiles = false + ); + + + //- Destructor + virtual ~residuals(); + + + // Member Functions + + //- Return name of the set of field min/max + virtual const word& name() const + { + return name_; + } + + //- Read the field min/max data + virtual void read(const dictionary&); + + //- Execute, currently does nothing + virtual void execute(); + + //- Execute at the final time-loop, currently does nothing + virtual void end(); + + //- Called when time was set at the end of the Time::operator++ + virtual void timeSet(); + + //- Calculate the field min/max + template + void writeResidual(const word& fieldName); + + //- Write the residuals + virtual void write(); + + //- Update for changes of mesh + virtual void updateMesh(const mapPolyMesh&) + {} + + //- Update for changes of mesh + virtual void movePoints(const polyMesh&) + {} +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository +# include "residualsTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.C b/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.C new file mode 100644 index 0000000000..ffec5c4604 --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.C @@ -0,0 +1,42 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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 "residualsFunctionObject.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineNamedTemplateTypeNameAndDebug(residualsFunctionObject, 0); + + addToRunTimeSelectionTable + ( + functionObject, + residualsFunctionObject, + dictionary + ); +} + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.H b/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.H new file mode 100644 index 0000000000..9bc49f984e --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/residualsFunctionObject.H @@ -0,0 +1,54 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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 . + +Typedef + Foam::residualsFunctionObject + +Description + FunctionObject wrapper around residuals to allow them to be created via + the functions entry within controlDict. + +SourceFiles + residualsFunctionObject.C + +\*---------------------------------------------------------------------------*/ + +#ifndef residualsFunctionObject_H +#define residualsFunctionObject_H + +#include "residuals.H" +#include "OutputFilterFunctionObject.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + typedef OutputFilterFunctionObject + residualsFunctionObject; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/postProcessing/functionObjects/utilities/residuals/residualsTemplates.C b/src/postProcessing/functionObjects/utilities/residuals/residualsTemplates.C new file mode 100644 index 0000000000..4d7c0c032e --- /dev/null +++ b/src/postProcessing/functionObjects/utilities/residuals/residualsTemplates.C @@ -0,0 +1,58 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 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 "residuals.H" +#include "volFields.H" +#include "dictionary.H" +#include "Time.H" +#include "ListOps.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template +void Foam::residuals::writeResidual +( + const word& fieldName +) +{ + typedef GeometricField fieldType; + + if (obr_.foundObject(fieldName)) + { + const fieldType& field = obr_.lookupObject(fieldName); + const fvMesh& mesh = field.mesh(); + const Foam::dictionary& solverDict = mesh.solverPerformanceDict(); + + if (solverDict.found(fieldName)) + { + const List sp(solverDict.lookup(fieldName)); + const scalar residual = sp.first().initialResidual(); + file() << token::TAB << residual; + } + } +} + + +// ************************************************************************* //