Skip to content

Commit 117bf00

Browse files
Merge pull request #3658 from AayushSabharwal/as/docs
docs: overhaul documentation for v10
2 parents b939fdc + 1daaa32 commit 117bf00

File tree

87 files changed

+2821
-1066
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2821
-1066
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
5151
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
5252
SCCNonlinearSolve = "9dfe8606-65a1-4bb3-9748-cb89d1561431"
5353
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
54+
SciMLPublic = "431bcebd-1456-4ced-9d72-93c2757fff0b"
5455
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
5556
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
5657
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
@@ -145,6 +146,7 @@ Reexport = "0.2, 1"
145146
RuntimeGeneratedFunctions = "0.5.9"
146147
SCCNonlinearSolve = "1.0.0"
147148
SciMLBase = "2.91.1"
149+
SciMLPublic = "1.0.0"
148150
SciMLStructures = "1.7"
149151
Serialization = "1"
150152
Setfield = "0.7, 0.8, 1"
@@ -156,7 +158,7 @@ StochasticDelayDiffEq = "1.10"
156158
StochasticDiffEq = "6.72.1"
157159
SymbolicIndexingInterface = "0.3.39"
158160
SymbolicUtils = "3.26.1"
159-
Symbolics = "6.37"
161+
Symbolics = "6.40"
160162
URIs = "1"
161163
UnPack = "0.1, 1.0"
162164
Unitful = "1.1"

demo.jl

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
macro mtkcompile(ex...)
2+
quote
3+
@mtkbuild $(ex...)
4+
end
5+
end
6+
7+
function mtkcompile(args...; kwargs...)
8+
structural_simplify(args...; kwargs...)
9+
end
10+
11+
#################################
12+
13+
using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq
14+
using ModelingToolkit: t_nounits as t, D_nounits as D
15+
16+
## ODEs
17+
18+
@parameters g
19+
@variables x(t) y(t) λ(t)
20+
eqs = [D(D(x)) ~ λ * x
21+
D(D(y)) ~ λ * y - g
22+
x^2 + y^2 ~ 1]
23+
@mtkbuild pend = System(eqs, t)
24+
prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
25+
26+
sol = solve(prob, FBDF())
27+
28+
## SDEs and unified `System`
29+
30+
@variables x(t) y(t) z(t)
31+
@parameters σ ρ β
32+
@brownian a
33+
34+
eqs = [
35+
D(x) ~ σ * (y - x) + 0.1x * a,
36+
D(y) ~ x *- z) - y + 0.1y * a,
37+
D(z) ~ x * y - β * z + 0.1z * a
38+
]
39+
40+
@mtkbuild sys1 = System(eqs, t)
41+
42+
eqs = [
43+
D(x) ~ σ * (y - x),
44+
D(y) ~ x *- z) - y,
45+
D(z) ~ x * y - β * z
46+
]
47+
48+
noiseeqs = [0.1*x;
49+
0.1*y;
50+
0.1*z;;]
51+
52+
@mtkbuild sys2 = SDESystem(eqs, noiseeqs, t)
53+
54+
u0 = [
55+
x => 1.0,
56+
y => 0.0,
57+
z => 0.0]
58+
59+
p ==> 28.0,
60+
ρ => 10.0,
61+
β => 8 / 3]
62+
63+
sdeprob = SDEProblem(sys1, u0, (0.0, 10.0), p)
64+
sdesol = solve(sdeprob, ImplicitEM())
65+
66+
odeprob = ODEProblem(sys1, u0, (0.0, 10.0), p) # error!
67+
odeprob = ODEProblem(sys1, u0, (0.0, 10.0), p; check_compatibility = false)
68+
69+
@variables x y z
70+
@parameters σ ρ β
71+
72+
# Define a nonlinear system
73+
eqs = [0 ~ σ * (y - x),
74+
y ~ x *- z),
75+
β * z ~ x * y]
76+
@mtkbuild sys = System(eqs)
77+
78+
## ImplicitDiscrete Affects
79+
80+
@parameters g
81+
@variables x(t) y(t) λ(t)
82+
eqs = [D(D(x)) ~ λ * x
83+
D(D(y)) ~ λ * y - g
84+
x^2 + y^2 ~ 1]
85+
c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1]
86+
@mtkbuild pend = System(eqs, t, continuous_events = c_evt)
87+
prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
88+
89+
sol = solve(prob, FBDF())
90+
91+
## `@named` and `ParentScope`
92+
93+
function SysA(; name, var1)
94+
@variables x(t)
95+
return System([D(x) ~ var1], t; name)
96+
end
97+
function SysB(; name, var1)
98+
@variables x(t)
99+
@named subsys = SysA(; var1)
100+
return System([D(x) ~ x], t; systems = [subsys], name)
101+
end
102+
function SysC(; name)
103+
@variables x(t)
104+
@named subsys = SysB(; var1 = x)
105+
return System([D(x) ~ x], t; systems = [subsys], name)
106+
end
107+
@mtkbuild sys = SysC()

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
1717
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1818
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1919
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
20+
OptimizationBase = "bca83a33-5cc9-4baa-983d-23429ab6bcbb"
2021
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
2122
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
2223
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
@@ -29,6 +30,10 @@ SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
2930
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
3031
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
3132

