@@ -24,12 +24,17 @@ function _generate_stochastic_kwargs(
2424) where {SF<: AbstractSaveFunc }
2525 cb_save = _generate_stochastic_save_callback (e_ops, sc_ops, tlist, store_measurement, progress_bar, method)
2626
27+ # Ensure that the noise is stored in tlist. # TODO : Fix this directly in DiffEqNoiseProcess.jl
28+ # See https://github.com/SciML/DiffEqNoiseProcess.jl/issues/214 for example
29+ tstops = haskey (kwargs, :tstops ) ? unique! (sort! (vcat (tlist, kwargs. tstops))) : tlist
30+ kwargs2 = merge (kwargs, (tstops = tstops,))
31+
2732 if SF === SaveFuncSSESolve
2833 cb_normalize = _ssesolve_generate_normalize_cb ()
29- return _merge_kwargs_with_callback (kwargs , CallbackSet (cb_normalize, cb_save))
34+ return _merge_kwargs_with_callback (kwargs2 , CallbackSet (cb_normalize, cb_save))
3035 end
3136
32- return _merge_kwargs_with_callback (kwargs , cb_save)
37+ return _merge_kwargs_with_callback (kwargs2 , cb_save)
3338end
3439_generate_stochastic_kwargs (
3540 e_ops:: Nothing ,
@@ -69,7 +74,9 @@ function _generate_stochastic_save_callback(e_ops, sc_ops, tlist, store_measurem
6974 expvals = e_ops isa Nothing ? nothing : Array {ComplexF64} (undef, length (e_ops), length (tlist))
7075 m_expvals = getVal (store_measurement) ? Array {Float64} (undef, length (sc_ops), length (tlist) - 1 ) : nothing
7176
72- _save_func = method (store_measurement, e_ops_data, m_ops_data, progr, Ref (1 ), expvals, m_expvals)
77+ _save_func_cache = Array {Float64} (undef, length (sc_ops))
78+ _save_func =
79+ method (store_measurement, e_ops_data, m_ops_data, progr, Ref (1 ), expvals, m_expvals, tlist, _save_func_cache)
7380 return FunctionCallingCallback (_save_func, funcat = tlist)
7481end
7582
@@ -162,9 +169,12 @@ _get_save_callback_idx(cb, method) = 1
162169
163170# %% ------------ Noise Measurement Helpers ------------ %%
164171
165- # TODO : Add some cache mechanism to avoid memory allocations
166- function _homodyne_dWdt (integrator)
167- @inbounds _dWdt = (integrator. W. u[end ] .- integrator. W. u[end - 1 ]) ./ (integrator. W. t[end ] - integrator. W. t[end - 1 ])
172+ # TODO : To improve. See https://github.com/SciML/DiffEqNoiseProcess.jl/issues/214
173+ function _homodyne_dWdt! (dWdt_cache, integrator, tlist, iter)
174+ idx = findfirst (>= (tlist[iter[]- 1 ]), integrator. W. t)
175+
176+ # We are assuming that the last element is tlist[iter[]]
177+ @inbounds dWdt_cache .= (integrator. W. u[end ] .- integrator. W. u[idx]) ./ (integrator. W. t[end ] - integrator. W. t[idx])
168178
169- return _dWdt
179+ return nothing
170180end
0 commit comments