Started adding beanish stuff - property listeners and such, first stab at a generic...
[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.SortedMap;\r
6 import java.util.TreeMap;\r
7 \r
8 import javax.measure.quantity.Area;\r
9 import javax.measure.quantity.Dimensionless;\r
10 import javax.measure.quantity.Duration;\r
11 import javax.measure.quantity.Force;\r
12 import javax.measure.quantity.Length;\r
13 import javax.measure.quantity.Mass;\r
14 import javax.measure.quantity.MassFlowRate;\r
15 import javax.measure.quantity.Pressure;\r
16 import javax.measure.quantity.Temperature;\r
17 import javax.measure.quantity.Velocity;\r
18 import javax.measure.quantity.Volume;\r
19 import javax.measure.quantity.VolumetricDensity;\r
20 import javax.measure.unit.SI;\r
21 \r
22 import org.apache.log4j.Logger;\r
23 import org.jscience.physics.amount.Amount;\r
24 import org.jscience.physics.amount.Constants;\r
25 \r
26 import com.billkuker.rocketry.motorsim.fuel.KNSU;\r
27 import com.billkuker.rocketry.motorsim.grain.CompoundGrain;\r
28 import com.billkuker.rocketry.motorsim.grain.CoredCylindricalGrain;\r
29 import com.billkuker.rocketry.motorsim.grain.MultiGrain;\r
30 import com.billkuker.rocketry.motorsim.visual.BurnPanel;\r
31 \r
32 public class Burn {\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         \r
40         private static double combustionEfficency = 0.97;\r
41         \r
42         private static double densityRatio = 0.96;\r
43         \r
44         public class Interval{\r
45                 Amount<Duration> time;\r
46                 public Amount<Length> regression;\r
47                 public Amount<Pressure> chamberPressure;\r
48                 Amount<Mass> chamberProduct;\r
49                 Amount<Force> thrust;\r
50 \r
51                 public String toString(){\r
52                         return time + " " + regression + " " + chamberPressure + " " + chamberProduct;\r
53                 }\r
54         }\r
55         \r
56         protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();\r
57         \r
58         public SortedMap<Amount<Duration>,Interval> getData(){\r
59                 return data;\r
60         }\r
61         \r
62         public Motor getMotor(){\r
63                 return motor;\r
64         }\r
65 \r
66         public Amount<Duration> burnTime(){\r
67                 return data.lastKey();\r
68         }\r
69         \r
70         public Burn(Motor m){\r
71                 motor = m;\r
72         }\r
73         \r
74         private void burn(){\r
75                 log.info("Starting burn...");\r
76                 \r
77                 Amount<Length> regStep = Amount.valueOf(0.0119904077, SI.MILLIMETER);\r
78 \r
79                 //if ( motor.getGrain() instanceof Grain.DiscreteRegression )\r
80                         //regStep = ((Grain.DiscreteRegression)motor.getGrain()).optimalRegressionStep();\r
81                 \r
82                 Interval initial = new Interval();\r
83                 initial.time = Amount.valueOf(0, SI.SECOND);\r
84                 initial.regression = Amount.valueOf(0, SI.MILLIMETER);\r
85                 initial.chamberPressure = atmosphereicPressure;\r
86                 initial.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);\r
87                 initial.thrust = Amount.valueOf(0, SI.NEWTON);\r
88                 \r
89                 data.put(Amount.valueOf(0, SI.SECOND), initial);\r
90                 \r
91                 for ( int i = 0; i < 5000; i++ ){\r
92 \r
93                         Interval prev = data.get(data.lastKey());\r
94                         log.debug(prev);\r
95                         log.debug("Step " + i + " ==============================");\r
96                         Interval next = new Interval();\r
97                         \r
98                         Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);\r
99                         \r
100                         log.debug("Burn Rate: " + burnRate);\r
101                         \r
102                         Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);\r
103                         \r
104                         data.put(data.lastKey().plus(dt), next);\r
105                         \r
106                         log.debug("Dt: " + dt);\r
107                         \r
108                         next.regression = prev.regression.plus(regStep);\r
109                         \r
110                         log.info("Regression: " + next.regression);\r
111                         \r
112                         next.time = prev.time.plus(dt);\r
113                         \r
114                         log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));\r
115                         \r
116                         log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));\r
117                         \r
118                         //TODO Amount<Volume> volumeBurnt = motor.getGrain().volume(prev.regression).minus(motor.getGrain().volume(next.regression));\r
119                         Amount<Volume> volumeBurnt = motor.getGrain().surfaceArea(prev.regression).times(regStep).to(Volume.UNIT);\r
120                         \r
121                         log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));\r
122                         \r
123                         Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().idealDensity().times(densityRatio)).divide(dt).to(MassFlowRate.UNIT);\r
124                         \r
125                         log.debug("Mass Gen Rate: " + mGenRate);\r
126                         \r
127                         Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().effectiveMolarWeight());\r
128                         Amount<Temperature> chamberTemp = motor.getFuel().getCombustionProduct().idealCombustionTemperature().times(combustionEfficency);\r
129                         \r
130                         Amount<MassFlowRate> mNozzle;\r
131                         {\r
132                                 Amount<Pressure> pDiff = prev.chamberPressure.minus(atmosphereicPressure);\r
133                                 \r
134                                 //pDiff = Amount.valueOf(.7342, MPA).minus(atmosphereicPressure);\r
135                                 \r
136                                 log.debug("Pdiff: " + pDiff);\r
137                                 \r
138                                 Amount<Area> aStar = motor.getNozzle().throatArea();\r
139                                 \r
140                                 double k = motor.getFuel().getCombustionProduct().ratioOfSpecificHeats();\r
141                                 \r
142                                 log.debug("K: " + k);\r
143                                 \r
144                                 double kSide = Math.sqrt(k) * Math.pow((2/(k+1)) , (((k+1)/2)/(k-1))); //Math.pow(2/k+1, (k+1)/(2*(k-1)));\r
145                                 \r
146                                 log.debug("K-Part: (good)" + kSide);\r
147                                 \r
148                                 \r
149                                 \r
150                                 //This unit conversion helps JScience to convert nozzle flow rate to\r
151                                 //kg/s a little later on I verified the conversion by hand and\r
152                                 //JScience checks it too.\r
153                                 specificGasConstant = specificGasConstant.to(\r
154                                                 SI.METER.pow(2).divide(SI.SECOND.pow(2).times(SI.KELVIN)));\r
155                                 \r
156                                 log.debug("Specific Gas Constant: (good)" + specificGasConstant);\r
157                                 \r
158                                 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();\r
159 \r
160                                 //Unit x = SI.JOULE.divide(SI.KILOGRAM).root(2);\r
161                                 \r
162                                 //sqrtPart = sqrtPart.times(Amount.valueOf(1, x));\r
163                                 \r
164                                 log.debug("Square Root Part: " + sqrtPart);\r
165                                 \r
166                                 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);\r
167                                 \r
168                                 log.debug("Nozzle Flow: " + mNozzle);\r
169                                 \r
170                                 log.debug("Nozzle Flow: " + mNozzle.to(MassFlowRate.UNIT));\r
171                                 \r
172                                 \r
173                                 \r
174                                 \r
175                         }\r
176                         \r
177                         Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);\r
178                         \r
179                         log.debug("Chamber Product rate: " + massStorageRate);\r
180                         \r
181                         next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));\r
182                         \r
183                         log.debug("Chamber Product: " + next.chamberProduct);\r
184                         \r
185                         Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);\r
186                         \r
187                         log.debug("Product Density: " + combustionProductDensity);\r
188                         \r
189                         next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);\r
190                         \r
191                         next.chamberPressure = Amount.valueOf(\r
192                                         next.chamberPressure.doubleValue(SI.PASCAL),\r
193                                         SI.PASCAL);\r
194                         \r
195                         next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().ratioOfSpecificHeats2Phase());\r
196                         \r
197                         if ( next.chamberPressure.approximates(atmosphereicPressure)){\r
198                                 log.info("Pressure at Patm on step " + i);\r
199                                 break;\r
200                         }\r
201                 }\r
202                                 \r
203         }\r
204         \r
205         public Amount<Pressure> pressure(Amount<Duration> time){\r
206                 return data.get(time).chamberPressure;\r
207         }\r
208         \r
209         public Amount<Force> thrust(Amount<Duration> time){\r
210                 return data.get(time).thrust;\r
211         }\r
212         \r
213         public Amount<Dimensionless> kn(Amount<Length> regression){\r
214                 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);\r
215         }\r
216         \r
217         public static void main( String args[]) throws Exception{\r
218                 Motor m = new Motor();\r
219                 m.setFuel(new KNSU());\r
220                 \r
221                 CylindricalChamber c = new CylindricalChamber();\r
222                 c.setLength(Amount.valueOf(200, SI.MILLIMETER));\r
223                 c.setID(Amount.valueOf(30, SI.MILLIMETER));\r
224                 m.setChamber(c);\r
225                 \r
226                 CoredCylindricalGrain g = new CoredCylindricalGrain();\r
227                 g.setLength(Amount.valueOf(70, SI.MILLIMETER));\r
228                 g.setOD(Amount.valueOf(30, SI.MILLIMETER));\r
229                 g.setID(Amount.valueOf(10, SI.MILLIMETER));\r
230                 m.setGrain(g);\r
231                 \r
232                 CoredCylindricalGrain g1 = new CoredCylindricalGrain();\r
233                 g1.setLength(Amount.valueOf(70, SI.MILLIMETER));\r
234                 g1.setOD(Amount.valueOf(30, SI.MILLIMETER));\r
235                 g1.setID(Amount.valueOf(18, SI.MILLIMETER));\r
236                 g1.inhibit(false, true, true);\r
237                 \r
238                 CoredCylindricalGrain g2 = new CoredCylindricalGrain();\r
239                 g2.setLength(Amount.valueOf(70, SI.MILLIMETER));\r
240                 g2.setOD(Amount.valueOf(12, SI.MILLIMETER));\r
241                 g2.setID(Amount.valueOf(0, SI.MILLIMETER));\r
242                 g2.inhibit(true, false, true);\r
243                 \r
244                 CompoundGrain cg = new CompoundGrain(g1, g2);\r
245                 \r
246                 m.setGrain( new MultiGrain(cg, 2) );\r
247                 \r
248                 //m.setGrain(new MultiGrain(g,2));\r
249                 \r
250                 //m.setGrain(new ExtrudedGrain());\r
251                 \r
252                 ConvergentDivergentNozzle n = new ConvergentDivergentNozzle();\r
253                 n.setThroatDiameter(Amount.valueOf(8.500, SI.MILLIMETER));\r
254                 n.setExitDiameter(Amount.valueOf(20.87, SI.MILLIMETER));\r
255                 n.setEfficiency(.87);\r
256                 m.setNozzle(n);\r
257                 \r
258                 Burn b = new Burn(m);\r
259                 \r
260                 b.burn();\r
261                 \r
262                 new BurnPanel(b).show();\r
263                 /*\r
264                 Chart<Duration, Pressure> r = new Chart<Duration, Pressure>(\r
265                                 SI.SECOND,\r
266                                 SI.MEGA(SI.PASCAL),\r
267                                 b,\r
268                                 "pressure");\r
269                 r.setDomain(b.data.keySet());\r
270                 r.show();\r
271                 \r
272                 Chart<Duration, Force> t = new Chart<Duration, Force>(\r
273                                 SI.SECOND,\r
274                                 SI.NEWTON,\r
275                                 b,\r
276                                 "thrust");\r
277                 t.setDomain(b.data.keySet());\r
278                 t.show();\r
279                 \r
280                 new GrainPanel( m.getGrain() ).show();*/\r
281                 \r
282         }\r
283 }\r