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-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 "steadyOptimisation.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(steadyOptimisation, 0);
39  (
40  optimisationManager,
41  steadyOptimisation,
42  dictionary
43  );
44 }
45 
46 
47 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 void 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  autoPtr<lineSearch>& lineSrch = optType_->getLineSearch();
74 
75  // Store starting point
76  optType_->storeDesignVariables();
77 
78  // Compute merit function before update
79  scalar meritFunction = optType_->computeMeritFunction();
80  lineSrch->setOldMeritValue(meritFunction);
81 
82  // Get merit function derivative
83  const scalar dirDerivative =
84  optType_->meritFunctionDirectionalDerivative();
85  lineSrch->setDeriv(dirDerivative);
86  lineSrch->setDirection(direction);
87 
88  // Reset initial step.
89  // Might be interpolated from previous optimisation cycles
90  lineSrch->reset();
91 
92  // Perform line search
93  for (label iter = 0; iter < lineSrch->maxIters(); ++iter)
94  {
95  Info<< "\n- - - - - - - - - - - - - - -" << endl;
96  Info<< "Line search iteration " << iter << endl;
97  Info<< "- - - - - - - - - - - - - - -\n" << endl;
98 
99  // Update design variables. Multiplication with line search step
100  // happens inside the update(direction) function
101  optType_->update(direction);
102 
103  // Solve all primal equations
104  solvePrimalEquations();
105 
106  // Compute and set new merit function
107  meritFunction = optType_->computeMeritFunction();
108  lineSrch->setNewMeritValue(meritFunction);
109 
110  if (lineSrch->converged())
111  {
112  // If line search criteria have been met, proceed
113  Info<< "Line search converged in " << iter + 1
114  << " iterations." << endl;
115  scalarField scaledCorrection(lineSrch->step()*direction);
116  optType_->updateOldCorrection(scaledCorrection);
117  optType_->write();
118  lineSrch()++;
119  break;
120  }
121  else
122  {
123  // If maximum number of iteration has been reached, continue
124  if (iter == lineSrch->maxIters()-1)
125  {
126  Info<< "Line search reached max. number of iterations.\n"
127  << "Proceeding to the next optimisation cycle" << endl;
128  scalarField scaledCorrection(lineSrch->step()*direction);
129  optType_->updateOldCorrection(scaledCorrection);
130  optType_->write();
131  lineSrch()++;
132  }
133  // Reset to initial design variables and update step
134  else
135  {
136  optType_->resetDesignVariables();
137  lineSrch->updateStep();
138  }
139  }
140  }
141 }
142 
143 
145 {
146  // Update based on fixed step
147  optType_->update(direction);
148 
149  // If direction has been scaled (say by setting the initial eta), the
150  // old correction has to be updated
151  optType_->updateOldCorrection(direction);
152  optType_->write();
153 
154  // Solve primal equations
155  solvePrimalEquations();
156 }
157 
158 
159 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
160 
161 Foam::steadyOptimisation::steadyOptimisation(fvMesh& mesh)
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  // Compute direction of update
227  tmp<scalarField> tdirection = optType_->computeDirection();
228  scalarField& direction = tdirection.ref();
229  autoPtr<lineSearch>& lineSrch = optType_->getLineSearch();
230 
231  // Update design variables using either a line-search scheme or
232  // a fixed-step update
233  if (lineSrch.valid())
234  {
235  lineSearchUpdate(direction);
236  }
237  else
238  {
239  fixedStepUpdate(direction);
240  }
241 
242  // Reset adjoint sensitivities in all adjoint solver managers
243  for (adjointSolverManager& adjSolverManager : adjointSolverManagers_)
244  {
245  adjSolverManager.clearSensitivities();
246  }
247 }
248 
249 
250 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::lineSearch::reset
virtual void reset()
Reset step to initial value.
Definition: lineSearch.C:163
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
update
mesh update()
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::optimisationManager::adjointSolverManagers_
PtrList< adjointSolverManager > adjointSolverManagers_
Definition: optimisationManager.H:68
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::steadyOptimisation::update
virtual bool update()
Whether to update the design variables.
Definition: steadyOptimisation.C:218
Foam::steadyOptimisation::operator++
virtual optimisationManager & operator++()
Prefix increment.
Definition: steadyOptimisation.C:183
Foam::autoPtr::valid
bool valid() const noexcept
True if the managed pointer is non-null.
Definition: autoPtrI.H:107
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::steadyOptimisation::end
virtual bool end()
Return true if end of optimisation run.
Definition: steadyOptimisation.C:212
Foam::steadyOptimisation::lineSearchUpdate
void lineSearchUpdate(scalarField &direction)
Update design variables using a line-search.
Definition: steadyOptimisation.C:71
Foam::optimisationManager::optType_
autoPtr< incompressible::optimisationType > optType_
Definition: optimisationManager.H:70
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
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
Foam::tmp::ref
T & ref() const
Definition: tmpI.H:258
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::optimisationManager::primalSolvers_
PtrList< primalSolver > primalSolvers_
Definition: optimisationManager.H:67
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:523
steadyOptimisation.H
Foam::lineSearch::converged
virtual bool converged()=0
Return the correction of the design variables.
Foam::adjointSolverManager
Class for managing adjoint solvers, which may be more than one per operating point.
Definition: adjointSolverManager.H:54
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:115
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::steadyOptimisation::checkEndOfLoopAndUpdate
virtual bool checkEndOfLoopAndUpdate()
Return true if end of optimisation run.
Definition: steadyOptimisation.C:202
Foam::direction
uint8_t direction
Definition: direction.H:47
Foam::steadyOptimisation::fixedStepUpdate
void fixedStepUpdate(scalarField &direction)
Update design variables using a fixed step.
Definition: steadyOptimisation.C:144
Foam::lineSearch::updateStep
virtual void updateStep()=0
Foam::steadyOptimisation::updateDesignVariables
virtual void updateDesignVariables()
Definition: steadyOptimisation.C:224
Foam::incompressible::optimisationType::New
static autoPtr< optimisationType > New(fvMesh &mesh, const dictionary &dict, PtrList< adjointSolverManager > &adjointSolverManagers)
Return a reference to the selected turbulence model.
Definition: optimisationTypeIncompressible.C:128
Foam::optimisationManager
Abstract base class for optimisation methods.
Definition: optimisationManager.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)