From bc952e63dedde46fb2005a7457a0ca9a338e6776 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 8 Sep 2011 11:42:05 +0100 Subject: [PATCH] ENH: storePrevIter now handled by solutionControl class --- .../solvers/compressible/rhoSimpleFoam/pEqn.H | 2 +- .../adjointShapeOptimizationFoam.C | 2 +- .../GeometricField/GeometricField.C | 4 +- src/OpenFOAM/matrices/solution/solution.C | 123 +++++++++++++++--- src/OpenFOAM/matrices/solution/solution.H | 21 ++- .../pimpleControl/pimpleControlI.H | 5 + .../simpleControl/simpleControlI.H | 6 + .../solutionControl/solutionControl.C | 11 ++ .../solutionControl/solutionControl.H | 13 ++ .../solutionControlTemplates.C | 58 +++++++++ .../fvMatrices/fvMatrix/fvMatrix.C | 4 +- 11 files changed, 219 insertions(+), 30 deletions(-) create mode 100644 src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControlTemplates.C diff --git a/applications/solvers/compressible/rhoSimpleFoam/pEqn.H b/applications/solvers/compressible/rhoSimpleFoam/pEqn.H index 54af64c003..df120f6dea 100644 --- a/applications/solvers/compressible/rhoSimpleFoam/pEqn.H +++ b/applications/solvers/compressible/rhoSimpleFoam/pEqn.H @@ -26,7 +26,7 @@ if (simple.transonic()) ); // Relax the pressure equation to ensure diagonal-dominance - pEqn.relax(mesh.relaxationFactor("pEqn")); + pEqn.relax(mesh.equationRelaxationFactor("pEqn")); pEqn.setReference(pRefCell, pRefValue); diff --git a/applications/solvers/incompressible/adjointShapeOptimizationFoam/adjointShapeOptimizationFoam.C b/applications/solvers/incompressible/adjointShapeOptimizationFoam/adjointShapeOptimizationFoam.C index 0f0c65355f..9f0171a02f 100644 --- a/applications/solvers/incompressible/adjointShapeOptimizationFoam/adjointShapeOptimizationFoam.C +++ b/applications/solvers/incompressible/adjointShapeOptimizationFoam/adjointShapeOptimizationFoam.C @@ -93,7 +93,7 @@ int main(int argc, char *argv[]) // mesh.relaxationFactor("alpha") // *(lambda*max(Ua & U, zeroSensitivity) - alpha); alpha += - mesh.relaxationFactor("alpha") + mesh.fieldRelaxationFactor("alpha") *(min(max(alpha + lambda*(Ua & U), zeroAlpha), alphaMax) - alpha); zeroCells(alpha, inletCells); diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C index 28d718b67f..8898b162f4 100644 --- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C +++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricField.C @@ -919,9 +919,9 @@ void Foam::GeometricField::relax() name += "Final"; } - if (this->mesh().relax(name)) + if (this->mesh().relaxField(name)) { - relax(this->mesh().relaxationFactor(name)); + relax(this->mesh().fieldRelaxationFactor(name)); } } diff --git a/src/OpenFOAM/matrices/solution/solution.C b/src/OpenFOAM/matrices/solution/solution.C index 79090bdbd9..aa4676f7e5 100644 --- a/src/OpenFOAM/matrices/solution/solution.C +++ b/src/OpenFOAM/matrices/solution/solution.C @@ -56,10 +56,52 @@ void Foam::solution::read(const dictionary& dict) if (dict.found("relaxationFactors")) { - relaxationFactors_ = dict.subDict("relaxationFactors"); + const dictionary& relaxDict(dict.subDict("relaxationFactors")); + if (relaxDict.found("variables")) + { + fieldRelaxDict_ = relaxDict.subDict("variables"); + eqnRelaxDict_ = relaxDict.subDict("equations"); + } + else + { + // backwards compatibility + const wordList entryNames(relaxDict.toc()); + forAll(entryNames, i) + { + const word& e = entryNames[i]; + scalar value = readScalar(relaxDict.lookup(e)); + + if (e(0, 1) == "p") + { + fieldRelaxDict_.add(e, value); + } + else if (e.length() >= 3) + { + if (e(0, 3) == "rho") + { + fieldRelaxDict_.add(e, value); + } + } + + } + + eqnRelaxDict_ = relaxDict; + } + + fieldRelaxDefault_ = + fieldRelaxDict_.lookupOrDefault("default", 0.0); + + eqnRelaxDefault_ = + eqnRelaxDict_.lookupOrDefault("default", 0.0); + + if (debug) + { + Info<< "relaxation factors:" << nl + << "fields: " << fieldRelaxDict_ << nl + << "equations: " << eqnRelaxDict_ << endl; + } } - relaxationFactors_.readIfPresent("default", defaultRelaxationFactor_); if (dict.found("solvers")) { @@ -92,11 +134,13 @@ Foam::solution::solution IOobject::NO_WRITE ) ), - cache_(ITstream("cache", tokenList())()), + cache_(dictionary::null), caching_(false), - relaxationFactors_(ITstream("relaxationFactors", tokenList())()), - defaultRelaxationFactor_(0), - solvers_(ITstream("solvers", tokenList())()) + fieldRelaxDict_(dictionary::null), + eqnRelaxDict_(dictionary::null), + fieldRelaxDefault_(0), + eqnRelaxDefault_(0), + solvers_(dictionary::null) { if ( @@ -207,41 +251,80 @@ bool Foam::solution::cache(const word& name) const } -bool Foam::solution::relax(const word& name) const +bool Foam::solution::relaxField(const word& name) const { if (debug) { - Info<< "Find relax for " << name << endl; + Info<< "Find variable relaxation factor for " << name << endl; } - return - relaxationFactors_.found(name) - || relaxationFactors_.found("default"); + return fieldRelaxDict_.found(name) || fieldRelaxDict_.found("default"); } -Foam::scalar Foam::solution::relaxationFactor(const word& name) const +bool Foam::solution::relaxEquation(const word& name) const { if (debug) { - Info<< "Lookup relaxationFactor for " << name << endl; + Info<< "Find equation relaxation factor for " << name << endl; } - if (relaxationFactors_.found(name)) + return eqnRelaxDict_.found(name) || eqnRelaxDict_.found("default"); +} + + +Foam::scalar Foam::solution::fieldRelaxationFactor(const word& name) const +{ + if (debug) { - return readScalar(relaxationFactors_.lookup(name)); + Info<< "Lookup variable relaxation factor for " << name << endl; } - else if (defaultRelaxationFactor_ > SMALL) + + if (fieldRelaxDict_.found(name)) { - return defaultRelaxationFactor_; + return readScalar(fieldRelaxDict_.lookup(name)); + } + else if (fieldRelaxDefault_ > SMALL) + { + return fieldRelaxDefault_; } else { FatalIOErrorIn ( - "Foam::solution::relaxationFactor(const word&)", - relaxationFactors_ - ) << "Cannot find relaxationFactor for '" << name + "Foam::solution::fieldRelaxationFactor(const word&)", + fieldRelaxDict_ + ) << "Cannot find variable relaxation factor for '" << name + << "' or a suitable default value." + << exit(FatalIOError); + + return 0; + } +} + + +Foam::scalar Foam::solution::equationRelaxationFactor(const word& name) const +{ + if (debug) + { + Info<< "Lookup equation relaxation factor for " << name << endl; + } + + if (eqnRelaxDict_.found(name)) + { + return readScalar(eqnRelaxDict_.lookup(name)); + } + else if (eqnRelaxDefault_ > SMALL) + { + return eqnRelaxDefault_; + } + else + { + FatalIOErrorIn + ( + "Foam::solution::eqnRelaxationFactor(const word&)", + eqnRelaxDict_ + ) << "Cannot find equation relaxation factor for '" << name << "' or a suitable default value." << exit(FatalIOError); diff --git a/src/OpenFOAM/matrices/solution/solution.H b/src/OpenFOAM/matrices/solution/solution.H index 627e83e49b..68bb0d8d7d 100644 --- a/src/OpenFOAM/matrices/solution/solution.H +++ b/src/OpenFOAM/matrices/solution/solution.H @@ -59,10 +59,16 @@ class solution bool caching_; //- Dictionary of relaxation factors for all the fields - dictionary relaxationFactors_; + dictionary fieldRelaxDict_; + + //- Dictionary of relaxation factors for all the equations + dictionary eqnRelaxDict_; //- Optional default relaxation factor for all the fields - scalar defaultRelaxationFactor_; + scalar fieldRelaxDefault_; + + //- Optional default relaxation factor for all the equations + scalar eqnRelaxDefault_; //- Dictionary of solver parameters for all the fields dictionary solvers_; @@ -107,10 +113,16 @@ public: bool cache(const word& name) const; //- Return true if the relaxation factor is given for the field - bool relax(const word& name) const; + bool relaxField(const word& name) const; + + //- Return true if the relaxation factor is given for the equation + bool relaxEquation(const word& name) const; //- Return the relaxation factor for the given field - scalar relaxationFactor(const word& name) const; + scalar fieldRelaxationFactor(const word& name) const; + + //- Return the relaxation factor for the given eqation + scalar equationRelaxationFactor(const word& name) const; //- Return the selected sub-dictionary of solvers if the "select" // keyword is given, otherwise return the complete dictionary @@ -122,6 +134,7 @@ public: //- Return the solver controls dictionary for the given field const dictionary& solver(const word& name) const; + // Read //- Read the solution dictionary diff --git a/src/finiteVolume/cfdTools/general/solutionControl/pimpleControl/pimpleControlI.H b/src/finiteVolume/cfdTools/general/solutionControl/pimpleControl/pimpleControlI.H index 471c97bf35..cc4062356f 100644 --- a/src/finiteVolume/cfdTools/general/solutionControl/pimpleControl/pimpleControlI.H +++ b/src/finiteVolume/cfdTools/general/solutionControl/pimpleControl/pimpleControlI.H @@ -75,6 +75,11 @@ inline bool Foam::pimpleControl::loop() Info<< algorithmName_ << ": iteration " << corr_ + 1 << endl; } + if (corr_ != 0) + { + storePrevIterFields(); + } + return true; } else diff --git a/src/finiteVolume/cfdTools/general/solutionControl/simpleControl/simpleControlI.H b/src/finiteVolume/cfdTools/general/solutionControl/simpleControl/simpleControlI.H index db807b5eda..9080c9b245 100644 --- a/src/finiteVolume/cfdTools/general/solutionControl/simpleControl/simpleControlI.H +++ b/src/finiteVolume/cfdTools/general/solutionControl/simpleControl/simpleControlI.H @@ -43,12 +43,18 @@ inline bool Foam::simpleControl::loop() // Set to finalise calculation time.writeAndEnd(); } + else + { + storePrevIterFields(); + } } else { initialised_ = true; + storePrevIterFields(); } + return time.loop(); } diff --git a/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControl.C b/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControl.C index ef5515f3aa..e1ea4007d5 100644 --- a/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControl.C +++ b/src/finiteVolume/cfdTools/general/solutionControl/solutionControl/solutionControl.C @@ -102,6 +102,17 @@ Foam::label Foam::solutionControl::applyToField(const word& fieldName) const } +void Foam::solutionControl::storePrevIterFields() const +{ +// storePrevIter