Moved burn summary math out of GUI
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / BurnSummary.java
1 package com.billkuker.rocketry.motorsim;\r
2 \r
3 import javax.measure.quantity.Duration;\r
4 import javax.measure.quantity.Force;\r
5 import javax.measure.quantity.Pressure;\r
6 import javax.measure.unit.SI;\r
7 \r
8 import org.jscience.physics.amount.Amount;\r
9 \r
10 import com.billkuker.rocketry.motorsim.Burn.Interval;\r
11 \r
12 public class BurnSummary {\r
13 \r
14         Amount<RocketScience.Impulse> ns = Amount.valueOf(0,\r
15                         RocketScience.NEWTON_SECOND);\r
16 \r
17         Amount<Duration> thrustTime = Amount.valueOf(0, SI.SECOND);\r
18         Amount<Force> maxThrust = Amount.valueOf(0, SI.NEWTON);\r
19         Amount<Pressure> maxPressure = Amount.valueOf(0, SI.MEGA(SI.PASCAL));\r
20         Amount<Duration> isp;\r
21         \r
22         public BurnSummary(Burn b) {\r
23                 for (Interval i : b.getData().values()) {\r
24                         ns = ns.plus(i.dt.times(i.thrust));\r
25                         if (i.thrust.isGreaterThan(Amount.valueOf(0.01, SI.NEWTON))) {\r
26                                 thrustTime = thrustTime.plus(i.dt);\r
27                         }\r
28                         if (i.thrust.isGreaterThan(maxThrust))\r
29                                 maxThrust = i.thrust;\r
30                         if (i.chamberPressure.isGreaterThan(maxPressure))\r
31                                 maxPressure = i.chamberPressure;\r
32                 }\r
33                 \r
34                 isp = ns\r
35                 .divide(b\r
36                                 .getMotor()\r
37                                 .getGrain()\r
38                                 .volume(Amount.valueOf(0, SI.MILLIMETER))\r
39                                 .times(b.getMotor()\r
40                                                 .getFuel()\r
41                                                 .getIdealDensity()\r
42                                                 .times(b.getMotor().getFuel()\r
43                                                                 .getDensityRatio())))\r
44                 .to(SI.METERS_PER_SECOND)\r
45                 .divide(Amount.valueOf(9.81,\r
46                                 SI.METERS_PER_SQUARE_SECOND)).to(SI.SECOND);\r
47 \r
48         }\r
49 \r
50         public String getRating() {\r
51                 float cnf = (float) (Math.log(ns\r
52                                 .doubleValue(RocketScience.NEWTON_SECOND) / 1.25) / Math\r
53                                 .log(2));\r
54                 int cn = (int) cnf;\r
55                 float fraction = cnf - cn;\r
56                 int percent = (int) (100 * fraction);\r
57                 char cl = (char) ((int) 'A' + cn);\r
58                 \r
59                 return percent + "% "\r
60                 + new String(new char[] { cl }) + "-"\r
61                 + Math.round(averageThrust().doubleValue(SI.NEWTON));\r
62         }\r
63 \r
64         public Amount<RocketScience.Impulse> totalImpulse() {\r
65                 return ns;\r
66         }\r
67 \r
68         public Amount<Duration> thrustTime() {\r
69                 return thrustTime;\r
70         }\r
71 \r
72         public Amount<Force> maxThrust() {\r
73                 return maxThrust;\r
74         }\r
75         \r
76         public Amount<Duration> specificImpulse(){\r
77                 return isp;\r
78         }\r
79 \r
80         public Amount<Force> averageThrust() {\r
81                 Amount<Force> averageThrust = Amount.valueOf(0, SI.NEWTON);\r
82                 if (thrustTime().isGreaterThan(Amount.valueOf(0, SI.SECOND)))\r
83                         averageThrust = totalImpulse().divide(thrustTime).to(SI.NEWTON);\r
84                 return averageThrust;\r
85         }\r
86 \r
87         public Amount<Pressure> maxPressure(){\r
88                 return maxPressure;\r
89         }\r
90 \r
91 }\r