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