试图模拟太阳系,但加速度是错误的

时间:2017-12-07 14:43:02

标签: java

我必须为我的物理课程制作太阳系的模拟。我已经制作了一个非常基本的代码,使用欧拉方程模拟Sol和地球相互绕过。

然而,使用牛顿万有引力定律产生的加速度是错误的,我不知道为什么。

public class SolarSystemSim
{

    public static void main(String[] args)
    {

        PhysicsVector EarthPos= new PhysicsVector(4.39e10,1.42e11,-2.44e7);
        PhysicsVector EarthVel= new PhysicsVector(-2.89e4,8.71e3,-8.36e-1);
        double EarthMass= 5.972e24;

        PhysicsVector SunPos= new PhysicsVector(2.93e8,8.96e8,-1.85e7);
        PhysicsVector SunVel= new PhysicsVector(-9.75,9.01,2.34e-1);
        double SunMass= 1.989e30;

        /**PhysicsVector MercuryPos= new PhysicsVector(5.791e10,0,0);
        PhysicsVector MercuryVel= new PhysicsVector(0,47400,0);
        double MercuryMass= 3.285e23;**/

        Particle Earth= new Particle(EarthPos,EarthVel,EarthMass);
        Particle Sun= new Particle(SunPos,SunVel, SunMass);
        //Particle Mercury= new Particle(MercuryPos,MercuryVel,MercuryMass);

        Scanner s1= new Scanner(System.in);
        System.out.println("Enter a timestep i.e 1 second");
        double TimeStep= s1.nextDouble();
        System.out.println("Enter a total time i.e There are 86400 seconds in a day");
        double FinalTime=s1.nextDouble();

        Particle[]Planets={Sun,Earth/**Mercury**/};
        PhysicsVector acc= new PhysicsVector();
        PhysicsVector[]PlanetAccelerations= new PhysicsVector[Planets.length];

        for(int t=0;t<FinalTime/TimeStep;t++)
        {

            for(int k=0;k<Planets.length;k++)
            {
                PhysicsVector PlanetPos= Planets[k].GetPos();
                acc= new PhysicsVector(0,0,0);

                for(int i=0;i<Planets.length;i++)
                {
                    PhysicsVector r= PhysicsVector.subtract(PlanetPos,Planets[i].GetPos());

                    System.out.println(Planets[k].GetMass());
                    System.out.println(Planets[i].GetMass());

                    Gravity Grav= new Gravity();
                    acc.increaseBy(Grav.GravitySim(r,Planets[i].GetMass()));

                    acc.print();
                }

            PlanetAccelerations[k]=acc;

            }

            for(int j=0;j<Planets.length;j++)
            {
                PhysicsVector ac= new PhysicsVector();
                ac= PlanetAccelerations[j];

                Planets[j].Euler(TimeStep,ac);

            }
        }

        Planets[0].GetPos().print();
        Planets[1].GetPos().print();
        //Planets[2].GetPos().print();
    }
}

public class PhysicsVector {  

    private static final int  vectorSize=3;
    private double[]  vectorComponents = new double[vectorSize];

