Skip to content

Commit 8438b67

Browse files
Merge pull request #1333 from baggepinnen/devdocs
Add some documentation of the internals
2 parents 399ff2a + b0eb283 commit 8438b67

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

docs/src/internals.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,18 @@ plotting their output, these relationships are stored and are then used to
1616
generate the `observed` equation found in the `SciMLFunction` interface, so that
1717
`sol[x]` lazily reconstructs the observed variable when necessary. In this sense,
1818
there is an equivalence between observables and the variable elimination system.
19+
20+
The procedure for variable elimination inside [`structural_simplify`](@ref) is
21+
1. [`ModelingToolkit.initialize_system_structure`](@ref).
22+
2. [`ModelingToolkit.alias_elimination`](@ref). This step moves equations into `observed(sys)`.
23+
3. [`ModelingToolkit.dae_index_lowering`](@ref) by means of [`pantelides!`](@ref) (if the system is an [`ODESystem`](@ref)).
24+
4. [`ModelingToolkit.tearing`](@ref).
25+
26+
## Preparing a system for simulation
27+
Before a simulation or optimization can be performed, the symbolic equations stored in an [`AbstractSystem`](@ref) must be converted into executable code. This step is typically occurs after the simplification explained above, and is performed when an instance of a [`AbsSciMLBase.SciMLProblem`](@ref), such as a [`ODEProblem`](@ref), is constructed.
28+
The call chain typically looks like this, with the function names in the case of an `ODESystem` indicated in parenthesis
29+
1. Problem constructor ([`ODEProblem`](@ref))
30+
2. Build an `DEFunction` ([`process_DEProblem`](@ref) -> [`ODEFunction`](@ref)
31+
3. Write actual executable code ([`generate_function`](@ref))
32+
33+
Apart from [`generate_function`](@ref), which generates the dynamics function, `ODEFunction` also builds functions for observed equations (`build_explicit_observed_function`) and jacobians (`generate_jacobian`) etc. These are all stored in the `ODEFunction`.

docs/src/mtkitize_tutorials/modelingtoolkitize_index_reduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ p = [9.8, 1]
2929
tspan = (0, 10.0)
3030
pendulum_prob = ODEProblem(pendulum_fun!, u0, tspan, p)
3131
traced_sys = modelingtoolkitize(pendulum_prob)
32-
pendulum_sys = structural_simplify(dae_index_lowering(traced_sys))
32+
pendulum_sys = structural_simplify(traced_sys)
3333
prob = ODAEProblem(pendulum_sys, Pair[], tspan)
3434
sol = solve(prob, Tsit5(),abstol=1e-8,reltol=1e-8)
3535
plot(sol, vars=states(traced_sys))

src/structural_transformation/codegen.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ struct ODAEProblem{iip}
346346
end
347347

348348
ODAEProblem(args...; kw...) = ODAEProblem{true}(args...; kw...)
349+
350+
"""
351+
ODAEProblem{iip}(sys, u0map, tspan, parammap = DiffEqBase.NullParameters(); kw...)
352+
353+
This constructor acts similar to the one for [`ODEProblem`](@ref) with the following changes:
354+
`ODESystem`s can sometimes be further reduced if `structural_simplify` has
355+
already been applied to them. This is done this constructor.
356+
In these cases, the constructor uses the knowledge of the strongly connected
357+
components calculated during the process of simplification as the basis for
358+
building pre-simplified nonlinear systems in the implicit solving.
359+
In summary: these problems are structurally modified, but could be
360+
more efficient and more stable. Note, the returned object is still of type [`ODEProblem`](@ref).
361+
"""
349362
function ODAEProblem{iip}(
350363
sys,
351364
u0map,

src/structural_transformation/pantelides.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ function pantelides!(sys::ODESystem; maxiters = 8000)
141141
end
142142

143143
"""
144-
dae_index_lowering(sys::ODESystem) -> ODESystem
144+
dae_index_lowering(sys::ODESystem; kwargs...) -> ODESystem
145145
146146
Perform the Pantelides algorithm to transform a higher index DAE to an index 1
147-
DAE.
147+
DAE. `kwargs` are forwarded to [`pantelides!`](@ref). End users are encouraged to call [`structural_simplify`](@ref)
148+
instead, which calls this function internally.
148149
"""
149150
function dae_index_lowering(sys::ODESystem; kwargs...)
150151
s = get_structure(sys)

src/structural_transformation/tearing.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
tear_graph(sys) -> sys
33
4-
Tear the bipartite graph in a system.
4+
Tear the bipartite graph in a system. End users are encouraged to call [`structural_simplify`](@ref)
5+
instead, which calls this function internally.
56
"""
67
function tear_graph(sys)
78
find_solvables!(sys)
@@ -220,6 +221,7 @@ end
220221
tearing(sys; simplify=false)
221222
222223
Tear the nonlinear equations in system. When `simplify=true`, we simplify the
223-
new residual residual equations after tearing.
224+
new residual residual equations after tearing. End users are encouraged to call [`structural_simplify`](@ref)
225+
instead, which calls this function internally.
224226
"""
225227
tearing(sys; simplify=false) = tearing_reassemble(tear_graph(algebraic_equations_scc(sys)); simplify=simplify)

0 commit comments

Comments
 (0)