Skip to content

Commit bdc79de

Browse files
first commit of real documentation
1 parent 2cb2fcb commit bdc79de

18 files changed

+708
-431
lines changed

README.md

Lines changed: 13 additions & 363 deletions
Large diffs are not rendered by default.

docs/make.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Documenter, ModelingToolkit, DiffEqBase
2-
1+
using Documenter, ModelingToolkit
32

43
makedocs(
54
sitename="ModelingToolkit.jl",

docs/src/IR.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# ModelingToolkit IR
2+
3+
ModelingToolkit IR, which falls under the `Expression` abstract type, mirrors
4+
the Julia AST but allows for easy mathematical manipulation by itself following
5+
mathematical semantics. The base of the IR is the `Variable` type which defines
6+
a symbolic variable. These variables are combined using `Operation`s, which are
7+
registered functions applied to the various variables. These `Operation`s then
8+
perform automatic tracing, so normal mathematical functions applied to an `Operation`
9+
generate a new `Operation`. For example, `op1 = x+y` is one `Operation` and
10+
`op2 = 2z` is another, and so `op1*op2` is another `Operation`. Then at the top,
11+
an `Equation`, normally written as `op1 ~ op2`, defines the symbolic equality
12+
between two operations.
13+
14+
### Types
15+
16+
```@docs
17+
Expression
18+
Variable
19+
ModelingToolkit.Constant
20+
Operation
21+
Equation
22+
```
23+
24+
### Function Registration
25+
26+
The ModelingToolkit graph only allowed for registered Julia functions for the
27+
operations. All other functions are automatically traced down to registred
28+
functions. By default, ModelingToolkit.jl pre-registers the common functions
29+
utilized in the AD package ruleset [DiffRules.jl](https://github.com/JuliaDiff/DiffRules.jl)
30+
and pre-defines their derivatives. However, the user can utilize the `@register`
31+
macro to add their function to allowed functions of the computation graph.
32+
33+
```@docs
34+
@register
35+
```
36+
37+
### Derivatives and Differentials
38+
39+
A `Differential(op)` is a partial derivative with respect to the operation `op`
40+
which can then be applied to some other operations. For example `D=Differential(t)`
41+
is what would commonly be referred to as `d/dt`, which can then be applied to
42+
other operations using its function call, so `D(x+y)` is `d(x+y)/dt`.
43+
44+
By default, the derivatives are left unexpanded to capture the symbolic
45+
representation of the differential equation. If the user would like to expand
46+
out all of the differentials, the `expand_derivatives` function eliminates all
47+
of the differentials down to basic one-variable expressions.
48+
49+
```@docs
50+
Differential
51+
expand_derivatives
52+
ModelingToolkit.derivative
53+
```
54+
### Adding Derivatives
55+
56+
There is a large amount of derivatives pre-defined by
57+
[DiffRules.jl](https://github.com/JuliaDiff/DiffRules.jl). Note that `Expression`
58+
types are defined as `<:Real`, and thus any functions which allow the use of real
59+
numbers can automatically be traced by the derivative mechanism. Thus for example:
60+
61+
```julia
62+
f(x,y,z) = x^2 + sin(x+y) - z
63+
```
64+
65+
automatically has the derivatives defined via the tracing mechanism. It will do
66+
this by directly building the operation the internals of your function and
67+
differentiating that.
68+
69+
However, in many cases you may want to define your own derivatives so that way
70+
automatic Jacobian etc. calculations can utilize this information. This can
71+
allow for more succinct versions of the derivatives to be calculated in order
72+
to better scale to larger systems. You can define derivatives for your own
73+
function via the dispatch:
74+
75+
```julia
76+
# `N` arguments are accepted by the relevant method of `my_function`
77+
ModelingToolkit.derivative(::typeof(my_function), args::NTuple{N,Any}, ::Val{i})
78+
```
79+
80+
where `i` means that it's the derivative of the `i`th argument. `args` is the
81+
array of arguments, so for example if your function is `f(x,t)` then `args = [x,t]`.
82+
You should return an `Operation` for the derivative of your function.
83+
84+
For example, `sin(t)`'s derivative (by `t`) is given by the following:
85+
86+
```julia
87+
ModelingToolkit.derivative(::typeof(sin), args::NTuple{1,Any}, ::Val{1}) = cos(args[1])
88+
```
89+
90+
### IR Manipulation
91+
92+
ModelingToolkit.jl provides functionality for easily manipulating `Expression`
93+
types. Most of the functionality comes by the `Expression` type obeying the
94+
standard mathematical semantics. For example, if one has `A` a matrix of
95+
`Expression`, then `A^2` calculates the `Expression`s for the squared matrix.
96+
In that sense, it is encouraged that one uses standard Julia for performing a
97+
lot of the manipulation on the IR, as for example calculating the sparse form
98+
of the matrix via `sparse(A)` is valid, legible, and easily understandable
99+
to all Julia programmers.
100+
101+
Other additional manipulation functions are given below.
102+
103+
```@docs
104+
simplify_constants
105+
rename
106+
get_variables
107+
substitute_expr!
108+
```
109+
110+
### Expression Generation and `build_function`
111+
112+
At any time, Julia expressions can be generated from ModelingToolkit IR by using
113+
`convert(Expr,x)`. This performs some cleaning to return an expression without
114+
extraneous pieces that commonly matches expressions one would write in functions
115+
like those for differential equation solvers and optimization libraries.
116+
117+
Additionally, the core compilation process of ModelingToolkit IR is `build_function`.
118+
`build_function` takes an operation or an `AbstractArray` of operations and
119+
generates a compile-able version of the model for numerical solvers.
120+
121+
```@docs
122+
build_function
123+
```

docs/src/api.md

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)