Documentation 6b54b78464
Loading...
Searching...
No Matches
CDiscAdjMultizoneDriver.hpp
Go to the documentation of this file.
1/*!
2 * \class CDiscAdjMultizoneDriver.hpp
3 * \brief Class for driving adjoint multi-zone problems.
4 * \author O. Burghardt, P. Gomes, T. Albring, R. Sanchez
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29#include "CMultizoneDriver.hpp"
30#include "../../../Common/include/toolboxes/CQuasiNewtonInvLeastSquares.hpp"
31#include "../../../Common/include/linear_algebra/CSysSolve.hpp"
32
33/*!
34 * \brief Block Gauss-Seidel driver for multizone / multiphysics discrete adjoint problems.
35 * \ingroup DiscAdj
36 */
37
39
40protected:
41 #ifdef CODI_FORWARD_TYPE
42 using Scalar = su2double;
43 #else
45 #endif
46
47 /*!
48 * \brief Kinds of recordings.
49 */
50 enum class Kind_Tape {
51 FULL_SOLVER_TAPE, /*!< \brief Entire derivative information for a coupled adjoint
52 solution update. */
53 OBJECTIVE_FUNCTION_TAPE, /*!< \brief Record only the dependence of the objective function
54 w.r.t. solver variables (from all zones). */
55 };
56
57 /*!
58 * \brief Position markers within a tape.
59 */
61 START = 0, /*!< \brief Beginning of the tape. */
62 REGISTERED = 1, /*!< \brief Solver variables are registered on the tape. */
63 DEPENDENCIES = 2, /*!< \brief Derived values (e.g. gradients) are set. */
64 OBJECTIVE_FUNCTION = 3, /*!< \brief Objective function is set. */
65 TRANSFER = 4, /*!< \brief Solution data is transferred between coupled solvers of
66 different physical zones (giving cross contributions). */
67 ITERATION_READY = 5 /*!< \brief Until here, all derivative information is gathered so
68 that it can be connected to a solver update evaluation. */
69 };
70
71 RECORDING RecordingState = RECORDING::CLEAR_INDICES; /*!< \brief The kind of recording that the tape currently holds. */
72
73 bool eval_transfer = false; /*!< \brief Evaluate the transfer section of the tape. */
74 su2double ObjFunc; /*!< \brief Value of the objective function. */
75 AD::Identifier ObjFunc_Index; /*!< \brief Index of the value of the objective function. */
76
77 CIteration*** direct_iteration; /*!< \brief Array of pointers to the direct iterations. */
78 COutput** direct_output; /*!< \brief Array of pointers to the direct outputs. */
79 vector<unsigned short> direct_nInst; /*!< \brief Total number of instances in the direct problem. */
80 vector<unsigned long> nInnerIter; /*!< \brief Number of inner iterations for each zone. */
81 unsigned long wrt_sol_freq = std::numeric_limits<unsigned long>::max(); /*!< \brief File output frequency. */
82
83 su2vector<bool> Has_Deformation; /*!< \brief True if iZone has mesh deformation (used for
84 lazy evaluation of TRANSFER tape section). */
85
86 /*!< \brief Individual cross-terms of the coupled problem, 5D array [iZone][jZone][iSol](iPoint,iVar).
87 The column sum, i.e. along all iZones for each jZone, gives the External (total cross-term)
88 for jZone, we need to store all terms to have BGS-type updates with relaxation. */
89 vector<vector<vector<su2passivematrix> > > Cross_Terms;
90
91 /*!< \brief Fixed-Point corrector that can be applied to inner iterations. */
92 vector<CQuasiNewtonInvLeastSquares<passivedouble> > FixPtCorrector;
93
94 /*!< \brief Members to use GMRES to drive inner iterations (alternative to quasi-Newton). */
95 static constexpr unsigned long KrylovMinIters = 3;
96 const Scalar KrylovTol = 0.01;
97 vector<CSysSolve<Scalar> > LinSolver;
98 vector<CSysVector<Scalar> > AdjRHS, AdjSol;
99
100public:
101
102 /*!
103 * \brief Constructor of the class.
104 * \param[in] confFile - Configuration file name.
105 * \param[in] val_nZone - Total number of zones.
106 * \param[in] MPICommunicator - MPI communicator for SU2.
107 */
108 CDiscAdjMultizoneDriver(char* confFile, unsigned short val_nZone, SU2_Comm MPICommunicator);
109
110 /*!
111 * \brief Destructor of the class.
112 */
113 ~CDiscAdjMultizoneDriver(void) override;
114
115 /*!
116 * \brief [Overload] Launch the computation for discrete adjoint multizone problems.
117 */
118 void StartSolver() override;
119
120 /*!
121 * \brief Preprocess the multizone iteration
122 */
123 void Preprocess(unsigned long TimeIter) override;
124
125 /*!
126 * \brief [Overload] Run an discrete adjoint update of all solvers within multiple zones.
127 */
128 void Run() override;
129
130 /*!
131 * \brief Run one inner iteration for a given zone.
132 * \return The result of "monitor".
133 */
134 bool Iterate(unsigned short iZone, unsigned long iInnerIter, bool KrylovMode = false);
135
136protected:
137
138 /*!
139 * \brief Run inner iterations using a Krylov method (GMRES atm).
140 */
141 void KrylovInnerIters(unsigned short iZone);
142
143 /*!
144 * \brief Evaluate the gradient of the objective function and add to "External".
145 * \return "True" if the gradient is numerically 0.
146 */
148
149 /*!
150 * \brief Evaluate sensitivites for the current adjoint solution and output files.
151 * \param[in] Iter - Current outer or time iteration.
152 * \param[in] force_writing - Force file output.
153 */
154 void EvaluateSensitivities(unsigned long Iter, bool force_writing);
155
156 /*!
157 * \brief Setup the matrix of cross-terms. Allocate necessary memory and initialize to zero.
158 */
160
161 /*!
162 * \brief Record one iteration of the primal problem within each zone.
163 * \param[in] kind_recording - Kind of variables with respect to which we are recording.
164 * \param[in] tape_type - indicator which part of a solution update will be recorded.
165 * \param[in] record_zone - zone where solution update will be recorded.
166 */
167 void SetRecording(RECORDING kind_recording, Kind_Tape tape_type, unsigned short record_zone);
168
169 /*!
170 * \brief Transfer data between zones and update grids when required.
171 */
172 void HandleDataTransfer();
173
174 /*!
175 * \brief Run one direct iteration in a zone.
176 * \param[in] iZone - Zone in which we run an iteration.
177 * \param[in] kind_recording - Kind of variables with respect to which we are recording.
178 */
179 void DirectIteration(unsigned short iZone, RECORDING kind_recording);
180
181 /*!
182 * \brief Set the objective function.
183 * \param[in] kind_recording - Kind of variables with respect to which we are recording.
184 */
185 void SetObjFunction(RECORDING kind_recording);
186
187 /*!
188 * \brief Initialize the adjoint value of the objective function.
189 */
190 void SetAdjObjFunction();
191
192 /*!
193 * \brief Summary of all routines to evaluate the adjoints in iZone.
194 * \param[in] iZone - Zone in which adjoints are evaluated depending on their (preceding) seeding.
195 * \param[in] eval_transfer - Evaluate adjoints of transfer and mesh deformation routines.
196 */
197 void ComputeAdjoints(unsigned short iZone, bool eval_transfer = true);
198
199 /*!
200 * \brief Puts BGSSolution_k back into Solution.
201 * \param[in] iZone - Zone index.
202 */
203 void Set_Solution_To_BGSSolution_k(unsigned short iZone);
204
205 /*!
206 * \brief Puts Solution into BGSSolution_k.
207 * \param[in] iZone - Zone index.
208 */
209 void Set_BGSSolution_k_To_Solution(unsigned short iZone);
210
211 /*!
212 * \brief Add Solution vector to External.
213 * \param[in] iZone - Zone index.
214 */
215 void AddSolutionToExternal(unsigned short iZone);
216
217 /*!
218 * \brief Puts dual time derivative vector to External.
219 */
221
222 /*!
223 * \brief Add External_Old vector to Solution.
224 * \param[in] iZone - Zone index.
225 */
226 void AddExternalToSolution(unsigned short iZone);
227
228 /*!
229 * \brief Puts Solution into SolutionOld.
230 * \param[in] iZone - Zone index.
231 */
232 void SetSolutionOldToSolution(unsigned short iZone);
233
234 /*!
235 * \brief Extract contribution of iZone to jZone with BGS relaxation.
236 * \param[in] iZone - Source zone (the one that was initialized).
237 * \param[in] jZone - Target zone (the one that transfers to iZone in the primal problem).
238 */
239 void UpdateCrossTerm(unsigned short iZone, unsigned short jZone);
240
241 /*!
242 * \brief Compute BGS residuals.
243 * \param[in] iZone - Zone where solver residuals are computed.
244 */
245 void SetResidual_BGS(unsigned short iZone);
246
247 /*!
248 * \brief gets Convergence on physical time scale, (deactivated in adjoint case)
249 * \return false
250 */
251 inline bool GetTimeConvergence() const override { return false; }
252
253 /*!
254 * \brief Get the external of all adjoint solvers in a zone.
255 * \param[in] iZone - Index of the zone.
256 * \param[out] rhs - Object with interface (iPoint,iVar), set to -external.
257 */
258 template<class Container>
259 void GetAdjointRHS(unsigned short iZone, Container& rhs) const {
260 const auto nPoint = geometry_container[iZone][INST_0][MESH_0]->GetnPoint();
261 for (auto iSol = 0u, offset = 0u; iSol < MAX_SOLS; ++iSol) {
262 auto solver = solver_container[iZone][INST_0][MESH_0][iSol];
263 if (!(solver && solver->GetAdjoint())) continue;
264 const auto& ext = solver->GetNodes()->Get_External();
265 for (auto iPoint = 0ul; iPoint < nPoint; ++iPoint)
266 for (auto iVar = 0ul; iVar < solver->GetnVar(); ++iVar)
267 rhs(iPoint,offset+iVar) = -SU2_TYPE::GetValue(ext(iPoint,iVar));
268 offset += solver->GetnVar();
269 }
270 }
271
272 /*!
273 * \brief Launch the tape test run of the discrete adjoint multizone solver.
274 */
275 void TapeTest ();
276
277 /*!
278 * \brief Get the total error count after a tape test run of the discrete adjoint multizone solver.
279 * \param[in] debug_control - DebugControl from which this rank's contribution to the total error count is read.
280 * \return The total error count across all ranks.
281 */
282 int TapeTestGatherErrors(AD::DebugControl& debug_control) const;
283};
Headers of the main subroutines for driving single or multi-zone problems. The subroutines and functi...
A templated matrix/vector-like object.
Definition C2DContainer.hpp:374
Block Gauss-Seidel driver for multizone / multiphysics discrete adjoint problems.
Definition CDiscAdjMultizoneDriver.hpp:38
void SetResidual_BGS(unsigned short iZone)
Compute BGS residuals.
Definition CDiscAdjMultizoneDriver.cpp:1177
void SetSolutionOldToSolution(unsigned short iZone)
Puts Solution into SolutionOld.
Definition CDiscAdjMultizoneDriver.cpp:1137
bool Iterate(unsigned short iZone, unsigned long iInnerIter, bool KrylovMode=false)
Run one inner iteration for a given zone.
Definition CDiscAdjMultizoneDriver.cpp:398
void ComputeAdjoints(unsigned short iZone, bool eval_transfer=true)
Summary of all routines to evaluate the adjoints in iZone.
Definition CDiscAdjMultizoneDriver.cpp:1008
void SetAdjObjFunction()
Initialize the adjoint value of the objective function.
Definition CDiscAdjMultizoneDriver.cpp:983
AD::Identifier ObjFunc_Index
Index of the value of the objective function.
Definition CDiscAdjMultizoneDriver.hpp:75
su2vector< bool > Has_Deformation
True if iZone has mesh deformation (used for lazy evaluation of TRANSFER tape section).
Definition CDiscAdjMultizoneDriver.hpp:83
vector< vector< vector< su2passivematrix > > > Cross_Terms
Fixed-Point corrector that can be applied to inner iterations.
Definition CDiscAdjMultizoneDriver.hpp:89
COutput ** direct_output
Array of pointers to the direct outputs.
Definition CDiscAdjMultizoneDriver.hpp:78
void Set_Solution_To_BGSSolution_k(unsigned short iZone)
Puts BGSSolution_k back into Solution.
Definition CDiscAdjMultizoneDriver.cpp:1157
void DirectIteration(unsigned short iZone, RECORDING kind_recording)
Run one direct iteration in a zone.
Definition CDiscAdjMultizoneDriver.cpp:903
Tape_Positions
Position markers within a tape.
Definition CDiscAdjMultizoneDriver.hpp:60
@ DEPENDENCIES
Derived values (e.g. gradients) are set.
Definition CDiscAdjMultizoneDriver.hpp:63
@ TRANSFER
Solution data is transferred between coupled solvers of different physical zones (giving cross contri...
Definition CDiscAdjMultizoneDriver.hpp:65
@ ITERATION_READY
Until here, all derivative information is gathered so that it can be connected to a solver update eva...
Definition CDiscAdjMultizoneDriver.hpp:67
@ REGISTERED
Solver variables are registered on the tape.
Definition CDiscAdjMultizoneDriver.hpp:62
@ OBJECTIVE_FUNCTION
Objective function is set.
Definition CDiscAdjMultizoneDriver.hpp:64
@ START
Beginning of the tape.
Definition CDiscAdjMultizoneDriver.hpp:61
void AddExternalToSolution(unsigned short iZone)
Add External_Old vector to Solution.
Definition CDiscAdjMultizoneDriver.cpp:1127
void Set_BGSSolution_k_To_Solution(unsigned short iZone)
Puts Solution into BGSSolution_k.
Definition CDiscAdjMultizoneDriver.cpp:1167
vector< unsigned long > nInnerIter
Number of inner iterations for each zone.
Definition CDiscAdjMultizoneDriver.hpp:80
void SetRecording(RECORDING kind_recording, Kind_Tape tape_type, unsigned short record_zone)
Record one iteration of the primal problem within each zone.
Definition CDiscAdjMultizoneDriver.cpp:769
~CDiscAdjMultizoneDriver(void) override
Destructor of the class.
Definition CDiscAdjMultizoneDriver.cpp:146
const Scalar KrylovTol
Definition CDiscAdjMultizoneDriver.hpp:96
vector< unsigned short > direct_nInst
Total number of instances in the direct problem.
Definition CDiscAdjMultizoneDriver.hpp:79
void InitializeCrossTerms()
Setup the matrix of cross-terms. Allocate necessary memory and initialize to zero.
Definition CDiscAdjMultizoneDriver.cpp:1045
void SetExternalToDualTimeDer()
Puts dual time derivative vector to External.
Definition CDiscAdjMultizoneDriver.cpp:1115
void StartSolver() override
[Overload] Launch the computation for discrete adjoint multizone problems.
Definition CDiscAdjMultizoneDriver.cpp:196
bool EvaluateObjectiveFunctionGradient()
Evaluate the gradient of the objective function and add to "External".
Definition CDiscAdjMultizoneDriver.cpp:662
vector< CQuasiNewtonInvLeastSquares< passivedouble > > FixPtCorrector
Members to use GMRES to drive inner iterations (alternative to quasi-Newton).
Definition CDiscAdjMultizoneDriver.hpp:92
vector< CSysSolve< Scalar > > LinSolver
Definition CDiscAdjMultizoneDriver.hpp:97
CIteration *** direct_iteration
Array of pointers to the direct iterations.
Definition CDiscAdjMultizoneDriver.hpp:77
vector< CSysVector< Scalar > > AdjRHS
Definition CDiscAdjMultizoneDriver.hpp:98
void UpdateCrossTerm(unsigned short iZone, unsigned short jZone)
Extract contribution of iZone to jZone with BGS relaxation.
Definition CDiscAdjMultizoneDriver.cpp:1147
unsigned long wrt_sol_freq
File output frequency.
Definition CDiscAdjMultizoneDriver.hpp:81
passivedouble Scalar
Definition CDiscAdjMultizoneDriver.hpp:44
void KrylovInnerIters(unsigned short iZone)
Run inner iterations using a Krylov method (GMRES atm).
Definition CDiscAdjMultizoneDriver.cpp:435
void EvaluateSensitivities(unsigned long Iter, bool force_writing)
Evaluate sensitivites for the current adjoint solution and output files.
Definition CDiscAdjMultizoneDriver.cpp:696
void Preprocess(unsigned long TimeIter) override
Preprocess the multizone iteration.
Definition CDiscAdjMultizoneDriver.cpp:162
Kind_Tape
Kinds of recordings.
Definition CDiscAdjMultizoneDriver.hpp:50
@ FULL_SOLVER_TAPE
Entire derivative information for a coupled adjoint solution update.
@ OBJECTIVE_FUNCTION_TAPE
Record only the dependence of the objective function w.r.t. solver variables (from all zones).
void HandleDataTransfer()
Transfer data between zones and update grids when required.
Definition CDiscAdjMultizoneDriver.cpp:1076
int TapeTestGatherErrors(AD::DebugControl &debug_control) const
Get the total error count after a tape test run of the discrete adjoint multizone solver.
Definition CDiscAdjMultizoneDriver.cpp:377
void TapeTest()
Launch the tape test run of the discrete adjoint multizone solver.
Definition CDiscAdjMultizoneDriver.cpp:269
RECORDING RecordingState
The kind of recording that the tape currently holds.
Definition CDiscAdjMultizoneDriver.hpp:71
void GetAdjointRHS(unsigned short iZone, Container &rhs) const
Get the external of all adjoint solvers in a zone.
Definition CDiscAdjMultizoneDriver.hpp:259
bool eval_transfer
Evaluate the transfer section of the tape.
Definition CDiscAdjMultizoneDriver.hpp:73
void SetObjFunction(RECORDING kind_recording)
Set the objective function.
Definition CDiscAdjMultizoneDriver.cpp:918
su2double ObjFunc
Value of the objective function.
Definition CDiscAdjMultizoneDriver.hpp:74
bool GetTimeConvergence() const override
gets Convergence on physical time scale, (deactivated in adjoint case)
Definition CDiscAdjMultizoneDriver.hpp:251
static constexpr unsigned long KrylovMinIters
Definition CDiscAdjMultizoneDriver.hpp:95
vector< CSysVector< Scalar > > AdjSol
Definition CDiscAdjMultizoneDriver.hpp:98
void AddSolutionToExternal(unsigned short iZone)
Add Solution vector to External.
Definition CDiscAdjMultizoneDriver.cpp:1105
void Run() override
[Overload] Run an discrete adjoint update of all solvers within multiple zones.
Definition CDiscAdjMultizoneDriver.cpp:488
CSolver ***** solver_container
Container vector with all the solutions.
Definition CDriverBase.hpp:71
CGeometry **** geometry_container
Geometrical definition of the problem.
Definition CDriverBase.hpp:70
unsigned short iZone
Iterator on zones.
Definition CDriverBase.hpp:59
unsigned long GetnPoint() const
Get number of points.
Definition CGeometry.hpp:587
Parent class for defining a single iteration of a physics problem.
Definition CIteration.hpp:50
Class for driving zone-specific iterations.
Definition CMultizoneDriver.hpp:40
unsigned long TimeIter
Definition CMultizoneDriver.hpp:46
Class for writing the convergence history and to write solution data to file.
Definition COutput.hpp:68
CVariable * GetNodes()
Allow outside access to the nodes of the solver, containing conservatives, primitives,...
Definition CSolver.hpp:229
const su2double * Get_External(unsigned long iPoint) const
Get the external contributions of the problem.
Definition CVariable.hpp:551
double passivedouble
Definition code_config.hpp:129
passivedouble su2double
Definition code_config.hpp:181
int SU2_Comm
Definition mpi_structure.hpp:619
int Identifier
Definition ad_structure.hpp:56
passivedouble GetValue(const su2double &data)
Get the (primitive) value of the datatype (needs to be specialized for active types).
Definition datatype_structure.hpp:113
const unsigned int INST_0
Definition of the first instance per grid level.
Definition option_structure.hpp:92
@ MAX_SOLS
Definition option_structure.hpp:80
const unsigned int MESH_0
Definition of the finest grid level.
Definition option_structure.hpp:88
RECORDING
Definition option_structure.hpp:2820
Struct to store information about errors during a tag debug run.
Definition ad_structure.hpp:332