Monday, March 13, 2017

Using JShell to test out Java code

In the following example, we have a method (getDryAirDensity) that expects the temperature in Celsius and pressure in hectopascals.  However, we have the data in Fahrenheit and inches of mercury!

Thankfully for JShell, it's a great resource for working with data.  Here we use JShell to make quick conversions before sending the data into the method.

jshell> // Convert Fahrenheit to Celsius
jshell> temperature = 33.3
temperature ==> 33.3
jshell> temperature = ((temperature -32)*5/9)
temperature ==> 0.7222222222222207

jshell> // Convert inHg to millibars/hectopascals(hPa)
jshell> double pressure = 30.47
pressure ==> 30.47
jshell> pressure = pressure * 33.86389
pressure ==> 1031.8327282999999

jshell> // Load air density utility (JAirDensity)
jshell> /open /code/JAirDensityUtils.jsh
jshell> double airPressure =       
        JAirDensityUtils.getDryAirDensity(temperature,pressure)
jshell> 1.3124289489747318

Take a look at rolling a dice with JShell here.

Oh yeah, here is JAirDensityUtils.jsh (place it under your /code folder):


/**
 * Java utility class that determines air density
 *
 * @author Robert James Liguori
 * @version 0.1.0
 * @since 0.1.0
 */
public class JAirDensityUtils {

    private static final double KELVIN = 273.16;

    private static final double DRY_AIR_GAS_CONSTANT = 287.058;
    private static final double HUMID_AIR_GAS_CONSTANT = 461.4964;

    private JAirDensityUtils() {

        // Prevent instantiation of this utility class
    }

    /**

     * Get the Dry Air Density using temperature and atmospheric pressure.
     * Equation implemented from:
     * http://www.brisbanehotairballooning.com.au/calculate-air-density/
     *
     * @param temperature in celcius
     * @param atmosphericPressure in millibars (a.k.a. hectoPascal (hPa)
     * @return air density in kg/m^3
     */
    public static double getDryAirDensity(Double temperature,
            Double atmosphericPressure) {
        Double airDensity
                = atmosphericPressure * 100 // convert from hPa to Pa
                / (JAirDensityUtils.DRY_AIR_GAS_CONSTANT
                * (temperature + JAirDensityUtils.KELVIN));
        return airDensity;
    }

    /**

     * Gets the humid air density using temperature, atmospheric pressure and
     * dew point using Herman Wobus's algorithm.
     *
     * @param temperature in celcius
     * @param atmosphericPressure in millibars (a.k.a. hectoPascal (hPa)
     * @param dewPoint in celcius
     * @return
     */
    public static double getHumidAirDensity(Double temperature,
            Double atmosphericPressure,
            Double dewPoint) {

        // The saturation water pressure at the dew point,

        Double c0 = 0.99999683;
        Double c1 = -0.90826951E-02;
        Double c2 = 0.78736169E-04;
        Double c3 = -0.61117958E-06;
        Double c4 = 0.43884187E-08;
        Double c5 = -0.29883885E-10;
        Double c6 = .21874425E-12;
        Double c7 = -0.17892321E-14;
        Double c8 = 0.11112018E-16;
        Double c9 = -0.30994571E-19;
        Double p = c0 + dewPoint
                * (c1 + dewPoint * (c2
                + dewPoint * (c3
                + dewPoint * (c4
                + dewPoint * (c5
                + dewPoint * (c6
                + dewPoint * (c7
                + dewPoint * (c8
                + dewPoint * (c9)))))))));
        double psatMbar = 6.1078 / Math.pow(p, 8);

        // The vapor pressure 

        double pvPascals = psatMbar * 100.0;

        // The pressure of dry air using vapor pressure and actual pressure 

        double pdPascals = (atmosphericPressure * 100) - pvPascals;

        /*

        * The air density using the equation for the density of a 
        * mixture of dry air and water vapor.
         */
        double airDensity
                = (pdPascals / (JAirDensityUtils.DRY_AIR_GAS_CONSTANT * (temperature + JAirDensityUtils.KELVIN)))
                + (pvPascals / (JAirDensityUtils.HUMID_AIR_GAS_CONSTANT * (temperature + JAirDensityUtils.KELVIN)));

        return airDensity;


    }


    /**

     * Gets the humid air density using temperature, atmospheric pressure, dew
     * point and elevation.
     *
     * @param temperature in celcius
     * @param atmosphericPressure in millibars (a.k.a. hectoPascal (hPa)
     * @param dewPoint in celcius
     * @param elevation in meters
     * @return
     */
    public static double getHumidAirDensity(Double temperature,
            Double atmosphericPressure,
            Double dewPoint,
            Double elevation) {
        throw new UnsupportedOperationException("getHumidAirDensity is not yet supported.");
    }

}


No comments:

Post a Comment

Landing Spicoli in the Wind

Many know by now that I have three Blade 200 S RC helicopters in my fleet: Speck - for training Spot - for maintenance exercises Spicol...