# Programmatically creating parameters in ModelingToolkit.jl leads to unexpected outcome

**URL:** <https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit\
**Created:** [September 20, 2023, 1:28pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063 "2023-09-20T13:28:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 20, 2023, 1:28pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/1 "2023-09-20T13:28:26Z")

</div>

I am trying to create programmatically a _parameter_ (with `@parameter`) with ModelingToolkit.jl. While I am succeeding, the outcome is not exactly what I wish for. Here are the two examples:

```julia
@variables t # independent variable (time)
dDt = Differential(t) # define an operator for the differentiation w.r.t. time
@variables T(t) = 300.0 # dependent

function lhs1(timescale)
    pars = @parameters τ_T = timescale
    τvar = first(pars)
    return τvar*dDt(T)
end

function lhs2(variable, timescale)
    varsymbol = Symbol(:τ_, Symbol(variable))
    pars = (@parameters $(varsymbol) = timescale)
    τvar = first(pars)
    return τvar*dDt(variable)
end

lhs1(5.0)
lhs2(T, 5.0)

```

which give:

```julia
        
julia> lhs1(5.0)
τ_T*Differential(t)(T(t))

julia> lhs2(T, 5.0)
var"τ_T(t)"*Differential(t)(T(t))

```

as you can see, in the second case the created parameter is first not displayed correctly, and second, it is not a parameter at all, it is presented as a function of time. What do I have to do to make `lhs2` give exactly the same expression as `lhs1`?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 20, 2023, 1:30pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/2 "2023-09-20T13:30:34Z")

</div>

EDIT: The second problem, that `T(t)` is displayed, happens because of casting the variable `T` into a symbol, which includes the `(t)`. So I know how to fix that. The first problem of being displayed as `var"..."` I don’t know how to fix.

---

<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:** [September 20, 2023, 1:36pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/3 "2023-09-20T13:36:16Z")

</div>

> [@Datseris](#):
>
> `Symbol(:τ_, Symbol(variable))`

`Symbol(:τ_, ModelingToolkit.name(variable))`

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 20, 2023, 1:36pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/4 "2023-09-20T13:36:27Z")

</div>

Interestingly, solving the second issue also solved the first. Simply doing

```julia
    varsymbol = Symbol(:τ_, Symbol(string(variable)[1:end-3]))

```

solves the problem!

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 20, 2023, 2:00pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/5 "2023-09-20T14:00:01Z")

</div>

Thanks @ChrisRackauckas but this function doesn’t seem to exist, and I just updated:

```julia
  [961ee093] ModelingToolkit v8.69.1

julia> ModelingToolkit.name
ERROR: UndefVarError: `name` not defined
Stacktrace:
 [1] getproperty(x::Module, f::Symbol)
   @ Base .\Base.jl:31
 [2] top-level scope
   @ REPL[2]:1

```

---

<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:** [September 20, 2023, 2:03pm UTC](https://discourse.julialang.org/t/programmatically-creating-parameters-in-modelingtoolkit-jl-leads-to-unexpected-outcome/104063/6 "2023-09-20T14:03:06Z")

</div>

oh sorry, `ModelingToolkit.getname`
