Skip to content

Accessors for differential and algebraic erquations #2601

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

Merged
merged 4 commits into from
Apr 2, 2024

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Apr 2, 2024

Creates 10 different general purpose accessors for handling differential and algebraic functions within systems:

is_alg_equation
is_diff_equation
alg_equations
diff_equations
has_alg_equations
has_diff_equations
get_alg_eqs
get_diff_eqs
has_alg_eqs
has_diff_eqs

Examples:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
@parameters p d
@variables X(t)
eq1 = D(X) ~ p - d*X
eq2 = 0 ~ p - d*X
@named osys1 = ODESystem([eq1], t)
@named osys2 = ODESystem([eq2], t)
osys12 = compose(osys1, [osys2])
osys21 = compose(osys2, [osys1])

is_alg_equation(eq1) # returns `false`.
is_alg_equation(eq2) # returns `true`.
is_diff_equation(eq1) # returns `true`.
is_diff_equation(eq2) # returns `false`.

has_alg_equations(osys1) # returns `false`.
has_alg_equations(osys2) # returns `true`.
has_diff_equations(osys1) # returns `true`.
has_diff_equations(osys2) # returns `false`.

alg_equations(osys1) # returns `Equation[]`.
alg_equations(osys2) # returns `[ 0 ~ p - d*X(t)]`.
diff_equations(osys1) # returns `[Differential(t)(X(t)) ~ p - d*X(t)]`.
diff_equations(osys2) # returns `Equation[]`.

get_alg_eqs(osys12) # returns `Equation[]`.
get_alg_eqs(osys21) # returns `[0 ~ p - d*X(t)]`.
get_diff_eqs(osys12) # returns `Differential(t)(X(t)) ~ p - d*X(t)`.
get_diff_eqs(osys21) # returns `Equation[]`.

has_alg_eqs(osys12) # returns `true`.
has_alg_eqs(osys21) # returns `false`.
has_diff_eqs(osys12) # returns `true`.
has_diff_eqs(osys21) # returns `false`.

There are docstrings and tests for all functions.

These are needed in Catalyst v14 to handle equations when you convert to ODESystems that may (or may not) contain algebraic equations. Sincet hey are general-purpose, I figured it was better to put them here than in Catalyst.

@ChrisRackauckas
Copy link
Member

Format and document

@TorkelE
Copy link
Member Author

TorkelE commented Apr 2, 2024

Got docstrings, so can link them in the docs, where would you want them? In the long term (as part of this PR or not), would it be useful to have an "API" page on the docs which just lists functions like these and their docstrings?

@ChrisRackauckas
Copy link
Member

No, API dumps are always bad. Make a proper manual page on equations. Add the has_algebraic and such to the right system docs pages (ODESystem, SDESystem, etc.)

@TorkelE
Copy link
Member Author

TorkelE commented Apr 2, 2024

Ok, I have added docs and run the formater.

Comment on lines +16 to +24
- `continuous_events(sys)`: The set of continuous events in the ODE.
- `discrete_events(sys)`: The set of discrete events in the ODE.
- `alg_equations(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE.
- `get_alg_eqs(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE. Only returns equations of the current-level system.
- `diff_equations(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE.
- `get_diff_eqs(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE. Only returns equations of the current-level system.
- `has_alg_equations(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential).
- `has_alg_eqs(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential). Only considers the current-level system.
- `has_diff_equations(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the new non-canonical, but we can handle that in a future PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for helping getting this one sorted out quickly!

@ChrisRackauckas ChrisRackauckas merged commit 4b06356 into SciML:master Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants