﻿//declaring the class
var Calculator = Class.create();

//	Variables
//		F 		- Rate of flow in USGPM or SCFH or Lbs./Hr.
//		Cv 		- Flow rating of valve
//		S 		- Specific gravity (relative to air or water)
//		T 		- Absolute temperature in degrees R. (460 degrees + degrees Fahrenheit)
//		P1 		- Inlet pressure, psia
//		P2 		- Downstream pressure, psia
//		P 	- Pressure drop across valve (open position), psi
//		X		- Quality of steam, %

//defining the rest of the class implementation
Calculator.prototype = {
   initialize: function() {
        // Variables
        
        this.F;
		this.Cv;
		this.S;
		this.T;
		this.P1;
		this.P2;
		this.P;
		this.X;
        
        this.GasConstantHP = 1391;  // Constant used in Gas calculations where P2 is 53% greater than P1
        this.GasConstantLP = 695.4;   // Constant used in Gas calculations where P2 is 53% less than P1
        this.GasConstantPercnt = .53;
        this.SteamConstant1 = 3;
        this.SteamConstant2 = 2;
        this.SteamConstantPercent = .57;
   },
   // Finds Flow (F) for a liquid in system
   getLiquidFlowRate: function(Cv, P, S){
        var temp = Math.sqrt(P / S)
        temp = Cv * temp;
        if(isNaN(temp)){
            return false;
        }else{
            return temp;
        }
   },
   // Finds Flow Coefficient (Cv) of valve for a given specific gravity
   getLiquidFlowCoeff: function(P, S, F){
        var temp = Math.sqrt(P / S);
		return F / temp;
   }, 
   // Finds Pressure Change (DeltaP) across a valve
   getLiquidPressureDrop: function(Cv, F, S){
        var temp = Math.pow((F/Cv), 2);
        return S * temp;
   },
   // Finds Flow (F) for a gas in a system
   getGasFlowRateHigh: function(Cv, S, T, P2, P){
        // Finds F
        var temp = Math.sqrt((P2 * P)/(S * T));
        return this.GasConstantHP * Cv * temp;
   },
   // Finds Flow (F) for a gas in a system
   getGasFlowRateLow: function(Cv, S, T, P1){
        // Finds F
        var temp = (Cv * P1)/(Math.sqrt(S * T));
        return this.GasConstantLP * temp ;
   },
   // Finds Flow Coefficient (Cv) of a valve for a given specific gravity
   getGasFlowCoeffHigh: function(S, T, P, P2, F){
        // Finds Cv
        var temp = Math.sqrt((P2 * P)/(S * T));
        return F / (this.GasConstantHP * temp);
   },
   getGasFlowCoeffLow: function(S, T, P1, F){
        // Finds Cv
        var temp = F*(Math.sqrt(S*T));
        return temp /(this.GasConstantLP*P1);
   },
   //
   getGasPressureDrop: function(Cv, S, T, F, P2){
        // Finds DeltaP
        var temp = Math.pow((F / (this.GasConstantHP * Cv)), 2);
        return ((S*T) / P2) * temp;
   }, 
  // 
  getSteamFlowRateHigh: function(Cv, P2, P, X){
        // Finds F
        var temp = Math.sqrt((P2 * P) / X);
        return 3 * Cv * temp;
   },
   getSteamFlowRateLow: function(Cv, P1, X){
        // Finds F
        var temp = (P1 / (2 * Math.sqrt(X)));
        return 3 * Cv * temp;
   },
   //
   getSteamFlowCoeffHigh: function(F, P2, P, X){
        // Find Cv
        var temp = 3 * (Math.sqrt((P2 * P) / X));
        return F / temp;
   },
   getSteamFlowCoeffLow: function(F, P1, X){
        // Finds Cv
		return (2 * F * Math.sqrt(X)) / (3 * P1);
   },
   //
   getSteamPressureDrop: function(Cv, F, P2, X){
        // Find DeltaP
        return Math.pow(F / (3 * Cv), 2) * (X / P2);
   },
   //
   IsP2Greater:function(P1, P2, percent) {
        //Returns true if Downstream pressure (P2) is greater than a certain percentage of the inlet pressure
        if ( P2 > (P1 * percent)) {
	        return true;
        } else {
            return false;
        }    
    },
    //
    DeltaP:function(P1, P2) {
	        return P1 - P2;
    },
    //
    convertPSIA:function(P){
        return P+14.7;
    },
    //
    convertAbsTemp:function(temp){
        return temp+460;
    },
    getBackPressure:function(P, P1){
        //P1 = P1 + 14.7;
        if(P1 >= P){        
            return P1 - P;
        }else{
            return false;
        }
    },
    
    percentToDec:function(n){
        return n/100;
    
    }
        
}// End of Calculator Class