使用 fortran 包装的右侧时,Julia 的 DifferentialEquations 包失败

标签 julia jit differential-equations

我正在尝试使用 Julia 的 DifferentialEquations 方法求解常微分方程组。我的 ODE 的右侧被包裹 Fortran 90 .这是我的 Julia 代码:

using DifferentialEquations
function rhs(dNdt,N,p,t)
    ccall((:__atmos_MOD_rhs, "./EvolveAtmFort.so"), Cvoid,(Ref{Float64}, Ref{Float64},Ref{Float64}),t,N,dNdt)
end

N0 = [0.0,298.9,0.0562,22.9,0.0166,35.96,0.0,0.0,0.0,0.0]*6.022e23
tspan = [0.0,1.0e6*365.0*24.0*60.0*60.0]
prob = ODEProblem(rhs,N0,tspan)
sol = solve(prob,Rodas5());

这会产生以下长错误,这与计算右侧的导数/雅可比矩阵有关。下面,我只包含一些看起来很重要的 Stacktrace 部分。

MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.TimeGradientWrapper{ODEFunction{true,typeof(rhs),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Array{Float64,1},DiffEqBase.NullParameters},Float64},Float64,1})
Closest candidates are:
  Float64(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  Float64(::T) where T<:Number at boot.jl:715
  Float64(!Matched::Int8) at float.jl:60
  ...

Stacktrace:
[1] convert(::Type{Float64},...
[2] Base.RefValue{Float64}...
[3] convert(::Type{Ref{Float64}}, ...
[4] cconvert(::Type{T} where T, 
[5] rhs(::Array{ForwardDiff.Dual{ForwardDiff.Tag{DiffEqBase.TimeGradientWrapper{...
[6] (::ODEFunction{true,typeof(rhs),LinearAlgebra...
[7] (::DiffEqBase.TimeGradientWrapper{ODEFunction{true,typeof(rhs),LinearAlgebra...
[8] derivative!(::Array{Float64,1},...
[9] calc_tderivative!(::OrdinaryDiffEq...
[10] calc_rosenbrock_differentiation! at...
[etc...]

当我使用无 jacobian 方法时,如 Tsit5(),集成工作正常。只有需要雅可比计算的方法才会失败。我究竟做错了什么?如何调整我的 Fortran 包装程序以便我可以使用隐式方法?谢谢!

最佳答案

这是由于某些隐式求解器中的隐式自动微分。您需要将其关闭,即 Rodas5(autodiff=false)

关于使用 fortran 包装的右侧时,Julia 的 DifferentialEquations 包失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66594137/

相关文章:

math - 如何将分段函数输入到 Wolfram alpha 中?

combinations - 在 Julia 中计算 Levenshtein 距离时记录所有最佳序列比对

macros - 如何在 julia 中创建多行宏?

php - 在 macOS 上安装 Composer 时出错(JIT 编译失败)

c# - C# 编译器或 Jitter 会优化这些类型的算术运算吗?

python - Scipy odeint 非负解

julia - Julia 集中图书馆

jit - 如何在不运行 julia 函数的情况下预热它?

java - 使用 JIT 编译器的 Collections.emptyList 和空 ArrayList 的性能

python - 使用 GEKKO 优化框架形成约束方程系统