Skip to content

fix: decide PsResid's Ps re-seed from the model's vectors - #126

Open
Jhawk414 wants to merge 5 commits into
OpenMDAO:masterfrom
Jhawk414:fix/ps-resid-stateless-guess
Open

Jhawk414 wants to merge 5 commits into
OpenMDAO:masterfrom
Jhawk414:fix/ps-resid-stateless-guess

Conversation

@Jhawk414

@Jhawk414 Jhawk414 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

PsResid.guess_nonlinear decided whether to re-seed Ps by comparing the new guess with self._ps_guess_cache, a Python attribute outside OpenMDAO's vectors. Restoring a model's state (load_case or a vector copy) therefore didn't determine the next solve (#125). This PR removes the attribute and makes the decision from the vectors:

  • area mode: re-seed Ps when |resid['Ps']| >= 1e-2, so a nearly converged Ps is kept as the starting point, which is what the cache was for. This is the same gate and threshold OpenMDAO's guess_nonlinear docs example uses. OpenMDAO recomputes the residual with apply_nonlinear right before guess_nonlinear, from whatever inputs are available at that point. On a cold start those can still be initial thermo values, so the residual can look small. Ps is therefore also seeded whenever it still holds its initial value.
  • MN mode: seed Ps only while it still holds its initial value. That's the cold-start condition the old first-call check stood in for. A residual gate here was tried and rejected: it fails TestStaticJanaf.test_case_MN case 5 (MN 2.0), because re-seeding Ps while T is still from the previous case sends that Newton astray. (Stock fails that case too when it's solved cold.)

Of the options in #125, this is (b). Before choosing it I tried (a) "always re-seed in area mode", which is what an opt-out flag would turn on. It restores history independence within one model, but not after loading a case into a fresh model, because of the MN first-call seed. It also costs 20–45% more inner Newton iterations. Option (c), a reset hook, only helps callers who know to use it.

Related Issues

Backwards incompatibilities

New Dependencies

None

Proof of behavior

Restored state reproduces the solve. For each example's OD point: solve condition A and record the case, then compare three re-solves of A. The baseline is load_case(A) with nothing run in between. Same model solves another condition B first, then load_case(A). Fresh model builds a new Problem and calls load_case(A).

Example Same model, before → after Fresh model, before → after
mixedflow_turbofan.py 1213 outputs differ → identical 3355 differ → identical
simple_turbojet.py 51 differ → identical 898 differ → identical

Convergence on the examples' own off-design sweeps (same sequence before and after; "inner" counts every Thermo Newton iteration):

Sweep Failed solves, before → after Outer Newton iterations Inner Newton iterations Max relative change in Fn / TSFC where both converge
high_bypass_turbofan.py __main__ envelope (21 conditions × 6 throttle steps × 2 points) 6 → 0 1297 → 1003 483k → 281k 5.2e-9 / 6.3e-9
mixedflow_turbofan.py OD (5 altitude/Mach × 5 T4) 0 → 0 93 → 93 51.2k → 35.9k 9.3e-8 / –
simple_turbojet.py OD1 (3 altitude/Mach × 5 Fn) 0 → 0 76 → 76 2176 → 1610 0 / –

The six stock failures are OD_full_pwr at M0.55 / 35 kft. There, inlet.out_stat.ps_resid.Ps sits at its lower bound (1e-4 bar) with a residual of 3.3e5 on every re-solve, because the guess at that condition never changes and so the cache never lets it re-seed. With the residual gate it re-seeds and converges.

Tests:

  • test_statics_restored_state restores the vectors into the same model and into a freshly built one. It fails on master (22 and 42 of 111 outputs differ) and passes with this change.
  • test_static_area_cold_start covers an area-mode static whose cold-start residual is small. Without the cold-start seed, Newton stalls at the Ps lower bound.
  • With the change, testflo pycycle example_cycles passes 69/69, and testflo -b example_cycles (the golden-value benchmarks in example_cycles/tests/ and example_cycles/N+3ref/) passes 13 with 1 skipped, the same as on master.

Environment: OpenMDAO 3.39.0, NumPy 2.0.2, SciPy 1.13.1, Python 3.9.6, macOS.

