Skip to content

Commit 0d73cfd

Browse files
committed
Merge branch 'master' into myb/ss
2 parents e4f643e + 963634b commit 0d73cfd

27 files changed

+439
-165
lines changed

.github/workflows/Downstream.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
os: [ubuntu-latest]
1919
package:
2020
- {user: SciML, repo: Catalyst.jl, group: All}
21+
- {user: SciML, repo: CellMLToolkit.jl, group: All}
2122
- {user: SciML, repo: NeuralPDE.jl, group: NNPDE}
2223
- {user: SciML, repo: DataDrivenDiffEq.jl, group: Standard}
2324

Project.toml

Lines changed: 6 additions & 5 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 = "5.13.6"
4+
version = "5.14.2"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
@@ -51,7 +51,7 @@ Distributions = "0.23, 0.24"
5151
DocStringExtensions = "0.7, 0.8"
5252
IfElse = "0.1"
5353
LabelledArrays = "1.3"
54-
Latexify = "0.11, 0.12, 0.13, 0.14"
54+
Latexify = "0.11, 0.12, 0.13, 0.14, 0.15"
5555
LightGraphs = "1.3"
5656
MacroTools = "0.5"
5757
NaNMath = "0.3"
@@ -65,8 +65,8 @@ SciMLBase = "1.3"
6565
Setfield = "0.7"
6666
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
6767
StaticArrays = "0.10, 0.11, 0.12, 1.0"
68-
SymbolicUtils = "0.8.3, 0.9"
69-
Symbolics = "0.1.8"
68+
SymbolicUtils = "0.10.1"
69+
Symbolics = "0.1.14"
7070
UnPack = "0.1, 1.0"
7171
Unitful = "1.1"
7272
julia = "1.2"
@@ -81,7 +81,8 @@ OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
8181
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
8282
SteadyStateDiffEq = "9672c7b4-1e72-59bd-8a11-6ac3964bc41f"
8383
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
84+
Sundials = "c3572dad-4567-51f8-b174-8c6c989267f4"
8485
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8586

8687
[targets]
87-
test = ["BenchmarkTools", "ForwardDiff", "GalacticOptim", "NonlinearSolve", "OrdinaryDiffEq", "Optim", "Random", "SteadyStateDiffEq", "Test", "StochasticDiffEq"]
88+
test = ["BenchmarkTools", "ForwardDiff", "GalacticOptim", "NonlinearSolve", "OrdinaryDiffEq", "Optim", "Random", "SteadyStateDiffEq", "Test", "StochasticDiffEq", "Sundials"]

