mirror of
https://github.com/christiaangoossens/Planetary-Orbit-Simulator
synced 2025-07-03 18:10:47 +00:00
Finished writing the leap frog system for calculating orbits
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package com.verictas.pos.simulator;
|
||||
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
|
||||
import javax.vecmath.*;
|
||||
|
||||
public class Main {
|
||||
@ -19,16 +21,19 @@ public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/**
|
||||
* Object definitions
|
||||
* Object definitions (in 1990)
|
||||
*/
|
||||
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 sun = new Object("Sun", 1.988544E30, AU.convertToMeter(new Vector3d(3.621484938699030E-03,3.203347049968909E-03,-1.609087138389905E-04)), AU.convertToMetersPerSecond(new Vector3d(-1.730306264794065E-06,6.909301960615850E-06,3.332250766613383E-08)));
|
||||
Object venus = new Object("Venus", 48.685E23, AU.convertToMeter(new Vector3d(-3.786926829662159E-01,-6.122709221027441E-01,1.346180701578967E-02)), AU.convertToMetersPerSecond(new Vector3d(1.703979708314098E-02,-1.075790617185284E-02,-1.130972411646143E-03)));
|
||||
Object earth = new Object("Earth", 5.97219E24, AU.convertToMeter(new Vector3d(1.000272608326749E+00,-1.305632418724720E-01,-1.614384880329670E-04)), AU.convertToMetersPerSecond(new Vector3d(2.003180730888720E-03,1.698793770993201E-02,5.869001824818362E-08)));
|
||||
Object mars = new Object("Mars", 6.4185E23, AU.convertToMeter(new Vector3d(8.638055532014732E-01,-1.094520306989018E+00,-4.427515002554464E-02)), AU.convertToMetersPerSecond(new Vector3d(1.154235320339802E-02,9.839355267552327E-03,-7.723750026136471E-05)));
|
||||
Object jupiter = new Object("Jupiter", 1898.13E24, AU.convertToMeter(new Vector3d(-5.440309619306835E+00,-2.383659935837559E-01,1.226571001615609E-01)), AU.convertToMetersPerSecond(new Vector3d(2.422143907277735E-04,-7.182284468246539E-03,2.440789748210396E-05)));
|
||||
|
||||
/**
|
||||
* Object listing
|
||||
*/
|
||||
|
||||
Object[] objects = {object1, object2};
|
||||
Object[] objects = {sun, venus, earth, mars, jupiter};
|
||||
|
||||
/**
|
||||
* Run the simulator for the specified objects
|
||||
|
@ -10,6 +10,8 @@ public class Object {
|
||||
public Vector3d acceleration;
|
||||
public Vector3d oldAcceleration;
|
||||
|
||||
public String name;
|
||||
|
||||
private double gravitationalConstant = 6.67384E-11;
|
||||
|
||||
/**
|
||||
@ -18,10 +20,16 @@ public class Object {
|
||||
* @param position The position vector of the object
|
||||
* @param speed The speed vector of the object
|
||||
*/
|
||||
public Object(double mass, Vector3d position, Vector3d speed) {
|
||||
public Object(String name, double mass, Vector3d position, Vector3d speed) {
|
||||
this.name = name;
|
||||
this.mass = mass;
|
||||
this.position = position;
|
||||
this.speed = speed;
|
||||
|
||||
System.out.println(speed);
|
||||
|
||||
this.oldAcceleration = new Vector3d(0,0,0);
|
||||
this.acceleration = new Vector3d(0,0,0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,9 @@
|
||||
package com.verictas.pos.simulator;
|
||||
import javax.vecmath.*;
|
||||
|
||||
import com.verictas.pos.simulator.dataWriter.DataWriter;
|
||||
import com.verictas.pos.simulator.dataWriter.WritingException;
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
import com.verictas.pos.simulator.mathUtils.Vector3dMatrix;
|
||||
|
||||
public class Simulator {
|
||||
@ -16,49 +20,73 @@ public class Simulator {
|
||||
*/
|
||||
System.out.println("========== Simulation Started ==========\n");
|
||||
|
||||
// Create a timer
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* Define the forces matrix for this round
|
||||
* Define the forces matrix and the DataWriter
|
||||
*/
|
||||
Vector3dMatrix matrix = new Vector3dMatrix(objects.length,objects.length);
|
||||
|
||||
accelerate(objects, matrix);
|
||||
try {
|
||||
DataWriter writer = new DataWriter();
|
||||
|
||||
/**
|
||||
* Start the rounds loop
|
||||
*/
|
||||
for(int t = 0; t != rounds; t++) {
|
||||
/**
|
||||
* The round has started
|
||||
* Write begin values
|
||||
*/
|
||||
System.out.println("\nRound " + (t + 1) + " started!");
|
||||
/**
|
||||
* Define the initial values
|
||||
*/
|
||||
double time = SimulatorConfig.time;
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
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);
|
||||
writer.write(objects[i].name, objects[i].position, objects[i].speed, objects[i].oldAcceleration, objects[i].acceleration, objects[i].mass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the leap frog integration!
|
||||
*/
|
||||
|
||||
accelerate(objects, matrix);
|
||||
|
||||
/**
|
||||
* Print the matrix for this round
|
||||
* Start the rounds loop
|
||||
*/
|
||||
for(int t = 0; t != rounds; t++) {
|
||||
/**
|
||||
* The round has started
|
||||
*/
|
||||
System.out.println("\nRound " + (t + 1) + " started!");
|
||||
/**
|
||||
* Define the initial values
|
||||
*/
|
||||
double time = SimulatorConfig.time;
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
objects[i].updatePosition(time);
|
||||
objects[i].updateAcceleration();
|
||||
}
|
||||
|
||||
accelerate(objects, matrix);
|
||||
|
||||
/**
|
||||
* Print the matrix for this round
|
||||
*/
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
writer.write(objects[i].name, objects[i].position, objects[i].speed, objects[i].oldAcceleration, objects[i].acceleration, objects[i].mass);
|
||||
}
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
objects[i].updateSpeed(time);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Log that the simulation has finished and save info to file
|
||||
*/
|
||||
writer.save();
|
||||
System.out.println("========== Simulation Finished ==========");
|
||||
|
||||
/**
|
||||
* Log that the simulation has finished
|
||||
*/
|
||||
System.out.println("========== Simulation Finished ==========");
|
||||
// Display time info
|
||||
long stopTime = System.currentTimeMillis();
|
||||
System.out.println("Simulation took: " + (stopTime - startTime) + "ms");
|
||||
} catch(WritingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void accelerate(Object[] objects, Vector3dMatrix matrix) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.verictas.pos.simulator;
|
||||
|
||||
public class SimulatorConfig {
|
||||
public static int rounds = 2;
|
||||
public static double time = 1;
|
||||
public static int rounds = 60 * 60 * 24 * 10;
|
||||
public static double time = 0.1; // in seconds
|
||||
}
|
||||
|
@ -0,0 +1,82 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
|
||||
import javax.vecmath.Vector3d;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DataWriter {
|
||||
private FileWriter writer = null;
|
||||
|
||||
// Delimiter used in text file (for import in Excel)
|
||||
private static final String DELIMITER = "\t";
|
||||
private static final String NEW_LINE = "\n";
|
||||
|
||||
public DataWriter() throws WritingException {
|
||||
try {
|
||||
// Define the save path
|
||||
String directory = System.getProperty("user.home") + File.separator + "simulatorExports";
|
||||
File directoryPath = new File(directory);
|
||||
|
||||
String path = directory + File.separator + getCurrentTimeStamp() + ".txt";
|
||||
System.out.println("WRITING DATA TO: " + path);
|
||||
|
||||
if (!directoryPath.exists()) {
|
||||
directoryPath.mkdirs();
|
||||
}
|
||||
|
||||
this.writer = new FileWriter(path);
|
||||
this.writer.write("Object" + DELIMITER + "Position (m)" + DELIMITER + "Position (AU)" + DELIMITER + "Speed (m)" + DELIMITER + "Speed (AU)" + DELIMITER + "Old Acceleration" + DELIMITER + "Acceleration" + DELIMITER + "Mass" + NEW_LINE);
|
||||
} catch(IOException e) {
|
||||
throw new WritingException("Whoop! Creation error!");
|
||||
} catch(Exception e) {
|
||||
throw new WritingException("Whoop! Unknown creation error!");
|
||||
}
|
||||
}
|
||||
|
||||
public void write(String string) throws WritingException {
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
this.writer.append(string);
|
||||
} catch (Exception e) {
|
||||
throw new WritingException("Whoop! Write error!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void write(String id, Vector3d position, Vector3d speed, Vector3d oldAcceleration, Vector3d acceleration, double mass) throws WritingException {
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
this.writer.append(id + DELIMITER + position.toString() + DELIMITER + AU.convertFromMeter(position).toString() + DELIMITER + speed.toString() + DELIMITER + AU.convertFromMetersPerSecond(speed).toString() + DELIMITER + oldAcceleration.toString() + DELIMITER + acceleration.toString() + DELIMITER + String.valueOf(mass) + NEW_LINE);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("Whoop! Write error!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() throws WritingException {
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
this.writer.flush();
|
||||
this.writer.close();
|
||||
} catch (IOException e) {
|
||||
throw new WritingException("Whoop! Save error!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getCurrentTimeStamp() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
public class WritingException extends Exception {
|
||||
public WritingException() { super(); }
|
||||
public WritingException(String message) { super(message); }
|
||||
public WritingException(String message, Throwable cause) { super(message, cause); }
|
||||
public WritingException(Throwable cause) { super(cause); }
|
||||
}
|
45
simulator/src/com/verictas/pos/simulator/mathUtils/AU.java
Normal file
45
simulator/src/com/verictas/pos/simulator/mathUtils/AU.java
Normal file
@ -0,0 +1,45 @@
|
||||
package com.verictas.pos.simulator.mathUtils;
|
||||
|
||||
import javax.vecmath.Vector3d;
|
||||
|
||||
public class AU {
|
||||
/**
|
||||
* Helper class for working with astronomical units
|
||||
*/
|
||||
public static Vector3d convertToMeter(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
// Convert AU to m by NASA
|
||||
output.scale(149597870.700); // Number to large when multiplied with 1000
|
||||
output.scale(1000);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Vector3d convertToMetersPerSecond(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
// 1 AU/day to M/s
|
||||
output.scale(1731456.84);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Vector3d convertFromMeter(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
// Convert m to AU by NASA
|
||||
output.scale(6.6845871E-12);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public static Vector3d convertFromMetersPerSecond(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
// Convert seconds to days by NASA
|
||||
output.scale(5.77548327E-7);
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.verictas.pos.simulator;
|
||||
|
||||
public class nBabel {
|
||||
}
|
Reference in New Issue
Block a user