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

未关闭 适合新手
#111 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp
领域
backend

调研方向

阅读 include/boost/numeric/odeint/stepper/rosenbrock4.hpp,重点查看 default_rosenbrock_coefficients 和系数 d4。将其符号与 issue 中给出的 RODAS 值进行比较,然后运行非自治收敛检查,例如提供的阶数研究。完成标准是 d4 具有所述的负号,并且非自治情况以四阶收敛。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C++
星标
55
派生
60
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

boostorg/odeint 的其他 Issue

查看 boostorg/odeint 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。