Welcome, Guest. Please Login.
Gravity Simulator
11/19/17 at 08:18:36
News: Registration for new users has been disabled to discourage spam. If you would like to join the forum please send me an email with your desired screen name to tony at gravitysimulator dot com.
Home Help Search Login


Pages: 1
Send Topic Print
Orbital elements (Read 1969 times)
jwbrase
Uploader



I Love YaBB 2!

Posts: 5
Orbital elements
08/31/17 at 21:01:07
 
I recall that the desktop version of Gravity Simulator allowed you to view the continously updated orbital elements of an object. In the Web app version, it's possible to see the XYZ coordinates continously updated, but the only place to view orbital elements seems to be the "Edit objects (orbital elements)" window,  which, of course, does not update continuously. Is there any chance that a means of viewing orbital elements that updates every frame could be added?
 
 
On another note,  a feature I'd like to see that wasn't in the desktop version is a means of pausing the simulation at the moment that a parameter reaches some value, or hits an inflection point. For example "Pause when a trojan reaches the point in its tadpole orbit where its  osculating semi-major axis is at a minimum", or "Pause whenever the moon's elongation with respect to the sun is 180 degrees (i.e, pause on every full moon)." I suppose this might be done with the autopilot,  but in that case would it overshoot if the desired event occurred between timesteps?
Back to top
 
 
View Profile   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #1 - 09/01/17 at 08:46:58
 
Welcome!
 
You can do the things you request in the Autopilot. I'll give you a more detailed answer later today and show you how.
Back to top
 
 
Email View Profile WWW   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #2 - 09/01/17 at 18:00:00
 
You can copy and paste the following examples into either:
menu Auto Pilot > per Graphic Update or
menu Auto Pilot > per Iteration
 
Note that in the dropdown list, Earth is the 4th object and the Moon is the 11th object.
The following code will compute the Moon's elongation from the Sun with respect to a viewer positioned at Earth's center:
 
Code:
var earthN = 4, moonN = 11;;
var CamX = objx[earthN];
var CamY = objy[earthN];
var CamZ = objz[earthN];

var ux = CamX - objx[1];
var uy = CamY - objy[1];
var uz = CamZ - objz[1];
var dES = Math.sqrt(ux*ux + uy*uy + uz*uz);

var vx = CamX - objx[moonN];
var vy = CamY - objy[moonN];
var vz = CamZ - objz[moonN];

var dot = ux*vx +uy*vy + uz*vz;
var magU = Math.sqrt(ux*ux + uy*uy + uz*uz);
var magV = Math.sqrt(vx*vx + vy*vy + vz*vz);

var theta = 180*Math.acos(dot / (magU * magV)) / Math.PI;

txtAreaQuickHelp.value = theta.toFixed(3) + " degrees";
 


 
First it establishes the two vectors: Earth to Sun (vector u), Earth to Moon (vector v).
It then finds the dot product of these two vectors.
It then finds the product of the magnitude of these vectors.
It then divides the dot product by the magnitude product and takes the arc cosine, and converts the answer from radians to degrees.
 
It then displays the result in the Quick Help box available from the Help Menu.
 
==============================
 
Here is the same example, except it prints it directly on the screen:
Code:
var earthN = 4;
var CamX = objx[earthN];
var CamY = objy[earthN];
var CamZ = objz[earthN];

var moonN = 11;
var ux = CamX - objx[1];
var uy = CamY - objy[1];
var uz = CamZ - objz[1];
var dES = Math.sqrt(ux*ux + uy*uy + uz*uz);

var vx = CamX - objx[moonN];
var vy = CamY - objy[moonN];
var vz = CamZ - objz[moonN];

var dot = ux*vx +uy*vy + uz*vz;
var magU = Math.sqrt(ux*ux + uy*uy + uz*uz);
var magV = Math.sqrt(vx*vx + vy*vy + vz*vz);

var theta = 180*Math.acos(dot / (magU * magV)) / Math.PI;

pContextA.fillStyle = '#FFFF00';
pContextA.font="20px Verdana";
var x = 65, y = 220;
pContextA.clearRect(x,y-30,500,50);
pContextA.fillText(theta.toFixed(3) + " degrees", x, y); 
 


Using the Quick Help box allows you to copy the text, while the other method prints as a graphic, so you can't copy it.
 
