rosenbrock4: d4 has the wrong sign, so non-autonomous problems converge at first order

Open Beginner friendly
#111 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Read include/boost/numeric/odeint/stepper/rosenbrock4.hpp, focusing on default_rosenbrock_coefficients and the d4 coefficient. Compare its sign with the RODAS value given in the issue, then run a non-autonomous convergence check such as the supplied order study. Done means d4 has the stated negative sign and the non-autonomous case converges at fourth order.

Written by the indexing model from the issue text.

Description

In include/boost/numeric/odeint/stepper/rosenbrock4.hpp (develop at efa150f), default_rosenbrock_coefficients sets

d4 ( static_cast< value_type >( 0.3620000000000023e-01 ) ) ,

The coefficients are Hairer's RODAS (METH = 1 in rodas.f), where D4 = -0.3620000000000023D-01. The d_i are the row sums of the Gamma matrix, and Boost's own gamma and c_ij give the same value: with Gamma^-1 = diag(1/gamma) - C, the fourth row sum of Gamma is -0.0362.

d4 multiplies dfdt, so only problems where f depends on t explicitly are affected. On y' = L (y - cos t) - sin t, y(0) = 1 (exact cos t), integrated to t = 2 with a Python port of do_step:

d4 error, L = -1, 160 steps observed order
+0.0362 (as in Boost) 3.9e-04 1.0
-0.0362 1.1e-11 4.0

With L = -50 the orders are 1.1 and 3.8. An autonomous problem gives order 4 with either sign, which may be why it has not shown up.

Suggested fix: d4 ( static_cast< value_type >( -0.3620000000000023e-01 ) ) ,

The port and the order study (Python, NumPy only)
"""Boost.odeint rosenbrock4 (stepper/rosenbrock4.hpp, develop at efa150f), do_step ported line by line, run with
d4 as Boost stores it (+0.0362) and as Hairer's RODAS has it (-0.0362, = row sum 4 of Gamma from Boost's own C).
Non-autonomous test y' = L (y - cos t) - sin t, y(0) = 1, exact y = cos t; and an autonomous one for contrast."""
import numpy as np

co = dict(gamma=0.25, d1=0.25, d2=-0.1043, d3=0.1035, c2=0.386, c3=0.21, c4=0.63,
          c21=-5.6688, a21=1.544, c31=-2.430093356833875, c32=-0.2063599157091915, a31=0.9466785280815826,
          a32=0.2557011698983284, c41=-0.1073529058151375, c42=-9.594562251023355, c43=-20.47028614809616,
          a41=3.314825187068521, a42=2.896124015972201, a43=0.9986419139977817,
          c51=7.496443313967647, c52=-10.24680431464352, c53=-33.99990352819905, c54=11.70890893206160,
          a51=1.221224509226641, a52=6.019134481288629, a53=12.53708332932087, a54=-0.6878860361058950,
          c61=8.083246795921522, c62=-7.981132988064893, c63=-31.52159432874371, c64=16.31930543123136,
          c65=-6.058818238834054)


def step(f, jac, dfdt, x, t, dt, d4):
    c = co
    n = len(x)
    M = np.eye(n) / c["gamma"] / dt - jac(x, t)
    solve = lambda r: np.linalg.solve(M, r)  # noqa: E731
    fx, ft = f(x, t), dfdt(x, t)
    g1 = solve(fx + dt * c["d1"] * ft)
    g2 = solve(f(x + c["a21"] * g1, t + c["c2"] * dt) + dt * c["d2"] * ft + c["c21"] * g1 / dt)
    g3 = solve(f(x + c["a31"] * g1 + c["a32"] * g2, t + c["c3"] * dt) + dt * c["d3"] * ft
               + (c["c31"] * g1 + c["c32"] * g2) / dt)
    g4 = solve(f(x + c["a41"] * g1 + c["a42"] * g2 + c["a43"] * g3, t + c["c4"] * dt) + dt * d4 * ft
               + (c["c41"] * g1 + c["c42"] * g2 + c["c43"] * g3) / dt)
    xt = x + c["a51"] * g1 + c["a52"] * g2 + c["a53"] * g3 + c["a54"] * g4
    g5 = solve(f(xt, t + dt) + (c["c51"] * g1 + c["c52"] * g2 + c["c53"] * g3 + c["c54"] * g4) / dt)
    xt = xt + g5
    err = solve(f(xt, t + dt) + (c["c61"] * g1 + c["c62"] * g2 + c["c63"] * g3 + c["c64"] * g4 + c["c65"] * g5) / dt)
    return xt + err


def study(L, nonauto=True):
    if nonauto:
        f = lambda x, t: L * (x - np.cos(t)) - np.sin(t)  # noqa: E731
        dfdt = lambda x, t: L * np.sin(t) - np.cos(t)  # noqa: E731
        exact = np.cos(2.0)
    else:
        f = lambda x, t: L * x  # noqa: E731
        dfdt = lambda x, t: 0 * x  # noqa: E731
        exact = np.exp(2.0 * L)
    jac = lambda x, t: np.array([[L]])  # noqa: E731
    out = {}
    for d4 in (+0.0362, -0.0362):
        errs = []
        for n in (10, 20, 40, 80, 160):
            x, t, h = np.array([1.0]), 0.0, 2.0 / n
            for _ in range(n):
                x = step(f, jac, dfdt, x, t, h, d4)
                t += h
            errs.append(abs(x[0] - exact))
        out[d4] = (errs, np.log2(np.array(errs[:-1]) / errs[1:]))
    return out


if __name__ == "__main__":
    for L, na in ((-1.0, True), (-50.0, True), (-1.0, False)):
        for d4, (errs, rates) in study(L, na).items():
            print(f"{'non-autonomous' if na else 'autonomous':15s} L={L:6g} d4={d4:+.4f}: err(n=160)={errs[-1]:.2e}"
                  f" rates={np.round(rates, 2)}")

I found this while testing physverify, a small verification tool I'm working on, against public codes. The tests and this write-up were done with help from an AI assistant; the numbers come from actually running the code.

Dominant language
C++
Stars
55
Forks
60
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/odeint

All issues in boostorg/odeint

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.