guess_nonlinear compared each new guess with _ps_guess_cache, a Python
attribute, so restoring a model's vectors (load_case or a vector copy)
did not determine the next solve (OpenMDAO#125).

Area mode now re-seeds Ps when |resid['Ps']| >= 1e-2, which OpenMDAO
recomputes just before guess_nonlinear. MN mode seeds only while Ps
still holds its initial value: the stateless form of the old
first-call check. A residual gate in MN mode was rejected because
re-seeding Ps with a stale T fails the Mach 2 CEA static test.

Converged results move only at solver tolerance. In the high-bypass
turbofan example's envelope sweep, six OD_full_pwr solves that stock
left stuck with the inlet exit Ps at its lower bound now converge.
@Jhawk414

Copy link
Copy Markdown
Contributor Author

Code Review

Changes requested. The area-mode gate introduces a cold-start convergence regression.

Severity Findings
Blocking • [P1] Keep the pressure seed on a cold start in pycycle/thermo/static_ps_resid.py:85. A tabulated static_A solve at 10 bar total pressure, 1000 K total temperature, 1 kg/s, area 0.00082 m², and guess:MN=0.7 converges to Mach 0.815 on master. The PR sees an initial enthalpy residual of −8.1e-5 while Ps is still at 0.001 bar, skips the seed, and Newton stalls at the lower pressure bound. Seed at the default pressure and add this case as a regression test.
Should Fix • [P2] Add a fresh-Problem restore case in pycycle/thermo/test/test_thermo_total_static_tabulated.py:248. The current test reuses a solved model, so it misses the MN-mode first-call behavior that can overwrite a restored pressure.
Nits None.

On 255e64ae with OpenMDAO 3.39.0, all 68 tests and nine example benchmarks passed locally, and both cycle examples reproduced their outputs bit for bit after same-model and fresh-model restores.
Seeding at the default pressure restored convergence in the failing case and preserves the vector-based fix for #125.

The area-mode residual gate skipped the seed whenever |resid['Ps']| was
below 1e-2, including on a cold start. There, Ps is still at its
initial 0.001 bar but the enthalpy residual can happen to be small
(-8.1e-5 for a tabulated static_A at 10 bar, 1000 K, 1 kg/s,
0.00082 m^2), and Newton then stalls at the lower pressure bound.

Area mode now seeds on the same cold-start check MN mode uses, and
still uses the residual gate after that.
The restored-state test only reused the solved model, so it could not
catch an MN-mode seed that overwrites a restored Ps on a model's first
solve. Move test_statics' setup into _setup_statics() and also restore
the saved vectors into a new problem. That check alone fails on master
(42 of 111 outputs differ).
@Jhawk414

Copy link
Copy Markdown
Contributor Author

Code review response

Agreed on both findings. The area-mode gate needed the same cold-start seed as MN mode, and the restore test now also restores into a freshly built Problem.

Finding Response Commit
[P1] Keep the pressure seed on a cold start (static_ps_resid.py:85) Reproduced: on 255e64a your tabulated static_A case (10 bar, 1000 K, 1 kg/s, 0.00082 m², guess:MN=0.7) stalled at the Ps lower bound with MN 21113. Area mode now seeds whenever Ps still holds its initial value, the same check MN mode uses, and keeps the residual gate after that. The case converges to MN 0.81528, matching master. b86ad7b
[P1] Add the cold-start case as a regression test Added test_static_area_cold_start. It fails on 255e64a and passes now. 113e164
[P2] Fresh-Problem restore case Moved test_statics' setup into _setup_statics(), and test_statics_restored_state now also restores the saved vectors into a new problem. That check on its own fails on master (42 of 111 outputs differ, from the MN first-call seed) and passes now. e5afdad

Kept as-is: nothing from this review.

  • The cold-start change doesn't move the example-cycle numbers in the PR description. Re-running the sweeps gave the same iteration counts: turbojet and MFTF with identical restores in both models, and the HBTF envelope with 0 of 252 solves failing and the same 1003 outer and 281k inner Newton iterations. I updated the description's test list for the two new checks.

Tests: testflo pycycle example_cycles 69 passed, 0 failed; example_cycles benchmarks 13 passed, 1 skipped (same as master).

@Jhawk414

Copy link
Copy Markdown
Contributor Author

Code re-review at e5afdad

Both findings are resolved on e5afdad. The cold-start regression test confirms that area mode seeds at the default pressure, and the fresh-Problem test catches the old Mach-mode overwrite. My local run passed 69 tests and 13 benchmarks, with one benchmark skipped.

Two small PR description tweaks:

  • Say OpenMDAO recomputes the residual from the available inputs, which can still hold initial thermo values on a cold start.
  • List testflo -b example_cycles as the benchmark command, since the count includes N+3ref.

@Jhawk414
Jhawk414 marked this pull request as ready for review November 27, 2026 19:45
Jhawk414 added a commit to Jhawk414/F404 that referenced this pull request Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PsResid keeps its guess cache outside OpenMDAO's vectors, so load_case doesn't reproduce off-design solves

1 participant