Skip to content

Commit 4b06356

Browse files
Merge pull request #2601 from TorkelE/new_equation_accessors
Accessors for differential and algebraic erquations
2 parents 8bfba3e + 751c35c commit 4b06356

File tree

6 files changed

+441
-4
lines changed

6 files changed

+441
-4
lines changed

docs/src/systems/ODESystem.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ ODESystem
1313
- `get_ps(sys)` or `parameters(sys)`: The parameters of the ODE.
1414
- `get_iv(sys)`: The independent variable of the ODE.
1515
- `get_u0_p(sys, u0map, parammap)` Numeric arrays for the initial condition and parameters given `var => value` maps.
16-
- `continuous_events(sys)`: The set of continuous events in the ODE
17-
- `discrete_events(sys)`: The set of discrete events in the ODE
16+
- `continuous_events(sys)`: The set of continuous events in the ODE.
17+
- `discrete_events(sys)`: The set of discrete events in the ODE.
18+
- `alg_equations(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE.
19+
- `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.
20+
- `diff_equations(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE.
21+
- `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.
22+
- `has_alg_equations(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential).
23+
- `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.
24+
- `has_diff_equations(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential).
25+
- `has_diff_eqs(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). Only considers the current-level system.
1826

1927
## Transformations
2028

docs/src/systems/SDESystem.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ sde = SDESystem(ode, noiseeqs)
1919
- `get_unknowns(sys)` or `unknowns(sys)`: The set of unknowns in the SDE.
2020
- `get_ps(sys)` or `parameters(sys)`: The parameters of the SDE.
2121
- `get_iv(sys)`: The independent variable of the SDE.
22-
- `continuous_events(sys)`: The set of continuous events in the SDE
23-
- `discrete_events(sys)`: The set of discrete events in the SDE
22+
- `continuous_events(sys)`: The set of continuous events in the SDE.
23+
- `discrete_events(sys)`: The set of discrete events in the SDE.
24+
- `alg_equations(sys)`: The algebraic equations (i.e. that does not contain a differential) that defines the ODE.
25+
- `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.
26+
- `diff_equations(sys)`: The differential equations (i.e. that contain a differential) that defines the ODE.
27+
- `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.
28+
- `has_alg_equations(sys)`: Returns `true` if the ODE contains any algebraic equations (i.e. that does not contain a differential).
29+
- `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.
30+
- `has_diff_equations(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential).
31+
- `has_diff_eqs(sys)`: Returns `true` if the ODE contains any differential equations (i.e. that does contain a differential). Only considers the current-level system.
2432

2533
## Transformations
2634

src/ModelingToolkit.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export build_function
258258
export modelingtoolkitize
259259
export generate_initializesystem
260260

261+
export alg_equations, diff_equations, has_alg_equations, has_diff_equations
262+
export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs
263+
261264
export @variables, @parameters, @constants, @brownian
262265
export @named, @nonamespace, @namespace, extend, compose, complete
263266
export debug_system

