Skip to content

Commit 8126cd9

Browse files
Merge pull request #2862 from hersle/check_iv_is_par
Create independent variables with @independent_variables
2 parents 94cd86b + 2b67034 commit 8126cd9

38 files changed

+175
-81
lines changed

docs/src/basics/FAQ.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ p, replace, alias = SciMLStructures.canonicalize(Tunable(), prob.p)
186186

187187
This error can come up after running `structural_simplify` on a system that generates dummy derivatives (i.e. variables with `ˍt`). For example, here even though all the variables are defined with initial values, the `ODEProblem` generation will throw an error that defaults are missing from the variable map.
188188

189-
```
189+
```julia
190190
using ModelingToolkit
191191
using ModelingToolkit: t_nounits as t, D_nounits as D
192192

@@ -197,13 +197,13 @@ eqs = [x1 + x2 + 1 ~ 0
197197
2 * D(D(x1)) + D(D(x2)) + D(D(x3)) + D(x4) + 4 ~ 0]
198198
@named sys = ODESystem(eqs, t)
199199
sys = structural_simplify(sys)
200-
prob = ODEProblem(sys, [], (0,1))
200+
prob = ODEProblem(sys, [], (0, 1))
201201
```
202202

203203
We can solve this problem by using the `missing_variable_defaults()` function
204204

205-
```
206-
prob = ODEProblem(sys, ModelingToolkit.missing_variable_defaults(sys), (0,1))
205+
```julia
206+
prob = ODEProblem(sys, ModelingToolkit.missing_variable_defaults(sys), (0, 1))
207207
```
208208

209209
This function provides 0 for the default values, which is a safe assumption for dummy derivatives of most models. However, the 2nd argument allows for a different default value or values to be used if needed.
@@ -221,12 +221,26 @@ julia> ModelingToolkit.missing_variable_defaults(sys, [1,2,3])
221221
Use the `u0_constructor` keyword argument to map an array to the desired
222222
container type. For example:
223223

224-
```
224+
```julia
225225
using ModelingToolkit, StaticArrays
226226
using ModelingToolkit: t_nounits as t, D_nounits as D
227227

228-
sts = @variables x1(t)=0.0
228+
sts = @variables x1(t) = 0.0
229229
eqs = [D(x1) ~ 1.1 * x1]
230230
@mtkbuild sys = ODESystem(eqs, t)
231-
prob = ODEProblem{false}(sys, [], (0,1); u0_constructor = x->SVector(x...))
231+
prob = ODEProblem{false}(sys, [], (0, 1); u0_constructor = x -> SVector(x...))
232+
```
233+
234+
## Using a custom independent variable
235+
236+
When possible, we recommend `using ModelingToolkit: t_nounits as t, D_nounits as D` as the independent variable and its derivative.
237+
However, if you want to use your own, you can do so:
238+
239+
```julia
240+
using ModelingToolkit
241+
242+
@independent_variables x
243+
D = Differential(x)
244+
@variables y(x)
245+
@named sys = ODESystem([D(y) ~ x], x)
232246
```

docs/src/basics/Validation.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ Units may be assigned with the following syntax.
88

99
```@example validation
1010
using ModelingToolkit, DynamicQuantities
11-
@variables t [unit = u"s"] x(t) [unit = u"m"] g(t) w(t) [unit = "Hz"]
11+
@independent_variables t [unit = u"s"]
12+
@variables x(t) [unit = u"m"] g(t) w(t) [unit = u"Hz"]
1213
13-
@variables(t, [unit = u"s"], x(t), [unit = u"m"], g(t), w(t), [unit = "Hz"])
14+
@parameters(t, [unit = u"s"])
15+
@variables(x(t), [unit = u"m"], g(t), w(t), [unit = u"Hz"])
1416
17+
@parameters begin
18+
t, [unit = u"s"]
19+
end
1520
@variables(begin
16-
t, [unit = u"s"],
1721
x(t), [unit = u"m"],
1822
g(t),
19-
w(t), [unit = "Hz"]
23+
w(t), [unit = u"Hz"]
2024
end)
2125
2226
# Simultaneously set default value (use plain numbers, not quantities)
@@ -46,10 +50,11 @@ Example usage below. Note that `ModelingToolkit` does not force unit conversions
4650

