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

Speed up the airfoil drag calculations #118

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

askprash
Copy link
Member

@askprash askprash commented Jan 7, 2025

This PR makes some improvements to the airfoil drag calculations to speed up the airsection calculations by roughly ~5x. The airfoil type wasn't really type stable/ it's field types weren't inferable by the compiler. Also the spline evaluations have been cleaned up a little and optimized for better performance.

On my M2 mac the performance of the airfoil section calculations are:

julia> bench = @benchmarkable aerodynamics.airfun($clp, $toc, $Mperp, 
                                               $airfoil_section) seconds = 30 evals = 100
Benchmark(evals=100, seconds=30.0, samples=10000)

julia> bench_airfun = run(bench)
BenchmarkTools.Trial: 10000 samples with 100 evaluations.
 Range (min  max):  696.250 ns   1.106 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     701.670 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   710.746 ns ± 24.497 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▇█▇▆▅▅▄▂▂▂▁▁    ▃▄▃▃▂▁                                       ▂
  █████████████▇▇██████████▆▇▇▆▆▅▆▆▆▇▅▇▆▆▆▅▆▄▆▅▆▅▄▅▄▃▅▄▄▄▅▃▄▂▅ █
  696 ns        Histogram: log(frequency) by time       824 ns <

 Memory estimate: 96 bytes, allocs estimate: 4.

This PR:

julia> bench_airfun = run(bench)
BenchmarkTools.Trial: 10000 samples with 100 evaluations.
 Range (min  max):  131.250 ns  392.500 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     133.750 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   134.427 ns ±   7.138 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

              ▂  ▆  █  ▆  ▄                                      
  ▂▁▁▃▁▁▃▁▁▆▁▁█▁▁█▁▁█▁▁█▁▁█▁▁█▁▁▅▁▁▄▁▁▃▁▁▂▁▁▂▁▁▂▁▁▂▁▁▂▁▁▂▁▁▂▁▁▂ ▃
  131 ns           Histogram: frequency by time          140 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 5.00000% with 57 lines in your changes missing coverage. Please review.

Project coverage is 5.68%. Comparing base (e182855) to head (6856d44).

Files with missing lines Patch % Lines
src/aero/airfun.jl 0.00% 31 Missing ⚠️
src/aero/trefftz.jl 0.00% 14 Missing ⚠️
src/aero/surfcd.jl 0.00% 8 Missing ⚠️
src/aero/airfoil.jl 20.00% 4 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (e182855) and HEAD (6856d44). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (e182855) HEAD (6856d44)
3 1
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #118       +/-   ##
==========================================
- Coverage   79.14%   5.68%   -73.47%     
==========================================
  Files          82      78        -4     
  Lines       13585   13552       -33     
==========================================
- Hits        10752     770     -9982     
- Misses       2833   12782     +9949     

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

@askprash askprash marked this pull request as ready for review January 23, 2025 15:21
Copy link
Contributor

@ngomezve ngomezve left a comment

Choose a reason for hiding this comment

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

The PR looks good to me. The aerodynamic functions in airfun.jl in general are the least readable in all of TASOPT: they have essentially no comments or documentation and the variable names are not descriptive. Since the PR is not any worse in this regard than main, I believe it can be pulled in.

@@ -87,12 +81,12 @@ function surfcd2(
Snorm = 0.0
ARe = wing.airsection.Re

for i = 1:n/2
frac = (float(i) - 0.5) / float(n / 2)
for i = 1:n÷2
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to comment on why the integer division is used

@askprash askprash force-pushed the dev_aero_speed branch 2 times, most recently from 1db0532 to 66082be Compare February 14, 2025 20:49
@askprash
Copy link
Member Author

Got rid of a few more stray allocations that weren't needed. Wing cd calculations are allocation free now and significantly faster relative to main:

julia> bench_surfcd2 = run(bench)
BenchmarkTools.Trial: 10000 samples with 5 evaluations.
 Range (min  max):  8.225 μs   1.750 ms  ┊ GC (min  max): 0.00%  99.12%
 Time  (median):     8.542 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   9.639 μs ± 29.016 μs  ┊ GC (mean ± σ):  7.69% ±  2.78%

  ▄▇█▇▅▅▄▃▄▄▃▃▂▂▂▂▁▁▁▁▁▁                                     ▂
  ██████████████████████████▇▇▇▆▅▅▆▅▄▆▆▄▅▅▄▄▅▁▅▄▅▁▄▄▄▅▃▄▄▄▃▄ █
  8.22 μs      Histogram: log(frequency) by time     14.6 μs <

 Memory estimate: 8.31 KiB, allocs estimate: 281.

This PR:

---------------------------------------
surfcd2 (FORTRAN on MacPro M2 ~ 2.01 μs)
---------------------------------------
BenchmarkTools.Trial: 10000 samples with 5 evaluations.
 Range (min  max):  1.633 μs   12.058 μs  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     1.750 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.782 μs ± 342.426 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

   ▂▂▂▄█                                                       
  ▃█████▇▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂ ▃
  1.63 μs         Histogram: frequency by time        3.18 μs <

 Memory estimate: 0 bytes, allocs estimate: 0. 

New documentation added too.

@askprash
Copy link
Member Author

Found a bug 🪲. The spline function had an error here:

B[i-1] = ΔS₊

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.

2 participants