1 package net.sf.openrocket.utils;
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
9 import net.sf.openrocket.file.GeneralMotorLoader;
10 import net.sf.openrocket.file.MotorLoader;
11 import net.sf.openrocket.motor.Manufacturer;
12 import net.sf.openrocket.motor.Motor;
13 import net.sf.openrocket.motor.ThrustCurveMotor;
15 public class MotorCompare {
17 /** Maximum allowed difference in maximum thrust */
18 private static final double MAX_THRUST_MARGIN = 0.30;
19 /** Maximum allowed difference in total impulse */
20 private static final double TOTAL_IMPULSE_MARGIN = 0.20;
21 /** Maximum allowed difference in mass values */
22 private static final double MASS_MARGIN = 0.20;
24 /** Number of time points in thrust curve to compare */
25 private static final int DIVISIONS = 100;
26 /** Maximum difference in thrust for a time point to be considered invalid */
27 private static final double THRUST_MARGIN = 0.20;
28 /** Number of invalid time points allowed */
29 private static final int ALLOWED_INVALID_POINTS = 20;
31 /** Minimum number of thrust curve points allowed (incl. start and end points) */
32 private static final int MIN_POINTS = 7;
35 public static void main(String[] args) throws IOException {
36 final double maxThrust;
48 List<String> cause = new ArrayList<String>();
50 MotorLoader loader = new GeneralMotorLoader();
51 List<Motor> motors = new ArrayList<Motor>();
52 List<String> files = new ArrayList<String>();
55 System.out.printf("Files :");
56 for (String file: args) {
57 System.out.printf("\t%s", file);
60 InputStream stream = new FileInputStream(file);
61 m = loader.load(stream, file);
63 } catch (IOException e) {
65 System.out.print("(ERR:" + e.getMessage() + ")");
69 for (int i=0; i<m.size(); i++)
75 compare(motors, files);
83 public static void compare(List<Motor> motors, List<String> files) {
84 final double maxThrust;
96 List<String> cause = new ArrayList<String>();
99 if (motors.size() == 0) {
100 System.err.println("No motors loaded.");
101 System.out.println("ERROR: No motors loaded.\n");
106 if (motors.size() == 1) {
107 System.out.println("Best (ONLY): " + files.get(0));
108 System.out.println();
112 final int n = motors.size();
113 goodness = new int[n];
116 for (String s: files) {
117 System.out.print("\t"+s);
119 System.out.println();
123 System.out.printf("Designation:");
124 String des = motors.get(0).getDesignation();
125 for (Motor m: motors) {
126 System.out.printf("\t%s", m.getDesignation());
127 if (!m.getDesignation().equals(des)) {
128 cause.add("Designation");
132 System.out.println();
135 System.out.printf("Manufacture:");
136 Manufacturer mfg = motors.get(0).getManufacturer();
137 for (Motor m: motors) {
138 System.out.printf("\t%s", m.getManufacturer());
139 if (m.getManufacturer() != mfg) {
140 cause.add("Manufacturer");
144 System.out.println();
149 min = Double.MAX_VALUE;
150 System.out.printf("Max.thrust :");
151 for (Motor m: motors) {
152 double f = m.getMaxThrust();
153 System.out.printf("\t%.2f", f);
154 max = Math.max(max, f);
155 min = Math.min(min, f);
157 diff = (max-min)/min;
158 if (diff > MAX_THRUST_MARGIN) {
160 cause.add("Max thrust");
162 System.out.printf("\t(discrepancy %.1f%%)\n", 100.0*diff);
163 maxThrust = (min+max)/2;
168 min = Double.MAX_VALUE;
169 System.out.printf("Total time :");
170 for (Motor m: motors) {
171 double t = m.getTotalTime();
172 System.out.printf("\t%.2f", t);
173 max = Math.max(max, t);
174 min = Math.min(min, t);
176 diff = (max-min)/min;
177 System.out.printf("\t(discrepancy %.1f%%)\n", 100.0*diff);
183 min = Double.MAX_VALUE;
184 System.out.printf("Impulse :");
185 for (Motor m: motors) {
186 double f = m.getTotalImpulse();
187 System.out.printf("\t%.2f", f);
188 max = Math.max(max, f);
189 min = Math.min(min, f);
191 diff = (max-min)/min;
192 if (diff > TOTAL_IMPULSE_MARGIN) {
194 cause.add("Total impulse");
196 System.out.printf("\t(discrepancy %.1f%%)\n", 100.0*diff);
201 min = Double.MAX_VALUE;
202 System.out.printf("Init mass :");
203 for (Motor m: motors) {
204 double f = m.getMass(0);
205 System.out.printf("\t%.2f", f*1000);
206 max = Math.max(max, f);
207 min = Math.min(min, f);
209 diff = (max-min)/min;
210 if (diff > MASS_MARGIN) {
212 cause.add("Initial mass");
214 System.out.printf("\t(discrepancy %.1f%%)\n", 100.0*diff);
219 min = Double.MAX_VALUE;
220 System.out.printf("Empty mass :");
221 for (Motor m: motors) {
222 double f = m.getMass(Double.POSITIVE_INFINITY);
223 System.out.printf("\t%.2f", f*1000);
224 max = Math.max(max, f);
225 min = Math.min(min, f);
227 diff = (max-min)/min;
228 if (diff > MASS_MARGIN) {
230 cause.add("Empty mass");
232 System.out.printf("\t(discrepancy %.1f%%)\n", 100.0*diff);
237 System.out.printf("Delays :");
238 for (Motor m: motors) {
239 System.out.printf("\t%d", m.getStandardDelays().length);
240 maxDelays = Math.max(maxDelays, m.getStandardDelays().length);
242 System.out.println();
247 System.out.printf("Points :");
248 for (Motor m: motors) {
249 System.out.printf("\t%d", ((ThrustCurveMotor)m).getTimePoints().length);
250 maxPoints = Math.max(maxPoints, ((ThrustCurveMotor)m).getTimePoints().length);
252 System.out.println();
257 System.out.printf("Comment len:");
258 for (Motor m: motors) {
259 System.out.printf("\t%d", m.getDescription().length());
260 maxCommentLen = Math.max(maxCommentLen, m.getDescription().length());
262 System.out.println();
266 String str = "ERROR: ";
267 for (int i=0; i<cause.size(); i++) {
269 if (i < cause.size()-1)
273 System.out.println(str);
274 System.out.println();
279 int invalidPoints = 0;
280 for (int i=0; i < DIVISIONS; i++) {
281 double t = maxTime * i/(DIVISIONS-1);
282 min = Double.MAX_VALUE;
284 // System.out.printf("%.2f:", t);
285 for (Motor m: motors) {
286 double f = m.getThrust(t);
287 // System.out.printf("\t%.2f", f);
288 min = Math.min(min, f);
289 max = Math.max(max, f);
291 diff = (max-min)/maxThrust;
292 // System.out.printf("\t(diff %.1f%%)\n", diff*100);
293 if (diff > THRUST_MARGIN)
297 if (invalidPoints > ALLOWED_INVALID_POINTS) {
298 System.out.println("ERROR: " + invalidPoints + "/" + DIVISIONS
299 + " points have thrust differing over " + (THRUST_MARGIN*100) + "%");
300 System.out.println();
306 for (int i=0; i<n; i++) {
307 Motor m = motors.get(i);
308 if (m.getStandardDelays().length == maxDelays)
310 if (((ThrustCurveMotor)m).getTimePoints().length == maxPoints)
312 if (m.getDescription().length() == maxCommentLen)
314 if (files.get(i).matches(".*\\.[rR][sS][eE]$"))
318 for (int i=1; i<n; i++) {
319 if (goodness[i] > goodness[best])
324 // Verify enough points
325 int pts = ((ThrustCurveMotor)motors.get(best)).getTimePoints().length;
326 if (pts < MIN_POINTS) {
327 System.out.println("WARNING: Best has only " + pts + " data points");
330 System.out.println("Best (" + goodness[best] + "): " + files.get(best));
331 System.out.println();