LBFGS.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-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 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 \*---------------------------------------------------------------------------*/
29 
30 #include "LBFGS.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(LBFGS, 0);
39  (
40  updateMethod,
41  LBFGS,
42  dictionary
43  );
44 }
45 
46 
47 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
48 
50 {
51  // Set active design variables, if necessary
52  if (activeDesignVars_.empty())
53  {
56  {
57  activeDesignVars_[dvI] = dvI;
58  }
59  }
60 
61  // Allocate vectors
62  label nVars(activeDesignVars_.size());
63  for (label i = 0; i < nPrevSteps_; i++)
64  {
65  y_.set(i, scalarField(nVars, Zero));
66  s_.set(i, scalarField(nVars, Zero));
67  }
68 }
69 
70 
72 {
73  if (counter_ > nPrevSteps_)
74  {
75  // Reorder list by moving pointers down the line
76  labelList newOrder(nPrevSteps_, -1);
77  newOrder[0] = nPrevSteps_ - 1;
78  for (label i = 1; i < nPrevSteps_; ++i)
79  {
80  newOrder[i] = i - 1;
81  }
82  list.reorder(newOrder);
83 
84  // Fill in last element with the provided field
85  list[nPrevSteps_ - 1] = f;
86  }
87  else
88  {
89  list[counter_ - 1] = f;
90  }
91 }
92 
93 
95 {
96  // Update list of y. Can only be done here since objectiveDerivatives_
97  // was not known at the end of the previous loop
98  scalarField yRecent
99  (objectiveDerivatives_ - derivativesOld_, activeDesignVars_);
100  pivotFields(y_, yRecent);
101  // Update list of s.
102  // correction_ holds the previous correction
103  scalarField sActive(correctionOld_, activeDesignVars_);
104  pivotFields(s_, sActive);
105 
106  DebugInfo
107  << "y fields" << nl << y_ << endl;
108  DebugInfo
109  << "s fields" << nl << s_ << endl;
110 }
111 
112 
114 {
115  Info<< "Using steepest descent to update design variables" << endl;
116  correction_ = -eta_*objectiveDerivatives_;
117 }
118 
119 
121 {
122  // L-BFGS two loop recursion
123  //~~~~~~~~~~~~~~~~~~~~~~~~~~
124  label nSteps(min(counter_, nPrevSteps_));
125  label nLast(nSteps - 1);
126  scalarField q(objectiveDerivatives_, activeDesignVars_);
127  scalarField a(nSteps, 0.);
128  scalarField r(nSteps, 0.);
129  for (label i = nLast; i > -1; --i)
130  {
131  r[i] = 1./globalSum(y_[i]*s_[i]);
132  a[i] = r[i]*globalSum(s_[i]*q);
133  q -= a[i]*y_[i];
134  }
135 
136  scalar gamma =
137  globalSum(y_[nLast]*s_[nLast])/globalSum(y_[nLast]*y_[nLast]);
138  q *= gamma;
139 
140  scalarField b(activeDesignVars_.size(), Zero);
141  for (label i = 0; i < nSteps; ++i)
142  {
143  b = r[i]*globalSum(y_[i]*q);
144  q += s_[i]*(a[i] -b);
145  }
146 
147  // Update correction
148  forAll(activeDesignVars_, varI)
149  {
150  correction_[activeDesignVars_[varI]] = -etaHessian_*q[varI];
151  }
152 }
153 
154 
156 {
157  // In the first few iterations, use steepest descent but update the Hessian
158  // matrix
159  if (counter_ < nSteepestDescent_)
160  {
161  steepestDescentUpdate();
162  }
163  // else use LBFGS formula to update the design variables
164  else
165  {
166  LBFGSUpdate();
167  }
168 
169  // Store fields for the next iteration
170  derivativesOld_ = objectiveDerivatives_;
171  correctionOld_ = correction_;
172 }
173 
174 
176 {
177  if (optMethodIODict_.headerOk())
178  {
179  optMethodIODict_.readEntry("y", y_);
180  optMethodIODict_.readEntry("s", s_);
181  optMethodIODict_.readEntry("derivativesOld", derivativesOld_);
182  optMethodIODict_.readEntry("counter", counter_);
183  optMethodIODict_.readEntry("eta", eta_);
184  optMethodIODict_.readEntry("correctionOld", correctionOld_);
185 
186  correction_ = scalarField(correctionOld_.size(), Zero);
187  }
188 }
189 
190 
191 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
192 
193 Foam::LBFGS::LBFGS
194 (
195  const fvMesh& mesh,
196  const dictionary& dict
197 )
198 :
200 
201  // Construct null matrix since we dont know the dimension yet
202  etaHessian_
203  (
204  coeffsDict().lookupOrDefault<scalar>("etaHessian", 1)
205  ),
206  nSteepestDescent_
207  (
208  coeffsDict().lookupOrDefault<label>("nSteepestDescent", 1)
209  ),
210  activeDesignVars_(0),
211  nPrevSteps_
212  (
213  coeffsDict().lookupOrDefault<label>("nPrevSteps", 10)
214  ),
215  y_(nPrevSteps_),
216  s_(nPrevSteps_),
217  derivativesOld_(0),
218  counter_(Zero)
219 {
220  if
221  (
222  !coeffsDict().readIfPresent("activeDesignVariables", activeDesignVars_)
223  )
224  {
225  // If not, all available design variables will be used. Number is not
226  // know at the moment
227  Info<< "\t Did not find explicit definition of active design variables. "
228  << "Treating all available ones as active " << endl;
229  }
230 
231  // Read old Hessian, correction and derivatives, if present
232  readFromDict();
233 }
234 
235 
236 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
237 
239 {
240  if (counter_ == 0)
241  {
242  allocateMatrices();
243  }
244  else
245  {
246  updateVectors();
247  }
248 
249  update();
250  ++counter_;
251 }
252 
253 
255 {
256  updateMethod::updateOldCorrection(oldCorrection);
257  correctionOld_ = oldCorrection;
258 }
259 
260 
262 {
263  optMethodIODict_.add<PtrList<scalarField>>("y", y_, true);
264  optMethodIODict_.add<PtrList<scalarField>>("s", s_, true);
265  optMethodIODict_.add<scalarField>("derivativesOld", derivativesOld_, true);
266  optMethodIODict_.add<scalarField>("correctionOld", correctionOld_, true);
267  optMethodIODict_.add<label>("counter", counter_, true);
268 
270 }
271 
272 
273 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
Foam::LBFGS::readFromDict
void readFromDict()
Read old info from dict.
Definition: LBFGS.C:175
update
mesh update()
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::LBFGS::allocateMatrices
void allocateMatrices()
Allocate matrices in the first optimisation cycle.
Definition: LBFGS.C:49
Foam::LBFGS::update
void update()
Update design variables.
Definition: LBFGS.C:155
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::LBFGS::y_
PtrList< scalarField > y_
The previous differences of derivatives. Holds nPrevSteps_ fields.
Definition: LBFGS.H:87
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::updateMethod::updateOldCorrection
virtual void updateOldCorrection(const scalarField &oldCorrection)
Definition: updateMethod.C:390
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::Field< scalar >
Foam::updateMethod
Abstract base class for optimisation methods.
Definition: updateMethod.H:54
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::LBFGS::updateOldCorrection
virtual void updateOldCorrection(const scalarField &oldCorrection)
Definition: LBFGS.C:254
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:65
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
LBFGS.H
Foam::LBFGS::nPrevSteps_
label nPrevSteps_
Number of old corrections and grad differences kept.
Definition: LBFGS.H:84
Foam::LBFGS::steepestDescentUpdate
void steepestDescentUpdate()
Update based on steepest descent.
Definition: LBFGS.C:113
Foam::LBFGS::write
virtual void write()
Write old info to dict.
Definition: LBFGS.C:261
Foam::updateMethod::write
virtual void write()
Write usefull quantities to files.
Definition: updateMethod.C:398
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::LBFGS::activeDesignVars_
labelList activeDesignVars_
Map to active design variables.
Definition: LBFGS.H:81
f
labelList f(nPoints)
Foam::LBFGS::s_
PtrList< scalarField > s_
The previous correction. Holds nPrevSteps_ fields.
Definition: LBFGS.H:90
Foam::List< label >
gamma
const scalar gamma
Definition: EEqn.H:9
Foam::LBFGS::LBFGSUpdate
void LBFGSUpdate()
Update based on LBFGS.
Definition: LBFGS.C:120
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::LBFGS::pivotFields
void pivotFields(PtrList< scalarField > &list, const scalarField &f)
Definition: LBFGS.C:71
Foam::updateMethod::objectiveDerivatives_
scalarField objectiveDerivatives_
Derivatives of the objective functions.
Definition: updateMethod.H:68
Foam::LBFGS::computeCorrection
void computeCorrection()
Compute design variables correction.
Definition: LBFGS.C:238
Foam::LBFGS::updateVectors
void updateVectors()
Update y and s vectors.
Definition: LBFGS.C:94