Skip to content

Commit 4293364

Browse files
authored
Merge branch 'SciML:master' into iss3707
2 parents 9237e47 + b0907ec commit 4293364

File tree

9 files changed

+122
-7
lines changed

9 files changed

+122
-7
lines changed

.github/workflows/Tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
- Downstream
4242
- RegressionI
4343
- FMI
44-
uses: "SciML/.github/.github/workflows/tests.yml@v1"
44+
uses: "SciML/.github/.github/workflows/tests.yml@master"
4545
with:
4646
julia-version: "${{ matrix.version }}"
4747
group: "${{ matrix.group }}"

Project.toml

Lines changed: 2 additions & 2 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 = ["Yingbo Ma <[email protected]>", "Chris Rackauckas <[email protected]> and contributors"]
4-
version = "10.10.0"
4+
version = "10.13.0"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
@@ -151,7 +151,7 @@ RecursiveArrayTools = "3.26"
151151
Reexport = "0.2, 1"
152152
RuntimeGeneratedFunctions = "0.5.9"
153153
SCCNonlinearSolve = "1.0.0"
154-
SciMLBase = "2.100.0"
154+
SciMLBase = "2.104.0"
155155
SciMLPublic = "1.0.0"
156156
SciMLStructures = "1.7"
157157
Serialization = "1"

src/ModelingToolkit.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ include("problems/docs.jl")
176176
include("systems/codegen.jl")
177177
include("systems/problem_utils.jl")
178178
include("linearization.jl")
179+
include("systems/solver_nlprob.jl")
179180

180181
include("problems/compatibility.jl")
181182
include("problems/odeproblem.jl")

src/linearization.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ function reorder_unknowns(sys::NamedTuple, old, new)
852852
issorted(perm) && return sys # shortcut return, no reordering
853853
P = zeros(Int, nx, nx)
854854
for i in 1:nx # Build permutation matrix
855-
P[i, perm[i]] = 1
855+
P[perm[i], i] = 1
856856
end
857857
similarity_transform(sys, P; unitary = true)
858858
end

src/problems/odeproblem.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
t = nothing, eval_expression = false, eval_module = @__MODULE__, sparse = false,
44
steady_state = false, checkbounds = false, sparsity = false, analytic = nothing,
55
simplify = false, cse = true, initialization_data = nothing, expression = Val{false},
6-
check_compatibility = true, kwargs...) where {iip, spec}
6+
check_compatibility = true, nlstep = false, kwargs...) where {iip, spec}
77
check_complete(sys, ODEFunction)
88
check_compatibility && check_compatible_system(ODEFunction, sys)
99

@@ -41,6 +41,12 @@
4141
M = calculate_massmatrix(sys)
4242
_M = concrete_massmatrix(M; sparse, u0)
4343

44+
if nlstep
45+
ode_nlstep = generate_ODENLStepData(sys, u0, p, M)
46+
else
47+
ode_nlstep = nothing
48+
end
49+
4450
observedfun = ObservedFunctionCache(
4551
sys; expression, steady_state, eval_expression, eval_module, checkbounds, cse)
4652

@@ -57,7 +63,8 @@
5763
observed = observedfun,
5864
sparsity = sparsity ? _W_sparsity : nothing,
5965
analytic = analytic,
60-
initialization_data)
66+
initialization_data,
67+
nlstep_data = ode_nlstep)
6168

6269
maybe_codegen_scimlfn(expression, ODEFunction{iip, spec}, args; kwargs...)
6370
end

src/systems/abstractsystem.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,9 @@ function process_parameter_equations(sys::AbstractSystem)
27572757
is_parameter(sys, sym) ||
27582758
symbolic_type(sym) == ArraySymbolic() &&
27592759
is_sized_array_symbolic(sym) &&
2760-
all(Base.Fix1(is_parameter, sys), collect(sym))
2760+
all(Base.Fix1(is_parameter, sys), collect(sym)) ||
2761+
iscall(sym) &&
2762+
operation(sym) === getindex && is_parameter(sys, arguments(sym)[1])
27612763
end
27622764
# Everything in `varsbuf` is a parameter, so this is a cheap `is_parameter`
27632765
# check.

src/systems/solver_nlprob.jl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
function generate_ODENLStepData(sys::System, u0, p, mm = calculate_massmatrix(sys))
2+
nlsys, outer_tmp, inner_tmp = inner_nlsystem(sys, mm)
3+
state = ProblemState(; u = u0, p)
4+
op = Dict()
5+
op[ODE_GAMMA[1]] = one(eltype(u0))
6+
op[ODE_GAMMA[2]] = one(eltype(u0))
7+
op[ODE_GAMMA[3]] = one(eltype(u0))
8+
op[ODE_C] = zero(eltype(u0))
9+
op[outer_tmp] = zeros(eltype(u0), size(outer_tmp))
10+
op[inner_tmp] = zeros(eltype(u0), size(inner_tmp))
11+
for v in [unknowns(nlsys); parameters(nlsys)]
12+
haskey(op, v) && continue
13+
op[v] = getsym(sys, v)(state)
14+
end
15+
nlprob = NonlinearProblem(nlsys, op; build_initializeprob = false)
16+
17+
subsetidxs = [findfirst(isequal(y),unknowns(sys)) for y in unknowns(nlsys)]
18+
set_gamma_c = setsym(nlsys, (ODE_GAMMA..., ODE_C))
19+
set_outer_tmp = setsym(nlsys, outer_tmp)
20+
set_inner_tmp = setsym(nlsys, inner_tmp)
21+
nlprobmap = generate_nlprobmap(sys, nlsys)
22+
23+
return SciMLBase.ODENLStepData(nlprob, subsetidxs, set_gamma_c, set_outer_tmp, set_inner_tmp, nlprobmap)
24+
end
25+
26+
const ODE_GAMMA = @parameters γ₁ₘₜₖ, γ₂ₘₜₖ, γ₃ₘₜₖ
27+
const ODE_C = only(@parameters cₘₜₖ)
28+
29+
function get_outer_tmp(n::Int)
30+
only(@parameters outer_tmpₘₜₖ[1:n])
31+
end
32+
33+
function get_inner_tmp(n::Int)
34+
only(@parameters inner_tmpₘₜₖ[1:n])
35+
end
36+
37+
function inner_nlsystem(sys::System, mm)
38+
dvs = unknowns(sys)
39+
eqs = full_equations(sys)
40+
t = get_iv(sys)
41+
N = length(dvs)
42+
@assert length(eqs) == N
43+
@assert mm == I || size(mm) == (N, N)
44+
rhss = [eq.rhs for eq in eqs]
45+
gamma1, gamma2, gamma3 = ODE_GAMMA
46+
c = ODE_C
47+
outer_tmp = get_outer_tmp(N)
48+
inner_tmp = get_inner_tmp(N)
49+
50+
subrules = Dict([v => gamma2*v + inner_tmp[i] for (i, v) in enumerate(dvs)])
51+
subrules[t] = c
52+
new_rhss = map(Base.Fix2(fast_substitute, subrules), rhss)
53+
new_rhss = collect(outer_tmp) .+ gamma1 .* new_rhss .- gamma3 * mm * dvs
54+
new_eqs = [0 ~ rhs for rhs in new_rhss]
55+
56+
new_dvs = unknowns(sys)
57+
new_ps = [parameters(sys); [gamma1, gamma2, gamma3, c, inner_tmp, outer_tmp]]
58+
nlsys = mtkcompile(System(new_eqs, new_dvs, new_ps; name = :nlsys); split = is_split(sys))
59+
return nlsys, outer_tmp, inner_tmp
60+
end
61+
62+
struct NLStep_probmap{F}
63+
f::F
64+
end
65+
66+
function (nlp::NLStep_probmap)(buffer, nlsol)
67+
nlp.f(buffer, state_values(nlsol), parameter_values(nlsol))
68+
end
69+
70+
function (nlp::NLStep_probmap)(nlsol)
71+
nlp.f(state_values(nlsol), parameter_values(nlsol))
72+
end
73+
74+
function generate_nlprobmap(sys::System, nlsys::System)
75+
return NLStep_probmap(build_explicit_observed_function(nlsys, unknowns(sys)))
76+
end

test/linearize.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
using ModelingToolkit, ADTypes, Test
22
using CommonSolve: solve
33

4+
# Test reorder_unknowns
5+
# sys = ssrand(1,1,4);
6+
mats = let
7+
A = [-1.617708540405859 0.14199864151523162 1.8120551022076838 -1.246419696614408;
8+
0.6704209450894298 -2.4251566699889575 0.6782529705706082 -1.3731519847672025;
9+
-0.09336677360807291 -0.11211714788917712 -3.6877851408229523 -0.7073967284605489;
10+
-1.1743200892334098 1.1808779444006103 1.5721685015907167 -0.10858833182921268]
11+
B = [-0.3286766047686936; -1.8473436385672866; -2.4092567234250954; -0.06371974677173559;;]
12+
C = [-0.7144567541084362 0.18898849455229796 0.023473101245754475 1.0369097263843963]
13+
D = [0.6397583934617636;;]
14+
(; A, B, C, D)
15+
end
16+
@variables x1 x2 x3 x4
17+
new = [x4, x1, x3, x2]
18+
old = [x1, x2, x3, x4]
19+
lsys = ModelingToolkit.reorder_unknowns(mats, old, new)
20+
P = [0 1 0 0; 0 0 0 1; 0 0 1 0; 1 0 0 0]
21+
@test isequal(P*new, old)
22+
@test lsys.A == ModelingToolkit.similarity_transform(mats, P).A
23+
424
# r is an input, and y is an output.
525
@independent_variables t
626
@variables x(t)=0 y(t)=0 u(t)=0 r(t)=0

test/parameter_dependencies.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,12 @@ end
389389
[x(k - 1) ~ x(k) + y(k) + p2, p2 ~ 2p1], t)
390390
@test is_parameter(sys, p1)
391391
end
392+
393+
@testset "Scalarized array as RHS of parameter dependency" begin
394+
@parameters p[1:2] p1 p2
395+
@variables x(t)
396+
@named sys = System([D(x) ~ x, p1 ~ p[1], p2 ~ p[2]], t)
397+
@test any(isequal(p), ModelingToolkit.get_ps(sys))
398+
sys = mtkcompile(sys)
399+
@test length(ModelingToolkit.parameter_dependencies(sys)) == 2
400+
end

0 commit comments

Comments
 (0)