From 4d63b39e3e121f0281ade5745db409daa6aa3bb0 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Wed, 8 Mar 2023 10:59:13 +0000 Subject: [PATCH] foamMultiRun: Added automatic region prefixing to the Info statements in the log e.g. for the rivuletBox case the output for a time-step now looks like: film Courant Number mean: 0.0003701330848 max: 0.1862204919 panel Diffusion Number mean: 0.007352456305 max: 0.1276468109 box Courant Number mean: 0.006324172752 max: 0.09030825997 deltaT = 0.001550908752 Time = 0.08294s film diagonal: Solving for alpha, Initial residual = 0, Final residual = 0, No Iterations 0 film diagonal: Solving for alpha, Initial residual = 0, Final residual = 0, No Iterations 0 box diagonal: Solving for rho, Initial residual = 0, Final residual = 0, No Iterations 0 film DILUPBiCGStab: Solving for Ux, Initial residual = 0.009869417958, Final residual = 2.132619614e-11, No Iterations 2 film DILUPBiCGStab: Solving for Uy, Initial residual = 0.0002799662756, Final residual = 6.101011285e-12, No Iterations 1 film DILUPBiCGStab: Solving for Uz, Initial residual = 1, Final residual = 1.854120599e-12, No Iterations 2 box DILUPBiCGStab: Solving for Ux, Initial residual = 0.004071057403, Final residual = 4.79249226e-07, No Iterations 1 box DILUPBiCGStab: Solving for Uy, Initial residual = 0.006370817152, Final residual = 9.606673696e-07, No Iterations 1 box DILUPBiCGStab: Solving for Uz, Initial residual = 0.0158299327, Final residual = 2.104129791e-06, No Iterations 1 film DILUPBiCGStab: Solving for e, Initial residual = 0.0002888908396, Final residual = 2.301587523e-11, No Iterations 1 panel GAMG: Solving for e, Initial residual = 0.00878508958, Final residual = 7.807579738e-12, No Iterations 1 box DILUPBiCGStab: Solving for h, Initial residual = 0.004403989559, Final residual = 1.334113552e-06, No Iterations 1 film DILUPBiCGStab: Solving for alpha, Initial residual = 0.0002760406755, Final residual = 2.267583256e-14, No Iterations 1 film time step continuity errors : sum local = 9.01334987e-12, global = 2.296671859e-13, cumulative = 1.907846466e-08 box GAMG: Solving for p_rgh, Initial residual = 0.002842335602, Final residual = 1.036572819e-05, No Iterations 4 box diagonal: Solving for rho, Initial residual = 0, Final residual = 0, No Iterations 0 box time step continuity errors : sum local = 4.538744531e-07, global = 1.922637799e-08, cumulative = -6.612579497e-09 box GAMG: Solving for p_rgh, Initial residual = 1.283128787e-05, Final residual = 7.063185653e-07, No Iterations 2 box diagonal: Solving for rho, Initial residual = 0, Final residual = 0, No Iterations 0 box time step continuity errors : sum local = 3.069629869e-08, global = 3.780547824e-10, cumulative = -6.234524715e-09 ExecutionTime = 19.382601 s ClockTime = 20 s film Courant Number mean: 0.0003684434169 max: 0.1840342756 panel Diffusion Number mean: 0.007352456305 max: 0.1276468109 box Courant Number mean: 0.006292704463 max: 0.09016861809 deltaT = 0.001550908752 Time = 0.0844909s where each line printed by each region solver is prefixed by the region name. Global messages for the time-step and time are just prefixed with spaces to align them with the region output. --- .../solvers/foamMultiRun/foamMultiRun.C | 36 +---- .../regionSolvers/regionSolvers.C | 90 +++++++++++- .../regionSolvers/regionSolvers.H | 128 +++++++++++++++- .../regionSolvers/regionSolversI.H | 138 ++++++++++++++++++ src/OpenFOAM/db/IOstreams/IOstreams.C | 4 +- src/OpenFOAM/db/IOstreams/IOstreams.H | 4 +- 6 files changed, 353 insertions(+), 47 deletions(-) create mode 100644 applications/solvers/foamMultiRun/regionSolvers/regionSolversI.H diff --git a/applications/solvers/foamMultiRun/foamMultiRun.C b/applications/solvers/foamMultiRun/foamMultiRun.C index 857d668cb7..3ceff44f74 100644 --- a/applications/solvers/foamMultiRun/foamMultiRun.C +++ b/applications/solvers/foamMultiRun/foamMultiRun.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -84,7 +84,6 @@ Usage \*---------------------------------------------------------------------------*/ #include "argList.H" -#include "solver.H" #include "regionSolvers.H" #include "pimpleMultiRegionControl.H" #include "setDeltaT.H" @@ -98,33 +97,8 @@ int main(int argc, char *argv[]) #include "setRootCase.H" #include "createTime.H" - regionSolvers regionSolvers(runTime); - PtrList regions(regionSolvers.size()); - PtrList solvers(regionSolvers.size()); - - forAll(regionSolvers, i) - { - regions.set - ( - i, - new fvMesh - ( - IOobject - ( - regionSolvers[i].first(), - runTime.name(), - runTime, - IOobject::MUST_READ - ) - ) - ); - - solvers.set - ( - i, - solver::New(regionSolvers[i].second(), regions[i]) - ); - } + // Create the region meshes and solvers + regionSolvers solvers(runTime); // Create the outer PIMPLE loop and control structure pimpleMultiRegionControl pimple(runTime, solvers); @@ -143,6 +117,8 @@ int main(int argc, char *argv[]) solvers[i].preSolve(); } + solvers.setGlobalPrefix(); + // Adjust the time-step according to the solver maxDeltaT adjustDeltaT(runTime, solvers); @@ -192,6 +168,8 @@ int main(int argc, char *argv[]) solvers[i].postSolve(); } + solvers.setGlobalPrefix(); + runTime.write(); Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s" diff --git a/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.C b/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.C index e45390648c..1fc8d5a125 100644 --- a/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.C +++ b/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.C @@ -31,6 +31,8 @@ License Foam::regionSolvers::regionSolvers(const Time& runTime) { + List> regionSolverNames; + if (runTime.controlDict().found("regionSolvers")) { const dictionary& regionSolversDict = @@ -41,10 +43,7 @@ Foam::regionSolvers::regionSolvers(const Time& runTime) const word regionName(iter().keyword()); const word solverName(iter().stream()); - // Load the solver library - solver::load(solverName); - - append(Pair(regionName, solverName)); + regionSolverNames.append(Pair(regionName, solverName)); } } else @@ -77,7 +76,10 @@ Foam::regionSolvers::regionSolvers(const Time& runTime) const wordList& fluidRegions = regions["solid"]; forAll(fluidRegions, i) { - append(Pair(fluidRegions[i], "solid")); + regionSolverNames.append + ( + Pair(fluidRegions[i], "solid") + ); } } @@ -86,7 +88,10 @@ Foam::regionSolvers::regionSolvers(const Time& runTime) const wordList& fluidRegions = regions["fluid"]; forAll(fluidRegions, i) { - append(Pair(fluidRegions[i], "fluid")); + regionSolverNames.append + ( + Pair(fluidRegions[i], "fluid") + ); } } } @@ -98,6 +103,50 @@ Foam::regionSolvers::regionSolvers(const Time& runTime) << exit(FatalIOError); } } + + regions_.setSize(regionSolverNames.size()); + solvers_.setSize(regionSolverNames.size()); + prefixes_.setSize(regionSolverNames.size()); + + string::size_type nRegionNameChars = 0; + + forAll(regionSolverNames, i) + { + const word& regionName = regionSolverNames[i].first(); + const word& solverName = regionSolverNames[i].second(); + + // Load the solver library + solver::load(solverName); + + regions_.set + ( + i, + new fvMesh + ( + IOobject + ( + regionName, + runTime.name(), + runTime, + IOobject::MUST_READ + ) + ) + ); + + solvers_.set(i, solver::New(solverName, regions_[i])); + + prefixes_[i] = regionName; + nRegionNameChars = max(nRegionNameChars, regionName.size()); + } + + nRegionNameChars++; + + prefix0_.append(nRegionNameChars, ' '); + + forAll(regionSolverNames, i) + { + prefixes_[i].append(nRegionNameChars - prefixes_[i].size(), ' '); + } } @@ -107,4 +156,33 @@ Foam::regionSolvers::~regionSolvers() {} +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +void Foam::regionSolvers::setGlobalPrefix() const +{ + Sout.prefix() = prefix0_; +} + + +void Foam::regionSolvers::setPrefix(const label i) const +{ + Sout.prefix() = prefixes_[i]; +} + + +void Foam::regionSolvers::resetPrefix() const +{ + Sout.prefix() = string::null; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +Foam::solver& Foam::regionSolvers::operator[](const label i) +{ + setPrefix(i); + return solvers_[i]; +} + + // ************************************************************************* // diff --git a/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.H b/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.H index a15888ed47..b243f04087 100644 --- a/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.H +++ b/applications/solvers/foamMultiRun/regionSolvers/regionSolvers.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2022 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -25,7 +25,40 @@ Class Foam::regionSolvers Description - Simple class to hold the list of region solvers + Class to hold the lists of region meshes and solvers + + Also provides loop and iteration functionality which automatically set the + region Info prefix for each of the solvers returned by the '[]' operator or + iterator. + +Usage + Given the \c regionSolvers named solvers: + \verbatim + // Create the region meshes and solvers + regionSolvers solvers(runTime); + \endverbatim + + The list of solvers can be looped over: + \verbatim + forAll(solvers, i) + { + solvers[i].momentumPredictor(); + } + \endverbatim + where the '[]' operator sets the region Info prefix. After the loop the + region Info prefix remains set to the last region prefix and so for global + messages, e.g. the global time-step the Info prefix must be specifically + reset to spaces by calling the \c setGlobalPrefix() function. + + Alternatively the list of solvers can be iterated over: + \verbatim + forAllIter(regionSolvers, solvers, solver) + { + solver->momentumPredictor(); + } + \endverbatim + where the iterator increment sets the region \c Info prefix and at the end + automatically resets the \c Info prefix to spaces. SourceFiles regionSolvers.C @@ -35,24 +68,34 @@ SourceFiles #ifndef regionSolvers_H #define regionSolvers_H -#include "word.H" -#include "Pair.H" +#include "solver.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { -class Time; - /*---------------------------------------------------------------------------*\ Class regionSolvers Declaration \*---------------------------------------------------------------------------*/ class regionSolvers -: - public List> { + // Private Member Data + + //- Region meshes + PtrList regions_; + + //- Region solvers + PtrList solvers_; + + //- Global space padding prefix + string prefix0_; + + //- List of space padded region prefixes + stringList prefixes_; + + public: // Constructors @@ -68,12 +111,81 @@ public: ~regionSolvers(); + // Member Functions + + //- Return the number of region solvers + inline label size() const; + + //- Set the Info prefix to space padding for global messages + void setGlobalPrefix() const; + + //- Set the Info prefix to the space padded region name + void setPrefix(const label i) const; + + //- Reset the Info prefix to null + void resetPrefix() const; + + + // Iterator + + class iterator; + friend class iterator; + + // Iterator class to loop over the solvers, setting the prefix for each + class iterator + { + // Private Data + + //- Reference to the regionSolvers + regionSolvers& regionSolvers_; + + //- Current solver index + label index_; + + public: + + friend class regionSolvers; + + // Constructors + + //- Construct for the regionSolvers + inline explicit iterator(regionSolvers&); + + // Member Operators + + inline bool operator==(const iterator&) const; + inline bool operator!=(const iterator&) const; + + inline solver& operator*(); + inline solver& operator()(); + inline solver* operator->(); + + inline iterator operator++(); + inline iterator operator++(int); + }; + + //- Return an iterator to begin traversing the solvers + inline iterator begin(); + + //- Return an iterator to end traversing the solvers + inline iterator end(); + + // Member Operators + //- Cast to the list of solvers + inline operator PtrList&(); + + //- Set the region i prefix and return the corresponding solver + solver& operator[](const label i); + //- Disallow default bitwise assignment void operator=(const regionSolvers&) = delete; }; +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "regionSolversI.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/solvers/foamMultiRun/regionSolvers/regionSolversI.H b/applications/solvers/foamMultiRun/regionSolvers/regionSolversI.H new file mode 100644 index 0000000000..f41f9eaacd --- /dev/null +++ b/applications/solvers/foamMultiRun/regionSolvers/regionSolversI.H @@ -0,0 +1,138 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | Website: https://openfoam.org + \\ / A nd | Copyright (C) 2023 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 . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline Foam::label Foam::regionSolvers::size() const +{ + return solvers_.size(); +} + + +// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * // + +inline Foam::regionSolvers::iterator::iterator(regionSolvers& rs) +: + regionSolvers_(rs), + index_(0) +{} + + +inline bool Foam::regionSolvers::iterator::operator== +( + const iterator& iter +) const +{ + return index_ == iter.index_; +} + + +inline bool Foam::regionSolvers::iterator::operator!= +( + const iterator& iter +) const +{ + return !operator==(iter); +} + + +inline Foam::solver& Foam::regionSolvers::iterator::operator*() +{ + return regionSolvers_.solvers_[index_]; +} + + +inline Foam::solver& Foam::regionSolvers::iterator::operator()() +{ + return regionSolvers_.solvers_[index_]; +} + + +inline Foam::solver* Foam::regionSolvers::iterator::operator->() +{ + return regionSolvers_.solvers_(index_); +} + + +inline Foam::regionSolvers::iterator +Foam::regionSolvers::iterator::operator++() +{ + if (++index_ >= regionSolvers_.solvers_.size()) + { + // Set index to -1 to indicate end + index_ = -1; + + // Reset the prefix to global space padding + regionSolvers_.setGlobalPrefix(); + } + else + { + // Set the prefix for region corresponding to index_ + regionSolvers_.setPrefix(index_); + } + + return *this; +} + + +inline Foam::regionSolvers::iterator +Foam::regionSolvers::iterator::operator++(int) +{ + iterator tmp(*this); + operator++(); + return tmp; +} + + +inline Foam::regionSolvers::iterator Foam::regionSolvers::begin() +{ + // Set the prefix for region 0 + setPrefix(0); + + // Return the iterator for region 0 + return iterator(*this); +} + + +inline Foam::regionSolvers::iterator Foam::regionSolvers::end() +{ + iterator endIter(*this); + + // Set index to -1 to indicate end + endIter.index_ = -1; + + return endIter; +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline Foam::regionSolvers::operator PtrList&() +{ + return solvers_; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/IOstreams/IOstreams.C b/src/OpenFOAM/db/IOstreams/IOstreams.C index ba2c9b5ad9..f8348d9438 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams.C +++ b/src/OpenFOAM/db/IOstreams/IOstreams.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -42,7 +42,7 @@ unsigned int IOstream::precision_(debug::infoSwitch("writePrecision", 6)); // Global IO streams ISstream Sin(cin, "Sin"); -OSstream Sout(cout, "Sout"); +prefixOSstream Sout(cout, "Sout"); OSstream Serr(cerr, "Serr"); OFstream Snull("/dev/null"); diff --git a/src/OpenFOAM/db/IOstreams/IOstreams.H b/src/OpenFOAM/db/IOstreams/IOstreams.H index 44529b1c75..c1cce48519 100644 --- a/src/OpenFOAM/db/IOstreams/IOstreams.H +++ b/src/OpenFOAM/db/IOstreams/IOstreams.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -48,7 +48,7 @@ Description namespace Foam { extern ISstream Sin; - extern OSstream Sout; + extern prefixOSstream Sout; extern OSstream Serr; extern prefixOSstream Pout; extern prefixOSstream Perr;