DBFGS.C 7.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2007-2019 PCOpt/NTUA
    Copyright (C) 2013-2019 FOSS GP
10
    Copyright (C) 2019-2020 OpenCFD Ltd.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
-------------------------------------------------------------------------------
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 <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "DBFGS.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineTypeNameAndDebug(DBFGS, 0);
    addToRunTimeSelectionTable
    (
        updateMethod,
        DBFGS,
        dictionary
    );
}


// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

void Foam::DBFGS::allocateMatrices()
{
    // Set active design variables, if necessary
    if (activeDesignVars_.empty())
    {
54
        activeDesignVars_ = identity(objectiveDerivatives_.size());
55 56 57
    }

    // Set previous Hessian to be a diagonal matrix
58
    SquareMatrix<scalar> temp(activeDesignVars_.size(), I);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    // Allocate correct size and content to Hessian matrices
    // has a max. capability of approximately 34000 variables.
    HessianOld_ = temp;
    Hessian_ = temp;
}


void Foam::DBFGS::updateHessian()
{
    // Vectors needed to construct the inverse Hessian matrix
    scalarField y(activeDesignVars_.size(), Zero);
    scalarField s(activeDesignVars_.size(), Zero);
    y.map(objectiveDerivatives_ - derivativesOld_, activeDesignVars_);
    s.map(correctionOld_, activeDesignVars_);

    scalar ys = globalSum(s*y);

    if (counter_ == 1 && scaleFirstHessian_)
    {
        scalar scaleFactor = ys/globalSum(y*y);
        Info<< "Scaling Hessian with factor " << scaleFactor << endl;
        forAll(activeDesignVars_, varI)
        {
            HessianOld_[varI][varI] /= scaleFactor;
        }
    }

    scalar sBs = globalSum(leftMult(s, HessianOld_)*s);

    // Check curvature condition and apply dampening is necessary
    scalar theta(1);
    if (ys < gamma_*sBs)
    {
        WarningInFunction
            << " y*s is below threshold. Using damped form" << endl;
95
        theta = (scalar(1)-gamma_)*sBs/(sBs - ys);
96 97 98 99 100
    }

    DebugInfo
        << "Hessian curvature index " << ys << endl;

101
    scalarField r(theta*y + (scalar(1)-theta)*rightMult(HessianOld_, s));
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    // Construct the inverse Hessian
    Hessian_ =
        HessianOld_
      - outerProd(rightMult(HessianOld_, s), leftMult(s/sBs, HessianOld_))
      + outerProd(r, r/globalSum(s*r));
}


void Foam::DBFGS::update()
{
    SquareMatrix<scalar> HessianInv = inv(Hessian_);

    // In the first few iterations, use steepest descent but update the Hessian
    // matrix
    if (counter_ < nSteepestDescent_)
    {
        Info<< "Using steepest descent to update design variables" << endl;
        correction_ = -eta_*objectiveDerivatives_;
    }
    // Else use DBFGS formula to update design variables
    else
    {
        // Compute correction for active design variables
        scalarField activeDerivs(activeDesignVars_.size(), Zero);
        activeDerivs.map(objectiveDerivatives_, activeDesignVars_);
        scalarField activeCorrection
        (
            -etaHessian_*rightMult(HessianInv, activeDerivs)
        );

        // Transfer correction to the global list
        correction_ = Zero;
        forAll(activeDesignVars_, varI)
        {
            correction_[activeDesignVars_[varI]] = activeCorrection[varI];
        }
    }

    // Store fields for the next iteration
    derivativesOld_ = objectiveDerivatives_;
    correctionOld_ = correction_;
    HessianOld_ = Hessian_;
}


void Foam::DBFGS::readFromDict()
{
    if (optMethodIODict_.headerOk())
    {
        optMethodIODict_.readEntry("HessianOld",  HessianOld_);
        optMethodIODict_.readEntry("derivativesOld", derivativesOld_);
        optMethodIODict_.readEntry("correctionOld", correctionOld_);
        optMethodIODict_.readEntry("counter", counter_);
        optMethodIODict_.readEntry("eta", eta_);

        label n = HessianOld_.n();
        Hessian_ = SquareMatrix<scalar>(n, Zero);
        correction_ = scalarField(correctionOld_.size(), Zero);
161 162 163 164 165

        if (activeDesignVars_.empty())
        {
            activeDesignVars_ = identity(n);
        }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    }
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::DBFGS::DBFGS
(
    const fvMesh& mesh,
    const dictionary& dict
)
:
    updateMethod(mesh, dict),

    // Construct null matrix since we dont know the dimension yet
    etaHessian_
    (
183
        coeffsDict().getOrDefault<scalar>("etaHessian", 1)
184 185 186
    ),
    nSteepestDescent_
    (
187
        coeffsDict().getOrDefault<label>("nSteepestDescent", 1)
188 189 190 191
    ),
    activeDesignVars_(0),
    scaleFirstHessian_
    (
192
        coeffsDict().getOrDefault<bool>("scaleFirstHessian", false)
193 194 195
    ),
    curvatureThreshold_
    (
196
        coeffsDict().getOrDefault<scalar>("curvatureThreshold", 1e-10)
197 198 199 200 201 202
    ),
    Hessian_(),
    HessianOld_(),
    derivativesOld_(0),
    correctionOld_(0),
    counter_(0),
203
    gamma_(coeffsDict().getOrDefault<scalar>("gamma", 0.2))
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

{
    if
    (
        !coeffsDict().readIfPresent("activeDesignVariables", activeDesignVars_)
    )
    {
        // If not, all available design variables will be used. Number is not
        // know at the moment
        Info<< "\t Did not find explicit definition of active design variables."
               " Treating all available ones as active " << endl;
    }

    // read old hessian, correction and derivatives, if present
    readFromDict();
219
}
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

void Foam::DBFGS::computeCorrection()
{
    if (counter_ == 0)
    {
        allocateMatrices();
    }
    else
    {
        updateHessian();
    }

    update();
    ++counter_;
}


void Foam::DBFGS::updateOldCorrection(const scalarField& oldCorrection)
{
    updateMethod::updateOldCorrection(oldCorrection);
    correctionOld_ = oldCorrection;
}


void Foam::DBFGS::write()
{
    optMethodIODict_.add<SquareMatrix<scalar>>("HessianOld", HessianOld_, true);
    optMethodIODict_.add<scalarField>("derivativesOld", derivativesOld_, true);
    optMethodIODict_.add<scalarField>("correctionOld", correctionOld_, true);
    optMethodIODict_.add<label>("counter", counter_, true);

    updateMethod::write();
}


// ************************************************************************* //