Skip to content

Commit b328c1b

Browse files
Merge pull request #492 from SciML/patch
Jacobians safe for LabelledArrays
2 parents 419e6c2 + 9f4e441 commit b328c1b

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
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 = ["Chris Rackauckas <accounts@chrisrackauckas.com>"]
4-
version = "3.12.0"
4+
version = "3.12.1"
55

66
[deps]
77
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/build_function.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,14 @@ function _build_function(target::JuliaTarget, rhss, args...;
336336
arr_sys_expr = (typeof(rhss) <: Vector || typeof(rhss) <: Matrix) && !(eltype(rhss) <: AbstractArray) ? quote
337337
if typeof($(fargs.args[1])) <: Union{ModelingToolkit.StaticArrays.SArray,ModelingToolkit.LabelledArrays.SLArray}
338338
$xname = ModelingToolkit.StaticArrays.@SArray $arr_sys_expr
339-
convert(typeof($(fargs.args[1])),$xname)
339+
if $(typeof(rhss) <: Vector) # Only try converting if it should match `u`
340+
convert(typeof($(fargs.args[1])),$xname)
341+
else
342+
$xname
343+
end
340344
else
341345
$xname = $arr_sys_expr
342-
if !(typeof($(fargs.args[1])) <: Array)
346+
if !(typeof($(fargs.args[1])) <: Array) && $(typeof(rhss) <: Vector)
343347
convert(typeof($(fargs.args[1])),$xname)
344348
else
345349
$xname

test/labelledarrays.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ eqs = [D(x) ~ σ*(y-x),
1313
D(z) ~ x*y - β*z]
1414

1515
de = ODESystem(eqs)
16-
f = ODEFunction(de, [x,y,z], [σ,ρ,β])
16+
ff = ODEFunction(de, [x,y,z], [σ,ρ,β], jac=true)
1717

1818
a = @SVector [1.0,2.0,3.0]
1919
b = SLVector(x=1.0,y=2.0,z=3.0)
2020
c = [1.0,2.0,3.0]
2121
p = SLVector=10.0=26.0=8/3)
22-
@test f(a,p,0.0) isa SVector
23-
@test typeof(f(b,p,0.0)) <: SLArray
24-
@test f(c,p,0.0) isa Vector
25-
@test f(a,p,0.0) == f(b,p,0.0)
26-
@test f(a,p,0.0) == f(c,p,0.0)
22+
@test ff(a,p,0.0) isa SVector
23+
@test typeof(ff(b,p,0.0)) <: SLArray
24+
@test ff(c,p,0.0) isa Vector
25+
@test ff(a,p,0.0) == ff(b,p,0.0)
26+
@test ff(a,p,0.0) == ff(c,p,0.0)
27+
28+
@test ff.jac(a,p,0.0) isa SMatrix
29+
@test typeof(ff.jac(b,p,0.0)) <: SMatrix
30+
@test ff.jac(c,p,0.0) isa Matrix
31+
@test ff.jac(a,p,0.0) == ff.jac(b,p,0.0)
32+
@test ff.jac(a,p,0.0) == ff.jac(c,p,0.0)

0 commit comments

Comments
 (0)