SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 207 forks source link

Cannot retrieve arrays of array variables from sol with `sol[u[1]]` #1481

Closed xtalax closed 2 years ago

xtalax commented 2 years ago
using ModelingToolkit, MethodOfLines, NonlinearSolve, DomainSets, SymbolicUtils
# Parameters, variables, and derivatives
@parameters x
@variables u[1:2](..) 

function constructprob(v)

    depvars = map(w -> (w~0).lhs, v)
    w = map(depvars) do y
        op = SymbolicUtils.operation(y)
        sym= Symbol(string(op))
        collect(first(@variables $sym[1:2]))
    end

    eqs = [w[1][1] ~ 1., w[2][1] ~ 2., w[1][2] ~ 3., w[2][2] ~ 4.]
    alldepvars = vec(reduce(vcat, vec(w)))#
    ps = Num[]
    defaults = Dict([w[1][1] => 1., w[2][1] => 2., w[1][2] => 3., w[2][2] => 4.])#

    sys = NonlinearSystem(eqs, alldepvars, ps, defaults=defaults, name=:sys)

    return NonlinearProblem(sys, ones(length(sys.states)))
end

prob = constructprob([u[1](x), u[2](x)])
# Solve ODE problem
sol = NonlinearSolve.solve(prob, NewtonRaphson())

sol[u[1]]
ERROR: LoadError: UndefVarError: u not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/SymbolicUtils/v2ZkM/src/code.jl:351 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/RuntimeGeneratedFunctions/KrkGo/src/RuntimeGeneratedFunctions.jl:129 [inlined]
 [3] macro expansion
   @ ./none:0 [inlined]
 [4] generated_callfunc
   @ ./none:0 [inlined]
 [5] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#7127272041482501345"), Symbol("##arg#5964805160111424296")), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xba83f00c, 0x125fdafe, 0x84de63c2, 0x3c6961f9, 0xb68dad88)})(::Vector{Float64}, ::Nothing)
   @ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/KrkGo/src/RuntimeGeneratedFunctions.jl:117
    .    .    .
in expression starting at /home/alex/.julia/dev/MethodOfLines.jl/src/scratch/arrayvarmwe.jl:30
YingboMa commented 2 years ago
    depvars = map(w -> (w~0).lhs, v)

and

    w = map(depvars) do y
        op = SymbolicUtils.operation(y)
        sym= Symbol(string(op))
        collect(first(@variables $sym[1:2]))
    end

are just really wrong. What are you trying to do?

xtalax commented 2 years ago

That's equivalent what we're doing in MethodOfLines for non array variables but for array variables - how could I do equivalent in the right way?