1 package net.sf.openrocket.file.motor;
3 import java.io.IOException;
5 import java.nio.charset.Charset;
6 import java.util.ArrayList;
7 import java.util.HashMap;
10 import net.sf.openrocket.aerodynamics.WarningSet;
11 import net.sf.openrocket.file.simplesax.ElementHandler;
12 import net.sf.openrocket.file.simplesax.NullElementHandler;
13 import net.sf.openrocket.file.simplesax.PlainTextHandler;
14 import net.sf.openrocket.file.simplesax.SimpleSAX;
15 import net.sf.openrocket.motor.Manufacturer;
16 import net.sf.openrocket.motor.Motor;
17 import net.sf.openrocket.motor.MotorDigest;
18 import net.sf.openrocket.motor.ThrustCurveMotor;
19 import net.sf.openrocket.motor.MotorDigest.DataType;
20 import net.sf.openrocket.util.Coordinate;
22 import org.xml.sax.InputSource;
23 import org.xml.sax.SAXException;
25 public class RockSimMotorLoader extends AbstractMotorLoader {
27 public static final String CHARSET_NAME = "UTF-8";
29 public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
32 /** Any delay longed than this will be interpreted as a plugged motor. */
33 private static final int DELAY_LIMIT = 90;
38 protected Charset getDefaultCharset() {
45 * Load a <code>Motor</code> from a RockSim motor definition file specified by the
46 * <code>Reader</code>. The <code>Reader</code> is responsible for using the correct
49 * If automatic CG/mass calculation is used, then the CG is assumed to be located at
50 * the center of the motor casing and the mass is calculated from the thrust curve
51 * by assuming a constant exhaust velocity.
53 * @param reader the source of the file.
54 * @return a list of the {@link Motor} objects defined in the file.
55 * @throws IOException if an I/O error occurs or if the file format is invalid.
58 public List<Motor> load(Reader reader, String filename) throws IOException {
59 InputSource source = new InputSource(reader);
60 RSEHandler handler = new RSEHandler();
61 WarningSet warnings = new WarningSet();
64 SimpleSAX.readXML(source, handler, warnings);
65 return handler.getMotors();
66 } catch (SAXException e) {
67 throw new IOException(e.getMessage(), e);
74 * Initial handler for the RockSim engine files.
76 private static class RSEHandler extends ElementHandler {
77 private final List<Motor> motors = new ArrayList<Motor>();
79 private RSEMotorHandler motorHandler;
81 public List<Motor> getMotors() {
86 public ElementHandler openElement(String element,
87 HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
89 if (element.equals("engine-database") ||
90 element.equals("engine-list")) {
91 // Ignore <engine-database> and <engine-list> elements
95 if (element.equals("version")) {
96 // Ignore <version> elements completely
100 if (element.equals("engine")) {
101 motorHandler = new RSEMotorHandler(attributes);
109 public void closeElement(String element, HashMap<String, String> attributes,
110 String content, WarningSet warnings) throws SAXException {
112 if (element.equals("engine")) {
113 Motor motor = motorHandler.getMotor();
121 * Handler for a RockSim engine file <motor> element.
123 private static class RSEMotorHandler extends ElementHandler {
125 private final String manufacturer;
126 private final String designation;
127 private final double[] delays;
128 private final double diameter;
129 private final double length;
130 private final double initMass;
131 private final double propMass;
132 private final Motor.Type type;
133 private boolean calculateMass;
134 private boolean calculateCG;
136 private String description = "";
138 private List<Double> time;
139 private List<Double> force;
140 private List<Double> mass;
141 private List<Double> cg;
143 private RSEMotorDataHandler dataHandler = null;
146 public RSEMotorHandler(HashMap<String, String> attributes) throws SAXException {
150 str = attributes.get("mfg");
152 throw new SAXException("Manufacturer missing");
156 str = attributes.get("code");
158 throw new SAXException("Designation missing");
159 designation = removeDelay(str);
162 ArrayList<Double> delayList = new ArrayList<Double>();
163 str = attributes.get("delays");
165 String[] split = str.split(",");
166 for (String delay : split) {
169 double d = Double.parseDouble(delay);
170 if (d >= DELAY_LIMIT)
174 } catch (NumberFormatException e) {
175 if (str.equalsIgnoreCase("P") || str.equalsIgnoreCase("plugged")) {
176 delayList.add(Motor.PLUGGED);
181 delays = new double[delayList.size()];
182 for (int i = 0; i < delayList.size(); i++) {
183 delays[i] = delayList.get(i);
187 str = attributes.get("dia");
189 throw new SAXException("Diameter missing");
191 diameter = Double.parseDouble(str) / 1000.0;
192 } catch (NumberFormatException e) {
193 throw new SAXException("Invalid diameter " + str);
197 str = attributes.get("len");
199 throw new SAXException("Length missing");
201 length = Double.parseDouble(str) / 1000.0;
202 } catch (NumberFormatException e) {
203 throw new SAXException("Invalid length " + str);
207 str = attributes.get("initWt");
209 throw new SAXException("Initial mass missing");
211 initMass = Double.parseDouble(str) / 1000.0;
212 } catch (NumberFormatException e) {
213 throw new SAXException("Invalid initial mass " + str);
217 str = attributes.get("propWt");
219 throw new SAXException("Propellant mass missing");
221 propMass = Double.parseDouble(str) / 1000.0;
222 } catch (NumberFormatException e) {
223 throw new SAXException("Invalid propellant mass " + str);
226 if (propMass > initMass) {
227 throw new SAXException("Propellant weight exceeds total weight in " +
228 "RockSim engine format");
232 str = attributes.get("Type");
233 if (str != null && str.equalsIgnoreCase("single-use")) {
234 type = Motor.Type.SINGLE;
235 } else if (str != null && str.equalsIgnoreCase("hybrid")) {
236 type = Motor.Type.HYBRID;
237 } else if (str != null && str.equalsIgnoreCase("reloadable")) {
238 type = Motor.Type.RELOAD;
240 type = Motor.Type.UNKNOWN;
244 str = attributes.get("auto-calc-mass");
245 if ("0".equals(str) || "false".equalsIgnoreCase(str)) {
246 calculateMass = false;
248 calculateMass = true;
252 str = attributes.get("auto-calc-cg");
253 if ("0".equals(str) || "false".equalsIgnoreCase(str)) {
261 public ElementHandler openElement(String element,
262 HashMap<String, String> attributes, WarningSet warnings) throws SAXException {
264 if (element.equals("comments")) {
265 return PlainTextHandler.INSTANCE;
268 if (element.equals("data")) {
269 if (dataHandler != null) {
270 throw new SAXException("Multiple data elements encountered in motor " +
273 dataHandler = new RSEMotorDataHandler();
277 warnings.add("Unknown element '" + element + "' encountered, ignoring.");
282 public void closeElement(String element, HashMap<String, String> attributes,
283 String content, WarningSet warnings) {
285 if (element.equals("comments")) {
286 if (description.length() > 0) {
287 description = description + "\n\n" + content.trim();
289 description = content.trim();
294 if (element.equals("data")) {
295 time = dataHandler.getTime();
296 force = dataHandler.getForce();
297 mass = dataHandler.getMass();
298 cg = dataHandler.getCG();
300 sortLists(time, force, mass, cg);
302 for (double d : mass) {
303 if (Double.isNaN(d)) {
304 calculateMass = true;
308 for (double d : cg) {
309 if (Double.isNaN(d)) {
318 public Motor getMotor() throws SAXException {
319 if (time == null || time.size() == 0)
320 throw new SAXException("Illegal motor data");
323 finalizeThrustCurve(time, force, mass, cg);
324 final int n = time.size();
326 if (hasIllegalValue(mass))
327 calculateMass = true;
328 if (hasIllegalValue(cg))
332 mass = calculateMass(time, force, initMass, propMass);
335 for (int i = 0; i < n; i++) {
336 cg.set(i, length / 2);
340 double[] timeArray = toArray(time);
341 double[] thrustArray = toArray(force);
342 Coordinate[] cgArray = new Coordinate[n];
343 for (int i = 0; i < n; i++) {
344 cgArray[i] = new Coordinate(cg.get(i), 0, 0, mass.get(i));
348 // Create the motor digest from all data available in the file
349 MotorDigest motorDigest = new MotorDigest();
350 motorDigest.update(DataType.TIME_ARRAY, timeArray);
351 if (!calculateMass) {
352 motorDigest.update(DataType.MASS_PER_TIME, toArray(mass));
354 motorDigest.update(DataType.MASS_SPECIFIC, initMass, initMass - propMass);
357 motorDigest.update(DataType.CG_PER_TIME, toArray(cg));
359 motorDigest.update(DataType.FORCE_PER_TIME, thrustArray);
360 // TODO: HIGH: Motor digest?
361 // final String digest = motorDigest.getDigest();
365 return new ThrustCurveMotor(Manufacturer.getManufacturer(manufacturer),
366 designation, description, type,
367 delays, diameter, length, timeArray, thrustArray, cgArray);
368 } catch (IllegalArgumentException e) {
369 throw new SAXException("Illegal motor data", e);
376 * Handler for the <data> element in a RockSim engine file motor definition.
378 private static class RSEMotorDataHandler extends ElementHandler {
380 private final List<Double> time = new ArrayList<Double>();
381 private final List<Double> force = new ArrayList<Double>();
382 private final List<Double> mass = new ArrayList<Double>();
383 private final List<Double> cg = new ArrayList<Double>();
386 public List<Double> getTime() {
390 public List<Double> getForce() {
394 public List<Double> getMass() {
398 public List<Double> getCG() {
404 public ElementHandler openElement(String element,
405 HashMap<String, String> attributes, WarningSet warnings) {
407 if (element.equals("eng-data")) {
408 return NullElementHandler.INSTANCE;
411 warnings.add("Unknown element '" + element + "' encountered, ignoring.");
416 public void closeElement(String element, HashMap<String, String> attributes,
417 String content, WarningSet warnings) throws SAXException {
419 double t = parseDouble(attributes.get("t"));
420 double f = parseDouble(attributes.get("f"));
421 double m = parseDouble(attributes.get("m")) / 1000.0;
422 double g = parseDouble(attributes.get("cg")) / 1000.0;
424 if (Double.isNaN(t) || Double.isNaN(f)) {
425 throw new SAXException("Illegal motor data point encountered");
435 private double parseDouble(String str) {
439 return Double.parseDouble(str);
440 } catch (NumberFormatException e) {
448 private static boolean hasIllegalValue(List<Double> list) {
449 for (Double d : list) {
450 if (d == null || d.isNaN() || d.isInfinite()) {
457 private static double[] toArray(List<Double> list) {
458 final int n = list.size();
459 double[] array = new double[n];
460 for (int i = 0; i < n; i++) {
461 array[i] = list.get(i);