Calculate flux of N-layered solutions using automatic differentiation #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This creates and edits several new features:
ForwardDiff
by a simple call like...ForwardDiff.derivative(dz -> fluence_DA_Nlay_cylinder_CW(ρ, μa, μsp, n_ext, n_med, l, a, dz, besselroots), z)
. This allows the fluence and flux to be calculated in the same computation time.t
is just an AbstractFloat. However, if we want to calculate for many time points usinghyper_fixed
there were several errors to fix. The primary one being is that ForwardDiff uses Dual numbers that are embedded within the function arguments that are passed tohyper_fixed
. There are several spots wherehyper_fixed
allocates before evaluated the function so did not know the type. There were a couple of options that were tried here. One was just to evaluate the anonymous function passed tohyper_fixed
first for a single value then allocate based on those types. However, we want to be able to thread these solutions because the single evaluation is costly. Instead, I updated thehyper_fixed
functions to create an optional input argument being the eltype to preallocate the vectors inhyper_fixed
with. This is done with creating an intermediate function:function _ILT(z::T) where {T} return hyper_fixed(s -> _fluence_DA_Nlay_cylinder_Laplace(ρ, μa, μsp, n_ext, n_med, l, a, z, s, besselroots), t, N = N, T = T) end
where now the type parameter of z which is what we are differentiating with respect to is known at compile time an d fed tohyper_fixed
.