language selector, bug fixed
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / domains / StabilityDomain.java
1 package net.sf.openrocket.optimization.rocketoptimization.domains;
2
3 import net.sf.openrocket.aerodynamics.AerodynamicCalculator;
4 import net.sf.openrocket.aerodynamics.BarrowmanCalculator;
5 import net.sf.openrocket.aerodynamics.FlightConditions;
6 import net.sf.openrocket.document.Simulation;
7 import net.sf.openrocket.masscalc.BasicMassCalculator;
8 import net.sf.openrocket.masscalc.MassCalculator;
9 import net.sf.openrocket.masscalc.MassCalculator.MassCalcType;
10 import net.sf.openrocket.optimization.rocketoptimization.SimulationDomain;
11 import net.sf.openrocket.rocketcomponent.Configuration;
12 import net.sf.openrocket.rocketcomponent.RocketComponent;
13 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
14 import net.sf.openrocket.util.Coordinate;
15 import net.sf.openrocket.util.MathUtil;
16 import net.sf.openrocket.util.Pair;
17 import net.sf.openrocket.util.Prefs;
18
19 /**
20  * A simulation domain that limits the requires stability of the rocket.
21  * 
22  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23  */
24 public class StabilityDomain implements SimulationDomain {
25         
26         /*
27          * TODO: HIGH:  Should this rather inspect stability during flight
28          */
29
30         private final double limit;
31         private final boolean absolute;
32         
33         
34         public StabilityDomain(double limit, boolean absolute) {
35                 this.limit = limit;
36                 this.absolute = absolute;
37         }
38         
39         
40         @Override
41         public Pair<Double, Double> getDistanceToDomain(Simulation simulation) {
42                 Coordinate cp, cg;
43                 double cpx, cgx;
44                 double reference;
45                 
46                 /*
47                  * These are instantiated each time because this class must be thread-safe.
48                  * Caching would in any case be inefficient since the rocket changes all the time.
49                  */
50                 AerodynamicCalculator aerodynamicCalculator = new BarrowmanCalculator();
51                 MassCalculator massCalculator = new BasicMassCalculator();
52                 
53
54                 Configuration configuration = simulation.getConfiguration();
55                 FlightConditions conditions = new FlightConditions(configuration);
56                 conditions.setMach(Prefs.getDefaultMach());
57                 conditions.setAOA(0);
58                 conditions.setRollRate(0);
59                 
60                 // TODO: HIGH: This re-calculates the worst theta value every time
61                 cp = aerodynamicCalculator.getWorstCP(configuration, conditions, null);
62                 cg = massCalculator.getCG(configuration, MassCalcType.LAUNCH_MASS);
63                 
64                 if (cp.weight > 0.000001)
65                         cpx = cp.x;
66                 else
67                         cpx = Double.NaN;
68                 
69                 if (cg.weight > 0.000001)
70                         cgx = cg.x;
71                 else
72                         cgx = Double.NaN;
73                 
74
75                 // Calculate the reference (absolute or relative)
76                 reference = cpx - cgx;
77                 if (!absolute) {
78                         double diameter = 0;
79                         for (RocketComponent c : configuration) {
80                                 if (c instanceof SymmetricComponent) {
81                                         double d1 = ((SymmetricComponent) c).getForeRadius() * 2;
82                                         double d2 = ((SymmetricComponent) c).getAftRadius() * 2;
83                                         diameter = MathUtil.max(diameter, d1, d2);
84                                 }
85                         }
86                         
87                         reference = (cpx - cgx) / diameter;
88                 }
89                 
90                 System.out.println("DOMAIN: limit=" + limit + " reference=" + reference + " result=" + (limit - reference));
91                 
92                 return new Pair<Double, Double>(limit - reference, reference);
93         }
94         
95 }