Runge-Kutta N-Body实施无效

时间:2017-03-17 02:26:52

标签: java algorithm math runge-kutta

我已经在Java中实现了本文所述的Runge-Kutta 4算法。 http://spiff.rit.edu/richmond/nbody/OrbitRungeKutta4.pdf

但是,这些动作不稳定。即使只有两具尸体,也没有周期性的移动。

protected void integrateRK4(double time) {

    final double H = time;
    final double HO2 = H/2;
    final double HO6 = H/6;

    Vector2[] currentVelocities = new Vector2[objects.length];
    Vector2[] currentPositions = new Vector2[objects.length];
    Vector2[] vk1 = new Vector2[objects.length];
    Vector2[] vk2 = new Vector2[objects.length];
    Vector2[] vk3 = new Vector2[objects.length];
    Vector2[] vk4 = new Vector2[objects.length];
    Vector2[] rk1 = new Vector2[objects.length];
    Vector2[] rk2 = new Vector2[objects.length];
    Vector2[] rk3 = new Vector2[objects.length];
    Vector2[] rk4 = new Vector2[objects.length];


    for (int i=0; i<objects.length; i++) {
        currentVelocities[i] = objects[i].velocity().clone();
        currentPositions[i] = objects[i].position().clone();
    }

        vk1 = computeAccelerations(objects);
    for (int i=0; i<objects.length; i++) {
        rk1[i] = currentVelocities[i].clone();
    }

    for (int i=0; i<objects.length; i++) {
        objects[i].setPosition(Vectors2.add(currentPositions[i], Vectors2.prod(rk1[i], HO2)));
    }
        vk2 = computeAccelerations(objects);

    for (int i=0; i<objects.length; i++) {
        rk2[i] = Vectors2.prod(vk1[i], HO2);
    }


    for (int i=0; i<objects.length; i++) {
        objects[i].setPosition(Vectors2.add(currentPositions[i], Vectors2.prod(rk2[i], HO2)));
    }
        vk3 = computeAccelerations(objects);

    for (int i=0; i<objects.length; i++) {
        rk3[i] = Vectors2.prod(vk2[i], HO2);
    }


    for (int i=0; i<objects.length; i++) {
        objects[i].setPosition(Vectors2.add(currentPositions[i], Vectors2.prod(rk3[i], H)));
    }
        vk4 = computeAccelerations(objects);

    for (int i=0; i<objects.length; i++) {
        rk4[i] = Vectors2.prod(vk3[i], H);
    }


    for (int i=0; i<objects.length; i++) {
        objects[i].setVelocity(Vectors2.add(currentVelocities[i], Vectors2.prod(Vectors2.add(vk1[i], Vectors2.prod(vk2[i], 2), Vectors2.prod(vk3[i], 2), vk4[i]), HO6)));
        objects[i].setPosition(Vectors2.add(currentPositions[i], Vectors2.prod(Vectors2.add(rk1[i], Vectors2.prod(rk2[i], 2), Vectors2.prod(rk3[i], 2), rk4[i]), HO6)));
    }
}

我的实施方式是否有误?

注意:

Vectors2是我自己的Vectors实现,它是一个大小为2的Tensor 所有静态方法Vectors2.*都返回向量的副本 调用Vector2实例的所有非静态方法都修改了向量,objects[i].addVelocity()objects[i].addPosition()

相同

Vectors2.add(Vector2... vectors2)进行元素添加 Vectors2.prod(Vector2... vectors2)进行逐元素乘法 Vectors2.prod(Vector2 vector2, double c)进行逐元素乘法。

computeAccelerations(PointBody[] objects)返回加速度矢量数组 变量objects是类NBodyIntegrator的属性,这些函数是其中的一部分。它是PointBody[]的数组。

为了确保它不是其他错误,我通过删除k2,k3和k4将RK4算法简化为显式欧拉算法。 这个按预期工作。

protected void integrateRK1(double time) {
    final double H = time;
    final double HO2 = H/2;

    Vector2[] currentVelocities = new Vector2[objects.length];
    Vector2[] currentPositions = new Vector2[objects.length];
    Vector2[] vk1;
    Vector2[] rk1 = new Vector2[objects.length];


    for (int i=0; i<objects.length; i++) {
        currentVelocities[i] = objects[i].velocity().clone();
        currentPositions[i] = objects[i].position().clone();
    }


        vk1 = computeAccelerations(objects);
    for (int i=0; i<objects.length; i++) {
        rk1[i] = currentVelocities[i].clone();
    }


    for (int i=0; i<objects.length; i++) {
        objects[i].setVelocity(Vectors2.add(currentVelocities[i], Vectors2.prod(Vectors2.add(vk1[i]), H)));
        objects[i].setPosition(Vectors2.add(currentPositions[i], Vectors2.prod(Vectors2.add(rk1[i]), H)));
    }
}

2 个答案:

答案 0 :(得分:2)

您正在设置

rk1 = v0
pos2 = pos0 + rk1*h/2
vk2 = acc(pos2)

这是正确的。但接着你继续

rk2 = vk1*h/2

应该在哪里

rk2 = v0 + vk1*h/2

等等。当然,累积位置更新也是错误的。

答案 1 :(得分:-1)

这不是RK集成的良好实现。

您正在使用共享的可变数据。这不是线程安全的。

我曾见过的任何数值集成的正确实现都将要集成的函数抽象为传递到集成方案中的方法。您应该只需将函数传递给新例程即可更改集成方案。

从集成接口开始,该接口将函数和初始条件作为参数。