From 3bffd9a07054243f2f0e21644bfb455d11ccffe0 Mon Sep 17 00:00:00 2001 From: Danny Sharp Date: Tue, 16 Aug 2022 15:55:41 -0400 Subject: [PATCH 1/5] Add @add_kwonly for NP --- src/types.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.jl b/src/types.jl index 8849f99..53feb71 100644 --- a/src/types.jl +++ b/src/types.jl @@ -228,7 +228,7 @@ end (W::NoiseProcess)(out1, out2, u, p, t) = interpolate!(out1, out2, W, u, p, t) adaptive_alg(W::NoiseProcess) = adaptive_alg(W.rswm) -function NoiseProcess(t0, W0, Z0, dist, bridge; kwargs...) +SciMLBase.@add_kwonly function NoiseProcess(t0, W0, Z0, dist, bridge; kwargs...) iip = DiffEqBase.isinplace(dist, 7) NoiseProcess{iip}(t0, W0, Z0, dist, bridge; kwargs...) end From a5ae8d0ba99013bb6efd994d11995eb52504fe69 Mon Sep 17 00:00:00 2001 From: Danny Sharp Date: Tue, 16 Aug 2022 17:52:32 -0400 Subject: [PATCH 2/5] Add remake --- src/DiffEqNoiseProcess.jl | 2 ++ src/noise_interfaces/common.jl | 25 +++++++++++++++++++++++++ src/types.jl | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/DiffEqNoiseProcess.jl b/src/DiffEqNoiseProcess.jl index 13939a1..1eba9e5 100644 --- a/src/DiffEqNoiseProcess.jl +++ b/src/DiffEqNoiseProcess.jl @@ -11,6 +11,8 @@ import RandomNumbers, Random123 import DiffEqBase: isinplace, solve, AbstractNoiseProcess, DEIntegrator, AbstractNoiseProblem +import SciMLBase: remake + import PoissonRandom, Distributions import QuadGK, Optim diff --git a/src/noise_interfaces/common.jl b/src/noise_interfaces/common.jl index e34fc92..b7ffd04 100644 --- a/src/noise_interfaces/common.jl +++ b/src/noise_interfaces/common.jl @@ -88,3 +88,28 @@ function Base.reverse(W::AbstractNoiseProcess) end return backwardnoise end + +function Base.similar(np::NoiseProcess, ::Type{NoiseProcess}=NoiseProcess) + NoiseProcess{isinplace(np)}(0., 0., np.Z isa AbstractVector ? np.Z[1] : np.Z, np.dist, np.bridge) +end + +function Base.copy(np::NoiseProcess) + np2 = similar(np) + for f in propertynames(np) + setfield!(np2, f, getfield(np, f)) + end + np2 +end + +function SciMLBase.remake(np::NoiseProcess; kwargs...) + np_new = copy(np) + inits = (t0 = :t, W0 = :W, Z0 = :Z) + for kwarg in kwargs + if first(kwarg) in keys(inits) + setfield!(np_new, inits[first(kwarg)], [second(kwarg)]) + else + setfield!(np_new, kwarg...) + end + end + np_new +end \ No newline at end of file diff --git a/src/types.jl b/src/types.jl index 53feb71..8849f99 100644 --- a/src/types.jl +++ b/src/types.jl @@ -228,7 +228,7 @@ end (W::NoiseProcess)(out1, out2, u, p, t) = interpolate!(out1, out2, W, u, p, t) adaptive_alg(W::NoiseProcess) = adaptive_alg(W.rswm) -SciMLBase.@add_kwonly function NoiseProcess(t0, W0, Z0, dist, bridge; kwargs...) +function NoiseProcess(t0, W0, Z0, dist, bridge; kwargs...) iip = DiffEqBase.isinplace(dist, 7) NoiseProcess{iip}(t0, W0, Z0, dist, bridge; kwargs...) end From d64352adb51c8ce8ac619f2e2c49dda547d63bd4 Mon Sep 17 00:00:00 2001 From: Danny Sharp Date: Wed, 17 Aug 2022 16:06:16 -0400 Subject: [PATCH 3/5] Add remake & copy tests, format --- src/noise_interfaces/common.jl | 12 +++++++++--- test/noise_process_remake.jl | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/noise_process_remake.jl diff --git a/src/noise_interfaces/common.jl b/src/noise_interfaces/common.jl index b7ffd04..248bae5 100644 --- a/src/noise_interfaces/common.jl +++ b/src/noise_interfaces/common.jl @@ -89,8 +89,14 @@ function Base.reverse(W::AbstractNoiseProcess) return backwardnoise end -function Base.similar(np::NoiseProcess, ::Type{NoiseProcess}=NoiseProcess) - NoiseProcess{isinplace(np)}(0., 0., np.Z isa AbstractVector ? np.Z[1] : np.Z, np.dist, np.bridge) +function Base.similar(np::NoiseProcess, ::Type{NoiseProcess} = NoiseProcess) + NoiseProcess{isinplace(np)}(0.0, 0.0, np.Z isa AbstractVector ? np.Z[1] : np.Z, np.dist, + np.bridge; + rswm = np.rswm, save_everystep = np.save_everystep, + rng = deepcopy(np.rng), + reset = np.reset, reseed = np.reseed, + continuous = np.continuous, + cache = np.cache) end function Base.copy(np::NoiseProcess) @@ -112,4 +118,4 @@ function SciMLBase.remake(np::NoiseProcess; kwargs...) end end np_new -end \ No newline at end of file +end diff --git a/test/noise_process_remake.jl b/test/noise_process_remake.jl new file mode 100644 index 0000000..8e36c88 --- /dev/null +++ b/test/noise_process_remake.jl @@ -0,0 +1,23 @@ +@testset "Remake" begin + using SciMLBase, DiffEqNoiseProcess, Test, Random + W = WienerProcess(0.0, 1.0, 1.0, rng = Xoshiro(42)) + dt = 0.1 + W.dt = dt + u = nothing + p = nothing # for state-dependent distributions + calculate_step!(W, dt, u, p) + for i in 1:10 + accept_step!(W, dt, u, p) + end + W2 = copy(W) + for prop in propertynames(W) + @test getfield(W, prop) === getfield(W2, prop) + end + rng2 = Xoshiro(43) + W3 = remake(W2, rng = rng2) + @test W3.rng === rng2 + W.rng = rng2 + for prop in propertynames(W) + @test getfield(W, prop) === getfield(W3, prop) + end +end From b305c47f86794778091f481e7ab68bc02d165009 Mon Sep 17 00:00:00 2001 From: Danny Sharp Date: Thu, 18 Aug 2022 09:49:32 -0400 Subject: [PATCH 4/5] Actually include new tests in runtests.jl --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 33538ce..0bb429e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,6 +8,7 @@ using Test include("correlated.jl") include("noise_wrapper.jl") include("noise_function.jl") + include("noise_process_remake.jl") include("VBT_test.jl") include("noise_grid.jl") include("noise_approximation.jl") From 956bbc448770f8451ce80a78980c9fcd6c409c02 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 19 Aug 2022 16:24:47 -0400 Subject: [PATCH 5/5] Update noise_process_remake.jl --- test/noise_process_remake.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/noise_process_remake.jl b/test/noise_process_remake.jl index 8e36c88..2bade15 100644 --- a/test/noise_process_remake.jl +++ b/test/noise_process_remake.jl @@ -1,6 +1,6 @@ @testset "Remake" begin using SciMLBase, DiffEqNoiseProcess, Test, Random - W = WienerProcess(0.0, 1.0, 1.0, rng = Xoshiro(42)) + W = WienerProcess(0.0, 1.0, 1.0, rng = Random.Xoshiro(42)) dt = 0.1 W.dt = dt u = nothing @@ -13,7 +13,7 @@ for prop in propertynames(W) @test getfield(W, prop) === getfield(W2, prop) end - rng2 = Xoshiro(43) + rng2 = Random.Xoshiro(43) W3 = remake(W2, rng = rng2) @test W3.rng === rng2 W.rng = rng2