From 08bb4ad03292bd36f6e363613cdeb87843fe80f1 Mon Sep 17 00:00:00 2001 From: oameye Date: Sat, 24 Feb 2024 15:52:57 +0100 Subject: [PATCH 1/2] add iip or oop kwargs to `simulate` and `relax` --- src/trajectories/simulation.jl | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/trajectories/simulation.jl b/src/trajectories/simulation.jl index 2cc9081d..5b266a64 100644 --- a/src/trajectories/simulation.jl +++ b/src/trajectories/simulation.jl @@ -18,18 +18,19 @@ For more info, see [`SDEProblem`](https://diffeq.sciml.ai/stable/types/sde_types > Warning: This function has only been tested for the `EM()` solver and out-of-place `SDEFunction`s. """ -function simulate(sys::StochSystem, init::State; - dt=0.01, - tmax=1e3, - solver=EM(), - callback=nothing, - progress=true, - kwargs...) - - prob = SDEProblem(sys.f, σg(sys), init, (0, tmax), p(sys), noise=stochprocess(sys)) - solve(prob, solver; dt=dt, callback=callback, progress=progress, kwargs...) -end; - +function simulate(sys::StochSystem, init; + dt = 0.01, + tmax = 1e3, + solver = EM(), + callback = nothing, + progress = true, + iip = is_iip(sys.f), + kwargs...) + + prob = SDEProblem{iip}( + sys.f, σg(sys), init, (0, tmax), p(sys), noise = stochprocess(sys)) + solve(prob, solver; dt = dt, callback = callback, progress = progress, kwargs...) +end """ relax(sys::StochSystem, init::State; kwargs...) Simulates the deterministic dynamics of StochSystem `sys` in time, starting at initial condition `init`. @@ -43,7 +44,7 @@ This function integrates `sys.f` forward in time, using the [`ODEProblem`](https * `callback=nothing`: callback condition * `kwargs...`: keyword arguments for [`solve(ODEProblem)`](https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts/#solver_options) -For more info, see [`ODEProblem`](https://diffeq.sciml.ai/stable/types/ode_types/#SciMLBase.ODEProblem). +For more info, see [`ODEProblem`](https://diffeq.sciml.ai/stable/types/ode_types/#SciMLBase.ODEProblem). For stochastic integration, see [`simulate`](@ref). > Warning: This function has only been tested for the `Euler()` solver. @@ -53,8 +54,9 @@ function relax(sys::StochSystem, init::State; tmax=1e3, solver=Euler(), callback=nothing, + iip = is_iip(sys.f), kwargs...) - - prob = ODEProblem(sys.f, init, (0, tmax), p(sys)) + + prob = ODEProblem{iip}(sys.f, init, (0, tmax), p(sys)) solve(prob, solver; dt=dt, callback=callback, kwargs...) -end; \ No newline at end of file +end; From 1e94dedf6458dcc219fa650173bc216ab9f165d0 Mon Sep 17 00:00:00 2001 From: oameye Date: Sat, 24 Feb 2024 16:00:23 +0100 Subject: [PATCH 2/2] parameter bug when using `p(sys::StochSystem)` --- src/StochSystem.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/StochSystem.jl b/src/StochSystem.jl index 85e02366..42688085 100644 --- a/src/StochSystem.jl +++ b/src/StochSystem.jl @@ -48,8 +48,8 @@ end Concatenates the deterministic and stochastic parameter vectors `pf` and `pg` of a StochSystem `sys`. """ function p(sys::StochSystem) - [sys.pf, sys.pg] -end; + isnothing(sys.pg) ? sys.pf : [sys.pf, sys.pg] +end """ CoupledODEs(sys::StochSystem; diffeq, t0=0.0) @@ -67,4 +67,4 @@ Converts a [`CoupledODEs`](https://juliadynamics.github.io/DynamicalSystems.jl/s system into a [`StochSystem`](@ref). """ StochSystem(ds::DynamicalSystemsBase.CoupledODEs; σ=0.0, g=idfunc, pg=nothing, Σ=I(length(get_state(ds))), process="WhiteGauss") = -StochSystem(dynamic_rule(ds), [ds.p0], get_state(ds), σ, g, pg, Σ, process) \ No newline at end of file +StochSystem(dynamic_rule(ds), [ds.p0], get_state(ds), σ, g, pg, Σ, process)