Loading...
Searching...
No Matches
quasiNewton.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2007-2023 PCOpt/NTUA
9 Copyright (C) 2013-2023 FOSS GP
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "quasiNewton.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34namespace Foam
36 defineTypeNameAndDebug(quasiNewton, 0);
37}
38
39
40// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
41
43{
45 // Read in Hessian Matrix or initialiase
46 const label nDVs(designVars_().activeDesignVariables().size());
47 forAll(designVars_().activeDesignVariables(), iDV)
48 {
49 if (found("Hessian" + Foam::name(iDV)))
50 {
51 Hessian_().subColumn(iDV) =
52 scalarField("Hessian" + Foam::name(iDV), *this, nDVs);
53 }
54 }
55}
56
57
58// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
59
60Foam::quasiNewton::quasiNewton
61(
62 const fvMesh& mesh,
63 const dictionary& dict,
64 autoPtr<designVariables>& designVars,
65 const label nConstraints,
66 const word& type
67)
68:
69 updateMethod(mesh, dict, designVars, nConstraints, type),
70 etaHessian_(coeffsDict(type).getOrDefault<scalar>("etaHessian", 1)),
71 nSteepestDescent_
72 (
73 coeffsDict(type).getOrDefault<label>("nSteepestDescent", 1)
74 ),
75 scaleFirstHessian_
76 (
77 coeffsDict(type).getOrDefault<bool>("scaleFirstHessian", false)
78 ),
79 Hessian_(nullptr),
80 derivativesOld_
81 (
82 readOrZeroField("derivativesOld", objectiveDerivatives_.size())
83 ),
85{}
86
87
88// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
89
91{
92 // The first iteration uses a unitary Hessian. No need to update
93 if (counter_ != 0)
94 {
95 updateHessian();
96 }
97
98 update();
99 ++counter_;
100}
101
102
104{
106 correctionOld_ = oldCorrection;
107}
108
109
111{
112 if (Hessian_)
113 {
114 // Matrices cannot be written/read in binary.
115 // Circumvent this by writing separate columns as scalarFields
116 forAll(designVars_().activeDesignVariables(), iDV)
117 {
118 Hessian_().subColumn(iDV).operator Field<scalar>().
119 writeEntry("Hessian" + Foam::name(iDV), os);
120 }
121 }
122 derivativesOld_.writeEntry("derivativesOld", os);
123 correctionOld_.writeEntry("correctionOld", os);
124
126}
127
128
129// ************************************************************************* //
bool found
Macros for easy insertion into run-time selection tables.
label size() const noexcept
The number of elements in list.
Definition DLListBase.H:194
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A templated (N x N) square matrix of objects of <Type>, containing N*N elements, derived from Matrix.
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
Base class for quasi-Newton methods.
Definition quasiNewton.H:52
void computeCorrection()
Compute design variables correction.
Definition quasiNewton.C:83
scalarField correctionOld_
The previous correction.
Definition quasiNewton.H:88
autoPtr< SquareMatrix< scalar > > Hessian_
The Hessian or its inverse, depending on the deriving class.
Definition quasiNewton.H:78
virtual bool writeData(Ostream &os) const
Write useful quantities to files.
label nSteepestDescent_
Number of first steepest descent steps.
Definition quasiNewton.H:65
virtual void updateHessian()=0
Update approximation of the inverse Hessian.
scalar etaHessian_
Step for the Newton method.
Definition quasiNewton.H:60
bool scaleFirstHessian_
Scale the initial unitary Hessian approximation.
Definition quasiNewton.H:70
scalarField derivativesOld_
The previous derivatives.
Definition quasiNewton.H:83
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. Useful for quasi-Newton methods coupled with line search.
Definition quasiNewton.C:96
void allocateHessian()
Allocate the Hessian matrix.
Definition quasiNewton.C:35
Abstract base class for optimisation methods.
scalarField objectiveDerivatives_
Derivatives of the objective functions.
scalarField correction_
Design variables correction.
dictionary coeffsDict(const word &type) const
Return optional dictionary with parameters specific to each method.
virtual bool writeData(Ostream &os) const
Write continuation data under uniform.
tmp< scalarField > readOrZeroField(const word &name, const label size)
Helper function to either read a scalarField of certain size from a dictionary, or construct a zero f...
const labelList & activeDesignVars_
Map to active design variables.
label counter_
Optimisation cycle count.
label nConstraints() const
Get the number of constraints.
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. Useful for quasi-newton methods coupled with line search.
autoPtr< designVariables > & designVars_
Design variables.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
mesh update()
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
static const Identity< scalar > I
Definition Identity.H:100
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299