mirror of
https://github.com/christiaangoossens/Planetary-Orbit-Simulator
synced 2025-07-02 09:40:48 +00:00
Added logging system from v2 to v1
This commit is contained in:
@ -19,6 +19,8 @@ public class Main {
|
||||
* The full license is included in the git repository as LICENSE.md
|
||||
*/
|
||||
|
||||
public static int version = 1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
/**
|
||||
* Object definitions
|
||||
|
@ -23,7 +23,7 @@ public class SimulatorConfig {
|
||||
* Time settings
|
||||
*/
|
||||
|
||||
public static int rounds = 1051896*500; // Amount of rounds to run the simulator for // 3000000 = 250.000 jaar
|
||||
public static int rounds = 1051896*50; // Amount of rounds to run the simulator for // 3000000 = 250.000 jaar
|
||||
public static double time = 30; // Time steps in seconds // 2592000 = 1 month
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,35 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AOPDataWriter extends DataWriter {
|
||||
public AOPDataWriter() throws WritingException {
|
||||
super("arguments");
|
||||
try {
|
||||
|
||||
/**
|
||||
* Write the lines with information about the columns
|
||||
*/
|
||||
|
||||
this.writer.write("OBJECT" + DELIMITER + "ROUND" + DELIMITER + "ARGUMENT (RAD)" + NEW_LINE);
|
||||
this.counter++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("An error occurred while writing to the file!");
|
||||
}
|
||||
}
|
||||
|
||||
public void write(String object, TreeMap<Integer, Double> arguments) throws WritingException {
|
||||
try {
|
||||
for (Map.Entry<Integer, Double> entry : arguments.entrySet()) {
|
||||
Integer key = entry.getKey();
|
||||
Double value = entry.getValue();
|
||||
this.writer.append(object + DELIMITER + key + DELIMITER + decimalFormatter(value) + NEW_LINE);
|
||||
this.counter++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("An error occurred while writing to the file!");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,30 +1,24 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
import com.verictas.pos.simulator.Object;
|
||||
import com.verictas.pos.simulator.Simulator;
|
||||
import com.verictas.pos.simulator.Main;
|
||||
import com.verictas.pos.simulator.SimulatorConfig;
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
import com.verictas.pos.simulator.processor.ObjectProcessor;
|
||||
|
||||
import javax.vecmath.Vector3d;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.*;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class DataWriter {
|
||||
private FileWriter writer = null;
|
||||
protected FileWriter writer = null;
|
||||
|
||||
/**
|
||||
* 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";
|
||||
protected static final String DELIMITER = "\t";
|
||||
protected static final String NEW_LINE = "\n";
|
||||
|
||||
private int counter = 0;
|
||||
protected int counter = 0;
|
||||
|
||||
/**
|
||||
* Decimal formatter
|
||||
@ -36,7 +30,7 @@ public class DataWriter {
|
||||
* Constructor
|
||||
* @throws WritingException
|
||||
*/
|
||||
public DataWriter() throws WritingException {
|
||||
public DataWriter(String filenameAppendix) throws WritingException {
|
||||
|
||||
/**
|
||||
* Prepare the locale
|
||||
@ -49,7 +43,7 @@ public class DataWriter {
|
||||
String directory = System.getProperty("user.home") + File.separator + "simulatorExports";
|
||||
File directoryPath = new File(directory);
|
||||
|
||||
String path = directory + File.separator + getCurrentTimeStamp() + ".txt";
|
||||
String path = directory + File.separator + "v" + Main.version + "-" + getCurrentTimeStamp() + "-" + filenameAppendix + ".txt";
|
||||
System.out.println("WRITING DATA TO: " + path);
|
||||
|
||||
/**
|
||||
@ -64,18 +58,6 @@ public class DataWriter {
|
||||
*/
|
||||
this.writer = new FileWriter(path);
|
||||
|
||||
/**
|
||||
* Write the lines with information about the columns
|
||||
*/
|
||||
|
||||
if (SimulatorConfig.outputUnit.equals("AU")) {
|
||||
this.writer.write("Object" + DELIMITER + "X (AU)" + DELIMITER + "Y (AU)" + DELIMITER + "Z (AU)" + DELIMITER + "VX (AU/day)" + DELIMITER + "VY (AU/day)" + DELIMITER + "VZ (AU/day)" + NEW_LINE);
|
||||
} else {
|
||||
this.writer.write("Object" + DELIMITER + "X (m)" + DELIMITER + "Y (m)" + DELIMITER + "Z (m)" + DELIMITER + "VX (m/s)" + DELIMITER + "VY (m/s)" + DELIMITER + "VZ (m/s)" + NEW_LINE);
|
||||
}
|
||||
|
||||
this.counter++;
|
||||
|
||||
/**
|
||||
* Configure the decimal formatter
|
||||
*/
|
||||
@ -118,39 +100,7 @@ public class DataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param object The object you want to write data about
|
||||
* @param reference The system's star
|
||||
* @throws WritingException
|
||||
*/
|
||||
public void write(Object object, Object reference) throws WritingException {
|
||||
String id = object.name;
|
||||
Vector3d position = object.position;
|
||||
Vector3d speed = object.speed;
|
||||
Vector3d AUposition = AU.convertFromMeter(position);
|
||||
Vector3d AUspeed = AU.convertFromMetersPerSecond(speed);
|
||||
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
if (this.counter % SimulatorConfig.skipLines == 0) {
|
||||
if (SimulatorConfig.outputUnit.equals("AU")) {
|
||||
this.writer.append(id + DELIMITER + decimalFormatter(AUposition.getX()) + DELIMITER + decimalFormatter(AUposition.getY()) + DELIMITER + decimalFormatter(AUposition.getZ()) + DELIMITER + decimalFormatter(AUspeed.getX()) + DELIMITER + decimalFormatter(AUspeed.getY()) + DELIMITER + decimalFormatter(AUspeed.getZ()) + NEW_LINE);
|
||||
} else {
|
||||
this.writer.append(id + DELIMITER + decimalFormatter(position.getX()) + DELIMITER + decimalFormatter(position.getY()) + DELIMITER + decimalFormatter(position.getZ()) + DELIMITER + decimalFormatter(speed.getX()) + DELIMITER + decimalFormatter(speed.getY()) + DELIMITER + decimalFormatter(speed.getZ()) + NEW_LINE);
|
||||
}
|
||||
}
|
||||
this.counter++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("An error occurred while writing to the file!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String decimalFormatter(double input) {
|
||||
protected String decimalFormatter(double input) {
|
||||
return this.formatter.format(input);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,61 @@
|
||||
package com.verictas.pos.simulator.dataWriter;
|
||||
|
||||
import com.verictas.pos.simulator.Object;
|
||||
import com.verictas.pos.simulator.SimulatorConfig;
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
|
||||
import javax.vecmath.Vector3d;
|
||||
|
||||
public class PosDataWriter extends DataWriter {
|
||||
public PosDataWriter() throws WritingException {
|
||||
super("position");
|
||||
try {
|
||||
|
||||
/**
|
||||
* Write the lines with information about the columns
|
||||
*/
|
||||
|
||||
if (SimulatorConfig.outputUnit.equals("AU")) {
|
||||
this.writer.write("Object" + DELIMITER + "X (AU)" + DELIMITER + "Y (AU)" + DELIMITER + "Z (AU)" + DELIMITER + "VX (AU/day)" + DELIMITER + "VY (AU/day)" + DELIMITER + "VZ (AU/day)" + NEW_LINE);
|
||||
} else {
|
||||
this.writer.write("Object" + DELIMITER + "X (m)" + DELIMITER + "Y (m)" + DELIMITER + "Z (m)" + DELIMITER + "VX (m/s)" + DELIMITER + "VY (m/s)" + DELIMITER + "VZ (m/s)" + NEW_LINE);
|
||||
}
|
||||
|
||||
this.counter++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("An error occurred while writing to the file!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param object The object you want to write data about
|
||||
* @throws WritingException
|
||||
*/
|
||||
public void write(Object object) throws WritingException {
|
||||
String id = object.name;
|
||||
Vector3d position = object.position;
|
||||
Vector3d speed = object.speed;
|
||||
Vector3d AUposition = AU.convertFromMeter(position);
|
||||
Vector3d AUspeed = AU.convertFromMetersPerSecond(speed);
|
||||
|
||||
if (this.writer == null) {
|
||||
throw new WritingException("The writer isn't defined yet");
|
||||
} else {
|
||||
try {
|
||||
if (this.counter % SimulatorConfig.skipLines == 0) {
|
||||
if (SimulatorConfig.outputUnit.equals("AU")) {
|
||||
this.writer.append(id + DELIMITER + decimalFormatter(AUposition.getX()) + DELIMITER + decimalFormatter(AUposition.getY()) + DELIMITER + decimalFormatter(AUposition.getZ()) + DELIMITER + decimalFormatter(AUspeed.getX()) + DELIMITER + decimalFormatter(AUspeed.getY()) + DELIMITER + decimalFormatter(AUspeed.getZ()) + NEW_LINE);
|
||||
} else {
|
||||
this.writer.append(id + DELIMITER + decimalFormatter(position.getX()) + DELIMITER + decimalFormatter(position.getY()) + DELIMITER + decimalFormatter(position.getZ()) + DELIMITER + decimalFormatter(speed.getX()) + DELIMITER + decimalFormatter(speed.getY()) + DELIMITER + decimalFormatter(speed.getZ()) + NEW_LINE);
|
||||
}
|
||||
}
|
||||
this.counter++;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new WritingException("An error occurred while writing to the file!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,25 +3,30 @@ package com.verictas.pos.simulator.processor;
|
||||
import com.verictas.pos.simulator.Object;
|
||||
import com.verictas.pos.simulator.Simulator;
|
||||
import com.verictas.pos.simulator.SimulatorConfig;
|
||||
import com.verictas.pos.simulator.dataWriter.AOPDataWriter;
|
||||
import com.verictas.pos.simulator.dataWriter.DataWriter;
|
||||
import com.verictas.pos.simulator.dataWriter.PosDataWriter;
|
||||
import com.verictas.pos.simulator.dataWriter.WritingException;
|
||||
import com.verictas.pos.simulator.mathUtils.AOP;
|
||||
import com.verictas.pos.simulator.mathUtils.AU;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class Processor {
|
||||
private DataWriter writer;
|
||||
private PosDataWriter writer;
|
||||
private AOPDataWriter aopWriter;
|
||||
public HashMap<String, Object> initialObjectValues = new HashMap<>();
|
||||
public HashMap<String, ObjectProcessor> objects = new HashMap<>();
|
||||
public HashMap<String, ArrayList<Double>> arguments = new HashMap<>();
|
||||
public HashMap<String, TreeMap<Integer, Double>> arguments = new HashMap<>();
|
||||
|
||||
public Processor(Object[] objects) throws ProcessingException, WritingException {
|
||||
/**
|
||||
* Initialize DataWriter
|
||||
*/
|
||||
this.writer = new DataWriter();
|
||||
this.writer = new PosDataWriter();
|
||||
this.aopWriter = new AOPDataWriter();
|
||||
|
||||
/**
|
||||
* Store the initial values of all the objects in memory (and to a file) for later use
|
||||
@ -29,7 +34,7 @@ public class Processor {
|
||||
this.initialObjectValues = objectArrayToHashMap(objects);
|
||||
|
||||
// Write initial values to file
|
||||
this.write(initialObjectValues);
|
||||
this.writePos(initialObjectValues);
|
||||
|
||||
/**
|
||||
* Create the object processing array
|
||||
@ -117,11 +122,12 @@ public class Processor {
|
||||
// Add the node to the list
|
||||
if (arguments.get(objectName) == null) {
|
||||
// If not defined
|
||||
ArrayList<Double> agmnts = new ArrayList<>();
|
||||
TreeMap<Integer, Double> agmnts = new TreeMap<>();
|
||||
arguments.put(objectName, agmnts);
|
||||
}
|
||||
|
||||
arguments.get(objectName).add(AOP.calculate(object.ascendingNode, object.perihelion, object.aphelion));
|
||||
|
||||
arguments.get(objectName).put(Simulator.round, AOP.calculate(object.ascendingNode, object.perihelion, object.aphelion));
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -143,17 +149,17 @@ public class Processor {
|
||||
this.objects.put(objectName, object);
|
||||
}
|
||||
|
||||
this.write(objects);
|
||||
this.writePos(objects);
|
||||
}
|
||||
|
||||
private void write(HashMap<String, Object> objects) throws ProcessingException, WritingException {
|
||||
private void writePos(HashMap<String, Object> objects) throws ProcessingException, WritingException {
|
||||
if (SimulatorConfig.skipUnnecessary) {
|
||||
for (String name : SimulatorConfig.objectNames) {
|
||||
this.writer.write(objects.get(name), objects.get(SimulatorConfig.sunName));
|
||||
this.writer.write(objects.get(name));
|
||||
}
|
||||
} else {
|
||||
for (Object object : objects.values()) {
|
||||
this.writer.write(object, objects.get(SimulatorConfig.sunName));
|
||||
this.writer.write(object);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -172,30 +178,37 @@ public class Processor {
|
||||
public void close() throws ProcessingException {
|
||||
try {
|
||||
this.writer.save();
|
||||
|
||||
System.out.println("TOTAL RESULTS: " + arguments);
|
||||
|
||||
System.out.println("");
|
||||
for(String objectName : SimulatorConfig.objectNames) {
|
||||
ArrayList<Double> arguments = this.arguments.get(objectName);
|
||||
TreeMap<Integer, Double> arguments = this.arguments.get(objectName);
|
||||
|
||||
this.aopWriter.write(objectName, arguments);
|
||||
|
||||
double score = 0;
|
||||
|
||||
Double[] empty = new Double[arguments.size()];
|
||||
Double[] agmnts = arguments.values().toArray(empty);
|
||||
|
||||
// Calculate score
|
||||
for(int i = 1; i < arguments.size() - 1; i++) {
|
||||
score = score + Math.abs(arguments.get(i-1) - arguments.get(i));
|
||||
for(int i = 1; i < agmnts.length - 1; i++) {
|
||||
score = score + Math.abs(agmnts[i-1] - agmnts[i]);
|
||||
}
|
||||
|
||||
System.out.println("SCORE (" + objectName + "): " + score);
|
||||
|
||||
// CALCULATE AVERAGE
|
||||
double sum = 0;
|
||||
for (int i = 0; i < arguments.size(); i++){
|
||||
sum = sum + arguments.get(i);
|
||||
for (int i = 0; i < agmnts.length; i++){
|
||||
sum = sum + agmnts[i];
|
||||
}
|
||||
// calculate average
|
||||
double average = sum / arguments.size();
|
||||
double average = sum / agmnts.length;
|
||||
|
||||
System.out.println("AVERAGE (" + objectName + ") (degrees): " + Math.toDegrees(average));
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
this.aopWriter.save();
|
||||
} catch(WritingException e) {
|
||||
throw new ProcessingException("An error occurred during creation of the file writer: " + e.toString());
|
||||
}
|
||||
|
Reference in New Issue
Block a user