Skip to content

Fix finite difference gradients at a nonzero design - #3067

Open
SahilKumar965 wants to merge 1 commit into
su2code:developfrom
SahilKumar965:fix/findiff-nonzero-design
Open

SahilKumar965 wants to merge 1 commit into
su2code:developfrom
SahilKumar965:fix/findiff-nonzero-design

Conversation

@SahilKumar965

Copy link
Copy Markdown

Proposed Changes

With GRADIENT_METHOD= FINDIFF, the Python wrapper gives wrong gradients as soon as the design is not zero. At x = 0 everything looks fine, which is probably why it went unnoticed.

There are two places where an already deformed mesh gets deformed again:

  1. findiff() in gradients.py: the base design is computed first, so the mesh is already at x. Each finite difference run then applied the full x + h on top of that mesh instead of only the step h. Now only the step of each design variable is applied, and the dvs_base code that is no longer needed is removed. This also removes the double scaling of dvs_base with SCALE != 1 that was pointed out in Fix DV scaling of gradients in the python optimizer #3056.
  2. update_mesh() in functions.py: in the optimizer, Design._eval evaluates the objective and then its gradient with the same config. obj_df calls unpack_dvs, which resets DV_VALUE_OLD to 0, so the base run of the finite differences deformed the mesh a second time. update_mesh() now skips the deformation when the state's mesh is already at the requested values.

The adjoint methods are not affected.

Check on NACA0012 (inviscid, Mach 0.5, one Hicks-Henne variable, objective LIFT, evaluated like the optimizer: objective first, then gradient). The ratio compares obj_df with a central difference of obj_f, so 1 is correct:

SCALE x0 develop this PR (+ #3056)
1.0 0.00 1.0024 1.0024
1.0 0.02 24.17 1.0024
2.0 0.01 25.41 1.0048
0.5 0.04 – 1.0012

The remaining difference is the error of the one-sided step, which scales with SCALE. DISCRETE_ADJOINT gives 1.0000 before and after.

Related Work

Follow-up to #3056, which fixes the DV scale of the gradients in obj_df. With SCALE != 1, both PRs are needed for correct FINDIFF gradients. With SCALE = 1, this one works on its own.

PR Checklist

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

findiff applied the full design values on top of a mesh that was already
deformed to the base design, so every step deformed it twice. Only apply
the step of each design variable, and skip the deformation in update_mesh
when the mesh of the state is already at the requested design.

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.

1 participant