Skip to content

Commit 8a00ec7

Browse files
committed
Add some docstrings
1 parent c296d20 commit 8a00ec7

File tree

8 files changed

+316
-2
lines changed

8 files changed

+316
-2
lines changed

src/ModelingToolkit.jl

Lines changed: 52 additions & 2 deletions
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

src/differentials.jl

Lines changed: 85 additions & 0 deletions
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

src/equations.jl

Lines changed: 30 additions & 0 deletions
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)

src/function_registration.jl

Lines changed: 5 additions & 0 deletions
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]

src/operations.jl

Lines changed: 42 additions & 0 deletions
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

src/systems/diffeqs/diffeqsystem.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,43 @@ function to_diffeq(eq::Equation)
3131
end
3232
Base.:(==)(a::DiffEq, b::DiffEq) = isequal((a.x, a.n, a.rhs), (b.x, b.n, b.rhs))
3333

34+
"""
35+
$(TYPEDEF)
36+
37+
A system of ordinary differential equations.
38+
39+
# Fields
40+
* `eqs` - The ODEs defining the system.
41+
42+
# Examples
43+
44+
```
45+
using ModelingToolkit
46+
47+
@parameters t σ ρ β
48+
@variables x(t) y(t) z(t)
49+
@derivatives D'~t
50+
51+
eqs = [D(x) ~ σ*(y-x),
52+
D(y) ~ x*(ρ-z)-y,
53+
D(z) ~ x*y - β*z]
54+
55+
de = ODESystem(eqs)
56+
```
57+
"""
3458
struct ODESystem <: AbstractSystem
59+
"""The ODEs defining the system."""
3560
eqs::Vector{DiffEq}
61+
"""Independent variable."""
3662
iv::Variable
63+
"""Dependent (state) variables."""
3764
dvs::Vector{Variable}
65+
"""Parameter variables."""
3866
ps::Vector{Variable}
67+
"""
68+
Jacobian matrix. Note: this field will not be defined until
69+
[`calculate_jacobian`](@ref) is called on the system.
70+
"""
3971
jac::RefValue{Matrix{Expression}}
4072
end
4173

@@ -156,6 +188,13 @@ function generate_factorized_W(sys::ODESystem, simplify=true; version::FunctionV
156188
return (Wfact_func, Wfact_t_func)
157189
end
158190

191+
"""
192+
$(SIGNATURES)
193+
194+
Create an `ODEFunction` from the [`ODESystem`](@ref). The arguments `dvs` and `ps`
195+
are used to set the order of the dependent variable and parameter vectors,
196+
respectively.
197+
"""
159198
function DiffEqBase.ODEFunction(sys::ODESystem, dvs, ps; version::FunctionVersion = ArrayFunction)
160199
expr = generate_function(sys, dvs, ps; version = version)
161200
if version === ArrayFunction

src/systems/nonlinear/nonlinear_system.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,32 @@ function Base.convert(::Type{NLEq}, eq::Equation)
1010
end
1111
Base.:(==)(a::NLEq, b::NLEq) = a.rhs == b.rhs
1212

13+
"""
14+
$(TYPEDEF)
15+
16+
A nonlinear system of equations.
17+
18+
# Fields
19+
* `eqs` - Vector of equations defining the system.
20+
21+
# Examples
22+
23+
```
24+
@variables x y z
25+
@parameters σ ρ β
26+
27+
eqs = [0 ~ σ*(y-x),
28+
0 ~ x*(ρ-z)-y,
29+
0 ~ x*y - β*z]
30+
ns = NonlinearSystem(eqs, [x,y,z])
31+
```
32+
"""
1333
struct NonlinearSystem <: AbstractSystem
34+
"""Vector of equations defining the system."""
1435
eqs::Vector{NLEq}
36+
"""Unknown variables."""
1537
vs::Vector{Expression}
38+
"""Parameters."""
1639
ps::Vector{Variable}
1740
function NonlinearSystem(eqs, vs)
1841
rhss = [eq.rhs for eq eqs]

0 commit comments

Comments
 (0)