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