Skip to content

Commit de06aae

Browse files
Merge pull request #125 from jlumpe/docs
Start on API documentation
2 parents cc5df4c + 978aca7 commit de06aae

13 files changed

+397
-2
lines changed

Diff for: Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.3.0"
66
[deps]
77
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
88
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
9+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
910
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1011
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1112
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"

Diff for: docs/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
site/

Diff for: docs/make.jl

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Documenter, ModelingToolkit, DiffEqBase
2+
3+
4+
makedocs(
5+
sitename="ModelingToolkit.jl",
6+
modules=[ModelingToolkit],
7+
pages=[
8+
"Home" => "index.md",
9+
"api.md",
10+
]
11+
)

Diff for: docs/src/api.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# API
2+
3+
4+
## Intermediate representation
5+
6+
### Types
7+
8+
```@docs
9+
Expression
10+
Variable
11+
ModelingToolkit.Constant
12+
Operation
13+
Equation
14+
Differential
15+
```
16+
17+
### Functions
18+
19+
```@docs
20+
Base.get(c::ModelingToolkit.Constant)
21+
Base.:~(::Expression, ::Expression)
22+
expand_derivatives
23+
ModelingToolkit.derivative
24+
```
25+
26+
### Macros
27+
28+
```@docs
29+
@parameters
30+
@variables
31+
@derivatives
32+
@register
33+
```
34+
35+
## Systems
36+
37+
### Types
38+
39+
```@docs
40+
ModelingToolkit.AbstractSystem
41+
ODESystem
42+
NonlinearSystem
43+
```
44+
45+
### Functions
46+
47+
```@docs
48+
independent_variables
49+
dependent_variables
50+
parameters
51+
calculate_jacobian
52+
generate_jacobian
53+
generate_function
54+
DiffEqBase.ODEFunction(sys::ODESystem, dvs, ps; version::FunctionVersion = ArrayFunction)
55+
```

Diff for: docs/src/index.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ModelingToolkit.jl Documentation
2+
3+
4+
ModelingToolkit.jl is an intermediate representation (IR) of computational graphs
5+
for scientific computing problems. Its purpose is to be a common target for
6+
modeling DSLs in order to allow for a common platform for model inspection and
7+
transformation. It uses a tagged variable IR in order to allow specification of
8+
complex models and allow for transformations of models. It has ways to plug into
9+
its function registration and derivative system so that way it can interact
10+
nicely with user-defined routines. Together, this is an abstract form of a
11+
scientific model that is easy for humans to generate but also easy for programs
12+
to manipulate.

Diff for: src/ModelingToolkit.jl

+52-2
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,70 @@ using StaticArrays, LinearAlgebra
1212
using MacroTools
1313
import MacroTools: splitdef, combinedef
1414

15+
using DocStringExtensions
16+
17+
"""
18+
$(TYPEDEF)
19+
20+
Base type for a symbolic expression.
21+
"""
1522
abstract type Expression <: Number end
23+
24+
"""
25+
$(TYPEDEF)
26+
27+
TODO
28+
"""
1629
abstract type AbstractSystem end
1730

1831
Base.promote_rule(::Type{<:Number},::Type{<:Expression}) = Expression
1932
Base.zero(::Type{<:Expression}) = Constant(0)
2033
Base.one(::Type{<:Expression}) = Constant(1)
2134

35+
"""
36+
$(TYPEDSIGNATURES)
37+
38+
Calculate the jacobian matrix of a system.
39+
40+
Returns a matrix of [`Expression`](@ref) instances. The result from the first
41+
call will be cached in the system object.
42+
"""
2243
function calculate_jacobian end
44+
45+
"""
46+
$(TYPEDSIGNATURES)
47+
48+
Generate a function to calculate the Jacobian of the system.
49+
"""
2350
function generate_jacobian end
51+
52+
"""
53+
$(TYPEDSIGNATURES)
54+
55+
Generate a function to evaluate the system's equations.
56+
"""
2457
function generate_function end
2558

59+
"""
60+
$(TYPEDSIGNATURES)
61+
62+
Get the set of independent variables for the given system.
63+
"""
2664
function independent_variables end
27-
function dependent_variables end
28-
function parameters end
65+
66+
"""
67+
$(TYPEDSIGNATURES)
68+
69+
Get the set of dependent variables for the given system.
70+
"""
71+
function dependent_variables end
72+
73+
"""
74+
$(TYPEDSIGNATURES)
75+
76+
Get the set of parameters variables for the given system.
77+
"""
78+
function parameters end
2979

3080
@enum FunctionVersion ArrayFunction=1 SArrayFunction=2
3181

Diff for: src/differentials.jl

+85
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
export Differential, expand_derivatives, @derivatives
22

33

4+
"""
5+
$(TYPEDEF)
6+
7+
Represents a differential operator.
8+
9+
# Fields
10+
$(FIELDS)
11+
12+
# Examples
13+
14+
```jldoctest
15+
julia> using ModelingToolkit
16+
17+
julia> @variables x y;
18+
19+
julia> D = Differential(x)
20+
(D'~x())
21+
22+
julia> D(y) # Differentiate y wrt. x
23+
(D'~x())(y())
24+
```
25+
"""
426
struct Differential <: Function
27+
"""The variable or expression to differentiate with respect to."""
528
x::Expression
629
end
730
(D::Differential)(x) = Operation(D, Expression[x])
@@ -11,6 +34,11 @@ Base.convert(::Type{Expr}, D::Differential) = D
1134

