SR1.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 "SR1.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(SR1, 0);
39  (
40  updateMethod,
41  SR1,
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  // Set previous HessianInv to be a diagonal matrix
64  {
65  temp[i][i] = scalar(1);
66  }
67 
68  // Allocate correct size and content to HessianInv matrices
69  // has a max. capability of approximately 34000 variables.
70  HessianInvOld_ = temp;
71  HessianInv_ = temp;
72 }
73 
74 
76 {
77  // Vectors needed to construct the inverse HessianInv matrix
78  scalarField y(activeDesignVars_.size(), Zero);
79  scalarField s(activeDesignVars_.size(), Zero);
80  y.map(objectiveDerivatives_ - derivativesOld_, activeDesignVars_);
81  s.map(correctionOld_, activeDesignVars_);
82 
83  scalarField temp(s - rightMult(HessianInvOld_, y));
84 
85  // Construct the inverse HessianInv
86  scalar tempMag = sqrt(globalSum(sqr(temp)));
87  scalar yMag = sqrt(globalSum(sqr(y)));
88  scalar HessYMag = sqrt(globalSum(sqr(rightMult(HessianInvOld_, y))));
89 
90  // Stability check
91  if (tempMag > ratioThreshold_ * yMag * HessYMag)
92  {
93  HessianInv_ =
94  HessianInvOld_
95  + (scalar(1)/(globalSum(temp*y)))*outerProd(temp, temp);
96  }
97  else
98  {
100  << "Denominator of update too small. Keeping old Hessian" << endl;
101  HessianInv_ = HessianInvOld_;
102  }
103 }
104 
105 
107 {
108  // In the first few iterations, use steepest descent but update the Hessian
109  // matrix
110  if (counter_ < nSteepestDescent_)
111  {
112  Info<< "Using steepest descent to update design variables ... " << endl;
113  correction_ = -eta_*objectiveDerivatives_;
114  }
115  else
116  {
117  scalarField activeDerivs(activeDesignVars_.size(), Zero);
118  activeDerivs.map(objectiveDerivatives_, activeDesignVars_);
119  scalarField activeCorrection
120  (
121  -etaHessian_*rightMult(HessianInv_, activeDerivs)
122  );
123 
124  // Transfer correction to the global list
125  correction_ = Zero;
126  forAll(activeDesignVars_, varI)
127  {
128  correction_[activeDesignVars_[varI]] = activeCorrection[varI];
129  }
130  }
131 
132  // Store fields for the next iteration
133  derivativesOld_ = objectiveDerivatives_;
134  correctionOld_ = correction_;
135  HessianInvOld_ = HessianInv_;
136 }
137 
138 
140 {
141  if (optMethodIODict_.headerOk())
142  {
143  optMethodIODict_.readEntry("HessianInvOld", HessianInvOld_);
144  optMethodIODict_.readEntry("derivativesOld", derivativesOld_);
145  optMethodIODict_.readEntry("correctionOld", correctionOld_);
146  optMethodIODict_.readEntry("counter", counter_);
147  optMethodIODict_.readEntry("eta", eta_);
148 
149  label n = HessianInvOld_.n();
150  HessianInv_ = SquareMatrix<scalar>(n, Zero);
151  correction_ = scalarField(correctionOld_.size(), Zero);
152  }
153 }
154 
155 
156 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
157 
158 Foam::SR1::SR1(const fvMesh& mesh, const dictionary& dict)
159 :
161 
162  // Construct null matrix since we dont know the dimension yet
163  etaHessian_
164  (
165  coeffsDict().lookupOrDefault<scalar>("etaHessian", 1)
166  ),
167  nSteepestDescent_
168  (
169  coeffsDict().lookupOrDefault<label>("nSteepestDescent", 1)
170  ),
171  ratioThreshold_
172  (
173  coeffsDict().lookupOrDefault<scalar>("ratioThreshold", 1e-08)
174  ),
175  activeDesignVars_(0),
176  HessianInv_(),
177  HessianInvOld_(),
178  derivativesOld_(0),
179  correctionOld_(0),
180  counter_(0)
181 {
182  if
183  (
184  !coeffsDict().readIfPresent("activeDesignVariables", activeDesignVars_)
185  )
186  {
187  // If not, all available design variables will be used. Number is not
188  // know at the moment
189  Info<< "\t Didn't find explicit definition of active design variables. "
190  << "Treating all available ones as active " << endl;
191  }
192 
193  // Read old hessian, correction and derivatives, if present
194  readFromDict();
195 }
196 
197 
198 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
199 
201 {
202  if (counter_ == 0)
203  {
204  allocateMatrices();
205  }
206  else
207  {
208  updateHessian();
209  }
210 
211  update();
212  ++counter_;
213 }
214 
215 
216 void Foam::SR1::updateOldCorrection(const scalarField& oldCorrection)
217 {
218  updateMethod::updateOldCorrection(oldCorrection);
219  correctionOld_ = oldCorrection;
220 }
221 
222 
224 {
225  optMethodIODict_.add<SquareMatrix<scalar>>
226  (
227  "HessianInvOld",
228  HessianInvOld_,
229  true
230  );
231  optMethodIODict_.add<scalarField>("derivativesOld", derivativesOld_, true);
232  optMethodIODict_.add<scalarField>("correctionOld", correctionOld_, true);
233  optMethodIODict_.add<label>("counter", counter_, true);
234 
236 }
237 
238 
239 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::scalarField
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Definition: primitiveFieldsFwd.H:52
s
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))
Definition: gmvOutputSpray.H:25
update
mesh update()
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::SR1::activeDesignVars_
labelList activeDesignVars_
Map to active design variables.
Definition: SR1.H:72
Foam::SR1::computeCorrection
void computeCorrection()
Compute design variables correction.
Definition: SR1.C:200
Foam::SR1::allocateMatrices
void allocateMatrices()
Allocate matrices in the first optimisation cycle.
Definition: SR1.C:49
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::Field::map
void map(const UList< Type > &mapF, const labelUList &mapAddressing)
1 to 1 map from the given field
Definition: Field.C:263
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::SR1::readFromDict
void readFromDict()
Read old info from dict.
Definition: SR1.C:139
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::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)
SR1.H
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
Foam::updateMethod::correction_
scalarField correction_
Design variables correction.
Definition: updateMethod.H:80
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
Foam::updateMethod::coeffsDict
dictionary coeffsDict()
Return optional dictionary with parameters specific to each method.
Definition: updateMethod.C:254
Foam::SquareMatrix< scalar >
Foam::updateMethod::write
virtual void write()
Write usefull quantities to files.
Definition: updateMethod.C:398
Foam::SR1::write
virtual void write()
Write old info to dict.
Definition: SR1.C:223
Foam::sqr
dimensionedSymmTensor sqr(const dimensionedVector &dv)
Definition: dimensionedSymmTensor.C:51
Foam::SR1::HessianInv_
SquareMatrix< scalar > HessianInv_
Definition: SR1.H:76
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:144
Foam::SR1::update
void update()
Update design variables.
Definition: SR1.C:106
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::SR1::HessianInvOld_
SquareMatrix< scalar > HessianInvOld_
The previous Hessian inverse.
Definition: SR1.H:79
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::SR1::updateHessian
void updateHessian()
Update approximation of the inverse Hessian.
Definition: SR1.C:75
Foam::SR1::updateOldCorrection
virtual void updateOldCorrection(const scalarField &oldCorrection)
Definition: SR1.C:216
y
scalar y
Definition: LISASMDCalcMethod1.H:14