/**
 *  @file WaterSSTP.h
 * Declares a ThermoPhase class consisting of pure water (see @ref thermoprops
 * and class @link Cantera::WaterSSTP WaterSSTP@endlink).
 */

// This file is part of Cantera. See License.txt in the top-level directory or
// at https://cantera.org/license.txt for license and copyright information.

#ifndef CT_WATERSSTP_H
#define CT_WATERSSTP_H

#include "SingleSpeciesTP.h"
#include "cantera/thermo/WaterPropsIAPWS.h"
#include "cantera/thermo/WaterProps.h"

namespace Cantera
{

class WaterPropsIAPWS;
class WaterProps;
//! Class for single-component water. This is designed to cover just the liquid
//! and supercritical phases of water.
/*!
 * The reference is W. Wagner, A. Pruss, "The IAPWS Formulation 1995 for the
 * Thermodynamic Properties of Ordinary Water Substance for General and
 * Scientific Use," J. Phys. Chem. Ref. Dat, 31, 387, 2002.
 *
 * ## Specification of Species Standard State Properties
 *
 * The offsets used in the steam tables are different than NIST's. They assume
 * u_liq(TP) = 0.0, s_liq(TP) = 0.0, where TP is the triple point conditions:
 *
 *      -  u(273.16, rho)    = 0.0
 *      -  s(273.16, rho)    = 0.0
 *      -  psat(273.16)      = 611.655 Pascal
 *      -  rho(273.16, psat) = 999.793 kg m-3
 *
 * These "steam table" assumptions are used by the WaterPropsIAPWS class.
 * Therefore, offsets must be calculated to make the thermodynamic properties
 * calculated within this class to be consistent with thermo properties within
 * Cantera.
 *
 * The thermodynamic base state for water is set to the NIST basis here by
 * specifying constants, #EW_Offset and #SW_Offset, one for energy quantities
 * and one for entropy quantities. The offsets are specified so that the
 * following properties hold:
 *
 * - Delta_Hfo_idealgas(298.15) = -241.826 kJ/gmol
 * - So_idealgas(298.15, 1bar)  =  188.835 J/gmolK
 *
 * (From http://webbook.nist.gov)
 *
 * The "o" here refers to a hypothetical ideal gas state. The way we achieve
 * this in practice is to evaluate at a very low pressure and then use the
 * theoretical ideal gas results to scale up to higher pressures:
 *
 * Ho(1bar) = H(P0)
 *
 * So(1bar) = S(P0) + RT ln(1bar/P0)
 *
 * ## Application within Kinetics Managers
 *
 * This is unimplemented.
 *
 * @ingroup thermoprops
 */
class WaterSSTP : public SingleSpeciesTP
{
public:
    //! Full constructor for a water phase
    /*!
     * @param inputFile String name of the input file
     * @param id        string id of the phase name
     */
    explicit WaterSSTP(const std::string& inputFile="",
                       const std::string& id="");

    virtual std::string type() const {
        return "liquid-water-IAPWS95";
    }

    virtual std::string phaseOfMatter() const;

    //! @name  Molar Thermodynamic Properties of the Solution
    //! @{

    virtual double cv_mole() const;

    //! @}
    //! @name Mechanical Equation of State Properties
    //! @{

    virtual double pressure() const;
    virtual void setPressure(double p);
    virtual double isothermalCompressibility() const;
    virtual double thermalExpansionCoeff() const;

    //! Return the derivative of the volumetric thermal expansion coefficient.
    //! Units: 1/K2.
    virtual double dthermalExpansionCoeffdT() const;

    //! @}
    //! @name Properties of the Standard State of the Species in the Solution
    //! @{

    virtual void getStandardChemPotentials(double* gss) const;
    virtual void getGibbs_RT(double* grt) const;
    virtual void getEnthalpy_RT(double* hrt) const;
    virtual void getEntropy_R(double* sr) const;
    virtual void getCp_R(double* cpr) const;
    virtual void getIntEnergy_RT(double* urt) const;

    //! @}
    //! @name Thermodynamic Values for the Species Reference State
    //!
    //! All functions in this group need to be overridden, because the
    //! m_spthermo MultiSpeciesThermo function is not adequate for the real
    //! equation of state.
    //! @{

    virtual void getEnthalpy_RT_ref(double* hrt) const;
    virtual void getGibbs_RT_ref(double* grt) const;
    virtual void getGibbs_ref(double* g) const;
    virtual void getEntropy_R_ref(double* er) const;
    virtual void getCp_R_ref(double* cprt) const;
    virtual void getStandardVolumes_ref(double* vol) const;
    //! @}

    virtual double critTemperature() const;
    virtual double critPressure() const;
    virtual double critDensity() const;

    virtual double satPressure(double t);

    virtual bool compatibleWithMultiPhase() const {
        return false;
    }

    //! Return the fraction of vapor at the current conditions
    /*!
     * Below Tcrit, this routine will always return 0, by definition of the
     * functionality of the routine. Above Tcrit, we query the density to toggle
     * between 0 and 1.
     */
    virtual double vaporFraction() const;

    //! Set the temperature of the phase
    /*!
     * The density and composition of the phase is constant during this
     * operator.
     *
     * @param temp Temperature (Kelvin)
     */
    virtual void setTemperature(const double temp);

    //! Set the density of the phase
    /*!
     * The temperature and composition of the phase is constant during this
     * operator.
     *
     * @param dens value of the density in kg m-3
     */
    virtual void setDensity(const double dens);

    virtual void initThermo();

    //! Get a pointer to a changeable WaterPropsIAPWS object
    WaterPropsIAPWS* getWater() {
        return &m_sub;
    }

    //! Get a pointer to a changeable WaterPropsIAPWS object
    WaterProps* getWaterProps() {
        return m_waterProps.get();
    }

    //! Switch that enables calculations in the gas phase
    /**
     *  Since this phase represents a liquid (or supercritical) phase, it is an
     *  error to return a gas-phase answer. The sole intended use for this
     *  member function is to check the thermodynamic consistency of the
     *  underlying WaterProps class with ideal-gas thermo functions.
     */
    void _allowGasPhase(bool flag) { m_allowGasPhase = flag; }

protected:
    //! This routine must be overridden because it is not applicable.
    void _updateThermo() const;

private:
    //! WaterPropsIAPWS that calculates the real properties of water.
    mutable WaterPropsIAPWS m_sub;

    //! Pointer to the WaterProps object
    /*!
     * This class is used to house several approximation routines for properties
     * of water. This object owns m_waterProps, and the WaterPropsIAPWS object
     * used by WaterProps is m_sub, which is defined above.
     */
    unique_ptr<WaterProps> m_waterProps;

    //! Molecular weight of Water -> %Cantera assumption
    double m_mw = 0.0;

    //! Offset constants used to obtain consistency with the NIST database.
    /*!
     *  This is added to all internal energy and enthalpy results.
     *  units = J kmol-1.
     */
    double EW_Offset = 0.0;

    //! Offset constant used to obtain consistency with NIST convention.
    /*!
     *  This is added to all internal entropy results.
     *  units = J kmol-1 K-1.
     */
    double SW_Offset = 0.0;

    //! Boolean is true if object has been properly initialized for calculation
    bool m_ready = false;

    /**
     *  Since this phase represents a liquid (or supercritical) phase, it is an
     *  error to return a gas-phase answer. However, if the below is true, then
     *  a gas-phase answer is allowed. This is used to check the thermodynamic
     *  consistency with ideal-gas thermo functions for example.
     */
    bool m_allowGasPhase = false;
};

}

#endif
