# Is there a good Fortran package for SDE, DDE and ODE?

**URL:** <https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756>\
**Category:** Uncategorized\
**Created:** [August 27, 2021, 6:19pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756 "2021-08-27T18:19:57Z")\
**Posts on this page:** 20\
**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:** [August 27, 2021, 6:19pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/1 "2021-08-27T18:19:58Z")

</div>

Hello all,

I just wanted to ask, has anyone have used, any good Fortran package/libraries or packages or subroutines, that contains

1. stochastic differential equation (SDE) solver,
2. delayed differential equation (DDE) solver, and
3. ordinary differential equations (ODE) solver?

If so how is the performance compared with Julia’s differential equation solver? Is Julia’s differential equation solver is best on the market now?

I just wanted to find good Fortran differential solver so that I can use it in my current Fortran code.  
I am not going to completely translate my Fortran code to Julia because based on my experience it seems that, it is not very easy to write a Julia code whose speed is the same as Fortran.

I am basically asking has anyone used any of the three (SDE/ODE/DDE) Fortran subroutines or solvers or libraries, and have any recommendations.

Thank you very much!

---

<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:** [August 27, 2021, 6:53pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/3 "2021-08-27T18:53:06Z")

</div>

I am basically asking has anyone used any of the three (SDE/ODE/DDE) Fortran subroutines or solvers or libraries, and have any recommendations if any.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 27, 2021, 7:03pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/4 "2021-08-27T19:03:24Z")

</div>

I wrote my own for a specific equation:

