Skip to content

Commit 5fa998e

Browse files
author
dd
committed
Merge branch 'func-affect' of github.com:dodoplus/ModelingToolkit.jl into func-affect
2 parents 21b2d96 + 2dc75f3 commit 5fa998e

22 files changed

+342
-458
lines changed

Project.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ModelingToolkit"
22
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "8.14.1"
4+
version = "8.15.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -11,7 +11,6 @@ ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
1111
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1212
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
1313
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
14-
DiffEqJump = "c894b116-72e5-5b58-be3c-e6d8d4ac2b12"
1514
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
1615
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
1716
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
@@ -21,6 +20,7 @@ Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
2120
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
2221
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
2322
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
23+
JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5"
2424
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
2525
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
2626
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
@@ -30,7 +30,6 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
3030
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
3131
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
3232
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
33-
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
3433
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
3534
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
3635
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
@@ -51,22 +50,21 @@ ConstructionBase = "1"
5150
DataStructures = "0.17, 0.18"
5251
DiffEqBase = "6.83.0"
5352
DiffEqCallbacks = "2.16"
54-
DiffEqJump = "7.0, 8"
5553
DiffRules = "0.1, 1.0"
5654
Distributions = "0.23, 0.24, 0.25"
5755
DocStringExtensions = "0.7, 0.8, 0.9"
5856
DomainSets = "0.5"
5957
Graphs = "1.5.2"
6058
IfElse = "0.1"
6159
JuliaFormatter = "1"
60+
JumpProcesses = "9"
6261
LabelledArrays = "1.3"
6362
Latexify = "0.11, 0.12, 0.13, 0.14, 0.15"
6463
MacroTools = "0.5"
6564
NaNMath = "0.3, 1"
6665
NonlinearSolve = "0.3.8"
6766
RecursiveArrayTools = "2.3"
6867
Reexport = "0.2, 1"
69-
Requires = "1.0"
7068
RuntimeGeneratedFunctions = "0.4.3, 0.5"
7169
SciMLBase = "1.26.2"
7270
Setfield = "0.7, 0.8, 1"
@@ -79,9 +77,13 @@ Unitful = "1.1"
7977
julia = "1.6"
8078

8179
[extras]
80+
AmplNLWriter = "7c4d4715-977e-5154-bfe0-e096adeac482"
8281
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
8382
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
83+
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
84+
Ipopt_jll = "9cc047cb-c261-5740-88fc-0cf96f7bdcc7"
8485
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
86+
OptimizationMOI = "fd9f6733-72f4-499f-8506-86b2bdd0dea1"
8587
OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e"
8688
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
8789
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -93,4 +95,4 @@ Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
9395
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9496

