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-------------------------------------------------------------------------------
12License
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
35namespace Foam
36{
39}
40
41
42// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
43
45{
46 return dict_.optionalSubDict(type() + "Coeffs");
47}
48
49
50// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51
53:
54 dict_(dict),
55 lineSearchDict_
56 (
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
136void Foam::lineSearch::setDeriv(const scalar deriv)
137{
138 directionalDeriv_ = deriv;
139 stepUpdate_->setDeriv(deriv);
140}
141
142
144{
145 direction_ = direction;
146}
147
148
150{
151 newMeritValue_ = value;
152 stepUpdate_->setNewMeritValue(value);
153}
154
155
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
183Foam::label Foam::lineSearch::maxIters() const
184{
185 return maxIters_;
186}
187
188
189Foam::scalar Foam::lineSearch::step() const
190{
191 return step_;
192}
193
194
195void 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// ************************************************************************* //
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
The IOstreamOption is a simple container for options an IOstream can normally have.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void reset(autoPtr< T > &&other) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:117
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
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
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Abstract base class for line search methods.
Definition: lineSearch.H:57
label maxIters() const
Get max number of iterations.
Definition: lineSearch.C:183
const dictionary & coeffsDict()
Optional coeffs dict.
Definition: lineSearch.C:44
virtual void setDeriv(const scalar deriv)
Set objective derivative.
Definition: lineSearch.C:136
void setOldMeritValue(const scalar value)
Set old objective value.
Definition: lineSearch.C:156
scalar step() const
Get current step.
Definition: lineSearch.C:189
void setNewMeritValue(const scalar value)
Set new objective value.
Definition: lineSearch.C:149
const dictionary dict_
Subdict within updateMethod.
Definition: lineSearch.H:63
virtual void updateStep()=0
virtual lineSearch & operator++()
Definition: lineSearch.C:201
virtual void reset()
Reset step to initial value.
Definition: lineSearch.C:163
void setDirection(const scalarField &direction)
Set direction.
Definition: lineSearch.C:143
Abstract base class for step update methods used in line search.
Definition: stepUpdate.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
bool
Definition: EEqn.H:20
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:478
word timeName
Definition: getTimeIndex.H:3
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
messageStream Info
Information stream (stdout output on master, null elsewhere)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
uint8_t direction
Definition: direction.H:56
IOerror FatalIOError
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
#define defineRunTimeSelectionTable(baseType, argNames)
Define run-time selection table.
dictionary dict