- [dftatom/ode1d.f90 at fe479fd27a7e0f77c6a88a1949996406ec935ac2 · certik/dftatom · GitHub](https://github.com/certik/dftatom/blob/fe479fd27a7e0f77c6a88a1949996406ec935ac2/src/ode1d.f90#L193)
- [dftatom/rschroed.f90 at fe479fd27a7e0f77c6a88a1949996406ec935ac2 · certik/dftatom · GitHub](https://github.com/certik/dftatom/blob/fe479fd27a7e0f77c6a88a1949996406ec935ac2/src/rschroed.f90#L72)

It’s faster than other codes I tried. But if somebody has a faster solver, definitely let me know.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [August 27, 2021, 7:09pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/5 "2021-08-27T19:09:15Z")

</div>

The ones I am aware of for delay differential equations are:

- [DDE\_SOLVER](https://www.sciencedirect.com/science/article/abs/pii/S0168927405000966?via%3Dihub)
- [DKLAG6](https://www.sciencedirect.com/science/article/abs/pii/S0168927497000299?via%3Dihub) (predecessor of DDE\_SOLVER)
- [RETARD](http://www.unige.ch/~hairer/software.html)
- [RADAR5](http://www.unige.ch/~hairer/software.html)
- [DELSOL](https://www.sciencedirect.com/science/article/abs/pii/0168927492900178)

For other types of ODE’s the page of [Hairer](http://www.unige.ch/~hairer/software.html) includes several codes for both stiff and non-stiff problems. I believe many of them are heavily used across various fields.

Another classic resource are the ODE solvers published at [netlib](http://www.netlib.org/ode/index.html). Unfortunately, most of them are F77 style codes which are not thread-safe. An exception is the [rksuite](http://www.netlib.org/ode/rksuite/) which has a Fortran 90 interface and was later modified and included into the NAG library.

The [blog post](http://www.stochasticlifestyle.com/comparison-differential-equation-solver-suites-matlab-r-julia-python-c-fortran/) from Christopher Rackauckas (also listed above by @kargl) lists a few more Fortran codes including ODEPACK, Sundials, and FATODE. Many of these are still heavily used today. Some of the solvers by Hairer and from ODEPACK are wrapped in [scipy.integrate](https://docs.scipy.org/doc/scipy/reference/integrate.html).

Concerning performance and design, as Christopher states:

> DifferentialEquations.jl does offer pretty much everything from the other suite combined, but that’s no accident: our software organization came last and we used these suites as a guiding hand for how to design ours.

Some recent papers like [this one](https://arxiv.org/abs/2012.12696) found performance of Julia slightly better, but Fortran was comparable. [Another paper](https://link.springer.com/chapter/10.1007/978-3-030-10692-8_45) found Fortran to be faster for a Runge-Kutta method.

As far as stiff problems are concerned, many of the older Fortran packages don’t use LAPACK for the matrix factorizations but their own factorization routines. I believe with some small refactoring performance could be improved. Still it would take a prodigious effort to reach the same level of comprehensiveness that has gone into DifferentialEquations.jl. The author of that package lives and breathes differential equation solvers.

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [August 27, 2021, 7:18pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/6 "2021-08-27T19:18:53Z")

</div>

Is any of the code for the second paper publicly available? It appears the paper isn’t/

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [August 27, 2021, 7:21pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/7 "2021-08-27T19:21:23Z")

</div>

The code is made available on bitbucket: [fortran vs julia](https://bitbucket.org/mngev/fortran-vs-julia/src/master/).

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [August 27, 2021, 7:27pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/8 "2021-08-27T19:27:08Z")

</div>

Interestingly, the Julia code isn’t valid Julia.

```auto
function RKp6n1(...)
    ...
    local const a21 = 1/2
    ...
end

```

This is from RK.jl, and isn’t valid Julia syntax. That’s not entirely reassuring that the authors actually know what they’re doing.

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [August 27, 2021, 7:34pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/9 "2021-08-27T19:34:06Z")

</div>

Also, their code is incredibly inefficient. They wrote their test function in Julia to return a `Vector` of 2 values, while in `Fortran` it modifies the inputs. Had the Julia version simply returned 2 values (as a Tuple), they would have removed all of the garbage they were generating, and their function would have been about 10x faster.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [August 27, 2021, 7:42pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/11 "2021-08-27T19:42:44Z")

</div>

Just to be on the clear side, I was only quoting part of their conclusion. They do also make some remarks in favor of Julia. But perhaps the rebuttal of that paper can go to a new thread or the [Julia Discourse](https://discourse.julialang.org/).

To return to the original question, I have personally used the following ODE solvers:

- [dop853](https://github.com/jacobwilliams/dop853) (thanks to @jacobwilliams)
- [ddeabm](https://github.com/jacobwilliams/ddeabm) (also thanks to @jacobwilliams)
- [RKC: An explicit solver for parabolic PDEs](https://www.sciencedirect.com/science/article/pii/S0377042797002197) (available from netlib)
- [stiff3](https://github.com/ivan-pi/stiff3)

In a distant past I also used LSODE from ODEPACK which gave good results, but the API is rather cumbersome. Another problem with many of the Fortran ODE solvers is they are not thread-safe.

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [August 28, 2021, 3:10am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/12 "2021-08-28T03:10:08Z")

</div>

I remember [Sourcery Institute](https://github.com/sourceryinstitute) also had a nice differential equation solver library in modern Fortran. I searched their GitHub pages, but could not find it immediately. Perhaps @rouson could help more on this.

---

<div class="post-metadata">

**Author:** ![rouson](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@rouson](https://fortran-lang.discourse.group/u/rouson)\
**Post date:** [August 28, 2021, 3:20am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/13 "2021-08-28T03:20:51Z")

</div>

You’re probably referring to FOODiE by Zhagi, Curcic et al.:

[https://github.com/Fortran-FOSS-Programmers/FOODIE](https://github.com/Fortran-FOSS-Programmers/FOODIE)

I don’t think it was ever under Sourcery Institute, but it uses the Abstract Calculus pattern from the book I co-authored and I helped come up with the name.

Damian

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [August 28, 2021, 3:29am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/14 "2021-08-28T03:29:05Z")

</div>

That explains why I could not find it on Sourcery’s page. Thanks for your correction.

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [August 28, 2021, 3:51am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/15 "2021-08-28T03:51:42Z")

</div>

Others:

- [GitHub - princemahajan/FLINT: Fortran Library for numerical INTegration of differential equations\<](https://github.com/princemahajan/FLINT)
- [Fortran-Astrodynamics-Toolkit/rk\_module.f90 at master · jacobwilliams/Fortran-Astrodynamics-Toolkit · GitHub](https://github.com/jacobwilliams/Fortran-Astrodynamics-Toolkit/blob/master/src/rk_module.f90)
- [Fortran-Astrodynamics-Toolkit/rk\_module\_variable\_step.f90 at master · jacobwilliams/Fortran-Astrodynamics-Toolkit · GitHub](https://github.com/jacobwilliams/Fortran-Astrodynamics-Toolkit/blob/master/src/rk_module_variable_step.f90)

There are certainly a lot of ODE solvers floating around. A good stdlib project would be to modernize, clean up and organize them all into a nice modern API.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 28, 2021, 12:21pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/16 "2021-08-28T12:21:35Z")

</div>

The FLINT page says

> For performance comparison, the latest FLINT code is tested against Julia’s DifferentialEquations package ([https://docs.sciml.ai/release-2.0/index.html](https://docs.sciml.ai/release-2.0/index.html)) and FLINT appears to be _ **faster** _ by at least an order of magnitude as shown in the following screenshot. The Julia test code along with results are provided in the media folder on the FLINT’s GitHub repository [GitHub - princemahajan/FLINT: Fortran Library for numerical INTegration of differential equations](https://github.com/princemahajan/FLINT).

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 28, 2021, 2:38pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/17 "2021-08-28T14:38:54Z")

</div>

We should add FLINT to the packages here: [Fortran Packages - Fortran Programming Language](https://fortran-lang.org/packages/) and the others too!

---

<div class="post-metadata">

**Author:** ![jacobwilliams](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jacobwilliams/32/10_2.png) [@jacobwilliams](https://fortran-lang.discourse.group/u/jacobwilliams)\
**Post date:** [August 29, 2021, 11:15pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/18 "2021-08-29T23:15:29Z")

</div>

pull request with some of them here: [added 2 interpolation and 4 ode packages by jacobwilliams · Pull Request #306 · fortran-lang/fortran-lang.org ·](https://github.com/fortran-lang/fortran-lang.org/pull/306/files)

---

<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:** [August 30, 2021, 7:28pm UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/19 "2021-08-30T19:28:45Z")

</div>

Thank you very much indeed guys!

My purpose is to see if we can find corresponding Fortran packages/subroutines/libraries, which can be combined and have the same functions as in the Julia’s DifferentialEquations.jl

For now I see some ODE, DDE recommendations, and may I ask, do you have any recommendations for stochastic differential equation (SDE) solver?

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [September 2, 2021, 2:31am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/20 "2021-09-02T02:31:46Z")

</div>

Note that this benchmark is really flawed. I believe the results they see come from the fact that their Julia code sucks in ways their Fortran doesn’t. For example, the Julia version defines

```auto
function JacobiConstant(odesol)
    
    X = odesol[1:3,:]
    Xdot = odesol[4:6,:]

    ~,n = size(X)
    
    # distances
    R1 = copy(X)
    R2 = copy(X)
    R1[1,:] = R1[1,:] .+ mu
    R2[1,:] = R2[1,:] .- 1 .+ mu
    r1 = [norm(R1[:,i]) for i = 1:n]
    r2 = [norm(R2[:,i]) for i = 1:n]
    
    # radius and velocity nagnitudes
    R = [sqrt(sum((X.^2)[:,i])) for i = 1:n]
    V = [sqrt(sum((Xdot.^2)[:,i])) for i = 1:n]

    # pseudo-potential
    Omega = 1/2*(R.^2) .+ (1-mu)./r1 .+ mu./r2
    
    # Jacobi's Constant
    C = (V.^2)/2 .- Omega
    
    return C
end;

```

While in Fortran, they define

```auto
function JacobiC(mu, X)
        real(wp), intent(in) :: mu
        real(wp), dimension(:,:), intent(in) :: X
        real(wp), dimension(size(X,2)) :: JacobiC
        real(wp), dimension(size(X,2)) :: Omega
        real(WP), dimension(size(X,2)) :: r1, r2
        
        r1 = sqrt((X(1,:) + mu)**2 + X(2,:)**2 + X(3,:)**2)
        r2 = sqrt((X(1,:) - 1.0 + mu)**2 + X(2,:)**2+X(3,:)**2)
        Omega = 1.0/2.0*sum(X(1:3,:)**2,1) + (1.0-mu)/r1 + mu/r2
        ! Jacobian's Constant
        JacobiC = sum(X(4:6,:)**2,1)/2 - Omega
    end function JacobiC

```

The Julia version here is incredibly inefficient because when you do something like ` [sqrt(sum((X.^2)[:,i])) for i = 1:n]`, you are computing `X.^2` `n` times only to throw away all but 1 column, and allocating `n` matrices and `n` vectors to throw those away by summing over them. Just using `@views` appropriately, and doing less stupid stuff should easily regain the performance defecit here. Since this is the core of the benchmark, it’s kind of hard for me to believe that the authors are trying to make a fair comparison, when the Julia code is so obviously stupidly written (for this function, they would have gotten way better performance by just following the same logic that their fortran version used, instead of randomly computing a ton of info, allocating a ton of arrays, and then throwing them away).

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [September 2, 2021, 3:10am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/21 "2021-09-02T03:10:02Z")

</div>

Maybe it is something about Julia that prevents people from writing efficient code. I doubt so many people and smart scientists are so “stupid” to write inefficient “stupid stuff” so easily in a language as you describe.

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [September 2, 2021, 3:13am UTC](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/22 "2021-09-02T03:13:19Z")

</div>

They litterally could have written the exact same thing, except with `(X[:,i].^2))` instead of `(X.^2)[:,i])` and it would have been multiple times faster.

[Next page](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756.md?page=2)
