Test Case: An Atmosphere Modeling Tool


The preceding sections have provided an overview of some of the more significant classes and methods provided by the Java I/O libraries. Now, let's apply some of those classes and other built-in I/O capabilities to a real-life situation ”providing the I/O functionality for an atmosphere modeling tool. As with previous chapters, the important thing here is to focus on the process. Don't worry about whether you would ever use an atmosphere modeling tool.

The modeling tool computes the conditions in the Earth's atmosphere at a given altitude by interpolating the pressure and temperature between known data points. The data is from the U.S. 1976 Standard Atmosphere model. 1 The temperature interpolation is performed using a linear expression. The pressure is interpolated using either an exponential or power function. The density is calculated based on the pressure and temperature. The molar mass is assumed to be constant.

The USatm76 class source code is shown next . The USatm76 class implements the Serializable interface to indicate that a USatm76 object can be saved and restored. At the top of the code, instance variables and some constants are declared. The arrays h[] , T[] , and p[] contain the known altitude, temperature, and pressure data. The lambda coefficients are used in the interpolation process. The methods computeTemperature() and computePressure() interpolate the temperature and pressure values. The rest of the methods are used to the return the values of pressure, temperature, density, molar mass, altitude, geo-potential altitude, and labels that define the units for each quantity.

The USatm76 constructor takes two input arguments ”a String that defines whether the SI or English system of units will be used and the altitude at which the conditions will be computed. In the next few sections we will examine three ways to read the input data into the program and two ways to write out the results.

 import java.io.Serializable; public class USatm76 implements Serializable {   String units;   private double altitude, geoAltitude, tmpAltitude;   private double temperature, pressure, molarMass;   private double G = 9.80665;   private double radiusEarth = 6356766.0;   private double Rgas = 8.31432;   //  These are the known conditions at 8 different   //  altitudes. lambda[] determines how the data   //  is interpolated between points.   private double h[] = {0.0, 11000.0, 20000.0, 32000.0,                  47000.0, 51000.0, 71000.0, 84000.8520};   private double T[] = {288.15, 216.65, 216.65, 228.65,                        270.65, 270.65, 214.65, 186.946};   private double p[] = {101325.0,22631.9468,5474.8335,         868.0044, 110.9036, 66.9371, 3.95628, 0.373367};   private double lambda[] = {-0.0065, 0.0, 0.001,                           0.0028, 0.0, -0.0028, -0.002};   //  These arrays are used in the printData() method.   private String labelSI[] = {" m"," m", " K",                       " N/m^2", " kg/mole", " kg/m^3"};   private String labelEnglish[] = {" ft"," ft", " R",                    " psi", " lbm/lbmole", " lbm/ft^3"};   //  If the units are "English" the input altitude   //  is converted from feet to meters.   public USatm76(String units, double alt) {     this.units = units;     if ( units.equals("English") ) {       altitude = 0.3048*alt;       tmpAltitude = alt;     } else {       altitude = alt;     }     geoAltitude = radiusEarth*altitude/                  (radiusEarth + altitude);     molarMass = 0.0289645;     computeTemperature();     computePressure();   }   //  These methods compute temperature and pressure   //  by interpolating between the known data points.   private void computeTemperature() {     int i = 0;     if (geoAltitude > h[1]) i = 1;     if (geoAltitude > h[2]) i = 2;     if (geoAltitude > h[3]) i = 3;     if (geoAltitude > h[4]) i = 4;     if (geoAltitude > h[5]) i = 5;     if (geoAltitude > h[6]) i = 6;     if (geoAltitude > h[7]) i = 7;     temperature = T[i] + lambda[i]*                  (geoAltitude - h[i]);   }   private void computePressure() {     double AA, BB;     int i = 0;     if (geoAltitude > h[1]) i = 1;     if (geoAltitude > h[2]) i = 2;     if (geoAltitude > h[3]) i = 3;     if (geoAltitude > h[4]) i = 4;     if (geoAltitude > h[5]) i = 5;     if (geoAltitude > h[6]) i = 6;     if (geoAltitude > h[7]) i = 7;     if (lambda[i] != 0.0) {       AA = T[i]/           (T[i] + lambda[i]*(geoAltitude - h[i]));       BB = G*molarMass/(Rgas*lambda[i]);       pressure = p[i]*Math.pow(AA,BB);     } else {       AA = G*molarMass*           (geoAltitude - h[i])/(Rgas*T[i]);       pressure = p[i]*Math.exp(-AA);     }   }   //  These methods return the values of the altitude,   //  temperature, pressure, density, molar mass,   //  and number density.   public double getAltitude() {     if ( units.equals("English") ) {       return tmpAltitude;     } else {       return altitude;     }   }   //  SI units = m   //  English units = ft   public double getGeoPotentialAltitude() {     if ( units.equals("English") ) {       return 3.28084*geoAltitude;     } else {       return geoAltitude;     }   }   //  SI units = K   //  English units = R   public double getTemperature() {     if ( units.equals("English") ) {       return 1.8*temperature;     } else {       return temperature;     }   }   //  SI units = N/m^2   //  English units = lb/in^2   public double getPressure() {     if ( units.equals("English") ) {       return 1.45037e-4*pressure;     } else {       return pressure;     }   }   //  SI units = kg/m^3   //  English units = lbm/ft^3   public double getDensity() {     if ( units.equals("English") ) {       return 6.24279e-2*pressure*molarMass/                 (Rgas*temperature);     } else {       return pressure*molarMass/(Rgas*temperature);     }   }   //  SI units = kg/mole   //  English units = lbm/lbmole   public double getMolarMass() {     if ( units.equals("English") ) {       return 1000.0*molarMass;     } else {       return molarMass;     }   }   public String[] getLabels() {     if ( units.equals("English") ) {       return labelEnglish;     } else {       return labelSI;     }   } } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net