Removed old discreet reg step grain interface
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / Burn.java
1 package com.billkuker.rocketry.motorsim;\r
2 \r
3 \r
4 \r
5 import java.util.Date;\r
6 import java.util.SortedMap;\r
7 import java.util.TreeMap;\r
8 \r
9 import javax.measure.quantity.Area;\r
10 import javax.measure.quantity.Dimensionless;\r
11 import javax.measure.quantity.Duration;\r
12 import javax.measure.quantity.Force;\r
13 import javax.measure.quantity.Length;\r
14 import javax.measure.quantity.Mass;\r
15 import javax.measure.quantity.MassFlowRate;\r
16 import javax.measure.quantity.Pressure;\r
17 import javax.measure.quantity.Quantity;\r
18 import javax.measure.quantity.Temperature;\r
19 import javax.measure.quantity.Velocity;\r
20 import javax.measure.quantity.Volume;\r
21 import javax.measure.quantity.VolumetricDensity;\r
22 import javax.measure.unit.SI;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 import org.jscience.physics.amount.Amount;\r
26 import org.jscience.physics.amount.Constants;\r
27 \r
28 public class Burn {\r
29         \r
30         private static Logger log = Logger.getLogger(Burn.class);\r
31         protected final Motor motor;\r
32         \r
33         private static final Amount<Pressure> atmosphereicPressure = Amount.valueOf(101000, SI.PASCAL);\r
34         \r
35         public class Interval{\r
36                 public Amount<Duration> time;\r
37                 public Amount<Duration> dt;\r
38                 public Amount<Length> regression;\r
39                 public Amount<Pressure> chamberPressure;\r
40                 Amount<Mass> chamberProduct;\r
41                 public Amount<Force> thrust;\r
42 \r
43                 public String toString(){\r
44                         return time + " " + dt + " " + regression + " " + chamberPressure + " " + chamberProduct;\r
45                 }\r
46         }\r
47         \r
48         protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();\r
49         \r
50         public SortedMap<Amount<Duration>,Interval> getData(){\r
51                 return data;\r
52         }\r
53         \r
54         public Motor getMotor(){\r
55                 return motor;\r
56         }\r
57 \r
58         public Amount<Duration> burnTime(){\r
59                 return data.lastKey();\r
60         }\r
61         \r
62         public Burn(Motor m){\r
63                 motor = m;\r
64                 burn();\r
65         }\r
66         \r
67         private void burn(){\r
68                 log.info("Starting burn...");\r
69                 long start = new Date().getTime();\r
70                 \r
71                 Amount<Length> regStep = Amount.valueOf(0.01, SI.MILLIMETER);\r
72 \r
73                 Interval initial = new Interval();\r
74                 initial.time = Amount.valueOf(0, SI.SECOND);\r
75                 initial.dt = Amount.valueOf(0, SI.SECOND);\r
76                 initial.regression = Amount.valueOf(0, SI.MILLIMETER);\r
77                 initial.chamberPressure = atmosphereicPressure;\r
78                 initial.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);\r
79                 initial.thrust = Amount.valueOf(0, SI.NEWTON);\r
80                 \r
81                 data.put(Amount.valueOf(0, SI.SECOND), initial);\r
82                 \r
83                 step:\r
84                 for ( int i = 0; i < 5000; i++ ) {\r
85                         regStep = regStep.times(1.01);\r
86                         \r
87                         Interval prev = data.get(data.lastKey());\r
88                         log.debug(prev);\r
89                         log.debug("Step " + i + " ==============================");\r
90                         Interval next = new Interval();\r
91                         \r
92                         Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);\r
93                         assert(positive(burnRate));\r
94                         \r
95                         log.debug("Burn Rate: " + burnRate);\r
96                         \r
97                         Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);\r
98                         assert(positive(dt));\r
99                         next.dt = dt;\r
100                         \r
101 \r
102                         \r
103                         log.debug("Dt: " + dt);\r
104                         \r
105                         next.regression = prev.regression.plus(regStep);\r
106                         assert(positive(next.regression));\r
107                         \r
108                         log.debug("Regression: " + next.regression);\r
109                         \r
110                         next.time = prev.time.plus(dt);\r
111                         \r
112                         //log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));\r
113                         \r
114                         //log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));\r
115                         \r
116                         //TODO Amount<Volume> volumeBurnt = motor.getGrain().volume(prev.regression).minus(motor.getGrain().volume(next.regression));\r
117                         Amount<Volume> volumeBurnt = motor.getGrain().surfaceArea(prev.regression).times(regStep).to(Volume.UNIT);\r
118                         assert(positive(volumeBurnt));\r
119                         //log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));\r
120                         \r
121                         Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().getIdealDensity().times(motor.getFuel().getDensityRatio())).divide(dt).to(MassFlowRate.UNIT);\r
122                         assert(positive(mGenRate));\r
123                         \r
124                         //log.debug("Mass Gen Rate: " + mGenRate);\r
125                         \r
126                         //Calculate specific gas constant\r
127                         Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().getEffectiveMolarWeight());\r
128                         //This unit conversion helps JScience to convert nozzle flow rate to\r
129                         //kg/s a little later on I verified the conversion by hand and\r
130                         //JScience checks it too.\r
131                         specificGasConstant = convertSpecificGasConstantUnits(specificGasConstant);\r
132                         \r
133                         //Calculate chamber temperature\r
134                         Amount<Temperature> chamberTemp = motor.getFuel().getCombustionProduct().getIdealCombustionTemperature().times(motor.getFuel().getCombustionEfficiency());\r
135                         \r
136                         Amount<MassFlowRate> mNozzle;\r
137                         {\r
138                                 Amount<Pressure> pDiff = prev.chamberPressure.minus(atmosphereicPressure);\r
139                                 //log.debug("Pdiff: " + pDiff);\r
140                                 Amount<Area> aStar = motor.getNozzle().throatArea();\r
141                                 double k = motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats();\r
142                                 double kSide = Math.sqrt(k) * Math.pow((2/(k+1)) , (((k+1)/2)/(k-1)));\r
143                                 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();\r
144                                 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);\r
145                                 //log.debug("Mass Exit Rate: " + mNozzle.to(MassFlowRate.UNIT));                \r
146                         }\r
147                         assert(positive(mNozzle));\r
148                         \r
149                         Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);\r
150                         \r
151                         //log.debug("Mass Storage Rate: " + massStorageRate);\r
152 \r
153                         next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));\r
154                         \r
155                         //Product can not go negative!\r
156                         if ( !positive(next.chamberProduct) ){\r
157                                 log.warn("ChamberProduct Negative on step " + i + "!, Adjusting regstep down and repeating step!");\r
158                                 regStep = regStep.divide(2);\r
159                                 continue step;\r
160                         }\r
161                         assert(positive(next.chamberProduct));\r
162                         if ( next.chamberProduct.isLessThan(Amount.valueOf(0, SI.KILOGRAM)) )\r
163                                 next.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);\r
164                         \r
165                         //log.debug("Chamber Product: " + next.chamberProduct);\r
166                         \r
167                         Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);\r
168                         \r
169                         log.debug("Product Density: " + combustionProductDensity);\r
170                         \r
171                         next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);\r
172                         assert(positive(next.chamberPressure));\r
173                         \r
174                         next.chamberPressure = Amount.valueOf(\r
175                                         next.chamberPressure.doubleValue(SI.PASCAL),\r
176                                         SI.PASCAL);\r
177                         \r
178                         Amount<Pressure> dp = next.chamberPressure.minus(prev.chamberPressure);\r
179                         if ( dp.abs().isGreaterThan(Amount.valueOf(.5, SI.MEGA(SI.PASCAL)))){\r
180                                 log.warn("DP " + dp + " too big!, Adjusting regstep down and repeating step!");\r
181                                 regStep = regStep.divide(2);\r
182                                 continue step;\r
183                         }\r
184                         \r
185                         next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats2Phase());\r
186                         assert(positive(next.thrust));\r
187                         \r
188                         if ( i > 100 && next.chamberPressure.approximates(atmosphereicPressure)){\r
189                                 log.info("Pressure at Patm on step " + i);\r
190                                 break;\r
191                         }\r
192                         \r
193                         data.put(data.lastKey().plus(dt), next);\r
194 \r
195                         assert(positive(regStep));\r
196                 }\r
197 \r
198                 long time = new Date().getTime() - start;\r
199                 log.info("Burn took " + time + " millis.");\r
200         }\r
201         \r
202         @SuppressWarnings("unchecked")\r
203         /*\r
204          * This converts the units of this constant to something JScience is able\r
205          * to work from. This conversion is unchecked at compile time, but\r
206          * JScience keeps me honest at runtime.\r
207          */\r
208         private Amount convertSpecificGasConstantUnits(Amount a){\r
209                 return a.to(\r
210                                 SI.METER.pow(2).divide(SI.SECOND.pow(2).times(SI.KELVIN)));\r
211         }\r
212         \r
213         public Amount<Pressure> pressure(Amount<Duration> time){\r
214                 return data.get(time).chamberPressure;\r
215         }\r
216         \r
217         public Amount<Force> thrust(Amount<Duration> time){\r
218                 return data.get(time).thrust;\r
219         }\r
220         \r
221         public Amount<Dimensionless> kn(Amount<Length> regression){\r
222                 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);\r
223         }\r
224         \r
225         \r
226         private <Q extends Quantity> boolean positive(Amount<Q> a){\r
227                 return ( a.isGreaterThan(a.minus(a)) || a.equals(a.minus(a)));\r
228         }\r
229 \r
230 }\r