# How to adapt the timestep based on a subset of states

**URL:** <https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363>\
**Category:** General Usage\
**Tags:** diffeq, ode, differentialequation\
**Created:** [September 13, 2024, 9:01am UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363 "2024-09-13T09:01:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [September 13, 2024, 9:01am UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/1 "2024-09-13T09:01:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [September 13, 2024, 11:21am UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/3 "2024-09-13T11:21:06Z")

</div>

`error_estimate` does not seem to be a valid keyword argument but `internalnorm` is ([listed here](https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts/)) 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:

```julia
    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](https://docs.sciml.ai/DiffEqDocs/stable/extras/timestepping/#Common-Setup) with a single component of my `ComponentArray`?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 14, 2024, 8:31pm UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/4 "2024-09-14T20:31:16Z")

</div>

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.

> [@lieskjur](#):
>
> 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?

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.

---

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [September 14, 2024, 11:52pm UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/5 "2024-09-14T23:52:23Z")

</div>

> [@abraemer](#):
>
> 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 had my suspicions… guess this is a common thing now.

> [@abraemer](#):
>
> 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?

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.

> [@abraemer](#):
>
> Otherwise I would suggest using [Interpolations.jl](https://juliahub.com/ui/Packages/General/Interpolations)

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.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 15, 2024, 6:08am UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/6 "2024-09-15T06:08:24Z")

</div>

> [@lieskjur](#):
>
> I think it would be a shame to not to use the genius of “dense” output

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).

---

<div class="post-metadata">

**Author:** ![lieskjur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lieskjur/32/27611_2.png) [@lieskjur](https://discourse.julialang.org/u/lieskjur)\
**Post date:** [September 15, 2024, 7:53am UTC](https://discourse.julialang.org/t/how-to-adapt-the-timestep-based-on-a-subset-of-states/119363/7 "2024-09-15T07:53:47Z")

</div>

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.

> [@lieskjur](#):
>
> using “free” interpolation `x = sol(t, Val{1}).∫x`

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