# ODE solver gives correct results only if time interval is small

**URL:** <https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460>\
**Category:** Uncategorized\
**Created:** [February 23, 2024, 9:14am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460 "2024-02-23T09:14:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [February 23, 2024, 9:14am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460/1 "2024-02-23T09:14:10Z")

</div>

Dear all,

I have a quick question about ODE solvers.

As shown below, TV\_mm is solved by a bunch of ODEs, and finally we got how TV\_mm change as time goes by as shown below, I want to know the value of TV\_mm at time = 50,

 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/5/5d76439b3032e2468ce52c39706c8aaadda57fbb.png)

The problem is,

- if I directly solve the ODEs from T0 = 0 to T=50, I got the green line, and a wrong big value of TV\_mm at T=50.

- If I solve the ODEs piece-wisely from t=0 to T1=25, then solve from T1=25 to T=50, I got the purple line as shown above. The result of TV\_mm at T=50 is closer to the correct answer but still off.

- If I solve the ODE piece-wisely from time = 0 to 1, 1 to 2, 2 to 3, … you know set 1 as the time interval, then I got the red line as shown above, this line is smooth and at T=50 it gives the correct TV\_mm value.

I have used LSODA and DVERK as the ODE solvers (I have changed the ATOL and RTOL values but does not help), the results are the same as shown above. ODE solvers did not throw any error messages. But it seems like the errors are accumulated during solving, and the ODE solvers did not aware that.

Just curious, has anyone encounter similar ODE issues, such that you have to decrease the time interval in order to get correct results?  
If so, usually what do you do to detect such issues and prevent it from happening?

Many thanks!

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [February 23, 2024, 10:03am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460/2 "2024-02-23T10:03:08Z")

</div>

Have you tried to change the length of the first step or the maximum step?  
Have you tried to normalize the quantity such that they are not of the order 10^5?

I will try to do this changes, well first normalize, and then try to set the length of the first step, as a last resort the maximum step.

---

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [February 23, 2024, 10:18am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460/3 "2024-02-23T10:18:30Z")

</div>

Thank you @egio !

For the Y vs T, is there some criterion for the magnitude of Y and the time T?  
Like, in this case, the time T is the order of 10, Y is the order of 10^5, should Y also be normalized such that it is about the order of 10, something like that?

About the length of the first step of ODE solver, is there some criterion as how to roughly determine this first step’s size?

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [February 23, 2024, 10:19am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460/4 "2024-02-23T10:19:01Z")

</div>

I am not familiar with TV\_mm and the omniscient Google only brings up pages that have nothing to do with a system of ODEs. Could you describe the physical/chemical/mathematical problem?

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [February 23, 2024, 10:29am UTC](https://fortran-lang.discourse.group/t/ode-solver-gives-correct-results-only-if-time-interval-is-small/7460/5 "2024-02-23T10:29:08Z")

</div>

Without knowing anything of the problem you’re trying to solve, I would say:

- Does tightening the tolerances improve your predictions? for LSODA and other ODEPACK solvers, typical tolerances are RTOL\<1.0e-4. “Reference” accuracy is achieved with say, RTOL = 1.0e-08 or sqrt(epsilon(0.0\_wp)). ATOL depends on your variables and is as important, because internal error control is done as

\varepsilon = RTOL\cdot |y\_i| + ATOL

so if ATOL is too large, RTOL will have no effect

- LSODA includes automatic switching between a stiff and a non-stiff method. The non-stiff method in LSODA is not very accurate, so if your problem is stiff i.e. chemistry ([mol]?), you should use a stiff method e.g. LSODE or VODE

- Another wild guess: to _restart_ the solution in ODEPACK solvers, you need to do some work (i.e., reset to `ISTATE=3` among other things). It may be worth doublechecking very carefully that the solution, and all necessary flags, are correctly reinitialized between subsequent calls to the solver
