Skip to content

Commit 52e42ec

Browse files
committed
cleanup the discadj fluid iteration
1 parent f269773 commit 52e42ec

2 files changed

Lines changed: 110 additions & 182 deletions

File tree

SU2_CFD/src/iteration/CDiscAdjFluidIteration.cpp

Lines changed: 99 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -41,69 +41,77 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
4141
const bool grid_IsMoving = config[iZone]->GetGrid_Movement();
4242
const bool species = config[iZone]->GetKind_Species_Model() != SPECIES_MODEL::NONE;
4343
const bool heat = config[iZone]->GetWeakly_Coupled_Heat();
44+
const bool radiation = config[iZone]->AddRadiation();
4445

4546
auto solvers0 = solver[iZone][iInst][MESH_0];
4647
auto geometries = geometry[iZone][iInst];
4748

49+
unsigned long nSolvers = 1;
50+
std::array<int, 5> solversToProcess{{FLOW_SOL}};
51+
if (turbulent) {
52+
solversToProcess[nSolvers] = TURB_SOL;
53+
++nSolvers;
54+
}
55+
if (species) {
56+
solversToProcess[nSolvers] = SPECIES_SOL;
57+
++nSolvers;
58+
}
59+
if (heat) {
60+
solversToProcess[nSolvers] = HEAT_SOL;
61+
++nSolvers;
62+
}
63+
if (radiation) {
64+
solversToProcess[nSolvers] = RAD_SOL;
65+
++nSolvers;
66+
}
67+
4868
/*--- For the unsteady adjoint, load direct solutions from restart files. ---*/
4969

5070
if (config[iZone]->GetTime_Marching() != TIME_MARCHING::STEADY) {
51-
const int Direct_Iter = static_cast<int>(config[iZone]->GetUnst_AdjointIter()) - static_cast<int>(TimeIter) - 2 + dual_time;
71+
const int Direct_Iter = static_cast<int>(config[iZone]->GetUnst_AdjointIter()) -
72+
static_cast<int>(TimeIter) - 2 + dual_time;
5273

53-
/*--- For dual-time stepping we want to load the already converged solution at timestep n ---*/
74+
/*--- For dual-time stepping we want to load the already converged solution at previous timesteps.
75+
* In general we only load one file and shift the previously loaded solutions, on the first we
76+
* load one or two more (depending on dual time order). ---*/
5477

55-
if (TimeIter == 0) {
56-
if (dual_time_2nd) {
57-
/*--- Load solution at timestep n-2 ---*/
58-
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 2);
78+
if (dual_time_2nd) {
79+
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 2);
80+
} else if (dual_time_1st) {
81+
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 1);
82+
}
5983

