Skip to content

Commit 90be14f

Browse files
committed
Updates
1 parent 06ba087 commit 90be14f

File tree

10 files changed

+42
-73
lines changed

10 files changed

+42
-73
lines changed

docs/src/basics/AbstractSystem.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ same keyword arguments, which are:
2222

2323
- `system`: This is used for specifying subsystems for hierarchical modeling with
2424
reusable components. For more information, see the [components page](@ref components)
25-
- Defaults: Keyword arguments like `default_u0` are used for specifying default
25+
- Defaults: Keyword arguments like `defaults` are used for specifying default
2626
values which are used. If a value is not given at the `SciMLProblem` construction
2727
time, its numerical value will be the default.
2828

@@ -49,9 +49,7 @@ Optionally, a system could have:
4949

5050
- `observed(sys)`: All observed equations of the system and its subsystems.
5151
- `get_observed(sys)`: Observed equations of the current-level system.
52-
- `get_default_u0(sys)`: A `Dict` that maps states into their default initial
53-
condition.
54-
- `get_default_p(sys)`: A `Dict` that maps parameters into their default value.
52+
- `get_defaults(sys)`: A `Dict` that maps variables into their default values.
5553
- `independent_variable(sys)`: The independent variable of a system.
5654
- `get_noiseeqs(sys)`: Noise equations of the current-level system.
5755

@@ -132,7 +130,7 @@ u0 = [
132130
## Default Value Handling
133131

134132
The `AbstractSystem` types allow for specifying default values, for example
135-
`default_p` inside of them. At problem construction time, these values are merged
133+
`defaults` inside of them. At problem construction time, these values are merged
136134
into the value maps, where for any repeats the value maps override the default.
137135
In addition, defaults of a higher level in the system override the defaults of
138-
a lower level in the system.
136+
a lower level in the system.

docs/src/tutorials/acausal_components.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using ModelingToolkit, Plots, DifferentialEquations
1919
# Basic electric components
2020
function Pin(;name)
2121
@variables v(t) i(t)
22-
ODESystem(Equation[], t, [v, i], [], name=name, default_u0=[v=>1.0, i=>1.0])
22+
ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
2323
end
2424

2525
function Ground(;name)
@@ -39,7 +39,7 @@ function Resistor(;name, R = 1.0)
3939
0 ~ p.i + n.i
4040
v ~ p.i * R
4141
]
42-
ODESystem(eqs, t, [v], [R], systems=[p, n], default_p=Dict(R => val), name=name)
42+
ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
4343
end
4444

4545
function Capacitor(; name, C = 1.0)
@@ -54,7 +54,7 @@ function Capacitor(; name, C = 1.0)
5454
0 ~ p.i + n.i
5555
D(v) ~ p.i / C
5656
]
57-
ODESystem(eqs, t, [v], [C], systems=[p, n], default_p=Dict(C => val), name=name)
57+
ODESystem(eqs, t, [v], [C], systems=[p, n], defaults=Dict(C => val), name=name)
5858
end
5959

6060
function ConstantVoltage(;name, V = 1.0)
@@ -66,7 +66,7 @@ function ConstantVoltage(;name, V = 1.0)
6666
V ~ p.v - n.v
6767
0 ~ p.i + n.i
6868
]
69-
ODESystem(eqs, t, [], [V], systems=[p, n], default_p=Dict(V => val), name=name)
69+
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
7070
end
7171

7272
R = 1.0
@@ -122,7 +122,7 @@ component to simply be the values there:
122122
```julia
123123
function Pin(;name)
124124
@variables v(t) i(t)
125-
ODESystem(Equation[], t, [v, i], [], name=name, default_u0=[v=>1.0, i=>1.0])
125+
ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
126126
end
127127
```
128128

@@ -176,11 +176,11 @@ function Resistor(;name, R = 1.0)
176176
0 ~ p.i + n.i
177177
v ~ p.i * R
178178
]
179-
ODESystem(eqs, t, [v], [R], systems=[p, n], default_p=Dict(R => val), name=name)
179+
ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
180180
end
181181
```
182182

183-
Notice that we have created this system with a `default_p` for the resistor's
183+
Notice that we have created this system with a `defaults` for the resistor's
184184
resistance. By doing so, if the resistance of this resistor is not overridden
185185
by a higher level default or overridden at `ODEProblem` construction time, this
186186
will be the value of the resistance.
@@ -200,7 +200,7 @@ function Capacitor(; name, C = 1.0)
200200
0 ~ p.i + n.i
201201
D(v) ~ p.i / C
202202
]
203-
ODESystem(eqs, t, [v], [C], systems=[p, n], default_p=Dict(C => val), name=name)
203+
ODESystem(eqs, t, [v], [C], systems=[p, n], defaults=Dict(C => val), name=name)
204204
end
205205
```
206206

