1 package com.billkuker.rocketry.motorsim;
\r
5 import java.util.Date;
\r
6 import java.util.SortedMap;
\r
7 import java.util.TreeMap;
\r
9 import javax.measure.quantity.Area;
\r
10 import javax.measure.quantity.Dimensionless;
\r
11 import javax.measure.quantity.Duration;
\r
12 import javax.measure.quantity.Force;
\r
13 import javax.measure.quantity.Length;
\r
14 import javax.measure.quantity.Mass;
\r
15 import javax.measure.quantity.MassFlowRate;
\r
16 import javax.measure.quantity.Pressure;
\r
17 import javax.measure.quantity.Quantity;
\r
18 import javax.measure.quantity.Temperature;
\r
19 import javax.measure.quantity.Velocity;
\r
20 import javax.measure.quantity.Volume;
\r
21 import javax.measure.quantity.VolumetricDensity;
\r
22 import javax.measure.unit.SI;
\r
24 import org.apache.log4j.Logger;
\r
25 import org.jscience.physics.amount.Amount;
\r
26 import org.jscience.physics.amount.Constants;
\r
29 //Some constants to tune adaptive regression step
\r
30 private static final double regStepIncreaseFactor = 1.01;
\r
31 private static final double regStepDecreaseFactor = .5;
\r
32 private static final Amount<Pressure> chamberPressureMaxDelta = Amount.valueOf(.5, SI.MEGA(SI.PASCAL));
\r
34 private static Logger log = Logger.getLogger(Burn.class);
\r
35 protected final Motor motor;
\r
37 private static final Amount<Pressure> atmosphereicPressure = Amount.valueOf(101000, SI.PASCAL);
\r
39 public class Interval{
\r
40 public Amount<Duration> time;
\r
41 public Amount<Duration> dt;
\r
42 public Amount<Length> regression;
\r
43 public Amount<Pressure> chamberPressure;
\r
44 Amount<Mass> chamberProduct;
\r
45 public Amount<Force> thrust;
\r
47 public String toString(){
\r
48 return time + " " + dt + " " + regression + " " + chamberPressure + " " + chamberProduct;
\r
52 protected SortedMap<Amount<Duration>,Interval> data = new TreeMap<Amount<Duration>, Interval>();
\r
54 public SortedMap<Amount<Duration>,Interval> getData(){
\r
58 public Motor getMotor(){
\r
62 public Amount<Duration> burnTime(){
\r
63 return data.lastKey();
\r
66 public Burn(Motor m){
\r
71 private void burn(){
\r
72 log.info("Starting burn...");
\r
73 long start = new Date().getTime();
\r
75 Amount<Length> regStep = Amount.valueOf(0.01, SI.MILLIMETER);
\r
77 Interval initial = new Interval();
\r
78 initial.time = Amount.valueOf(0, SI.SECOND);
\r
79 initial.dt = Amount.valueOf(0, SI.SECOND);
\r
80 initial.regression = Amount.valueOf(0, SI.MILLIMETER);
\r
81 initial.chamberPressure = atmosphereicPressure;
\r
82 initial.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);
\r
83 initial.thrust = Amount.valueOf(0, SI.NEWTON);
\r
85 data.put(Amount.valueOf(0, SI.SECOND), initial);
\r
88 for ( int i = 0; i < 5000; i++ ) {
\r
89 assert(positive(regStep));
\r
90 regStep = regStep.times(regStepIncreaseFactor);
\r
92 Interval prev = data.get(data.lastKey());
\r
94 log.debug("Step " + i + " ==============================");
\r
95 Interval next = new Interval();
\r
97 Amount<Velocity> burnRate = motor.getFuel().burnRate(prev.chamberPressure);
\r
98 assert(positive(burnRate));
\r
100 log.debug("Burn Rate: " + burnRate);
\r
102 Amount<Duration> dt = regStep.divide(burnRate).to(Duration.UNIT);
\r
103 assert(positive(dt));
\r
108 log.debug("Dt: " + dt);
\r
110 next.regression = prev.regression.plus(regStep);
\r
111 assert(positive(next.regression));
\r
113 log.debug("Regression: " + next.regression);
\r
115 next.time = prev.time.plus(dt);
\r
117 //log.debug("Vold: " + motor.getGrain().volume(prev.regression).to(SI.MILLIMETER.pow(3)));
\r
119 //log.debug("Vnew: " + motor.getGrain().volume(next.regression).to(SI.MILLIMETER.pow(3)));
\r
121 //TODO Amount<Volume> volumeBurnt = motor.getGrain().volume(prev.regression).minus(motor.getGrain().volume(next.regression));
\r
122 Amount<Volume> volumeBurnt = motor.getGrain().surfaceArea(prev.regression).times(regStep).to(Volume.UNIT);
\r
123 assert(positive(volumeBurnt));
\r
124 //log.info("Volume Burnt: " + volumeBurnt.to(SI.MILLIMETER.pow(3)));
\r
126 Amount<MassFlowRate> mGenRate = volumeBurnt.times(motor.getFuel().getIdealDensity().times(motor.getFuel().getDensityRatio())).divide(dt).to(MassFlowRate.UNIT);
\r
127 assert(positive(mGenRate));
\r
129 //log.debug("Mass Gen Rate: " + mGenRate);
\r
131 //Calculate specific gas constant
\r
132 Amount specificGasConstant = Constants.R.divide(motor.getFuel().getCombustionProduct().getEffectiveMolarWeight());
\r
133 //This unit conversion helps JScience to convert nozzle flow rate to
\r
134 //kg/s a little later on I verified the conversion by hand and
\r
135 //JScience checks it too.
\r
136 specificGasConstant = convertSpecificGasConstantUnits(specificGasConstant);
\r
138 //Calculate chamber temperature
\r
139 Amount<Temperature> chamberTemp = motor.getFuel().getCombustionProduct().getIdealCombustionTemperature().times(motor.getFuel().getCombustionEfficiency());
\r
141 Amount<MassFlowRate> mNozzle;
\r
143 Amount<Pressure> pDiff = prev.chamberPressure.minus(atmosphereicPressure);
\r
144 //log.debug("Pdiff: " + pDiff);
\r
145 Amount<Area> aStar = motor.getNozzle().throatArea();
\r
146 double k = motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats();
\r
147 double kSide = Math.sqrt(k) * Math.pow((2/(k+1)) , (((k+1)/2)/(k-1)));
\r
148 Amount sqrtPart = specificGasConstant.times(chamberTemp).sqrt();
\r
149 mNozzle = pDiff.times(aStar).times(kSide).divide(sqrtPart).to(MassFlowRate.UNIT);
\r
150 //log.debug("Mass Exit Rate: " + mNozzle.to(MassFlowRate.UNIT));
\r
152 assert(positive(mNozzle));
\r
154 Amount<MassFlowRate> massStorageRate = mGenRate.minus(mNozzle);
\r
156 //log.debug("Mass Storage Rate: " + massStorageRate);
\r
158 next.chamberProduct = prev.chamberProduct.plus(massStorageRate.times(dt));
\r
160 //Product can not go negative!
\r
161 if ( !positive(next.chamberProduct) ){
\r
162 log.warn("ChamberProduct Negative on step " + i + "!, Adjusting regstep down and repeating step!");
\r
163 regStep = regStep.times(regStepDecreaseFactor);
\r
166 assert(positive(next.chamberProduct));
\r
167 if ( next.chamberProduct.isLessThan(Amount.valueOf(0, SI.KILOGRAM)) )
\r
168 next.chamberProduct = Amount.valueOf(0, SI.KILOGRAM);
\r
170 //log.debug("Chamber Product: " + next.chamberProduct);
\r
172 Amount<VolumetricDensity> combustionProductDensity = next.chamberProduct.divide(motor.getChamber().chamberVolume().minus(motor.getGrain().volume(next.regression))).to(VolumetricDensity.UNIT);
\r
174 log.debug("Product Density: " + combustionProductDensity);
\r
176 next.chamberPressure = combustionProductDensity.times(specificGasConstant).times(chamberTemp).plus(atmosphereicPressure).to(Pressure.UNIT);
\r
177 assert(positive(next.chamberPressure));
\r
179 next.chamberPressure = Amount.valueOf(
\r
180 next.chamberPressure.doubleValue(SI.PASCAL),
\r
183 Amount<Pressure> dp = next.chamberPressure.minus(prev.chamberPressure);
\r
184 if ( dp.abs().isGreaterThan(chamberPressureMaxDelta)){
\r
185 log.warn("DP " + dp + " too big!, Adjusting regstep down and repeating step!");
\r
186 regStep = regStep.times(regStepDecreaseFactor);
\r
190 next.thrust = motor.getNozzle().thrust(next.chamberPressure, atmosphereicPressure, atmosphereicPressure, motor.getFuel().getCombustionProduct().getRatioOfSpecificHeats2Phase());
\r
191 assert(positive(next.thrust));
\r
193 if ( i > 100 && next.chamberPressure.approximates(atmosphereicPressure)){
\r
194 log.info("Pressure at Patm on step " + i);
\r
198 data.put(data.lastKey().plus(dt), next);
\r
201 long time = new Date().getTime() - start;
\r
202 log.info("Burn took " + time + " millis.");
\r
205 @SuppressWarnings("unchecked")
\r
207 * This converts the units of this constant to something JScience is able
\r
208 * to work from. This conversion is unchecked at compile time, but
\r
209 * JScience keeps me honest at runtime.
\r
211 private Amount convertSpecificGasConstantUnits(Amount a){
\r
213 SI.METER.pow(2).divide(SI.SECOND.pow(2).times(SI.KELVIN)));
\r
216 public Amount<Pressure> pressure(Amount<Duration> time){
\r
217 return data.get(time).chamberPressure;
\r
220 public Amount<Force> thrust(Amount<Duration> time){
\r
221 return data.get(time).thrust;
\r
224 public Amount<Dimensionless> kn(Amount<Length> regression){
\r
225 return motor.getGrain().surfaceArea(regression).divide(motor.getNozzle().throatArea()).to(Dimensionless.UNIT);
\r
229 private <Q extends Quantity> boolean positive(Amount<Q> a){
\r
230 return ( a.isGreaterThan(a.minus(a)) || a.equals(a.minus(a)));
\r