60-
/*--- Push solution back to correct array ---*/
84+
if (TimeIter == 0) {
85+
/*--- Push solution back one or two levels. ---*/
6186

87+
if (dual_time) {
6288
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
63-
auto solvers = solver[iZone][iInst][iMesh];
64-
65-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n();
66-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n1();
67-
if (turbulent) {
68-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n();
69-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n1();
70-
}
71-
if (species) {
72-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n();
73-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n1();
74-
}
75-
if (heat) {
76-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n();
77-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n1();
89+
for (auto iSol = 0ul; iSol < nSolvers; ++iSol) {
90+
solver[iZone][iInst][iMesh][solversToProcess[iSol]]->GetNodes()->Set_Solution_time_n();
91+
if (dual_time_2nd)
92+
solver[iZone][iInst][iMesh][solversToProcess[iSol]]->GetNodes()->Set_Solution_time_n1();
7893
}
7994
if (grid_IsMoving) {
8095
geometries[iMesh]->nodes->SetCoord_n();
81-
geometries[iMesh]->nodes->SetCoord_n1();
96+
if (dual_time_2nd)
97+
geometries[iMesh]->nodes->SetCoord_n1();
8298
}
8399
if (config[iZone]->GetDynamic_Grid()) {
84100
geometries[iMesh]->nodes->SetVolume_n();
85-
geometries[iMesh]->nodes->SetVolume_nM1();
101+
if (dual_time_2nd)
102+
geometries[iMesh]->nodes->SetVolume_nM1();
86103
}
87104
}
88105
}
89-
if (dual_time) {
90-
/*--- Load solution at timestep n-1 ---*/
91-
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 1);
92106

93-
/*--- Push solution back to correct array ---*/
107+
/*--- If required load another time step. ---*/
94108

95-
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
96-
auto solvers = solver[iZone][iInst][iMesh];
109+
if (dual_time_2nd) {
110+
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 1);
97111

98-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n();
99-
if (turbulent) {
100-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n();
101-
}
102-
if (species) {
103-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n();
104-
}
105-
if (heat) {
106-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n();
112+
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
113+
for (auto iSol = 0ul; iSol < nSolvers; ++iSol) {
114+
solver[iZone][iInst][iMesh][solversToProcess[iSol]]->GetNodes()->Set_Solution_time_n();
107115
}
108116
if (grid_IsMoving) {
109117
geometries[iMesh]->nodes->SetCoord_n();
@@ -123,15 +131,13 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
123131
}
124132

125133
}
126-
else if ((TimeIter > 0) && dual_time) {
127-
/*---
128-
Here the primal solutions (only working variables) are loaded and put in the correct order
129-
into containers. For ALE the mesh coordinates have to be put into the
130-
correct containers as well, i.e. follow the same logic for the solution.
131-
Afterwards the GridVelocity is computed based on the Coordinates.
132-
---*/
133-
134-
/*--- Temporarily store the loaded volumes into old containers ---*/
134+
if ((TimeIter > 0) && dual_time) {
135+
/*--- Here the primal solutions (only working variables) are loaded and put in the correct order
136+
into containers. For ALE the mesh coordinates have to be put into the correct containers as well,
137+
i.e. follow the same logic for the solution. Afterwards the GridVelocity is computed based on
138+
the Coordinates. ---*/
139+
140+
/*--- Temporarily store the loaded volumes into old containers. ---*/
135141
if (config[iZone]->GetDynamic_Grid()) {
136142
for (auto iMesh=0; iMesh<=config[iZone]->GetnMGLevels();iMesh++) {
137143
geometries[iMesh]->nodes->SetVolume_Old();
@@ -140,155 +146,80 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
140146
}
141147
}
142148

143-
/*-- Load mesh solver ---*/
149+
/*--- Load mesh solver. ---*/
144150
if (config[iZone]->GetDeform_Mesh()) {
145151
solvers0[MESH_SOL]->LoadRestart(geometries, solver[iZone][iInst], config[iZone], Direct_Iter, true);
146152
}
147153

148-
/*--- Load solution timestep n-1 | n-2 for DualTimestepping 1st | 2nd order ---*/
149-
if (dual_time_1st) {
150-
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 1);
151-
} else {
152-
LoadUnsteady_Solution(geometry, solver, config, iZone, iInst, Direct_Iter - 2);
153-
154-
/*--- Set volumes into correct containers ---*/
155-
if (config[iZone]->GetDynamic_Grid()) {
156-
for (auto iMesh=0; iMesh<=config[iZone]->GetnMGLevels();iMesh++) {
157-
/*--- If negative iteration number, set default ---*/
158-
if (Direct_Iter - 2 < 0) {
159-
for(auto iPoint=0ul; iPoint<geometries[iMesh]->GetnPoint();iPoint++) {
160-
geometries[iMesh]->nodes->SetVolume(iPoint,0.0);
161-
}
154+
/*--- Set volumes into correct containers ---*/
155+
if (config[iZone]->GetDynamic_Grid()) {
156+
for (auto iMesh = 0; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
157+
/*--- If negative iteration number, set default. ---*/
158+
if (Direct_Iter - 1 - dual_time_2nd < 0) {
159+
for(auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
160+
geometries[iMesh]->nodes->SetVolume(iPoint, 0.0);
162161
}
162+
}
163163

164-
/*--- Set currently loaded volume to Volume_nM1 ---*/
165-
geometries[iMesh]->nodes->SetVolume_n();
166-
geometries[iMesh]->nodes->SetVolume_nM1();
164+
/*--- Set currently loaded volume to Volume_nM1 ---*/
165+
geometries[iMesh]->nodes->SetVolume_n();
166+
geometries[iMesh]->nodes->SetVolume_nM1();
167167

168-
/*--- Set Volume_n and Volume from old containers ---*/
169-
geometries[iMesh]->nodes->SetVolume_n_from_OldnM1();
170-
geometries[iMesh]->nodes->SetVolume_from_Oldn();
171-
}
168+
/*--- Set Volume_n and Volume from old containers ---*/
169+
geometries[iMesh]->nodes->SetVolume_n_from_OldnM1();
170+
geometries[iMesh]->nodes->SetVolume_from_Oldn();
172171
}
173172
}
174173

175-
/*--- Temporarily store the loaded solution in the Solution_Old array ---*/
174+
/*--- Temporarily store the loaded solution and coordinates in the "Old" arrays. ---*/
176175

177176
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
178-
auto solvers = solver[iZone][iInst][iMesh];
179-
180-
solvers[FLOW_SOL]->Set_OldSolution();
181-
if (turbulent) {
182-
solvers[TURB_SOL]->Set_OldSolution();
183-
}
184-
if (species) {
185-
solvers[SPECIES_SOL]->Set_OldSolution();
186-
}
187-
if (heat) {
188-
solvers[HEAT_SOL]->Set_OldSolution();
177+
for (auto iSol = 0ul; iSol < nSolvers; ++iSol) {
178+
solver[iZone][iInst][iMesh][solversToProcess[iSol]]->Set_OldSolution();
189179
}
190180
if (grid_IsMoving) {
191181
geometries[iMesh]->nodes->SetCoord_Old();
192182
}
193183
}
194184

195-
/*--- Set Solution at timestep n to solution at n-1 ---*/
185+
/*--- Move timestep n to current solution. ---*/
196186

197187
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
198-
auto solvers = solver[iZone][iInst][iMesh];
199-
200-
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
201-
solvers[FLOW_SOL]->GetNodes()->SetSolution(iPoint, solvers[FLOW_SOL]->GetNodes()->GetSolution_time_n(iPoint));
202-
203-
if (grid_IsMoving) {
204-
geometries[iMesh]->nodes->SetCoord(iPoint, geometries[iMesh]->nodes->GetCoord_n(iPoint));
205-
}
206-
if (turbulent) {
207-
solvers[TURB_SOL]->GetNodes()->SetSolution(iPoint, solvers[TURB_SOL]->GetNodes()->GetSolution_time_n(iPoint));
208-
}
209-
if (species) {
210-
solvers[SPECIES_SOL]->GetNodes()->SetSolution(iPoint, solvers[SPECIES_SOL]->GetNodes()->GetSolution_time_n(iPoint));
211-
}
212-
if (heat) {
213-
solvers[HEAT_SOL]->GetNodes()->SetSolution(iPoint, solvers[HEAT_SOL]->GetNodes()->GetSolution_time_n(iPoint));
188+
for (auto iSol = 0ul; iSol < nSolvers; ++iSol) {
189+
auto* s = solver[iZone][iInst][iMesh][solversToProcess[iSol]];
190+
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
191+
s->GetNodes()->SetSolution(iPoint, s->GetNodes()->GetSolution_time_n(iPoint));
214192
}
215193
}
216-
}
217-
if (dual_time_1st) {
218-
/*--- Set Solution at timestep n-1 to the previously loaded solution ---*/
219-
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
220-
auto solvers = solver[iZone][iInst][iMesh];
221-
194+
if (grid_IsMoving) {
222195
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
223-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n(
224-
iPoint, solvers[FLOW_SOL]->GetNodes()->GetSolution_Old(iPoint));
225-
226-
if (grid_IsMoving) {
227-
geometries[iMesh]->nodes->SetCoord_n(iPoint, geometries[iMesh]->nodes->GetCoord_Old(iPoint));
228-
}
229-
if (turbulent) {
230-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n(
231-
iPoint, solvers[TURB_SOL]->GetNodes()->GetSolution_Old(iPoint));
232-
}
233-
if (species) {
234-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n(
235-
iPoint, solvers[SPECIES_SOL]->GetNodes()->GetSolution_Old(iPoint));
236-
}
237-
if (heat) {
238-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n(
239-
iPoint, solvers[HEAT_SOL]->GetNodes()->GetSolution_Old(iPoint));
240-
}
196+
geometries[iMesh]->nodes->SetCoord(iPoint, geometries[iMesh]->nodes->GetCoord_n(iPoint));
241197
}
242198
}
243199
}
244-
if (dual_time_2nd) {
245-
/*--- Set Solution at timestep n-1 to solution at n-2 ---*/
246-
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
247-
auto solvers = solver[iZone][iInst][iMesh];
248200

249-
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
250-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n(
251-
iPoint, solvers[FLOW_SOL]->GetNodes()->GetSolution_time_n1(iPoint));
201+
/*--- Finally, place the loaded solution in the correct place (n or n-1 depending on order). ---*/
252202

253-
if (grid_IsMoving) {
254-
geometries[iMesh]->nodes->SetCoord_n(iPoint, geometries[iMesh]->nodes->GetCoord_n1(iPoint));
255-
}
256-
if (turbulent) {
257-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n(
258-
iPoint, solvers[TURB_SOL]->GetNodes()->GetSolution_time_n1(iPoint));
259-
}
260-
if (species) {
261-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n(
262-
iPoint, solvers[SPECIES_SOL]->GetNodes()->GetSolution_time_n1(iPoint));
263-
}
264-
if (heat) {
265-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n(
266-
iPoint, solvers[HEAT_SOL]->GetNodes()->GetSolution_time_n1(iPoint));
203+
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
204+
for (auto iSol = 0ul; iSol < nSolvers; ++iSol) {
205+
auto* s = solver[iZone][iInst][iMesh][solversToProcess[iSol]];
206+
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
207+
if (dual_time_2nd) {
208+
/*--- If required also move timestep n-1 to timestep n. ---*/
209+
s->GetNodes()->Set_Solution_time_n(iPoint, s->GetNodes()->GetSolution_time_n1(iPoint));
210+
s->GetNodes()->Set_Solution_time_n1(iPoint, s->GetNodes()->GetSolution_Old(iPoint));
211+
} else {
212+
s->GetNodes()->Set_Solution_time_n(iPoint, s->GetNodes()->GetSolution_Old(iPoint));
267213
}
268214
}
269215
}
270-
/*--- Set Solution at timestep n-2 to the previously loaded solution ---*/
271-
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++) {
272-
auto solvers = solver[iZone][iInst][iMesh];
273-
216+
if (grid_IsMoving) {
274217
for (auto iPoint = 0ul; iPoint < geometries[iMesh]->GetnPoint(); iPoint++) {
275-
solvers[FLOW_SOL]->GetNodes()->Set_Solution_time_n1(
276-
iPoint, solvers[FLOW_SOL]->GetNodes()->GetSolution_Old(iPoint));
277-
278-
if (grid_IsMoving) {
218+
if (dual_time_2nd) {
219+
geometries[iMesh]->nodes->SetCoord_n(iPoint, geometries[iMesh]->nodes->GetCoord_n1(iPoint));
279220
geometries[iMesh]->nodes->SetCoord_n1(iPoint, geometries[iMesh]->nodes->GetCoord_Old(iPoint));
280-
}
281-
if (turbulent) {
282-
solvers[TURB_SOL]->GetNodes()->Set_Solution_time_n1(
283-
iPoint, solvers[TURB_SOL]->GetNodes()->GetSolution_Old(iPoint));
284-
}
285-
if (species) {
286-
solvers[SPECIES_SOL]->GetNodes()->Set_Solution_time_n1(
287-
iPoint, solvers[SPECIES_SOL]->GetNodes()->GetSolution_Old(iPoint));
288-
}
289-
if (heat) {
290-
solvers[HEAT_SOL]->GetNodes()->Set_Solution_time_n1(
291-
iPoint, solvers[HEAT_SOL]->GetNodes()->GetSolution_Old(iPoint));
221+
} else {
222+
geometries[iMesh]->nodes->SetCoord_n(iPoint, geometries[iMesh]->nodes->GetCoord_Old(iPoint));
292223
}
293224
}
294225
}
@@ -297,9 +228,10 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
297228
} // else if TimeIter > 0
298229

299230
/*--- Compute & set Grid Velocity via finite differences of the Coordinates. ---*/
300-
if (grid_IsMoving)
231+
if (grid_IsMoving) {
301232
for (auto iMesh = 0u; iMesh <= config[iZone]->GetnMGLevels(); iMesh++)
302233
geometries[iMesh]->SetGridVelocity(config[iZone]);
234+
}
303235

304236
} // if unsteady
305237

@@ -328,7 +260,7 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
328260
if (heat) {
329261
SetSolutionDirect(solvers0, ADJHEAT_SOL, HEAT_SOL, geometries[MESH_0]->GetnPoint());
330262
}
331-
if (config[iZone]->AddRadiation()) {
263+
if (radiation) {
332264
SetSolutionDirect(solvers0, ADJRAD_SOL, RAD_SOL, geometries[MESH_0]->GetnPoint());
333265
}
334266
}
@@ -348,7 +280,7 @@ void CDiscAdjFluidIteration::Preprocess(COutput* output, CIntegration**** integr
348280
solvers0[ADJHEAT_SOL]->Preprocessing(geometries[MESH_0], solvers0, config[iZone],
349281
MESH_0, 0, RUNTIME_ADJHEAT_SYS, false);
350282
}
351-
if (config[iZone]->AddRadiation()) {
283+
if (radiation) {
352284
solvers0[ADJRAD_SOL]->Preprocessing(geometries[MESH_0], solvers0, config[iZone],
353285
MESH_0, 0, RUNTIME_ADJRAD_SYS, false);
354286
}

0 commit comments

Comments
 (0)