How to adapt the timestep based on a subset of states

Hi, I am using DifferentialEquations.jl with ComponentArrays.jl to not only integrate the state of my system but also some additional variables (by-products) which I later need to interpolate at arbitrary times (using “free” interpolation x = sol(t, Val{1}).∫x). I know this can be regarded as somewhat of a dirty trick but I can’t think of a better way to do this and my algorithm heavily relies on it being done efficiently.

My question is, how can I make it so that the additional variables do not effect the timestep’s adaptation, i.e. only use the component of the estimated error that corresponds to the “actual” state of the system to adapt the step-size?

error_estimate does not seem to be a valid keyword argument but internalnorm is (listed here) and in fact this seems to be exactly the amount of control I want.

The only thing I am slightly unsure about is “Required are two dispatches: one dispatch for the state variable and the other on the elements of the state variable (scalar norm)”. I understand that I need two methods: internalnorm(x::AbstractArray,t) and internalnorm(x::Number,t), I’m just not exactly sure why. Disregarding the explanation for now, is this pairing correct:

    function custom_norm(u::AbstractArray,t)
        y = u.y
        return sqrt(sum(y.^2)/length(y))
    end
    function custom_norm(y::Number,t)
        return y^2
    end

if I’m re-implementing the norm from here with a single component of my ComponentArray?

First of all the other answer was an AI-generated post that was meant to include an advertisement link. So I would not deem its content trustworthy.

I wonder whether this is actually a bad idea. After all you clearly care about the additional quantities right? So you would want to have them accurate. Would it not make sense to have the integrator know about them and ensure they are converged?
Otherwise I would suggest using Interpolations.jl to interpolate the quantities of interest between timepoints.

I had my suspicions… guess this is a common thing now.

I am on the fence regarding this. But my reasoning for excluding them is that accuracy of the state is clearly more important as all errors in its integration accumulate. In contrast the accuracy of the by-products is determined by the accuracy of the state at the given time.

I think it would be a shame to not to use the genius of “dense” output, especially as it uses individual stages of each step which is data that other interpolation techniques would disregard.

I mean you can also just compute your quantity at each time point from the dense output, can’t you? That should be conceptually equivalent, no? Note if you require derivatives you can get them from the solution object as well (but I don’t recall the symtax right now).

I can certainly use the dense output to re-calculated the quantities but I don’t want to take the performance hit of calculating things twice.

This recovers the derivative of the integrated value if that’s what you meant.