Skip to content

Size mismatch in dual number handling #841

@ChrisRackauckas

Description

@ChrisRackauckas

MWE:

using NaNMath
using LinearAlgebra: Diagonal
using ForwardDiff
using OrdinaryDiffEqRosenbrock
function mwe_dae!(du, u, params, t)

    (; k_f, C, N, S, Rs, ) = params
    Ap = 3.14e-4
    Av = 3.8e-4
    Tsh = t->233.15 + 1800*t 
    Kshf = 17.344
    k_ice = 2.4
    hf0 = .01315
    R = 8.3145
    ϕ = 0.96
    pch = 13.33
    ΔHsub = 2.838e6
    Mw = 18
    
    hf, Tf, μ0, μ2 = u
    hd = hf0 - hf
    μ1 = hd * Ap
    Qshf = Av*Kshf*(Tsh(t) - Tf)
    Tsub = Tf - Qshf/k_ice/Ap*hf
    k = k_f(Tsub)

    # exit hatch for bad parameter guesses or solver iterations
    if Tf < 0 # || Tsub < 0u"K"
        du .= NaN
        return
    end

    porefac = μ1^2*μ0/μ2^3 
    Rp = C*NaNMath.sqrt(R*Tsub/Mw)/ϕ*hd * sqrt(sqrt(NaNMath.sqrt(porefac))) 
    if !isfinite(Rp)
        Rp = 0.0 # No pores to resist flow
    end

    Rsp = Rs + Rp
    psub = 3.597e12 * exp(-6149.1/Tsub)
    dmdt = - Ap*(psub-pch)/Rsp
    Qsub = dmdt*ΔHsub

    dhf_dt = min(0.0, dmdt/ϕ/Ap) # Cap dhf_dt at 0: no desublimation
    dμ0_dt = -k/2*μ0^2 - N*Ap*dhf_dt
    dμ2_dt = k*μ1^2 - S*Ap*dhf_dt # dhf_dt < 0 for drying proceeding

    du[1] = dhf_dt
    du[2] = Qsub + Qshf
    du[3] = dμ0_dt
    du[4] = dμ2_dt

    return 
end

mwe_daef = ODEFunction(mwe_dae!, mass_matrix=Diagonal([1.0, 0.0, 1.0, 1.0]))
u0 = [0.01315, 233.15, 0, 0]
tspan = (1439.28, 7200)

function mwe_loss(x) 
    badps = (N = x*2.5055e14, S = 7.344991e-14, C = 0.02147, Rs = 1.9017e-5, k_f = x->8.38e-16)
    solve(ODEProblem(mwe_daef, u0, tspan, badps), Rodas4())
end
mwe_loss(1.0)
ForwardDiff.derivative(mwe_loss, 1.0)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions