Skip to content

Fix DV scaling of gradients in the python optimizer - #3056

Open
SahilKumar965 wants to merge 2 commits into
su2code:developfrom
SahilKumar965:fix/dv-scale-gradients
Open

SahilKumar965 wants to merge 2 commits into
su2code:developfrom
SahilKumar965:fix/dv-scale-gradients

Conversation

@SahilKumar965

Copy link
Copy Markdown

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:

                    scale   before   after
DISCRETE_ADJOINT     1.0     1.000   1.000
DISCRETE_ADJOINT     0.5     4.000   1.000
FINDIFF              1.0     1.002   1.002
FINDIFF              0.5     2.002   1.001

(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.

  • I am submitting my contribution to the develop branch.
  • My contribution generates no new compiler warnings (try with --warnlevel=3 when using meson).
  • My contribution is commented and consistent with SU2 style (https://su2code.github.io/docs_v7/Style-Guide/).
  • I used the pre-commit hook to prevent dirty commits and used pre-commit run --all to format old commits.
  • I have added a test case that demonstrates my contribution, if necessary.
  • I have updated appropriate documentation (Tutorials, Docs Page, config_template.cpp), if necessary.

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
Comment thread SU2_PY/SU2/eval/design.py
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread SU2_PY/SU2/eval/design.py
"""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."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, changed grad to grads (commit dc3b000). So DIRECTDIFF works again.

Comment thread SU2_PY/SU2/eval/design.py
#: def obj_dp()


def dv_gradient_scale(grad_method, dv_scl):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants