mirror of
https://github.com/christiaangoossens/Planetary-Orbit-Simulator
synced 2025-07-03 18:10:47 +00:00
Cleanup and comments
This commit is contained in:
@ -16,7 +16,7 @@ public class Main {
|
||||
* The MIT License (MIT)
|
||||
* Copyright (c) 2016 Christiaan Goossens (Verictas) & Daniel Boutros
|
||||
*
|
||||
* The full license is included in the git respository as LICENSE.md
|
||||
* The full license is included in the git repository as LICENSE.md
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -25,9 +25,6 @@ public class Object {
|
||||
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);
|
||||
}
|
||||
|
@ -13,13 +13,16 @@ public class Simulator {
|
||||
*/
|
||||
|
||||
int rounds = SimulatorConfig.rounds;
|
||||
double time = SimulatorConfig.time;
|
||||
|
||||
/**
|
||||
* Log a debug message to the console to signal the simulation has started
|
||||
*/
|
||||
System.out.println("========== Simulation Started ==========\n");
|
||||
|
||||
// Create a timer
|
||||
/**
|
||||
* Create a time to measure runtime
|
||||
*/
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
@ -52,10 +55,6 @@ public class Simulator {
|
||||
* 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);
|
||||
@ -64,15 +63,14 @@ public class Simulator {
|
||||
|
||||
accelerate(objects, matrix);
|
||||
|
||||
/**
|
||||
* Print the matrix for this round
|
||||
*/
|
||||
|
||||
for(int i = 0; i < objects.length; i++) {
|
||||
objects[i].updateSpeed(time);
|
||||
writer.write(objects[i].name, objects[i].position, objects[i].speed, objects[i].oldAcceleration, objects[i].acceleration, objects[i].mass);
|
||||
}
|
||||
|
||||
/**
|
||||
* The round has ended
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,7 +79,9 @@ public class Simulator {
|
||||
writer.save();
|
||||
System.out.println("========== Simulation Finished ==========");
|
||||
|
||||
// Display time info
|
||||
/**
|
||||
* Display information about the program runtime
|
||||
*/
|
||||
long stopTime = System.currentTimeMillis();
|
||||
System.out.println("Simulation took: " + (stopTime - startTime) + "ms");
|
||||
} catch(WritingException e) {
|
||||
|
@ -13,25 +13,39 @@ import java.util.Date;
|
||||
public class DataWriter {
|
||||
private FileWriter writer = null;
|
||||
|
||||
// Delimiter used in text file (for import in Excel)
|
||||
/**
|
||||
* Set global variables, such as the delimiter and the new line character
|
||||
*/
|
||||
private static final String DELIMITER = "\t";
|
||||
private static final String NEW_LINE = "\n";
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @throws WritingException
|
||||
*/
|
||||
public DataWriter() throws WritingException {
|
||||
try {
|
||||
// Define the save path
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Check if the saving directory exists to prevent IOException
|
||||
*/
|
||||
if (!directoryPath.exists()) {
|
||||
directoryPath.mkdirs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a file to write to and write the header
|
||||
*/
|
||||
this.writer = new FileWriter(path);
|
||||
this.writer.write("Object" + DELIMITER + "Position (m)" + DELIMITER + "Position (AU)" + DELIMITER + "Speed (m/s)" + DELIMITER + "Speed (AU/day)" + DELIMITER + "Old Acceleration" + DELIMITER + "Acceleration" + DELIMITER + "Mass" + NEW_LINE);
|
||||
this.counter++;
|
||||
@ -42,6 +56,11 @@ public class DataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string to the file
|
||||
* @param string
|
||||
* @throws WritingException
|
||||
*/
|
||||
public void write(String string) throws WritingException {
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
@ -58,6 +77,16 @@ public class DataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes some data about the current object to the file
|
||||
* @param id String
|
||||
* @param position Vector3d
|
||||
* @param speed Vector3d
|
||||
* @param oldAcceleration Vector3d
|
||||
* @param acceleration Vector3d
|
||||
* @param mass double
|
||||
* @throws WritingException
|
||||
*/
|
||||
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");
|
||||
@ -74,6 +103,10 @@ public class DataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the file to disk
|
||||
* @throws WritingException
|
||||
*/
|
||||
public void save() throws WritingException {
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
@ -87,11 +120,19 @@ public class DataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current line count
|
||||
* @return int
|
||||
*/
|
||||
public int getLines() {
|
||||
return this.counter;
|
||||
}
|
||||
|
||||
public String getCurrentTimeStamp() {
|
||||
/**
|
||||
* Gets the current filestamp for file naming
|
||||
* @return String
|
||||
*/
|
||||
private String getCurrentTimeStamp() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,12 @@ public class AU {
|
||||
/**
|
||||
* Helper class for working with astronomical units
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts AU to meters
|
||||
* @param input Vector3d with values in AU
|
||||
* @return Vector3d with values in meter
|
||||
*/
|
||||
public static Vector3d convertToMeter(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
@ -16,6 +22,11 @@ public class AU {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts AU/day to m/s
|
||||
* @param input Vector3d with values in AU/day
|
||||
* @return Vector3d with values in m/s
|
||||
*/
|
||||
public static Vector3d convertToMetersPerSecond(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
@ -25,6 +36,11 @@ public class AU {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts meters to AU for data collection
|
||||
* @param input Vector3d with values in meters
|
||||
* @return Vector3d with values in AU
|
||||
*/
|
||||
public static Vector3d convertFromMeter(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
@ -34,6 +50,11 @@ public class AU {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts m/s to AU/day for data collection
|
||||
* @param input Vector3d with values in m/s
|
||||
* @return Vector3d with values in AU/day
|
||||
*/
|
||||
public static Vector3d convertFromMetersPerSecond(Vector3d input) {
|
||||
Vector3d output = new Vector3d(input);
|
||||
|
||||
|
Reference in New Issue
Block a user