LBFGS.H
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  Copyright (C) 2019 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 Class
29  Foam::LBFGS
30 
31 Description
32  The quasi-Newton Limited-memory BFGS formula. Keeps nPrevSteps_ of the
33  y and s vectors and approximates the inverse areas through them.
34  Values of 3 < nPrevSteps_ < 20 are suggested.
35 
36 SourceFiles
37  LBFGS.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef LBFGS_H
42 #define LBFGS_H
43 
44 #include "quasiNewton.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 /*---------------------------------------------------------------------------*\
52  Class LBFGS Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class LBFGS
56 :
57  public quasiNewton
58 {
59 private:
60 
61  // Private Member Functions
62 
63  //- No copy construct
64  LBFGS(const LBFGS&) = delete;
65 
66  //- No copy assignment
67  void operator=(const LBFGS&) = delete;
68 
69 
70 protected:
71 
72  // Protected data
73 
74  //- Number of old corrections and grad differences kept
75  label nPrevSteps_;
76 
77  //- The previous differences of derivatives. Holds nPrevSteps_ fields
79 
80  //- The previous correction. Holds nPrevSteps_ fields
82 
83  //- Use damping for s to ensure positive-definitiveness
84  bool useSDamping_;
85 
86  //- Use damping for s to ensure positive-definitiveness
87  bool useYDamping_;
88 
89 
90  // Protected Member Functions
91 
92  //- Allocate matrices in the first optimisation cycle
93  void allocateVectors();
94 
95  //- Move pointers in PtrList to the left and replace last element with
96  //- given field
97  void pivotFields(PtrList<scalarField>& list, const scalarField& f);
98 
99  //- Update y and s vectors
100  void updateVectors
101  (
102  const scalarField& derivatives,
103  const scalarField& derivativesOld
104  );
105 
106  //- Use the damped version of s to ensure positive-definitiveness
107  // Usefull in conjunction with SQP
108  void applyDamping
109  (
110  scalarField& y,
111  scalarField& s
112  );
113 
114  //- Update the Hessian matrix by updating the base vectors
115  virtual void updateHessian();
116 
117  //- Update design variables
118  virtual void update();
119 
120  //- Compute the inverse Hessian - vector product.
121  // Input should have the size of all design variables or the active
122  // ones, output is the size of the active design variables.
123  // Can optionally received a seed diagonal matrix
125  (
126  const scalarField& vector
127  );
128 
129  //- Same as previous one, but with an explicit counter provided
131  (
132  const scalarField& vector,
133  const label counter,
134  tmp<scalarField> diag = nullptr
135  );
136 
137  //- Compute the Hessian - vector product
138  // Requires the solution of a system of equations twice the size of
139  // the bases. This should be OK since the latter is small.
140  // Input should have the size of all design variables or the active
141  // ones, output is the size of the active design variables
143  (
144  const scalarField& vector
145  );
146 
147  //- Same as previous one, but with an explicit counter provided
149  (
150  const scalarField& vector,
151  const label counter
152  );
153 
154  //- Return the diagonal of the Hessian.
155  // Requires the solution of a system of equations twice the size of
156  // the bases. This should be OK since the latter is small.
157  // Useful for preconditioning
159 
160 
161  // Similar functions, but using the SR1 formula instead of BFGS.
162  // Should become a separate class at some point
163 
164  //- Compute the Hessian - vector product
165  // Requires the solution of a system of equations twice the size
166  // of the bases. This should be OK since the latter is small.
167  // Input should have the size of all design variables or the
168  // active ones, output is the size of the active design variables
170  (
171  const scalarField& vector
172  );
173 
174  //- Same as previous one, but with an explicit counter provided
176  (
177  const scalarField& vector,
178  const label counter
179  );
180 
181  //- Return the diagonal of the Hessian.
182  // Requires the solution of a system of equations twice the size
183  // of the bases. This should be OK since the latter is small.
184  // Useful for preconditioning
186 
187 
188 public:
189 
190  //- Runtime type information
191  TypeName("LBFGS");
192 
193 
194  // Constructors
195 
196  //- Construct from components
197  LBFGS
198  (
199  const fvMesh& mesh,
200  const dictionary& dict,
201  autoPtr<designVariables>& designVars,
202  const label nConstraints,
203  const word& type
204  );
205 
206 
207  //- Destructor
208  virtual ~LBFGS() = default;
209 
210 
211  // Member Functions
212 
213  //- Write useful quantities to files
214  virtual bool writeData(Ostream& os) const;
215 };
216 
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 } // End namespace Foam
221 
222 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 
224 #endif
225 
226 // ************************************************************************* //
TypeName("LBFGS")
Runtime type information.
dictionary dict
virtual void update()
Update design variables.
Definition: LBFGS.C:590
label nConstraints() const
Get the number of constraints.
Definition: updateMethod.C:393
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
tmp< scalarField > SR1HessianDiag()
Return the diagonal of the Hessian.
Definition: LBFGS.C:515
virtual tmp< scalarField > invHessianVectorProduct(const scalarField &vector)
Compute the inverse Hessian - vector product.
Definition: LBFGS.C:160
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
scalar y
void diag(pointPatchField< vector > &, const pointPatchField< tensor > &)
dynamicFvMesh & mesh
void pivotFields(PtrList< scalarField > &list, const scalarField &f)
Move pointers in PtrList to the left and replace last element with given field.
Definition: LBFGS.C:67
Base class for quasi-Newton methods.
Definition: quasiNewton.H:49
virtual tmp< scalarField > HessianVectorProduct(const scalarField &vector)
Compute the Hessian - vector product.
Definition: LBFGS.C:242
A class for handling words, derived from Foam::string.
Definition: word.H:63
PtrList< scalarField > s_
The previous correction. Holds nPrevSteps_ fields.
Definition: LBFGS.H:86
void allocateVectors()
Allocate matrices in the first optimisation cycle.
Definition: LBFGS.C:42
label nPrevSteps_
Number of old corrections and grad differences kept.
Definition: LBFGS.H:76
virtual void updateHessian()
Update the Hessian matrix by updating the base vectors.
Definition: LBFGS.C:584
bool useSDamping_
Use damping for s to ensure positive-definitiveness.
Definition: LBFGS.H:91
virtual bool writeData(Ostream &os) const
Write useful quantities to files.
Definition: LBFGS.C:643
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
OBJstream os(runTime.globalPath()/outputName)
bool useYDamping_
Use damping for s to ensure positive-definitiveness.
Definition: LBFGS.H:96
labelList f(nPoints)
tmp< scalarField > HessianDiag()
Return the diagonal of the Hessian.
Definition: LBFGS.C:343
virtual tmp< scalarField > SR1HessianVectorProduct(const scalarField &vector)
Compute the Hessian - vector product.
Definition: LBFGS.C:418
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
PtrList< scalarField > y_
The previous differences of derivatives. Holds nPrevSteps_ fields.
Definition: LBFGS.H:81
The quasi-Newton Limited-memory BFGS formula. Keeps nPrevSteps_ of the y and s vectors and approximat...
Definition: LBFGS.H:50
virtual ~LBFGS()=default
Destructor.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
A class for managing temporary objects.
Definition: HashPtrTable.H:50
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
void updateVectors(const scalarField &derivatives, const scalarField &derivativesOld)
Update y and s vectors.
Definition: LBFGS.C:91
void applyDamping(scalarField &y, scalarField &s)
Use the damped version of s to ensure positive-definitiveness.
Definition: LBFGS.C:121
Namespace for OpenFOAM.