# Yet Another Component Example

**URL:** <https://discourse.julialang.org/t/yet-another-component-example/57318>\
**Category:** Modelling & Simulations\
**Created:** [March 16, 2021, 5:28pm UTC](https://discourse.julialang.org/t/yet-another-component-example/57318 "2021-03-16T17:28:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lungd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungd/32/10913_2.png) [@lungd](https://discourse.julialang.org/u/lungd)\
**Post date:** [March 16, 2021, 5:28pm UTC](https://discourse.julialang.org/t/yet-another-component-example/57318/1 "2021-03-16T17:28:06Z")

</div>

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](https://github.com/SciML/ModelingToolkit.jl/issues/814))…

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 / …

> <https://github.com/lungd/ModelingToolkit.jl/blob/components/test/components/full.jl>

> <https://github.com/lungd/ModelingToolkit.jl/blob/components/test/components/concCell/main.jl>

* * *

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

```julia
@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)]`

```julia
# 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](https://github.com/lungd/ModelingToolkit.jl/blob/components/src/systems/diffeqs/odesystem.jl#L297))

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!

---

<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:** [March 18, 2021, 12:13pm UTC](https://discourse.julialang.org/t/yet-another-component-example/57318/2 "2021-03-18T12:13:47Z")

</div>

> [@lungd](#):
>
> 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)]`

Open an issue?
