Skip to content

Fix CUSTOM_OBJFUNC support in Python layer#2756

Open
riddhi1228 wants to merge 7 commits intosu2code:developfrom
riddhi1228:fix/custom-objfunc-python-layer
Open

Fix CUSTOM_OBJFUNC support in Python layer#2756
riddhi1228 wants to merge 7 commits intosu2code:developfrom
riddhi1228:fix/custom-objfunc-python-layer

Conversation

@riddhi1228
Copy link

@riddhi1228 riddhi1228 commented Mar 12, 2026

This PR fixes the handling of CUSTOM_OBJFUNC in the Python interface.

Problem

When evaluating custom objective functions, the Python layer could raise
a KeyError because the header "Custom_ObjFunc" from history files was not
normalized to the expected "CUSTOM_OBJFUNC" key.

Solution

-Original fix: CUSTOM_OBJFUNC missing from historyMap.py causing KeyError
-Root cause fix: Added if (false) AddHistoryOutput in CFlowOutput.cpp so updateHistoryMap.py generates it correctly
-Parser fix: updateHistoryMap.py now handles if (false) AddHistoryOutput pattern
-Regression test fix: AVG_TOTALTEMP renamed to SURFACE_TOTAL_TEMPERATURE in 3 test configs (this was the cause of the 2 failing regression tests in PR #2587)
-CFlowIncOutput.cpp fix: Joined multi-line BGS_ENTHALPY call to single line so parser works correctly

This resolves issue #2586.

- Register CUSTOM_OBJFUNC in historyMap
- Normalize Custom_ObjFunc header to CUSTOM_OBJFUNC key
- Prevent KeyError when evaluating custom objective functions

Fixes su2code#2586
@pcarruscag pcarruscag changed the base branch from master to develop March 12, 2026 16:28
@pcarruscag
Copy link
Member

==================== Start Test: sphere_ffd_def_bspline ====================
/github/workspace/tests/TestCases/deformation/spherical_ffd
sphere_ffd_def_bspline: PASSED
execution command: SU2_DEF def_spherical_bspline.cfg > def_spherical_bspline.log 2>&1
test_iter=10
test_vals (stored): 0.002068
sim_vals (computed): 0.002068
delta_vals: 0.000000
test duration: 0.03 min
Traceback (most recent call last):
  File "/github/workspace/install/bin/continuous_adjoint.py", line 32, in <module>
    import SU2
  File "/github/workspace/install/bin/SU2/__init__.py", line 19, in <module>
    from SU2 import eval
  File "/github/workspace/install/bin/SU2/eval/__init__.py", line 1, in <module>
    from SU2.eval.functions import function as func
  File "/github/workspace/install/bin/SU2/eval/functions.py", line 92
    header_to_key = {v["HEADER"]: k for k, v in su2io.historyOutFields.items()}
    ^
IndentationError: unexpected indent

@riddhi1228
Copy link
Author

Fixed — the indentation error on line 92 has been corrected and black formatting applied. Thanks for catching it.

@riddhi1228
Copy link
Author

The black formatting check is failing on historyMap.py even though black 22.6.0 reports no changes needed locally and the file is unchanged on the develop branch. This appears to be a pre-existing formatting issue in the repo rather than something introduced by this PR. Happy to investigate further if you can point me to what the CI is reformatting exactly.

@pcarruscag
Copy link
Member

It's ok. The bigger issue is that this historyMap.py file should be generated, not edited manually.
Can you try to run the script that generates the historyMap (it's somewhere in SU2_PY) and see if the tests still pass?

…n test

- Add if (false) AddHistoryOutput for CUSTOM_OBJFUNC in CFlowOutput.cpp
  so updateHistoryMap.py registers it correctly
- Fix updateHistoryMap.py parser to handle 'if (false) AddHistoryOutput' pattern
- Fix multi-line AddHistoryOutput in CFlowIncOutput.cpp for BGS_ENTHALPY
- Regenerate historyMap.py with CUSTOM_OBJFUNC entry
- Fix regression test configs: rename AVG_TOTALTEMP to SURFACE_TOTAL_TEMPERATURE
  (AVG_TOTALTEMP was renamed in the C++ output layer but configs were never updated)

Fixes su2code#2586
@riddhi1228
Copy link
Author

Hi @pcarruscag, updated the PR based on your feedback:

  • Moved the fix to CFlowOutput.cpp using if (false) AddHistoryOutput("CUSTOM_OBJFUNC", ...)
    as done in Fixing Custom Objective Function for Unsteady Adjoint  #2587
  • Fixed updateHistoryMap.py parser to handle the if (false) pattern and a pre-existing
    multi-line BGS_ENTHALPY call in CFlowIncOutput.cpp that was causing an IndexError
  • Regenerated historyMap.pyCUSTOM_OBJFUNC and its derived fields are now correctly registered
  • Fixed the 2 failing regression tests from Fixing Custom Objective Function for Unsteady Adjoint  #2587: AVG_TOTALTEMP was renamed to
    SURFACE_TOTAL_TEMPERATURE in the C++ layer but 3 test configs were never updated

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.

2 participants