4751
```@example validation
4852
using ModelingToolkit, DynamicQuantities
53+
@independent_variables t [unit = u"ms"]
4954
@parameters τ [unit = u"ms"]
50-
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
55+
@variables E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
5156
D = Differential(t)
52-
eqs = eqs = [D(E) ~ P - E / τ,
57+
eqs = [D(E) ~ P - E / τ,
5358
0 ~ P]
5459
ModelingToolkit.validate(eqs)
5560
```
@@ -70,10 +75,11 @@ An example of an inconsistent system: at present, `ModelingToolkit` requires tha
7075

7176
```@example validation
7277
using ModelingToolkit, DynamicQuantities
78+
@independent_variables t [unit = u"ms"]
7379
@parameters τ [unit = u"ms"]
74-
@variables t [unit = u"ms"] E(t) [unit = u"J"] P(t) [unit = u"MW"]
80+
@variables E(t) [unit = u"J"] P(t) [unit = u"MW"]
7581
D = Differential(t)
76-
eqs = eqs = [D(E) ~ P - E / τ,
82+
eqs = [D(E) ~ P - E / τ,
7783
0 ~ P]
7884
ModelingToolkit.validate(eqs) #Returns false while displaying a warning message
7985
```
@@ -115,7 +121,8 @@ In order for a function to work correctly during both validation & execution, th
115121

116122
```julia
117123
using ModelingToolkit, DynamicQuantities
118-
@variables t [unit = u"ms"] E(t) [unit = u"J"] P(t) [unit = u"MW"]
124+
@independent_variables t [unit = u"ms"]
125+
@variables E(t) [unit = u"J"] P(t) [unit = u"MW"]
119126
D = Differential(t)
120127
eqs = [D(E) ~ P - E / 1u"ms"]
121128
ModelingToolkit.validate(eqs) #Returns false while displaying a warning message
@@ -129,8 +136,9 @@ Instead, they should be parameterized:
129136

130137
```@example validation3
131138
using ModelingToolkit, DynamicQuantities
139+
@independent_variables t [unit = u"ms"]
132140
@parameters τ [unit = u"ms"]
133-
@variables t [unit = u"ms"] E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
141+
@variables E(t) [unit = u"kJ"] P(t) [unit = u"MW"]
134142
D = Differential(t)
135143
eqs = [D(E) ~ P - E / τ]
136144
ModelingToolkit.validate(eqs) #Returns true

docs/src/tutorials/SampledData.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ H(z) = \dfrac{b_2 z^2 + b_1 z + b_0}{a_2 z^2 + a_1 z + a_0}
9797
may thus be modeled as
9898

