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