Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use copy for noise #509

Merged
merged 3 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ function DiffEqBase.__solve(prob::DiffEqBase.AbstractRODEProblem,
integrator = DiffEqBase.__init(prob,alg,timeseries,ts,recompile;kwargs...)
solve!(integrator)
if typeof(prob) <: DiffEqBase.AbstractRODEProblem && typeof(prob.noise) == typeof(integrator.sol.W) && (!haskey(kwargs, :alias_noise) || kwargs[:alias_noise] === true)
# would be better to make the following a function `noise_deepcopy!(W::T, Z::T) where {T <: AbstractNoiseProcess}` in `DiffEqNoiseProcess.jl` or a proper `copy` overload, but this should do it for the moment
for x in fieldnames(typeof(prob.noise))
setfield!(prob.noise, x, deepcopy(getfield(integrator.sol.W, x)))
end
copy!(prob.noise, integrator.sol.W)
end
integrator.sol
end
Expand Down Expand Up @@ -416,7 +413,7 @@ function DiffEqBase.__init(
=#
end
elseif typeof(prob) <: DiffEqBase.AbstractRODEProblem
W = (!haskey(kwargs, :alias_noise) || kwargs[:alias_noise] === true) ? deepcopy(prob.noise) : prob.noise
W = (!haskey(kwargs, :alias_noise) || kwargs[:alias_noise] === true) ? copy(prob.noise) : prob.noise
if W.reset
# Reseed
if typeof(W) <: Union{NoiseProcess, NoiseTransport} && W.reseed
Expand Down
6 changes: 6 additions & 0 deletions test/noise_type_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ sol = solve(prob,EM(),dt=1/100)

@test sol.W == prob.noise
@test objectid(prob.noise) != objectid(sol.W)
@test objectid(prob.noise.u) == objectid(prob.noise.W) != objectid(sol.W.W) == objectid(sol.W.u)

sol = solve(prob,EM(),dt=1/1000,alias_noise=false)

@test sol.W == prob.noise
@test objectid(prob.noise) == objectid(sol.W)
@test objectid(prob.noise.u) == objectid(prob.noise.W) == objectid(sol.W.W) == objectid(sol.W.u)

sol = solve(prob,EM(),dt=1/1000, alias_noise=true)

@test sol.W == prob.noise
@test objectid(prob.noise) != objectid(sol.W)
@test objectid(prob.noise.u) == objectid(prob.noise.W) != objectid(sol.W.W) == objectid(sol.W.u)

function g(du,u,p,t)
@test typeof(du) <: SparseMatrixCSC
Expand Down Expand Up @@ -96,6 +99,9 @@ sol = solve(prob,EM(),dt=0.01)
@test typeof(sol.W) == typeof(prob.noise) <: NoiseFunction
@test objectid(prob.noise) != objectid(sol.W)

sol = solve(prob,EM(),dt=1/1000,alias_noise=false)
@test objectid(prob.noise) == objectid(sol.W)

sol = solve(prob,EM(),dt=0.01,alias_noise=true)
@test sol.W.curt ≈ last(tspan)

Expand Down