From 254d38d772175284a7ac34eadba54598fa3fdffe Mon Sep 17 00:00:00 2001 From: Vaggelis Papoutsis Date: Tue, 31 Dec 2019 20:03:22 +0200 Subject: [PATCH] BUG: continuation of updateMethods with empty activeDesignVariables When activeDesignVariables are not set explicitly, all design variables are treated as active. These were allocated properly when starting from 0 but not when starting from an intermediate optimisation cycle (e.g. running 5 optimisation cycles, stopping and restarting). TUT: added a new tutorial including the restart of an optimisation run to help identify future regression --- .../optimisation/updateMethod/BFGS/BFGS.C | 13 +- .../optimisation/updateMethod/DBFGS/DBFGS.C | 11 +- .../optimisation/updateMethod/LBFGS/LBFGS.C | 11 +- .../optimisation/updateMethod/SQP/SQP.C | 11 +- .../optimisation/updateMethod/SR1/SR1.C | 13 +- .../conjugateGradient/conjugateGradient.C | 11 +- .../sbend/turbulent/opt/BFGS-continuation/0/U | 55 +++++++ .../turbulent/opt/BFGS-continuation/0/Ua | 56 ++++++++ .../turbulent/opt/BFGS-continuation/0/nuTilda | 55 +++++++ .../opt/BFGS-continuation/0/nuaTilda | 60 ++++++++ .../turbulent/opt/BFGS-continuation/0/nut | 55 +++++++ .../sbend/turbulent/opt/BFGS-continuation/0/p | 52 +++++++ .../turbulent/opt/BFGS-continuation/0/pa | 52 +++++++ .../turbulent/opt/BFGS-continuation/Allclean | 10 ++ .../turbulent/opt/BFGS-continuation/Allrun | 17 +++ .../constant/adjointRASProperties | 21 +++ .../constant/dynamicMeshDict | 47 ++++++ .../constant/transportProperties | 21 +++ .../constant/turbulenceProperties | 28 ++++ .../opt/BFGS-continuation/system/controlDict | 57 ++++++++ .../BFGS-continuation/system/decomposeParDict | 26 ++++ .../opt/BFGS-continuation/system/fvSchemes | 71 +++++++++ .../opt/BFGS-continuation/system/fvSolution | 65 +++++++++ .../BFGS-continuation/system/optimisationDict | 136 ++++++++++++++++++ 24 files changed, 922 insertions(+), 32 deletions(-) create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/U create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/Ua create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/nuTilda create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/nuaTilda create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/nut create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/p create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/0/pa create mode 100755 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/Allclean create mode 100755 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/Allrun create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/constant/adjointRASProperties create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/constant/dynamicMeshDict create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/constant/transportProperties create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/constant/turbulenceProperties create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/system/controlDict create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/system/decomposeParDict create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/system/fvSchemes create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/system/fvSolution create mode 100644 tutorials/incompressible/adjointOptimisationFoam/shapeOptimisation/sbend/turbulent/opt/BFGS-continuation/system/optimisationDict diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/BFGS/BFGS.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/BFGS/BFGS.C index 26b80e1803..9cd0671d9d 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/BFGS/BFGS.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/BFGS/BFGS.C @@ -51,11 +51,7 @@ void Foam::BFGS::allocateMatrices() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(correction_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Set previous HessianInv to be a diagonal matrix @@ -155,9 +151,14 @@ void Foam::BFGS::readFromDict() optMethodIODict_.readEntry("counter", counter_); optMethodIODict_.readEntry("eta", eta_); - label n = HessianInvOld_.n(); + const label n(HessianInvOld_.n()); HessianInv_ = SquareMatrix(n, Zero); correction_ = scalarField(correctionOld_.size(), Zero); + + if (activeDesignVars_.empty()) + { + activeDesignVars_ = identity(n); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/DBFGS/DBFGS.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/DBFGS/DBFGS.C index 0e5f0fe06c..fb38d03d12 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/DBFGS/DBFGS.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/DBFGS/DBFGS.C @@ -51,11 +51,7 @@ void Foam::DBFGS::allocateMatrices() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(correction_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Set previous Hessian to be a diagonal matrix @@ -162,6 +158,11 @@ void Foam::DBFGS::readFromDict() label n = HessianOld_.n(); Hessian_ = SquareMatrix(n, Zero); correction_ = scalarField(correctionOld_.size(), Zero); + + if (activeDesignVars_.empty()) + { + activeDesignVars_ = identity(n); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/LBFGS/LBFGS.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/LBFGS/LBFGS.C index c60d094b9f..5478281842 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/LBFGS/LBFGS.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/LBFGS/LBFGS.C @@ -51,11 +51,7 @@ void Foam::LBFGS::allocateMatrices() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(objectiveDerivatives_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Allocate vectors @@ -184,6 +180,11 @@ void Foam::LBFGS::readFromDict() optMethodIODict_.readEntry("correctionOld", correctionOld_); correction_ = scalarField(correctionOld_.size(), Zero); + + if (activeDesignVars_.empty()) + { + activeDesignVars_ = identity(derivativesOld_.size()); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SQP/SQP.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SQP/SQP.C index 8f91c5e227..9d988abe83 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SQP/SQP.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SQP/SQP.C @@ -58,11 +58,7 @@ void Foam::SQP::allocateMatrices() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(correction_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Set previous Hessian to be a diagonal matrix @@ -269,6 +265,11 @@ void Foam::SQP::readFromDict() optMethodIODict_.readEntry("eta", eta_); correction_ = scalarField(correctionOld_.size(), Zero); + + if (activeDesignVars_.empty()) + { + activeDesignVars_ = identity(correction_.size()); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SR1/SR1.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SR1/SR1.C index 69c98a54cb..b582b57a24 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SR1/SR1.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/SR1/SR1.C @@ -51,11 +51,7 @@ void Foam::SR1::allocateMatrices() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(correction_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Set previous HessianInv to be a diagonal matrix @@ -146,9 +142,14 @@ void Foam::SR1::readFromDict() optMethodIODict_.readEntry("counter", counter_); optMethodIODict_.readEntry("eta", eta_); - label n = HessianInvOld_.n(); + const label n(HessianInvOld_.n()); HessianInv_ = SquareMatrix(n, Zero); correction_ = scalarField(correctionOld_.size(), Zero); + + if (activeDesignVars_.empty()) + { + activeDesignVars_ = identity(n); + } } } diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/conjugateGradient/conjugateGradient.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/conjugateGradient/conjugateGradient.C index 629ba8221e..3e8ca3747a 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/conjugateGradient/conjugateGradient.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/updateMethod/conjugateGradient/conjugateGradient.C @@ -51,11 +51,7 @@ void Foam::conjugateGradient::allocateFields() // Set active design variables, if necessary if (activeDesignVars_.empty()) { - activeDesignVars_.setSize(objectiveDerivatives_.size()); - forAll(activeDesignVars_, dvI) - { - activeDesignVars_[dvI] = dvI; - } + activeDesignVars_ = identity(objectiveDerivatives_.size()); } // Allocate old fields @@ -75,6 +71,11 @@ void Foam::conjugateGradient::readFromDict() label nDVs = optMethodIODict_.get