9999
```julia
100-
@variables t y(t) [description = "Output"] u(t) [description = "Input"]
100+
t = ModelingToolkit.t_nounits
101+
@variables y(t) [description = "Output"] u(t) [description = "Input"]
101102
k = ShiftIndex(Clock(t, dt))
102103
eqs = [
103104
a2 * y(k) + a1 * y(k - 1) + a0 * y(k - 2) ~ b2 * u(k) + b1 * u(k - 1) + b0 * u(k - 2)

src/ModelingToolkit.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ using .BipartiteGraphs
127127

128128
include("variables.jl")
129129
include("parameters.jl")
130+
include("independent_variables.jl")
130131
include("constants.jl")
131132

132133
include("utils.jl")
@@ -183,13 +184,13 @@ for S in subtypes(ModelingToolkit.AbstractSystem)
183184
end
184185

185186
const t_nounits = let
186-
only(@parameters t)
187+
only(@independent_variables t)
187188
end
188189
const t_unitful = let
189-
only(@parameters t [unit = Unitful.u"s"])
190+
only(@independent_variables t [unit = Unitful.u"s"])
190191
end
191192
const t = let
192-
only(@parameters t [unit = DQ.u"s"])
193+
only(@independent_variables t [unit = DQ.u"s"])
193194
end
194195

195196
const D_nounits = Differential(t_nounits)
@@ -262,7 +263,7 @@ export generate_initializesystem
262263
export alg_equations, diff_equations, has_alg_equations, has_diff_equations
263264
export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs
264265

265-
export @variables, @parameters, @constants, @brownian
266+
export @variables, @parameters, @independent_variables, @constants, @brownian
266267
export @named, @nonamespace, @namespace, extend, compose, complete
267268
export debug_system
268269

src/discretedomain.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ $(FIELDS)
9898
```jldoctest
9999
julia> using Symbolics
100100
101-
julia> @variables t;
101+
julia> t = ModelingToolkit.t_nounits
102102
103103
julia> Δ = Sample(t, 0.01)
104104
(::Sample) (generic function with 2 methods)
@@ -166,7 +166,9 @@ The `ShiftIndex` operator allows you to index a signal and obtain a shifted disc
166166
# Examples
167167
168168
```
169-
julia> @variables t x(t);
169+
julia> t = ModelingToolkit.t_nounits;
170+
171+
julia> @variables x(t);
170172
171173
julia> k = ShiftIndex(t, 0.1);
172174

src/independent_variables.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
@independent_variables t₁ t₂ ...
3+
4+
Define one or more independent variables. For example:
5+
6+
@independent_variables t
7+
@variables x(t)
8+
"""
9+
macro independent_variables(ts...)
10+
:(@parameters $(ts...)) |> esc # TODO: treat independent variables separately from variables and parameters
11+
end

src/systems/abstractsystem.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,11 +2237,10 @@ This example builds the following feedback interconnection and linearizes it fro
22372237
22382238
```julia
22392239
using ModelingToolkit
2240-
@variables t
2240+
using ModelingToolkit: t_nounits as t, D_nounits as D
22412241
function plant(; name)
22422242
@variables x(t) = 1
22432243
@variables u(t)=0 y(t)=0
2244-
D = Differential(t)
22452244
eqs = [D(x) ~ -x + u
22462245
y ~ x]
22472246
ODESystem(eqs, t; name = name)
@@ -2250,7 +2249,6 @@ end
22502249
function ref_filt(; name)
22512250
@variables x(t)=0 y(t)=0
22522251
@variables u(t)=0 [input = true]
2253-
D = Differential(t)
22542252
eqs = [D(x) ~ -2 * x + u
22552253
y ~ x]
22562254
ODESystem(eqs, t, name = name)

src/systems/alias_elimination.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,10 @@ Use Kahn's algorithm to topologically sort observed equations.
388388
389389
Example:
390390
```julia
391-
julia> @variables t x(t) y(t) z(t) k(t)
392-
(t, x(t), y(t), z(t), k(t))
391+
julia> t = ModelingToolkit.t_nounits
392+
393+
julia> @variables x(t) y(t) z(t) k(t)
394+
(x(t), y(t), z(t), k(t))
393395
394396
julia> eqs = [
395397
x ~ y + z

src/systems/dependency_graphs.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ Example:
1515
1616
```julia
1717
using ModelingToolkit
18+
using ModelingToolkit: t_nounits as t
1819
@parameters β γ κ η
19-
@variables t S(t) I(t) R(t)
20+
@variables S(t) I(t) R(t)
2021
2122
rate₁ = β * S * I
2223
rate₂ = γ * I + t

src/systems/diffeqs/basic_transformations.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Example:
1616
```julia
1717
using ModelingToolkit, OrdinaryDiffEq, Test
1818
19-
@parameters t α β γ δ
19+
@independent_variables t
20+
@parameters α β γ δ
2021
@variables x(t) y(t)
2122
D = Differential(t)
2223

0 commit comments

Comments
 (0)