optimisationManager.H
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-2023 PCOpt/NTUA
9  Copyright (C) 2013-2023 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 Class
30  Foam::optimisationManager
31 
32 Description
33  Abstract base class for optimisation methods
34 
35 SourceFiles
36  optimisationManager.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef optimisationManager_H
41 #define optimisationManager_H
42 
43 #include "runTimeSelectionTables.H"
44 #include "IOdictionary.H"
45 #include "primalSolver.H"
46 #include "designVariables.H"
47 #include "designVariablesUpdate.H"
48 #include "adjointSolverManager.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 /*---------------------------------------------------------------------------*\
56  Class optimisationManager Declaration
57 \*---------------------------------------------------------------------------*/
58 
60 :
61  public IOdictionary
62 {
63 protected:
64 
65  // Protected data
66 
67  //- Reference to the mesh
68  fvMesh& mesh_;
69 
70  //- Reference to the time
71  Time& time_;
72 
73  //- Design variables of the optimisation problem.
74  // Constructed before the adjoint solvers, since
75  // some objective functions depend on their values
77 
78  //- List of primal solvers to be included in the optimisation
80 
81  //- List of adjoint solver managers to be included in the optimisation
83 
84  //- Type of the optimisation manager (singleRun, (un)steadyOptimisation)
85  const word managerType_;
86 
87  //- Helper class managing parts of the optimisation.
88  // Allocated only when an actual optimisation is conducted.
90 
91  //- Switch defining if the design variables should be updated or not.
93 
94 
95  // Protected Member Functions
96 
97  //- Reset time
98  // Does nothing in base, useful in unsteady adjoint
99  virtual void resetTime
100  (
102  const label startTimeIndex,
103  const scalar endTime
104  );
106  //- Update design variables
107  virtual void moveDesignVariables();
108 
109  //- Update design variables. Multiplication with line search step
110  // happens inside the update(direction) function
111  virtual void moveDesignVariables
112  (
113  const scalarField& direction
114  );
115 
116  //- Update design variables using a line-search
117  void lineSearchUpdate();
118 
119  //- Update design variables using a fixed step
120  void fixedStepUpdate();
121 
122  //- Initialization. Construct primal and adjoint solvers
123  virtual void initialize();
124 
125 
126 private:
127 
128  // Private Member Functions
129 
130  //- No copy construct
131  optimisationManager(const optimisationManager&) = delete;
132 
133  //- No copy assignment
134  void operator=(const optimisationManager&) = delete;
135 
136 
137 public:
138 
139  //- Runtime type information
140  TypeName("optimisationManager");
141 
142 
143  // Declare run-time constructor selection table
144 
146  (
147  autoPtr,
149  dictionary,
150  (
151  fvMesh& mesh
152  ),
153  (mesh)
154  );
155 
156 
157  // Constructors
158 
159  //- Construct from components
161 
162 
163  // Selectors
164 
165  //- Return a reference to the selected turbulence model
167 
168 
169  //- Destructor
170  virtual ~optimisationManager() = default;
171 
172 
173  // Member Functions
174 
175  // Access
176 
177  //- Get the primal solvers
179  {
180  return primalSolvers_;
181  }
182 
183  //- Get the adjoint solver managers
185  {
186  return adjointSolverManagers_;
187  }
188 
189  //- Get the design variables
190  inline autoPtr<designVariables>& getDesignVariables()
191  {
192  return designVars_;
193  }
194 
195  //- Get the mechanism supporting the update of the design variables
196  inline autoPtr<designVariablesUpdate>& getOptimisationType()
197  {
198  return dvUpdate_;
199  }
200 
201 
202  // Evolution
203 
204  //- Changes in case of run-time update of optimisationDict
205  virtual bool read();
206 
207  //- Prefix increment
208  virtual optimisationManager& operator++() = 0;
209 
210  //- Postfix increment, this is identical to the prefix increment
211  virtual optimisationManager& operator++(int) = 0;
212 
213  //- Return true if end of optimisation run.
214  // Also, updates the design variables if needed
215  virtual bool checkEndOfLoopAndUpdate() = 0;
216 
217  //- Return true if end of optimisation run
218  virtual bool end() = 0;
220  //- Whether to update the design variables
221  virtual bool update() = 0;
222 
223  //- Update design variables.
224  // Might employ a line search to find a correction satisfying the
225  // step convergence criteria
226  virtual void updateDesignVariables();
228  //- Solve all primal equations
229  virtual void solvePrimalEquations();
230 
231  //- Solve all adjoint equations
232  virtual void solveAdjointEquations();
233 
234  //- Compute all adjoint sensitivities
235  virtual void computeSensitivities();
236 
237  //- Clear all adjoint sensitivities
238  virtual void clearSensitivities();
239 
240  //- Solve all primal equations
241  virtual void updatePrimalBasedQuantities();
242 };
244 
245 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
246 
247 } // End namespace Foam
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 #endif
252 
253 // ************************************************************************* //
static autoPtr< optimisationManager > New(fvMesh &mesh)
Return a reference to the selected turbulence model.
virtual bool checkEndOfLoopAndUpdate()=0
Return true if end of optimisation run.
PtrList< adjointSolverManager > adjointSolverManagers_
List of adjoint solver managers to be included in the optimisation.
uint8_t direction
Definition: direction.H:46
void fixedStepUpdate()
Update design variables using a fixed step.
virtual void updatePrimalBasedQuantities()
Solve all primal equations.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
fvMesh & mesh_
Reference to the mesh.
Abstract base class for optimisation methods.
Time & time_
Reference to the time.
declareRunTimeSelectionTable(autoPtr, optimisationManager, dictionary,(fvMesh &mesh),(mesh))
autoPtr< designVariablesUpdate > & getOptimisationType()
Get the mechanism supporting the update of the design variables.
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, any/none. Also accepts 0/1 as a string and shortcuts t/f, y/n.
Definition: Switch.H:77
autoPtr< designVariablesUpdate > dvUpdate_
Helper class managing parts of the optimisation.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
virtual void initialize()
Initialization. Construct primal and adjoint solvers.
PtrList< primalSolver > primalSolvers_
List of primal solvers to be included in the optimisation.
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
const word managerType_
Type of the optimisation manager (singleRun, (un)steadyOptimisation)
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual ~optimisationManager()=default
Destructor.
void lineSearchUpdate()
Update design variables using a line-search.
PtrList< primalSolver > & primalSolvers()
Get the primal solvers.
PtrList< adjointSolverManager > & adjointSolverManagers()
Get the adjoint solver managers.
virtual void resetTime(const dimensionedScalar startTime, const label startTimeIndex, const scalar endTime)
Reset time.
virtual void moveDesignVariables()
Update design variables.
virtual bool end()=0
Return true if end of optimisation run.
autoPtr< designVariables > designVars_
Design variables of the optimisation problem.
virtual void computeSensitivities()
Compute all adjoint sensitivities.
virtual optimisationManager & operator++()=0
Prefix increment.
Switch shouldUpdateDesignVariables_
Switch defining if the design variables should be updated or not.
virtual void solveAdjointEquations()
Solve all adjoint equations.
TypeName("optimisationManager")
Runtime type information.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
autoPtr< designVariables > & getDesignVariables()
Get the design variables.
virtual void clearSensitivities()
Clear all adjoint sensitivities.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Macros to ease declaration of run-time selection tables.
Foam::label startTime
virtual bool read()
Changes in case of run-time update of optimisationDict.
virtual bool update()=0
Whether to update the design variables.
virtual void solvePrimalEquations()
Solve all primal equations.
virtual void updateDesignVariables()
Update design variables.
Namespace for OpenFOAM.