/*
Copyright: 
Year : (c) 2023 
Author : Rene De Ren
Contact details : zn375ix3@gmail.com 
Location : The Netherlands

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
(the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The author shall be notified of any and all improvements or adaptations this software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

class Fysics{
    
    constructor(){ 

        //gasses
        this.air_density = 0; // weight of air
        this.atm_pressure = 1.01325 ; //atm pressure at sea level
        this.earth_gravity = 9.80665 ; // acceleration of standard gravity on earth in m/s2
        this.water_molar_mass = 18.01528 // g/mol
        this.num_moles_water = 6.022 * 1023; // number of moles in water
        this.water_density =  997 ; // this.water_molar_mass * this.num_moles_water // water density in kg/m3; 
        
        //load converter
        this.convert = require('./index');

        // o2 solubility curve based on pressure and temp
        this.o2Solubility = {
            1:  // abs bar
            { 
                x:[0,5,10,15,20,25,30,35,40,45,50], // temp in degrees celcius
                y:[14.6,12.8,11.3,10.1,9.1,8.3,7.6,7,6.5,6,5.6], // mg/l
            },
            2:  // abs bar
            { 
                x:[0,5,10,15,20,25,30,35,40,45,50], // temp in degrees celcius
                y:[29.2,25.5,22.6,20.2,18.2,16.5,15.2,14,12.9,12,11.3], // mg/l
            },
            4:  // abs bar
            { 
                x:[0,5,10,15,20,25,30,35,40,45,50], // temp in degrees celcius
                y:[58.4,51.1,45.1,40.3,36.4,33.1,30.3,27.9,25.9,24,22.7], // mg/l
            },
        }

    }

    /*-------------------               functions               -------------------*/ 
    //use input to calculate air density in kg/m3 is valid up to 100 degrees
    //pressure in bar RH in % and temp in degrees celcius
    // Antoine Equation is,  Log P = A - B / ( T + C )
    // D8 = temp , d7 = RH , pressure in mbar = d6
    //=1.2923*(273.15/(273.15+temp))*(((pressure*100000)-0.3783*((((MACHT(10,(8.07-(1730.63/(233.43+temp)))))*1000/760)*RH)*100))/(1.01325*100000))
    /*
    calc_air_dens(pressure,RH,temp){

        let air_density = 
        1.2923 * 
        ( 
            273.15 / ( 273.15 + temp ) 
        )
        *
        (
            (
                (
                    pressure * 100000
                ) 
                - 0.3783 * 
                ( 
                    ( 
                        ( 
                            ( 
                                Math.pow( 10, ( 8.07 - ( 1730.63 / ( 233.43 + temp) ) ) ) 
                            )
                            *1000/760
                        )
                        *RH
                    )
                    *100
                ) 
            ) 
            / (this.atm_pressure * 100000 )
        );

        return air_density ;
    }
    */

    calc_air_dens(pressure, RH, temp) {
        const Rd = 287.05; // Specific gas constant for dry air in J/(kg·K)
        const Rv = 461.495; // Specific gas constant for water vapor in J/(kg·K)
        
        // Convert temperature to Kelvin
        const T = temp + 273.15;
        
        // Antoine constants for water vapor
        const A = 8.07131;
        const B = 1730.63;
        const C = 233.426;
        
        // Calculate saturation vapor pressure (e_s) using the Antoine equation (in hPa)
        const e_s = Math.pow(10, (A - (B / (C + temp))));
        
        // Actual vapor pressure (e) in hPa
        const e = RH * e_s / 100;
        
        // Convert pressure to Pascals
        const pressurePa = this.convert(pressure).from('mbar').to('Pa');
        
        // Partial pressure of dry air (Pa)
        const p_d = pressurePa - (e * 100);
        
        // Air density (kg/m³)
        const air_density = (p_d / (Rd * T)) + ((e * 100) / (Rv * T));
        
        return air_density;
    }

    //convert height to pressure ( density => kg/m3 , height => meter) output is in bar
    heigth_to_pressure(density,height){

        //calc pressure
        let pressure = density * this.earth_gravity * height;
        //convert Pa to bar
        pressure = this.convert(pressure).from('Pa').to('mbar');

        return pressure;
    }

    //input is in meters ! 
    calc_volume(height,width,length){
        let result = 0;
        result = height * width * length;
        return result ;
    }


}

/*
let fysics = new Fysics();
console.log("converted pressure : " + fysics.heigth_to_pressure(fysics.water_density,10) + " mbar ");
console.log( "air density : " +  fysics.calc_air_dens(1.012,0,0) + " kg / m3" );

//*/

module.exports = Fysics;