Skip to content

Commit 803c6a7

Browse files
committed
Added examples of using @constants to introductory documentation.
1 parent a977762 commit 803c6a7

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

docs/src/basics/ContextualVariables.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ All modeling projects have some form of parameters. `@parameters` marks a variab
2020
as being the parameter of some system, which allows automatic detection algorithms
2121
to ignore such variables when attempting to find the states of a system.
2222

23+
## Constants
24+
25+
Constants are like parameters that:
26+
- always have a default value, which must be assigned when the constants are
27+
declared
28+
- do not show up in the list of parameters of a system.
29+
30+
The intended use-cases for constants are:
31+
- representing literals (eg, π) symbolically, which results in cleaner
32+
Latexification of equations (avoids turning `d ~ 2π*r` into `d = 6.283185307179586 r`)
33+
- allowing auto-generated unit conversion factors to live outside the list of
34+
parameters
35+
- representing fundamental constants (eg, speed of light `c`) that should never
36+
be adjusted inadvertently.
37+
2338
## Variable metadata [Experimental/TODO]
2439

2540
In many engineering systems some variables act like "flows" while others do not.

docs/src/tutorials/ode_modeling.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ But if you want to just see some code and run, here's an example:
1414
using ModelingToolkit
1515

1616
@variables t x(t) # independent and dependent variables
17-
@parameters τ # parameters
17+
@parameters τ # parameters
18+
@constants h = 1 # constants have an assigned value
1819
D = Differential(t) # define an operator for the differentiation w.r.t. time
1920

2021
# your first ODE, consisting of a single equation, the equality indicated by ~
21-
@named fol = ODESystem([ D(x) ~ (1 - x)/τ])
22+
@named fol = ODESystem([ D(x) ~ (h - x)/τ])
2223

2324
using DifferentialEquations: solve
2425
using Plots: plot
2526

2627
prob = ODEProblem(fol, [x => 0.0], (0.0,10.0), [τ => 3.0])
28+
# parameter `τ` can be assigned a value, but constant `h` cannot
2729
sol = solve(prob)
2830
plot(sol)
2931
```
@@ -42,19 +44,20 @@ first-order lag element:
4244
```
4345

4446
Here, ``t`` is the independent variable (time), ``x(t)`` is the (scalar) state
45-
variable, ``f(t)`` is an external forcing function, and ``\tau`` is a constant
47+
variable, ``f(t)`` is an external forcing function, and ``\tau`` is a
4648
parameter. In MTK, this system can be modelled as follows. For simplicity, we
47-
first set the forcing function to a constant value.
49+
first set the forcing function to a time-independent value.
4850

4951
```julia
5052
using ModelingToolkit
5153

5254
@variables t x(t) # independent and dependent variables
5355
@parameters τ # parameters
56+
@constants h = 1 # constants
5457
D = Differential(t) # define an operator for the differentiation w.r.t. time
5558

5659
# your first ODE, consisting of a single equation, indicated by ~
57-
@named fol_model = ODESystem(D(x) ~ (1 - x)/τ)
60+
@named fol_model = ODESystem(D(x) ~ (h - x)/τ)
5861
# Model fol_model with 1 equations
5962
# States (1):
6063
# x(t)
@@ -89,7 +92,7 @@ intermediate variable `RHS`:
8992

9093
```julia
9194
@variables RHS(t)
92-
@named fol_separate = ODESystem([ RHS ~ (1 - x)/τ,
95+
@named fol_separate = ODESystem([ RHS ~ (h - x)/τ,
9396
D(x) ~ RHS ])
9497
# Model fol_separate with 2 equations
9598
# States (2):
@@ -110,7 +113,7 @@ fol_simplified = structural_simplify(fol_separate)
110113

111114
equations(fol_simplified)
112115
# 1-element Array{Equation,1}:
113-
# Differential(t)(x(t)) ~ (τ^-1)*(1 - x(t))
116+
# Differential(t)(x(t)) ~ (τ^-1)*(h - x(t))
114117

115118
equations(fol_simplified) == equations(fol_model)
116119
# true
@@ -133,6 +136,12 @@ sol = solve(prob)
133136
plot(sol, vars=[x, RHS])
134137
```
135138

139+
By default, `structural_simplify` also replaces symbolic `constants` with
140+
their default values. This allows additional simplifications not possible
141+
if using `parameters` (eg, solution of linear equations by dividing out
142+
the constant's value, which cannot be done for parameters since they may
143+
be zero).
144+
136145
![Simulation result of first-order lag element, with right-hand side](https://user-images.githubusercontent.com/13935112/111958403-7e8d3e00-8aed-11eb-9d18-08b5180a59f9.png)
137146

138147
Note that similarly the indexing of the solution works via the names, and so

0 commit comments

Comments
 (0)