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
34 private static double combustionEfficency = 0.97;
\r
36 private static double densityRatio = 0.96;
\r
38 public class Interval{
\r
39 public Amount<Duration> time;
\r
40 public Amount<Duration> dt;
\r
41 public Amount<Length> regression;
\r
42 public Amount<Pressure> chamberPressure;
\r
43 Amount<Mass> chamberProduct;
\r
44 public Amount<Force> thrust;
\r
46 public String toString(){
\r
47 return time + " " + dt + " " + regression + " " + chamberPressure + " " + chamberProduct;
\r
51 protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();
\r
53 public SortedMap<Amount<Duration>,Interval> getData(){
\r
57 public Motor getMotor(){
\r
61 public Amount<Duration> burnTime(){
\r
62 return data.lastKey();
\r
65 public Burn(Motor m){
\r
70 private void burn(){
\r
71 log.info("Starting burn...");
\r
73 Amount<Length> regStep = Amount.valueOf(0.0119904077, SI.MILLIMETER);
\r
75 //if ( motor.getGrain() instanceof Grain.DiscreteRegression )
\r
76 //regStep = ((Grain.DiscreteRegression)motor.getGrain()).optimalRegressionStep();
\r
78 Interval initial = new Interval();
\r
79 initial.time = Amount.valueOf(0, SI.SECOND);
\r
80 initial.dt = Amount.valueOf(0, SI.SECOND);
\r
81 initial.regression = Amount.valueOf(0, SI.MILLIMETER);
\r
82 initial.chamberPressure = atmosphereicPressure;
\r
83 initial.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);
\r
84 initial.thrust = Amount.valueOf(0, SI.NEWTON);
\r
86 data.put(Amount.valueOf(0, SI.SECOND), initial);
\r
88 for ( int i = 0; i < 5000; i++ ){
\r
90 Interval prev = data.get(data.lastKey());
\r
92 log.debug("Step " + i + " ==============================");
\r
93 Interval next = new Interval();
\r
95 Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);
\r
97 log.debug("Burn Rate: " + burnRate);
\r
99 Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);
\r
102 data.put(data.lastKey().plus(dt), next);
\r
104 log.debug("Dt: " + dt);
\r
106 next.regression = prev.regression.plus(regStep);
\r
108 log.info("Regression: " + next.regression);
\r
110 next.time = prev.time.plus(dt);
\r
112 log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));
\r
114 log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));
\r
116 //TODO Amount<Volume> volumeBurnt = motor.getGrain().volume(prev.regression).minus(motor.getGrain().volume(next.regression));
\r
117 Amount<Volume> volumeBurnt = motor.getGrain().surfaceArea(prev.regression).times(regStep).to(Volume.UNIT);
\r
119 log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));
\r
121 Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().getIdealDensity().times(motor.getFuel().getDensityRatio())).divide(dt).to(MassFlowRate.UNIT);
\r
123 log.debug("Mass Gen Rate: " + mGenRate);
\r
125 Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().getEffectiveMolarWeight());
\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
132 //pDiff = Amount.valueOf(.7342, MPA).minus(atmosphereicPressure);
\r
134 log.debug("Pdiff: " + pDiff);
\r
136 Amount<Area> aStar = motor.getNozzle().throatArea();
\r
138 double k = motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats();
\r
140 log.debug("K: " + k);
\r
142 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
144 log.debug("K-Part: (good)" + kSide);
\r
148 //This unit conversion helps JScience to convert nozzle flow rate to
\r
149 //kg/s a little later on I verified the conversion by hand and
\r
150 //JScience checks it too.
\r
151 specificGasConstant = convertSpecificGasConstantUnits(specificGasConstant);
\r
153 log.debug("Specific Gas Constant: (good)" + specificGasConstant);
\r
155 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();
\r
157 //Unit x = SI.JOULE.divide(SI.KILOGRAM).root(2);
\r
159 //sqrtPart = sqrtPart.times(Amount.valueOf(1, x));
\r
161 log.debug("Square Root Part: " + sqrtPart);
\r
163 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);
\r
165 log.debug("Nozzle Flow: " + mNozzle);
\r
167 log.debug("Nozzle Flow: " + mNozzle.to(MassFlowRate.UNIT));
\r
174 Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);
\r
176 log.debug("Chamber Product rate: " + massStorageRate);
\r
178 next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));
\r
180 log.debug("Chamber Product: " + next.chamberProduct);
\r
182 Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);
\r
184 log.debug("Product Density: " + combustionProductDensity);
\r
186 next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);
\r
188 next.chamberPressure = Amount.valueOf(
\r
189 next.chamberPressure.doubleValue(SI.PASCAL),
\r
192 next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats2Phase());
\r
194 if ( next.chamberPressure.approximates(atmosphereicPressure)){
\r
195 log.info("Pressure at Patm on step " + i);
\r
202 @SuppressWarnings("unchecked")
\r
204 * This converts the units of this constant to something JScience is able
\r
205 * to work from. This conversion is unchecked at compile time, but
\r
206 * JScience keeps me honest at runtime.
\r
208 private Amount convertSpecificGasConstantUnits(Amount a){
\r
210 SI.METER.pow(2).divide(SI.SECOND.pow(2).times(SI.KELVIN)));
\r
213 public Amount<Pressure> pressure(Amount<Duration> time){
\r
214 return data.get(time).chamberPressure;
\r
217 public Amount<Force> thrust(Amount<Duration> time){
\r
218 return data.get(time).thrust;
\r
221 public Amount<Dimensionless> kn(Amount<Length> regression){
\r
222 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);
\r