Skip to content

Commit 9938bd8

Browse files
ChrisRackauckasshashi
authored andcommitted
automatic equation reduction
1 parent 3454522 commit 9938bd8

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

test/reduction.jl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using ModelingToolkit, OrdinaryDiffEq, Test
2+
3+
@parameters t σ ρ β
4+
@variables x(t) y(t) z(t) a(t)
5+
@derivatives D'~t
6+
7+
eqs = [D(x) ~ σ*(y-x),
8+
D(y) ~ x*-z)-y,
9+
D(z) ~ a*y - β*z,
10+
0 ~ x - a]
11+
12+
lorenz1 = ODESystem(eqs,t,[x,y,z,a],[σ,ρ,β],name=:lorenz1)
13+
14+
lorenz1_aliased = alias_elimination(lorenz1)
15+
length(equations(lorenz1_reduced)) = 3
16+
length(states(lorenz1_reduced)) = 3
17+
18+
eqs = [D(x) ~ σ*(y-x),
19+
D(y) ~ x*-z)-y,
20+
D(z) ~ x*y - β*z]
21+
test_lorenz1_aliased = ODESystem(eqs,t,[x,y,z],[σ,ρ,β],observed=[a ~ x],name=:lorenz1)
22+
23+
# Multi-System Reduction
24+
25+
eqs1 = [D(x) ~ σ*(y-x) + F,
26+
D(y) ~ x*-z)-u,
27+
D(z) ~ x*y - β*z]
28+
aliases = [u ~ x + y - z]
29+
lorenz1 = ODESystem(eqs1,pins=[F],observed=aliases,name=:lorenz1)
30+
31+
eqs2 = [D(x) ~ F,
32+
D(y) ~ x*-z)-x,
33+
D(z) ~ x*y - β*z]
34+
aliases2 = [u ~ x - y - z]
35+
lorenz2 = ODESystem(eqs2,pins=[F],observed=aliases2,name=:lorenz2)
36+
37+
connections = [lorenz1.F ~ lorenz2.u,
38+
lorenz2.F ~ lorenz1.u]
39+
connected = ODESystem([0 ~ a + lorenz1.x - lorenz2.y],t,[a],[],observed=connections,systems=[lorenz1,lorenz2])
40+
41+
# Reduced Unflattened System
42+
43+
connections2 = [lorenz1.F ~ lorenz2.u,
44+
lorenz2.F ~ lorenz1.u,
45+
a ~ -lorenz1.x + lorenz2.y]
46+
connected = ODESystem(Equation[],t,[],[],observed=connections2,systems=[lorenz1,lorenz2])
47+
48+
# Reduced Flattened System
49+
50+
flattened_system = flatten(connected)
51+
flatten(sys::AbstractSystem) = ODESystem(equations(sys),states(sys),parameters(sys),independent_variable(sys))
52+
53+
aliased_flattened_system = alias_elimination(flattened_system)
54+
55+
states(reduced_flattened_system) == [
56+
lorenz1.x
57+
lorenz1.y
58+
lorenz1.z
59+
lorenz2.x
60+
lorenz2.y
61+
lorenz2.z
62+
]
63+
64+
parameters(reduced_flattened_system) == [
65+
lorenz1.σ
66+
lorenz1.ρ
67+
lorenz1.β
68+
lorenz2.σ
69+
lorenz2.ρ
70+
lorenz2.β
71+
]
72+
73+
equations(reduced_flattened_system) == [
74+
D(lorenz1.x) ~ lorenz1.σ*(lorenz1.y-lorenz1.x) + lorenz2.x - lorenz2.y - lorenz2.z,
75+
D(lorenz1.y) ~ lorenz1.x*-z)-lorenz1.x - lorenz1.y + lorenz1.z,
76+
D(lorenz1.z) ~ lorenz1.x*lorenz1.y - lorenz1.β*lorenz1.z
77+
D(lorenz2.x) ~ lorenz1.x + lorenz1.y - lorenz1.z,
78+
D(lorenz2.y) ~ lorenz2.x*(lorenz2.ρ-lorenz2.z)-lorenz2.x,
79+
D(lorenz2.z) ~ lorenz2.x*lorenz2.y - lorenz2.β*lorenz2.z]
80+
81+
observed(reduced_flattened_system) == [
82+
lorenz1.F ~ lorenz2.u
83+
lorenz2.F ~ lorenz1.u
84+
a ~ -lorenz1.x + lorenz2.y
85+
]

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ using SafeTestsets, Test
1919
@safetestset "Build Targets Test" begin include("build_targets.jl") end
2020
@safetestset "Domain Test" begin include("domains.jl") end
2121
@safetestset "Constraints Test" begin include("constraints.jl") end
22+
@safetestset "Reduction Test" begin include("reduction.jl") end
2223
@safetestset "PDE Construction Test" begin include("pde.jl") end
2324
@safetestset "Lowering Integration Test" begin include("lowering_solving.jl") end
2425
@safetestset "Test Big System Usage" begin include("bigsystem.jl") end

0 commit comments

Comments
 (0)