Skip to content

Commit ff8d231

Browse files
Merge pull request #2436 from AayushSabharwal/as/require-completed
refactor!: require systems to be `complete`d before creating `XProblem`/`XProblemExpr`
2 parents ce450b2 + a779f4b commit ff8d231

40 files changed

+202
-63
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Yingbo Ma <[email protected]>", "Chris Rackauckas <[email protected]> and contributors"]
4-
version = "9.0.0"
4+
version = "8.76.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

docs/src/basics/Events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function UnitMassWithFriction(k; name)
7373
ODESystem(eqs, t; continuous_events = [v ~ 0], name) # when v = 0 there is a discontinuity
7474
end
7575
@named m = UnitMassWithFriction(0.7)
76-
prob = ODEProblem(m, Pair[], (0, 10pi))
76+
prob = ODEProblem(complete(m), Pair[], (0, 10pi))
7777
sol = solve(prob, Tsit5())
7878
plot(sol)
7979
```
@@ -244,7 +244,7 @@ u0 = [N => 0.0]
244244
tspan = (0.0, 20.0)
245245
p = [α => 100.0, tinject => 10.0, M => 50]
246246
@named osys = ODESystem(eqs, t, [N], [α, M, tinject]; discrete_events = injection)
247-
oprob = ODEProblem(osys, u0, tspan, p)
247+
oprob = ODEProblem(complete(osys), u0, tspan, p)
248248
sol = solve(oprob, Tsit5(); tstops = 10.0)
249249
plot(sol)
250250
```

docs/src/examples/parsing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ using ModelingToolkit, NonlinearSolve
2727
vars = union(ModelingToolkit.vars.(eqs)...)
2828
@named ns = NonlinearSystem(eqs, vars, [])
2929
30-
prob = NonlinearProblem(ns, [1.0, 1.0, 1.0])
30+
prob = NonlinearProblem(complete(ns), [1.0, 1.0, 1.0])
3131
sol = solve(prob, NewtonRaphson())
3232
```

docs/src/examples/sparse_jacobians.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Now let's use `modelingtoolkitize` to generate the symbolic version:
5555

5656
```@example sparsejac
5757
sys = modelingtoolkitize(prob);
58+
sys = complete(sys);
5859
nothing # hide
5960
```
6061

docs/src/tutorials/bifurcation_diagram_computation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using ModelingToolkit
1313
eqs = [0 ~ μ * x - x^3 + α * y,
1414
0 ~ -y]
1515
@named nsys = NonlinearSystem(eqs, [x, y], [μ, α])
16+
nsys = complete(nsys)
1617
```
1718

1819
we wish to compute a bifurcation diagram for this system as we vary the parameter `μ`. For this, we need to provide the following information:
@@ -97,6 +98,7 @@ D = Differential(t)
9798
eqs = [D(x) ~ μ * x - y - x * (x^2 + y^2),
9899
D(y) ~ x + μ * y - y * (x^2 + y^2)]
99100
@named osys = ODESystem(eqs, t)
101+
osys = complete(osys)
100102
101103
bif_par = μ
102104
plot_var = x

docs/src/tutorials/modelingtoolkitize.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ sys = modelingtoolkitize(prob)
5858
Using this, we can symbolically build the Jacobian and then rebuild the ODEProblem:
5959

6060
```@example mtkize
61-
prob_jac = ODEProblem(sys, [], (0.0, 1e5), jac = true)
61+
prob_jac = ODEProblem(complete(sys), [], (0.0, 1e5), jac = true)
6262
```

docs/src/tutorials/nonlinear.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ eqs = [0 ~ σ * (y - x),
1717
0 ~ x * (ρ - z) - y,
1818
0 ~ x * y - β * z]
1919
@named ns = NonlinearSystem(eqs, [x, y, z], [σ, ρ, β])
20+
ns = complete(ns)
2021
2122
guess = [x => 1.0,
2223
y => 0.0,

docs/src/tutorials/optimization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ u0 = [x => 1.0
5050
p = [a => 1.0
5151
b => 100.0]
5252
53-
prob = OptimizationProblem(sys, u0, p, grad = true, hess = true)
53+
prob = OptimizationProblem(complete(sys), u0, p, grad = true, hess = true)
5454
solve(prob, GradientDescent())
5555
```
5656

@@ -71,7 +71,7 @@ cons = [
7171
@named sys = OptimizationSystem(loss, [x, y], [a, b], constraints = cons)
7272
u0 = [x => 0.14
7373
y => 0.14]
74-
prob = OptimizationProblem(sys, u0, grad = true, hess = true, cons_j = true, cons_h = true)
74+
prob = OptimizationProblem(complete(sys), u0, grad = true, hess = true, cons_j = true, cons_h = true)
7575
solve(prob, IPNewton())
7676
```
7777

docs/src/tutorials/stochastic_diffeq.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ noiseeqs = [0.1 * x,
2424
0.1 * z]
2525
2626
@named de = SDESystem(eqs, noiseeqs, t, [x, y, z], [σ, ρ, β])
27+
de = complete(de)
2728
2829
u0map = [
2930
x => 1.0,

ext/MTKBifurcationKitExt.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ function BifurcationKit.BifurcationProblem(nsys::NonlinearSystem,
8888
record_from_solution = BifurcationKit.record_sol_default,
8989
jac = true,
9090
kwargs...)
91+
if !ModelingToolkit.iscomplete(nsys)
92+
error("A completed `NonlinearSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`")
93+
end
9194
# Creates F and J functions.
9295
ofun = NonlinearFunction(nsys; jac = jac)
9396
F = ofun.f
@@ -133,11 +136,14 @@ end
133136

134137
# When input is a ODESystem.
135138
function BifurcationKit.BifurcationProblem(osys::ODESystem, args...; kwargs...)
139+
if !ModelingToolkit.iscomplete(osys)
140+
error("A completed `ODESystem` is required. Call `complete` or `structural_simplify` on the system before creating a `BifurcationProblem`")
141+
end
136142
nsys = NonlinearSystem([0 ~ eq.rhs for eq in equations(osys)],
137143
unknowns(osys),
138144
parameters(osys);
139145
name = nameof(osys))
140-
return BifurcationKit.BifurcationProblem(nsys, args...; kwargs...)
146+
return BifurcationKit.BifurcationProblem(complete(nsys), args...; kwargs...)
141147
end
142148

143149
end # module

0 commit comments

Comments
 (0)