Skip to content

Commit

Permalink
Added support for CSV Output.
Browse files Browse the repository at this point in the history
  • Loading branch information
MauveCloud committed Mar 17, 2019
1 parent 1b7513e commit b0e0a8f
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 64 deletions.
66 changes: 59 additions & 7 deletions src/Ic2ExpReactorPlanner/AutomationSimulator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package Ic2ExpReactorPlanner;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
Expand All @@ -24,6 +27,10 @@ public class AutomationSimulator extends SwingWorker<Void, String> {

private final int initialHeat;

private final File csvFile;

private final int csvLimit;

private double minEUoutput = Double.MAX_VALUE;

private double maxEUoutput = 0.0;
Expand Down Expand Up @@ -54,21 +61,30 @@ public class AutomationSimulator extends SwingWorker<Void, String> {

private static final ResourceBundle BUNDLE = java.util.ResourceBundle.getBundle("Ic2ExpReactorPlanner/Bundle");

public AutomationSimulator(final Reactor reactor, final JTextArea output, final JPanel[][] reactorButtonPanels, final int initialHeat,
final int onPulseDuration, final int offPulseDuration, final int suspendTemp, final int resumeTemp) {
public AutomationSimulator(final Reactor reactor, final JTextArea output, final JPanel[][] reactorButtonPanels, final File csvFile, final int csvLimit) {
this.reactor = reactor;
this.output = output;
this.reactorButtonPanels = reactorButtonPanels;
this.initialHeat = initialHeat;
this.onPulseDuration = onPulseDuration;
this.offPulseDuration = offPulseDuration;
this.suspendTemp = suspendTemp;
this.resumeTemp = resumeTemp;
this.initialHeat = (int)reactor.getCurrentHeat();
this.onPulseDuration = reactor.getOnPulse();
this.offPulseDuration = reactor.getOffPulse();
this.suspendTemp = reactor.getSuspendTemp();
this.resumeTemp = reactor.getResumeTemp();
this.nextOffTime = onPulseDuration;
this.csvFile = csvFile;
this.csvLimit = csvLimit;
}

@Override
protected Void doInBackground() throws Exception {
PrintWriter csvOut = null;
if (csvFile != null) {
try {
csvOut = new PrintWriter(csvFile);
} catch (IOException ex) {
publish(BUNDLE.getString("Simulation.CSVOpenFailure"));
}
}
long startTime = System.nanoTime();
int reactorTicks = 0;
int activeTime = 0;
Expand All @@ -79,6 +95,19 @@ protected Void doInBackground() throws Exception {
int cooldownTicks = 0;
int totalRodCount = 0;
try {
if (csvOut != null) {
csvOut.print(BUNDLE.getString("CSVData.HeaderReactorTick"));
csvOut.print(BUNDLE.getString("CSVData.HeaderCoreHeat"));
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
if (component != null) {
csvOut.printf(BUNDLE.getString("CSVData.HeaderComponentName"), ComponentFactory.getDefaultComponent(ComponentFactory.getID(component)).toString(), row, col);
}
}
}
csvOut.println();
}
publish(""); //NOI18N
publish(BUNDLE.getString("Simulation.Started"));
reactor.setCurrentHeat(initialHeat);
Expand Down Expand Up @@ -250,6 +279,23 @@ protected Void doInBackground() throws Exception {
if (!active) {
currentActiveTime = 0;
}
if (csvOut != null && reactorTicks <= csvLimit) {
csvOut.printf(BUNDLE.getString("CSVData.EntryReactorTick"), reactorTicks);
csvOut.printf(BUNDLE.getString("CSVData.EntryCoreHeat"), reactor.getCurrentHeat());
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
if (component != null) {
double componentValue = component.getCurrentDamage();
if (component.getMaxHeat() > 1.0) {
componentValue = component.getCurrentHeat();
}
csvOut.printf(BUNDLE.getString("CSVData.EntryComponentValue"), componentValue);
}
}
}
csvOut.println();
}
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
Expand All @@ -261,6 +307,9 @@ protected Void doInBackground() throws Exception {
}
}
} while (reactor.getCurrentHeat() < reactor.getMaxHeat() && reactorTicks < 5000000 && !isCancelled());
if (csvOut != null) {
csvOut.close();
}
if (isCancelled()) {
publish(String.format(BUNDLE.getString("Simulation.CancelledAtTick"), reactorTicks));
return null;
Expand Down Expand Up @@ -373,6 +422,9 @@ protected Void doInBackground() throws Exception {
publish(String.format(BUNDLE.getString("Simulation.ErrorCooldown"), cooldownTicks));
}
publish(e.toString(), " ", Arrays.toString(e.getStackTrace())); // NO18N
if (csvOut != null) {
csvOut.close();
}
}
long endTime = System.nanoTime();
publish(String.format(BUNDLE.getString("Simulation.ElapsedTime"), (endTime - startTime) / 1e9));
Expand Down
14 changes: 14 additions & 0 deletions src/Ic2ExpReactorPlanner/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ ComponentName.ReactorPlating=Reactor Plating
ComponentName.RshCondensator=RSH-Condensator
ComponentName.ThickNeutronReflector=Thick Neutron Reflector

Config.CSVCheckbox=Output CSV Data
Config.CSVLimit=For how many simulated reactor ticks:
Config.EUReactor=EU Reactor
Config.FluidReactor=Fluid Reactor
Config.InitialComponentHeat=Initial Heat:
Expand All @@ -79,6 +81,13 @@ Config.SimulationStyle=Simulation Style:
Config.SuspendTemp=Suspend when reactor temp >=
Config.SuspendTempHelp=(both temps can be set to match explode temp to mimic having no temperature control)

CSVData.EntryComponentValue=,%.2f
CSVData.EntryCoreHeat=,%.2f
CSVData.EntryReactorTick=%d
CSVData.HeaderComponentName=,%s (R%dC%d)
CSVData.HeaderCoreHeat=,Core Heat
CSVData.HeaderReactorTick=Reactor Tick

MaterialName.AdvancedAlloy=Advanced Alloy
MaterialName.Coal=Coal
MaterialName.Copper=Copper
Expand All @@ -105,6 +114,7 @@ MaterialName.UraniumFuel=Uranium Fuel
Simulation.ActiveTime=Reactor was active for a total of %,d seconds (%,d to %,d seconds at a time).\n
Simulation.CancelledAtTick=Simulation cancelled at tick %,d.\n
Simulation.ComponentsReplaced=Components replaced:\n%s
Simulation.CSVOpenFailure=Failed to open CSV file for output.\n
Simulation.CycleCompleteTime=Cycle complete after %,d seconds.\n
Simulation.Efficiency=Efficiency: %.2f average, %.2f minimum, %.2f maximum\n
Simulation.ElapsedTime=Simulation took %.2f seconds.\n
Expand Down Expand Up @@ -172,6 +182,10 @@ UI.ComponentPlacingDefault=Placing Component: None
UI.ComponentPlacingSpecific=Placing Component: %s
UI.ComponentTab=Component
UI.CopyCodeButton=Copy Code
UI.CSVBrowseButton=Browse
UI.CSVFileDefault=No File Selected
UI.CSVHelp=<html>Warnings:<ol><li>Simulation will likely run much slower with CSV Output enabled, especially if the chosen file is on a mechanical hard drive.</li><li>User is responsible for making sure target drive has enough space for the CSV file.</li><li>Additional simulations will overwrite the CSV file unless the target is manually changed.</li></ol></html>
UI.CSVTab=CSV
UI.InitialHeatDisplay= (initial heat: %,d)
UI.InitialReactorHeat=Initial Reactor Heat:
UI.MainTitle=IC2 Experimental Reactor Planner
Expand Down
66 changes: 59 additions & 7 deletions src/Ic2ExpReactorPlanner/PulsedSimulator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package Ic2ExpReactorPlanner;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -40,6 +43,10 @@ public class PulsedSimulator extends SwingWorker<Void, String> {

private final int resumeTemp;

private final File csvFile;

private final int csvLimit;

private boolean active = true;

private int nextOffTime = 0;
Expand All @@ -52,26 +59,48 @@ public class PulsedSimulator extends SwingWorker<Void, String> {

private static final ResourceBundle BUNDLE = java.util.ResourceBundle.getBundle("Ic2ExpReactorPlanner/Bundle");

public PulsedSimulator(final Reactor reactor, final JTextArea output, final JPanel[][] reactorButtonPanels, final int initialHeat,
final int onPulseDuration, final int offPulseDuration, final int suspendTemp, final int resumeTemp) {
public PulsedSimulator(final Reactor reactor, final JTextArea output, final JPanel[][] reactorButtonPanels, final File csvFile, final int csvLimit) {
this.reactor = reactor;
this.output = output;
this.reactorButtonPanels = reactorButtonPanels;
this.initialHeat = initialHeat;
this.onPulseDuration = onPulseDuration;
this.offPulseDuration = offPulseDuration;
this.suspendTemp = suspendTemp;
this.resumeTemp = resumeTemp;
this.initialHeat = (int)reactor.getCurrentHeat();
this.onPulseDuration = reactor.getOnPulse();
this.offPulseDuration = reactor.getOffPulse();
this.suspendTemp = reactor.getSuspendTemp();
this.resumeTemp = reactor.getResumeTemp();
this.nextOffTime = onPulseDuration;
this.csvFile = csvFile;
this.csvLimit = csvLimit;
}

@Override
protected Void doInBackground() throws Exception {
PrintWriter csvOut = null;
if (csvFile != null) {
try {
csvOut = new PrintWriter(csvFile);
} catch (IOException ex) {
publish(BUNDLE.getString("Simulation.CSVOpenFailure"));
}
}
long startTime = System.nanoTime();
int reactorTicks = 0;
int cooldownTicks = 0;
int totalRodCount = 0;
try {
if (csvOut != null) {
csvOut.print(BUNDLE.getString("CSVData.HeaderReactorTick"));
csvOut.print(BUNDLE.getString("CSVData.HeaderCoreHeat"));
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
if (component != null) {
csvOut.printf(BUNDLE.getString("CSVData.HeaderComponentName"), ComponentFactory.getDefaultComponent(ComponentFactory.getID(component)).toString(), row, col);
}
}
}
csvOut.println();
}
publish(""); //NOI18N
publish(BUNDLE.getString("Simulation.Started"));
reactor.setCurrentHeat(initialHeat);
Expand Down Expand Up @@ -196,6 +225,23 @@ protected Void doInBackground() throws Exception {
minHeatOutput = Math.min(lastHeatOutput, minHeatOutput);
maxHeatOutput = Math.max(lastHeatOutput, maxHeatOutput);
}
if (csvOut != null && reactorTicks <= csvLimit) {
csvOut.printf(BUNDLE.getString("CSVData.EntryReactorTick"), reactorTicks);
csvOut.printf(BUNDLE.getString("CSVData.EntryCoreHeat"), reactor.getCurrentHeat());
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
if (component != null) {
double componentValue = component.getCurrentDamage();
if (component.getMaxHeat() > 1.0) {
componentValue = component.getCurrentHeat();
}
csvOut.printf(BUNDLE.getString("CSVData.EntryComponentValue"), componentValue);
}
}
}
csvOut.println();
}
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 9; col++) {
ReactorComponent component = reactor.getComponentAt(row, col);
Expand Down Expand Up @@ -249,6 +295,9 @@ protected Void doInBackground() throws Exception {
}
}
} while (reactor.getCurrentHeat() < reactor.getMaxHeat() && (!allFuelRodsDepleted || lastEUoutput > 0 || lastHeatOutput > 0) && reactorTicks < 5000000 && !isCancelled());
if (csvOut != null) {
csvOut.close();
}
if (isCancelled()) {
publish(String.format(BUNDLE.getString("Simulation.CancelledAtTick"), reactorTicks));
return null;
Expand Down Expand Up @@ -353,6 +402,9 @@ protected Void doInBackground() throws Exception {
publish(String.format(BUNDLE.getString("Simulation.ErrorCooldown"), cooldownTicks));
}
publish(e.toString(), " ", Arrays.toString(e.getStackTrace())); //NOI18N
if (csvOut != null) {
csvOut.close();
}
}
long endTime = System.nanoTime();
publish(String.format(BUNDLE.getString("Simulation.ElapsedTime"), (endTime - startTime) / 1e9));
Expand Down
Loading

0 comments on commit b0e0a8f

Please sign in to comment.