@@ -219,7 +219,7 @@ function ConstantVoltage(;name, V = 1.0)
219219
V ~ p.v - n.v
220220
0 ~ p.i + n.i
221221
]
222-
ODESystem(eqs, t, [], [V], systems=[p, n], default_p=Dict(V => val), name=name)
222+
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
223223
end
224224
```
225225

docs/src/tutorials/tearing_parallelism.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const t = Sym{ModelingToolkit.Parameter{Real}}(:t)
4141
const D = Differential(t)
4242
function Pin(;name)
4343
@variables v(t) i(t)
44-
ODESystem(Equation[], t, [v, i], [], name=name, default_u0=Dict([v=>1.0, i=>1.0]))
44+
ODESystem(Equation[], t, [v, i], [], name=name, defaults=Dict([v=>1.0, i=>1.0]))
4545
end
4646

4747
function Ground(;name)
@@ -59,12 +59,12 @@ function ConstantVoltage(;name, V = 1.0)
5959
V ~ p.v - n.v
6060
0 ~ p.i + n.i
6161
]
62-
ODESystem(eqs, t, [], [V], systems=[p, n], default_p=Dict(V => val), name=name)
62+
ODESystem(eqs, t, [], [V], systems=[p, n], default=Dict(V => val), name=name)
6363
end
6464

6565
function HeatPort(;name)
6666
@variables T(t) Q_flow(t)
67-
return ODESystem(Equation[], t, [T, Q_flow], [], default_u0=Dict(T=>293.15, Q_flow=>0.0), name=name)
67+
return ODESystem(Equation[], t, [T, Q_flow], [], default=Dict(T=>293.15, Q_flow=>0.0), name=name)
6868
end
6969

7070
function HeatingResistor(;name, R=1.0, TAmbient=293.15, alpha=1.0)
@@ -83,8 +83,10 @@ function HeatingResistor(;name, R=1.0, TAmbient=293.15, alpha=1.0)
8383
]
8484
ODESystem(
8585
eqs, t, [v, RTherm], [R, TAmbient, alpha], systems=[p, n, h],
86-
default_p=Dict(R=>R_val, TAmbient=>TAmbient_val, alpha=>alpha_val),
87-
default_u0=Dict(v=>0.0, RTherm=>R_val),
86+
defaults=Dict(
87+
R=>R_val, TAmbient=>TAmbient_val, alpha=>alpha_val,
88+
v=>0.0, RTherm=>R_val
89+
),
8890
name=name,
8991
)
9092
end
@@ -99,7 +101,7 @@ function HeatCapacitor(;name, rho=8050, V=1, cp=460, TAmbient=293.15)
99101
]
100102
ODESystem(
101103
eqs, t, [], [rho, V, cp], systems=[h],
102-
default_p=Dict(rho=>rho_val, V=>V_val, cp=>cp_val),
104+
default=Dict(rho=>rho_val, V=>V_val, cp=>cp_val),
103105
name=name,
104106
)
105107
end
@@ -117,8 +119,7 @@ function Capacitor(;name, C = 1.0)
117119
]
118120
ODESystem(
119121
eqs, t, [v], [C], systems=[p, n],
120-
default_u0=Dict(v => 0.0),
121-
default_p=Dict(C => val),
122+
defaults=Dict(v => 0.0, C => val),
122123
name=name
123124
)
124125
end
@@ -159,7 +160,7 @@ end
159160
eqs = [
160161
D(E) ~ sum(((i, sys),)->getproperty(sys, Symbol(:resistor, i)).h.Q_flow, enumerate(rc_systems))
161162
]
162-
big_rc = ODESystem(eqs, t, [], [], systems=rc_systems, default_u0=Dict(E=>0.0))
163+
big_rc = ODESystem(eqs, t, [], [], systems=rc_systems, default=Dict(E=>0.0))
163164
```
164165

165166
Now let's say we want to expose a bit more parallelism via running tearing.

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using ModelingToolkit: ODESystem, var_from_nested_derivative, Differential,
1616
states, equations, vars, Symbolic, diff2term, value,
1717
operation, arguments, Sym, Term, simplify, solve_for,
1818
isdiffeq, isdifferential,
19-
get_structure, get_reduced_states, default_u0, default_p
19+
get_structure, get_reduced_states, defaults
2020

2121
using ModelingToolkit.BipartiteGraphs
2222
using ModelingToolkit.SystemStructures

src/structural_transformation/codegen.jl

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function gen_nlsolve(sys, eqs, vars)
131131
allvars = unique(collect(Iterators.flatten(map(ModelingToolkit.vars, rhss))))
132132
params = setdiff(allvars, vars)
133133

