23178a33b8a58d822f3f9e559541efd355ab7275
[debian/openrocket] / core / src / net / sf / openrocket / aerodynamics / barrowman / SymmetricComponentCalc.java
1 package net.sf.openrocket.aerodynamics.barrowman;
2
3 import static net.sf.openrocket.models.atmosphere.AtmosphericConditions.GAMMA;
4 import static net.sf.openrocket.util.MathUtil.pow2;
5 import net.sf.openrocket.aerodynamics.AerodynamicForces;
6 import net.sf.openrocket.aerodynamics.BarrowmanCalculator;
7 import net.sf.openrocket.aerodynamics.FlightConditions;
8 import net.sf.openrocket.aerodynamics.WarningSet;
9 import net.sf.openrocket.rocketcomponent.BodyTube;
10 import net.sf.openrocket.rocketcomponent.RocketComponent;
11 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
12 import net.sf.openrocket.rocketcomponent.Transition;
13 import net.sf.openrocket.util.BugException;
14 import net.sf.openrocket.util.Coordinate;
15 import net.sf.openrocket.util.LinearInterpolator;
16 import net.sf.openrocket.util.MathUtil;
17 import net.sf.openrocket.util.PolyInterpolator;
18
19
20
21 /**
22  * Calculates the aerodynamic properties of a <code>SymmetricComponent</code>.
23  * <p>
24  * CP and CNa are calculated by the Barrowman method extended to account for body lift
25  * by the method presented by Galejs.  Supersonic CNa and CP are assumed to be the
26  * same as the subsonic values.
27  * 
28  * 
29  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
30  */
31 public class SymmetricComponentCalc extends RocketComponentCalc {
32         
33         public static final double BODY_LIFT_K = 1.1;
34         
35         private final double length;
36         private final double foreRadius, aftRadius;
37         private final double fineness;
38         private final Transition.Shape shape;
39         private final double param;
40         private final double frontalArea;
41         private final double fullVolume;
42         private final double planformArea, planformCenter;
43         private final double sinphi;
44         
45         public SymmetricComponentCalc(RocketComponent c) {
46                 super(c);
47                 if (!(c instanceof SymmetricComponent)) {
48                         throw new IllegalArgumentException("Illegal component type " + c);
49                 }
50                 SymmetricComponent component = (SymmetricComponent) c;
51                 
52
53                 length = component.getLength();
54                 foreRadius = component.getForeRadius();
55                 aftRadius = component.getAftRadius();
56                 
57                 fineness = length / (2 * Math.abs(aftRadius - foreRadius));
58                 fullVolume = component.getFullVolume();
59                 planformArea = component.getComponentPlanformArea();
60                 planformCenter = component.getComponentPlanformCenter();
61                 
62                 if (component instanceof BodyTube) {
63                         shape = null;
64                         param = 0;
65                         frontalArea = 0;
66                         sinphi = 0;
67                 } else if (component instanceof Transition) {
68                         shape = ((Transition) component).getType();
69                         param = ((Transition) component).getShapeParameter();
70                         frontalArea = Math.abs(Math.PI * (foreRadius * foreRadius - aftRadius * aftRadius));
71                         
72                         double r = component.getRadius(0.99 * length);
73                         sinphi = (aftRadius - r) / MathUtil.hypot(aftRadius - r, 0.01 * length);
74                 } else {
75                         throw new UnsupportedOperationException("Unknown component type " +
76                                         component.getComponentName());
77                 }
78         }
79         
80         
81         private boolean isTube = false;
82         private double cnaCache = Double.NaN;
83         private double cpCache = Double.NaN;
84         
85         
86         /**
87          * Calculates the non-axial forces produced by the fins (normal and side forces,
88          * pitch, yaw and roll moments, CP position, CNa).
89          * <p> 
90          * This method uses the Barrowman method for CP and CNa calculation and the 
91          * extension presented by Galejs for the effect of body lift.
92          * <p>
93          * The CP and CNa at supersonic speeds are assumed to be the same as those at
94          * subsonic speeds.
95          */
96         @Override
97         public void calculateNonaxialForces(FlightConditions conditions,
98                         AerodynamicForces forces, WarningSet warnings) {
99                 
100                 // Pre-calculate and store the results
101                 if (Double.isNaN(cnaCache)) {
102                         final double r0 = foreRadius;
103                         final double r1 = aftRadius;
104                         
105                         if (MathUtil.equals(r0, r1)) {
106                                 isTube = true;
107                                 cnaCache = 0;
108                         } else {
109                                 isTube = false;
110                                 
111                                 final double A0 = Math.PI * pow2(r0);
112                                 final double A1 = Math.PI * pow2(r1);
113                                 
114                                 cnaCache = 2 * (A1 - A0);
115                                 //                              System.out.println("cnaCache = " + cnaCache);
116                                 cpCache = (length * A1 - fullVolume) / (A1 - A0);
117                         }
118                 }
119                 
120                 Coordinate cp;
121                 
122                 // If fore == aft, only body lift is encountered
123                 if (isTube) {
124                         cp = getLiftCP(conditions, warnings);
125                 } else {
126                         cp = new Coordinate(cpCache, 0, 0, cnaCache * conditions.getSincAOA() /
127                                         conditions.getRefArea()).average(getLiftCP(conditions, warnings));
128                 }
129                 
130                 forces.setCP(cp);
131                 forces.setCNa(cp.weight);
132                 forces.setCN(forces.getCNa() * conditions.getAOA());
133                 forces.setCm(forces.getCN() * cp.x / conditions.getRefLength());
134                 forces.setCroll(0);
135                 forces.setCrollDamp(0);
136                 forces.setCrollForce(0);
137                 forces.setCside(0);
138                 forces.setCyaw(0);
139                 
140
141                 // Add warning on supersonic flight
142                 if (conditions.getMach() > 1.1) {
143                         warnings.add("Body calculations may not be entirely accurate at supersonic speeds.");
144                 }
145                 
146         }
147         
148         
149
150         /**
151          * Calculate the body lift effect according to Galejs.
152          */
153         protected Coordinate getLiftCP(FlightConditions conditions, WarningSet warnings) {
154                 
155                 /*
156                  * Without this extra multiplier the rocket may become unstable at apogee
157                  * when turning around, and begin oscillating horizontally.  During the flight
158                  * of the rocket this has no effect.  It is effective only when AOA > 45 deg
159                  * and the velocity is less than 15 m/s.
160                  * 
161                  * TODO: MEDIUM:  This causes an anomaly to the flight results with the CP jumping at apogee
162                  */
163                 double mul = 1;
164                 if ((conditions.getMach() < 0.05) && (conditions.getAOA() > Math.PI / 4)) {
165                         mul = pow2(conditions.getMach() / 0.05);
166                 }
167                 
168                 return new Coordinate(planformCenter, 0, 0, mul * BODY_LIFT_K * planformArea / conditions.getRefArea() *
169                                 conditions.getSinAOA() * conditions.getSincAOA()); // sin(aoa)^2 / aoa
170         }
171         
172         
173
174         private LinearInterpolator interpolator = null;
175         
176         @Override
177         public double calculatePressureDragForce(FlightConditions conditions,
178                         double stagnationCD, double baseCD, WarningSet warnings) {
179                 
180                 // Check for simple cases first
181                 if (MathUtil.equals(foreRadius, aftRadius))
182                         return 0;
183                 
184                 if (length < 0.001) {
185                         if (foreRadius < aftRadius) {
186                                 return stagnationCD * frontalArea / conditions.getRefArea();
187                         } else {
188                                 return baseCD * frontalArea / conditions.getRefArea();
189                         }
190                 }
191                 
192
193                 // Boattail drag computed directly from base drag
194                 if (aftRadius < foreRadius) {
195                         if (fineness >= 3)
196                                 return 0;
197                         double cd = baseCD * frontalArea / conditions.getRefArea();
198                         if (fineness <= 1)
199                                 return cd;
200                         return cd * (3 - fineness) / 2;
201                 }
202                 
203
204                 // All nose cones and shoulders from pre-calculated and interpolating 
205                 if (interpolator == null) {
206                         calculateNoseInterpolator();
207                 }
208                 
209                 return interpolator.getValue(conditions.getMach()) * frontalArea / conditions.getRefArea();
210         }
211         
212         
213
214         /* 
215          * Experimental values of pressure drag for different nose cone shapes with a fineness
216          * ratio of 3.  The data is taken from 'Collection of Zero-Lift Drag Data on Bodies
217          * of Revolution from Free-Flight Investigations', NASA TR-R-100, NTRS 19630004995,
218          * page 16.
219          * 
220          * This data is extrapolated for other fineness ratios.
221          */
222
223         private static final LinearInterpolator ellipsoidInterpolator = new LinearInterpolator(
224                         new double[] { 1.2, 1.25, 1.3, 1.4, 1.6, 2.0, 2.4 },
225                         new double[] { 0.110, 0.128, 0.140, 0.148, 0.152, 0.159, 0.162 /* constant */}
226                         );
227         private static final LinearInterpolator x14Interpolator = new LinearInterpolator(
228                         new double[] { 1.2, 1.3, 1.4, 1.6, 1.8, 2.2, 2.6, 3.0, 3.6 },
229                         new double[] { 0.140, 0.156, 0.169, 0.192, 0.206, 0.227, 0.241, 0.249, 0.252 }
230                         );
231         private static final LinearInterpolator x12Interpolator = new LinearInterpolator(
232                         new double[] { 0.925, 0.95, 1.0, 1.05, 1.1, 1.2, 1.3, 1.7, 2.0 },
233                         new double[] { 0, 0.014, 0.050, 0.060, 0.059, 0.081, 0.084, 0.085, 0.078 }
234                         );
235         private static final LinearInterpolator x34Interpolator = new LinearInterpolator(
236                         new double[] { 0.8, 0.9, 1.0, 1.06, 1.2, 1.4, 1.6, 2.0, 2.8, 3.4 },
237                         new double[] { 0, 0.015, 0.078, 0.121, 0.110, 0.098, 0.090, 0.084, 0.078, 0.074 }
238                         );
239         private static final LinearInterpolator vonKarmanInterpolator = new LinearInterpolator(
240                         new double[] { 0.9, 0.95, 1.0, 1.05, 1.1, 1.2, 1.4, 1.6, 2.0, 3.0 },
241                         new double[] { 0, 0.010, 0.027, 0.055, 0.070, 0.081, 0.095, 0.097, 0.091, 0.083 }
242                         );
243         private static final LinearInterpolator lvHaackInterpolator = new LinearInterpolator(
244                         new double[] { 0.9, 0.95, 1.0, 1.05, 1.1, 1.2, 1.4, 1.6, 2.0 },
245                         new double[] { 0, 0.010, 0.024, 0.066, 0.084, 0.100, 0.114, 0.117, 0.113 }
246                         );
247         private static final LinearInterpolator parabolicInterpolator = new LinearInterpolator(
248                         new double[] { 0.95, 0.975, 1.0, 1.05, 1.1, 1.2, 1.4, 1.7 },
249                         new double[] { 0, 0.016, 0.041, 0.092, 0.109, 0.119, 0.113, 0.108 }
250                         );
251         private static final LinearInterpolator parabolic12Interpolator = new LinearInterpolator(
252                         new double[] { 0.8, 0.9, 0.95, 1.0, 1.05, 1.1, 1.3, 1.5, 1.8 },
253                         new double[] { 0, 0.016, 0.042, 0.100, 0.126, 0.125, 0.100, 0.090, 0.088 }
254                         );
255         private static final LinearInterpolator parabolic34Interpolator = new LinearInterpolator(
256                         new double[] { 0.9, 0.95, 1.0, 1.05, 1.1, 1.2, 1.4, 1.7 },
257                         new double[] { 0, 0.023, 0.073, 0.098, 0.107, 0.106, 0.089, 0.082 }
258                         );
259         private static final LinearInterpolator bluntInterpolator = new LinearInterpolator();
260         static {
261                 for (double m = 0; m < 3; m += 0.05)
262                         bluntInterpolator.addPoint(m, BarrowmanCalculator.calculateStagnationCD(m));
263         }
264         
265         /**
266          * Calculate the LinearInterpolator 'interpolator'.  After this call, if can be used
267          * to get the pressure drag coefficient at any Mach number.
268          * 
269          * First, the transonic/supersonic region is computed.  For conical and ogive shapes
270          * this is calculated directly.  For other shapes, the values for fineness-ratio 3
271          * transitions are taken from the experimental values stored above (for parameterized
272          * shapes the values are interpolated between the parameter values).  These are then
273          * extrapolated to the current fineness ratio.
274          * 
275          * Finally, if the first data points in the interpolator are not zero, the subsonic
276          * region is interpolated in the form   Cd = a*M^b + Cd(M=0).
277          */
278         @SuppressWarnings("null")
279         private void calculateNoseInterpolator() {
280                 LinearInterpolator int1 = null, int2 = null;
281                 double p = 0;
282                 
283                 interpolator = new LinearInterpolator();
284                 
285
286                 /*
287                  * Take into account nose cone shape.  Conical and ogive generate the interpolator
288                  * directly.  Others store a interpolator for fineness ratio 3 into int1, or
289                  * for parameterized shapes store the bounding fineness ratio 3 interpolators into
290                  * int1 and int2 and set 0 <= p <= 1 according to the bounds. 
291                  */
292                 switch (shape) {
293                 case CONICAL:
294                         interpolator = calculateOgiveNoseInterpolator(0, sinphi); // param==0 -> conical
295                         break;
296                 
297                 case OGIVE:
298                         interpolator = calculateOgiveNoseInterpolator(param, sinphi);
299                         break;
300                 
301                 case ELLIPSOID:
302                         int1 = ellipsoidInterpolator;
303                         break;
304                 
305                 case POWER:
306                         if (param <= 0.25) {
307                                 int1 = bluntInterpolator;
308                                 int2 = x14Interpolator;
309                                 p = param * 4;
310                         } else if (param <= 0.5) {
311                                 int1 = x14Interpolator;
312                                 int2 = x12Interpolator;
313                                 p = (param - 0.25) * 4;
314                         } else if (param <= 0.75) {
315                                 int1 = x12Interpolator;
316                                 int2 = x34Interpolator;
317                                 p = (param - 0.5) * 4;
318                         } else {
319                                 int1 = x34Interpolator;
320                                 int2 = calculateOgiveNoseInterpolator(0, 1 / MathUtil.safeSqrt(1 + 4 * pow2(fineness)));
321                                 p = (param - 0.75) * 4;
322                         }
323                         break;
324                 
325                 case PARABOLIC:
326                         if (param <= 0.5) {
327                                 int1 = calculateOgiveNoseInterpolator(0, 1 / MathUtil.safeSqrt(1 + 4 * pow2(fineness)));
328                                 int2 = parabolic12Interpolator;
329                                 p = param * 2;
330                         } else if (param <= 0.75) {
331                                 int1 = parabolic12Interpolator;
332                                 int2 = parabolic34Interpolator;
333                                 p = (param - 0.5) * 4;
334                         } else {
335                                 int1 = parabolic34Interpolator;
336                                 int2 = parabolicInterpolator;
337                                 p = (param - 0.75) * 4;
338                         }
339                         break;
340                 
341                 case HAACK:
342                         int1 = vonKarmanInterpolator;
343                         int2 = lvHaackInterpolator;
344                         p = param * 3;
345                         break;
346                 
347                 default:
348                         throw new UnsupportedOperationException("Unknown transition shape: " + shape);
349                 }
350                 
351                 if (p < 0 || p > 1.00001) {
352                         throw new BugException("Inconsistent parameter value p=" + p + " shape=" + shape);
353                 }
354                 
355
356                 // Check for parameterized shape and interpolate if necessary
357                 if (int2 != null) {
358                         LinearInterpolator int3 = new LinearInterpolator();
359                         for (double m : int1.getXPoints()) {
360                                 int3.addPoint(m, p * int2.getValue(m) + (1 - p) * int1.getValue(m));
361                         }
362                         for (double m : int2.getXPoints()) {
363                                 int3.addPoint(m, p * int2.getValue(m) + (1 - p) * int1.getValue(m));
364                         }
365                         int1 = int3;
366                 }
367                 
368                 // Extrapolate for fineness ratio if necessary
369                 if (int1 != null) {
370                         double log4 = Math.log(fineness + 1) / Math.log(4);
371                         for (double m : int1.getXPoints()) {
372                                 double stag = bluntInterpolator.getValue(m);
373                                 interpolator.addPoint(m, stag * Math.pow(int1.getValue(m) / stag, log4));
374                         }
375                 }
376                 
377
378                 /*
379                  * Now the transonic/supersonic region is ok.  We still need to interpolate
380                  * the subsonic region, if the values are non-zero.
381                  */
382
383                 double min = interpolator.getXPoints()[0];
384                 double minValue = interpolator.getValue(min);
385                 if (minValue < 0.001) {
386                         // No interpolation necessary
387                         return;
388                 }
389                 
390                 double cdMach0 = 0.8 * pow2(sinphi);
391                 double minDeriv = (interpolator.getValue(min + 0.01) - minValue) / 0.01;
392                 
393                 // These should not occur, but might cause havoc for the interpolation
394                 if ((cdMach0 >= minValue - 0.01) || (minDeriv <= 0.01)) {
395                         return;
396                 }
397                 
398                 // Cd = a*M^b + cdMach0
399                 double a = minValue - cdMach0;
400                 double b = minDeriv / a;
401                 
402                 for (double m = 0; m < minValue; m += 0.05) {
403                         interpolator.addPoint(m, a * Math.pow(m, b) + cdMach0);
404                 }
405         }
406         
407         
408         private static final PolyInterpolator conicalPolyInterpolator =
409                         new PolyInterpolator(new double[] { 1.0, 1.3 }, new double[] { 1.0, 1.3 });
410         
411         private static LinearInterpolator calculateOgiveNoseInterpolator(double param,
412                         double sinphi) {
413                 LinearInterpolator interpolator = new LinearInterpolator();
414                 
415                 // In the range M = 1 ... 1.3 use polynomial approximation
416                 double cdMach1 = 2.1 * pow2(sinphi) + 0.6019 * sinphi;
417                 
418                 double[] poly = conicalPolyInterpolator.interpolator(
419                                 1.0 * sinphi, cdMach1,
420                                 4 / (GAMMA + 1) * (1 - 0.5 * cdMach1), -1.1341 * sinphi
421                                 );
422                 
423                 // Shape parameter multiplier
424                 double mul = 0.72 * pow2(param - 0.5) + 0.82;
425                 
426                 for (double m = 1; m < 1.3001; m += 0.02) {
427                         interpolator.addPoint(m, mul * PolyInterpolator.eval(m, poly));
428                 }
429                 
430                 // Above M = 1.3 use direct formula
431                 for (double m = 1.32; m < 4; m += 0.02) {
432                         interpolator.addPoint(m, mul * (2.1 * pow2(sinphi) + 0.5 * sinphi / MathUtil.safeSqrt(m * m - 1)));
433                 }
434                 
435                 return interpolator;
436         }
437         
438
439
440 }