8
$\begingroup$

I'm studying a particular case of the restricted three-body problem. It's been found that some objects follow a horseshoe orbit pattern, and I'm trying to sort out something through an integration code in C. I'm following some advice in the article Families of periodic horseshoe orbits in the restricted three-body problem, which gives me ideal initial conditions and the equations in the centre of mass system. (m is the mass of Earth, and consequent position of the sun in the center of mass reference system, (x,y) are the coordinates of the third body, assumed massless (as the restricted problem requires).

$$ O=(x^2+y^2)/2+\dfrac{(1-m)}{r1}+\dfrac{m}{r2}+\dfrac{(1-m)m}{2} $$ $$r1^2=(x-m)^2+y^2$$
$$r2^2=(x-m+1)^2+y^2$$ $$ a(x)=\dfrac{\mathrm{d}{O}}{\mathrm{d}x} + 2v(y)$$ $$a(y)=\dfrac{\mathrm{d}{O}}{\mathrm{d}y}-2v(x)$$

The positions of the "sun" and "earth" are fixed to (m,0) and (m-1,0), in the same reference system. (rotating reference system, assuming the earth has a circular orbit.)

From all this I've calculated the equations to describe the system:

$$a(x)=x+\dfrac{(m-1)(x-m)}{((x-m)^2+y^2)^1.5}-\dfrac{2m(x-m+1)}{((x-m+1)^2+y^2)^1.5} + 2v(y)$$ $$a(y)=y-\dfrac{y(1-m)}{((x-m)^2+y^2)^1.5} -\dfrac{2ym}{((x-m+1)^2+y^2)^1.5} -2v(x)$$

I've used the algorithm of Runge-Kutta 4 to integrate those equations. (I know the code is quite mind-twisting, but I just can't use pointers and I use structs everywhere).

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define dt 0.0001
#define N 100

typedef struct{
    long double x,y;
}vec;

typedef struct{
    vec k1,k2,k3,k4;
}runge;

typedef struct{
    runge r,v;
}big;

double dS,dE,m;

double accx(double,double,double);
double accy(double,double,double);
void rad(vec);
big rungekutta(vec,vec);
vec moto(vec,runge);
double jacobi(vec);

int main(){
  vec r,v;
    big f;
    double J,t;
    int i,Num;
    FILE* s1;
    s1=fopen("HorseShoe.dat","w");

    Num=(int)N/dt;
    scanf("%Lf",&r.x);
    scanf("%Lf",&r.y);
    scanf("%Lf",&v.x);
    scanf("%Lf",&v.y);
    scanf("%lf",&m);

    for(i=0;i<Num;i++){
        t=(i+1)*dt;
        rad(r);
        f=rungekutta(r,v);
        r=moto(r,f.r);
        v=moto(v,f.v);
        J=jacobi(r);
        fprintf(s1,"%lf\t%Lf\t%Lf\t%Lf\t%Lf\t%lf\n",t,r.x,r.y,v.x,v.y,J);
    }
return 0;
}

void rad(vec r){
    dS=pow(r.x-m,2)+pow(r.y,2);
    dE=pow(r.x-m+1,2)+pow(r.y,2);
}

double jacobi(vec r){
    return pow(r.x,2)+pow(r.y,2)+2*(1-m)/dS+2*m/dE+m*(1-m);
}

double accx(double x,double y,double v){
    return x-(x-m)*(1-m)/pow(pow(x-m,2)+pow(y,2),1.5)-m*(x-m+1)/pow(pow(x-m+1,2)+pow(y,2),1.5)+2*v;
}

double accy(double x,double y,double v){
    return y-(1-m)*y/pow(pow(y,2)+pow(x-m,2),1.5)-m*y/pow(pow(y,2)+pow(x-m+1,2),1.5)-2*v;
}

