GodotMisogi / AeroFuse.jl

A toolbox meant for aircraft design analyses.
https://godotmisogi.github.io/AeroFuse.jl/
MIT License
44 stars 8 forks source link

Strange floating point precision error #124

Open cathaypacific8747 opened 1 year ago

cathaypacific8747 commented 1 year ago

I was trying to calculate the lift coefficient of a NACA4412 airfoil and I encountered the following error:

foil = naca4((4,4,1,2), 70)
uniform = Uniform2D(1.0, 4.0)
system = solve_case(
    foil,
    uniform
)
cl = lift_coefficient(system)
ERROR: BoundsError: attempt to access 69-element extrapolate(interpolate((view(::Matrix{Float64}, :, 1),), ::Vector{Float64}, Gridded(Linear())), Throw()) with element type Float64 at index [-0.000268420185446816]
Stacktrace:
  [1] throw_boundserror(A::Interpolations.Extrapolation{Float64, 1, Interpolations.GriddedInterpolation{Float64, 1, Vector{Float64}, Interpolations.Gridded{Interpolations.Linear{Interpolations.Throw{Interpolations.OnGrid}}}, Tuple{SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}}, Interpolations.Gridded{Interpolations.Linear{Interpolations.Throw{Interpolations.OnGrid}}}, Interpolations.Throw{Nothing}}, I::Tuple{Float64})
    @ Base ./abstractarray.jl:691
  [2] inbounds_index
    @ ~/.julia/packages/Interpolations/Sxe87/src/extrapolation/extrapolation.jl:111 [inlined]
  [3] inbounds_position
    @ ~/.julia/packages/Interpolations/Sxe87/src/extrapolation/extrapolation.jl:102 [inlined]
  [4] Extrapolation
    @ ~/.julia/packages/Interpolations/Sxe87/src/extrapolation/extrapolation.jl:48 [inlined]
  [5] _broadcast_getindex_evalf
    @ ./broadcast.jl:670 [inlined]
  [6] _broadcast_getindex
    @ ./broadcast.jl:643 [inlined]
  [7] getindex
    @ ./broadcast.jl:597 [inlined]
  [8] macro expansion
    @ ./broadcast.jl:961 [inlined]
  [9] macro expansion
    @ ./simdloop.jl:77 [inlined]
 [10] copyto!
    @ ./broadcast.jl:960 [inlined]
 [11] copyto!
    @ ./broadcast.jl:913 [inlined]
 [12] copy
    @ ./broadcast.jl:885 [inlined]
 [13] materialize
    @ ./broadcast.jl:860 [inlined]
 [14] interpolate(foil::Foil{Float64}, xs::Vector{Float64})
    @ AeroFuse.AircraftGeometry ~/.julia/packages/AeroFuse/dwOGa/src/Geometry/AircraftGeometry/Foils/foil.jl:76
 [15] cosine_interpolation
    @ ~/.julia/packages/AeroFuse/dwOGa/src/Geometry/AircraftGeometry/Foils/foil.jl:125 [inlined]
 [16] make_panels(foil::Foil{Float64}, n::Int64)
    @ AeroFuse.AircraftGeometry ~/.julia/packages/AeroFuse/dwOGa/src/Geometry/AircraftGeometry/Foils/foil.jl:149
 [17] #solve_case#7
    @ ~/.julia/packages/AeroFuse/dwOGa/src/Aerodynamics/Cases/foil_cases.jl:10 [inlined]
 [18] solve_case(foil::Foil{Float64}, freestream::Uniform2D{Float64})
    @ AeroFuse ~/.julia/packages/AeroFuse/dwOGa/src/Aerodynamics/Cases/foil_cases.jl:10
 [19] top-level scope
    @ REPL[4]:1

Strangely, this error only occurs for specific values of n:

foil = naca4((4,4,1,2), n) # fails if n = 69, 70, 72, 73 but works if n = 67, 68, 71, 74

Looking through the code, I suspect that when it is calculating the cosine interpolation, https://github.com/GodotMisogi/AeroFuse.jl/blob/57268688ae38ce1b655b23e0b9bfccd6599140d6/src/Tools/MathTools.jl#L141 https://github.com/GodotMisogi/AeroFuse.jl/blob/a242e25d25c10510c79abdb06fe2a84d90d9a85a/src/Geometry/AircraftGeometry/Foils/foil.jl#L123-L125 the resulting minima of the interpolated values can sometimes be lower than the minima of the input values, causing interpolate to fail due to it being out of bounds. Maybe change it to the following to avoid the loss of precision?

cosine_spacing(x_start, x_end, n :: Integer = 40) = @. max(x_start + (x_end - x_start) * (cos(-π:π/(n-1):0)+1)/2, x_end)
# ...
x_circ = cosine_spacing(x_min, x_max, n)

Thanks!

GodotMisogi commented 1 year ago

Thanks for letting me know! I've been working on this issue by revamping the foil interpolation methods. A new version with fixes should be released soon.