==================================
For orbital elements, use the function ComputeOrbitalElements(x,y)
This gives you the elements for x with respect to y.
For example, Earth is object 4, and the Sun is object 1. So to get Earth's orbital elements with respect to the Sun you would call ComputeOrbitalElements(4,1);
After calling this function, the elements are available in the following variables:
oeSMA, oeEcc, oeInc, oeLAN, oePeri, oeMA.
 
Here is an example that displays Earth's Mean Anomaly and eccentricity in the Quick Help box:
Code:
var earthN = 4, sunN = 1;
var LF = String.fromCharCode(10); // line feed
ComputeOrbitalElements(earthN, sunN);
txtAreaQuickHelp.value = "MA: " + (180 * oeMA/ Math.PI).toFixed(3) + " degrees" + LF + "ecc: " + oeEcc.toFixed(10);
 


 
To pause on a certain condition use the If statement, call the pause() function, then delete the pause function from the autopilot so you can manually unpause. Here is the previous example, except that it pauses when Earth's Mean Anomoly exceeds 300 degrees.
 
Code:
var earthN = 4, sunN = 1;
var LF = String.fromCharCode(10); // line feed
ComputeOrbitalElements(earthN, sunN);
txtAreaQuickHelp.value = "MA: " + (180 * oeMA/ Math.PI).toFixed(3) + " degrees" + LF + "ecc: " + oeEcc.toFixed(10);

if (180 * oeMA / Math.PI > 300) {
pause();
txtAreaAPperGraphicUpdate.value = txtAreaAPperGraphicUpdate.value.replace("pause()", "");
updateAPperGraphicUpdate();
}
 


or if you are in Autopilot Per iteration you would use txtAreaAPperIteration and updateAPperIteration()
 
State vectors are also available to you as objx[n], objy[n], objz[n], objvx[n], objvy[n], objvz[n]
 
Or rather than pausing every time the Moon's elongation reached 180, you could have it tally the moments in the Quick Help box. This way you could run the simulation slowly at time step = 1 second, so you wouldn't overshoot the desired event between time steps. Your output statements would then look like:
Code:
txtAreaQuickHelp.value = txtAreaQuickHelp.value + ...
 


To help you keep track of inflection, the variables apNV1, apNV2, apNV3, and apNV4 are available for your global use in the autopilot.
 
State vectors are also available to you as objx[n], objy[n], objz[n], objvx[n], objvy[n], objvz[n].
elapsedTime tells you how much time elapsed since the beginning of the simulation. To convert it to a calendar date:
 
Code:
 var x = UTC(elapsedTime * 1000 + startDate); // UTC calendar
 var x = (elapsedTime * 1000 + startDate); // computer local calendar
 var x = elapsedTime / 86400 + julianStartDate / 86400; // julian day
 


 
Additionally, if you would like your vectors with respect to a city on Earth, rather than geocenter, menu Display > Cities A, choose a city or enter lat lon, and replace the CamX, CamY, and CamZ lines above with
 
Code:
var CamX = objx[fromObjA] + dCore * Math.cos(dElta) * Math.cos(aLpha);
var CamY = objy[fromObjA] + dCore * Math.cos(dElta) * Math.sin(aLpha);
var CamZ = objz[fromObjA] - dCore * Math.sin(dElta);
 


 
 
Back to top
 
 
Email View Profile WWW   IP Logged
jwbrase
Uploader



I Love YaBB 2!

Posts: 5
Re: Orbital elements
Reply #3 - 09/03/17 at 18:45:31
 
Quote from Tony on 09/01/17 at 18:00:00:

To help you keep track of inflection, the variables apNV1, apNV2, apNV3, and apNV4 are available for your global use in the autopilot.

 
Could you elaborate on this a bit?
 
Also,  is there documentation of avaliable functions and variables anywhere?
Back to top
 
 
View Profile   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #4 - 09/04/17 at 11:50:53
 
Any variables created in the auto pilot are local variables and get destroyed after each iteration of the autopilot block of code.
If you want to use a variable to store any information between iterations, you must use a global variable.
You also must avoid over-writing any global variables that the program uses for other purposes (unless that is your intent).
 
apNV1, apNV2, apNV3 and apNV4 are global variables that the main program does not use. So you are free to use them.
I also just added APA[] as an unused global array. So you can use APA[0] =..., APA[1] = ... as many as you need.
 
