Repository navigation
Fix finite difference gradients at a nonzero design - #3067
Open
SahilKumar965 wants to merge 1 commit into
Open
SahilKumar965 wants to merge 1 commit into
SahilKumar965 wants to merge 1 commit into
Conversation
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.
3 of 6 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
findiff()ingradients.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 thedvs_basecode that is no longer needed is removed. This also removes the double scaling ofdvs_basewith SCALE != 1 that was pointed out in Fix DV scaling of gradients in the python optimizer #3056.update_mesh()infunctions.py: in the optimizer,Design._evalevaluates the objective and then its gradient with the same config.obj_dfcallsunpack_dvs, which resetsDV_VALUE_OLDto 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 comparesobj_dfwith a central difference ofobj_f, so 1 is correct:The remaining difference is the error of the one-sided step, which scales with SCALE.
DISCRETE_ADJOINTgives 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
pre-commit run --allto format old commits.