Skip to content

Commit f86cc6a

Browse files
start composition discussion
1 parent 230d44f commit f86cc6a

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

docs/src/basics/Composition.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
11
# [Composing Models and Building Reusable Components](@ref components)
22

3-
## The `alias_elimination` Transformation
3+
The symbolic models of ModelingToolkit can be composed together to
4+
easily build large models. The composition is lazy and only instantiated
5+
at the time of conversion to numerical models, allowing a more performant
6+
way in terms of computation time and memory.
7+
8+
## Basics of Model Composition
9+
10+
Every `AbstractSystem` has a `system` keyword argument for specifying
11+
subsystems. A model is the composition of itself and its subsystems.
12+
For example, if we have:
13+
14+
```julia
15+
@named sys = ODESystem(eqs,indepvar,states,ps,system=[subsys])
16+
```
17+
18+
the `equations` of `sys` is the concatenation of `get_eqs(sys)` and
19+
`equations(subsys)`, the states are the concatenation of their states,
20+
etc. When the `ODEProblem` or `ODEFunction` is generated from this
21+
system, it will build and compile the functions associated with this
22+
composition.
23+
24+
The new equations within the higher level system can access the variables
25+
in the lower level system by namespacing via the `nameof(subsys)`. For
26+
example, let's say there is a variable `x` in `states` and a variable
27+
`x` in `subsys`. We can declare that these two variables are the same
28+
by specifying their equality: `x ~ subsys.x` in the `eqs` for `sys`.
29+
This algebraic relationship can then be simplified by transformations
30+
like `alias_elimination` or `tearing` which will be described later.
31+
32+
### Simple Model Composition Example
33+
34+
The following is an example of building a model in a library with
35+
an optional forcing function, and allowing the user to specify the
36+
forcing later. Here, the library author defines a component named
37+
`decay`. The user then builds two `decay` components and connects them,
38+
saying the forcing term of `decay1` is a constant while the forcing term
39+
of `decay2` is the value of the state variable `x`.
40+
41+
```julia
42+
function decay(;name)
43+
@parameters t a
44+
@variables x(t) f(t)
45+
D = Differential(t)
46+
ODESystem([
47+
D(x) ~ -a*x + f
48+
];
49+
name=name)
50+
end
51+
52+
@named decay1 = decay()
53+
@named decay2 = decay()
54+
55+
@parameters t
56+
D = Differential(t)
57+
connected = ODESystem([
58+
decay2.f ~ decay1.x
59+
D(decay1.f) ~ 0
60+
], t, systems=[decay1, decay2])
61+
```
62+
63+
## Combine
64+
65+
## Alias Elimination
66+
67+
## The Tearing Transformation
68+
69+
## Automatic Model Promotion

0 commit comments

Comments
 (0)