# How to obtain numerical values of observed variables after structural simplification?

**URL:** https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639
**Category:** General Usage
**Tags:** modelingtoolkit
**Created:** [May 25, 2022, 10:23am UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639 "2022-05-25T10:23:46Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Vinicius\_Viena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_viena/32/36579_2.png) [@Vinicius\_Viena](https://discourse.julialang.org/u/Vinicius_Viena)
#### Post date: [May 25, 2022, 10:23am UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/1 "2022-05-25T10:23:47Z")

</div>

Hi there. I am quite new to Julia and sorry if this question is too silly, but here it goes.

I have used the modelingtoolkitize function to convert my discretized PDE to symbolic form and then used the strucutural\_simplify function followed by the ODAEProblem to convert it into an ODE. I have observed an incredible speedup in the solution time (25x) and I am very impressed!

In the original PDE discretization I have 248 variables. After structural\_simplify and alias\_elimination, the solve() gives me back 246 “state variables”. I can “access” the other 2 variables via sys.observed, however, they are symbolic equations and they are the most important variables in my system.

Here is a picture of the Equation type after sys.observed command.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/d/bd8485adfb874e3c6c4d7fd7077c3afab621044a.jpeg)

I have read the entire documentation and could not find a way to go from the “Equation” type to calculating these two variables X\_62(t) and x\_124(t) over time. Could someone give me a hint on this?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [May 25, 2022, 4:43pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/2 "2022-05-25T16:43:31Z")

</div>

I can’t find the documentation for this, but the [solution](https://diffeq.sciml.ai/stable/basics/solution/#Interpolations-and-Calculating-Derivatives) struct returned from the call to `solve` can be indexed by variable:

```julia
sol(t, idxs=[X_62, x_124])

```

---

<div class="post-metadata">

### Author: ![Vinicius\_Viena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_viena/32/36579_2.png) [@Vinicius\_Viena](https://discourse.julialang.org/u/Vinicius_Viena)
#### Post date: [May 26, 2022, 9:41am UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/3 "2022-05-26T09:41:03Z")

</div>

Hey! I really appreciate your help and suggestion.

However, it seems that this method can only query variables returned from the solve() call. The problem is that x\_62 and x\_124 are not returned in the solve call when I use structural\_simplify. They are separated from the other 246 variables (states) and are stored in the sys.observed as symbolic equations.

---

<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: [May 26, 2022, 12:37pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/4 "2022-05-26T12:37:56Z")

</div>

> [@Vinicius\_Viena](#):
>
> However, it seems that this method can only query variables returned from the solve() call

No, it can query observed variables.

---

<div class="post-metadata">

### Author: ![Vinicius\_Viena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_viena/32/36579_2.png) [@Vinicius\_Viena](https://discourse.julialang.org/u/Vinicius_Viena)
#### Post date: [May 26, 2022, 2:47pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/5 "2022-05-26T14:47:53Z")

</div>

Dear Chris,

First, thank you for your answer. I have been following your work and have to say thank you for all high-quality material you make available in the internet.

Despite your invaluable comments, I am not able to query observed variables in my problem.

Aditionally, I tried to print the state variables available after running states(sys) and it returns the following symbols which has length 246 (the same dimension of the vector returned by solve() method).  
**Just reminding that my original problem - PDE discretization - had 248 variables**.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/d/1ddda0e7dcc78fe376a6f912de2b354158b04402.png)

If i try states(sys)[124] it will give me back the state X\_128(t). For some reason, it is sorting the states in an order different from the index. And if i try solution(solution.t, idxs = [124]) it gives me the variable x\_128.

I do not know why but two unexpected behaviors are happening:  
solve() is returning 246 variables and hiding the other 2.  
Moreover, it is sorting the solution vector in an awkward manner (x\_1(t) is the last value).

Thank you for the support

---

<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: [May 26, 2022, 3:07pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/6 "2022-05-26T15:07:51Z")

</div>

> [@Vinicius\_Viena](#):
>
> solve() is returning 246 variables and hiding the other 2.  
> Moreover, it is sorting the solution vector in an awkward manner (x\_1(t) is the last value).

See the first tutorial.

[https://mtk.sciml.ai/dev/tutorials/ode\_modeling/](https://mtk.sciml.ai/dev/tutorials/ode_modeling/)

As @contradict mentioned, the solution only solves for a subset of the system, but using the symbols of the observed variables, such as `sol[x_128]`, will retrieve their values. You can use the symbol in place of an index for this feature and it will compute their outputs on demand. Likewise, if you built this from a symbolic vector, `sol[x]` should work and return the timeseries for the full `x`, or similarly `sol[x,i]` etc. using the symbol `x` itself.

---

<div class="post-metadata">

### Author: ![Vinicius\_Viena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_viena/32/36579_2.png) [@Vinicius\_Viena](https://discourse.julialang.org/u/Vinicius_Viena)
#### Post date: [May 26, 2022, 4:26pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/7 "2022-05-26T16:26:40Z")

</div>

Dear Chris,

I managed to get the variables values with your tip! Thank you for your patience and attention. It is taking me a while to grasp symbolic variables with differentialequations.jl library!

---

<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: [May 26, 2022, 4:31pm UTC](https://discourse.julialang.org/t/how-to-obtain-numerical-values-of-observed-variables-after-structural-simplification/81639/8 "2022-05-26T16:31:12Z")

</div>

> [@Vinicius\_Viena](#):
>
> I managed to get the variables values with your tip! Thank you for your patience and attention. It is taking me a while to grasp symbolic variables with differentialequations.jl library!

No worries, it’s not you. The symbolic stuff is pretty new so it’s just starting to become standard.
