1
0
mirror of https://github.com/christiaangoossens/Planetary-Orbit-Simulator synced 2025-07-02 01:40:47 +00:00

Leap frog integration

This commit is contained in:
Christiaan Goossens
2016-09-15 09:11:59 +02:00
parent 44b6f8c40d
commit 5adf08abe0
14 changed files with 590 additions and 139 deletions

View File

@ -21,21 +21,18 @@ public class Main {
/**
* Object definitions
*/
Object object1 = new Object(1000, new Vector3d(1,2,3), new Vector3d(0,4,3));
Object object2 = new Object(200, new Vector3d(2,38,2), new Vector3d(3,4,5));
Object object3 = new Object(200, new Vector3d(2,-20,2), new Vector3d(3,4,5));
Object object4 = new Object(200, new Vector3d(2,4,2), new Vector3d(3,4,5));
Object object1 = new Object(1/6.67384E-11, new Vector3d(1,0,0), new Vector3d(0,0,0));
Object object2 = new Object(1, new Vector3d(0,0,0), new Vector3d(0,0,0));
/**
* Object listing
*/
Object[] objects = {object1, object2, object3, object4};
Object[] objects = {object1, object2};
/**
* Run the simulator for the specified amount of rounds
* Run the simulator for the specified objects
*/
int rounds = 2;
Simulator.run(rounds, objects);
Simulator.run(objects);
}
}

View File

