# ModellingToolkit system with ODEForwardSensitivityProblem

**URL:** https://discourse.julialang.org/t/modellingtoolkit-system-with-odeforwardsensitivityproblem/76093
**Category:** Modelling & Simulations
**Tags:** diffeq, modelingtoolkit
**Created:** [February 9, 2022, 3:22pm UTC](https://discourse.julialang.org/t/modellingtoolkit-system-with-odeforwardsensitivityproblem/76093 "2022-02-09T15:22:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ap2013](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@ap2013](https://discourse.julialang.org/u/ap2013)
#### Post date: [February 9, 2022, 3:22pm UTC](https://discourse.julialang.org/t/modellingtoolkit-system-with-odeforwardsensitivityproblem/76093/1 "2022-02-09T15:22:38Z")

</div>

Hi,

I am using ModellingToolkit to define a system following the [spring-mass example](https://mtk.sciml.ai/dev/tutorials/spring_mass/) in the documentation. I solve the ODEForwardSensitivityProblem of the system.

The problem is when I try to access the states of the system in the solution using `solS[mass.v[1]]`. I get the following error:  
`ERROR: LoadError: Indexing symbol mass₊v[1](t) is unknown.`  
It seems that ModellingToolkit is not working well with the ODEForwardSensisitivityProblem. Indeed, there is no problem when I solve the usual ODEProblem.

This is the code:

```julia
using ModelingToolkit
using DiffEqSensitivity

using Plots, DifferentialEquations, LinearAlgebra
using Symbolics: scalarize

@variables t
D = Differential(t)

function Mass(; name, m = 1.0, xy = [0., 0.], u = [0., 0.])
    ps = @parameters m=m
    sts = @variables pos[1:2](t)=xy v[1:2](t)=u
    eqs = scalarize(D.(pos) .~ v)
    ODESystem(eqs, t, [pos..., v...], ps; name)
end

function Spring(; name, k = 1e4, l = 1.)
    ps = @parameters k=k l=l
    @variables x(t), dir[1:2](t)
    ODESystem(Equation[], t, [x, dir...], ps; name)
end

function connect_spring(spring, a, b)
    [
        spring.x ~ norm(scalarize(a .- b))
        scalarize(spring.dir .~ scalarize(a .- b))
    ]
end

spring_force(spring) = -spring.k .* scalarize(spring.dir) .* (spring.x - spring.l) ./ spring.x

m = 1.0
xy = [1., -1.]
k = 1e4
l = 1.
center = [0., 0.]
g = [0., -9.81]
@named mass = Mass(m=m, xy=xy)
@named spring = Spring(k=k, l=l)

eqs = [
    connect_spring(spring, mass.pos, center)
    scalarize(D.(mass.v) .~ spring_force(spring) / mass.m .+ g)
]

@named _model = ODESystem(eqs, t)
@named model = compose(_model, mass, spring)
sys = structural_simplify(model)

prob = ODEProblem(sys, ones(length(states(sys))), (0., 3.))
sol = solve(prob, Rosenbrock23())
plot(sol.t,sol[mass.v[1]])

probS = ODELocalSensitivityProblem(sys, ones(length(states(sys))), (0., 3.),[k,l,m])
solS = solve(probS)

plot(solS.t,solS[mass.v[1]])

```

Thanks! I appreciate any help.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 9, 2022, 11:37pm UTC](https://discourse.julialang.org/t/modellingtoolkit-system-with-odeforwardsensitivityproblem/76093/2 "2022-02-09T23:37:32Z")

</div>

It doesn’t pass on the symbolic pieces. We really should be doing it on the MTK side, though that’s still a wip [https://github.com/SciML/ModelingToolkit.jl/pull/398](https://github.com/SciML/ModelingToolkit.jl/pull/398)
