Repository navigation
Fix DV scaling of gradients in the python optimizer - #3056
SahilKumar965 wants to merge 2 commits into
Conversation
Config.unpack_dvs sets the design variable values to SCALE * x, where x is the optimizer variable, so the optimizer needs dJ/dx. obj_df, con_dceq and con_dcieq divided the gradients by SCALE instead. - Adjoint gradients (and the SU2_GEO gradients of geometric functions) are taken with respect to the design variable value, so they are now multiplied by SCALE. - FINDIFF perturbs x itself and already returns dJ/dx, so no DV scale is applied. With SCALE = 0.5 on the inviscid NACA0012, obj_df was 4x (discrete adjoint) and 2x (FINDIFF) a central difference of obj_f; both now agree. Gradients with SCALE = 1 are unchanged. Fixes su2code#3054
| the optimizer variable x, where the design variable value is dv_scl * x | ||
| (see Config.unpack_dvs). Finite differences perturb x itself, adjoint and | ||
| geometry gradients are taken with respect to the design variable value.""" | ||
| return 1.0 if grad_method == "FINDIFF" else dv_scl |
There was a problem hiding this comment.
FINDIFF at a nonzero design (your follow-up issue): please do open it. One extra piece for it: in findiff() (gradients.py), dvs_base comes from DV_VALUE_NEW, which is already physical (scaled), and unpack_dvs(this_dvs, dvs_base) multiplies it by the scale again. So with SCALE != 1 there is a second error on top of the one you saw at scale 1.
There was a problem hiding this comment.
Thanks for pointing this out. I opened a new PR for it: #3067.
In that PR I removed dvs_base completely. The mesh is already moved to the base design, so now each finite difference run only adds its own small step. This also fixes the double scaling you mentioned.
I tested it on NACA0012 and compared it with a central difference:
- SCALE = 2: ratio 1.0048 (before the fix: 25.4)
- SCALE = 0.5: ratio 1.0012
The small leftover difference is normal for a one-sided step.
| """Factor that turns a gradient from su2grad into the gradient with respect to | ||
| the optimizer variable x, where the design variable value is dv_scl * x | ||
| (see Config.unpack_dvs). Finite differences perturb x itself, adjoint and | ||
| geometry gradients are taken with respect to the design variable value.""" |
There was a problem hiding this comment.
Optional one-liner: in su2grad() (gradients.py), the DIRECTDIFF branch assigns grad = directdiff(...), but the next line uses grads, so DIRECTDIFF always fails with a NameError. Feel free to fix that here. (The scaling itself is right for DIRECTDIFF: the derivative is seeded on the physical DV_VALUE, so it needs dv_scl like the adjoint.)
There was a problem hiding this comment.
Fixed, changed grad to grads (commit dc3b000). So DIRECTDIFF works again.
| #: def obj_dp() | ||
|
|
||
|
|
||
| def dv_gradient_scale(grad_method, dv_scl): |
There was a problem hiding this comment.
Optional: a small test of the scaling with SCALE != 1 would protect this from being reverted, since every current testcase uses SCALE = 1. Even a unit test of dv_gradient_scale would help.
There was a problem hiding this comment.
Added a test: SU2_PY/tests/test_dv_scale.py.
It checks that the gradient is scaled the right way for FINDIFF and for the adjoint, using DV scales of 2 and 0.5. The test passes with this PR and fails without it.
I also added it to the Python Tests workflow, so it runs automatically in CI.
The DIRECTDIFF branch of gradient() stored its result in grad instead of grads, so the gradients were never saved. Add a unit test of the DV scale applied in obj_df and run it with the other Python tests.
Proposed Changes
When DEFINITION_DV has a scale other than 1 the gradients from the python wrapper are wrong. unpack_dvs uses dv = scale * x, so the gradient wrt x needs to be multiplied by the scale, but obj_df, con_dceq and con_dcieq divide by it.
For finite differences the wrapper already perturbs x directly, so there the gradient is already wrt x and should not be scaled at all. So I added a small function that returns the scale for the adjoint (and geometry) gradients and 1 for FINDIFF, and used it in the 4 places.
I checked it on the inviscid naca0012 (mach 0.5, one hicks-henne dv, LIFT) against a central difference of obj_f:
(ratio obj_df / central difference)
With scale 1 nothing changes, and all the testcases use scale 1.
One more thing I noticed: FINDIFF is also off at a nonzero design point, even with scale 1 (around 24x at x=0.02). That looks like a separate problem in findiff() with DV_VALUE_OLD/NEW, so I left it out of this PR. I can open an issue for it.
Related Work
Fixes #3054
PR Checklist
Put an X by all that apply. You can fill this out after submitting the PR. If you have any questions, don't hesitate to ask! We want to help. These are a guide for you to know what the reviewers will be looking for in your contribution.
pre-commit run --allto format old commits.