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