1 package com.billkuker.rocketry.motorsim;
\r
5 import java.util.SortedMap;
\r
6 import java.util.TreeMap;
\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
22 import org.apache.log4j.Logger;
\r
23 import org.jscience.physics.amount.Amount;
\r
24 import org.jscience.physics.amount.Constants;
\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
36 private static Logger log = Logger.getLogger(Burn.class);
\r
37 protected final Motor motor;
\r
39 private static final Amount<Pressure> atmosphereicPressure = Amount.valueOf(101000, SI.PASCAL);
\r
42 private static double combustionEfficency = 0.97;
\r
44 private static double densityRatio = 0.96;
\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
53 public String toString(){
\r
54 return time + " " + regression + " " + chamberPressure + " " + chamberProduct;
\r
58 protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();
\r
60 public SortedMap<Amount<Duration>,Interval> getData(){
\r
64 public Motor getMotor(){
\r
68 public Amount<Duration> burnTime(){
\r
69 return data.lastKey();
\r
72 public Burn(Motor m){
\r
76 private void burn(){
\r
77 log.info("Starting burn...");
\r
79 Amount<Length> regStep = Amount.valueOf(0.0119904077, SI.MILLIMETER);
\r
81 //if ( motor.getGrain() instanceof Grain.DiscreteRegression )
\r
82 //regStep = ((Grain.DiscreteRegression)motor.getGrain()).optimalRegressionStep();
\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
91 data.put(Amount.valueOf(0, SI.SECOND), initial);
\r
93 for ( int i = 0; i < 5000; i++ ){
\r
95 Interval prev = data.get(data.lastKey());
\r
97 log.debug("Step " + i + " ==============================");
\r
98 Interval next = new Interval();
\r
100 Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);
\r
102 log.debug("Burn Rate: " + burnRate);
\r
104 Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);
\r
106 data.put(data.lastKey().plus(dt), next);
\r
108 log.debug("Dt: " + dt);
\r
110 next.regression = prev.regression.plus(regStep);
\r
112 log.info("Regression: " + next.regression);
\r
114 next.time = prev.time.plus(dt);
\r
116 log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));
\r
118 log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));
\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
123 log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));
\r
125 Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().idealDensity().times(densityRatio)).divide(dt).to(MassFlowRate.UNIT);
\r
127 log.debug("Mass Gen Rate: " + mGenRate);
\r
129 Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().effectiveMolarWeight());
\r
130 Amount<Temperature> chamberTemp = motor.getFuel().getCombustionProduct().idealCombustionTemperature().times(combustionEfficency);
\r
132 Amount<MassFlowRate> mNozzle;
\r
134 Amount<Pressure> pDiff = prev.chamberPressure.minus(atmosphereicPressure);
\r
136 //pDiff = Amount.valueOf(.7342, MPA).minus(atmosphereicPressure);
\r
138 log.debug("Pdiff: " + pDiff);
\r
140 Amount<Area> aStar = motor.getNozzle().throatArea();
\r
142 double k = motor.getFuel().getCombustionProduct().ratioOfSpecificHeats();
\r
144 log.debug("K: " + k);
\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
148 log.debug("K-Part: (good)" + kSide);
\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
158 log.debug("Specific Gas Constant: (good)" + specificGasConstant);
\r
160 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();
\r
162 //Unit x = SI.JOULE.divide(SI.KILOGRAM).root(2);
\r
164 //sqrtPart = sqrtPart.times(Amount.valueOf(1, x));
\r
166 log.debug("Square Root Part: " + sqrtPart);
\r
168 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);
\r
170 log.debug("Nozzle Flow: " + mNozzle);
\r
172 log.debug("Nozzle Flow: " + mNozzle.to(MassFlowRate.UNIT));
\r
179 Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);
\r
181 log.debug("Chamber Product rate: " + massStorageRate);
\r
183 next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));
\r
185 log.debug("Chamber Product: " + next.chamberProduct);
\r
187 Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);
\r
189 log.debug("Product Density: " + combustionProductDensity);
\r
191 next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);
\r
193 next.chamberPressure = Amount.valueOf(
\r
194 next.chamberPressure.doubleValue(SI.PASCAL),
\r
197 next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().ratioOfSpecificHeats2Phase());
\r
199 if ( next.chamberPressure.approximates(atmosphereicPressure)){
\r
200 log.info("Pressure at Patm on step " + i);
\r
207 public Amount<Pressure> pressure(Amount<Duration> time){
\r
208 return data.get(time).chamberPressure;
\r
211 public Amount<Force> thrust(Amount<Duration> time){
\r
212 return data.get(time).thrust;
\r
215 public Amount<Dimensionless> kn(Amount<Length> regression){
\r
216 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);
\r
219 public static void main( String args[]) throws Exception{
\r
220 Motor m = new Motor();
\r
221 m.setFuel(new KNSU());
\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
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
234 m.setGrain(new MultiGrain(g,2));
\r
236 //m.setGrain(new ExtrudedGrain());
\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
244 Burn b = new Burn(m);
\r
248 new BurnPanel(b).show();
\r
250 Chart<Duration, Pressure> r = new Chart<Duration, Pressure>(
\r
252 SI.MEGA(SI.PASCAL),
\r
255 r.setDomain(b.data.keySet());
\r
258 Chart<Duration, Force> t = new Chart<Duration, Force>(
\r
263 t.setDomain(b.data.keySet());
\r
266 new GrainPanel( m.getGrain() ).show();*/
\r