09/24/18 at 07:37:20 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.

 Pages: 1
 Tony YaBB Administrator Posts: 1057 Gender: 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 IP Logged
 Tony YaBB Administrator Posts: 1057 Gender: 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 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 IP Logged