    public PhysicsVector()
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] =0.;
        }
    }

    public PhysicsVector(double x, double y)
    {
        vectorComponents[0] = x;
        vectorComponents[1] = y;
        vectorComponents[2] = 0.;
    }

    public PhysicsVector(double x, double y, double z)
    {
        vectorComponents[0] = x;
        vectorComponents[1] = y;
        vectorComponents[2] = z;
    }

    public PhysicsVector(double [] x)
    {
        if (x.length == vectorSize){
            for (int i=0; i<vectorComponents.length; i++)
                {vectorComponents[i] = x[i];}
        }
        else if (x.length == vectorSize-1 ) {
            for (int i=0; i<x.length; i++)
                {vectorComponents[i] = x[i];}
            vectorComponents[vectorComponents.length-1] = 0.;
        }
        else {this.setVector(new PhysicsVector());
            System.out.println(
                "WARNING: PhysicsVector(double [] x) " +
                "requires an array of length " + vectorSize);
        }
    }

    public PhysicsVector(PhysicsVector v)
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] = v.vectorComponents[i];
        }
    }

    public void print()
    {
        String text = this.returnString();
        System.out.println(text);
    }

    public void setVector(double x, double y, double z)
    {
        vectorComponents[0] = x;
        vectorComponents[1] = y;
        vectorComponents[2] = z;
    }

    public void setVector(double x, double y)
    {
        vectorComponents[0] = x;
        vectorComponents[1] = y;
        vectorComponents[2] = 0.;
    }

    public void setVector(PhysicsVector v)
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] = v.vectorComponents[i];
        }
    }

    public void increaseBy(PhysicsVector v)
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] += v.vectorComponents[i];
        }
    }

    public void decreaseBy(PhysicsVector v)
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] -= v.vectorComponents[i];
        }
    }

    public double magnitude()
    {
        double mag = dot(this,this);
        mag = Math.sqrt(mag);
        return mag;
    }

    public double getX()
    {
        return vectorComponents[0];
    }

    public double getY()
    {
        return vectorComponents[1];
    }

    public double getZ()
    {
        return vectorComponents[2];
    }

    public PhysicsVector getUnitVector()
    {
        PhysicsVector unit = new PhysicsVector(this);
        double magnitude = this.magnitude();

        if (Math.abs(magnitude) > 1.e-34)

            {unit.scale(1/magnitude);}
        else 
            {unit = new PhysicsVector();}
        return unit;
    }

    public void scale(double x)
    {
        for (int i=0; i<vectorComponents.length; i++)
        {
            vectorComponents[i] *= x;
        }
    }

    public static PhysicsVector scale(double x, PhysicsVector v)
    {
        PhysicsVector scaled = new PhysicsVector(v);
        for (int i=0; i<(scaled.vectorComponents).length; i++)
        {
            scaled.vectorComponents[i] *=x;
        }
        return scaled;
    }

    public static PhysicsVector subtract(PhysicsVector u, PhysicsVector v)
    {
        PhysicsVector sum = new PhysicsVector(u);
        sum.decreaseBy(v);
        return sum;
    }

    public static double SumArray(double[] a)
    {
        double sum= 0;
        for(int i=0;i<a.length;i++)
        {
            sum+=a[i];
        }
        return sum;
    }

    public static void PrintArray(PhysicsVector[] b)
    {
        for(int i=0;i<b.length;i++)
        {
            b[i].print();
        }
    }

}

public class Gravity
{
    private double G=6.67408e-11;
    private PhysicsVector position= new PhysicsVector();
    private double Mass;

    public Gravity(PhysicsVector x, double M)
    {
        position=  new PhysicsVector(x);
        double Mass= M;
    }

    public Gravity()
    {
        position= new PhysicsVector();
        double Mass= 0.0;
    }

    public PhysicsVector GravitySim(PhysicsVector r, double M)
    {

        if(r.magnitude()!=0)
        {
            double g=-(G*M)/(r.magnitude()*r.magnitude());

            PhysicsVector acc=new PhysicsVector(r.getUnitVector());
            acc.scale(g);

            return acc;
        }

        else
        {
            PhysicsVector zero= new PhysicsVector(); 
            return zero;
        }

    }

}

public class Particle
{
    private PhysicsVector velocity= new PhysicsVector();
    private PhysicsVector position= new PhysicsVector();
    private PhysicsVector TempV= new PhysicsVector();
    private PhysicsVector TempV1= new PhysicsVector();
    private PhysicsVector TempV2= new PhysicsVector();
    private PhysicsVector TempP= new PhysicsVector();
    private PhysicsVector TempP1= new PhysicsVector();
    private double Mass;

    public Particle()
    {
        position= new PhysicsVector();
        velocity= new PhysicsVector();
        Mass= 0.0;
    }

    public Particle(PhysicsVector x,PhysicsVector y,double M)
    {
        position=new PhysicsVector(x);
        velocity=new PhysicsVector(y);
        Mass=M;
    }

    public void Euler(double t,PhysicsVector ac)
    {   
        position.increaseBy(PhysicsVector.scale(t,velocity));
        velocity.increaseBy(PhysicsVector.scale(t,ac));
    }

    public PhysicsVector GetPos()
    {
        return position;
    }

    public double GetMass()
    {
        return Mass;
    }
}

我一直在TimeStep = 1和FinalTime = 2进行模拟,并得到结果:

  • 太阳加速度= 5.6e-9 m/s^2

  • 地球加速度= -1.8e-3 m/s^2

什么时候应该:

  • 太阳加速度= 1.8e-8 m/s^2

  • 地球加速度= - 5.9e-3 m/s^2

0 个答案:

没有答案