/* Scilab code "sunearthmoon.sce" from: Shaula Fiorelli Vilmart and Gilles
Vilmart, Computing the long term evolution of the solar system with geometric
numerical integrators, Snapshots of modern mathematics from Oberwolfach, 2017.

The snapshot is licensed under the Creative Commons Attribution-ShareAlike 4.0 
license (CC-BY-SA-4.0). 
This code is licensed under the MIT license as specified in the copyright
notice below.

Code and snapshot are available from http://dx.doi.org/10.14760/SNAP-2017-009-EN

Further information on Scilab and download is available from http://www.scilab.org


Copyright Notice/License Agreement (MIT License) 
------------------------------------------------

Copyright © 2017 Shaula Fiorelli Vilmart and Gilles Vilmart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

//______________________________________________________________________________

//Newton's law of gravitation
function f=fun_v(q)
  deff('[v]=vecf(v0)','v=v0/norm(v0).^3');
  sun=1:3;earth=4:6;moon=7:9;
  f(sun)  =-G*m0*m1*vecf(q(sun) -q(earth))..
           -G*m0*m2*vecf(q(sun) -q(moon));
  f(earth)=-G*m1*m0*vecf(q(earth)-q(sun))..
           -G*m1*m2*vecf(q(earth)-q(moon));
  f(moon) =-G*m2*m0*vecf(q(moon)-q(sun))..
           -G*m2*m1*vecf(q(moon)-q(earth));
endfunction

//Newton's second law of motion
function f=fun_u(p)
  f=[p(1:3)/m0;p(4:6)/m1;p(7:9)/m2];
endfunction

//Symplectic Euler method
function [vp,vq]=euler_symplectic(n,h,p,q)
vp=p;vq=q;
for i=1:n
  q=q+h*fun_u(p);  p=p+h*fun_v(q);
  vq=[vq,q];       vp=[vp,q];
end
endfunction

//Explicit Euler method
function [vp,vq]=euler_explicit(n,h,p,q)
vp=p;vq=q;
for i=1:n
  tmp=q; q=q+h*fun_u(p);  p=p+h*fun_v(tmp); 
  vq=[vq,q];       vp=[vp,q];
end
endfunction

//Sun-Earth-Moon system integration
//
//body masses
m0=1;m1=3.00348959632E-6;m2=m1*1.23000383E-2; 
//gravitational constant
G=2.95912208286e-4; 
//Initial conditions
//Source: PORTAIL SYSTEME SOLAIRE
//        OBSERVATOIRE VIRTUEL DE L'IMCCE
//        Observatoire de Paris / CNRS
//http://vo.imcce.fr/webservices/miriade/?forms
//Target: p:Earth, s:Moon
//Epoch: 2016-01-01 00:00:00, 1, 1.0 - day, UTC
//Reference center: 
//INPOP Ecliptic Rectangular AstrometricJ2000
//Ecliptic coordinates
//Initial positions and velocities
q0=[0;0;0;-0.1667743823220;0.9690675883429;-0.0000342671456;..
          -0.1694619061456;0.9692330175719;-0.0000266725711];
v0=[0;0;0;-0.0172346557280;-0.0029762680930;-0.0000004154391;..
          -0.0172817331582;-0.0035325102831;0.0000491191454];
//Initial momenta
p0=[v0(1:3)*m0;v0(4:6)*m1;v0(7:9)*m2];
          
//Time integration over 365 days -- choose one of the following:
[vp,vq]=euler_symplectic(365*10,0.1,p0,q0) //stepsize h=0.1
//[vp,vq]=euler_explicit(365*10,0.1,p0,q0) //stepsize h=0.1

//Trajectories with respect to the Sun placed at the origin
vq(4:6,:)=vq(4:6,:)-vq(1:3,:); 
vq(7:9,:)=vq(7:9,:)-vq(1:3,:);  
comet3d(0,0,0);

//Increase the Earth-Moon distance by a factor 100 for visualization.
vq(7:9,:)=vq(4:6,:)+100*(vq(7:9,:)-vq(4:6,:))
comet3d([vq(4,:)',vq(7,:)'],[vq(5,:)',vq(8,:)'],[vq(6,:)',vq(9,:)']);
