Skip to content

Commit 0ac9509

Browse files
authored
ParallelDynamicalSystem for StroboscopicMap and other API (#225)
* add smap for pds systems * fix param setting for generic fallback * add modified set_state! for smap * fix smap psys dispatch * add parallel specific tests * remove Revise * increase patch version
1 parent 6fb6fff commit 0ac9509

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

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.13.1"
4+
version = "3.13.2"
55

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

src/derived_systems/parallel_systems.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,23 @@ function ParallelDynamicalSystem(ds::CoreDynamicalSystem, states::Vector{<:Abstr
6363
pds = CoupledODEs(prob, ds.diffeq; internalnorm = inorm)
6464
end
6565
M = ds isa CoupledODEs && isinplace(ds)
66-
prob = referrenced_sciml_prob(ds)
66+
prob = referrenced_sciml_prob(ds)
6767
return ParallelDynamicalSystemAnalytic{typeof(pds), M}(pds, dynamic_rule(ds), prob)
6868
end
6969

70+
function ParallelDynamicalSystem(smap::StroboscopicMap,states)
71+
f, st = parallel_rule(smap.ds, states)
72+
T = eltype(first(st))
73+
prob = ODEProblem{true}(f, st, (T(initial_time(smap)), T(Inf)), current_parameters(smap))
74+
inorm = prob.u0 isa Matrix ? matrixnorm : vectornorm
75+
cont_pds = CoupledODEs(prob, smap.ds.diffeq; internalnorm = inorm)
76+
pds = StroboscopicMap(cont_pds,smap.period)
77+
78+
M = smap.ds isa CoupledODEs && isinplace(smap.ds)
79+
prob = referrenced_sciml_prob(smap.ds)
80+
return ParallelDynamicalSystemAnalytic{typeof(pds), M}(pds, dynamic_rule(smap), prob)
81+
end
82+
7083
function ParallelDynamicalSystem(ds::CoreDynamicalSystem, mappings::Vector{<:Dict})
7184
# convert to vector of arrays:
7285
u = Array(current_state(ds))
@@ -164,6 +177,14 @@ function set_state!(pdsa::PDSAM, u::AbstractArray, i::Int = 1)
164177
return pdsa
165178
end
166179

180+
181+
function set_state!(pdsa::PDSAM{<: StroboscopicMap}, u::AbstractArray, i::Int = 1)
182+
current_state(pdsa, i) .= u
183+
u_modified!(pdsa.ds.ds.integ, true)
184+
return pdsa
185+
end
186+
187+
167188
# We make one more extension here: for continuous time, in place systems
168189
# the state is a matrix (each column a parallel state) for performance.
169190
# re-init will not work because there is no way to do the recursive copy. we do it ourselves
@@ -210,7 +231,7 @@ current_states(pdtds::PDTDS) = [current_state(ds) for ds in pdtds.systems]
210231
initial_states(pdtds::PDTDS) = [initial_state(ds) for ds in pdtds.systems]
211232

212233
# Set stuff
213-
set_parameter!(pdtds::PDTDS) = for ds in pdtds.systems; set_parameter!(ds, args...); end
234+
set_parameter!(pdtds::PDTDS,index,value) = for ds in pdtds.systems; set_parameter!(ds, index,value); end
214235
function set_state!(pdtds::PDTDS, u, i::Int = 1)
215236
# We need to set state in all systems, in case this does
216237
# some kind of resetting, e.g., the `u_modified!` stuff.

test/parallel.jl

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ for (ds, idt, iip) in zip(
9898
end
9999

100100
@testset "parallel stroboscopic" begin
101-
# Generic Parallel
101+
102102
@inbounds function duffing_rule(x, p, t)
103103
ω, f, d, β = p
104104
dx1 = x[2]
@@ -122,10 +122,44 @@ states = [u0, u0 .+ 0.01]
122122
pds_cont_oop = ParallelDynamicalSystem(duffing_oop, states)
123123
pds_cont_iip = ParallelDynamicalSystem(duffing_iip, deepcopy(states))
124124

125+
#generic ds test
125126
@testset "IIP=$iip" for (ds, iip) in zip((pds_cont_oop, pds_cont_iip,), (true, false))
126127
test_dynamical_system(ds, u0, p0; idt = true, iip = true, test_trajectory = false)
127128
end
129+
130+
#tests for multistate stuff
131+
132+
#alteration
133+
states = [ones(2) for i in 1:2]
134+
p = p0 .+ 0.1
135+
for i in 1:2
136+
set_state!(pds_cont_oop,states[i],i)
137+
set_state!(pds_cont_iip,states[i],i)
138+
end
139+
set_parameters!(pds_cont_oop,p)
140+
set_parameters!(pds_cont_iip,p)
141+
142+
#obtaining info
143+
@test all(current_states(pds_cont_oop) .== states)
144+
@test all(current_states(pds_cont_iip) .== states)
145+
@test all(current_parameters(pds_cont_oop) .== p)
146+
@test all(current_parameters(pds_cont_iip) .== p)
147+
148+
#time evolution
149+
step!(pds_cont_oop)
150+
@test all(current_states(pds_cont_oop)[1] .== current_states(pds_cont_oop)[2])
151+
step!(pds_cont_iip)
152+
@test all(current_states(pds_cont_iip)[1] .== current_states(pds_cont_iip)[2])
153+
154+
#reinit!
155+
reinit!(pds_cont_oop)
156+
reinit!(pds_cont_iip)
157+
@test all(current_states(pds_cont_oop) .== initial_states(pds_cont_oop))
158+
@test all(current_states(pds_cont_iip) .== initial_states(pds_cont_iip))
159+
128160
end
129161

130162
# TODO: Test that Lyapunovs of this match the original system
131-
# But test this in ChaosTools.jl
163+
# But test this in ChaosTools.jl
164+
165+
#benchmarks, comparison with old version

0 commit comments

Comments
 (0)