diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.C index 690b2dde1560e0ee732bbb60ce2345950580bbce..0247ae205e30f4f3cc86772e33b73bf8f1c7f26d 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.C @@ -136,82 +136,6 @@ void Foam::designVariablesUpdate::writeToCostFile(bool zeroAdjointSolns) } -void Foam::designVariablesUpdate::checkConvergence -( - const scalarField& oldCorrection -) -{ - bool converged(false); - // Design variables convergence check - if (designVarsThreshold_) - { - const labelList& activeVarIDs = - designVars_->activeDesignVariables(); - const scalarField correction(oldCorrection, activeVarIDs); - const scalarField activeVars(designVars_->getVars(), activeVarIDs); - const scalar scaledCorrection = - gMax(mag(correction)/(mag(activeVars) + SMALL)); - DebugInfo - << "Current correction " << correction << nl - << "Active vars " << activeVars << endl; - Info<< "Max. scaled correction of the design variables = " - << scaledCorrection - << endl; - if (scaledCorrection < designVarsThreshold_()) - { - Info<< tab << "Design variables have converged " << endl; - converged = true; - } - } - // Objective convergence check - if (objectiveThreshold_) - { - const scalar newObjective = computeObjectiveValue(); - const scalar oldObjective = updateMethod_->getObjectiveValueOld(); - const scalar relativeUpdate = - mag(newObjective - oldObjective)/(mag(oldObjective) + SMALL); - Info<< "Relative change of the objective value = " - << relativeUpdate - << endl; - if (relativeUpdate < objectiveThreshold_()) - { - Info<< tab << "Objective function has converged " << endl; - converged = true; - } - } - // Feasibility check - const scalarField& constraints = updateMethod_->getConstraintValues(); - const scalar feasibility = sum(pos(constraints)*constraints); - Info<< "Feasibility = " << feasibility << endl; - if (converged && feasibility < feasibilityThreshold_) - { - Info<< "Stopping criteria met and all constraints satisfied." << nl - << "Optimisation has converged, stopping ..." << nl << nl - << "End" << nl - << endl; - // Force writing of all objective and constraint functions, to get - // the converged results to files - for (adjointSolverManager& am : adjointSolvManagers_) - { - for (adjointSolver& as : am.adjointSolvers()) - { - // Use dummy weighted objective - as.getObjectiveManager().writeObjectives(); - } - } - writeToCostFile(true); - if (UPstream::parRun()) - { - UPstream::exit(0); - } - else - { - std::exit(0); - } - } -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::designVariablesUpdate::designVariablesUpdate @@ -570,10 +494,86 @@ void Foam::designVariablesUpdate::postUpdate(const scalarField& oldCorrection) } } } + checkConvergence(); } - if (convergenceCriteriaDefined_) +} + + +void Foam::designVariablesUpdate::checkConvergence() +{ + if (!convergenceCriteriaDefined_) { - checkConvergence(oldCorrection); + return; + } + + bool converged(false); + const scalarField& oldCorrection = updateMethod_->returnCorrection(); + // Design variables convergence check + if (designVarsThreshold_) + { + const labelList& activeVarIDs = + designVars_->activeDesignVariables(); + const scalarField correction(oldCorrection, activeVarIDs); + const scalarField activeVars(designVars_->getVars(), activeVarIDs); + const scalar scaledCorrection = + gMax(mag(correction)/(mag(activeVars) + SMALL)); + DebugInfo + << "Current correction " << correction << nl + << "Active vars " << activeVars << endl; + Info<< "Max. scaled correction of the design variables = " + << scaledCorrection + << endl; + if (scaledCorrection < designVarsThreshold_()) + { + Info<< tab << "Design variables have converged " << endl; + converged = true; + } + } + // Objective convergence check + if (objectiveThreshold_ && updateMethod_->getObjectiveValueOld()) + { + const scalar newObjective = computeObjectiveValue(); + const scalar oldObjective = updateMethod_->getObjectiveValueOld()(); + const scalar relativeUpdate = + mag(newObjective - oldObjective)/(mag(oldObjective) + SMALL); + Info<< "Relative change of the objective value = " + << relativeUpdate + << endl; + if (relativeUpdate < objectiveThreshold_()) + { + Info<< tab << "Objective function has converged " << endl; + converged = true; + } + } + // Feasibility check + const scalarField& constraints = updateMethod_->getConstraintValues(); + const scalar feasibility = sum(pos(constraints)*constraints); + Info<< "Feasibility = " << feasibility << endl; + if (converged && feasibility < feasibilityThreshold_) + { + Info<< "Stopping criteria met and all constraints satisfied." << nl + << "Optimisation has converged, stopping ..." << nl << nl + << "End" << nl + << endl; + // Force writing of all objective and constraint functions, to get + // the converged results to files + for (adjointSolverManager& am : adjointSolvManagers_) + { + for (adjointSolver& as : am.adjointSolvers()) + { + // Use dummy weighted objective + as.getObjectiveManager().writeObjectives(); + } + } + writeToCostFile(true); + if (UPstream::parRun()) + { + UPstream::exit(0); + } + else + { + std::exit(0); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.H b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.H index 2ed9edddf2b0f9268c6bef087663840fc15595b9..69fd7c39f225eabe85c7123f4bd232b0585ace50 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.H +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/designVariablesUpdate/designVariablesUpdate.H @@ -138,11 +138,6 @@ protected: //- Write to cost file void writeToCostFile(bool zeroAdjointSolns = false); - //- Check if the optimisation loop has converged based on the provided - //- criteria - // May terminate the program - void checkConvergence(const scalarField& oldCorrection); - private: @@ -216,6 +211,11 @@ public: //- in the fixedStep approach void postUpdate(const scalarField& oldCorrection); + //- Check if the optimisation loop has converged based on the provided + //- criteria + // May terminate the program + void checkConvergence(); + //- Get access to design variables inline autoPtr<designVariables>& getDesignVariables() { diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C index 796a373eaa423c39d396ab8f4a6bc0cc92c59fca..20671a895c5a09b4b4cc1351264087df6547e706 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C @@ -172,6 +172,9 @@ void Foam::optimisationManager::fixedStepUpdate() // Solve primal equations solvePrimalEquations(); + // Check the convergence criteria of the optimisation loop + dvUpdate_->checkConvergence(); + // Reset adjoint sensitivities in all adjoint solver managers clearSensitivities(); diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.C index b08a2ffe2c6595f4d5e7bc44d2462d498dd36221..d7d2a78962657520c6679531d813a9bf42f0667b 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.C @@ -237,7 +237,7 @@ Foam::updateMethod::updateMethod objectiveDerivatives_(designVars().getVars().size(), Zero), constraintDerivatives_(0), objectiveValue_(0), - objectiveValueOld_(0), + objectiveValueOld_(nullptr), cValues_(0), correction_(readOrZeroField("correction", designVars().getVars().size())), cumulativeCorrection_(0), @@ -334,7 +334,11 @@ void Foam::updateMethod::setObjectiveValue(const scalar value) void Foam::updateMethod::setObjectiveValueOld(const scalar value) { - objectiveValueOld_ = value; + if (!objectiveValueOld_) + { + objectiveValueOld_.reset(new scalar(Zero)); + } + objectiveValueOld_.ref() = value; } @@ -350,7 +354,8 @@ Foam::scalar Foam::updateMethod::getObjectiveValue() const } -Foam::scalar Foam::updateMethod::getObjectiveValueOld() const +const Foam::autoPtr<Foam::scalar>& +Foam::updateMethod::getObjectiveValueOld() const { return objectiveValueOld_; } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.H b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.H index 49ee8c61c622e2fbd33c813c0a06f9de2d926065..759512346f5e5dde38939e78acf161e0b43627cd 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.H +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/updateMethod/updateMethod.H @@ -84,7 +84,7 @@ protected: //- Old objective value // Used for convergence checking - scalar objectiveValueOld_; + autoPtr<scalar> objectiveValueOld_; //- Constraint values scalarField cValues_; @@ -239,7 +239,7 @@ public: scalar getObjectiveValue() const; //- Get old objective value - scalar getObjectiveValueOld() const; + const autoPtr<scalar>& getObjectiveValueOld() const; //- Get values of constraints const scalarField& getConstraintValues() const;