Hi everyone!
I am solving a system of non-linear equations by using nlsolve. I want to use ForwardDiff to calculate the Jacobian inside nlsolve. The code which is running without any errors is the following:
function getvals2D(ϵᶜ, tᶜ, ϵᶠ, tᶠ, V0, guess)
p=ceil(Int,N/2)
array_size=Int(p*(p+1)/2)
function f(F, x::AbstractArray{T}) where T
be = zeros(eltype(x),N,N)
λe = zeros(eltype(x),N,N)
Hp=zeros(Complex{T},2,2,N,N,2,2,N,N)
Gp=zeros(Complex{T},size,size)
Gbp=zeros(Complex{T},2,2,N,N,2,2,N,N)
be=x[:,:,1]
λe=x[:,:,2]
for indices in Iterators.product(ranges1...)
a = equationst(ϵᶜ, tᶜ, ϵᶠ, tᶠ, V0, be, λe, indices, Hp, Gp, Gbp)
F[indices...,1] = real(a[1])
F[indices...,2] = real(a[2])
end
end
sol = nlsolve(f, guess, method=:newton, autodiff=:forward, iterations = 10, show_trace=true)
return sol.zero
end
I want to remove the allocations from the function f (namely my preallocations of be, Hp, etc.) but I can’t figure out how to do this because I always run into method errors connected to the Dual type of ForwardDiff as soon as I try to define my tensors outside of f. Is there any way I can remove these allocations from f? I can’t figure out what type I have to define them as because even copying the Datatype from the working Code above doesn’t solve the issue.
Thanks a lot for your help!