docs/src/basics/ContextualVariables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ the `@variable` which is defined by
77
[Symbolics.jl](https://github.com/JuliaSymbolics/Symbolics.jl). For example:
88

99
```julia
10-
@variabes x y(x)
10+
@variables x y(x)
1111
```
1212

1313
This is used for the "normal" variable of a given system, like the states of a

docs/src/tutorials/ode_modeling.md

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

16-
@variables t, x(t) RHS(t) # independent and dependent variables
16+
@variables t x(t) RHS(t) # independent and dependent variables
1717
@parameters τ # parameters
1818
D = Differential(t) # define an operator for the differentiation w.r.t. time
1919

@@ -51,7 +51,7 @@ first set the forcing function to a constant value.
5151
```julia
5252
using ModelingToolkit
5353

54-
@variables t, x(t) # independent and dependent variables
54+
@variables t x(t) # independent and dependent variables
5555
@parameters τ # parameters
5656
D = Differential(t) # define an operator for the differentiation w.r.t. time
5757

docs/src/tutorials/tearing_parallelism.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function connect_heat(ps...)
3737
end
3838

3939
# Basic electric components
40-
const t = Sym{ModelingToolkit.Parameter{Real}}(:t)
40+
@parameters t
4141
const D = Differential(t)
4242
function Pin(;name)
4343
@variables v(t) i(t)

examples/electrical_components.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ using Test
22
using ModelingToolkit, OrdinaryDiffEq
33

44
# Basic electric components
5-
#const t = Sym{ModelingToolkit.Parameter{Real}}(:t)
65
@parameters t
76
function Pin(;name)
87
@variables v(t) i(t)

src/ModelingToolkit.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ include("structural_transformation/StructuralTransformations.jl")
137137
@reexport using .StructuralTransformations
138138

139139
export ODESystem, ODEFunction, ODEFunctionExpr, ODEProblemExpr
140+
export DAEFunctionExpr, DAEProblemExpr
140141
export SDESystem, SDEFunction, SDEFunctionExpr, SDESystemExpr
141142
export SystemStructure
142143
export JumpSystem
@@ -157,6 +158,7 @@ export Differential, expand_derivatives, @derivatives
157158
export IntervalDomain, ProductDomain, , CircleDomain
158159
export Equation, ConstrainedEquation
159160
export Term, Sym
161+
export SymScope, LocalScope, ParentScope, GlobalScope
160162
export independent_variable, states, parameters, equations, controls, observed, structure
161163
export structural_simplify
162164

src/parameters.jl

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
import SymbolicUtils: symtype, term
2-
struct Parameter{T} end
1+
import SymbolicUtils: symtype, term, hasmetadata
2+
struct MTKParameterCtx end
33

4+
isparameter(x::Num) = isparameter(value(x))
5+
isparameter(x::Symbolic) = getmetadata(x, MTKParameterCtx, false)
46
isparameter(x) = false
5-
isparameter(::Sym{<:Parameter}) = true
6-
isparameter(::Sym{<:FnType{<:Any, <:Parameter}}) = true
77

8-
SymbolicUtils.@number_methods(Sym{Parameter{Real}},
9-
term(f, a),
10-
term(f, a, b), skipbasics)
8+
"""
9+
toparam(s::Sym)
1110
12-
SymbolicUtils.symtype(s::Symbolic{Parameter{T}}) where T = T
13-
SymbolicUtils.similarterm(t::Term{<:Parameter}, f, args) = Term(f, args)
11+
Maps the variable to a paramter.
12+
"""
13+
toparam(s::Symbolic) = setmetadata(s, MTKParameterCtx, true)
14+
toparam(s::Num) = Num(toparam(value(s)))
1415

15-
Base.convert(::Type{Num}, x::Symbolic{Parameter{T}}) where {T<:Number} = Num(x)
16+
"""
17+
tovar(s::Sym)
18+
19+
Maps the variable to a state.
20+
"""
21+
tovar(s::Symbolic) = setmetadata(s, MTKParameterCtx, false)
22+
tovar(s::Num) = Num(tovar(value(s)))
1623

1724
"""
1825
$(SIGNATURES)
1926
2027
Define one or more known variables.
2128
"""
2229
macro parameters(xs...)
23-
esc(_parse_vars(:parameters, Parameter{Real}, xs))
30+
Symbolics._parse_vars(:parameters,
31+
Real,
32+
xs,
33+
x -> x isa Array ? toparam.(x) : toparam(x)
34+
) |> esc
2435
end

src/structural_transformation/codegen.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function partitions_dag(s::SystemStructure)
124124
sparse(I, J, true, n, n)
125125
end
126126

127-
function gen_nlsolve(sys, eqs, vars)
127+
function gen_nlsolve(sys, eqs, vars; checkbounds=true)
128128
@assert !isempty(vars)
129129
@assert length(eqs) == length(vars)
130130
rhss = map(x->x.rhs, eqs)
@@ -142,8 +142,8 @@ function gen_nlsolve(sys, eqs, vars)
142142
fname = gensym("fun")
143143
f = Func(
144144
[
145-
DestructuredArgs(vars)
146-
DestructuredArgs(params)
145+
DestructuredArgs(vars, inbounds=!checkbounds)
146+
DestructuredArgs(params, inbounds=!checkbounds)
147147
],
148148
[],
149149
isscalar ? rhss[1] : MakeArray(rhss, SVector)
@@ -161,7 +161,7 @@ function gen_nlsolve(sys, eqs, vars)
161161

162162
[
163163
fname @RuntimeGeneratedFunction(f)
164-
DestructuredArgs(vars) solver_call
164+
DestructuredArgs(vars, inbounds=!checkbounds) solver_call
165165
]
166166
end
167167

@@ -205,8 +205,8 @@ function build_torn_function(
205205
Func(
206206
[
207207
out
208-
DestructuredArgs(states)
209-
DestructuredArgs(parameters(sys))
208+
DestructuredArgs(states, inbounds=!checkbounds)
209+
DestructuredArgs(parameters(sys), inbounds=!checkbounds)
210210
independent_variable(sys)
211211
],
212212
[],
@@ -222,7 +222,7 @@ function build_torn_function(
222222
observedfun = let sys = sys, dict = Dict()
223223
function generated_observed(obsvar, u, p, t)
224224
obs = get!(dict, value(obsvar)) do
225-
build_observed_function(sys, obsvar)
225+
build_observed_function(sys, obsvar, checkbounds=checkbounds)
226226
end
227227
obs(u, p, t)
228228
end
@@ -257,7 +257,8 @@ end
257257
function build_observed_function(
258258
sys, syms;
259259
expression=false,
260-
output_type=Array
260+
output_type=Array,
261+
checkbounds=true
261262
)
262263

263264
if (isscalar = !(syms isa Vector))
@@ -293,7 +294,7 @@ function build_observed_function(
293294
torn_eqs = map(idxs-> eqs[idxs.e_residual], subset)
294295
torn_vars = map(idxs->fullvars[idxs.v_residual], subset)
295296

296-
solves = gen_nlsolve.((sys,), torn_eqs, torn_vars)
297+
solves = gen_nlsolve.((sys,), torn_eqs, torn_vars; checkbounds=checkbounds)
297298
else
298299
solves = []
299300
end
@@ -308,8 +309,8 @@ function build_observed_function(
308309

309310
ex = Func(
310311
[
311-
DestructuredArgs(diffvars)
312-
DestructuredArgs(parameters(sys))
312+
DestructuredArgs(diffvars, inbounds=!checkbounds)
313+
DestructuredArgs(parameters(sys), inbounds=!checkbounds)
313314
independent_variable(sys)
314315
],
315316
[],

src/structural_transformation/pantelides.jl

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,6 @@
22
### Reassemble: structural information -> system
33
###
44

5-
# Naive subtree matching, we can make the dict have levels
6-
# Going forward we should look into storing the depth in Terms
7-
function walk_and_substitute(expr, substitution_dict)
8-
if haskey(substitution_dict, expr)
9-
return substitution_dict[expr]
10-
elseif istree(expr)
11-
return similarterm(
12-
expr,
13-
walk_and_substitute(operation(expr), substitution_dict),
14-
map(x->walk_and_substitute(x, substitution_dict), arguments(expr))
15-
)
16-
else
17-
@assert !(expr isa Equation) "RHS cannot contain equations"
18-
return expr
19-
end
20-
end
21-
225
function pantelides_reassemble(sys, eqassoc, assign)
236
s = structure(sys)
247
@unpack fullvars, varassoc = s
@@ -74,7 +57,7 @@ function pantelides_reassemble(sys, eqassoc, assign)
7457
end
7558
rhs = ModelingToolkit.expand_derivatives(D(eq.rhs))
7659
substitution_dict = Dict(x.lhs => x.rhs for x in out_eqs if x !== nothing && x.lhs isa Symbolic)
77-
sub_rhs = walk_and_substitute(rhs, substitution_dict)
60+
sub_rhs = substitute(rhs, substitution_dict)
7861
out_eqs[e] = lhs ~ sub_rhs
7962
end
8063

0 commit comments

Comments
 (0)