From 53e8f2a807b619a784ec561147b36a4df141d057 Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 30 Sep 2021 13:37:08 +0100 Subject: [PATCH] ENH: fvSolution: allow Function1 for all scalars TUT: demonstrate some ramping - compressible/rhoPimpleFoam/RAS/angledDuct --- src/OpenFOAM/matrices/solution/solution.C | 72 +++++++++++++++---- src/OpenFOAM/matrices/solution/solution.H | 39 +++++++--- .../matrices/solution/solutionTemplates.C | 6 +- .../RAS/angledDuct/system/fvSolution | 7 +- 4 files changed, 91 insertions(+), 33 deletions(-) diff --git a/src/OpenFOAM/matrices/solution/solution.C b/src/OpenFOAM/matrices/solution/solution.C index 63389c2aa8..1f0c0a6624 100644 --- a/src/OpenFOAM/matrices/solution/solution.C +++ b/src/OpenFOAM/matrices/solution/solution.C @@ -27,6 +27,8 @@ License \*---------------------------------------------------------------------------*/ #include "solution.H" +#include "HashPtrTable.H" +#include "Function1.H" #include "Time.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -55,23 +57,27 @@ void Foam::solution::read(const dictionary& dict) if (dict.found("relaxationFactors")) { - const dictionary& relaxDict(dict.subDict("relaxationFactors")); + const dictionary& relaxDict = dict.subDict("relaxationFactors"); + if (relaxDict.found("fields") || relaxDict.found("equations")) { if (relaxDict.found("fields")) { fieldRelaxDict_ = relaxDict.subDict("fields"); + fieldRelaxCache_.clear(); } if (relaxDict.found("equations")) { eqnRelaxDict_ = relaxDict.subDict("equations"); + eqnRelaxCache_.clear(); } } else { // backwards compatibility fieldRelaxDict_.clear(); + fieldRelaxCache_.clear(); for (const word& e : relaxDict.toc()) { @@ -85,17 +91,38 @@ void Foam::solution::read(const dictionary& dict) { fieldRelaxDict_.add(e, value); } - } eqnRelaxDict_ = relaxDict; + eqnRelaxCache_.clear(); } - fieldRelaxDefault_ = - fieldRelaxDict_.getOrDefault("default", 0); - eqnRelaxDefault_ = - eqnRelaxDict_.getOrDefault("default", 0); + fieldRelaxDefault_ = Function1::NewIfPresent + ( + "default", + fieldRelaxDict_ + ); + if (!fieldRelaxDefault_) + { + fieldRelaxDefault_.reset + ( + new Function1Types::Constant("default", 0) + ); + } + + eqnRelaxDefault_ = Function1::NewIfPresent + ( + "default", + eqnRelaxDict_ + ); + if (!eqnRelaxDefault_) + { + eqnRelaxDefault_.reset + ( + new Function1Types::Constant("default", 0) + ); + } DebugInfo << "Relaxation factors:" << nl @@ -141,8 +168,6 @@ Foam::solution::solution caching_(false), fieldRelaxDict_(), eqnRelaxDict_(), - fieldRelaxDefault_(0), - eqnRelaxDefault_(0), solvers_() { if @@ -157,6 +182,13 @@ Foam::solution::solution } +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +// A non-default destructor since we had incomplete types in the header +Foam::solution::~solution() +{} + + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // Foam::label Foam::solution::upgradeSolverDict @@ -257,11 +289,17 @@ Foam::scalar Foam::solution::fieldRelaxationFactor(const word& name) const if (fieldRelaxDict_.found(name)) { - return fieldRelaxDict_.get(name); + return Function1::New + ( + fieldRelaxCache_, // cache + name, + fieldRelaxDict_, + keyType::REGEX + )().value(time().timeOutputValue()); } - else if (fieldRelaxDefault_ > SMALL) + else if (fieldRelaxDefault_) { - return fieldRelaxDefault_; + return fieldRelaxDefault_().value(time().timeOutputValue()); } FatalIOErrorInFunction(fieldRelaxDict_) @@ -279,11 +317,17 @@ Foam::scalar Foam::solution::equationRelaxationFactor(const word& name) const if (eqnRelaxDict_.found(name)) { - return eqnRelaxDict_.get(name); + return Function1::New + ( + eqnRelaxCache_, // cache + name, + eqnRelaxDict_, + keyType::REGEX + )().value(time().timeOutputValue()); } - else if (eqnRelaxDefault_ > SMALL) + else if (eqnRelaxDefault_) { - return eqnRelaxDefault_; + return eqnRelaxDefault_().value(time().timeOutputValue()); } FatalIOErrorInFunction(eqnRelaxDict_) diff --git a/src/OpenFOAM/matrices/solution/solution.H b/src/OpenFOAM/matrices/solution/solution.H index d9ae37e353..28cfb1e6c2 100644 --- a/src/OpenFOAM/matrices/solution/solution.H +++ b/src/OpenFOAM/matrices/solution/solution.H @@ -39,12 +39,16 @@ SourceFiles #define solution_H #include "IOdictionary.H" +#include "HashPtrTable.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward Declarations +template class Function1; + /*---------------------------------------------------------------------------*\ Class solution Declaration \*---------------------------------------------------------------------------*/ @@ -64,14 +68,20 @@ class solution //- Dictionary of relaxation factors for all the fields dictionary fieldRelaxDict_; + //- Cache of Function1s in above dictionary + mutable HashPtrTable> fieldRelaxCache_; + //- Dictionary of relaxation factors for all the equations dictionary eqnRelaxDict_; + //- Cache of Function1s in above dictionary + mutable HashPtrTable> eqnRelaxCache_; + //- Optional default relaxation factor for all the fields - scalar fieldRelaxDefault_; + autoPtr> fieldRelaxDefault_; //- Optional default relaxation factor for all the equations - scalar eqnRelaxDefault_; + autoPtr> eqnRelaxDefault_; //- Dictionary of solver parameters for all the fields dictionary solvers_; @@ -125,6 +135,10 @@ public: {} + //- Destructor + virtual ~solution(); + + // Member Functions // Access @@ -132,15 +146,6 @@ public: //- Return true if the given field should be cached bool cache(const word& name) const; - //- Helper for printing cache message - template - static void cachePrintMessage - ( - const char* message, - const word& name, - const FieldType& vf - ); - //- Return true if the relaxation factor is given for the field bool relaxField(const word& name) const; @@ -168,6 +173,18 @@ public: //- Read the solution dictionary bool read(); + + + // Other + + //- Helper for printing cache message + template + static void cachePrintMessage + ( + const char* message, + const word& name, + const FieldType& vf + ); }; diff --git a/src/OpenFOAM/matrices/solution/solutionTemplates.C b/src/OpenFOAM/matrices/solution/solutionTemplates.C index 568dd8c5cd..4a72e9c669 100644 --- a/src/OpenFOAM/matrices/solution/solutionTemplates.C +++ b/src/OpenFOAM/matrices/solution/solutionTemplates.C @@ -27,8 +27,7 @@ License #include "solution.H" - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template void Foam::solution::cachePrintMessage @@ -48,7 +47,4 @@ void Foam::solution::cachePrintMessage } -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - // ************************************************************************* // diff --git a/tutorials/compressible/rhoPimpleFoam/RAS/angledDuct/system/fvSolution b/tutorials/compressible/rhoPimpleFoam/RAS/angledDuct/system/fvSolution index 89494939f8..2fe2fb7957 100644 --- a/tutorials/compressible/rhoPimpleFoam/RAS/angledDuct/system/fvSolution +++ b/tutorials/compressible/rhoPimpleFoam/RAS/angledDuct/system/fvSolution @@ -79,9 +79,10 @@ relaxationFactors } equations { - "U.*" 0.7; - "e.*" 0.7; - "(k|epsilon).*" 0.7; + // "(U|e|k|epsilon).*" 0.7; + + // Demonstrate some ramping + "(U|e|k|epsilon).*" table ((0 0.4) (0.5 0.7)); } }