create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / models / atmosphere / AtmosphericConditions.java
1 package net.sf.openrocket.models.atmosphere;
2
3 import net.sf.openrocket.util.BugException;
4 import net.sf.openrocket.util.MathUtil;
5 import net.sf.openrocket.util.Monitorable;
6 import net.sf.openrocket.util.UniqueID;
7
8 public class AtmosphericConditions implements Cloneable, Monitorable {
9
10         /** Specific gas constant of dry air. */
11         public static final double R = 287.053;
12         
13         /** Specific heat ratio of air. */
14         public static final double GAMMA = 1.4;
15         
16         /** The standard air pressure (1.01325 bar). */
17         public static final double STANDARD_PRESSURE = 101325.0;
18         
19         /** The standard air temperature (20 degrees Celcius). */
20         public static final double STANDARD_TEMPERATURE = 293.15;
21         
22         
23         
24         /** Air pressure, in Pascals. */
25         private double pressure;
26         
27         /** Air temperature, in Kelvins. */
28         private double temperature;
29         
30         private int modID;
31         
32         
33         /**
34          * Construct standard atmospheric conditions.
35          */
36         public AtmosphericConditions() {
37                 this(STANDARD_TEMPERATURE, STANDARD_PRESSURE);
38         }
39         
40         /**
41          * Construct specified atmospheric conditions.
42          * 
43          * @param temperature   the temperature in Kelvins.
44          * @param pressure              the pressure in Pascals.
45          */
46         public AtmosphericConditions(double temperature, double pressure) {
47                 this.setTemperature(temperature);
48                 this.setPressure(pressure);
49                 this.modID = UniqueID.next();
50         }
51         
52         
53         
54         public double getPressure() {
55                 return pressure;
56         }
57
58         public void setPressure(double pressure) {
59                 this.pressure = pressure;
60                 this.modID = UniqueID.next();
61         }
62
63         public double getTemperature() {
64                 return temperature;
65         }
66
67         public void setTemperature(double temperature) {
68                 this.temperature = temperature;
69                 this.modID = UniqueID.next();
70         }
71
72         /**
73          * Return the current density of air for dry air.
74          * 
75          * @return   the current density of air.
76          */
77         public double getDensity() {
78                 return getPressure() / (R*getTemperature());
79         }
80         
81         
82         /**
83          * Return the current speed of sound for dry air.
84          * <p>
85          * The speed of sound is calculated using the expansion around the temperature 0 C
86          * <code> c = 331.3 + 0.606*T </code> where T is in Celcius.  The result is accurate
87          * to about 0.5 m/s for temperatures between -30 and 30 C, and within 2 m/s
88          * for temperatures between -55 and 30 C.
89          * 
90          * @return   the current speed of sound.
91          */
92         public double getMachSpeed() {
93                 return 165.77 + 0.606 * getTemperature();
94         }
95         
96         
97         /**
98          * Return the current kinematic viscosity of the air.
99          * <p>
100          * The effect of temperature on the viscosity of a gas can be computed using
101          * Sutherland's formula.  In the region of -40 ... 40 degrees Celcius the effect
102          * is highly linear, and thus a linear approximation is used in its stead.
103          * This is divided by the result of {@link #getDensity()} to achieve the
104          * kinematic viscosity.
105          * 
106          * @return      the current kinematic viscosity.
107          */
108         public double getKinematicViscosity() {
109                 double v = 3.7291e-06 + 4.9944e-08 * getTemperature();
110                 return v / getDensity();
111         }
112         
113         
114         /**
115          * Return a copy of the atmospheric conditions.
116          */
117         @Override
118         public AtmosphericConditions clone() {
119                 try {
120                         return (AtmosphericConditions) super.clone();
121                 } catch (CloneNotSupportedException e) {
122                         throw new BugException("CloneNotSupportedException encountered!");
123                 }
124         }
125         
126         @Override
127         public boolean equals(Object other) {
128                 if (this == other)
129                         return true;
130                 if (!(other instanceof AtmosphericConditions))
131                         return false;
132                 AtmosphericConditions o = (AtmosphericConditions) other;
133                 return MathUtil.equals(this.pressure, o.pressure) && MathUtil.equals(this.temperature, o.temperature);
134         }
135         
136         @Override
137         public int hashCode() {
138                 return (int) (this.pressure + this.temperature*1000);
139         }
140         
141         @Override
142         public int getModID() {
143                 return modID;
144         }
145         
146         @Override
147         public String toString() {
148                 return String.format("AtmosphericConditions[T=%.2f,P=%.2f]", getTemperature(), getPressure());
149         }
150         
151 }