Skip to content

Commit 5be0654

Browse files
authored
Fix container keyword not aknowledged in trajectory (#258)
* Fix `container` keyword not aknowledged in `trajectory` * fix mistake in rteturn value
1 parent 567d298 commit 5be0654

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v3.18.1
2+
3+
- Fixed a key bug in `trajectory` where it was not respecting the `container` keyword.
4+
15
# v3.18
26

37
- The public API functions `referrenced_sciml_prob`, `referrenced_sciml_model`, and the internal `has_referrenced_model` have been renamed to fix the misspelling: `referenced_sciml_prob`, `referenced_sciml_model`, `has_referenced_model`. The misspelled names are kept as deprecated aliases that forward to the new ones, so existing code keeps working but emits a deprecation warning. `referenced_sciml_prob` is now also exported and documented.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicalSystemsBase"
22
uuid = "6e36e845-645a-534a-86f2-f5d4aa5a06b4"
33
repo = "https://github.com/JuliaDynamics/DynamicalSystemsBase.jl.git"
4-
version = "3.18.0"
4+
version = "3.18.1"
55

66
[deps]
77
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"

src/core/trajectory.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,45 @@ trajectory (but remember to convert the output of `solve` to a `StateSpaceSet`).
3939
"""
4040
function trajectory(
4141
ds::DynamicalSystem, T, u0 = current_state(ds);
42-
save_idxs = nothing, t0 = initial_time(ds), kwargs...
42+
save_idxs = nothing, t0 = initial_time(ds), container = SVector, kwargs...
4343
)
4444
accessor = svector_access(save_idxs)
4545
reinit!(ds, u0; t0)
46+
X = isnothing(accessor) ? dimension(ds) : length(accessor)
47+
t = eltype(u0)
48+
if container <: SVector
49+
U = SVector{X, t}
50+
elseif container <: MVector
51+
U = MVector{X, t}
52+
else
53+
U = Vector{t}
54+
end
4655
if isdiscretetime(ds)
47-
X, tvec = trajectory_discrete(ds, T; accessor, kwargs...)
56+
X, tvec = trajectory_discrete(ds, T, U; accessor, kwargs...)
4857
else
49-
X, tvec = trajectory_continuous(ds, T; accessor, kwargs...)
58+
X, tvec = trajectory_continuous(ds, T, U; accessor, kwargs...)
5059
end
5160
# name automatically if possible
5261
if isnothing(save_idxs)
53-
X = StateSpaceSet(X; names = named_variables(ds))
62+
return StateSpaceSet(X; container, names = named_variables(ds)), tvec
63+
else
64+
return StateSpaceSet(X; container), tvec
5465
end
55-
return X, tvec
5666
end
5767

5868
function trajectory_discrete(
59-
ds, T;
69+
ds, T, U;
6070
Dt::Integer = 1, Δt::Integer = Dt, Ttr::Integer = 0, accessor = nothing,
61-
kw... # container keyword
6271
)
63-
ET = eltype(current_state(ds))
6472
t0 = current_time(ds)
6573
tvec = (t0 + Ttr):Δt:(t0 + T + Ttr)
6674
L = length(tvec)
67-
X = isnothing(accessor) ? dimension(ds) : length(accessor)
68-
data = Vector{SVector{X, ET}}(undef, L)
75+
data = Vector{U}(undef, L)
6976
Ttr 0 && step!(ds, Ttr)
7077
data[1] = obtain_state(current_state(ds), accessor)
7178
for i in 2:L
7279
step!(ds, Δt)
73-
data[i] = SVector{X, ET}(obtain_state(ds, current_time(ds), accessor))
80+
data[i] = U(obtain_state(ds, current_time(ds), accessor))
7481
if !successful_step(ds)
7582
# Diverged trajectory; set final state to remaining set
7683
# and exit iteration early
@@ -80,23 +87,20 @@ function trajectory_discrete(
8087
break
8188
end
8289
end
83-
return StateSpaceSet(data; kw...), tvec
90+
return data, tvec
8491
end
8592

86-
function trajectory_continuous(ds, T; Dt = 0.1, Δt = Dt, Ttr = 0.0, accessor = nothing, kw...)
87-
D = dimension(ds)
93+
function trajectory_continuous(ds, T, U; Dt = 0.1, Δt = Dt, Ttr = 0.0, accessor = nothing)
8894
t0 = current_time(ds)
8995
tvec = (t0 + Ttr):Δt:(t0 + T + Ttr)
90-
X = isnothing(accessor) ? D : length(accessor)
91-
ET = eltype(current_state(ds))
92-
sol = Vector{SVector{X, ET}}(undef, length(tvec))
96+
sol = Vector{U}(undef, length(tvec))
9397
step!(ds, Ttr)
9498
for (i, t) in enumerate(tvec)
9599
while t > current_time(ds)
96100
step!(ds)
97101
successful_step(ds) || break
98102
end
99-
sol[i] = SVector{X, ET}(obtain_state(ds, t, accessor))
103+
sol[i] = U(obtain_state(ds, t, accessor))
100104
if !successful_step(ds)
101105
# Diverged trajectory; set final state to remaining set
102106
# and exit iteration early
@@ -107,7 +111,7 @@ function trajectory_continuous(ds, T; Dt = 0.1, Δt = Dt, Ttr = 0.0, accessor =
107111
end
108112

109113
end
110-
return StateSpaceSet(sol; kw...), tvec
114+
return sol, tvec
111115
end
112116

113117
# Util functions for `trajectory` to make indexing static vectors

test/trajectory.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ using Test
1919
x, t = trajectory(ds, N - 1)
2020
@test length(x) > 1
2121
end
22+
23+
@testset "container" begin
24+
u0 = zeros(2)
25+
p0 = [1.4, 0.3]
26+
henon_rule(x, p, n) = SVector{2}(1.0 - p[1] * x[1]^2 + x[2], p[2] * x[1])
27+
henon_oop = DeterministicIteratedMap(henon_rule, u0, p0)
28+
29+
x, t = trajectory(henon_oop, 100)
30+
@test eltype(vec(x)) <: SVector
31+
x, t = trajectory(henon_oop, 100; container = Vector)
32+
@test eltype(vec(x)) <: Vector
33+
end

0 commit comments

Comments
 (0)