src/systems/abstractsystem.jl

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,3 +2384,238 @@ function dump_unknowns(sys::AbstractSystem)
23842384
meta
23852385
end
23862386
end
2387+
2388+
### Functions for accessing algebraic/differential equations in systems ###
2389+
2390+
"""
2391+
is_diff_equation(eq)
2392+
2393+
Returns `true` if the input is a differential equation, i.e. is an equatation that contain some
2394+
form of differential.
2395+
2396+
Example:
2397+
```julia
2398+
using ModelingToolkit
2399+
using ModelingToolkit: t_nounits as t, D_nounits as D
2400+
@parameters p d
2401+
@variables X(t)
2402+
eq1 = D(X) ~ p - d*X
2403+
eq2 = 0 ~ p - d*X
2404+
2405+
is_diff_equation(eq1) # true
2406+
is_diff_equation(eq2) # false
2407+
```
2408+
"""
2409+
function is_diff_equation(eq)
2410+
(eq isa Equation) || (return false)
2411+
isdefined(eq, :lhs) && occursin(is_derivative, wrap(eq.lhs)) && (return true)
2412+
isdefined(eq, :rhs) && occursin(is_derivative, wrap(eq.rhs)) && (return true)
2413+
return false
2414+
end
2415+
2416+
"""
2417+
is_alg_equation(eq)
2418+
2419+
Returns `true` if the input is an algebraic equation, i.e. is an equatation that does not contain
2420+
any differentials.
2421+
2422+
Example:
2423+
```julia
2424+
using ModelingToolkit
2425+
using ModelingToolkit: t_nounits as t, D_nounits as D
2426+
@parameters p d
2427+
@variables X(t)
2428+
eq1 = D(X) ~ p - d*X
2429+
eq2 = 0 ~ p - d*X
2430+
2431+
is_alg_equation(eq1) # false
2432+
is_alg_equation(eq2) # true
2433+
```
2434+
"""
2435+
function is_alg_equation(eq)
2436+
return (eq isa Equation) && !is_diff_equation(eq)
2437+
end
2438+
2439+
"""
2440+
alg_equations(sys::AbstractSystem)
2441+
2442+
For a system, returns a vector of all its algebraic equations (i.e. that does not contain any
2443+
differentials).
2444+
2445+
Example:
2446+
```julia
2447+
using ModelingToolkit
2448+
using ModelingToolkit: t_nounits as t, D_nounits as D
2449+
@parameters p d
2450+
@variables X(t)
2451+
eq1 = D(X) ~ p - d*X
2452+
eq2 = 0 ~ p - d*X
2453+
@named osys = ODESystem([eq1, eq2], t)
2454+
2455+
alg_equations(osys) # returns `[0 ~ p - d*X(t)]`.
2456+
"""
2457+
alg_equations(sys::AbstractSystem) = filter(is_alg_equation, equations(sys))
2458+
2459+
"""
2460+
diff_equations(sys::AbstractSystem)
2461+
2462+
For a system, returns a vector of all its differential equations (i.e. that does contain a differential).
2463+
2464+
Example:
2465+
```julia
2466+
using ModelingToolkit
2467+
using ModelingToolkit: t_nounits as t, D_nounits as D
2468+
@parameters p d
2469+
@variables X(t)
2470+
eq1 = D(X) ~ p - d*X
2471+
eq2 = 0 ~ p - d*X
2472+
@named osys = ODESystem([eq1, eq2], t)
2473+
2474+
diff_equations(osys) # returns `[Differential(t)(X(t)) ~ p - d*X(t)]`.
2475+
"""
2476+
diff_equations(sys::AbstractSystem) = filter(is_diff_equation, equations(sys))
2477+
2478+
"""
2479+
has_alg_equations(sys::AbstractSystem)
2480+
2481+
For a system, returns true if it contain at least one algebraic equation (i.e. that does not contain any
2482+
differentials).
2483+
2484+
Example:
2485+
```julia
2486+
using ModelingToolkit
2487+
using ModelingToolkit: t_nounits as t, D_nounits as D
2488+
@parameters p d
2489+
@variables X(t)
2490+
eq1 = D(X) ~ p - d*X
2491+
eq2 = 0 ~ p - d*X
2492+
@named osys1 = ODESystem([eq1], t)
2493+
@named osys2 = ODESystem([eq2], t)
2494+
2495+
has_alg_equations(osys1) # returns `false`.
2496+
has_alg_equations(osys2) # returns `true`.
2497+
```
2498+
"""
2499+
has_alg_equations(sys::AbstractSystem) = any(is_alg_equation, equations(sys))
2500+
2501+
"""
2502+
has_diff_equations(sys::AbstractSystem)
2503+
2504+
For a system, returns true if it contain at least one differential equation (i.e. that contain a differential).
2505+
2506+
Example:
2507+
```julia
2508+
using ModelingToolkit
2509+
using ModelingToolkit: t_nounits as t, D_nounits as D
2510+
@parameters p d
2511+
@variables X(t)
2512+
eq1 = D(X) ~ p - d*X
2513+
eq2 = 0 ~ p - d*X
2514+
@named osys1 = ODESystem([eq1], t)
2515+
@named osys2 = ODESystem([eq2], t)
2516+
2517+
has_diff_equations(osys1) # returns `true`.
2518+
has_diff_equations(osys2) # returns `false`.
2519+
```
2520+
"""
2521+
has_diff_equations(sys::AbstractSystem) = any(is_diff_equation, equations(sys))
2522+
2523+
"""
2524+
get_alg_eqs(sys::AbstractSystem)
2525+
2526+
For a system, returns a vector of all algebraic equations (i.e. that does not contain any
2527+
differentials) in its *top-level system*.
2528+
2529+
Example:
2530+
```julia
2531+
using ModelingToolkit
2532+
using ModelingToolkit: t_nounits as t, D_nounits as D
2533+
@parameters p d
2534+
@variables X(t)
2535+
eq1 = D(X) ~ p - d*X
2536+
eq2 = 0 ~ p - d*X
2537+
@named osys1 = ODESystem([eq1], t)
2538+
@named osys2 = ODESystem([eq2], t)
2539+
osys12 = compose(osys1, [osys2])
2540+
osys21 = compose(osys2, [osys1])
2541+
2542+
get_alg_eqs(osys12) # returns `Equation[]`.
2543+
get_alg_eqs(osys21) # returns `[0 ~ p - d*X(t)]`.
2544+
```
2545+
"""
2546+
get_alg_eqs(sys::AbstractSystem) = filter(is_alg_equation, get_eqs(sys))
2547+
2548+
"""
2549+
get_diff_eqs(sys::AbstractSystem)
2550+
2551+
For a system, returns a vector of all differential equations (i.e. that does contain a differential)
2552+
in its *top-level system*.
2553+
2554+
Example:
2555+
```julia
2556+
using ModelingToolkit
2557+
using ModelingToolkit: t_nounits as t, D_nounits as D
2558+
@parameters p d
2559+
@variables X(t)
2560+
eq1 = D(X) ~ p - d*X
2561+
eq2 = 0 ~ p - d*X
2562+
@named osys1 = ODESystem([eq1], t)
2563+
@named osys2 = ODESystem([eq2], t)
2564+
osys12 = compose(osys1, [osys2])
2565+
osys21 = compose(osys2, [osys1])
2566+
2567+
get_diff_eqs(osys12) # returns `[Differential(t)(X(t)) ~ p - d*X(t)]`.
2568+
get_diff_eqs(osys21) # returns `Equation[]``.
2569+
```
2570+
"""
2571+
get_diff_eqs(sys::AbstractSystem) = filter(is_diff_equation, get_eqs(sys))
2572+
2573+
"""
2574+
has_alg_eqs(sys::AbstractSystem)
2575+
2576+
For a system, returns true if it contain at least one algebraic equation (i.e. that does not contain any
2577+
differentials) in its *top-level system*.
2578+
2579+
Example:
2580+
```julia
2581+
using ModelingToolkit
2582+
using ModelingToolkit: t_nounits as t, D_nounits as D
2583+
@parameters p d
2584+
@variables X(t)
2585+
eq1 = D(X) ~ p - d*X
2586+
eq2 = 0 ~ p - d*X
2587+
@named osys1 = ODESystem([eq1], t)
2588+
@named osys2 = ODESystem([eq2], t)
2589+
osys12 = compose(osys1, [osys2])
2590+
osys21 = compose(osys2, [osys1])
2591+
2592+
has_alg_eqs(osys12) # returns `false`.
2593+
has_alg_eqs(osys21) # returns `true`.
2594+
```
2595+
"""
2596+
has_alg_eqs(sys::AbstractSystem) = any(is_alg_equation, get_eqs(sys))
2597+
2598+
"""
2599+
has_diff_eqs(sys::AbstractSystem)
2600+
2601+
For a system, returns true if it contain at least one differential equation (i.e. that contain a
2602+
differential) in its *top-level system*.
2603+
2604+
Example:
2605+
```julia
2606+
using ModelingToolkit
2607+
using ModelingToolkit: t_nounits as t, D_nounits as D
2608+
@parameters p d
2609+
@variables X(t)
2610+
eq1 = D(X) ~ p - d*X
2611+
eq2 = 0 ~ p - d*X
2612+
@named osys1 = ODESystem([eq1], t)
2613+
@named osys2 = ODESystem([eq2], t)
2614+
osys12 = compose(osys1, [osys2])
2615+
osys21 = compose(osys2, [osys1])
2616+
2617+
has_diff_eqs(osys12) # returns `true`.
2618+
has_diff_eqs(osys21) # returns `false`.
2619+
```
2620+
"""
2621+
has_diff_eqs(sys::AbstractSystem) = any(is_diff_equation, get_eqs(sys))

0 commit comments

Comments
 (0)