在 odeint 中使用 std::vector<Eigen::Vector3d> 作为状态类型

Using a std::vector<Eigen::Vector3d> as state type in odeint

本文关键字:gt Vector3d 类型 状态 Eigen odeint std vector lt      更新时间:2023-10-16

我目前正在尝试使用 odeint 和 Eigen3 来集成 nBody 系统(目标是提供用于行星形成的高级例程的库,例如 MVS 的混合变量辛或钱伯斯混合变体)。在尝试不同的步进器时,我发现当使用普通步进器时,state_type std::vector<Eigen::Vector3d>工作正常,但使用受控步进器(如burlisch_stoer)编译失败,第一条错误消息是:

/usr/include/boost/numeric/odeint/stepper/controlled_runge_kutta.hpp:87:40: error: cannot convert ‘boost::numeric::odeint::norm_result_type<std::vector<Eigen::Matrix<double, 3, 1> >, void>::type {aka Eigen::Matrix<double, 3, 1>}’ to ‘boost::numeric::odeint::default_error_checker<double, boost::numeric::odeint::range_algebra, boost::numeric::odeint::default_operations>::value_type {aka double}’ in return
    return algebra.norm_inf( x_err );

这是否意味着norm_result_type的推断不正确?这个规范到底有什么作用?它应该是x_err中发现的最高value_type吗?

第二个:

/usr/include/boost/numeric/odeint/algebra/range_algebra.hpp:132:89: error: call of overloaded ‘Matrix(int)’ is ambiguous
    static_cast< typename norm_result_type<S>::type >( 0 ) );

我必须提供自己的代数才能以这种方式使用它吗?我宁愿不切换到std::vector<double>或特征::VectorNd,因为将坐标分组非常有利于常微分方程右侧的可读性。

这是我使用的代码的简化示例。

#include "boost/numeric/odeint.hpp"
#include "boost/numeric/odeint/external/eigen/eigen.hpp"
#include "Eigen/Core"
using namespace boost::numeric::odeint;
typedef std::vector<Eigen::Vector3d> state_type;
struct f
{
    void operator ()(const state_type& state, state_type& change, const  double /*time*/)
    {
    };
};
int main()
{
    // Using this compiles
    typedef euler <state_type> stepper_euler;
    // Using this does not compile
    typedef bulirsch_stoer <state_type> stepper_burlisch;
    state_type x;
    integrate_const(
        stepper_burlisch(),
        f(),
        x,
        0.0,
        1.0,
        0.1
        );
    return 0;
}

头肩解决方案奏效了。我创建了一个从range_algebra继承的代数:

class custom_algebra : public boost::numeric::odeint::range_algebra {
    public:
        template< typename S >
        static double norm_inf( const S &s )
        {
            double norm = 0;
            for (const auto& inner : s){
                const double tmp = inner.maxCoeff();
                if (tmp > norm)
                    norm = tmp;
            }
            return norm;
        }
} ;

我认为应该可以使用range_algebravector_space_algebra创建这样的代数,但我还没有尝试过。

我担心你的状态类型不容易集成。问题是,将类似范围的状态类型(vector<>)与类似向量空间的状态类型(Vector3d)混合在一起。您需要range_algebra来迭代向量。但是range_algebra norm_inf不适用于您的情况。

您可以做的是复制range_algebra并保留所有for_eachX方法,并且仅重新实现norm_inf以适应您的状态类型。这应该不难。然后,您只需通过以下途径使用新的代数

typedef bulirsch_stoer< state_type , double , state_type , double , your_algebra > stepper_burlisch