# Implementation of custom block in Modeling Toolkit

**URL:** https://discourse.julialang.org/t/implementation-of-custom-block-in-modeling-toolkit/128212
**Category:** New to Julia
**Tags:** modelingtoolkit
**Created:** [April 19, 2025, 8:29am UTC](https://discourse.julialang.org/t/implementation-of-custom-block-in-modeling-toolkit/128212 "2025-04-19T08:29:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)
#### Post date: [April 19, 2025, 8:29am UTC](https://discourse.julialang.org/t/implementation-of-custom-block-in-modeling-toolkit/128212/1 "2025-04-19T08:29:06Z")

</div>

Hello everyone,

As an exercise to learn how to use Modeling Toolkit, I set out to ‘translate’ some of the models I had implemented in Simulink. So far, I’ve managed to implement everything perfectly, but there’s one function I haven’t been able to replicate in Modeling Toolkit. This function is the following:

**If** `input == 1` → `output = 1`

**If** `input` **changes to 0** , **then** :

- `output = 2` **for 𝜏 seconds** (𝜏 is a parameter),
- **then** `output = 0`.

It would be a great help if someone could provide suggestions on how to implement this model using the `@mtkmodel` macro. I’ve attached a diagram of the system’s operation for clarity. Thanks in advance!"

 ![doubt](https://global.discourse-cdn.com/julialang/original/3X/7/3/73abd777018b368645ffc7fb5d9ef8b60088c2e7.png)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [April 19, 2025, 6:29pm UTC](https://discourse.julialang.org/t/implementation-of-custom-block-in-modeling-toolkit/128212/2 "2025-04-19T18:29:55Z")

</div>

What did you try?

If you don’t try yourself the chance is low that other people write code for you. You could also ask an AI tool like perplexity.ai first.

---

<div class="post-metadata">

### Author: ![JaviJR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/javijr/32/218294_2.png) [@JaviJR](https://discourse.julialang.org/u/JaviJR)
#### Post date: [April 20, 2025, 9:22am UTC](https://discourse.julialang.org/t/implementation-of-custom-block-in-modeling-toolkit/128212/3 "2025-04-20T09:22:23Z")

</div>

Hello,  
You’re absolutely right — yesterday I wrote the question quickly and forgot to attach the code I had tried. Luckily, after thinking about it more this morning, I found a way to implement this functionality. I’m attaching the code here in case anyone needs to implement something similar.

```julia
@mtkmodel ResponseSystemSub begin
    @parameters begin

        delay = 3.0 # Delay of system

    end

	@components begin 

		output = RealOutput()
		input = RealInput()

	end
    
    @variables begin

        timer(t) = 0.0 # Timer
        phase(t) = 0.0 # Internal state

    end
    
    @equations begin

        # Timer increases only if phase==2
        D(timer) ~ ifelse(phase == 2,1.0,0.0)
        
        # Phase rate of change is 0, this variable will only be updated through discrete events!
        D(phase) ~ 0

        # output logic
    
	 output.u ~ ifelse(phase==0,0.0,ifelse(phase==1,1.0,2.0))

	end

	@discrete_events begin

		(input.u - 0.5 > 0) & (phase == 0) => [phase ~ 1.0, timer ~ 0.0]
		(input.u - 0.5 < 0) & (phase == 1) => [phase ~ 2.0, timer ~ 0.0]
		(timer - delay > 0) & (phase == 2) => [phase ~ 0.0, timer ~ 0.0]
		
	end

end

```

There are other causal implementations of this kind of system, such as the one found in IEC 61400-27, however, several auxiliary functions must be implemented. This solution works and is quite simple. If anyone finds another way to implement the same logic or finds a way to improve the model I will be glad to discuss it!

Thanks.
