Yet Another Component Example

As far as I know, there are some WIP tutorials describing how to create components and how they can be merged. I was not sure where to put the following, maybe it would fit better into an existing github issue (e.g. Connectors · Issue #814 · SciML/ModelingToolkit.jl · GitHub)…

I want to share my attempt to define a model of a biological neural network created with reusable components. (Hodgkin-Huxley model, or a custom (WIP) model including some ionic concentrations)
Components are: Networks / Cells / Compartments (soma) / Ion channels / Synapses / …


Let’s assume the following model: net.cell.soma
soma has a variable I_syn(t)
soma.eqs = [I_syn(t) ~ 1.0] # default equation / mapping

I can add a new system syn to net with

@named syn = Syn()
insert_comp!(net, syn, [
syn.v_pre ~ net.cell.soma.v,
syn.v_post ~ net.cell.soma.v,
net.cell.soma.I_syn ~ syn.I])

This will not only push the new sys and equation but also “save and remove” existing eqs from soma and the parent systems (cell, net)
The soma’s “I_syn-equation” has to be removed, otherwise structural_simplify() will fail due to multiple equations for the same variable: soma.eq = [I_syn(t) ~ 1.0] and net.eqs = [cell₊soma₊I_syn(t) ~ syn₊I(t)]

# after insert_comp!()
# soma.eqs=[  ] # it's actually [get_iv(soma) ~ get_iv(soma)] because I didn't know how to remove an eq
# net.system = [cell, syn]
# net.eqs = [syn₊v_pre(t) ~ cell₊soma₊v(t),
#syn₊v_post(t) ~ cell.soma.v(t),
#cell₊soma₊I_syn(t) ~ syn₊I(t) + 1.0]

To make this work, I had to apply some changes to MTK.jl (ModelingToolkit.jl/odesystem.jl at components · lungd/ModelingToolkit.jl · GitHub)

The code needs some serious refactoring but I would already like to get some feedback for that approach to add components and connecting them with some additional equations.
Thanks in advance!

Open an issue?