@ -6,6 +6,10 @@ public class Object {
public double mass;
public Vector3d position;
public Vector3d speed;
public Vector3d acceleration;
public Vector3d oldAcceleration;
private double gravitationalConstant = 6.67384E-11;
/**
@ -27,6 +31,19 @@ public class Object {
public void setSpeed(Vector3d speed) {
this.speed = speed;
}
public void setSpeed(double[] speed) {
this.speed = new Vector3d(speed[0], speed[1], speed[2]);
}
/**
* Gets the speed into a double[3]
* @return double[3]
*/
public double[] getSpeed() {
double[] v = new double[3];
this.speed.get(v);
return v;
}
/**
* Sets the position vector of an object.
@ -35,6 +52,57 @@ public class Object {
public void setPosition(Vector3d position) {
this.position = position;
}
public void setPosition(double[] position) {
this.position = new Vector3d(position[0], position[1], position[2]);
}
/**
* Gets the position into a double[3]
* @return double[3]
*/
public double[] getPosition() {
double[] r = new double[3];
this.position.get(r);
return r;
}
/**
* Sets the acceleration vector of an object
* @param acceleration Current acceleration vector
*/
public void setAcceleration(Vector3d acceleration) { this.acceleration = acceleration; }
public void setAcceleration(double[] acceleration) {
this.acceleration = new Vector3d(acceleration[0], acceleration[1], acceleration[2]);
}
/**
* Gets the acceleration into a double[3]
* @return double[3]
*/
public double[] getAcceleration() {
double[] a = new double[3];
this.acceleration.get(a);
return a;
}
/**
* Sets the acceleration vector of an object
* @param acceleration Current acceleration vector
*/
public void setOldAcceleration(Vector3d acceleration) { this.acceleration = acceleration; }
public void setOldAcceleration(double[] acceleration) {
this.oldAcceleration = new Vector3d(acceleration[0], acceleration[1], acceleration[2]);
}
/**
* Gets the acceleration into a double[3]
* @return double[3]
*/
public double[] getOldAcceleration() {
double[] a = new double[3];
this.oldAcceleration.get(a);
return a;
}
/**
* Changes an object into readable form
@ -61,9 +129,67 @@ public class Object {
* @param secondObject The passed object.
* @return Vector3d The distance vector
*/
private Vector3d getDistance(Object secondObject) {
public Vector3d getDistance(Object secondObject) {
Vector3d distance = new Vector3d(0,0,0); // Empty
distance.sub(this.position, secondObject.position);
return distance;
}
/**
* Updates the position based on dt
* @param dt The difference in time
*/
public void updatePosition(double dt) {
// Write the vectors to double[3]
double[] r = this.getPosition();
double[] v = this.getSpeed();
double[] a = this.getAcceleration();
for (int i = 0; i != 3; i++){
double dt2 = dt * dt;
r[i] += v[i] * dt + 0.5 * a[i] * dt2;
}
// Write the doubles into the vectors to save them
setPosition(r);
setSpeed(v);
setAcceleration(a);
}
/**
* Updates the speed based on dt
* @param dt The difference in speed
*/
public void updateSpeed(double dt) {
// Write the vectors to double[3]
double[] v = this.getSpeed();
double[] a = this.getAcceleration();
double[] aold = this.getOldAcceleration();
for (int i = 0; i != 3; i++){
v[i] += 0.5 * dt *(a[i] + aold[i]);
}
setSpeed(v);
setAcceleration(a);
setOldAcceleration(aold);
}
/**
* Updates the acceleration based on dt
*/
public void updateAcceleration() {
this.oldAcceleration = this.acceleration;
}
/**
* Enacts a certain force on the object
* @param force The force in N.
*/
public void enactForceOnObject(Vector3d force) {
double factor = 1/this.mass;
Vector3d acceleration = force;
acceleration.scale(factor);
this.acceleration = acceleration;
}
}

View File

@ -3,57 +3,56 @@ import javax.vecmath.*;
import com.verictas.pos.simulator.mathUtils.Vector3dMatrix;
public class Simulator {
public static void run(int rounds, Object[] objects) {
public static void run(Object[] objects) {
/**
* Get variables from the config
*/
int rounds = SimulatorConfig.rounds;
/**
* Log a debug message to the console to signal the simulation has started
*/
System.out.println("========== Simulation Started ==========\n");
/**
* Define the forces matrix for this round
*/
Vector3dMatrix matrix = new Vector3dMatrix(objects.length,objects.length);
accelerate(objects, matrix);
/**
* Start the rounds loop
*/
for(int t = 0; t < rounds; t++) {
for(int t = 0; t != rounds; t++) {
/**
* The round has started
*/
System.out.println("Round " + (t + 1) + " started!");
System.out.println("\nRound " + (t + 1) + " started!");
/**
* Define the forces matrix for this round
* Define the initial values
*/
Vector3dMatrix matrix = new Vector3dMatrix(objects.length,objects.length);
double time = SimulatorConfig.time;
for(int i = 0; i < objects.length; i++) {
/**
* For every object: calculate the force upon it.
*/
for (int o = 0; o < objects.length; o++) {
/**
* Loop through all other objects
*/
if (o == i) {
break;
}
Vector3d force = objects[i].getForceOnObject(objects[o]);
matrix.setPosition(force, i, o);
System.out.println("Force " + (i + 1) + " on " + (o + 1) + " - " + force);
/**
* Also put in the opposite force
*/
force.scale(-1);
matrix.setPosition(force, o, i);
System.out.println("Force " + (o + 1) + " on " + (i + 1) + " - " + force);
}
System.out.println("Object " + (i+1) + " was at " + objects[i].position);
objects[i].updatePosition(time);
objects[i].updateAcceleration();
System.out.println("Object " + (i+1) + " is at " + objects[i].position);
}
accelerate(objects, matrix);
/**
* Print the matrix for this round
*/
System.out.println("\n");
System.out.println(matrix);
for(int i = 0; i < objects.length; i++) {
objects[i].updateSpeed(time);
}
}
/**
@ -61,4 +60,49 @@ public class Simulator {
*/
System.out.println("========== Simulation Finished ==========");
}
public static void accelerate(Object[] objects, Vector3dMatrix matrix) {
// Loop
for(int i = 0; i < objects.length; i++) {
/**
* For every object: calculate the force upon it.
*/
// Reset acceleration
objects[i].setAcceleration(new Vector3d(0, 0, 0));
for (int o = 0; o < objects.length; o++) {
/**
* Loop through all other objects
*/
if (o == i) {
break;
}
Vector3d force = objects[i].getForceOnObject(objects[o]);
matrix.setPosition(force, i, o);
System.out.println("Force " + (i + 1) + " on " + (o + 1) + " - " + force);
/**
* Also put in the opposite force
*/
force.scale(-1);
matrix.setPosition(force, o, i);
System.out.println("Force " + (o + 1) + " on " + (i + 1) + " - " + force);
}
}
System.out.println("\n");
System.out.println(matrix);
for(int i = 0; i < objects.length; i++) {
/**
* Progress forces on the object
*/
Vector3d forceOnI = matrix.getColumnTotal(i);
System.out.println("All forces on " + (i + 1) + " - " + forceOnI);
objects[i].enactForceOnObject(forceOnI);
}
}
}

View File

@ -0,0 +1,6 @@
package com.verictas.pos.simulator;
public class SimulatorConfig {
public static int rounds = 2;
public static double time = 1;
}

View File

@ -113,7 +113,6 @@ public class Vector3dMatrix extends GMatrix {
resultVector.add(new Vector3d(x, y, z));
}
System.out.println(resultVector);
return resultVector;
}
@ -142,7 +141,6 @@ public class Vector3dMatrix extends GMatrix {
resultVector.add(new Vector3d(x, y, z));
}
System.out.println(resultVector);
return resultVector;
}
}

View File

@ -0,0 +1,4 @@
package com.verictas.pos.simulator;
public class nBabel {
}