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
28 private static Logger log = Logger.getLogger(Burn.class);
\r
29 protected final Motor motor;
\r
31 private static final Amount<Pressure> atmosphereicPressure = Amount.valueOf(101000, SI.PASCAL);
\r
33 public class Interval{
\r
34 public Amount<Duration> time;
\r
35 public Amount<Duration> dt;
\r
36 public Amount<Length> regression;
\r
37 public Amount<Pressure> chamberPressure;
\r
38 Amount<Mass> chamberProduct;
\r
39 public Amount<Force> thrust;
\r
41 public String toString(){
\r
42 return time + " " + dt + " " + regression + " " + chamberPressure + " " + chamberProduct;
\r
46 protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();
\r
48 public SortedMap<Amount<Duration>,Interval> getData(){
\r
52 public Motor getMotor(){
\r
56 public Amount<Duration> burnTime(){
\r
57 return data.lastKey();
\r
60 public Burn(Motor m){
\r
65 private void burn(){
\r
66 log.info("Starting burn...");
\r
68 Amount<Length> regStep = Amount.valueOf(0.0119904077, SI.MILLIMETER);
\r
70 //if ( motor.getGrain() instanceof Grain.DiscreteRegression )
\r
71 //regStep = ((Grain.DiscreteRegression)motor.getGrain()).optimalRegressionStep();
\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
81 data.put(Amount.valueOf(0, SI.SECOND), initial);
\r
83 for ( int i = 0; i < 5000; i++ ){
\r
85 Interval prev = data.get(data.lastKey());
\r
87 log.debug("Step " + i + " ==============================");
\r
88 Interval next = new Interval();
\r
90 Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);
\r
92 log.debug("Burn Rate: " + burnRate);
\r
94 Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);
\r
97 data.put(data.lastKey().plus(dt), next);
\r
99 log.debug("Dt: " + dt);
\r
101 next.regression = prev.regression.plus(regStep);
\r
103 log.info("Regression: " + next.regression);
\r
105 next.time = prev.time.plus(dt);
\r
107 log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));
\r
109 log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));
\r
111 //TODO Amount<Volume> volumeBurnt = motor.getGrain().volume(prev.regression).minus(motor.getGrain().volume(next.regression));
\r
112 Amount<Volume> volumeBurnt = motor.getGrain().surfaceArea(prev.regression).times(regStep).to(Volume.UNIT);
\r
113 log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));
\r
115 Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().getIdealDensity().times(motor.getFuel().getDensityRatio())).divide(dt).to(MassFlowRate.UNIT);
\r
116 log.debug("Mass Gen Rate: " + mGenRate);
\r
118 //Calculate specific gas constant
\r
119 Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().getEffectiveMolarWeight());
\r
120 //This unit conversion helps JScience to convert nozzle flow rate to
\r
121 //kg/s a little later on I verified the conversion by hand and
\r
122 //JScience checks it too.
\r
123 specificGasConstant = convertSpecificGasConstantUnits(specificGasConstant);
\r
125 //Calculate chamber temperature
\r
126 Amount<Temperature> chamberTemp = motor.getFuel().getCombustionProduct().getIdealCombustionTemperature().times(motor.getFuel().getCombustionEfficiency());
\r
128 Amount<MassFlowRate> mNozzle;
\r
130 Amount<Pressure> pDiff = prev.chamberPressure.minus(atmosphereicPressure);
\r
131 log.debug("Pdiff: " + pDiff);
\r
132 Amount<Area> aStar = motor.getNozzle().throatArea();
\r
133 double k = motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats();
\r
134 double kSide = Math.sqrt(k) * Math.pow((2/(k+1)) , (((k+1)/2)/(k-1)));
\r
135 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();
\r
136 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);
\r
137 log.debug("Mass Exit Rate: " + mNozzle.to(MassFlowRate.UNIT));
\r
140 Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);
\r
142 log.debug("Mass Storage Rate: " + massStorageRate);
\r
144 next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));
\r
146 //Product can not go negative!
\r
147 if ( next.chamberProduct.isLessThan(Amount.valueOf(0, SI.KILOGRAM)) )
\r
148 next.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);
\r
150 log.debug("Chamber Product: " + next.chamberProduct);
\r
152 Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);
\r
154 log.debug("Product Density: " + combustionProductDensity);
\r
156 next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);
\r
158 next.chamberPressure = Amount.valueOf(
\r
159 next.chamberPressure.doubleValue(SI.PASCAL),
\r
162 next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats2Phase());
\r
164 if ( i > 100 && next.chamberPressure.approximates(atmosphereicPressure)){
\r
165 log.info("Pressure at Patm on step " + i);
\r
172 @SuppressWarnings("unchecked")
\r
174 * This converts the units of this constant to something JScience is able
\r
175 * to work from. This conversion is unchecked at compile time, but
\r
176 * JScience keeps me honest at runtime.
\r
178 private Amount convertSpecificGasConstantUnits(Amount a){
\r
180 SI.METER.pow(2).divide(SI.SECOND.pow(2).times(SI.KELVIN)));
\r
183 public Amount<Pressure> pressure(Amount<Duration> time){
\r
184 return data.get(time).chamberPressure;
\r
187 public Amount<Force> thrust(Amount<Duration> time){
\r
188 return data.get(time).thrust;
\r
191 public Amount<Dimensionless> kn(Amount<Length> regression){
\r
192 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);
\r