c# - 求解复杂的 ODE 集

c# - solving complexed ODE set

简介 有些集合 od ODE 无法解析求解。在这种情况下,有很多众所周知的方法,尤其是在像 MATLAB 这样的典型科学软件中。只要你坚持下去,一切都很好。但是,如果您尝试将此功能移植到其他环境,问题就会出现。就我而言,我需要在 C# 中使用它。

一些细节 当然,有一些用于 ODE 的 C# 库,但在大多数情况下(至少在我熟悉的情况下),它们非常有限。让我们看看 OSLO 库,这里有一个示例查询:

var sol = Ode.RK547M(0, new Vector(5.0, 1.0),
(t, x) => new Vector(
x[0] - x[0] * x[1],
-x[1] + x[0] * x[1]));

如您所见,它不允许提供任何额外的支持非 OD 方程,也不允许提供嵌入式算法。例如,如果我们必须解决这样的设置,它会有点受限:

a=x*2+7
b=y*x+3
c- need to be calculated with external algorithm basing and "b" and "x"
dx/dt=x - xy + a + c
dx/dt=-y +xy + b

在这种情况下,上述 lib 似乎效率不高。在 C++ 中,我通过 boost 使用 odeint 库。我可以这样定义结构:

struct solveODE
{
    void operator()( const vector_type &y , vector_type &ODE , const double t )
    {
        double x=y[0];
        double y=y[1];
        a=x*2+7;
        b=y*x+3;
        additional_solve(b, x);
        ODE[0]=x - xy + a + c;
        ODE[1]=-y +xy + b;
        }
};

并这样称呼它:

integrate_const(make_dense_output<stepper_type>( 1E-12, 1E-6 ),
                    solveODE(),
                    y, 0.0, t_end, dt ,
                    std::bind(&calc::printResults , std::ref(*this) , pl::_1 , pl::_2));

问题

问题是我将使用哪个 C# 库提供此功能,除了解决僵硬的 ode 集?性能非常重要,因为 ode set 可能包含 25 个以上的方程 + 大量支持代数方程。更具体地说——我什至无法计算解析雅可比行列式,因为它在时间上不是常数,因此潜在求解器的选择是有限的。

你应该可以使用

var sol = Ode.RK547M(0, new Vector(5.0, 1.0),
    (t, u) => {
        double x=u[0], y=u[1];
        double a=x*2+7, b=y*x+3;
        double c = additional_solve(b, x);
        return new Vector(
            x - x*y + a + c,
            -y +x*y + b
        );
     });

作为 lambda 委托定义的长格式,即使用 x => x*xx => { return x*x; } 的缩写,x => { return x*x; }delegate(x) { return x*x; } 的缩写,依此类推。