33+
[sources]
34+
ModelingToolkitStandardLibrary = {rev = "mtk-v10", url = "https://github.com/SciML/ModelingToolkitStandardLibrary.jl/"}
35+
OptimizationBase = {rev = "as/mtk-v10", url = "https://github.com/AayushSabharwal/OptimizationBase.jl"}
36+
3237
[compat]
3338
Attractors = "1.24"
3439
BenchmarkTools = "1.3"

docs/pages.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ pages = [
2727
"Advanced Examples" => Any["examples/tearing_parallelism.md",
2828
"examples/sparse_jacobians.md",
2929
"examples/perturbation.md"]],
30-
"Basics" => Any["basics/AbstractSystem.md",
31-
"basics/ContextualVariables.md",
32-
"basics/Variable_metadata.md",
30+
"API" => Any["API/System.md",
31+
"API/variables.md",
32+
"API/model_building.md",
33+
"API/problems.md",
34+
"API/codegen.md",
35+
"API/PDESystem.md"],
36+
"Basics" => Any[
3337
"basics/Composition.md",
3438
"basics/Events.md",
3539
"basics/Linearization.md",
@@ -40,14 +44,6 @@ pages = [
4044
"basics/DependencyGraphs.md",
4145
"basics/Precompilation.md",
4246
"basics/FAQ.md"],
43-
"System Types" => Any["systems/ODESystem.md",
44-
"systems/SDESystem.md",
45-
"systems/JumpSystem.md",
46-
"systems/NonlinearSystem.md",
47-
"systems/OptimizationSystem.md",
48-
"systems/PDESystem.md",
49-
"systems/DiscreteSystem.md",
50-
"systems/ImplicitDiscreteSystem.md"],
5147
"comparison.md",
5248
"internals.md"
5349
]
File renamed without changes.

docs/src/API/System.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# [The `System` type](@id System_type)
2+
3+
ModelingToolkit.jl uses `System` to symbolically represent all types of numerical problems.
4+
Users create `System`s representing the problem they want to solve and `mtkcompile` transforms
5+
them into a format ModelingToolkit.jl can generate code for (alongside performing other
6+
optimizations).
7+
8+
```@docs
9+
System
10+
```
11+
12+
## Utility constructors
13+
14+
Several utility constructors also exist to easily construct alternative system formulations.
15+
16+
```@docs
17+
NonlinearSystem
18+
SDESystem
19+
JumpSystem
20+
OptimizationSystem
21+
```
22+
23+
## Accessor functions
24+
25+
Several accessor functions exist to query systems for the information they contain. In general,
26+
for every field `x` there exists a `has_x` function which checks if the system contains the
27+
field and a `get_x` function for obtaining the value in the field. Note that fields of a system
28+
cannot be accessed via `getproperty` - that is reserved for accessing variables, subsystems
29+
or analysis points of the hierarchical system.
30+
31+
```@docs
32+
ModelingToolkit.has_eqs
33+
ModelingToolkit.get_eqs
34+
equations
35+
equations_toplevel
36+
full_equations
37+
ModelingToolkit.has_noise_eqs
38+
ModelingToolkit.get_noise_eqs
39+
ModelingToolkit.has_jumps
40+
ModelingToolkit.get_jumps
41+
jumps
42+
ModelingToolkit.has_constraints
43+
ModelingToolkit.get_constraints
44+
constraints
45+
ModelingToolkit.has_costs
46+
ModelingToolkit.get_costs
47+
costs
48+
ModelingToolkit.has_consolidate
49+
ModelingToolkit.get_consolidate
50+
ModelingToolkit.has_unknowns
51+
ModelingToolkit.get_unknowns
52+
unknowns
53+
unknowns_toplevel
54+
ModelingToolkit.has_ps
55+
ModelingToolkit.get_ps
56+
parameters
57+
parameters_toplevel
58+
tunable_parameters
59+
ModelingToolkit.has_brownians
60+
ModelingToolkit.get_brownians
61+
brownians
62+
ModelingToolkit.has_iv
63+
ModelingToolkit.get_iv
64+
ModelingToolkit.has_observed
65+
ModelingToolkit.get_observed
66+
observed
67+
observables
68+
ModelingToolkit.has_name
69+
ModelingToolkit.get_name
70+
nameof
71+
ModelingToolkit.has_description
72+
ModelingToolkit.get_description
73+
ModelingToolkit.description
74+
ModelingToolkit.has_defaults
75+
ModelingToolkit.get_defaults
76+
defaults
77+
ModelingToolkit.has_guesses
78+
ModelingToolkit.get_guesses
79+
guesses
80+
ModelingToolkit.has_initialization_eqs
81+
ModelingToolkit.get_initialization_eqs
82+
initialization_equations
83+
ModelingToolkit.has_continuous_events
84+
ModelingToolkit.get_continuous_events
85+
continuous_events
86+
continuous_events_toplevel
87+
ModelingToolkit.has_discrete_events
88+
ModelingToolkit.get_discrete_events
89+
discrete_events_toplevel
90+
ModelingToolkit.has_assertions
91+
ModelingToolkit.get_assertions
92+
assertions
93+
ModelingToolkit.has_metadata
94+
ModelingToolkit.get_metadata
95+
SymbolicUtils.getmetadata(::ModelingToolkit.AbstractSystem, ::DataType, ::Any)
96+
SymbolicUtils.setmetadata(::ModelingToolkit.AbstractSystem, ::DataType, ::Any)
97+
ModelingToolkit.has_is_dde
98+
ModelingToolkit.get_is_dde
99+
ModelingToolkit.is_dde
100+
ModelingToolkit.has_tstops
101+
ModelingToolkit.get_tstops
102+
ModelingToolkit.symbolic_tstops
103+
ModelingToolkit.has_tearing_state
104+
ModelingToolkit.get_tearing_state
105+
ModelingToolkit.does_namespacing
106+
toggle_namespacing
107+
ModelingToolkit.iscomplete
108+
ModelingToolkit.has_preface
109+
ModelingToolkit.get_preface
110+
ModelingToolkit.preface
111+
ModelingToolkit.has_parent
112+
ModelingToolkit.get_parent
113+
ModelingToolkit.has_initializesystem
114+
ModelingToolkit.get_initializesystem
115+
ModelingToolkit.is_initializesystem
116+
```
117+
118+
## `getproperty` syntax
119+
120+
ModelingToolkit allows obtaining in a system using `getproperty`. For a system `sys` with a
121+
subcomponent `inner` containing variable `var`, `sys.inner.var` will obtain the appropriately
122+
namespaced version of `var`. Note that this can also be used to access subsystems (`sys.inner`)
123+
or analysis points.
124+
125+
!!! note
126+
127+
By default, top-level systems not marked as `complete` will apply their namespace. Systems
128+
marked as `complete` will not do this namespacing. This namespacing behavior can be toggled
129+
independently of whether the system is completed using [`toggle_namespacing`](@ref) and the
130+
current namespacing behavior can be queried via [`ModelingToolkit.does_namespacing`](@ref).
131+
132+
```@docs
133+
Base.getproperty(::ModelingToolkit.AbstractSystem, ::Symbol)
134+
```
135+
136+
## Functions for querying system equations
137+
138+
```@docs
139+
has_diff_eqs
140+
has_alg_eqs
141+
get_diff_eqs
142+
get_alg_eqs
143+
has_diff_equations
144+
has_alg_equations
145+
diff_equations
146+
alg_equations
147+
is_alg_equation
148+
is_diff_equation
149+
```
150+
151+
## String parsing
152+
153+
ModelingToolkit can parse system variables from strings.
154+
155+
```@docs
156+
ModelingToolkit.parse_variable
157+
```
158+
159+
## Dumping system data
160+
161+
```@docs
162+
ModelingToolkit.dump_unknowns
163+
ModelingToolkit.dump_parameters
164+
```
165+
166+
```@docs; canonical = false
167+
ModelingToolkit.dump_variable_metadata
168+
```
169+
170+
## Debugging utilities
171+
172+
```@docs
173+
debug_system
174+
```

0 commit comments

Comments
 (0)