For example, if ecc was slowly rising, reaches a max and then slowly decreases, and you wanted to find the max, you need to store the old value of oeEcc to compare it to the new value. You also need to store whether or not it is rising or falling. In the following example, apNV2 = 1 if oeEcc is rising, and =0 if it is falling. apNV1 stores the old value of oeEcc so you can compare it and see if it is larger or smaller.
 
The following example records the min and max values for the Moon's eccentricity around Earth.
You can pull the bottom right corner of the Quick Help box to expand its size and make it more readable.
It also uses the Simulation Description box (availiable in the Help menu) to show you the values of apNV1 & apNV2 at each iteration. It also uses the document title (name of the browser tab) to display the current eccentricity.
First, you must initialize apNV1 and apNV2. Open the AutoPilot Do Now box. Enter:
Code:
apNV1 = 0;
apNV2 = 0;
 


and press Update
 
Then open the Auto Pilot per Iteration box and paste the following:
Code:
ComputeOrbitalElements(11,4);
document.title = oeEcc;
txtAreaSimulationDescription.value = apNV1 + LF + apNV2;

//begins rising after falling
if (oeEcc > apNV1 && apNV2 == 0) {
  apNV2 = 1;
  txtAreaQuickHelp.value = txtAreaQuickHelp.value + "Min: " + oeEcc + " " + UTC(elapsedTime * 1000 + startDate) + LF;
}

//begins falling after rising
else if (oeEcc < apNV1 && apNV2 == 1) {
  apNV2 = 0;
  txtAreaQuickHelp.value = txtAreaQuickHelp.value + "Max: " + oeEcc + " " + UTC(elapsedTime * 1000 + startDate) + LF;
}
apNV1 = oeEcc;

 


Press Update, unpause, sit back and watch the data flow in.
Back to top
 
 
Email View Profile WWW   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #5 - 09/04/17 at 11:53:12
 
There is a thread in the "sticky" threads on the top of the forum called "Auto Pilot in the browser version" that documents a lot of what you can do in the auto pilot.
You can also press ctrl + U on your browser to see the source code of the program. Search for the word "function" to see all the functions.
Back to top
 
 
Email View Profile WWW   IP Logged
jwbrase
Uploader



I Love YaBB 2!

Posts: 5
Re: Orbital elements
Reply #6 - 09/05/17 at 01:17:55
 
Quote from Tony on 09/04/17 at 11:50:53:


apNV1, apNV2, apNV3 and apNV4 are global variables that the main program does not use. So you are free to use them.
I also just added APA[] as an unused global array. So you can use APA[0] =..., APA[1] = ... as many as you need.


 
OK, I thought that was the case, just wanted to make sure, it wasn't entirely obvious from your initial statement. I'm glad you have the array, though, I was wondering how I was going to handle things with just four variables.
 
Just for syntactic sugar (I can do without something like this) would it be possible to allow the user to create an "on_load" section in Single Events to allow for user-named persistent variables, for example:
 
Code:
on_load {{
var foo = 1;
var bar = 2;
var baz = function(parameter);}} 


 
With the user then being able in per-iteration or per graphic update code to access them something like this:
 
Code:
APvars.foo = function(APvars.foo, APvars.bar, APvars.baz); 

Back to top
 
 
View Profile   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #7 - 09/06/17 at 07:30:18
 
I could probably add an "on load", but for the moment, you can just set the single event timer to 1 second after the start of your sim.
But any variables you define that way will be only local and temporary. I think there is a way to define global variables from within a function. I'll have to Google it to figure it out.
Back to top
 
 
Email View Profile WWW   IP Logged
Tony
YaBB Administrator
*****




Posts: 1051
Gender: male
Re: Orbital elements
Reply #8 - 09/08/17 at 08:26:04
 
http://orbitsimulator.com/gsim.html
 
I added on_load to the Autopilot Single Events.
It will execute the code in between {{  }} the moment the update button is pressed, just like the Autopilot Now, if you saved your sim, the moment it loads.
 
You can use
on_load {{ }}
onload {{ }}
onLoad {{ }}
now {{ }}
They all do the same thing.
I also just learned that you can define global variables from within the auto pilots by simply assigning them a value without using a var statement. For example:
 
var helloWorld = 7;
would create a local variable that will not be saved after the current iteration is executed. It can not be shared between auto pilots.
 
helloWorld = 7;
will create a global variable.
 
Give it a test and let me know if it works for you.
Back to top
 
 
Email View Profile WWW   IP Logged
Pages: 1
Send Topic Print