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 -------------------------------------------------------------------------------
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  // 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 
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  // 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 // ************************************************************************* //
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:61
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::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::steadyOptimisation::end
virtual bool end()
Return true if end of optimisation run.
Definition: steadyOptimisation.C:212
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:296
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:227
Foam::Field< scalar >
Foam::optimisationManager::primalSolvers_
PtrList< primalSolver > primalSolvers_
Definition: optimisationManager.H:67
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::steadyOptimisation::fixedStepUpdate
void fixedStepUpdate()
Update design variables using a fixed step.
Definition: steadyOptimisation.C:149
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:460
steadyOptimisation.H
Foam::steadyOptimisation::lineSearchUpdate
void lineSearchUpdate()
Update design variables using a line-search.
Definition: steadyOptimisation.C:71
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:121
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
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:52
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:127
Foam::optimisationManager
Abstract base class for optimisation methods.
Definition: optimisationManager.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)