steadyOptimisation.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-2020 PCOpt/NTUA
9 Copyright (C) 2013-2020 FOSS GP
10 Copyright (C) 2019-2020 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 "steadyOptimisation.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35namespace Foam
36{
39 (
43 );
44}
45
46
47// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48
49void Foam::steadyOptimisation::updateOptTypeSource()
50{
52 {
53 primalSolvers_[pI].updateOptTypeSource(optType_->sourcePtr());
54 }
55
57 {
58 PtrList<adjointSolver>& adjointSolvers =
59 adjointSolverManagers_[asmI].adjointSolvers();
60
61 forAll(adjointSolvers, aI)
62 {
63 adjointSolvers[aI].updateOptTypeSource(optType_->sourcePtr());
64 }
65 }
66}
67
68
69// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
70
72{
73 // Compute direction of update
74 tmp<scalarField> tdirection = optType_->computeDirection();
75 scalarField& direction = tdirection.ref();
76
77 // Grab reference to line search
78 autoPtr<lineSearch>& lineSrch = optType_->getLineSearch();
79
80 // Store starting point
81 optType_->storeDesignVariables();
82
83 // Compute merit function before update
84 scalar meritFunction = optType_->computeMeritFunction();
85 lineSrch->setOldMeritValue(meritFunction);
86
87 // Get merit function derivative
88 const scalar dirDerivative =
89 optType_->meritFunctionDirectionalDerivative();
90 lineSrch->setDeriv(dirDerivative);
91 lineSrch->setDirection(direction);
92
93 // Reset initial step.
94 // Might be interpolated from previous optimisation cycles
95 lineSrch->reset();
96
97 // Perform line search
98 for (label iter = 0; iter < lineSrch->maxIters(); ++iter)
99 {
100 Info<< "\n- - - - - - - - - - - - - - -" << endl;
101 Info<< "Line search iteration " << iter << endl;
102 Info<< "- - - - - - - - - - - - - - -\n" << endl;
103
104 // Update design variables. Multiplication with line search step
105 // happens inside the update(direction) function
106 optType_->update(direction);
107
108 // Solve all primal equations
109 solvePrimalEquations();
110
111 // Compute and set new merit function
112 meritFunction = optType_->computeMeritFunction();
113 lineSrch->setNewMeritValue(meritFunction);
114
115 if (lineSrch->converged())
116 {
117 // If line search criteria have been met, proceed
118 Info<< "Line search converged in " << iter + 1
119 << " iterations." << endl;
120 scalarField scaledCorrection(lineSrch->step()*direction);
121 optType_->updateOldCorrection(scaledCorrection);
122 optType_->write();
123 lineSrch()++;
124 break;
125 }
126 else
127 {
128 // If maximum number of iteration has been reached, continue
129 if (iter == lineSrch->maxIters() - 1)
130 {
131 Info<< "Line search reached max. number of iterations.\n"
132 << "Proceeding to the next optimisation cycle" << endl;
133 scalarField scaledCorrection(lineSrch->step()*direction);
134 optType_->updateOldCorrection(scaledCorrection);
135 optType_->write();
136 lineSrch()++;
137 }
138 // Reset to initial design variables and update step
139 else
140 {
141 optType_->resetDesignVariables();
142 lineSrch->updateStep();
143 }
144 }
145 }
146}
147
148
150{
151 // Update design variables
152 optType_->update();
153
154 // Solve primal equations
155 solvePrimalEquations();
156}
157
158
159// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
160
162:
164{
165 optType_.reset
166 (
168 (
169 mesh,
170 subDict("optimisation"),
172 ).ptr()
173 );
174
175 // Update source ptrs in all solvers to look at the source held in optType
176 // Possible problem if mesh is adapted
177 updateOptTypeSource();
178}
179
180
181// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
182
184{
185 time_++;
186 if (!end())
187 {
188 Info<< "\n* * * * * * * * * * * * * * * * *" << endl;
189 Info<< "Optimisation cycle " << time_.value() << endl;
190 Info<< "* * * * * * * * * * * * * * * * *\n" << endl;
191 }
192 return *this;
193}
194
195
197{
198 return operator++();
199}
200
201
203{
204 if (update())
205 {
206 optType_->update();
207 }
208 return end();
209}
210
211
213{
214 return time_.end();
215}
216
217
219{
220 return (time_.timeIndex() != 1 && !end());
221}
222
223
225{
226 // Update design variables using either a line-search scheme or
227 // a fixed-step update
228 if (optType_->getLineSearch())
229 {
230 lineSearchUpdate();
231 }
232 else
233 {
234 fixedStepUpdate();
235 }
236
237 // Reset adjoint sensitivities in all adjoint solver managers
238 for (adjointSolverManager& adjSolverManager : adjointSolverManagers_)
239 {
240 adjSolverManager.clearSensitivities();
241 }
242}
243
244
245// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Class for managing adjoint solvers, which may be more than one per operating point.
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 & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:460
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:91
Abstract base class for optimisation methods.
autoPtr< incompressible::optimisationType > optType_
PtrList< adjointSolverManager > adjointSolverManagers_
PtrList< primalSolver > primalSolvers_
Iterate the optimisation cycles. For steady state opt, this coinsides with evolving Time.
virtual optimisationManager & operator++()
Prefix increment.
virtual void updateDesignVariables()
virtual bool update()
Whether to update the design variables.
virtual bool checkEndOfLoopAndUpdate()
Return true if end of optimisation run.
void lineSearchUpdate()
Update design variables using a line-search.
virtual bool end()
Return true if end of optimisation run.
void fixedStepUpdate()
Update design variables using a fixed step.
A class for managing temporary objects.
Definition: tmp.H:65
T & ref() const
Definition: tmpI.H:227
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
mesh update()
dynamicFvMesh & mesh
Namespace for OpenFOAM.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
uint8_t direction
Definition: direction.H:56
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333