Hi -
Sorry to keep bothering you, but A quick question -
Using adjoint_sensitivities - I get a type error, and I am not understanding how it arises. Is again something I shouldnāt be doing with MassMatrix ODE?
This is the error:
TypeError: in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,1}
TypeError: in setindex!, in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,1}
Here is the code:
# Generate System
X=vcat(U,P) # X concatenates U and P to generate equations as function of U and P
Eqs=lambdify(A,X) # Generate matrix with all system equations based on X
# Generate Jacobian w/r to vars
Jvars=jacobian(A*U,U) # Symbolic jacobian of system for fixed parameters (p) with respct to variables (U)
Jac_vars=lambdify(Jvars,X) # Evaluates symbolic jacobian of system at fixed p for given U=u, for example u=IC
# Generate Jacobian w/r to params
Jparams=jacobian(A*U,P) # Symbolic jacobian of system for fixed variables (u) with respct to parameters (P)
Jac_params=lambdify(Jparams,X) # Evaluates symbolic jacobian of system at fixed u for given P=p
s=size(A)
varnum=21
parnum=32
algnum=6
dv=varnum - algnum
diff_vars=[if iā¤dv true else false end for i in 1:varnum ]
MM = zeros(varnum,varnum)
[MM[i,i] = 1 for i in 1:dv]
c=zeros(varnum)
function ODE_Sym(du,u,p,t)
E1=convert(Array{Float64,2}, Eqs(vcat(u,p)...))
for i in 1:s[1]
if (iā¤dv)
du[i]=(BLAS.gemv('N', E1, u)[i]) - c[i]
else
du[i]=(BLAS.gemv('N', E1, u)[i]) - c[i]
end
end
# mul!(du,Eqs(vcat(u,p)...),u)
end
function Jac_ODE_Sym(J,u,p,t)
J1=convert(Array{Float64,2}, Jac_vars(vcat(u,p)...))
for i in 1:s[1]
for j in 1:s[2]
J[i,j]=J1[i,j]
end
end
end
function paramJac_ODE_Sym(pJ,u,p,t)
pJ1=convert(Array{Float64,2}, Jac_params(vcat(u,p)...))
for i in 1:varnum
for j in 1:parnum
pJ[i,j]=pJ1[i,j]
end
end
end
ODE_test = ODEFunction(ODE_Sym;mass_matrix=MM,jac=Jac_ODE_Sym,paramjac=paramJac_ODE_Sym)
ODE_test_Prob = ODEProblem(ODE_test,IC,tspan,p)
ODE_test_sol = DifferentialEquations.solve(ODE_test_Prob,Rodas5())#, alghint=:stiff)
This solves no problem -
And i get a nice solution fro ODE_test_sol
when I try to use adjoint sensitivity
# objective function is to obtain sensitivity with respect to tumor ratio u[19] I could change to tumor volume (u[16])
g(u,p,t) = (sum(u[19]).^2) ./ 2
function dg(out,u,p,t)
# [out[i]= 0 for i in vcat(1:18,20:21)]
# out[19]= u[19]
[out[i]= u[19] for i in 1:varnum]
end
res = adjoint_sensitivities(ODE_test_sol, Rodas5(),g,nothing,dg)
when I tried with Vern9 or even DP8 on a server no result has shown up after 2 hours
My system has 21 equation (6 algebraic), 21 variables and 32 parameters
i appreciate any advice-
Thanks,
A
Thanks
PS- Once I am done with adjoint i am moving to sobol, but i was asked for sensitivity against u[19] explicitly