Skip to content

Regression with differentiation of expressions involving arrays? #567

@rschiemann1

Description

@rschiemann1

PR 530 seems to introduce the intended error message Differentiation of expressions involving arrays and array variables is not yet supported. also in cases which ran fine before the PR.

The following code worked with jac=true before the latest Symbolics.jl release, which incorporated PR #530

using OrdinaryDiffEq, ModelingToolkit

function build_system(n)

    @parameters t # or would @variables t be better!?
    vars = @variables (Tgas[1:n](t) = fill(100,n)), 
                      (Tsol[1:n](t) = fill(20, n)), 
                      (cR[1:n](t) = fill(3000, n)), 
                      (mflux(t) = 0.5)

    @parameters params[1:3]
    D = Differential(t)
    

    dp, Tin, h = params 

    dx = h / n

    eqns = Array{Equation}(undef, 3 * n + 1)
    count = 1

    rR = 5.5e6 .* exp.(.-5680 ./(273 .+ Tsol)) .* (1 .- exp.(.-cR./50))
    
    q = 1e4 * (Tgas - Tsol)
    
    eqns[count] = 0 ~  mflux * 1000 * (Tin - Tgas[1]) - q[1] * dx
    count += 1
    for i in 2:n
        eqns[count] = 0 ~ mflux * 1000 * (Tgas[i-1] - Tgas[i]) - q[i] * dx
	count += 1
    end

    # solids energy balance
    for i in 1:n
        eqns[count] = D(Tsol[i]) ~ q[i] / (1000 * 2000)
        count += 1
    end

    # gas momentum balance
    eqns[count] = 0 ~ - mflux[1] + 5e-3 * sqrt(dp)
    count += 1

    # reactant concentration equations
    for i in 1:n
        eqns[count] = D(cR[i]) ~ -rR[i]
        count += 1
    end

    @named sys = ODESystem(eqns, t, vcat(vars...), params)
    return sys

end

dp = 100e2
Tin = 200
h = 0.5
parameters = [dp, Tin, h]

N = 100
tspan = (0.0, 1500.0)

sys = build_system(N)


u0 = ones(length(sys.states))
u0[1:N] .= parameters[2]
u0[N+1 : 2*N] .= 20
u0[2*N+1 : 3*N] .= 3000
u0[3*N+1] = 0.5


# works
prob = ODEProblem(sys, u0, tspan, parameters)

# errors with: Differentiation of expressions involving arrays and array variables is not yet supported.
prob = ODEProblem(sys, u0, tspan, parameters, jac=true, sparse=true)

┆Issue is synchronized with this Trello card by Unito

Metadata

Metadata

Assignees

No one assigned

    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