8d12426101b08453468cff9d2281288712d84bfe
[debian/openrocket] / core / src / net / sf / openrocket / simulation / FlightDataType.java
1 package net.sf.openrocket.simulation;
2
3 import java.util.HashMap;
4 import java.util.Locale;
5 import java.util.Map;
6
7 import net.sf.openrocket.l10n.Translator;
8 import net.sf.openrocket.startup.Application;
9 import net.sf.openrocket.unit.UnitGroup;
10
11 /**
12  * A class defining a storable simulation variable type.  This class defined numerous ready
13  * types, and allows also creating new types with any name.  When retrieving types based on
14  * a name, you should use {@link #getType(String, UnitGroup)} to return the default unit type,
15  * or a new type if the name does not currently exist.
16  * <p>
17  * Each type has a type name (description), a unit group and a priority.  The type is identified
18  * purely by its name case-insensitively.  The unit group provides the units for the type.
19  * The priority is used to order the types.  The pre-existing types are defined specific priority
20  * numbers, and other types have a default priority number that is after all other types.
21  * 
22  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23  */
24 public class FlightDataType implements Comparable<FlightDataType> {
25         private static final Translator trans = Application.getTranslator();
26         
27         /** Priority of custom-created variables */
28         private static final int DEFAULT_PRIORITY = 999;
29         
30         /** List of existing types.  MUST BE DEFINED BEFORE ANY TYPES!! */
31         private static final Map<String, FlightDataType> EXISTING_TYPES = new HashMap<String, FlightDataType>();
32         
33         
34         
35         //// Time
36         public static final FlightDataType TYPE_TIME = newType("TYPE_TIME", "t", UnitGroup.UNITS_FLIGHT_TIME, 1);
37         
38         //// Vertical position and motion
39         //// Altitude
40         public static final FlightDataType TYPE_ALTITUDE = newType("TYPE_ALTITUDE", "h", UnitGroup.UNITS_DISTANCE, 10);
41         //// Vertical velocity
42         public static final FlightDataType TYPE_VELOCITY_Z = newType("TYPE_VELOCITY_Z", "Vz", UnitGroup.UNITS_VELOCITY, 11);
43         //// Vertical acceleration
44         public static final FlightDataType TYPE_ACCELERATION_Z = newType("TYPE_ACCELERATION_Z", "Az", UnitGroup.UNITS_ACCELERATION, 12);
45         
46         
47         //// Total motion
48         //// Total velocity
49         public static final FlightDataType TYPE_VELOCITY_TOTAL = newType("TYPE_VELOCITY_TOTAL", "Vt", UnitGroup.UNITS_VELOCITY, 20);
50         //// Total acceleration
51         public static final FlightDataType TYPE_ACCELERATION_TOTAL = newType("TYPE_ACCELERATION_TOTAL", "At", UnitGroup.UNITS_ACCELERATION, 21);
52         
53         
54         //// Lateral position and motion
55         //// Position upwind
56         public static final FlightDataType TYPE_POSITION_X = newType("TYPE_POSITION_X", "Px", UnitGroup.UNITS_DISTANCE, 30);
57         //// Position parallel to wind
58         public static final FlightDataType TYPE_POSITION_Y = newType("TYPE_POSITION_Y", "Py", UnitGroup.UNITS_DISTANCE, 31);
59         //// Lateral distance
60         public static final FlightDataType TYPE_POSITION_XY = newType("TYPE_POSITION_XY", "Pl", UnitGroup.UNITS_DISTANCE, 32);
61         //// Lateral direction
62         public static final FlightDataType TYPE_POSITION_DIRECTION = newType("TYPE_POSITION_DIRECTION", "\u03b8l", UnitGroup.UNITS_ANGLE, 33);
63         //// Lateral velocity
64         public static final FlightDataType TYPE_VELOCITY_XY = newType("TYPE_VELOCITY_XY", "Vl", UnitGroup.UNITS_VELOCITY, 34);
65         //// Lateral acceleration
66         public static final FlightDataType TYPE_ACCELERATION_XY = newType("TYPE_ACCELERATION_XY", "Al", UnitGroup.UNITS_ACCELERATION, 35);
67         //// Latitude
68         public static final FlightDataType TYPE_LATITUDE = newType("TYPE_LATITUDE", "\u03c6", UnitGroup.UNITS_ANGLE, 36);
69         //// Longitude
70         public static final FlightDataType TYPE_LONGITUDE = newType("TYPE_LONGITUDE", "\u03bb", UnitGroup.UNITS_ANGLE, 37);
71         
72         //// Angular motion
73         //// Angle of attack
74         public static final FlightDataType TYPE_AOA = newType("TYPE_AOA", "\u03b1", UnitGroup.UNITS_ANGLE, 40);
75         //// Roll rate
76         public static final FlightDataType TYPE_ROLL_RATE = newType("TYPE_ROLL_RATE", "d\u03a6", UnitGroup.UNITS_ROLL, 41);
77         //// Pitch rate
78         public static final FlightDataType TYPE_PITCH_RATE = newType("TYPE_PITCH_RATE", "d\u03b8", UnitGroup.UNITS_ROLL, 42);
79         //// Yaw rate
80         public static final FlightDataType TYPE_YAW_RATE = newType("TYPE_YAW_RATE", "d\u03a8", UnitGroup.UNITS_ROLL, 43);
81         
82         
83         //// Stability information
84         //// Mass
85         public static final FlightDataType TYPE_MASS = newType("TYPE_MASS", "m", UnitGroup.UNITS_MASS, 50);
86         //// Longitudinal moment of inertia
87         public static final FlightDataType TYPE_LONGITUDINAL_INERTIA = newType("TYPE_LONGITUDINAL_INERTIA", "Il", UnitGroup.UNITS_INERTIA, 51);
88         //// Rotational moment of inertia
89         public static final FlightDataType TYPE_ROTATIONAL_INERTIA = newType("TYPE_ROTATIONAL_INERTIA", "Ir", UnitGroup.UNITS_INERTIA, 52);
90         //// CP location
91         public static final FlightDataType TYPE_CP_LOCATION = newType("TYPE_CP_LOCATION", "Cp", UnitGroup.UNITS_LENGTH, 53);
92         //// CG location
93         public static final FlightDataType TYPE_CG_LOCATION = newType("TYPE_CG_LOCATION", "Cg", UnitGroup.UNITS_LENGTH, 54);
94         //// Stability margin calibers
95         public static final FlightDataType TYPE_STABILITY = newType("TYPE_STABILITY", "S", UnitGroup.UNITS_COEFFICIENT, 55);
96         
97         
98         //// Characteristic numbers
99         //// Mach number
100         public static final FlightDataType TYPE_MACH_NUMBER = newType("TYPE_MACH_NUMBER", "M", UnitGroup.UNITS_COEFFICIENT, 60);
101         //// Reynolds number
102         public static final FlightDataType TYPE_REYNOLDS_NUMBER = newType("TYPE_REYNOLDS_NUMBER", "R", UnitGroup.UNITS_COEFFICIENT, 61);
103         
104         
105         //// Thrust and drag
106         //// Thrust
107         public static final FlightDataType TYPE_THRUST_FORCE = newType("TYPE_THRUST_FORCE", "Ft", UnitGroup.UNITS_FORCE, 70);
108         //// Drag force
109         public static final FlightDataType TYPE_DRAG_FORCE = newType("TYPE_DRAG_FORCE", "Fd", UnitGroup.UNITS_FORCE, 71);
110         //// Drag coefficient
111         public static final FlightDataType TYPE_DRAG_COEFF = newType("TYPE_DRAG_COEFF", "Cd", UnitGroup.UNITS_COEFFICIENT, 72);
112         //// Axial drag coefficient
113         public static final FlightDataType TYPE_AXIAL_DRAG_COEFF = newType("TYPE_AXIAL_DRAG_COEFF", "Cda", UnitGroup.UNITS_COEFFICIENT, 73);
114         
115         
116         ////  Component drag coefficients
117         //// Friction drag coefficient
118         public static final FlightDataType TYPE_FRICTION_DRAG_COEFF = newType("TYPE_FRICTION_DRAG_COEFF", "Cdf", UnitGroup.UNITS_COEFFICIENT, 80);
119         //// Pressure drag coefficient
120         public static final FlightDataType TYPE_PRESSURE_DRAG_COEFF = newType("TYPE_PRESSURE_DRAG_COEFF", "Cdp", UnitGroup.UNITS_COEFFICIENT, 81);
121         //// Base drag coefficient
122         public static final FlightDataType TYPE_BASE_DRAG_COEFF = newType("TYPE_BASE_DRAG_COEFF", "Cdb", UnitGroup.UNITS_COEFFICIENT, 82);
123         
124         
125         ////  Other coefficients
126         //// Normal force coefficient
127         public static final FlightDataType TYPE_NORMAL_FORCE_COEFF = newType("TYPE_NORMAL_FORCE_COEFF", "Cn", UnitGroup.UNITS_COEFFICIENT, 90);
128         //// Pitch moment coefficient
129         public static final FlightDataType TYPE_PITCH_MOMENT_COEFF = newType("TYPE_PITCH_MOMENT_COEFF", "C\u03b8", UnitGroup.UNITS_COEFFICIENT, 91);
130         //// Yaw moment coefficient
131         public static final FlightDataType TYPE_YAW_MOMENT_COEFF = newType("TYPE_YAW_MOMENT_COEFF", "C\u03c4\u03a8", UnitGroup.UNITS_COEFFICIENT, 92);
132         //// Side force coefficient
133         public static final FlightDataType TYPE_SIDE_FORCE_COEFF = newType("TYPE_SIDE_FORCE_COEFF", "C\u03c4s", UnitGroup.UNITS_COEFFICIENT, 93);
134         //// Roll moment coefficient
135         public static final FlightDataType TYPE_ROLL_MOMENT_COEFF = newType("TYPE_ROLL_MOMENT_COEFF", "C\u03c4\u03a6", UnitGroup.UNITS_COEFFICIENT, 94);
136         //// Roll forcing coefficient
137         public static final FlightDataType TYPE_ROLL_FORCING_COEFF = newType("TYPE_ROLL_FORCING_COEFF", "Cf\u03a6", UnitGroup.UNITS_COEFFICIENT, 95);
138         //// Roll damping coefficient
139         public static final FlightDataType TYPE_ROLL_DAMPING_COEFF = newType("TYPE_ROLL_DAMPING_COEFF", "C\u03b6\u03a6", UnitGroup.UNITS_COEFFICIENT, 96);
140         
141         //// Pitch damping coefficient
142         public static final FlightDataType TYPE_PITCH_DAMPING_MOMENT_COEFF = newType("TYPE_PITCH_DAMPING_MOMENT_COEFF", "C\u03b6\u03b8", UnitGroup.UNITS_COEFFICIENT, 97);
143         //// Yaw damping coefficient
144         public static final FlightDataType TYPE_YAW_DAMPING_MOMENT_COEFF = newType("TYPE_YAW_DAMPING_MOMENT_COEFF", "C\u03b6\u03a8", UnitGroup.UNITS_COEFFICIENT, 98);
145         
146         //// Coriolis acceleration
147         public static final FlightDataType TYPE_CORIOLIS_ACCELERATION = newType("TYPE_CORIOLIS_ACCELERATION", "Ac", UnitGroup.UNITS_ACCELERATION, 99);
148         
149         
150         ////  Reference length + area
151         //// Reference length
152         public static final FlightDataType TYPE_REFERENCE_LENGTH = newType("TYPE_REFERENCE_LENGTH", "Lr", UnitGroup.UNITS_LENGTH, 100);
153         //// Reference area
154         public static final FlightDataType TYPE_REFERENCE_AREA = newType("TYPE_REFERENCE_AREA", "Ar", UnitGroup.UNITS_AREA, 101);
155         
156         
157         ////  Orientation
158         //// Vertical orientation (zenith)
159         public static final FlightDataType TYPE_ORIENTATION_THETA = newType("TYPE_ORIENTATION_THETA", "\u0398", UnitGroup.UNITS_ANGLE, 106);
160         //// Lateral orientation (azimuth)
161         public static final FlightDataType TYPE_ORIENTATION_PHI = newType("TYPE_ORIENTATION_PHI", "\u03a6", UnitGroup.UNITS_ANGLE, 107);
162         
163         
164         ////  Atmospheric conditions
165         //// Wind velocity
166         public static final FlightDataType TYPE_WIND_VELOCITY = newType("TYPE_WIND_VELOCITY", "Vw", UnitGroup.UNITS_VELOCITY, 110);
167         //// Air temperature
168         public static final FlightDataType TYPE_AIR_TEMPERATURE = newType("TYPE_AIR_TEMPERATURE", "T", UnitGroup.UNITS_TEMPERATURE, 111);
169         //// Air pressure
170         public static final FlightDataType TYPE_AIR_PRESSURE = newType("TYPE_AIR_PRESSURE", "p", UnitGroup.UNITS_PRESSURE, 112);
171         //// Speed of sound
172         public static final FlightDataType TYPE_SPEED_OF_SOUND = newType("TYPE_SPEED_OF_SOUND", "Vs", UnitGroup.UNITS_VELOCITY, 113);
173         
174         ////  Simulation information
175         //// Simulation time step
176         public static final FlightDataType TYPE_TIME_STEP = newType("TYPE_TIME_STEP", "dt", UnitGroup.UNITS_TIME_STEP, 200);
177         //// Computation time
178         public static final FlightDataType TYPE_COMPUTATION_TIME = newType("TYPE_COMPUTATION_TIME", "tc", UnitGroup.UNITS_SHORT_TIME, 201);     
179         
180         // An array of all the built in types
181         public static final FlightDataType[] ALL_TYPES = { 
182                 TYPE_TIME,
183                 TYPE_ALTITUDE ,
184                 TYPE_VELOCITY_Z ,
185                 TYPE_ACCELERATION_Z, 
186                 TYPE_VELOCITY_TOTAL, 
187                 TYPE_ACCELERATION_TOTAL, 
188                 TYPE_POSITION_X, 
189                 TYPE_POSITION_Y,  
190                 TYPE_POSITION_XY, 
191                 TYPE_POSITION_DIRECTION, 
192                 TYPE_VELOCITY_XY, 
193                 TYPE_ACCELERATION_XY, 
194                 TYPE_LATITUDE, 
195                 TYPE_LONGITUDE, 
196                 TYPE_AOA,
197                 TYPE_ROLL_RATE,
198                 TYPE_PITCH_RATE,
199                 TYPE_YAW_RATE,
200                 TYPE_MASS,
201                 TYPE_LONGITUDINAL_INERTIA,
202                 TYPE_ROTATIONAL_INERTIA,
203                 TYPE_CP_LOCATION,
204                 TYPE_CG_LOCATION,
205                 TYPE_STABILITY,
206                 TYPE_MACH_NUMBER,
207                 TYPE_REYNOLDS_NUMBER,
208                 TYPE_THRUST_FORCE,
209                 TYPE_DRAG_FORCE,
210                 TYPE_DRAG_COEFF,
211                 TYPE_AXIAL_DRAG_COEFF,
212                 TYPE_FRICTION_DRAG_COEFF,
213                 TYPE_PRESSURE_DRAG_COEFF,
214                 TYPE_BASE_DRAG_COEFF,
215                 TYPE_NORMAL_FORCE_COEFF,
216                 TYPE_PITCH_MOMENT_COEFF,
217                 TYPE_YAW_MOMENT_COEFF,
218                 TYPE_SIDE_FORCE_COEFF,
219                 TYPE_ROLL_MOMENT_COEFF,
220                 TYPE_ROLL_FORCING_COEFF,
221                 TYPE_ROLL_DAMPING_COEFF,
222                 TYPE_PITCH_DAMPING_MOMENT_COEFF,
223                 TYPE_YAW_DAMPING_MOMENT_COEFF,
224                 TYPE_CORIOLIS_ACCELERATION,
225                 TYPE_REFERENCE_LENGTH,
226                 TYPE_REFERENCE_AREA,
227                 TYPE_ORIENTATION_THETA,
228                 TYPE_ORIENTATION_PHI,
229                 TYPE_WIND_VELOCITY,
230                 TYPE_AIR_TEMPERATURE,
231                 TYPE_AIR_PRESSURE,
232                 TYPE_SPEED_OF_SOUND,
233                 TYPE_TIME_STEP,
234                 TYPE_COMPUTATION_TIME
235                 };
236         
237         /**
238          * Return a {@link FlightDataType} based on a string description.  This returns known data types
239          * if possible, or a new type otherwise.
240          * 
241          * @param s             the string description of the type.
242          * @param u             the unit group the new type should belong to if a new group is created.
243          * @return              a data type.
244          */
245         public static synchronized FlightDataType getType(String s, String symbol, UnitGroup u) {
246                 // modified to include the unit
247                 FlightDataType type = EXISTING_TYPES.get(s.toLowerCase(Locale.ENGLISH));
248                 
249                 // added this for backward compatibility. Will update type if symbol undefined
250                 //if (type != null && type.getSymbol() != symbol){
251                 //      EXISTING_TYPES.remove(type);
252                 //      type = null;
253                 //}
254                 
255                 if (type != null) {
256                         return type;
257                 }
258                 type = newType("UserDefined." + s, s, symbol, u, DEFAULT_PRIORITY);
259                 return type;
260         }
261         
262         /**
263          * Used while initializing the class.
264          */
265
266         private static FlightDataType newType( String key , String symbol, UnitGroup u, int priority ) {
267                 String name = trans.get("FlightDataType." + key );
268                 return newType( key, name, symbol, u, priority );
269         }
270         
271         private static synchronized FlightDataType newType(String key, String s, String symbol, UnitGroup u, int priority) {
272                 FlightDataType type = new FlightDataType(key, s, symbol, u, priority);
273                 EXISTING_TYPES.put(s.toLowerCase(Locale.ENGLISH), type);
274                 return type;
275         }
276         
277         private final String key;
278         private final String name;
279         private final String symbol;
280         private final UnitGroup units;
281         private final int priority;
282         private final int hashCode;
283         
284         
285         private FlightDataType(String key, String typeName, String symbol, UnitGroup units, int priority) {
286                 this.key = key;
287                 if (typeName == null)
288                         throw new IllegalArgumentException("typeName is null");
289                 if (units == null)
290                         throw new IllegalArgumentException("units is null");
291                 this.name = typeName;
292                 this.symbol = symbol;
293                 this.units = units;
294                 this.priority = priority;
295                 this.hashCode = this.key.hashCode();
296         }
297         
298         /*
299         public void setPriority(int p){
300                 this.priority = p;
301         }
302         */
303         
304         public String getKey() {
305                 return key;
306         }
307         
308         public String getName() {
309                 return name;
310         }
311         
312         public String getSymbol(){
313                 return symbol;
314         }
315         
316         public UnitGroup getUnitGroup() {
317                 return units;
318         }
319         
320         @Override
321         public String toString() {
322                 return name; //+" ("+symbol+") "+units.getDefaultUnit().toString();
323         }
324         
325         @Override
326         public boolean equals(Object other) {
327                 if (!(other instanceof FlightDataType))
328                         return false;
329                 return this.hashCode == other.hashCode();
330         }
331         
332         @Override
333         public int hashCode() {
334                 return hashCode;
335         }
336         
337         @Override
338         public int compareTo(FlightDataType o) {
339                 if (this.priority != o.priority)
340                         return this.priority - o.priority;
341                 return this.name.compareToIgnoreCase(o.name);
342         }
343 }