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 -------------------------------------------------------------------------------
11 License
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 
34 namespace Foam
35 {
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 
60 Foam::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  ),
84  correctionOld_(readOrZeroField("correctionOld", correction_.size()))
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 {
105  updateMethod::updateOldCorrection(oldCorrection);
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 
125  return updateMethod::writeData(os);
126 }
127 
128 
129 // ************************************************************************* //
autoPtr< designVariables > & designVars_
Design variables.
Definition: updateMethod.H:65
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void writeEntry(const word &keyword, Ostream &os) const
Write the field as a dictionary entry.
Definition: Field.C:720
virtual bool writeData(Ostream &os) const
Write useful quantities to files.
Definition: quasiNewton.C:103
Macros for easy insertion into run-time selection tables.
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:104
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void computeCorrection()
Compute design variables correction.
Definition: quasiNewton.C:83
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
const labelList & activeDesignVars_
Map to active design variables.
Definition: updateMethod.H:75
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. Useful for quasi-newton methods coupled with line search.
Definition: updateMethod.C:458
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
static const Identity< scalar > I
Definition: Identity.H:100
Base class for quasi-Newton methods.
Definition: quasiNewton.H:49
autoPtr< SquareMatrix< scalar > > Hessian_
The Hessian or its inverse, depending on the deriving class.
Definition: quasiNewton.H:78
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
void allocateHessian()
Allocate the Hessian matrix.
Definition: quasiNewton.C:35
OBJstream os(runTime.globalPath()/outputName)
mesh update()
defineTypeNameAndDebug(combustionModel, 0)
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. Useful for quasi-Newton methods coupled with line search.
Definition: quasiNewton.C:96
virtual bool writeData(Ostream &os) const
Write continuation data under uniform.
Definition: updateMethod.C:466
Namespace for OpenFOAM.