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

Create a file creation system for argument output

This commit is contained in:
2016-12-10 16:15:34 +01:00
parent e1e616b28a
commit d2568bfeea
7 changed files with 413 additions and 209 deletions

View File

@ -13,7 +13,7 @@ public class SimulatorConfig {
*/
public static String sunName = "Sun"; // The name of the sun to calculate values TO
public static String[] objectNames = { "Sedna" }; // The name of the object(s) your want to calculate the values OF
public static String[] objectNames = { "Jupiter", "Sedna" }; // The name of the object(s) your want to calculate the values OF
/**
* Output preferences

View File

@ -0,0 +1,47 @@
package com.verictas.pos.simulator.dataWriter;
import com.verictas.pos.simulator.Object;
import com.verictas.pos.simulator.Simulator;
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.*;
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!");
}
}
}

View File

@ -16,15 +16,15 @@ 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 +36,7 @@ public class DataWriter {
* Constructor
* @throws WritingException
*/
public DataWriter() throws WritingException {
public DataWriter(String filenameAppendix) throws WritingException {
/**
* Prepare the locale
@ -49,7 +49,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 + getCurrentTimeStamp() + "-" + filenameAppendix + ".txt";
System.out.println("WRITING DATA TO: " + path);
/**
@ -64,18 +64,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 +106,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);
}

View File

@ -0,0 +1,70 @@
package com.verictas.pos.simulator.dataWriter;
import com.verictas.pos.simulator.Object;
import com.verictas.pos.simulator.Simulator;
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 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!");
}
}
}
}

View File

@ -3,25 +3,31 @@ 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 javax.xml.crypto.Data;
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, SimpleObjectProcessor> 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
@ -53,11 +59,10 @@ public class Processor {
if (Simulator.round % SimulatorConfig.moduloArgument == 0) {
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(object.calculateAOP());
arguments.get(objectName).put(Simulator.round, object.calculateAOP());
}
this.objects.put(objectName, object);
@ -69,11 +74,11 @@ public class Processor {
private void write(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);
}
}
}
@ -92,30 +97,41 @@ public class Processor {
public void close() throws ProcessingException {
try {
this.writer.save();
System.out.println("");
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());
}