# Parameter sharing and abstraction in MTK

**URL:** <https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [January 26, 2026, 9:00am UTC](https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269 "2026-01-26T09:00:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 26, 2026, 9:00am UTC](https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269/1 "2026-01-26T09:00:49Z")

</div>

I have started using `ParametrizedInterpolation` from the standard library in my code, and while it works fine, having to add a clock to each component that uses it tarnishes the code a bit. So I wrote this:

```julia
""" A step function, where `step_function.u` is `ys[i]` when `t = ts[i]` (and afterwards).

The point is that while `ParametrizedInterpolation` can be used directly, its interface requires an
explicit `clock`. `StepFunction` is simple: add it as a System, and access its values with `sf.u`

@mtkcompile sf = StepFunction([1,2,3], [4,5,3])
res = solve(ODEProblem(sf, [], [1,10]))
println(res.([1,1.5,2,3,4,8], idxs=sf.val))

# output
[4.0, 4.0, 5.0, 3.0, 3.0, 3.0]
 """
@component function StepFunction(ts, ys; name=:unnamed_interpolator)
    @named interp = ParametrizedInterpolation(ExtrapolatedConstantInterpolation,
                                              Float64.(ys), Float64.(ts))
    @named clk = ContinuousClock()
    @variables val(t)
    eqs = [connect(clk.output, interp.input),
           val ~ interp.output.u]
    return System(eqs, t, [val], []; name, systems=[interp, clk])
end

```

And it works nicely.

Question 1: is there anything wrong with that?

Questions 2: Now setting the `ts` parameter looks like `my_motor.input.interp.ts => [1,2,10]`. How can I make it like `my_motor.input.ts => [1,2,10]`? I’ve read [Initialization of Systems · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/dev/tutorials/initialization/#Initialization-of-parameters), but I’m not so confident. Should I set the `interp.ts` to `missing`, and set up an equation like `interp.ts ~ ts` with an additional `ts` parameter in `StepFunction`?

---

<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:** [January 26, 2026, 11:18am UTC](https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269/2 "2026-01-26T11:18:03Z")

</div>

You might want to share clocks in order to reduce the total number of clocks that need to be tracked.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [January 29, 2026, 6:28pm UTC](https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269/3 "2026-01-29T18:28:19Z")

</div>

You could explore using [ModelingToolkitInputs.jl](https://bradcarman.github.io/ModelingToolkitInputs.jl/dev/). This will allow you to easy change your step function, like

```julia
step = Input(sys.x, [4.0, 5.0, 3.0], [1, 2, 3])
sol = solve(sys; inputs=[step])

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [January 30, 2026, 9:26am UTC](https://discourse.julialang.org/t/parameter-sharing-and-abstraction-in-mtk/135269/4 "2026-01-30T09:26:27Z")

</div>

Interesting! Have you used it for optimization? Given that it supports `remake`, it should work out?
