Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events for @mtkmodel components #2421

Open
cgutsche opened this issue Jan 10, 2024 · 5 comments
Open

Events for @mtkmodel components #2421

cgutsche opened this issue Jan 10, 2024 · 5 comments
Labels

Comments

@cgutsche
Copy link
Contributor

In component based modeling your components might need to do event handeling. These events should be declarable within the component for easy modeling. As far as I can understand the code, there is no nice way to do this at the moment. Therefore I think additional syntax like the following would be nice:

@mtkmodel FOL begin
    @parameters begin
        τ # parameters
    end
    @variables begin
        x(t) # dependent variables
    end
    @equations begin
        D(x) ~ (1 - x) / τ
    end
    @discrete_events begin
        (t > 5) => [x ~ x + 1]
    end
    @continuous_events begin
        [t ~ 1] => [x ~ x + 1]
    end
end

I think this would be the easiest way to do it. Maybe a more Modelica like syntax including the events into the equation block would be possible but I think this is not necessary.

@cgutsche
Copy link
Contributor Author

Hey, me again.

I implemented the feature by changing model_parsing.jl, you can have a look here:
https://github.com/cgutsche/ModelingToolkit.jl/tree/dev-event-support

I tested the implementation with the following code and it worked:

@variables t
D = Differential(t)

@mtkmodel M begin
	@parameters begin 
		b[1:2]
	end
	@variables begin
		f(t)
		x(t)
	end
	@equations begin
		b[1] * f ~ D(f)
		b[2] * x ~ D(x)
	end
	@continuous_events begin
		[x ~ 0.9] => [b[2] ~ 5]
		[t ~ 0.5] => [x ~ 1]
		[t ~ 0.75] => [x ~ 1, f ~ f + 1]
	end
	@discrete_events begin
		(t == 0.8) => [b[2] ~ 2]
		(t == 0.5) => [f ~ f + 1]
	end
end

@mtkmodel N begin
	@components begin
		m = M(; b=[0, 1])
	end
	@parameters begin
		ω
	end
	@variables begin
		y(t)
		v(t)
	end 
	@equations begin
		v ~ D(y)
		ω*y ~ -D(v)
	end
	@continuous_events begin
		[t ~ 1.5] => [y ~ 5]
	end
end


@mtkbuild model = N(; ω=10)

u0 = [model.y => 1.0, model.v => 0, model.m.f => 1.0, model.m.x => 10]
prob = ODEProblem(model, u0, (0, 2.0))
sol = solve(prob, FBDF(), tstops=[0.5, 0.8])

p = plot(sol)
display(p)

However, I needed to add some if statements to create specific ODESystem Expressions for different cases where events are defined or not. (see line 99...)
Maybe there is a better way to do that?

@cgutsche cgutsche closed this as not planned Won't fix, can't repro, duplicate, stale Jan 11, 2024
@cgutsche
Copy link
Contributor Author

accidently closed the issue, oops

@cgutsche cgutsche reopened this Jan 11, 2024
@cgutsche
Copy link
Contributor Author

Okay, I think I found the best way to avoid the if statements. See the solution here:
https://github.com/cgutsche/ModelingToolkit.jl/tree/dev-add-event-support-mtkmodel

@SLiemann
Copy link

SLiemann commented Feb 9, 2024

Hi,

would the event support for @mtkmodel also include the generalized affect functions like in https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#func_affects ?

Does #2427 respect this?

Thanks for any comments.

@cgutsche
Copy link
Contributor Author

cgutsche commented Feb 9, 2024

Hey,

would the event support for @mtkmodel also include the generalized affect functions like in https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#func_affects ?

Does #2427 respect this?

Yes, generalized affect functions are supported too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants