Merge pull request #2236 from SiboVG/issue-2225

[#2225] Copy simulation data from sustainer to other stages for CSV exporting
This commit is contained in:
Joe Pfeiffer 2023-07-23 08:51:49 -06:00 committed by GitHub
commit d2bb552807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 192 additions and 76 deletions

View File

@ -92,9 +92,11 @@ public class CSVExport {
private static void writeData(PrintWriter writer, FlightDataBranch branch,
FlightDataType[] fields, Unit[] units, String fieldSeparator, int decimalPlaces, boolean isExponentialNotation,
boolean eventComments, String commentStarter) {
// Time variable
List<Double> time = branch.get(FlightDataType.TYPE_TIME);
// Number of data points
int n = branch.getLength();
int n = time != null ? time.size() : branch.getLength();
// Flight events in occurrence order
List<FlightEvent> events = branch.getEvents();
@ -102,15 +104,14 @@ public class CSVExport {
int eventPosition = 0;
// List of field values
List<List<Double>> fieldValues = new ArrayList<List<Double>>();
List<List<Double>> fieldValues = new ArrayList<>();
for (FlightDataType t : fields) {
fieldValues.add(branch.get(t));
List<Double> values = branch.get(t);
fieldValues.add(values);
}
// Time variable
List<Double> time = branch.get(FlightDataType.TYPE_TIME);
// If time information is not available, print events at beginning of file
if (eventComments && time == null) {
// If time information is not available, print events at beginning of file
for (FlightEvent e : events) {
printEvent(writer, e, commentStarter);
}

View File

@ -216,21 +216,22 @@ public class OpenRocketSaver extends RocketSaver {
/*
* NOTE: Remember to update the supported versions in DocumentConfig as well!
*
* File version 1.8 is required for:
* File version 1.9 is required for:
* - new-style positioning
* - external/parallel booster stages
* - external pods
* - Rail Buttons
* - Flight event source saving
*
* Otherwise use version 1.8.
* Otherwise use version 1.9.
*/
/////////////////
// Version 1.8 //
// Version 1.9 //
/////////////////
// for any new-style positioning: 'axialoffset', 'angleoffset', 'radiusoffset' tags
// these tags are used for any RocketComponent child classes positioning... so... ALL the classes.
return FILE_VERSION_DIVISOR + 8;
return FILE_VERSION_DIVISOR + 9;
}
@ -531,8 +532,13 @@ public class OpenRocketSaver extends RocketSaver {
// Write events
for (FlightEvent event : branch.getEvents()) {
writeln("<event time=\"" + TextUtil.doubleToString(event.getTime())
+ "\" type=\"" + enumToXMLName(event.getType()) + "\"/>");
String eventStr = "<event time=\"" + TextUtil.doubleToString(event.getTime())
+ "\" type=\"" + enumToXMLName(event.getType());
if (event.getSource() != null) {
eventStr += "\" source=\"" + TextUtil.escapeXML(event.getSource().getID());
}
eventStr += "\"/>";
writeln(eventStr);
}
// Write the data

View File

@ -52,7 +52,7 @@ import net.sf.openrocket.util.Reflection;
class DocumentConfig {
/* Remember to update OpenRocketSaver as well! */
public static final String[] SUPPORTED_VERSIONS = { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8" };
public static final String[] SUPPORTED_VERSIONS = { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9" };
/**
* Divisor used in converting an integer version to the point-represented version.
@ -113,6 +113,8 @@ class DocumentConfig {
// RocketComponent
setters.put("RocketComponent:name", new StringSetter(
Reflection.findMethod(RocketComponent.class, "setName", String.class)));
setters.put("RocketComponent:id", new StringSetter(
Reflection.findMethod(RocketComponent.class, "setID", String.class)));
setters.put("RocketComponent:color", new ColorSetter(
Reflection.findMethod(RocketComponent.class, "setColor", Color.class)));
setters.put("RocketComponent:linestyle", new EnumSetter<LineStyle>(

View File

@ -8,6 +8,8 @@ import net.sf.openrocket.file.simplesax.AbstractElementHandler;
import net.sf.openrocket.file.simplesax.ElementHandler;
import net.sf.openrocket.file.simplesax.PlainTextHandler;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.simulation.FlightDataBranch;
import net.sf.openrocket.simulation.FlightDataType;
import net.sf.openrocket.simulation.FlightEvent;
@ -126,6 +128,8 @@ class FlightDataBranchHandler extends AbstractElementHandler {
if (element.equals("event")) {
double time;
FlightEvent.Type type;
String sourceID;
RocketComponent source = null;
try {
time = DocumentConfig.stringToDouble(attributes.get("time"));
@ -139,8 +143,15 @@ class FlightDataBranchHandler extends AbstractElementHandler {
warnings.add("Illegal event specification, ignoring.");
return;
}
// Get the event source
Rocket rocket = context.getOpenRocketDocument().getRocket();
sourceID = attributes.get("source");
if (sourceID != null) {
source = rocket.findComponent(sourceID);
}
branch.addEvent(new FlightEvent(type, time));
branch.addEvent(new FlightEvent(type, time, source));
return;
}

View File

@ -36,6 +36,7 @@ public class RocketComponentSaver {
protected void addParams(net.sf.openrocket.rocketcomponent.RocketComponent c, List<String> elements) {
elements.add("<name>" + TextUtil.escapeXML(c.getName()) + "</name>");
elements.add("<id>" + TextUtil.escapeXML(c.getID()) + "</id>");
ComponentPreset preset = c.getPresetComponent();
if (preset != null) {

View File

@ -1282,8 +1282,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
mutex.verify();
this.id = UniqueID.uuid();
}
/**
* Set the ID for this component.
* Generally not recommended to directly set the ID, this is done automatically. Only use this in case you have to.
* @param newID new ID
*/
public void setID(String newID) {
mutex.verify();
this.id = newID;
}
/**
@ -2047,6 +2055,16 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
}
return children;
}
/**
* Checks whether this component contains <component> as one of its (sub-)children.
* @param component component to check
* @return true if component is a (sub-)child of this component
*/
public final boolean containsChild(RocketComponent component) {
List<RocketComponent> allChildren = getAllChildren();
return allChildren.contains(component);
}
/**

View File

@ -489,7 +489,7 @@ public class BasicEventSimulationEngine implements SimulationEngine {
SimulationStatus boosterStatus = new SimulationStatus(currentStatus);
// Prepare the new simulation branch
boosterStatus.setFlightData(new FlightDataBranch(boosterStage.getName(), currentStatus.getFlightData()));
boosterStatus.setFlightData(new FlightDataBranch(boosterStage.getName(), boosterStage, currentStatus.getFlightData()));
boosterStatus.getFlightData().addEvent(event);
// Mark the current status as having dropped the current stage and all stages below it

View File

@ -134,8 +134,12 @@ public class FlightData {
return branches.size();
}
public FlightDataBranch getBranch(int n) {
return branches.get(n);
public FlightDataBranch getBranch(int stageNr) {
return branches.get(stageNr);
}
public int getStageNr(FlightDataBranch branch) {
return branches.indexOf(branch);
}
public List<FlightDataBranch> getBranches() {

View File

@ -6,6 +6,9 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.sf.openrocket.rocketcomponent.AxialStage;
import net.sf.openrocket.rocketcomponent.Rocket;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.Monitorable;
import net.sf.openrocket.util.Mutable;
@ -30,11 +33,10 @@ public class FlightDataBranch implements Monitorable {
/** The name of this flight data branch. */
private final String branchName;
private final Map<FlightDataType, ArrayList<Double>> values =
new LinkedHashMap<FlightDataType, ArrayList<Double>>();
private final Map<FlightDataType, ArrayList<Double>> values = new LinkedHashMap<>();
private final Map<FlightDataType, Double> maxValues = new HashMap<FlightDataType, Double>();
private final Map<FlightDataType, Double> minValues = new HashMap<FlightDataType, Double>();
private final Map<FlightDataType, Double> maxValues = new HashMap<>();
private final Map<FlightDataType, Double> minValues = new HashMap<>();
/**
* time for the rocket to reach apogee if the flight had been no recovery deployment
@ -77,23 +79,19 @@ public class FlightDataBranch implements Monitorable {
}
/**
* Make a flight data branch with one data point copied from its parent. Intended for use
* Make a flight data branch with all data points copied from its parent. Intended for use
* when creating a new branch upon stage separation, so the data at separation is present
* in both branches (and if the new branch has an immediate exception, it can be plotted)
*
* @param branchName the name of the new branch.
* @param srcComponent the component that is the source of the new branch.
* @param parent the parent branch to copy data from.
*/
public FlightDataBranch(String branchName, FlightDataBranch parent) {
public FlightDataBranch(String branchName, RocketComponent srcComponent, FlightDataBranch parent) {
this.branchName = branchName;
// need to have at least one type to set up values
values.put(FlightDataType.TYPE_TIME, new ArrayList<Double>());
minValues.put(FlightDataType.TYPE_TIME, Double.NaN);
maxValues.put(FlightDataType.TYPE_TIME, Double.NaN);
// copy all values into new FlightDataBranch
this.addPoint();
for (FlightDataType t : parent.getTypes()) {
this.setValue(t, parent.getLast(t));
}
// Copy all the values from the parent
copyValuesFromBranch(parent, srcComponent);
}
/**
@ -115,12 +113,27 @@ public class FlightDataBranch implements Monitorable {
public void addPoint() {
mutable.check();
for (FlightDataType t : values.keySet()) {
values.get(t).add(Double.NaN);
for (FlightDataType type : values.keySet()) {
sanityCheckValues(type, Double.NaN);
values.get(type).add(Double.NaN);
}
modID++;
}
private void sanityCheckValues(FlightDataType type, Double value) {
ArrayList<Double> list = values.get(type);
if (list == null) {
list = new ArrayList<>();
int n = getLength();
for (int i = 0; i < n; i++) {
list.add(Double.NaN);
}
values.put(type, list);
minValues.put(type, value);
maxValues.put(type, value);
}
}
/**
* Set the value for a specific data type at the latest point. New variable types can be
@ -132,20 +145,10 @@ public class FlightDataBranch implements Monitorable {
*/
public void setValue(FlightDataType type, double value) {
mutable.check();
sanityCheckValues(type, value);
ArrayList<Double> list = values.get(type);
if (list == null) {
list = new ArrayList<Double>();
int n = getLength();
for (int i = 0; i < n; i++) {
list.add(Double.NaN);
}
values.put(type, list);
minValues.put(type, value);
maxValues.put(type, value);
}
if (list.size() > 0) {
list.set(list.size() - 1, value);
}
@ -161,7 +164,68 @@ public class FlightDataBranch implements Monitorable {
}
modID++;
}
/**
* Clears all the current values in the branch and copies the values from the given branch.
* @param srcBranch the branch to copy values from
* @param srcComponent the component that is the source of this branch (used for copying events)
*/
private void copyValuesFromBranch(FlightDataBranch srcBranch, RocketComponent srcComponent) {
this.values.clear();
// Need to have at least one type to set up values
values.put(FlightDataType.TYPE_TIME, new ArrayList<>());
minValues.put(FlightDataType.TYPE_TIME, Double.NaN);
maxValues.put(FlightDataType.TYPE_TIME, Double.NaN);
if (srcBranch == null) {
return;
}
// Copy flight data
for (int i = 0; i < srcBranch.getLength(); i++) {
this.addPoint();
for (FlightDataType type : srcBranch.getTypes()) {
this.setValue(type, srcBranch.getByIndex(type, i));
}
}
// Copy flight events belonging to this branch
List<FlightEvent> sustainerEvents = srcBranch.getEvents();
for (FlightEvent event : sustainerEvents) {
// Stage separation is already added elsewhere, so don't copy it over (otherwise you have a duplicate)
if (event.getType() == FlightEvent.Type.STAGE_SEPARATION) {
continue;
}
RocketComponent srcEventComponent = event.getSource();
// Ignore null events
if (srcComponent == null || srcEventComponent == null) {
continue;
}
// Ignore events from other stages. Important for when the current stage has a booster stage; we don't want to copy over the booster events.
if (getStageForComponent(srcComponent) != getStageForComponent(srcEventComponent)) {
continue;
}
if (srcComponent == srcEventComponent || srcComponent.containsChild(srcEventComponent)) {
events.add(event);
}
}
}
/**
* A safer method for checking the stage of a component (that shouldn't throw exceptions when calling on stages/rockets)
* @param component the component to get the stage of
* @return the stage of the component, or null if the component is a rocket
*/
private AxialStage getStageForComponent(RocketComponent component) {
if (component instanceof AxialStage) {
return (AxialStage) component;
} else if (component instanceof Rocket) {
return null;
} else {
return component.getStage();
}
}
/**
* Return the branch name.
@ -203,6 +267,23 @@ public class FlightDataBranch implements Monitorable {
return null;
return list.clone();
}
/**
* Return the value of the specified type at the specified index.
* @param type the variable type
* @param index the data index of the value
* @return the value at the specified index
*/
public Double getByIndex(FlightDataType type, int index) {
if (index < 0 || index >= getLength()) {
throw new IllegalArgumentException("Index out of bounds");
}
ArrayList<Double> list = values.get(type);
if (list == null) {
return null;
}
return list.get(index);
}
/**
* Return the last value of the specified type in the branch, or NaN if the type is

View File

@ -331,9 +331,9 @@ public class OpenRocketSaverTest {
////////////////////////////////
@Test
public void testFileVersion108_withSimulationExtension() {
public void testFileVersion109_withSimulationExtension() {
OpenRocketDocument rocketDoc = TestRockets.makeTestRocket_v107_withSimulationExtension(SIMULATION_EXTENSION_SCRIPT);
assertEquals(108, getCalculatedFileVersion(rocketDoc));
assertEquals(109, getCalculatedFileVersion(rocketDoc));
}

View File

@ -108,8 +108,8 @@ public class FlightEventsTest extends BaseTestCase {
// events whose time is too variable to check are given a time of 1200
for (int b = 0; b < 3; b++) {
FlightEvent[] expectedEvents;
final RocketComponent[] expectedSources;
switch (b) {
// Sustainer (payload fairing stage)
case 0:
expectedEvents = new FlightEvent[] {
new FlightEvent(FlightEvent.Type.LAUNCH, 0.0, rocket),
@ -129,15 +129,23 @@ public class FlightEventsTest extends BaseTestCase {
new FlightEvent(FlightEvent.Type.SIMULATION_END, 1200, null)
};
break;
// Core stage
case 1:
expectedEvents = new FlightEvent[] {
new FlightEvent(FlightEvent.Type.IGNITION, 0.0, coreBody),
new FlightEvent(FlightEvent.Type.BURNOUT, 2.0, coreBody),
new FlightEvent(FlightEvent.Type.EJECTION_CHARGE, 2.0, coreStage),
new FlightEvent(FlightEvent.Type.STAGE_SEPARATION, 2.0, coreStage),
new FlightEvent(FlightEvent.Type.GROUND_HIT, 1200, null),
new FlightEvent(FlightEvent.Type.SIMULATION_END, 1200, null)
};
break;
// Booster stage
case 2:
expectedEvents = new FlightEvent[] {
new FlightEvent(FlightEvent.Type.IGNITION, 0.0, boosterMotorTubes),
new FlightEvent(FlightEvent.Type.BURNOUT, 2.0, boosterMotorTubes),
new FlightEvent(FlightEvent.Type.EJECTION_CHARGE, 2.0, boosterStage),
new FlightEvent(FlightEvent.Type.STAGE_SEPARATION, 2.0, boosterStage),
new FlightEvent(FlightEvent.Type.TUMBLE, 3.551, null),
new FlightEvent(FlightEvent.Type.GROUND_HIT, 1200, null),
@ -150,10 +158,7 @@ public class FlightEventsTest extends BaseTestCase {
// Test event count
final FlightDataBranch branch = sim.getSimulatedData().getBranch(b);
final FlightEvent[] events = (FlightEvent[]) branch.getEvents().toArray(new FlightEvent[0]);
for (int i = 0; i < events.length; i++) {
System.out.println("branch " + b + " index " + i + " event " + events[i]);
}
final FlightEvent[] events = branch.getEvents().toArray(new FlightEvent[0]);
assertEquals(" Multi-stage simulation, branch " + b + " invalid number of events ", expectedEvents.length, events.length);
// Test that all expected events are present, in the right order, at the right time, from the right sources

View File

@ -63,3 +63,6 @@ The following file format versions exist:
Rename <fincount> to <instancecount> (<fincount> remains for backward compatibility)
Rename <position> to <axialoffset> (<position> remains for backward compatibility)
Rename <rotation> to <angleoffset> (<rotation> remains for backward compatibility)
1.9: Introduced with OpenRocket 23.xx.
Added ID for each rocket component, to in turn add this ID as a source for flight events.

View File

@ -192,9 +192,8 @@ public class SimulationPlot {
}
data[axis].addSeries(series);
}
// For each of the secondary branches, we use data from branch 0 for the earlier times
// Secondary branches
for (int branchIndex = 1; branchIndex < branchCount; branchIndex++) {
FlightDataBranch primaryBranch = simulation.getSimulatedData().getBranch(0);
FlightDataBranch thisBranch = simulation.getSimulatedData().getBranch(branchIndex);
// Ignore empty branches
@ -206,25 +205,10 @@ public class SimulationPlot {
continue;
}
// Get first time index used in secondary branch;
double firstSampleTime = thisBranch.get(FlightDataType.TYPE_TIME).get(0);
XYSeries series = new XYSeries(seriesCount++, false, true);
series.setDescription(thisBranch.getBranchName() + ": " + name);
// Copy the first points from the primaryBranch.
List<Double> primaryT = primaryBranch.get(FlightDataType.TYPE_TIME);
List<Double> primaryx = primaryBranch.get(domainType);
List<Double> primaryy = primaryBranch.get(type);
for (int j = 0; j < primaryT.size(); j++) {
if (primaryT.get(j) >= firstSampleTime) {
break;
}
series.add(domainUnit.toUnit(primaryx.get(j)), unit.toUnit(primaryy.get(j)));
}
// Now copy all the data from the secondary branch
// Copy all the data from the secondary branch
List<Double> plotx = thisBranch.get(domainType);
List<Double> ploty = thisBranch.get(type);