MTK & `@mtkmodel` & `@mtkbuild`

Great to see the latest update of the ModelingToolkit.jl documentation! The “new way” of doing things with @mtkmodel makes sense to me!

There are a few things that I don’t understand in the transition from the “old” way to do things, and the new way.

  1. Will the “old way” still work? [I.e., not using new macros, etc.?]

  2. New @mtkbuild

  • When I try to run the Getting Started section, the example under Copy-Pastable Simplified Example , I get the following error message:

    [Note: I just did Pkg.update()]

  • how does @mtkbuild (when I get it to work) compare to the “old way”? From the first example in the updated documentation (Getting Started section):

@mtkmodel FOL begin
    @parameters begin
        τ # parameters
    @variables begin
        x(t) # dependent variables
    @equations begin
        D(x) ~ (1 - x) / τ

@mtkbuild fol = FOL()
prob = ODEProblem(fol, [fol.x => 0.0], (0.0, 10.0), [fol.τ => 3.0])

In the “old way”, I always did instantiation followed by structural_simplify before sending the model to ODEProblem.

  • Does @mtkbuild combine the instantiation step (@named fol = FOL()) and the structural_simplify?
  • One reason for my uncertainty… the first example of the subsequent Building component-based, hierarchical models builds the model in the “old way”, i.e., does not use the @mtkbuild macro, and proceeds with @named, etc.
  1. The macro @structural_parameters… I assume that the listed structural parameters, e.g., can be used for switching between different equations in the model?

  2. Parameters vs. “derived parameters”
    Sometimes in the past, I have needed to introduce a common (“independent” parameter?), and then two derived parameters (“dependent” parameters?):

@parameters a=1, b=2*a, c=1/a

where a does not appear in the equations. That has caused problems, although the problems are avoided by adding the list of parameters in the ODESystem specification.

Should this case be handled as follows when using @mtkmodel?

@parameters begin
    a = 1

    b = 2*a
    c = 1/a
  1. Does the order of the various macro blocks (@parameters, etc.) matter within the @mtkmodel block? In other words: do I have to specify parameter values before I use them to set initial values for states, etc.?

Good to hear!

Yes, and it’s documented here:

That probably even de-mystifies some of the IR. Basically, @mtkmodel is the front-end to ModelingToolkit. It lowers to the intermediate representation (IR). The IR is quite nice to work on though: it’s not like LLVM, the IR is itself the symbolic representation that you previously used to work with. We now just put a face over it.

What version do you have? You need the latest of the latest, the one released like 2 days ago.

Yes, well and another step.

@named fol = FOL()
fol = complete(structural_simplify(fol))

is how to to write it in full. Technically, you should’ve been calling complete before: it’s what makes fol.x “work well” (because a completed system by default does not namespace the top level). In practice, I don’t think a lot of people did use complete, and so this will help people do things much more easily.

oh I’ll update that.

Not quite yet. We’ll get that done rather soon though.

@parameters begin
    a = 1
    b = 2*a
    c = 1/a

is the intended syntax. I’ll need to check if that works.

I’m not sure if we check that right now, but I think it would be sane to enforce and order. Sometimes a little bit of order makes the world a bit cleaner.

1 Like


Okay yes you need v8.72.2. Try updating?

Ah. I did a Pkg.update() before I posted the questions 5 hours ago. I’m trying again – seems like now Symbolics has been updated from v5.8.0 to v5.9.0 within these 5 hours.

ModelingToolkit does not update, but there is a warning associated with it:

Do ]add ModelingToolkit@8.72.2? Something may be holding it back.

Will try. From Pkg.status():
So there is a new version waiting…

OK – I could uninstall MethodOfLines for now.

Yeah not everything will have updated around the ecosystem yet. You’re on the cutting edge :sweat_smile: . (@xtalax make note)

Cutting edge makes life exciting :stuck_out_tongue:
[I’ve been playing around with `@mtkmodel` for a couple of weeks, based on the `ModelingToolkitStandardLibrary` presentation at JuliaCon 2023 + some input from @baggepinnen .]

remove MethodOfLines enabled updating ModelingToolkit.

Now, @mtkbuild works.

Still, my code doesn’t work yet… I get an error message on None Sym BasicSymbolic doesn't have a name.

Will post in a new thread.