1235
Base.:(==)(D1::Differential, D2::Differential) = isequal(D1.x, D2.x)
1336

37+
"""
38+
$(SIGNATURES)
39+
40+
TODO
41+
"""
1442
function expand_derivatives(O::Operation)
1543
@. O.args = expand_derivatives(O.args)
1644

@@ -32,6 +60,40 @@ end
3260
expand_derivatives(x) = x
3361

3462
# Don't specialize on the function here
63+
"""
64+
$(SIGNATURES)
65+
66+
Calculate the derivative of the op `O` with respect to its argument with index
67+
`idx`.
68+
69+
# Examples
70+
71+
```jldoctest label1
72+
julia> using ModelingToolkit
73+
74+
julia> @variables x y;
75+
76+
julia> ModelingToolkit.derivative(sin(x), 1)
77+
cos(x())
78+
```
79+
80+
Note that the function does not recurse into the operation's arguments, i.e. the
81+
chain rule is not applied:
82+
83+
```jldoctest label1
84+
julia> myop = sin(x) * y^2
85+
sin(x()) * y() ^ 2
86+
87+
julia> typeof(myop.op) # Op is multiplication function
88+
typeof(*)
89+
90+
julia> ModelingToolkit.derivative(myop, 1) # wrt. sin(x)
91+
y() ^ 2
92+
93+
julia> ModelingToolkit.derivative(myop, 2) # wrt. y^2
94+
sin(x())
95+
```
96+
"""
3597
derivative(O::Operation, idx) = derivative(O.op, (O.args...,), Val(idx))
3698

3799
# Pre-defined derivatives
@@ -76,10 +138,33 @@ function _differential_macro(x)
76138
ex
77139
end
78140

141+
"""
142+
$(SIGNATURES)
143+
144+
Define one or more differentials.
145+
146+
# Examples
147+
148+
```jldoctest
149+
julia> using ModelingToolkit
150+
151+
julia> @variables x y z;
152+
153+
julia> @derivatives Dx'~x Dy'~y # Create differentials wrt. x and y
154+
((D'~x()), (D'~y()))
155+
156+
julia> Dx(z) # Differentiate z wrt. x
157+
(D'~x())(z())
158+
159+
julia> Dy(z) # Differentiate z wrt. y
160+
(D'~y())(z())
161+
```
162+
"""
79163
macro derivatives(x...)
80164
esc(_differential_macro(x))
81165
end
82166

167+
83168
function calculate_jacobian(eqs, dvs)
84169
Expression[Differential(dv)(eq) for eq eqs, dv dvs]
85170
end

Diff for: src/equations.jl

+30
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
export Equation
22

33

4+
"""
5+
$(TYPEDEF)
6+
7+
An equality relationship between two expressions.
8+
9+
# Fields
10+
$(FIELDS)
11+
"""
412
struct Equation
13+
"""The expression on the left hand side of the equation."""
514
lhs::Expression
15+
"""The expression on the right hand side of the equation."""
616
rhs::Expression
717
end
818
Base.:(==)(a::Equation, b::Equation) = isequal((a.lhs, a.rhs), (b.lhs, b.rhs))
919

20+
"""
21+
$(TYPEDSIGNATURES)
22+
23+
Create an [`Equation`](@ref) out of two [`Expression`](@ref) instances, or an
24+
`Expression` and a `Number`.
25+
26+
# Examples
27+
28+
```jldoctest
29+
julia> using ModelingToolkit
30+
31+
julia> @variables x y;
32+
33+
julia> x ~ y
34+
Equation(x(), y())
35+
36+
julia> x - y ~ 0
37+
Equation(x() - y(), ModelingToolkit.Constant(0))
38+
```
39+
"""
1040
Base.:~(lhs::Expression, rhs::Expression) = Equation(lhs, rhs)
1141
Base.:~(lhs::Expression, rhs::Number ) = Equation(lhs, rhs)
1242
Base.:~(lhs::Number , rhs::Expression) = Equation(lhs, rhs)

Diff for: src/function_registration.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Register functions and handle literals
2+
"""
3+
$(SIGNATURES)
4+
5+
TODO
6+
"""
27
macro register(sig)
38
splitsig = splitdef(:($sig = nothing))
49
name = splitsig[:name]

Diff for: src/operations.jl

+42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
1+
"""
2+
$(TYPEDEF)
3+
4+
An expression representing the application of a function to symbolic arguments.
5+
6+
# Fields
7+
$(FIELDS)
8+
9+
# Examples
10+
11+
Operations can be built by application of most built-in mathematical functions
12+
to other [`Expression`](@ref) instances:
13+
14+
```jldoctest
15+
julia> using ModelingToolkit
16+
17+
julia> @variables x y;
18+
19+
julia> op1 = sin(x)
20+
sin(x())
21+
22+
julia> typeof(op1.op)
23+
typeof(sin)
24+
25+
julia> op1.args
26+
1-element Array{Expression,1}:
27+
x()
28+
29+
julia> op2 = x + y
30+
x() + y()
31+
32+
julia> typeof(op2.op)
33+
typeof(+)
34+
35+
julia> op2.args
36+
2-element Array{Expression,1}:
37+
x()
38+
y()
39+
```
40+
"""
141
struct Operation <: Expression
42+
"""The function to be applied."""
243
op::Function
44+
"""The arguments the function is applied to."""
345
args::Vector{Expression}
446
end
547

0 commit comments

Comments
 (0)