So, the problem that I wanted to solve is the following. For a system that I study I wanted to record time and phase state when F(x, t) = 0, where F(x, t) is some non-linear function. If F(x, t) is linear, then there is such functional in DynamicalSystems.jl, but in my case I need something like F(x, t) = \sin (x_k - x_k^{0}). My system is 2\pi periodic in some of the variables and this effectively replaces a family of surfaces of section when trajectory wanders on torus and in \mathbb{R}^n. As far as I’ve understood, SavingCallback from library of callbacks does not allow me to do this since DiscreteCallback operates on different type of “event” (pardon my MATLAB/Python terminology :)). I tried to mimic its implementation to use in a ContinuousCallback, and come up with the following code:
struct ContSavedValues{tType, savevalType}
t::Vector{tType}
saveval::Vector{savevalType}
end
function ContSavedValues(::Type{tType}, ::Type{savevalType}) where {tType,savevalType}
ContSavedValues{tType, savevalType}(Vector{tType}(), Vector{savevalType}())
end
function Base.show(io::IO, saved_values::ContSavedValues)
tType = eltype(saved_values.t)
savevalType = eltype(saved_values.saveval)
print(io, "SavedValues{tType=", tType, ", savevalType=", savevalType, "}",
"\nt:\n", saved_values.t, "\nsaveval:\n", saved_values.saveval)
end
mutable struct ContSavingAffect{tType, savevalType}
saved_values::ContSavedValues{tType, savevalType}
end
function (affect!::ContSavingAffect)(integrator)
push!(affect!.saved_values.t, integrator.t)
push!(affect!.saved_values.saveval, Tuple(Float64(v) for v in integrator.u))
end
and use it like
observer_array = ContSavedValues(Float64, Tuple{Float64, Float64})
affectFunc! = ContSavingAffect(observer_array)
cb = ContinuousCallback(myCondition, affectFunc!, nothing)
sol = solve(myProblem, callback=cb)
The part that I’m worried the most is this:
function (affect!::ContSavingAffect)(integrator)
push!(affect!.saved_values.t, integrator.t)
push!(affect!.saved_values.saveval, Tuple(Float64(v) for v in integrator.u))
end
I’m assuming that when affectFunc!
will finally be called, the state of integrator corresponds to intersection with surface of interest. It runs okay on few tests that I’ve made, but is there any caveat that I might have overlooked?