SafetyMutex and rocket optimization updates
[debian/openrocket] / src / net / sf / openrocket / motor / ThrustCurveMotor.java
index 10a97deb1e9e2b3de51db5ad7c665c134719d0c0..a51283eaefd341c447d32c861c807b187ce0349c 100644 (file)
@@ -4,7 +4,9 @@ import java.text.Collator;
 import java.util.Arrays;
 import java.util.Locale;
 
+import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.models.atmosphere.AtmosphericConditions;
+import net.sf.openrocket.startup.Application;
 import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.Coordinate;
 import net.sf.openrocket.util.Inertia;
@@ -12,6 +14,7 @@ import net.sf.openrocket.util.MathUtil;
 
 
 public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
+       private static final LogHelper log = Application.getLogger();
        
        public static final double MAX_THRUST = 10e6;
        
@@ -171,7 +174,12 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
        }
        
        
-
+       /**
+        * {@inheritDoc}
+        * <p>
+        * NOTE: In most cases you want to examine the motor type of the ThrustCurveMotorSet,
+        * not the ThrustCurveMotor itself.
+        */
        @Override
        public Type getMotorType() {
                return type;
@@ -272,12 +280,11 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                        throw new BugException("Could not compute burn start time, maxThrust=" + maxThrust +
                                        " limit=" + thrustLimit + " thrust=" + Arrays.toString(thrust));
                }
-               if (MathUtil.equals(thrust[pos], thrust[pos + 1])) {
+               if (MathUtil.equals(thrust[pos - 1], thrust[pos])) {
                        // For safety
-                       burnStart = (time[pos] + time[pos + 1]) / 2;
+                       burnStart = (time[pos - 1] + time[pos]) / 2;
                } else {
-                       burnStart = MathUtil.map(thrustLimit, thrust[pos], thrust[pos + 1],
-                                       time[pos], time[pos + 1]);
+                       burnStart = MathUtil.map(thrustLimit, thrust[pos - 1], thrust[pos], time[pos - 1], time[pos]);
                }
                
 
@@ -311,9 +318,9 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                        double t0 = time[pos];
                        double t1 = time[pos + 1];
                        double f0 = thrust[pos];
-                       double f1 = thrust[pos];
+                       double f1 = thrust[pos + 1];
                        
-                       totalImpulse += (f0 + f1) / 2 * (t1 - t0);
+                       totalImpulse += (t1 - t0) * (f0 + f1) / 2;
                        
                        if (t0 < burnStart && t1 > burnStart) {
                                double fStart = MathUtil.map(burnStart, t0, t1, f0, f1);
@@ -386,11 +393,12 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                private Coordinate instCG;
                
                private final double unitRotationalInertia;
-               private final double unitLongitudalInertia;
+               private final double unitLongitudinalInertia;
                
                private int modID = 0;
                
                public ThrustCurveMotorInstance() {
+                       log.debug("ThrustCurveMotor:  Creating motor instance of " + ThrustCurveMotor.this);
                        position = 0;
                        prevTime = 0;
                        instThrust = 0;
@@ -398,7 +406,7 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                        instCG = cg[0];
                        stepCG = cg[0];
                        unitRotationalInertia = Inertia.filledCylinderRotational(getDiameter() / 2);
-                       unitLongitudalInertia = Inertia.filledCylinderLongitudal(getDiameter() / 2, getLength());
+                       unitLongitudinalInertia = Inertia.filledCylinderLongitudinal(getDiameter() / 2, getLength());
                }
                
                @Override
@@ -412,8 +420,8 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                }
                
                @Override
-               public double getLongitudalInertia() {
-                       return unitLongitudalInertia * stepCG.weight;
+               public double getLongitudinalInertia() {
+                       return unitLongitudinalInertia * stepCG.weight;
                }
                
                @Override
@@ -434,22 +442,18 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                @Override
                public void step(double nextTime, double acceleration, AtmosphericConditions cond) {
                        
-                       System.out.println("MOTOR: Stepping instance " + this + " to time " + nextTime);
-                       
                        if (!(nextTime >= prevTime)) {
                                // Also catches NaN
                                throw new IllegalArgumentException("Stepping backwards in time, current=" +
                                                prevTime + " new=" + nextTime);
                        }
                        if (MathUtil.equals(prevTime, nextTime)) {
-                               System.out.println("Same time as earlier");
                                return;
                        }
                        
                        modID++;
                        
                        if (position >= time.length - 1) {
-                               System.out.println("Thrust has ended");
                                // Thrust has ended
                                prevTime = nextTime;
                                stepThrust = 0;
@@ -501,19 +505,10 @@ public class ThrustCurveMotor implements Motor, Comparable<ThrustCurveMotor> {
                        if (position < time.length - 1) {
                                nextCG = MathUtil.map(nextTime, time[position], time[position + 1],
                                                cg[position], cg[position + 1]);
-                               
-                               System.out.println("nextTime=" + nextTime +
-                                               " time[position]=" + time[position] +
-                                               " time[position+1]=" + time[position + 1] +
-                                               " mass[position]=" + cg[position].weight * 1000 +
-                                               " mass[position+1]=" + cg[position + 1].weight * 1000 +
-                                               " result=" + nextCG.weight * 1000 +
-                                               " position=" + position);
                        } else {
                                nextCG = cg[cg.length - 1];
                        }
                        stepCG = instCG.add(nextCG).multiply(0.5);
-                       System.out.println("instMass=" + instCG.weight + " nextMass=" + nextCG.weight + " stepMass=" + stepCG.weight);
                        instCG = nextCG;
                        
                        // Update time