lineSearch.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-2021 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 "lineSearch.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(lineSearch, 0);
38  defineRunTimeSelectionTable(lineSearch, dictionary);
39 }
40 
41 
42 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
43 
45 {
46  return dict_.optionalSubDict(type() + "Coeffs");
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
52 Foam::lineSearch::lineSearch(const dictionary& dict, const Time& time)
53 :
54  dict_(dict),
55  lineSearchDict_
56  (
57  IOobject
58  (
59  "lineSearch",
60  time.timeName(),
61  "uniform",
62  time,
63  IOobject::READ_IF_PRESENT,
64  IOobject::NO_WRITE,
65  false
66  )
67  ),
68  directionalDeriv_(Zero),
69  direction_(0),
70  oldMeritValue_(Zero),
71  newMeritValue_(Zero),
72  prevMeritDeriv_
73  (
74  lineSearchDict_.getOrDefault<scalar>("prevMeritDeriv", Zero)
75  ),
76  initialStep_(dict.getOrDefault<scalar>("initialStep", 1.)),
77  minStep_(dict.getOrDefault<scalar>("minStep", 0.3)),
78  step_(Zero),
79  iter_(lineSearchDict_.getOrDefault<label>("iter", 0)),
80  maxIters_(dict.getOrDefault<label>("maxIters", 4)),
81  extrapolateInitialStep_
82  (
83  dict.getOrDefault<bool>
84  (
85  "extrapolateInitialStep",
86  false
87  )
88  ),
89  stepUpdate_(stepUpdate::New(dict))
90 {}
91 
92 
93 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
94 
96 (
97  const dictionary& dict,
98  const Time& time
99 )
100 {
101  autoPtr<lineSearch> lineSrch(nullptr);
102 
103  const word modelType(dict.getOrDefault<word>("type", "none"));
104 
105  Info<< "lineSearch type : " << modelType << endl;
106 
107  if (modelType != "none")
108  {
109  auto* ctorPtr = dictionaryConstructorTable(modelType);
110 
111  if (!ctorPtr)
112  {
114  (
115  dict,
116  "lineSearch",
117  modelType,
118  *dictionaryConstructorTablePtr_
119  ) << exit(FatalIOError);
120  }
121 
122  lineSrch.reset((ctorPtr(dict, time)).ptr());
123  }
124  else
125  {
126  Info<< "No line search method specified. "
127  << "Proceeding with constant step" << endl;
128  }
129 
130  return lineSrch;
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * //
135 
136 void Foam::lineSearch::setDeriv(const scalar deriv)
137 {
138  directionalDeriv_ = deriv;
139  stepUpdate_->setDeriv(deriv);
140 }
141 
142 
144 {
145  direction_ = direction;
146 }
147 
148 
149 void Foam::lineSearch::setNewMeritValue(const scalar value)
150 {
151  newMeritValue_ = value;
152  stepUpdate_->setNewMeritValue(value);
153 }
154 
155 
156 void Foam::lineSearch::setOldMeritValue(const scalar value)
157 {
158  oldMeritValue_ = value;
159  stepUpdate_->setOldMeritValue(value);
160 }
161 
162 
164 {
165  if (extrapolateInitialStep_ && iter_ != 0)
166  {
167  // step_ = 2*(oldMeritValue_-prevMeritValue_)/directionalDeriv_;
168  // Interpolate in order to get same improvement with the previous
169  // optimisation cycle
170  step_ = max(min(step_*prevMeritDeriv_/directionalDeriv_, 1.), minStep_);
171  Info<< "\n------- Computing initial step-------" << endl;
172  Info<< "old dphi(0) " << prevMeritDeriv_ << endl;
173  Info<< "dphi(0) " << directionalDeriv_ << endl;
174  Info<< "Setting initial step value " << step_ << endl << endl;
175  }
176  else
177  {
178  step_ = initialStep_;
179  }
180 }
181 
182 
183 Foam::label Foam::lineSearch::maxIters() const
184 {
185  return maxIters_;
186 }
187 
188 
189 Foam::scalar Foam::lineSearch::step() const
190 {
191  return step_;
192 }
193 
194 
195 void Foam::lineSearch::updateStep(const scalar newStep)
196 {
197  step_ = newStep;
198 }
199 
200 
202 {
203  iter_++;
204  prevMeritDeriv_ = directionalDeriv_;
205  lineSearchDict_.add<scalar>("prevMeritDeriv", prevMeritDeriv_, true);
206  lineSearchDict_.add<label>("iter", iter_, true);
207  lineSearchDict_.regIOobject::writeObject
208  (
210  true
211  );
212 
213  return *this;
214 }
215 
216 
218 {
219  return operator++();
220 }
221 
222 
223 // ************************************************************************* //
Foam::lineSearch::reset
virtual void reset()
Reset step to initial value.
Definition: lineSearch.C:163
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::lineSearch::setOldMeritValue
void setOldMeritValue(const scalar value)
Set old objective value.
Definition: lineSearch.C:156
Foam::lineSearch::setNewMeritValue
void setNewMeritValue(const scalar value)
Set new objective value.
Definition: lineSearch.C:149
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(reactionRateFlameArea, dictionary)
Foam::lineSearch::step
scalar step() const
Get current step.
Definition: lineSearch.C:189
Foam::lineSearch::setDeriv
virtual void setDeriv(const scalar deriv)
Set objective derivative.
Definition: lineSearch.C:136
Foam::lineSearch::operator++
virtual lineSearch & operator++()
Definition: lineSearch.C:201
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::lineSearch::dict_
const dictionary dict_
Subdict within updateMethod.
Definition: lineSearch.H:63
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
Foam::lineSearch::maxIters
label maxIters() const
Get max number of iterations.
Definition: lineSearch.C:183
Foam::lineSearch::setDirection
void setDirection(const scalarField &direction)
Set direction.
Definition: lineSearch.C:143
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
Foam::Field< scalar >
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::lineSearch::New
static autoPtr< lineSearch > New(const dictionary &dict, const Time &time)
Return a reference to the selected turbulence model.
Definition: lineSearch.C:96
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
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
timeName
word timeName
Definition: getTimeIndex.H:3
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:123
Foam::lineSearch::coeffsDict
const dictionary & coeffsDict()
Optional coeffs dict.
Definition: lineSearch.C:44
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::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::autoPtr::reset
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
Time.H
Foam::autoPtr< Foam::lineSearch >
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
lineSearch.H
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
bool
bool
Definition: EEqn.H:20
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::dictionary::optionalSubDict
const dictionary & optionalSubDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary, otherwise return this dictionary.
Definition: dictionary.C:577
Foam::lineSearch::updateStep
virtual void updateStep()=0
Foam::stepUpdate
Abstract base class for step update methods used in line search.
Definition: stepUpdate.H:54
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::lineSearch
Abstract base class for line search methods.
Definition: lineSearch.H:56
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)