Update README.md
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / io / ENGExporter.java
1 package com.billkuker.rocketry.motorsim.io;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStream;
7 import java.text.DecimalFormat;
8 import java.text.DecimalFormatSymbols;
9 import java.text.NumberFormat;
10 import java.util.Locale;
11
12 import javax.measure.quantity.Duration;
13 import javax.measure.quantity.Force;
14 import javax.measure.quantity.Mass;
15 import javax.measure.unit.SI;
16
17 import org.jscience.physics.amount.Amount;
18
19 import com.billkuker.rocketry.motorsim.Burn;
20 import com.billkuker.rocketry.motorsim.GraphSimplifier;
21 import com.billkuker.rocketry.motorsim.ICylindricalChamber;
22
23 public class ENGExporter {
24
25         public static void export(Iterable<Burn> bb, File f) throws IOException {
26                 export(bb, new FileOutputStream(f));
27         }
28
29         public static void export(Iterable<Burn> bb, OutputStream os) throws IOException {
30                 for (Burn b : bb) {
31                         export(b, os);
32                 }
33         }
34
35         public static void export(Burn b, OutputStream os) throws IOException {
36
37                 ICylindricalChamber cha = (ICylindricalChamber) b.getMotor().getChamber();
38
39                 NumberFormat nf = new DecimalFormat("0.####", new DecimalFormatSymbols(Locale.US));
40
41                 StringBuffer out = new StringBuffer();
42
43                 out.append(";Output from Motorsim, motorsim@billkuker.com\n");
44                 out.append(";You must fill in Delays and Total Weight\n");
45                 out.append(";Name Diameter Length Delays ProWt Wt Manufacturer\n");
46                 out.append(b.getMotor().getName().replace(" ", "-") + " ");
47
48                 double dia = cha.getOD().doubleValue(SI.MILLIMETER);
49                 double len = cha.getLength().doubleValue(SI.MILLIMETER);
50
51                 Amount<Mass> prop = b.getMotor().getGrain().volume(
52                                 Amount.valueOf(0, SI.MILLIMETER)).times(
53                                 b.getMotor().getFuel().getIdealDensity().times(
54                                                 b.getMotor().getFuel().getDensityRatio())).to(
55                                 SI.KILOGRAM);
56                 double wt = prop.doubleValue(SI.KILOGRAM);
57                 
58                 double delay = b.getMotor().getEjectionDelay().doubleValue(SI.SECOND);
59                 String delayString = Integer.toString((int)delay);
60
61                 out.append(nf.format(dia) + " " + nf.format(len) + " " + delayString + "-0-0 "
62                                 + nf.format(wt) + " " + nf.format(wt + 0.1) + " MF\n");
63
64                 GraphSimplifier<Duration, Force> gs = null;
65                 try {
66                         gs = new GraphSimplifier<Duration, Force>(b, "thrust", b.getData()
67                                         .keySet().iterator());
68                 } catch (Exception e) {
69                         e.printStackTrace();
70                         return;
71                 }
72
73                 int cnt = 0;
74                 double lastTime = 0;
75                 for (Amount<Duration> t : gs.getDomain()) {
76                         cnt++;
77                         double thrust = gs.value(t).doubleValue(SI.NEWTON);
78                         if (cnt < 10 && thrust == 0.0) {
79                                 continue; // This is a hack to ignore 0 thrust early in burn
80                         }
81                         out.append("   ");
82                         out.append(nf.format(lastTime = t.doubleValue(SI.SECOND)));
83                         out.append(" ");
84                         out.append(nf.format(thrust));
85                         out.append("\n");
86                 }
87                 
88                 out.append("   ");
89                 out.append(nf.format(lastTime + 0.01));
90                 out.append(" ");
91                 out.append(nf.format(0.0));
92                 out.append("\n");
93                 out.append(";\n\n");
94
95                 os.write(out.toString().getBytes());
96         }
97 }