Skip to content

Commit ec23b6e

Browse files
Merge pull request #72 from ValentinKaisermayer/add-doctests
adds doctests
2 parents 1100e95 + 1541500 commit ec23b6e

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](http://mtkstdlib.sciml.ai/stable/)
55
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](http://mtkstdlib.sciml.ai/dev/)
66

7-
The ModelingToolkit Standard Library is standard library of components to model the world and beyond.
7+
The ModelingToolkit Standard Library is a standard library of components to model the world and beyond.
88

99
![](https://user-images.githubusercontent.com/1814174/172000112-3579f5cf-c370-48c2-8047-558fbc46aeb6.png)
1010

@@ -41,27 +41,30 @@ The following is the [RC Circuit Demonstration](http://mtkstdlib.sciml.ai/dev/tu
4141
```julia
4242
using ModelingToolkit, OrdinaryDiffEq, Plots
4343
using ModelingToolkitStandardLibrary.Electrical
44+
using ModelingToolkitStandardLibrary.Blocks: Constant
4445

4546
R = 1.0
4647
C = 1.0
4748
V = 1.0
4849
@variables t
4950
@named resistor = Resistor(R=R)
5051
@named capacitor = Capacitor(C=C)
51-
@named source = ConstantVoltage(V=V)
52+
@named source = Voltage()
53+
@named constant = Constant(k=V)
5254
@named ground = Ground()
5355

5456
rc_eqs = [
57+
connect(constant.output, source.V)
5558
connect(source.p, resistor.p)
5659
connect(resistor.n, capacitor.p)
5760
connect(capacitor.n, source.n, ground.g)
5861
]
5962

60-
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground])
63+
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, constant, source, ground])
6164
sys = structural_simplify(rc_model)
6265
prob = ODAEProblem(sys, Pair[], (0, 10.0))
6366
sol = solve(prob, Tsit5())
64-
plot(sol, vars = [capacitor.v,resistor.i],
67+
plot(sol, vars = [capacitor.v, resistor.i],
6568
title = "RC Circuit Demonstration",
6669
labels = ["Capacitor Voltage" "Resistor Current"])
6770
savefig("plot.png")

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
4+
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
5+
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
6+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
7+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
38

49
[compat]
510
Documenter = "0.26, 0.27"

docs/make.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ makedocs(
1414
authors="Julia Computing",
1515
clean=true,
1616
doctest=false,
17+
strict=[
18+
:doctest,
19+
:example_block,
20+
],
1721
modules=[ModelingToolkitStandardLibrary,
1822
ModelingToolkitStandardLibrary.Blocks,
1923
ModelingToolkitStandardLibrary.Mechanical,

docs/src/tutorials/custom_component.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Custom Component
2-
In this tutorial the creation of a custom component is demonstrated via the [Chua's circuit](https://en.wikipedia.org/wiki/Chua%27s_circuit).
2+
In this tutorial, the creation of a custom component is demonstrated via the [Chua's circuit](https://en.wikipedia.org/wiki/Chua%27s_circuit).
33
The circuit is a simple circuit that shows chaotic behaviour.
44
Except for a non-linear resistor every other component already is part of `ModelingToolkitStandardLibrary.Electrical`.
55

6-
First we need to make some imports.
7-
```julia
6+
First, we need to make some imports.
7+
```@example components
88
using ModelingToolkit
99
using ModelingToolkitStandardLibrary.Electrical
1010
using ModelingToolkitStandardLibrary.Electrical: OnePort
@@ -26,8 +26,8 @@ equation
2626
i = if (v < -Ve) then Gb*(v + Ve) - Ga*Ve else if (v > Ve) then Gb*(v - Ve) + Ga*Ve else Ga*v;
2727
end NonlinearResistor;
2828
```
29-
this can almost be directly translate it to the syntax of `ModelingToolkit`.
30-
```julia
29+
this can almost be directly translated to the syntax of `ModelingToolkit`.
30+
```@example components
3131
@parameters t
3232
3333
function NonlinearResistor(;name, Ga, Gb, Ve)
@@ -45,36 +45,37 @@ function NonlinearResistor(;name, Ga, Gb, Ve)
4545
]
4646
extend(ODESystem(eqs, t, [], pars; name=name), oneport)
4747
end
48+
nothing # hide
4849
```
4950

5051
### Explanation
51-
All components in `ModelingToolkit` are created via a function that serves as the constructor and returns some form of system, in this case a `ODESystem`.
52+
All components in `ModelingToolkit` are created via a function that serves as the constructor and returns some form of system, in this case, an `ODESystem`.
5253
Since the non-linear resistor is essentially a standard electrical component with two ports, we can extend from the `OnePort` component of the library.
5354
```julia
5455
@named oneport = OnePort()
5556
```
5657
This creates a `OnePort` with the `name = :oneport`.
57-
For easier notation we can unpack the states of the component
58+
For easier notation, we can unpack the states of the component
5859
```julia
5960
@unpack v, i = oneport
6061
```
6162
It might be a good idea to create parameters for the constants of the `NonlinearResistor`.
6263
```julia
6364
pars = @parameters Ga=Ga Gb=Gb Ve=Ve
6465
```
65-
The syntax looks funny but it simply creates symbolic parameters with the name `Ga` where it's default value is set from the function's argument `Ga`.
66-
While this is not strictly necessary it allows the user to `remake` the problem easily with different parameters or allow for auto-tuning or parameter optimization without having to do all costly steps that may be involved with building and simplifying a model.
66+
The syntax looks funny but it simply creates symbolic parameters with the name `Ga` where its default value is set from the function's argument `Ga`.
67+
While this is not strictly necessary it allows the user to `remake` the problem easily with different parameters or allow for auto-tuning or parameter optimization without having to do all the costly steps that may be involved with building and simplifying a model.
6768
The non-linear (in this case piece-wise constant) equation for the current can be implemented using `IfElse.ifelse`.
6869
Finally, the created `oneport` component is extended with the created equations and parameters.
69-
In this case no extra state variables are added, hence an empty vector is supplied.
70-
The independent variable `t` needs to be supplied as second argument.
70+
In this case, no extra state variables are added, hence an empty vector is supplied.
71+
The independent variable `t` needs to be supplied as the second argument.
7172
```julia
7273
extend(ODESystem(eqs, t, [], pars; name=name), oneport)
7374
```
7475

7576
## Building the Model
7677
The final model can now be created with the components from the library and the new custom component.
77-
```julia
78+
```@example components
7879
@named L = Inductor(L=18)
7980
@named Ro = Resistor(R=12.5e-3)
8081
@named G = Conductor(G=0.565)
@@ -99,24 +100,27 @@ connections = [
99100
]
100101
101102
@named model = ODESystem(connections, t, systems=[L, Ro, G, C1, C2, Nr])
103+
nothing # hide
102104
```
103105

104106
## Simulating the Model
105107
Now the model can be simulated.
106-
First `structural_simplify` is called on the model and a `ODEProblem` is build from the result.
108+
First, `structural_simplify` is called on the model and an `ODEProblem` is built from the result.
107109
Since the initial voltage of the first capacitor was already specified via `v_start`, no initial condition is given and an empty pair is supplied.
108-
```julia
110+
```@example components
109111
sys = structural_simplify(model)
110112
prob = ODEProblem(sys, Pair[], (0, 5e4), saveat=0.01)
111113
sol = solve(prob, Rodas4())
112114
113115
Plots.plot(sol[C1.v], sol[C2.v], title="Chaotic Attractor", label="", ylabel="C1 Voltage in V", xlabel="C2 Voltage in V")
114116
Plots.savefig("chua_phase_plane.png")
117+
nothing # hide
115118
116119
Plots.plot(sol; vars=[C1.v, C2.v, L.i], labels=["C1 Voltage in V" "C1 Voltage in V" "Inductor Current in A"])
117120
Plots.savefig("chua.png")
121+
nothing # hide
118122
```
119123

120-
![Time series plot of C1.v, C2.v and L.i](https://user-images.githubusercontent.com/50108075/169712569-9ae5a074-ca1a-4801-b666-75a2f6e21bf5.png)
124+
![Time series plot of C1.v, C2.v and L.i](chua_phase_plane.png)
121125

122-
![Phase plane plot of C1.v and C2.v](https://user-images.githubusercontent.com/50108075/169712578-b3f314f6-3310-4471-a31e-af7fac3c0fbc.png)
126+
![Phase plane plot of C1.v and C2.v](chua.png)

docs/src/tutorials/rc_circuit.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
# RC Circuit Model
22

33
This tutorial is a simplified version of the [RC circuit tutorial in the
4-
ModelingToolkit.jl documentation](https://mtk.sciml.ai/dev/tutorials/acausal_components/).
4+
`ModelingToolkit.jl` documentation](https://mtk.sciml.ai/dev/tutorials/acausal_components/).
55
In that tutorial, the full RC circuit is built from scratch. Here, we will use the
66
components of the `Electrical` model in the ModelingToolkit Standard Library to simply
77
connect pre-made components and simulate the model.
88

9-
```julia
9+
```@example
1010
using ModelingToolkit, OrdinaryDiffEq, Plots
1111
using ModelingToolkitStandardLibrary.Electrical
12+
using ModelingToolkitStandardLibrary.Blocks: Constant
1213
1314
R = 1.0
1415
C = 1.0
1516
V = 1.0
1617
@variables t
1718
@named resistor = Resistor(R=R)
1819
@named capacitor = Capacitor(C=C)
19-
@named source = ConstantVoltage(V=V)
20+
@named source = Voltage()
21+
@named constant = Constant(k=V)
2022
@named ground = Ground()
2123
2224
rc_eqs = [
25+
connect(constant.output, source.V)
2326
connect(source.p, resistor.p)
2427
connect(resistor.n, capacitor.p)
2528
connect(capacitor.n, source.n, ground.g)
2629
]
2730
28-
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground])
31+
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, constant, source, ground])
2932
sys = structural_simplify(rc_model)
3033
prob = ODAEProblem(sys, Pair[], (0, 10.0))
3134
sol = solve(prob, Tsit5())
32-
plot(sol, vars = [capacitor.v,resistor.i],
35+
plot(sol, vars = [capacitor.v, resistor.i],
3336
title = "RC Circuit Demonstration",
3437
labels = ["Capacitor Voltage" "Resistor Current"])
35-
savefig("plot.png")
38+
savefig("plot.png"); nothing # hide
3639
```
3740

38-
![](https://user-images.githubusercontent.com/1814174/164912983-c3f73628-0e19-4e42-b085-4f62ba6f23d1.png)
41+
![](plot.png)

0 commit comments

Comments
 (0)