数值积分器来求解ode的系统

数值积分器来求解ode的系统

本文介绍了C ++数值积分器来求解ode的系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用C ++,我刚刚创建了一个类,该类允许集成用户定义的ode's系统.它使用两个不同的积分器来比较其性能.这是代码的一般布局:

I recently started using C++ and I just created a class that allows the integration of a user-defined system of ode's. It uses two different integrators in order to compare its performance. Here is the general layout of the code:

    class integrators {
    private:
      double  ti;    // initial time
            double *xi;  // initial solution
            double  tf;    // end time
            double  dt;  // time step
            int      n;  // number of ode's

        public:
            // Function prototypes
            double   f(double, double *, double *);  // function to integrate
            double rk4(int, double, double, double, double *, double *);
            double dp8(int, double, double, double, double *, double *);
        };

        // 4th Order Runge-Kutta function
        double integrators::rk4(int n, double ti, double tf, double dt, double *xi, double *xf) {
            // Function statements
        }

        // 8th Order Dormand-Prince function
        double integrators::dp8(int n, double ti, double tf, double dt, double *xi, double *xf) {
            // Function statements
        }

       // System of first order differential equations
       double integrators::f(double t, double *x, double *dx) {
           // Function statements
       }

        int main() {
            // Initial conditions and time related parameters
            const int n = 4;
            double t0, tmax, dt;
            double x0[n], xf[n];

            x0[0] = 0.0;
            x0[1] = 0.0;
            x0[2] = 1.0;
            x0[3] = 2.0;

            // Calling class integrators
            integrators example01;
            integrators example02;

            // First integrator
            example02.dp8(n, t0, tmax, dt, x0, xf);

            // Second integrator
            example01.rk4(n, t0, tmax, dt, x0, xf);
        }

问题在于,主数组中包含初始条件x0的数组在执行第一个积分器后会发生变化,除非我定义另一个具有相同初始条件(x0_rk4和x0_dp8)的数组,否则我无法对第二个积分器使用相同的初始条件).有没有更专业的方法来保持此数组恒定以便在两个积分器中都使用?

The problem is that the array containing the initial conditions x0 in main, changes after executing the first integrator and I cannot use the same initial conditions for the second integrator, unless I define another array with the same initial conditions (x0_rk4 and x0_dp8). Is there a more professional way to keep this array constant in order to use it in both integrators?

推荐答案

不,不是.但是,还有一个更优雅的解决方案:

No, not really. But there exist a more elegant solution:

std::array<double, n> x0_rk4 = { 0.0, 0.0, 1.0, 2.0 };
auto x0_dp8 = x0_rk4; // copy!

您将必须使用x0_rk4.data()来访问基础数组.请注意,如果您使用std::array和其他现代C ++功能而不是原始指针之类,那就更好了.

You will have to use x0_rk4.data() to access the underlying array. Note that it would be better if you used std::array and other modern C++ features instead of raw pointers and the like.

这篇关于C ++数值积分器来求解ode的系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:49