Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in wave drag derivatives #432

Merged
merged 18 commits into from
Jul 16, 2024
Merged

Conversation

A-CGray
Copy link
Member

@A-CGray A-CGray commented Jul 15, 2024

Purpose

Wave drag is zero if the flight Mach number is below the critical Mach number. In this case, the partial derivatives should also be zero. However, the compute_partials method of the wave drag component simply does nothing to the partials arrays if M < Mcrit, leaving whatever values are already in the partial derivative arrays:

Wave Drag Compute Partials:
Mcrit=array([0.79169576])
M=array([0.77])
partials['CDw', 'CL']=array([[1.09376204e+09]])
partials['CDw', 'lengths_spanwise']=array([[-1.88346814e+08, -2.42085539e+08, -2.95707476e+08,
        -3.49292345e+08, -4.02879431e+08, -4.56485145e+08,
        -5.10114915e+08]])
partials['CDw', 'widths']=array([[2.03039031e+08, 2.62837681e+08, 3.23338326e+08, 3.84629052e+08,
        4.46753302e+08, 5.09729247e+08, 5.73562904e+08]])
partials['CDw', 'Mach_number']=array([[8.08674014e+09]])
partials['CDw', 'chords']=array([[-2929614.48597978, -5040768.89678295, -3420983.43417756,
        -1816297.48330755,  -202958.56134173,  1423657.05368816,
         3062503.08397319,  1942355.92887645]])
partials['CDw', 't_over_c']=array([[7.63448035e+08, 9.80843713e+08, 1.19704483e+09, 1.41292912e+09,
        1.62892546e+09, 1.84520841e+09, 2.06183049e+09]])

This PR fixes this issue by explicitly zeroing out the wave drag partial derivatives at the start of compute_partials.

I also made a few code quality improvements to remove some minor differences between code in compute and compute_partials that should be identical:

  • Renamed mean_chords in compute and chords in compute_partials to panel_mid_chords
  • Switched to using self.ka instead of a hardcoded 0.95 in compute_partials as is done in compute
  • Pre compute the summed panel area in compute as is done in compute_partials

Expected time until merged

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

Successfully converged optimisation that was previously failing. Also tested partial derivatives.

Before this change:

----------------------------------------------------------
Component: WaveDrag 'cruise.wing_perf.aero_funcs.wavedrag'
----------------------------------------------------------

+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| of '<variable>' | wrt '<variable>'   |   calc mag. |  check mag. |  a(cal-chk) |  r(cal-chk) | error desc         |
+=================+====================+=============+=============+=============+=============+====================+
| 'CDw'           | 'CL'               |  1.0938e+09 |  0.0000e+00 |  1.0938e+09 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| 'CDw'           | 'Mach_number'      |  8.0867e+09 |  0.0000e+00 |  8.0867e+09 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| 'CDw'           | 'chords'           |  8.0133e+06 |  0.0000e+00 |  8.0133e+06 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| 'CDw'           | 'lengths_spanwise' |  9.6666e+08 |  0.0000e+00 |  9.6666e+08 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| 'CDw'           | 't_over_c'         |  3.9094e+09 |  0.0000e+00 |  3.9094e+09 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+
| 'CDw'           | 'widths'           |  1.0729e+09 |  0.0000e+00 |  1.0729e+09 |  1.0000e+00 |  >ABS_TOL >REL_TOL |
+-----------------+--------------------+-------------+-------------+-------------+-------------+--------------------+

After this change:

+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| of '<variable>' | wrt '<variable>'   |   calc mag. |  check mag. |  a(cal-chk) | r(cal-chk) | error desc |
+=================+====================+=============+=============+=============+============+============+
| 'CDw'           | 'CL'               |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| 'CDw'           | 'Mach_number'      |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| 'CDw'           | 'chords'           |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| 'CDw'           | 'lengths_spanwise' |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| 'CDw'           | 't_over_c'         |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+
| 'CDw'           | 'widths'           |  0.0000e+00 |  0.0000e+00 |  0.0000e+00 |        nan |            |
+-----------------+--------------------+-------------+-------------+-------------+------------+------------+

If there is an existing test that checks our aero derivatives we should add a case for which M < Mcrit, I couldn't find any such tests though.

Checklist

  • I have run flake8 and black to make sure the Python code adheres to PEP-8 and is consistently formatted
  • I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@A-CGray A-CGray requested a review from a team as a code owner July 15, 2024 02:51
@A-CGray A-CGray requested review from kanekosh and eytanadler July 15, 2024 02:51
Copy link

codecov bot commented Jul 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.95%. Comparing base (d788e11) to head (c1a3e2a).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #432      +/-   ##
==========================================
+ Coverage   93.92%   93.95%   +0.02%     
==========================================
  Files         104      104              
  Lines        6472     6468       -4     
==========================================
- Hits         6079     6077       -2     
+ Misses        393      391       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@eytanadler eytanadler left a comment

Choose a reason for hiding this comment

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

Good catch! Could you add a test that breaks before these changes and succeeds with them? Then it's good to go from me.

@A-CGray
Copy link
Member Author

A-CGray commented Jul 15, 2024

Good catch! Could you add a test that breaks before these changes and succeeds with them? Then it's good to go from me.

I was initially going to test this fix by enabling wave drag in the scaneagle test (which has a super low Mach number) and then adding a check_totals test. However, the totals there were fine. I kept the new totals derivatives test in there though as I think it's a good addition.

I'm assuming the reason the scaneagle test didn't catch the bug is that the values in the partials arrays that get passed in to compute_partials contain the partials from the last time the method was called, so to trigger this bug you need an case that starts off with M > Mcrit but has an optimum where M < Mcrit. Presumably this is why nobody has run into this bug before.

I managed to engineer a case like this by reducing the Mach number in test_aerostruct_wingbox_wave_fuel_vol_constraint_opt.py from 0.85 to 0.77 and increasing the initial t_over_c values. This ensures that the initial design has wave drag and that the optimum is wave drag free.

@eytanadler eytanadler self-requested a review July 15, 2024 15:39
eytanadler
eytanadler previously approved these changes Jul 15, 2024
@A-CGray A-CGray requested a review from eytanadler July 15, 2024 21:19
kanekosh
kanekosh previously approved these changes Jul 16, 2024
@eytanadler eytanadler merged commit 97be635 into mdolab:main Jul 16, 2024
9 checks passed
@A-CGray A-CGray deleted the WaveDragFix branch July 16, 2024 14:44
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.

3 participants