# \[ModelingToolkit.jl\] varmap\_to\_vars function

**URL:** <https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207>\
**Category:** General Usage\
**Tags:** question, modelingtoolkit\
**Created:** [May 2, 2023, 3:16pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207 "2023-05-02T15:16:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [May 2, 2023, 3:16pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207/1 "2023-05-02T15:16:24Z")

</div>

Hello,  
I have a question about the ModelingToolkit package, and in particular about `@parameters` value maps.

```julia
@parameters t k₁₂ k₂₁ α
p = [k₁₂ => 0.0005
	k₂₁ => 0.009
	α => 0.09]

```

I have an ODE system and I want to fit some data on it.  
As a test, I tried to generate fake data with the following code (from documentation of [DiffEqParamEstim.jl: Parameter Estimation for Differential Equations · DiffEqParamEstim.jl](https://docs.sciml.ai/DiffEqParamEstim/stable/)) **without noise**.

```julia
begin
using RecursiveArrayTools # for VectorOfArray
randomized = VectorOfArray([sol(time[i]) for i in 1:length(time)])
data = convert(Array,randomized)
end

```

and the cost function is as follow :

```julia
cost_function = build_loss_objective(prob, Tsit5(), L2Loss(time, data[1,:]),
                                     Optimization.AutoForwardDiff(),
                                     maxiters=100000,verbose=false, save_idxs = [1])

```

Thus, normally, `cost_data(p)` must be equal to `0`. However, cost\_data doesn’t eat value maps directly and we have to convert `p` in an` Array/Vector`. We can’t just create a `Vector ` with the values of `p` `[0.09, 0.009, 0.0005]` because the symbol ordering is not guaranteed :

 ![Capture d’écran 2023-05-02 165256](https://global.discourse-cdn.com/julialang/original/3X/e/1/e1dc8221183abe4c71ed47f13905510ed9800831.jpeg)

The function `varmap_to_vars` ( [Frequently Asked Questions · ModelingToolkit.jl (sciml.ai)](https://docs.sciml.ai/ModelingToolkit/dev/basics/FAQ/)) seems to be intended for that , but how it work is obscure. Documentation give the following example :

```julia
p = @parameters x y z
idxs = ModelingToolkit.varmap_to_vars([p[1] => 1, p[2] => 2, p[3] => 3], p)
p[idxs]

```

but `p[idxs]` is just `p` and not a vector …

How to correctly manage the symbol ordering when we work with value maps of ModelingToolkit ?

Thanks !

---

<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 2, 2023, 4:04pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207/2 "2023-05-02T16:04:31Z")

</div>

> [@fdekerme](#):
>
> `ModelingToolkit.varmap_to_vars([p[1] => 1, p[2] => 2, p[3] => 3], p)`

That gives you the index ordering for `p[1]`, `p[2]`, `p[3]`. So then when you index `p[idxs]` it’s in the order you’re expecting.

---

<div class="post-metadata">

**Author:** ![fdekerme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fdekerme/32/43574_2.png) [@fdekerme](https://discourse.julialang.org/u/fdekerme)\
**Post date:** [May 3, 2023, 7:50pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207/3 "2023-05-03T19:50:43Z")

</div>

Ok thanks I understand !

But when I do optimisation :

```julia
optprob = Optimization.OptimizationProblem(cost_function, [0.02, 0.05, 0.0005], lb = [0.0, 0.0, 0.0], ub = [0.1, 0.1, 0.1])
result_bfgs = solve(optprob, BFGS())

```

how can I know in which order pass the initial parameters `[0.02, 0.05, 0.0005]` in the OptimizationProblem, and in which order they are returned by the `solve` function (stored in` result_bfgs`) ?

---

<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 7, 2023, 3:28am UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-varmap-to-vars-function/98207/4 "2023-05-07T03:28:44Z")

</div>

> [@fdekerme](#):
>
> how can I know in which order pass the initial parameters `[0.02, 0.05, 0.0005]` in the OptimizationProblem, and in which order they are returned by the `solve` function

`states(sys)` would give you the ordering, and then you can use the `varmap_to_vars` to find the `permute!` indices.
