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

Can't use autodiff to calculate jacobian in the time-domain #5

Open
heltonmc opened this issue Aug 29, 2021 · 1 comment
Open

Can't use autodiff to calculate jacobian in the time-domain #5

heltonmc opened this issue Aug 29, 2021 · 1 comment
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@heltonmc
Copy link
Owner

heltonmc commented Aug 29, 2021

julia> using LightPropagation
julia> using ForwardDiff
julia> ForwardDiff.derivative(dx -> fluence_DA_Nlay_cylinder_CW(1.0, [dx, 0.1], [10.0, 10.0], 1.0, [1.0, 1.0], [1.0, 10.0], 10.0, 0.0, besselroots[1:1000]), 0.1)
-0.12899771300257823

julia> ForwardDiff.derivative(dx -> fluence_DA_Nlay_cylinder_TD(1.0,1.0, [dx, 0.1], [10.0, 10.0], 1.0, [1.0, 1.0], [1.0, 10.0], 10.0, 0.0, besselroots[1:1000]), 0.1)
ERROR: MethodError: no method matching ldexp(::ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float64}, Float64, 1}, ::Int64)
Closest candidates are:
  ldexp(::Float16, ::Integer) at math.jl:787
  ldexp(::T, ::Integer) where T<:Union{Float16, Float32, Float64} at math.jl:745
  ldexp(::BigFloat, ::Int64) at mpfr.jl:626
  ...
Stacktrace:
  [1] sqrt(z::Complex{ForwardDiff.Dual{ForwardDiff.Tag{var"#11#12", Float64}, Float64, 1}})
    @ Base ./complex.jl:488
  [2] α_coeff!
    @ ~/.julia/packages/LightPropagation/nHQWE/src/forwardmodels/Diffusion Approximation/DAcylinder_layered.jl:309 [inlined]

This is caused by the inverse laplace transform needing to use complex variables. The error is thrown because we need to evalue sqrt(Complex{ForwardDiff.Dual{Float64}... unsure the best fix for this. We are ok when calculating the flux because the z variable is never square rooted

For references the function throwing error is:

@inline function α_coeff!(α, μa, D, sn)
    @inbounds for ind in 1:length(μa)
        α[ind] = sqrt(μa[ind] / D[ind] + sn^2)
    end
    return α
end
@heltonmc heltonmc added the bug Something isn't working label Nov 11, 2021
@heltonmc heltonmc added the help wanted Extra attention is needed label Jan 4, 2022
@heltonmc
Copy link
Owner Author

heltonmc commented Apr 21, 2022

This seems to work now on v0.9.0 but is incredibly slow.... perhaps linked to #21

julia> ForwardDiff.derivative(dx -> fluence_DA_Nlay_cylinder_TD(1.0,1.0, (0.1, dx), (10.0, 10.0)), 0.1)
-0.002803585371894059

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant