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