9597
[targets]
96-
test = ["BenchmarkTools", "ForwardDiff", "Optimization", "OptimizationOptimJL", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials"]
98+
test = ["AmplNLWriter", "BenchmarkTools", "ForwardDiff", "Ipopt", "Ipopt_jll", "Optimization", "OptimizationOptimJL", "OptimizationMOI", "OrdinaryDiffEq", "Random", "ReferenceTests", "SafeTestsets", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials"]

docs/pages.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pages = [
88
"tutorials/nonlinear.md",
99
"tutorials/optimization.md",
1010
"tutorials/stochastic_diffeq.md",
11-
"tutorials/nonlinear_optimal_control.md",
1211
"tutorials/parameter_identifiability.md"],
1312
"ModelingToolkitize Tutorials" => Any["mtkitize_tutorials/modelingtoolkitize.md",
1413
"mtkitize_tutorials/modelingtoolkitize_index_reduction.md",
@@ -25,7 +24,6 @@ pages = [
2524
"systems/JumpSystem.md",
2625
"systems/NonlinearSystem.md",
2726
"systems/OptimizationSystem.md",
28-
"systems/ControlSystem.md",
2927
"systems/PDESystem.md"],
3028
"comparison.md",
3129
"internals.md",

docs/src/systems/ControlSystem.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/src/tutorials/nonlinear_optimal_control.md

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/ModelingToolkit.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Latexify, Unitful, ArrayInterfaceCore
1212
using MacroTools
1313
@reexport using UnPack
1414
using Setfield, ConstructionBase
15-
using DiffEqJump
15+
using JumpProcesses
1616
using DataStructures
1717
using SpecialFunctions, NaNMath
1818
using RuntimeGeneratedFunctions
@@ -60,8 +60,6 @@ import DiffEqBase: @add_kwonly
6060

6161
import Graphs: SimpleDiGraph, add_edge!, incidence_matrix
6262

63-
using Requires
64-
6563
for fun in [:toexpr]
6664
@eval begin
6765
function $fun(eq::Equation; kw...)
@@ -137,8 +135,6 @@ include("systems/nonlinear/modelingtoolkitize.jl")
137135

138136
include("systems/optimization/optimizationsystem.jl")
139137

140-
include("systems/control/controlsystem.jl")
141-
142138
include("systems/pde/pdesystem.jl")
143139

144140
include("systems/sparsematrixclil.jl")
@@ -173,13 +169,11 @@ export AutoModelingToolkit
173169
export SteadyStateProblem, SteadyStateProblemExpr
174170
export JumpProblem, DiscreteProblem
175171
export NonlinearSystem, OptimizationSystem
176-
export ControlSystem
177172
export alias_elimination, flatten
178173
export connect, @connector, Connection, Flow, Stream, instream
179174
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
180175
tunable_parameters, isirreducible
181176
export ode_order_lowering, dae_order_lowering, liouville_transform
182-
export runge_kutta_discretize
183177
export PDESystem
184178
export Differential, expand_derivatives, @derivatives
185179
export Equation, ConstrainedEquation

src/bipartite_graph.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,53 @@ function complete(g::BipartiteGraph{I}) where {I}
184184
BipartiteGraph(g.ne, g.fadjlist, badjlist)
185185
end
186186

187+
# Matrix whose only purpose is to pretty-print the bipartite graph
188+
struct BipartiteAdjacencyList
189+
u::Union{Vector{Int}, Nothing}
190+
end
191+
function Base.show(io::IO, l::BipartiteAdjacencyList)
192+
if l.u === nothing
193+
printstyled(io, '', color = :light_black)
194+
elseif isempty(l.u)
195+
printstyled(io, '', color = :light_black)
196+
else
197+
print(io, l.u)
198+
end
199+
end
200+
201+
struct Label
202+
s::String
203+
end
204+
Base.show(io::IO, l::Label) = print(io, l.s)
205+
206+
struct BipartiteGraphPrintMatrix <:
207+
AbstractMatrix{Union{Label, Int, BipartiteAdjacencyList}}
208+
bpg::BipartiteGraph
209+
end
210+
Base.size(bgpm::BipartiteGraphPrintMatrix) = (max(nsrcs(bgpm.bpg), ndsts(bgpm.bpg)) + 1, 3)
211+
function Base.getindex(bgpm::BipartiteGraphPrintMatrix, i::Integer, j::Integer)
212+
checkbounds(bgpm, i, j)
213+
if i == 1
214+
return (Label.(("#", "src", "dst")))[j]
215+
elseif j == 1
216+
return i - 1
217+
elseif j == 2
218+
return BipartiteAdjacencyList(i - 1 <= nsrcs(bgpm.bpg) ?
219+
𝑠neighbors(bgpm.bpg, i - 1) : nothing)
220+
elseif j == 3
221+
return BipartiteAdjacencyList(i - 1 <= ndsts(bgpm.bpg) ?
222+
𝑑neighbors(bgpm.bpg, i - 1) : nothing)
223+
else
224+
@assert false
225+
end
226+
end
227+
228+
function Base.show(io::IO, b::BipartiteGraph)
229+
print(io, "BipartiteGraph with (", length(b.fadjlist), ", ",
230+
isa(b.badjlist, Int) ? b.badjlist : length(b.badjlist), ") (𝑠,𝑑)-vertices\n")
231+
Base.print_matrix(io, BipartiteGraphPrintMatrix(b))
232+
end
233+
187234
"""
188235
```julia
189236
Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer}

src/systems/callbacks.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,14 @@ affect. The generated function has the signature `affect!(integrator)`.
282282
Notes
283283
- `expression = Val{true}`, causes the generated function to be returned as an expression.
284284
If set to `Val{false}` a `RuntimeGeneratedFunction` will be returned.
285-
- `outputidxs`, a vector of indices of the states that correspond to outputs.
285+
- `outputidxs`, a vector of indices of the output variables which should correspond to
286+
`states(sys)`. If provided, checks that the LHS of affect equations are variables are
287+
dropped, i.e. it is assumed these indices are correct and affect equations are
288+
well-formed.
286289
- `kwargs` are passed through to `Symbolics.build_function`.
287290
"""
288291
function compile_affect(eqs::Vector{Equation}, sys, dvs, ps; outputidxs = nothing,
289-
expression = Val{true},
290-
kwargs...)
292+
expression = Val{true}, checkvars = true, kwargs...)
291293
if isempty(eqs)
292294
if expression == Val{true}
293295
return :((args...) -> ())
@@ -299,6 +301,8 @@ function compile_affect(eqs::Vector{Equation}, sys, dvs, ps; outputidxs = nothin
299301
outvar = :u
300302
if outputidxs === nothing
301303
lhss = map(x -> x.lhs, eqs)
304+
all(isvariable, lhss) ||
305+
error("Non-variable symbolic expression found on the left hand side of an affect equation. Such equations must be of the form variable ~ symbolic expression for the new value of the variable.")
302306
update_vars = collect(Iterators.flatten(map(ModelingToolkit.vars, lhss))) # these are the ones we're changing
303307
length(update_vars) == length(unique(update_vars)) == length(eqs) ||
304308
error("affected variables not unique, each state can only be affected by one equation for a single `root_eqs => affects` pair.")
@@ -318,8 +322,13 @@ function compile_affect(eqs::Vector{Equation}, sys, dvs, ps; outputidxs = nothin
318322
update_inds = outputidxs
319323
end
320324

321-
u = map(x -> time_varying_as_func(value(x), sys), dvs)
322-
p = map(x -> time_varying_as_func(value(x), sys), ps)
325+
if checkvars
326+
u = map(x -> time_varying_as_func(value(x), sys), dvs)
327+
p = map(x -> time_varying_as_func(value(x), sys), ps)
328+
else
329+
u = dvs
330+
p = ps
331+
end
323332
t = get_iv(sys)
324333
rf_oop, rf_ip = build_function(rhss, u, p, t; expression = expression,
325334
wrap_code = add_integrator_header(outvar),
@@ -483,4 +492,4 @@ function process_events(sys; callback = nothing, has_difference = false, kwargs.
483492
# cb = merge_cb(cb, callback)
484493
# @show typeof(cbs),cbs
485494
cb
486-
end
495+
end

0 commit comments

Comments
 (0)