diff --git a/simulator/.idea/workspace.xml b/simulator/.idea/workspace.xml index 1b83e39..f1ee7d3 100644 --- a/simulator/.idea/workspace.xml +++ b/simulator/.idea/workspace.xml @@ -7,7 +7,11 @@ + + + + @@ -28,7 +32,71 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -126,6 +194,9 @@ + + + @@ -244,9 +315,6 @@ - - - @@ -558,16 +626,18 @@ - - + + + - + + @@ -576,18 +646,16 @@ + - - - @@ -618,21 +686,6 @@ - - - - - - - - - - - - - - - @@ -896,14 +949,7 @@ - - - - - - - - + @@ -912,25 +958,6 @@ - - - - - - - - - - - - - - - - - - - @@ -939,14 +966,6 @@ - - - - - - - - @@ -984,10 +1003,53 @@ + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/simulator/src/com/verictas/pos/simulator/Main.java b/simulator/src/com/verictas/pos/simulator/Main.java index 5bc0406..37539c1 100644 --- a/simulator/src/com/verictas/pos/simulator/Main.java +++ b/simulator/src/com/verictas/pos/simulator/Main.java @@ -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 diff --git a/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java b/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java index ca0dde1..b99c0a4 100644 --- a/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java +++ b/simulator/src/com/verictas/pos/simulator/SimulatorConfig.java @@ -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 /** diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java new file mode 100644 index 0000000..cdc8096 --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/AOPDataWriter.java @@ -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 arguments) throws WritingException { + try { + for (Map.Entry 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!"); + } + } +} \ No newline at end of file diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java index 91ef51a..6ccfecd 100644 --- a/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/DataWriter.java @@ -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); } diff --git a/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java b/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java new file mode 100644 index 0000000..0446fcb --- /dev/null +++ b/simulator/src/com/verictas/pos/simulator/dataWriter/PosDataWriter.java @@ -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!"); + } + } + } +} \ No newline at end of file diff --git a/simulator/src/com/verictas/pos/simulator/processor/Processor.java b/simulator/src/com/verictas/pos/simulator/processor/Processor.java index ab8e2fd..07b5935 100644 --- a/simulator/src/com/verictas/pos/simulator/processor/Processor.java +++ b/simulator/src/com/verictas/pos/simulator/processor/Processor.java @@ -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 initialObjectValues = new HashMap<>(); public HashMap objects = new HashMap<>(); - public HashMap> arguments = new HashMap<>(); + public HashMap> 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 agmnts = new ArrayList<>(); + TreeMap 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 objects) throws ProcessingException, WritingException { + private void writePos(HashMap 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 arguments = this.arguments.get(objectName); + TreeMap 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()); }