Skip to content

Commit 68e8de1

Browse files
authored
Merge pull request #1371 from su2code/feature_directres
Make primal residual for adjoint like normal screen output.
2 parents 5ce84cc + 6f50158 commit 68e8de1

5 files changed

Lines changed: 123 additions & 133 deletions

File tree

SU2_CFD/include/drivers/CDiscAdjSinglezoneDriver.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ class CDiscAdjSinglezoneDriver : public CSinglezoneDriver {
112112
*/
113113
void SetAdj_ObjFunction(void);
114114

115-
/*!
116-
* \brief Print out the direct residuals.
117-
* \param[in] kind_recording - Type of recording (full list in ENUM_RECORDING, option_structure.hpp)
118-
*/
119-
void Print_DirectResidual(RECORDING kind_recording);
120-
121115
/*!
122116
* \brief Record the main computational path.
123117
*/

SU2_CFD/include/drivers/CDriver.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ class CDriver {
313313
*/
314314
virtual void Update() {}
315315

316+
/*!
317+
* \brief Print out the direct residuals.
318+
* \param[in] kind_recording - Type of recording (full list in ENUM_RECORDING, option_structure.hpp)
319+
*/
320+
void Print_DirectResidual(RECORDING kind_recording);
321+
316322
public:
317323

318324
/*!

SU2_CFD/src/drivers/CDiscAdjMultizoneDriver.cpp

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ void CDiscAdjMultizoneDriver::SetRecording(RECORDING kind_recording, Kind_Tape t
665665
config_container, iZone, INST_0);
666666
AD::Push_TapePosition(); /// leave_zone
667667
}
668+
Print_DirectResidual(kind_recording);
668669
}
669670

670671
if (kind_recording != RECORDING::CLEAR_INDICES && driver_config->GetWrt_AD_Statistics()) {
@@ -700,58 +701,6 @@ void CDiscAdjMultizoneDriver::DirectIteration(unsigned short iZone, RECORDING ki
700701
solver_container, numerics_container, config_container,
701702
surface_movement, grid_movement, FFDBox, iZone, INST_0);
702703

703-
/*--- Print residuals in the first iteration ---*/
704-
705-
if (rank == MASTER_NODE && kind_recording == RECORDING::SOLUTION_VARIABLES) {
706-
707-
auto solvers = solver_container[iZone][INST_0][MESH_0];
708-
709-
switch (config_container[iZone]->GetKind_Solver()) {
710-
711-
case DISC_ADJ_EULER: case DISC_ADJ_NAVIER_STOKES:
712-
case DISC_ADJ_INC_EULER: case DISC_ADJ_INC_NAVIER_STOKES:
713-
cout << " Zone " << iZone << " (flow) - log10[U(0)] : "
714-
<< log10(solvers[FLOW_SOL]->GetRes_RMS(0)) << endl;
715-
if (config_container[iZone]->AddRadiation()) {
716-
717-
cout << " Zone " << iZone << " (radiation) - log10[Rad(0)] : "
718-
<< log10(solvers[RAD_SOL]->GetRes_RMS(0)) << endl;
719-
}
720-
break;
721-
722-
case DISC_ADJ_RANS: case DISC_ADJ_INC_RANS:
723-
cout << " Zone " << iZone << " (flow) - log10[U(0)] : "
724-
<< log10(solvers[FLOW_SOL]->GetRes_RMS(0)) << endl;
725-
726-
if (!config_container[iZone]->GetFrozen_Visc_Disc()) {
727-
728-
cout << " Zone " << iZone << " (turbulence) - log10[Turb(0)] : "
729-
<< log10(solvers[TURB_SOL]->GetRes_RMS(0)) << endl;
730-
}
731-
if (config_container[iZone]->AddRadiation()) {
732-
733-
cout << " Zone " << iZone << " (radiation) - log10[Rad(0)] : "
734-
<< log10(solvers[RAD_SOL]->GetRes_RMS(0)) << endl;
735-
}
736-
break;
737-
738-
case DISC_ADJ_HEAT:
739-
cout << " Zone " << iZone << " (heat) - log10[Heat(0)] : "
740-
<< log10(solvers[HEAT_SOL]->GetRes_RMS(0)) << endl;
741-
break;
742-
743-
case DISC_ADJ_FEM:
744-
cout << " Zone " << iZone << " (structure) - ";
745-
if(config_container[iZone]->GetGeometricConditions() == STRUCT_DEFORMATION::LARGE)
746-
cout << "log10[RTOL-A] : " << log10(solvers[FEA_SOL]->GetRes_FEM(1)) << endl;
747-
else
748-
cout << "log10[RMS Ux] : " << log10(solvers[FEA_SOL]->GetRes_RMS(0)) << endl;
749-
break;
750-
751-
default:
752-
break;
753-
}
754-
}
755704
}
756705

757706
void CDiscAdjMultizoneDriver::SetObjFunction(RECORDING kind_recording) {

SU2_CFD/src/drivers/CDiscAdjSinglezoneDriver.cpp

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,24 @@ void CDiscAdjSinglezoneDriver::SetRecording(RECORDING kind_recording){
244244

245245
iteration->SetRecording(solver_container, geometry_container, config_container, ZONE_0, INST_0, kind_recording);
246246

247+
if (rank == MASTER_NODE) {
248+
cout << "\n-------------------------------------------------------------------------\n";
249+
switch(kind_recording) {
250+
case RECORDING::CLEAR_INDICES: cout << "Clearing the computational graph." << endl; break;
251+
case RECORDING::MESH_COORDS: cout << "Storing computational graph wrt MESH COORDINATES." << endl; break;
252+
case RECORDING::SOLUTION_VARIABLES:
253+
cout << "Direct iteration to store the primal computational graph." << endl;
254+
cout << "Computing residuals to check the convergence of the direct problem." << endl; break;
255+
default: break;
256+
}
257+
}
258+
247259
/*---Enable recording and register input of the iteration --- */
248260

249261
if (kind_recording != RECORDING::CLEAR_INDICES){
250262

251263
AD::StartRecording();
252264

253-
if (rank == MASTER_NODE && kind_recording == MainVariables) {
254-
cout << endl << "-------------------------------------------------------------------------" << endl;
255-
cout << "Direct iteration to store the primal computational graph." << endl;
256-
cout << "Compute residuals to check the convergence of the direct problem." << endl;
257-
}
258-
259265
iteration->RegisterInput(solver_container, geometry_container, config_container, ZONE_0, INST_0, kind_recording);
260266
}
261267

@@ -437,75 +443,6 @@ void CDiscAdjSinglezoneDriver::DirectRun(RECORDING kind_recording){
437443

438444
}
439445

440-
void CDiscAdjSinglezoneDriver::Print_DirectResidual(RECORDING kind_recording){
441-
442-
/*--- Print the residuals of the direct iteration that we just recorded ---*/
443-
/*--- This routine should be moved to the output, once the new structure is in place ---*/
444-
if ((rank == MASTER_NODE) && (kind_recording == MainVariables)){
445-
446-
switch (config->GetKind_Solver()) {
447-
448-
case DISC_ADJ_EULER: case DISC_ADJ_NAVIER_STOKES: case DISC_ADJ_RANS:
449-
case DISC_ADJ_INC_EULER: case DISC_ADJ_INC_NAVIER_STOKES: case DISC_ADJ_INC_RANS:
450-
case DISC_ADJ_FEM_EULER : case DISC_ADJ_FEM_NS : case DISC_ADJ_FEM_RANS :
451-
cout << "log10[U(0)]: " << log10(solver[FLOW_SOL]->GetRes_RMS(0))
452-
<< ", log10[U(1)]: " << log10(solver[FLOW_SOL]->GetRes_RMS(1))
453-
<< ", log10[U(2)]: " << log10(solver[FLOW_SOL]->GetRes_RMS(2)) << "." << endl;
454-
cout << "log10[U(3)]: " << log10(solver[FLOW_SOL]->GetRes_RMS(3));
455-
if (geometry->GetnDim() == 3) cout << ", log10[U(4)]: " << log10(solver[FLOW_SOL]->GetRes_RMS(4));
456-
cout << "." << endl;
457-
if ( config->GetKind_Turb_Model() != NONE && !config->GetFrozen_Visc_Disc()) {
458-
cout << "log10[Turb(0)]: " << log10(solver[TURB_SOL]->GetRes_RMS(0));
459-
if (solver[TURB_SOL]->GetnVar() > 1) cout << ", log10[Turb(1)]: " << log10(solver[TURB_SOL]->GetRes_RMS(1));
460-
cout << "." << endl;
461-
}
462-
if (config->GetWeakly_Coupled_Heat()){
463-
cout << "log10[Heat(0)]: " << log10(solver[HEAT_SOL]->GetRes_RMS(0)) << "." << endl;
464-
}
465-
if ( config->AddRadiation()) {
466-
cout <<"log10[E(rad)]: " << log10(solver[RAD_SOL]->GetRes_RMS(0)) << endl;
467-
}
468-
break;
469-
470-
case DISC_ADJ_FEM:
471-
472-
if (config->GetGeometricConditions() == STRUCT_DEFORMATION::LARGE){
473-
cout << "UTOL-A: " << log10(solver[FEA_SOL]->GetRes_FEM(0))
474-
<< ", RTOL-A: " << log10(solver[FEA_SOL]->GetRes_FEM(1))
475-
<< ", ETOL-A: " << log10(solver[FEA_SOL]->GetRes_FEM(2)) << "." << endl;
476-
}
477-
else{
478-
if (geometry->GetnDim() == 2){
479-
cout << "log10[RMS Ux]: " << log10(solver[FEA_SOL]->GetRes_RMS(0))
480-
<< ", log10[RMS Uy]: " << log10(solver[FEA_SOL]->GetRes_RMS(1)) << "." << endl;
481-
}
482-
else{
483-
cout << "log10[RMS Ux]: " << log10(solver[FEA_SOL]->GetRes_RMS(0))
484-
<< ", log10[RMS Uy]: " << log10(solver[FEA_SOL]->GetRes_RMS(1))
485-
<< ", log10[RMS Uz]: " << log10(solver[FEA_SOL]->GetRes_RMS(2))<< "." << endl;
486-
}
487-
}
488-
489-
break;
490-
491-
case DISC_ADJ_HEAT:
492-
cout << "log10[Cons(0)]: " << log10(solver[HEAT_SOL]->GetRes_RMS(0)) << "." << endl;
493-
break;
494-
495-
}
496-
497-
cout << "-------------------------------------------------------------------------" << endl << endl;
498-
}
499-
else if ((rank == MASTER_NODE) && (kind_recording == SecondaryVariables) && (SecondaryVariables != RECORDING::CLEAR_INDICES)){
500-
cout << endl << "Recording the computational graph with respect to the ";
501-
switch (SecondaryVariables){
502-
case RECORDING::MESH_COORDS: cout << "mesh coordinates." << endl; break;
503-
default: cout << "secondary variables." << endl; break;
504-
}
505-
}
506-
507-
}
508-
509446
void CDiscAdjSinglezoneDriver::MainRecording(){
510447

511448
/*--- SetRecording stores the computational graph on one iteration of the direct problem. Calling it with NONE

SU2_CFD/src/drivers/CDriver.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,110 @@ void CDriver::Turbomachinery_Preprocessing(CConfig** config, CGeometry**** geome
26542654

26552655
CDriver::~CDriver(void) {}
26562656

2657+
void CDriver::Print_DirectResidual(RECORDING kind_recording) {
2658+
2659+
if (!(rank == MASTER_NODE && kind_recording == RECORDING::SOLUTION_VARIABLES)) return;
2660+
2661+
const bool multizone = config_container[ZONE_0]->GetMultizone_Problem();
2662+
2663+
/*--- Helper lambda func to return lenghty [iVar][iZone] string. ---*/
2664+
auto iVar_iZone2string = [&](unsigned short ivar, unsigned short izone) {
2665+
if (multizone)
2666+
return "[" + std::to_string(ivar) + "][" + std::to_string(izone) + "]";
2667+
else
2668+
return "[" + std::to_string(ivar) + "]";
2669+
};
2670+
2671+
/*--- Print residuals in the first iteration ---*/
2672+
2673+
const unsigned short fieldWidth = 15;
2674+
PrintingToolbox::CTablePrinter RMSTable(&std::cout);
2675+
2676+
/*--- The CTablePrinter requires two sweeps:
2677+
*--- 0. Add the colum names (addVals=0=false) plus CTablePrinter.PrintHeader()
2678+
*--- 1. Add the RMS-residual values (addVals=1=true) plus CTablePrinter.PrintFooter() ---*/
2679+
for (int addVals = 0; addVals < 2; addVals++) {
2680+
2681+
for (unsigned short iZone = 0; iZone < nZone; iZone++) {
2682+
2683+
auto solvers = solver_container[iZone][INST_0][MESH_0];
2684+
auto configs = config_container[iZone];
2685+
2686+
/*--- Note: the FEM-Flow solvers are availalbe for disc. adjoint runs only for SingleZone. ---*/
2687+
if (configs->GetFluidProblem() || configs->GetFEMSolver()) {
2688+
2689+
for (unsigned short iVar = 0; iVar < solvers[FLOW_SOL]->GetnVar(); iVar++) {
2690+
if (!addVals)
2691+
RMSTable.AddColumn("rms_Flow" + iVar_iZone2string(iVar, iZone), fieldWidth);
2692+
else
2693+
RMSTable << log10(solvers[FLOW_SOL]->GetRes_RMS(iVar));
2694+
}
2695+
2696+
if (configs->GetKind_Turb_Model() != NONE && !configs->GetFrozen_Visc_Disc()) {
2697+
for (unsigned short iVar = 0; iVar < solvers[TURB_SOL]->GetnVar(); iVar++) {
2698+
if (!addVals)
2699+
RMSTable.AddColumn("rms_Turb" + iVar_iZone2string(iVar, iZone), fieldWidth);
2700+
else
2701+
RMSTable << log10(solvers[TURB_SOL]->GetRes_RMS(iVar));
2702+
}
2703+
}
2704+
2705+
if (!multizone && configs->GetWeakly_Coupled_Heat()){
2706+
if (!addVals) RMSTable.AddColumn("rms_Heat" + iVar_iZone2string(0, iZone), fieldWidth);
2707+
else RMSTable << log10(solvers[HEAT_SOL]->GetRes_RMS(0));
2708+
}
2709+
2710+
if (configs->AddRadiation()) {
2711+
if (!addVals) RMSTable.AddColumn("rms_Rad" + iVar_iZone2string(0, iZone), fieldWidth);
2712+
else RMSTable << log10(solvers[RAD_SOL]->GetRes_RMS(0));
2713+
}
2714+
2715+
}
2716+
else if (configs->GetStructuralProblem()) {
2717+
2718+
if (configs->GetGeometricConditions() == STRUCT_DEFORMATION::LARGE){
2719+
if (!addVals) {
2720+
RMSTable.AddColumn("UTOL-A", fieldWidth);
2721+
RMSTable.AddColumn("RTOL-A", fieldWidth);
2722+
RMSTable.AddColumn("ETOL-A", fieldWidth);
2723+
}
2724+
else {
2725+
RMSTable << log10(solvers[FEA_SOL]->GetRes_FEM(0))
2726+
<< log10(solvers[FEA_SOL]->GetRes_FEM(1))
2727+
<< log10(solvers[FEA_SOL]->GetRes_FEM(2));
2728+
}
2729+
}
2730+
else{
2731+
if (!addVals) {
2732+
RMSTable.AddColumn("log10[RMS Ux]", fieldWidth);
2733+
RMSTable.AddColumn("log10[RMS Uy]", fieldWidth);
2734+
if (nDim == 3) RMSTable.AddColumn("log10[RMS Uz]", fieldWidth);
2735+
}
2736+
else {
2737+
RMSTable << log10(solvers[FEA_SOL]->GetRes_FEM(0))
2738+
<< log10(solvers[FEA_SOL]->GetRes_FEM(1));
2739+
if (nDim == 3) RMSTable << log10(solvers[FEA_SOL]->GetRes_FEM(2));
2740+
}
2741+
}
2742+
2743+
}
2744+
else if (configs->GetHeatProblem()) {
2745+
2746+
if (!addVals) RMSTable.AddColumn("rms_Heat" + iVar_iZone2string(0, iZone), fieldWidth);
2747+
else RMSTable << log10(solvers[HEAT_SOL]->GetRes_RMS(0));
2748+
} else {
2749+
SU2_MPI::Error("Invalid KindSolver for CDiscAdj-MultiZone/SingleZone-Driver.", CURRENT_FUNCTION);
2750+
}
2751+
} // loop iZone
2752+
2753+
if (!addVals) RMSTable.PrintHeader();
2754+
else RMSTable.PrintFooter();
2755+
2756+
} // for addVals
2757+
2758+
cout << "\n-------------------------------------------------------------------------\n" << endl;
2759+
2760+
}
26572761

26582762
CFluidDriver::CFluidDriver(char* confFile, unsigned short val_nZone, SU2_Comm MPICommunicator) : CDriver(confFile, val_nZone, MPICommunicator, false) {
26592763
Max_Iter = config_container[ZONE_0]->GetnInner_Iter();

0 commit comments

Comments
 (0)