big rungekutta(vec r,vec v){
    big f;
    f.r.k1.x=v.x;
    f.r.k1.y=v.y;
    f.v.k1.x=accx(r.x,r.y,v.y);
    f.v.k1.y=accy(r.x,r.y,v.x);
    f.r.k2.x=v.x+f.v.k1.x*dt/2;
    f.r.k2.y=v.y+f.v.k1.y*dt/2;
    f.v.k2.x=accx(r.x+f.r.k1.x*dt/2,r.y+f.r.k1.y*dt/2,v.y+f.v.k1.y*dt/2);
    f.v.k2.y=accy(r.x+f.r.k1.x*dt/2,r.y+f.r.k1.y*dt/2,v.x+f.v.k1.x*dt/2);
    f.r.k3.x=v.x+f.v.k2.x*dt/2;
    f.r.k3.y=v.y+f.v.k2.y*dt/2;
    f.v.k3.x=accx(r.x+f.r.k2.x*dt/2,r.y+f.r.k2.y*dt/2,v.y+f.v.k2.y*dt/2);
    f.v.k3.y=accy(r.x+f.r.k2.x*dt/2,r.y+f.r.k2.y*dt/2,v.x+f.v.k2.x*dt/2);
    f.r.k4.x=v.x+f.v.k3.x*dt;
    f.r.k4.y=v.y+f.v.k3.y*dt;
    f.v.k4.x=accx(r.x+f.r.k3.x*dt,r.y+f.r.k3.y*dt,v.y+f.v.k3.y*dt);
    f.v.k4.y=accy(r.x+f.r.k3.x*dt,r.y+f.r.k3.y*dt,v.x+f.v.k3.x*dt);
    return f;
}

vec moto(vec r,runge rk){
    r.x+=(rk.k1.x+2*rk.k2.x+2*rk.k3.x+rk.k4.x)*dt/6;
    r.y+=(rk.k1.y+2*rk.k2.y+2*rk.k3.y+rk.k4.y)*dt/6;
    return r;
}

Plotting the results I just get a spiral, while using the given inputs I should get a horseshoe orbit. I've tried many different inputs, (m=0.0001 and m=0.000003, the latter is in scale with the actual values of Earth and Sun masses (sun mass is 1-m)).

I just can't see what's wrong (probably everything :D), can anyone please help me?

$\endgroup$
  • $\begingroup$ People might be better able to help if you explain what your variables (in the equations) are: O,x,y,m,r1/2 etc. Also, your question doesn't make it clear what the problem is that you are encountering. "I can't see what's wrong" -- what are you supposed to get, and what do you get instead? $\endgroup$ – Alex Dec 21 '16 at 22:30
  • $\begingroup$ Before proceeding, I would like to see two things: A link to the reference paper you used so as to see if you made a mistake in your math, and your inputs. I suspect your inputs might be off. This is a non-standard system of units in which the mass of the Earth $m$ should be about $3\times10^{-6}$, the mass of the Sun is 1 less this small number, and the magnitude of the position vector $(x,y)$ is about 1. Time appears to be scaled, also. This is not a place for SI units. $\endgroup$ – David Hammen Dec 22 '16 at 3:55
  • $\begingroup$ I've just edited the question adding the details you asked (sorry if I'm a bit messy, first question in here). I used G=1, of course I couldn't use SI units ;) $\endgroup$ – Elisa Dec 22 '16 at 8:31
  • 5
    $\begingroup$ Feel free to post an answer to your own question detailing what was wrong and how you fixed it. This may prove useful to others in the future who come to this question! $\endgroup$ – zephyr Dec 22 '16 at 19:22
  • 2
    $\begingroup$ Just a small but important remark: 4ᵗʰ order Runge-Kutta is not symplectic, meaning, it changes the total orbital energy over time, slow at first but increasing exponentially in its severity. Depending on your desired integration interval, you had better ditch RK4 in favor of something more suited for celestial mechanics. See for example this paper for some background and recommendations. $\endgroup$ – Rody Oldenhuis Dec 23 '16 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.