// ---- Physics: the real solar system. Newtonian point-mass gravity between every pair, real masses, // ---- no softening, leapfrog integrator. Units: AU, days, solar masses. Runs headless in tests too. const Sim=(function(){ const K_GAUSS=0.01720209895, G=K_GAUSS*K_GAUSS; // AU^3 / (solar mass * day^2) const AU_KM=149597870.7, DEG=Math.PI/180, MAX_STEP=.2, MIN_SUB=8; // JPL "Approximate Positions of the Planets", Table 1 (1800-2050), J2000 ecliptic: // [a AU, e, I deg, L deg, long.peri deg, long.node deg] and their rates per Julian century const EL={ Mercury:[[0.38709927,0.20563593,7.00497902,252.25032350,77.45779628,48.33076593],[0.00000037,0.00001906,-0.00594749,149472.67411175,0.16047689,-0.12534081]], Venus:[[0.72333566,0.00677672,3.39467605,181.97909950,131.60246718,76.67984255],[0.00000390,-0.00004107,-0.00078890,58517.81538729,0.00268329,-0.27769418]], EMB:[[1.00000261,0.01671123,-0.00001531,100.46457166,102.93768193,0.0],[0.00000562,-0.00004392,-0.01294668,35999.37244981,0.32327364,0.0]], Mars:[[1.52371034,0.09339410,1.84969142,-4.55343205,-23.94362959,49.55953891],[0.00001847,0.00007882,-0.00813131,19140.30268499,0.44441088,-0.29257343]], Jupiter:[[5.20288700,0.04838624,1.30439695,34.39644051,14.72847983,100.47390909],[-0.00011607,-0.00013253,-0.00183714,3034.74612775,0.21252668,0.20469106]], Saturn:[[9.53667594,0.05386179,2.48599187,49.95424423,92.59887831,113.66242448],[-0.00125060,-0.00050991,0.00193609,1222.49362201,-0.41897216,-0.28867794]], Uranus:[[19.18916464,0.04725744,0.77263783,313.23810451,170.95427630,74.01692503],[-0.00196176,-0.00004397,-0.00242939,428.48202785,0.40805281,0.04240589]], Neptune:[[30.06992276,0.00859048,1.77004347,-55.12002969,44.96476227,131.78422574],[0.00026291,0.00005105,0.00035372,218.45945325,-0.32241464,-0.00508664]] }; // mass in solar masses, mean radius in km, orbital period in days (used only to pace the trails) const DEFS=[ {name:'Sun', kind:'star', m:1, R:695700,color:0xffcc33,P:4332.6}, {name:'Mercury',kind:'planet',m:1.6601e-7, R:2439.7,color:0xb0a8a0,P:87.969, el:'Mercury'}, {name:'Venus', kind:'planet',m:2.4478e-6, R:6051.8,color:0xf0d090,P:224.701,el:'Venus'}, {name:'Earth', kind:'planet',m:3.0035e-6, R:6371.0,color:0x4fa3ff,P:365.256}, {name:'Moon', kind:'moon', m:3.6943e-8, R:1737.4,color:0xcfcfcf,P:27.3217,parent:'Earth'}, {name:'Mars', kind:'planet',m:3.2272e-7, R:3389.5,color:0xff6b4a,P:686.98, el:'Mars'}, {name:'Jupiter',kind:'planet',m:9.5479e-4, R:69911, color:0xe8b98a,P:4332.6, el:'Jupiter'}, {name:'Saturn', kind:'planet',m:2.8589e-4, R:58232, color:0xe9d9a6,P:10759.2,el:'Saturn'}, {name:'Uranus', kind:'planet',m:4.3662e-5, R:25362, color:0x9fe3e8,P:30688.5,el:'Uranus'}, {name:'Neptune',kind:'planet',m:5.1514e-5, R:24622, color:0x5b7cff,P:60182, el:'Neptune'} ]; const MOON_FRAC=3.6943e-8/(3.6943e-8+3.0035e-6); function planetEcl(name,jd){ // heliocentric J2000 ecliptic position, AU const T=(jd-2451545)/36525,[e0,r]=EL[name]; const a=e0[0]+r[0]*T,e=e0[1]+r[1]*T,I=(e0[2]+r[2]*T)*DEG,L=e0[3]+r[3]*T,wb=e0[4]+r[4]*T,Om=(e0[5]+r[5]*T)*DEG; let M=(((L-wb)%360)+540)%360-180; M*=DEG; let E=M+e*Math.sin(M); for(let i=0;i<10;i++) E-=(E-e*Math.sin(E)-M)/(1-e*Math.cos(E)); const xp=a*(Math.cos(E)-e),yp=a*Math.sqrt(1-e*e)*Math.sin(E); const w=wb*DEG-Om,cw=Math.cos(w),sw=Math.sin(w),cO=Math.cos(Om),sO=Math.sin(Om),cI=Math.cos(I),sI=Math.sin(I); return [(cw*cO-sw*sO*cI)*xp+(-sw*cO-cw*sO*cI)*yp,(cw*sO+sw*cO*cI)*xp+(-sw*sO+cw*cO*cI)*yp,sw*sI*xp+cw*sI*yp]; } function moonEcl(jd){ // Moon relative to Earth, J2000 ecliptic, AU (truncated lunar series, main periodic terms) const d=jd-2451545,T=d/36525,s=x=>Math.sin(x*DEG),c=x=>Math.cos(x*DEG); const Lp=218.3164477+13.17639648*d, D=297.8501921+12.19074912*d, M=357.5291092+0.98560028*d, Mp=134.9633964+13.06499295*d, F=93.2720950+13.22935024*d; const lon=Lp+6.288774*s(Mp)+1.274027*s(2*D-Mp)+.658314*s(2*D)+.213618*s(2*Mp)-.185116*s(M)-.114332*s(2*F)+.058793*s(2*D-2*Mp)+.057066*s(2*D-M-Mp)+.053322*s(2*D+Mp)+.045758*s(2*D-M)-.040923*s(M-Mp)-.034720*s(D)-.030383*s(M+Mp) -1.396971*T; // precess from the ecliptic of date back to J2000 const lat=5.128122*s(F)+.280602*s(Mp+F)+.277693*s(Mp-F)+.173237*s(2*D-F)+.055413*s(2*D-Mp+F)+.046271*s(2*D-Mp-F)+.032573*s(2*D+F); const r=(385000.56-20905.355*c(Mp)-3699.111*c(2*D-Mp)-2955.968*c(2*D)-569.925*c(2*Mp)+48.888*c(M)-3.149*c(2*F)+246.158*c(2*D-2*Mp)-152.138*c(2*D-M-Mp)-170.733*c(2*D+Mp)-204.586*c(2*D-M)-129.620*c(M-Mp)+108.743*c(D)+104.755*c(M+Mp))/AU_KM; return [r*c(lat)*c(lon),r*c(lat)*s(lon),r*s(lat)]; } // heliocentric ecliptic position of a named body from the formulas above (no N-body): used for start-up and for checking function formula(name,jd){ if(name==='Sun') return [0,0,0]; if(name==='Earth'||name==='Moon'){ const e=planetEcl('EMB',jd),m=moonEcl(jd),k=name==='Earth'?-MOON_FRAC:1-MOON_FRAC; return [e[0]+k*m[0],e[1]+k*m[1],e[2]+k*m[2]]; } return planetEcl(name,jd); } let nextId=1; function body(o){ return Object.assign({id:nextId++,x:0,y:0,z:0,vx:0,vy:0,vz:0,ax:0,ay:0,az:0,launched:false},o); } // scene axes: ecliptic x -> x, ecliptic north -> y (up), ecliptic y -> -z const ax3=p=>[p[0],p[2],-p[1]]; function make(jd,mode){ const bodies=[]; for(const d of DEFS){ const h=d.kind==='moon'?.002:.02, p=formula(d.name,jd),a=formula(d.name,jd-h),b=formula(d.name,jd+h); // velocity = time derivative of the ephemeris const P=ax3(p),V=ax3([(b[0]-a[0])/(2*h),(b[1]-a[1])/(2*h),(b[2]-a[2])/(2*h)]); bodies.push(body({name:d.name,kind:d.kind,m:d.m,R:d.R,Rau:d.R/AU_KM,color:d.color,P:d.P,parent:d.parent,x:P[0],y:P[1],z:P[2],vx:V[0],vy:V[1],vz:V[2]})); } if(mode==='binary'){ // what-if: the Sun's mass split into two stars 0.08 AU apart const S=bodies[0],sep=.08,mA=.6,mB=.4,vr=Math.sqrt(G*(mA+mB)/sep); const A=body({name:'Sun A',kind:'star',m:mA,R:695700*.62,Rau:695700*.62/AU_KM,color:0xffcc33,P:4332.6,x:S.x-sep*mB,y:S.y,z:S.z,vx:S.vx,vy:S.vy,vz:S.vz+vr*mB}); const B=body({name:'Sun B',kind:'star',m:mB,R:695700*.42,Rau:695700*.42/AU_KM,color:0xff8a4a,P:4332.6,x:S.x+sep*mA,y:S.y,z:S.z,vx:S.vx,vy:S.vy,vz:S.vz-vr*mA}); bodies.splice(0,1,A,B); } // work in the frame of the system's centre of mass let px=0,py=0,pz=0,cx=0,cy=0,cz=0,T=0; for(const b of bodies){ px+=b.vx*b.m;py+=b.vy*b.m;pz+=b.vz*b.m; cx+=b.x*b.m;cy+=b.y*b.m;cz+=b.z*b.m; T+=b.m; } for(const b of bodies){ b.vx-=px/T;b.vy-=py/T;b.vz-=pz/T; b.x-=cx/T;b.y-=cy/T;b.z-=cz/T; } return {mode:mode==='binary'?'binary':'single',bodies,jd,jd0:jd}; } function add(world,o){ const b=body(o); world.bodies.push(b); return b; } function accel(bs){ const n=bs.length; for(let i=0;i=0;i--){ b=bs[i]; if(b.kind==='star') continue; let gone=null; for(const s of bs){ if(s.kind!=='star') continue; const dx=b.x-s.x,dy=b.y-s.y,dz=b.z-s.z; if(dx*dx+dy*dy+dz*dz500*500) gone='escaped'; } if(gone){ bs.splice(i,1); ev.push({type:gone,body:b}); } } return ev; } function energy(bs){ let E=0; for(let i=0;i0){ o.period=2*Math.PI*Math.sqrt(a*a*a/mu); // days o.hill=a*(1-e)*Math.cbrt(b.m/(3*S.m)); // Hill radius, AU: inside it the body's own gravity wins const GMs=G_SI*S.m*MSUN_KG, am=a*AU_KM*1000; o.gr=6*Math.PI*GMs/(c*c*am*(1-e*e))*206264.806*(36525/o.period); // general-relativistic perihelion advance, arcsec per century o.sunDilation=GMs/(am*c*c)+v2*Math.pow(AU_KM*1000/DAY,2)/(2*c*c); } // clock rate lost to the Sun's potential plus orbital speed return o; } // gravitational potential per unit mass at a point, in solar masses per AU (multiply by G for AU^2/day^2). Inside a body it stays at the surface value. function potential(bs,x,y,z){ let p=0; for(const b of bs){ const d=Math.hypot(b.x-x,b.y-y,b.z-z); p+=b.m/Math.max(d,b.Rau); } return p; } // ---- The Sun's future, after Schroder & Smith (2008, MNRAS 386, 155), Table 1. t = billions of years from now. // Rows marked P are the paper's own models; the others are smooth joins between them (and a standard white-dwarf cooling track), // there only so the picture does not jump. Columns: t, radius (present Sun = 1), surface temperature K, mass, stage. const SUN=[ [0, 1, 5774, 1, 'Main sequence (today)','P'], [2.55, 1.11, 5820, 1, 'Main sequence, at its hottest','P'], [5.42, 1.37, 5751, 1, 'End of the main sequence','P'], [7.00, 2.3, 4950, 0.998, 'Subgiant'], [7.42, 9.5, 4540, 0.985, 'Red giant'], [7.555, 70, 3450, 0.91, 'Red giant'], [7.585, 180, 2850, 0.74, 'Red giant'], [7.59, 256, 2602, 0.668, 'Tip of the red giant branch','P'], [7.5903, 11.2, 4667, 0.668, 'Helium ignites: the Sun shrinks','P'], [7.69, 16, 4500, 0.664, 'Burning helium'], [7.714, 90, 3500, 0.61, 'Second ascent (asymptotic giant)'], [7.72, 149, 3200, 0.546, 'Tip of the asymptotic giant branch','P'], [7.7201, 179, 3467, 0.544, 'Final thermal pulse','P'], [7.7212, 1.2, 30000, 0.5405, 'Envelope gone: the hot core is exposed'], [7.723, 0.03, 100000,0.5405, 'New white dwarf'], [7.80, 0.0135, 28000, 0.5405, 'White dwarf, cooling'], [8.70, 0.0128, 9500, 0.5405, 'White dwarf, cooling'], [12, 0.0126, 5000, 0.5405, 'White dwarf, cooling'] ]; const T_END=SUN[SUN.length-1][0], T_TIP=7.59, T_EARTH=7.5895; // Earth is drawn in by tides just before the tip (the paper's result) function sunAt(t){ t=Math.min(T_END,Math.max(0,t)); let i=0; while(iSUN[i+1][0]) i++; const A=SUN[i],B=SUN[i+1],k=(t-A[0])/(B[0]-A[0]), R=Math.exp(Math.log(A[1])+(Math.log(B[1])-Math.log(A[1]))*k), T=Math.exp(Math.log(A[2])+(Math.log(B[2])-Math.log(A[2]))*k); const M=A[3]+(B[3]-A[3])*k, L=R*R*Math.pow(T/5774,4); // luminosity from radius and temperature (Stefan-Boltzmann) return {t,R,Rau:R*RSUN_AU,T,M,L,stage:k<.5?A[4]:B[4],modelled:(k<.02&&A[5]==='P')||(k>.98&&B[5]==='P'),hz:[.95*Math.sqrt(L),1.37*Math.sqrt(L)]}; } // Planets in the far future. While the Sun loses mass slowly the orbits widen as a = a0 / M (angular momentum is conserved); // a planet is lost when the Sun's surface reaches its closest approach. Planets that start inside 1.15 AU are also dragged in // by tides near the tip of the red giant branch (Schroder & Smith's limit), which is what takes Earth. const FUTURE=DEFS.filter(d=>d.kind==='planet').map(d=>{ const el=EL[d.el||'EMB'][0]; return {d,a0:el[0],e:el[1],I:el[2]*DEG,wb:el[4]*DEG,Om:el[5]*DEG,L0:el[3]*DEG,lost:null,why:''}; }); for(const p of FUTURE){ for(let t=0;t<=T_END;t+=t<7.3?.01:t<7.8?.00002:.01){ const s=sunAt(t); if(s.Rau>=p.a0/s.M*(1-p.e)){ p.lost=t; p.why='swallowed as the Sun swells'; break; } } if((p.lost===null||p.lost>T_EARTH)&&p.a0<1.15){ p.lost=T_EARTH; p.why='dragged in by tides at the red giant tip'; } } function future(t){ const s=sunAt(t); return {sun:s,planets:FUTURE.map(p=>({name:p.d.name,a0:p.a0,a:p.a0/s.M,e:p.e,lost:p.lost,why:p.why,alive:p.lost===null||t=p.lost) continue; bodies.push(body({name:p.d.name,kind:'planet',m:p.d.m,R:p.d.R,Rau:p.d.R/AU_KM,color:p.d.color,P:p.d.P})); } const w={mode:'future',bodies,jd:clock,jd0:0,t}; placeFuture(w,t,clock); return w; } function placeFuture(w,t,clock){ const s=sunAt(t), S=w.bodies[0]; S.m=s.M; S.R=s.R*695700; S.Rau=s.Rau; S.x=S.y=S.z=S.vx=S.vy=S.vz=0; w.t=t; w.jd=clock; for(let i=w.bodies.length-1;i>0;i--){ const b=w.bodies[i], p=FUTURE.find(q=>q.d.name===b.name); if(p.lost!==null&&t>=p.lost){ w.bodies.splice(i,1); continue; } const a=p.a0/s.M, n=K_GAUSS*Math.sqrt(s.M)/Math.pow(a,1.5); b.P=2*Math.PI/n; let M=p.L0-p.wb+n*clock, E=M+p.e*Math.sin(M); for(let k=0;k<8;k++) E-=(E-p.e*Math.sin(E)-M)/(1-p.e*Math.cos(E)); const pos=u=>{ const xp=a*(Math.cos(u)-p.e),yp=a*Math.sqrt(1-p.e*p.e)*Math.sin(u), w2=p.wb-p.Om,cw=Math.cos(w2),sw=Math.sin(w2),cO=Math.cos(p.Om),sO=Math.sin(p.Om),cI=Math.cos(p.I),sI=Math.sin(p.I); return ax3([(cw*cO-sw*sO*cI)*xp+(-sw*cO-cw*sO*cI)*yp,(cw*sO+sw*cO*cI)*xp+(-sw*sO+cw*cO*cI)*yp,sw*sI*xp+cw*sI*yp]); }; const P0=pos(E), P1=pos(E+1e-4), dt=1e-4*(1-p.e*Math.cos(E))/n; b.x=P0[0];b.y=P0[1];b.z=P0[2]; b.vx=(P1[0]-P0[0])/dt;b.vy=(P1[1]-P0[1])/dt;b.vz=(P1[2]-P0[2])/dt; } return w; } return {G,AU_KM,DEFS,make,add,accel,advance,starCentre,energy,formula,ax3,facts,potential,sunAt,future,makeFuture,placeFuture,SUN,T_END,RSUN_AU}; })(); if(typeof module!=='undefined') module.exports=Sim;