conjugateGradient.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 "conjugateGradient.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(conjugateGradient, 0);
39  (
40  updateMethod,
41  conjugateGradient,
42  dictionary
43  );
44 }
45 
46 
47 // * * * * * * * * * * * * * Private 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 old fields
64 }
65 
66 
68 {
69  if (optMethodIODict_.headerOk())
70  {
71  optMethodIODict_.readEntry("dxOld", dxOld_);
72  optMethodIODict_.readEntry("sOld", sOld_);
73  optMethodIODict_.readEntry("counter", counter_);
74  optMethodIODict_.readEntry("eta", eta_);
75 
76  label nDVs = optMethodIODict_.get<label>("nDVs");
77  correction_ = scalarField(nDVs, Zero);
78  }
79 }
80 
81 
82 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
83 
84 Foam::conjugateGradient::conjugateGradient
85 (
86  const fvMesh& mesh,
87  const dictionary& dict
88 )
89 :
91 
92  activeDesignVars_(0),
93  dxOld_(0),
94  sOld_(0),
95  counter_(0),
96  betaType_
97  (
98  coeffsDict().lookupOrDefault<word>("betaType", "FletcherReeves")
99  )
100 {
101  if
102  (
103  !coeffsDict().readIfPresent("activeDesignVariables", activeDesignVars_)
104  )
105  {
106  // If not, all available design variables will be used.
107  // Number is not know at the moment
108  Info<< "\t Did not find explicit definition of active design variables. "
109  << "Treating all available ones as active " << endl;
110  }
111 
112  // Check if beta type is valid
113  if
114  (
115  !(betaType_ == "FletcherReeves")
116  && !(betaType_ == "PolakRibiere")
117  && !(betaType_ == "PolakRibiereRestarted")
118  )
119  {
121  << "Invalid betaType " << betaType_ << ". Valid options are "
122  << "FletcherReeves, PolakRibiere, PolakRibiereRestarted"
123  << nl << nl
124  << exit(FatalError);
125  }
126 
127  // Read old dx and s, if present
128  readFromDict();
129 }
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
135 {
136  if (counter_ == 0)
137  {
138  allocateFields();
139 
140  Info<< "Using steepest descent for the first iteration" << endl;
141  correction_ = -eta_*objectiveDerivatives_;
142 
143  dxOld_.map(-objectiveDerivatives_, activeDesignVars_);
144  sOld_ = dxOld_;
145  }
146  else
147  {
148  scalarField dx = scalarField(activeDesignVars_.size(), Zero);
149  dx.map(-objectiveDerivatives_, activeDesignVars_);
150 
151  scalar beta(Zero);
152  if (betaType_ == "FletcherReeves")
153  {
154  beta = globalSum(dx*dx)/globalSum(dxOld_ * dxOld_);
155  }
156  else if (betaType_ == "PolakRibiere")
157  {
158  beta = globalSum(dx*(dx - dxOld_))/globalSum(dxOld_ * dxOld_);
159  }
160  else if (betaType_ == "PolakRibiereRestarted")
161  {
162  beta =
163  max
164  (
165  scalar(0),
166  globalSum(dx*(dx - dxOld_))/globalSum(dxOld_ * dxOld_)
167  );
168  if (beta == scalar(0))
169  {
170  Info<< "Computed negative beta. Resetting to zero" << endl;
171  }
172  }
173 
174  scalarField s(dx + beta*sOld_);
175 
176  correction_ = Zero;
177  forAll(activeDesignVars_, varI)
178  {
179  correction_[activeDesignVars_[varI]] = eta_*s[varI];
180  }
181 
182  // Store fields for the next iteration
183  dxOld_ = dx;
184  sOld_ = s;
185  }
186 
187  ++counter_;
188 }
189 
190 
192 (
193  const scalarField& oldCorrection
194 )
195 {
196  sOld_.map(oldCorrection, activeDesignVars_);
197  sOld_ /= eta_;
198  correction_ = oldCorrection;
199 }
200 
201 
203 {
204  optMethodIODict_.add<scalarField>("dxOld", dxOld_, true);
205  optMethodIODict_.add<scalarField>("sOld", sOld_, true);
206  optMethodIODict_.add<label>("counter", counter_, true);
207  optMethodIODict_.add<label>("nDVs", objectiveDerivatives_.size(), true);
208 
210 }
211 
212 
213 // ************************************************************************* //
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
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::conjugateGradient::write
virtual void write()
Write old info to dict.
Definition: conjugateGradient.C:202
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
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
conjugateGradient.H
Foam::conjugateGradient::computeCorrection
void computeCorrection()
Compute design variables correction.
Definition: conjugateGradient.C:134
Foam::conjugateGradient::readFromDict
void readFromDict()
Read old info from dict.
Definition: conjugateGradient.C:67
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::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)
beta
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
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
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::conjugateGradient::updateOldCorrection
virtual void updateOldCorrection(const scalarField &oldCorrection)
Update old correction. For use when eta has been changed externally.
Definition: conjugateGradient.C:192
Foam::updateMethod::write
virtual void write()
Write usefull quantities to files.
Definition: updateMethod.C:398
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::conjugateGradient::dxOld_
scalarField dxOld_
Definition: conjugateGradient.H:66
Foam::conjugateGradient::activeDesignVars_
labelList activeDesignVars_
Definition: conjugateGradient.H:65
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::conjugateGradient::sOld_
scalarField sOld_
Definition: conjugateGradient.H:67
Foam::conjugateGradient::allocateFields
void allocateFields()
Allocate matrices in the first optimisation cycle.
Definition: conjugateGradient.C:49
Foam::updateMethod::objectiveDerivatives_
scalarField objectiveDerivatives_
Derivatives of the objective functions.
Definition: updateMethod.H:68