134-
u0map = default_u0(sys)
134+
u0map = defaults(sys)
135135
# splatting to tighten the type
136136
u0 = [map(var->get(u0map, var, 1e-3), vars)...]
137137
# specialize on the scalar case
@@ -338,22 +338,11 @@ function ODAEProblem{iip}(
338338
s = structure(sys)
339339
@unpack fullvars = s
340340
dvs = fullvars[diffvars_range(s)]
341-
defaults = merge(default_p(sys), default_u0(sys))
342-
u0map′ = ModelingToolkit.lower_mapnames(u0map, independent_variable(sys))
343-
u0 = ModelingToolkit.varmap_to_vars(u0map′, dvs; defaults=defaults)
344-
345341
ps = parameters(sys)
346-
if parammap isa DiffEqBase.NullParameters && isempty(default_p(sys))
347-
isempty(ps) || throw(ArgumentError("The model has non-empty parameters but no parameters are specified in the problem."))
348-
p = parammap
349-
else
350-
if parammap isa DiffEqBase.NullParameters
351-
pp = Pair[]
352-
else
353-
pp = ModelingToolkit.lower_mapnames(parammap)
354-
end
355-
p = ModelingToolkit.varmap_to_vars(pp, ps; defaults=defaults)
356-
end
342+
defs = defaults(sys)
343+
344+
u0 = ModelingToolkit.varmap_to_vars(u0map, dvs; defaults=defs)
345+
p = ModelingToolkit.varmap_to_vars(parammap, ps; defaults=defs)
357346

358347
ODEProblem{iip}(build_torn_function(sys; kw...), u0, tspan, p; kw...)
359348
end

src/utils.jl

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,3 @@ Maps the variable to a state.
8888
"""
8989
tovar(s::Sym{<:Parameter}) = Sym{symtype(s)}(s.name)
9090
tovar(s::Sym) = s
91-
92-
function lower_mapnames(umap::AbstractArray{T}) where {T<:Pair}
93-
T[value(k) => value(v) for (k, v) in umap]
94-
end
95-
function lower_mapnames(umap::AbstractArray{T},name) where {T<:Pair}
96-
T[lower_varname(value(k), name) => value(v) for (k, v) in umap]
97-
end
98-
function lower_mapnames(umap::NTuple{N,T}) where {N,T<:Pair}
99-
ntuple(i->value(umap[i][1]) => value(umap[i][2]),N)
100-
end
101-
function lower_mapnames(umap::NTuple{N,T},name) where {N,T<:Pair}
102-
ntuple(i->lower_varname(value(umap[i][1]), name) => value(umap[i][2]),N)
103-
end
104-
105-
lower_mapnames(umap::AbstractArray) = umap # Ambiguity
106-
lower_mapnames(umap::AbstractArray,name) = umap
107-
lower_mapnames(umap::Tuple) = umap
108-
lower_mapnames(umap::Tuple, name) = umap

test/components.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ using ModelingToolkit, OrdinaryDiffEq
55
const t = Sym{ModelingToolkit.Parameter{Real}}(:t)
66
function Pin(;name)
77
@variables v(t) i(t)
8-
ODESystem(Equation[], t, [v, i], [], name=name, default_u0=[v=>1.0, i=>1.0])
8+
ODESystem(Equation[], t, [v, i], [], name=name, default=[v=>1.0, i=>1.0])
99
end
1010

1111
function Ground(name)
@@ -23,7 +23,7 @@ function ConstantVoltage(name; V = 1.0)
2323
V ~ p.v - n.v
2424
0 ~ p.i + n.i
2525
]
26-
ODESystem(eqs, t, [], [V], systems=[p, n], default_p=Dict(V => val), name=name)
26+
ODESystem(eqs, t, [], [V], systems=[p, n], default=Dict(V => val), name=name)
2727
end
2828

2929
function Resistor(name; R = 1.0)
@@ -37,7 +37,7 @@ function Resistor(name; R = 1.0)
3737
0 ~ p.i + n.i
3838
v ~ p.i * R
3939
]
40-
ODESystem(eqs, t, [v], [R], systems=[p, n], default_p=Dict(R => val), name=name)
40+
ODESystem(eqs, t, [v], [R], systems=[p, n], default=Dict(R => val), name=name)
4141
end
4242

4343
function Capacitor(name; C = 1.0)
@@ -52,7 +52,7 @@ function Capacitor(name; C = 1.0)
5252
0 ~ p.i + n.i
5353
D(v) ~ p.i / C
5454
]
55-
ODESystem(eqs, t, [v], [C], systems=[p, n], default_p=Dict(C => val), name=name)
55+
ODESystem(eqs, t, [v], [C], systems=[p, n], default=Dict(C => val), name=name)
5656
end
5757

5858
R = 1.0
@@ -82,7 +82,7 @@ rc_eqs = [
8282

8383
rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground], name=:rc)
8484
sys = structural_simplify(rc_model)
85-
@test ModelingToolkit.default_p(sys) == Dict(
85+
@test ModelingToolkit.default(sys) == Dict(
8686
capacitor.C => 1.0,
8787
source.V => 1.0,
8888
resistor.R => 1.0,

test/odesystem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ D = Differential(t)
205205
eqs = [D(y₁) ~ -k₁*y₁+k₃*y₂*y₃,
206206
0 ~ y₁ + y₂ + y₃ - 1,
207207
D(y₂) ~ k₁*y₁-k₂*y₂^2-k₃*y₂*y₃]
208-
sys = ODESystem(eqs, default_p=[k₁ => 100, k₂ => 3e7], default_u0=[y₁ => 1.0])
208+
sys = ODESystem(eqs, defaults=[k₁ => 100, k₂ => 3e7, y₁ => 1.0])
209209
u0 = Pair[]
210210
push!(u0, y₂ => 0.0)
211211
push!(u0, y₃ => 0.0)
@@ -311,8 +311,8 @@ eqs = [D(D(x)) ~ -b/M*D(x) - k/M*x]
311311
ps = [M, b, k]
312312
default_u0 = [D(x) => 0.0, x => 10.0]
313313
default_p = [M => 1.0, b => 1.0, k => 1.0]
314-
@named sys = ODESystem(eqs, t, [x], ps, default_u0=default_u0, default_p=default_p)
314+
@named sys = ODESystem(eqs, t, [x], ps, defaults=[default_u0; default_p])
315315
sys = ode_order_lowering(sys)
316-
prob = ODEProblem(sys, Pair[], tspan)
316+
prob = ODEProblem(sys, [], tspan)
317317
sol = solve(prob, Tsit5())
318318
@test sum(abs, sol[end]) < 1

test/structural_transformation/tearing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ du = [0.0]; u = [1.0]; pr = 0.2; tt = 0.1
156156
@test du [-asin(u[1] - pr * tt)] atol=1e-5
157157

158158
# test the initial guess is respected
159-
infprob = ODAEProblem(tearing(ODESystem(eqs, t, default_u0=Dict(z=>Inf))), [x=>1.0], (0, 1.0), [p=>0.2])
159+
infprob = ODAEProblem(tearing(ODESystem(eqs, t, defaults=Dict(z=>Inf))), [x=>1.0], (0, 1.0), [p=>0.2])
160160
@test_throws DomainError infprob.f(du, u, pr, tt)
161161

162162
sol1 = solve(prob, Tsit5())

test/symbolic_parameters.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ u0 = Pair{Num, Any}[
1919
y => σ, # default u0 from default p
2020
z => u-0.1,
2121
]
22-
ns = NonlinearSystem(eqs, [x,y,z],[σ,ρ,β], name=:ns, default_p=par, default_u0=u0)
22+
ns = NonlinearSystem(eqs, [x,y,z],[σ,ρ,β], name=:ns, defaults=merge(par, u0))
2323
ns.y = u*1.1
24-
ModelingToolkit.default_p(ns)
25-
resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults=ModelingToolkit.default_p(ns))
24+
resolved = ModelingToolkit.varmap_to_vars(Dict(), parameters(ns), defaults=ModelingToolkit.defaults(ns))
2625
@test resolved == [1, 0.1+1, (0.1+1)*1.1]
2726

2827
prob = NonlinearProblem(ns, [u=>1.0], Pair[])
@@ -35,7 +34,7 @@ top = NonlinearSystem([0 ~ -a + ns.x+b], [a], [b], systems=[ns], name=:top)
3534
top.b = ns.σ*0.5
3635
top.ns.x = u*0.5
3736

38-
res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), defaults=ModelingToolkit.default_p(top))
37+
res = ModelingToolkit.varmap_to_vars(Dict(), parameters(top), defaults=ModelingToolkit.defaults(top))
3938
@test res == [0.5, 1, 0.1+1, (0.1+1)*1.1]
4039

4140
prob = NonlinearProblem(top, [states(ns, u)=>1.0, a=>1.0], Pair[])
@@ -45,4 +44,4 @@ prob = NonlinearProblem(top, [states(ns, u)=>1.0, a=>1.0], Pair[])
4544
# test NullParameters+defaults
4645
prob = NonlinearProblem(top, [states(ns, u)=>1.0, a=>1.0])
4746
@test prob.u0 == [1.0, 0.5, 1.1, 0.9]
48-
@show sol = solve(prob,NewtonRaphson())
47+
@show sol = solve(prob,NewtonRaphson())

0 commit comments

Comments
 (0)