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

Strange floating point precision error #124

Open
cathaypacific8747 opened this issue Nov 25, 2022 · 1 comment
Open

Strange floating point precision error #124

cathaypacific8747 opened this issue Nov 25, 2022 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@cathaypacific8747
Copy link

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,

cosine_spacing(x_center, diameter, n :: Integer = 40) = @. x_center + (diameter / 2) * cos(-π/(n-1):0)

x_circ = cosine_spacing((x_min + x_max) / 2, x_max - x_min, n)
interpolate(foil, x_circ)

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
Copy link
Owner

GodotMisogi commented Nov 28, 2022

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.

@GodotMisogi GodotMisogi added the help wanted Extra attention is needed label Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants