Solves the momentum equations using dual-time Picard iteration with Jameson RK smoothing.
805{
806 (void)ibm;
807 (void)fsi;
808
811
812
813 const PetscInt ti = simCtx->
step;
814 const PetscReal dt = simCtx->
dt;
815
816
818 const PetscReal alfa[] = {0.25, 1.0/3.0, 0.5, 1.0};
819
820
821 PetscBool force_restart = PETSC_FALSE;
822
823
825 const PetscReal tol_abs_delta = simCtx->
mom_atol;
826 const PetscReal tol_rtol_delta = simCtx->
mom_rtol;
827
828
829 PetscErrorCode ierr;
830 PetscMPIInt rank;
831 PetscInt istage, pseudo_iter;
832 PetscInt accepted_iter = 0, rejected_iter = 0, recovery_streak = 0;
833 PetscReal ts, te, cput;
834
835
836 PetscReal global_norm_delta = 10.0;
837 PetscReal global_rel_delta = 1.0;
838 PetscReal global_norm_resid = 1.0;
839 PetscReal global_rel_resid = 1.0;
840 PetscReal smoothed_trial_ratio = 1.0;
841
842
843
844
845
846
847
848
849 PetscReal lambda_max = 0.0;
850
851
852
853
854
856
857
858
859 PetscReal resid_ref = 0.0;
860 PetscReal dtau_min, dtau_max;
861
862
863 PetscReal *delta_sol_norm_init, *delta_sol_norm_prev, *delta_sol_norm_curr, *delta_sol_rel_curr;
864 PetscReal *resid_norm_init, *resid_norm_prev, *resid_norm_curr, *resid_rel_curr;
865 PetscReal *pseudo_dtau;
866 PetscReal *trial_ratio_log;
867 PetscReal last_accepted_resid;
868
869 PetscFunctionBeginUser;
872 LOG_ALLOW(
GLOBAL,
LOG_INFO,
"Executing Dual-Time Momentum Solver for %" PetscInt_FMT
" block(s).\n", block_number);
873
874
875 ierr = PetscMalloc2(block_number, &delta_sol_norm_init, block_number, &delta_sol_norm_prev); CHKERRQ(ierr);
876 ierr = PetscMalloc2(block_number, &delta_sol_norm_curr, block_number, &delta_sol_rel_curr); CHKERRQ(ierr);
877 ierr = PetscMalloc2(block_number, &resid_norm_init, block_number, &resid_norm_prev); CHKERRQ(ierr);
878 ierr = PetscMalloc2(block_number, &resid_norm_curr, block_number, &pseudo_dtau); CHKERRQ(ierr);
879 ierr = PetscMalloc1(block_number, &resid_rel_curr); CHKERRQ(ierr);
880 ierr = PetscMalloc1(block_number, &trial_ratio_log); CHKERRQ(ierr);
881 last_accepted_resid = 0.0;
882
883 ierr = PetscTime(&ts); CHKERRQ(ierr);
884
885
886
887 if (block_number > 1) {
888
889 }
890
891 for (PetscInt bi = 0; bi < block_number; bi++) {
893
894
895
896
897
898
899 if (!user[bi].Rhs) { ierr = VecDuplicate(user[bi].Ucont, &user[bi].Rhs); CHKERRQ(ierr); }
900 if (!user[bi].pRhs) { ierr = VecDuplicate(user[bi].Ucont, &user[bi].pRhs); CHKERRQ(ierr); }
901 if (!user[bi].dUcont) { ierr = VecDuplicate(user[bi].Ucont, &user[bi].dUcont); CHKERRQ(ierr); }
902 if (!user[bi].pUcont) { ierr = VecDuplicate(user[bi].Ucont, &user[bi].pUcont); CHKERRQ(ierr); }
903 ierr = VecZeroEntries(user[bi].Rhs); CHKERRQ(ierr);
904
905
906 ierr = VecCopy(user[bi].Ucont, user[bi].pUcont); CHKERRQ(ierr);
907
908
910
911
912 ierr = VecCopy(user[bi].Rhs, user[bi].pRhs); CHKERRQ(ierr);
913
914
915 ierr = VecNorm(user[bi].Rhs, NORM_INFINITY, &resid_norm_init[bi]); CHKERRQ(ierr);
916
917
918
919 {
920 PetscReal ucont_inf;
921 ierr = VecNorm(user[bi].Ucont, NORM_INFINITY, &ucont_inf); CHKERRQ(ierr);
923 }
924
925
926 resid_norm_prev[bi] = resid_norm_init[bi];
927 delta_sol_norm_prev[bi] = 1000.0;
928
929
930 LOG_ALLOW(
GLOBAL,
LOG_INFO,
" Block %" PetscInt_FMT
" | Max RHS = %.6f | initial pseudo-CFL = %.4f .\n", bi, resid_norm_init[bi], cfl);
931 }
932
933
934
935
936
937
938
939
940
941
945
946
947 for (PetscInt bi = 0; bi < block_number; bi++) {
948 pseudo_dtau[bi] = cfl / lambda_max;
949 }
950
951
952
953
954
955 {
956 PetscBool shadow = PETSC_FALSE;
957 ierr = PetscOptionsGetBool(NULL, NULL, "-mom_stability_shadow", &shadow, NULL); CHKERRQ(ierr);
958 if (shadow) {
963
964
966 "Momentum scale [shadow]: legacy_dtau=%.4e new_dtau=%.4e ratio=%.4f limiter=%s | "
967 "lambda_legacy=%.4e lambda_new=%.4e (B=%.4e C=%.4e D=%.4e) | "
968 "lt=%.4e lc=%.4e lv=%.4e | cell=(%d,%d,%d) blk=%d class=%d onesided=%d\n",
969 cfl / lambda_max, cfl / rep.
lambda, rep.
lambda / lambda_max, lim,
973 }
974 }
975
977 "Dual-time solver: lambda_max=%.4e [1/s] resid_ref=%.4e dtau_init=%.4e dtau range [%.4e, %.4e] "
978 "CFL range [%.4f, %.4f] rejection_threshold=%.3f (EMA alpha=%.2f) "
979 "growth=%.3f reduction=%.3f max_accepted=%" PetscInt_FMT ".\n",
980 lambda_max, resid_ref, cfl / lambda_max, dtau_min, dtau_max,
985 max_pseudo_steps);
986
987
988 pseudo_iter = 0;
989 PetscBool residual_convergence_enabled =
991 PetscBool converged = PETSC_FALSE;
992 PetscBool last_trial_nonfinite = PETSC_FALSE;
993
994
995 const PetscInt max_total_attempts = max_pseudo_steps * 3;
996 while (!converged && accepted_iter < max_pseudo_steps && pseudo_iter < max_total_attempts)
997 {
998 pseudo_iter++;
999 force_restart = PETSC_FALSE;
1000
1001
1002 for (PetscInt bi = 0; bi < block_number; bi++) {
1003 ierr = VecCopy(user[bi].pUcont, user[bi].Ucont); CHKERRQ(ierr);
1004 ierr = VecCopy(user[bi].pRhs, user[bi].Rhs); CHKERRQ(ierr);
1008 }
1009
1010 for (PetscInt bi = 0; bi < block_number; bi++) {
1011
1012
1013 for (istage = 0; istage < 4; istage++) {
1014
1015 LOG_ALLOW(
GLOBAL,
LOG_TRACE,
" Pseudo-Iter: %" PetscInt_FMT
" | RK-Stage: %" PetscInt_FMT
"\n", pseudo_iter, istage);
1016
1017
1018
1019
1020 ierr = VecWAXPY(user[bi].Ucont,
1021 pseudo_dtau[bi] * alfa[istage],
1022 user[bi].Rhs,
1023 user[bi].pUcont); CHKERRQ(ierr);
1024
1025
1028
1030
1031
1033
1034 }
1035
1036
1037
1038
1039
1040
1041 ierr = VecWAXPY(user[bi].dUcont, -1.0, user[bi].pUcont, user[bi].Ucont); CHKERRQ(ierr);
1042
1043
1044 ierr = VecNorm(user[bi].dUcont, NORM_INFINITY, &delta_sol_norm_curr[bi]); CHKERRQ(ierr);
1045 ierr = VecNorm(user[bi].Rhs, NORM_INFINITY, &resid_norm_curr[bi]); CHKERRQ(ierr);
1046
1047
1048 if (pseudo_iter == 1) {
1049 delta_sol_norm_init[bi] = delta_sol_norm_curr[bi];
1050 delta_sol_rel_curr[bi] = 1.0;
1051 resid_rel_curr[bi] = 1.0;
1052
1053 } else {
1054 if (delta_sol_norm_init[bi] > 1.0e-10)
1055 delta_sol_rel_curr[bi] = delta_sol_norm_curr[bi] / delta_sol_norm_init[bi];
1056 else
1057 delta_sol_rel_curr[bi] = 0.0;
1058
1059 if(resid_norm_init[bi] > 1.0e-10)
1060 resid_rel_curr[bi] = resid_norm_curr[bi] / resid_norm_init[bi];
1061 else
1062 resid_rel_curr[bi] = 0.0;
1063 }
1064
1065
1066 {
1067 const PetscReal resid_floor_log = 1.0e-30;
1068 if (resid_norm_prev[bi] > resid_floor_log)
1069 trial_ratio_log[bi] = resid_norm_curr[bi] / resid_norm_prev[bi];
1070 else if (resid_norm_curr[bi] <= resid_floor_log)
1071 trial_ratio_log[bi] = 0.0;
1072 else
1073 trial_ratio_log[bi] = PETSC_MAX_REAL;
1074 }
1075 }
1076
1077
1078 global_norm_delta = -1.0e20;
1079 global_rel_delta = -1.0e20;
1080 global_norm_resid = -1.0e20;
1081 global_rel_resid = -1.0e20;
1082
1083 for (PetscInt bi = 0; bi < block_number; bi++) {
1084 global_norm_delta = PetscMax(delta_sol_norm_curr[bi], global_norm_delta);
1085 global_rel_delta = PetscMax(delta_sol_rel_curr[bi], global_rel_delta);
1086 global_norm_resid = PetscMax(resid_norm_curr[bi], global_norm_resid);
1087 global_rel_resid = PetscMax(resid_rel_curr[bi], global_rel_resid);
1088 }
1089 ierr = PetscTime(&te); CHKERRQ(ierr);
1090 cput = te - ts;
1091 LOG_ALLOW(
GLOBAL,
LOG_INFO,
" Pseudo-Iter(k) %" PetscInt_FMT
": |dUk|=%e, |dUk|/|dU0| = %e, |Rk|/|R0| = %e, CPU=%.2fs\n",
1092 pseudo_iter, global_norm_delta, global_rel_delta, global_rel_resid, cput);
1093
1094
1095 const PetscReal resid_floor = 1.0e-30;
1096 PetscReal global_trial_ratio = 0.0;
1097 PetscBool global_nonfinite = PETSC_FALSE;
1098 for (PetscInt bi = 0; bi < block_number; bi++) {
1099 PetscReal ratio;
1100 if (resid_norm_prev[bi] > resid_floor) {
1101 ratio = resid_norm_curr[bi] / resid_norm_prev[bi];
1102 } else if (resid_norm_curr[bi] <= resid_floor) {
1103 ratio = 0.0;
1104 } else {
1105 ratio = PETSC_MAX_REAL;
1106 }
1107 global_trial_ratio = PetscMax(global_trial_ratio, ratio);
1108 global_nonfinite = (PetscBool)(global_nonfinite ||
1109 PetscIsInfOrNanReal(delta_sol_norm_curr[bi]) ||
1110 PetscIsInfOrNanReal(resid_norm_curr[bi]) ||
1111 PetscIsInfOrNanReal(ratio));
1112 }
1113
1114 PetscReal reduced_trial_ratio;
1115 PetscMPIInt local_nonfinite = global_nonfinite ? 1 : 0;
1116 PetscMPIInt reduced_nonfinite = 0;
1117 ierr = MPI_Allreduce(&global_trial_ratio, &reduced_trial_ratio, 1, MPIU_REAL, MPI_MAX, PETSC_COMM_WORLD); CHKERRQ(ierr);
1118 ierr = MPI_Allreduce(&local_nonfinite, &reduced_nonfinite, 1, MPI_INT, MPI_LOR, PETSC_COMM_WORLD); CHKERRQ(ierr);
1119 global_trial_ratio = reduced_trial_ratio;
1120 global_nonfinite = reduced_nonfinite ? PETSC_TRUE : PETSC_FALSE;
1121 last_trial_nonfinite = global_nonfinite;
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135 PetscReal trial_smoothed = smoothed_trial_ratio;
1136 if (!global_nonfinite) {
1139 }
1141 " [k=%" PetscInt_FMT "] raw_ratio=%.4e smoothed_ratio=%.4e (threshold=%.3f) | "
1142 "|R_prev|=%.6e | |R_curr|=%.6e | CFL=%.6f\n",
1143 pseudo_iter, global_trial_ratio, trial_smoothed,
1145 resid_norm_prev[0], resid_norm_curr[0], pseudo_dtau[0]);
1146
1148
1149 force_restart = global_nonfinite;
1150 } else {
1151 force_restart = (PetscBool)(global_nonfinite ||
1153 }
1154
1155 if (force_restart) {
1156
1157
1158
1159 PetscReal old_dtau = pseudo_dtau[0];
1161
1162
1165 rejected_iter++;
1166 recovery_streak = 0;
1167 for (PetscInt bi = 0; bi < block_number; bi++) {
1168 ierr = VecCopy(user[bi].pUcont, user[bi].Ucont); CHKERRQ(ierr);
1169 ierr = VecCopy(user[bi].pRhs, user[bi].Rhs); CHKERRQ(ierr);
1173 pseudo_dtau[bi] = next_dtau;
1174 }
1175
1177 " Trial %" PetscInt_FMT " REJECTED (raw_ratio=%.4e, smoothed=%.4e, nonfinite=%d); "
1178 "dtau %.4e -> %.4e (cfl_eff %.4f -> %.4f)%s\n",
1179 pseudo_iter, global_trial_ratio, trial_smoothed, (int)global_nonfinite,
1180 old_dtau, next_dtau, old_dtau * lambda_max, next_dtau * lambda_max,
1181 (old_dtau == next_dtau) ? " [AT FLOOR — no dtau reduction]" : "");
1182
1183 if (!rank) {
1185 delta_sol_norm_curr, delta_sol_rel_curr, resid_norm_curr, resid_rel_curr,
1186 trial_ratio_log, trial_smoothed, "rejected"); CHKERRQ(ierr);
1187 }
1188 if (old_dtau <= dtau_min) {
1189 if (global_nonfinite) {
1190 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED,
1191 "Momentum solver produced a non-finite trial at minimum pseudo-CFL.");
1192 }
1193
1194
1196 " Trial %" PetscInt_FMT " REJECTED (ratio=%.4e) at minimum dtau (%.4e, cfl_eff=%.4f) "
1197 "with no further reduction possible; breaking retry loop.\n",
1198 pseudo_iter, global_trial_ratio, old_dtau, old_dtau * lambda_max);
1199 break;
1200 }
1201 continue;
1202 }
1203
1204 accepted_iter++;
1205 smoothed_trial_ratio = trial_smoothed;
1206 last_trial_nonfinite = PETSC_FALSE;
1207 last_accepted_resid = resid_norm_curr[0];
1208 for (PetscInt bi = 0; bi < block_number; bi++) {
1209 ierr = VecCopy(user[bi].Ucont, user[bi].pUcont); CHKERRQ(ierr);
1210 ierr = VecCopy(user[bi].Rhs, user[bi].pRhs); CHKERRQ(ierr);
1211 resid_norm_prev[bi] = resid_norm_curr[bi];
1212 delta_sol_norm_prev[bi] = delta_sol_norm_curr[bi];
1213 }
1214
1215
1216
1217
1218 PetscReal old_dtau = pseudo_dtau[0];
1219 PetscReal next_dtau = old_dtau;
1220 if (global_trial_ratio < 0.90) {
1221
1223 recovery_streak = 0;
1224 } else if (global_trial_ratio <= 1.0) {
1225
1226 recovery_streak++;
1227 if (recovery_streak >= 3) {
1229 recovery_streak = 0;
1230 }
1231 } else {
1232
1234 recovery_streak = 0;
1235 }
1236
1237
1238
1240 next_dtau = PetscMin(next_dtau, dtau_max);
1241 next_dtau = PetscMin(next_dtau, cfl_cap / lambda_max);
1242 next_dtau = PetscMax(next_dtau, dtau_min);
1243 for (PetscInt bi = 0; bi < block_number; bi++) pseudo_dtau[bi] = next_dtau;
1244
1246 " Trial %" PetscInt_FMT " ACCEPTED (raw_ratio=%.4e, smoothed=%.4e); |dU|=%.6e | "
1247 "dtau %.4e -> %.4e (cfl_eff %.4f -> %.4f)\n",
1248 pseudo_iter, global_trial_ratio, trial_smoothed, global_norm_delta,
1249 old_dtau, next_dtau, old_dtau * lambda_max, next_dtau * lambda_max);
1250
1251
1252 if (!rank) {
1254 delta_sol_norm_curr, delta_sol_rel_curr, resid_norm_curr, resid_rel_curr,
1255 trial_ratio_log, trial_smoothed, "accepted"); CHKERRQ(ierr);
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 if (residual_convergence_enabled) {
1292 const PetscBool residual_abs_pass = (PetscBool)(
1294 global_norm_resid <= simCtx->mom_resid_atol * resid_ref);
1295 const PetscBool residual_rel_pass = (PetscBool)(
1296 simCtx->
mom_resid_rtol > 0.0 && global_rel_resid <= simCtx->mom_resid_rtol);
1297 const PetscBool update_pass = (PetscBool)(
1298 tol_rtol_delta <= 0.0 || global_rel_delta <= tol_rtol_delta);
1299 converged = (PetscBool)(residual_abs_pass || (residual_rel_pass && update_pass));
1300 } else {
1301 converged = (PetscBool)(global_norm_delta <= tol_abs_delta &&
1302 global_rel_delta <= tol_rtol_delta);
1303 }
1304
1305 if (block_number > 1) {
1306
1307 }
1308 }
1309
1310 if (last_trial_nonfinite) {
1311 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED,
1312 "Momentum solver exhausted its attempt limit while recovering from a non-finite trial.");
1313 }
1314
1315
1316
1317
1318
1319 PetscReal next_dtau_start = pseudo_dtau[0];
1320 PetscReal next_cfl_warmstart = next_dtau_start * lambda_max;
1321
1322 if (!rank) {
1324 " Step %" PetscInt_FMT " finished: dtau=%.4e cfl_eff=%.4f lambda_max=%.4e [1/s]. "
1325 "Next step warm-starts at cfl=%.4f.\n",
1326 simCtx->
step, next_dtau_start, next_cfl_warmstart, lambda_max, next_cfl_warmstart);
1327 }
1328
1329
1332
1333
1334
1335
1336 if (accepted_iter == 0) {
1337 for (PetscInt bi = 0; bi < block_number; bi++) {
1338 ierr = VecCopy(user[bi].pUcont, user[bi].Ucont); CHKERRQ(ierr);
1339 ierr = VecCopy(user[bi].pRhs, user[bi].Rhs); CHKERRQ(ierr);
1340 }
1341 }
1342 for (PetscInt bi = 0; bi < block_number; bi++) {
1346 }
1347
1349 if (!converged) {
1350 PetscPrintf(PETSC_COMM_WORLD,
1351 "[WARNING] Momentum solver step %d: reached %" PetscInt_FMT " total attempts (%" PetscInt_FMT " accepted, %" PetscInt_FMT " rejected) "
1352 "without convergence; continuing from last accepted finite state.\n",
1353 (int)ti, pseudo_iter, accepted_iter, rejected_iter);
1354 }
1355 if (accepted_iter == 0) {
1356 PetscPrintf(PETSC_COMM_WORLD,
1357 "[WARNING] Momentum solver step %d: no pseudo-time trials were accepted; "
1358 "retaining physical-step entry state.\n", (int)ti);
1359 }
1361 "Momentum solver finished: %" PetscInt_FMT " attempts (%" PetscInt_FMT " accepted, %" PetscInt_FMT " rejected) of %" PetscInt_FMT " max accepted / %" PetscInt_FMT " hard cap, "
1362 "converged=%s, last accepted |R|=%.6e, last accepted |dU|=%.6e, "
1363 "next_dtau=%.4e (cfl=%.4f).\n",
1364 pseudo_iter, accepted_iter, rejected_iter, max_pseudo_steps, max_total_attempts,
1365 converged ? "yes" : "no",
1366 (accepted_iter > 0) ? last_accepted_resid : resid_norm_init[0],
1367 (accepted_iter > 0) ? delta_sol_norm_prev[0] : 0.0,
1368 next_dtau_start, next_cfl_warmstart);
1369
1370
1371
1372 ierr = PetscFree2(delta_sol_norm_init, delta_sol_norm_prev);CHKERRQ(ierr);
1373 ierr = PetscFree2(delta_sol_norm_curr, delta_sol_rel_curr);CHKERRQ(ierr);
1374 ierr = PetscFree2(resid_norm_init, resid_norm_prev);CHKERRQ(ierr);
1375 ierr = PetscFree2(resid_norm_curr, pseudo_dtau); CHKERRQ(ierr);
1376 ierr = PetscFree(resid_rel_curr); CHKERRQ(ierr);
1377 ierr = PetscFree(trial_ratio_log); CHKERRQ(ierr);
1378
1380 PetscFunctionReturn(0);
1381}
@ LOG_TRACE
Very fine-grained tracing information for in-depth debugging.
@ LOG_WARNING
Non-critical issues that warrant attention.
static PetscErrorCode AppendPicardHistoryRows(SimCtx *simCtx, PetscInt block_number, PetscInt pseudo_iter, PetscReal dtau, PetscReal dtau_after, PetscReal lambda_max, const PetscReal *delta, const PetscReal *delta_rel, const PetscReal *resid, const PetscReal *resid_rel, const PetscReal *trial_ratio, PetscReal smoothed_ratio, const char *status)
Appends one pseudo-iteration's row per block to the Picard momentum history CSVs.
#define MOM_CFL_CAP_SAFETY
static PetscErrorCode ComputeGlobalSpectralRadiusEstimate(UserCtx *user, PetscInt block_number, PetscReal dt, PetscReal *lambda_max_out)
Compute a conservative global pseudo-time spectral radius estimate.
PetscErrorCode ComputeTotalResidual(UserCtx *user)
Shared implementation of ComputeTotalResidual().
PetscErrorCode ComputeMomentumStabilityEstimate(UserCtx *user, PetscInt block_number, PetscReal dt, MomStabCandidate candidate, MomStabilityReport *rep)
Practical conservative momentum pseudo-time stability estimate (shadow).
#define MOM_CFL_CAP_RELAX
Diagnostic report produced by ComputeMomentumStabilityEstimate().
PetscReal mom_last_lambda_max
PetscReal pseudo_cfl_reduction_factor
PetscBool mom_last_converged
PetscBool no_pseudo_cfl_backtrack
PetscReal mom_dt_jameson_residual_norm_noise_allowance_factor
PetscReal pseudo_cfl_growth_factor
PetscInt mom_max_pseudo_steps
PetscReal mom_ratio_ema_alpha