From 410dd699daa2e01af6e07aca8eeba63a37bfef20 Mon Sep 17 00:00:00 2001 From: Harry Moffat Date: Mon, 4 Jun 2007 18:39:07 +0000 Subject: [PATCH] Copy constructors, assignment operators, and duplicators: Extended these to the more common ThermoPhase objects. --- Cantera/src/thermo/DebyeHuckel.h | 7 ++ Cantera/src/thermo/HMWSoln.h | 39 +++++++- Cantera/src/thermo/IdealGasPhase.cpp | 127 ++++++++++++++++++++++++- Cantera/src/thermo/IdealGasPhase.h | 59 ++++++++---- Cantera/src/thermo/StoichSubstance.cpp | 72 ++++++++++++++ Cantera/src/thermo/StoichSubstance.h | 43 +++++++-- Cantera/src/thermo/SurfPhase.cpp | 85 ++++++++++++++--- Cantera/src/thermo/SurfPhase.h | 30 ++++++ Cantera/src/thermo/ThermoPhase.h | 17 ++-- 9 files changed, 422 insertions(+), 57 deletions(-) diff --git a/Cantera/src/thermo/DebyeHuckel.h b/Cantera/src/thermo/DebyeHuckel.h index a3e169b5f..b303a9285 100644 --- a/Cantera/src/thermo/DebyeHuckel.h +++ b/Cantera/src/thermo/DebyeHuckel.h @@ -639,6 +639,13 @@ namespace Cantera { virtual ~DebyeHuckel(); //! Duplicator from the ThermoPhase parent class + /*! + * Given a pointer to a ThermoPhase object, this function will + * duplicate the ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a ThermoPhase + */ ThermoPhase *duplMyselfAsThermoPhase() const; /** diff --git a/Cantera/src/thermo/HMWSoln.h b/Cantera/src/thermo/HMWSoln.h index 11d4a7ca3..51dc31b3e 100644 --- a/Cantera/src/thermo/HMWSoln.h +++ b/Cantera/src/thermo/HMWSoln.h @@ -89,15 +89,37 @@ namespace Cantera { public: - /// Constructors + /// Default Constructor HMWSoln(); - HMWSoln(const HMWSoln &); - HMWSoln& operator=(const HMWSoln&); - + //! Full constructor for setting up the entire ThermoPhase Object HMWSoln(std::string inputFile, std::string id = ""); + + //! Full constructor for setting up the entire ThermoPhase Object HMWSoln(XML_Node& phaseRef, std::string id = ""); + + //! Copy Constructor + /*! + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + HMWSoln(const HMWSoln &right); + + //! Asignment operator + /*! + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + HMWSoln& operator=(const HMWSoln& right); + /** * This is a special constructor, used to replicate test problems * during the initial verification of the object @@ -107,7 +129,14 @@ namespace Cantera { /// Destructor. virtual ~HMWSoln(); - + //! Duplicator from the ThermoPhase parent class + /*! + * Given a pointer to a ThermoPhase object, this function will + * duplicate the ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a ThermoPhase + */ ThermoPhase *duplMyselfAsThermoPhase() const; /** diff --git a/Cantera/src/thermo/IdealGasPhase.cpp b/Cantera/src/thermo/IdealGasPhase.cpp index acb10629f..7f3ac00f7 100644 --- a/Cantera/src/thermo/IdealGasPhase.cpp +++ b/Cantera/src/thermo/IdealGasPhase.cpp @@ -23,7 +23,7 @@ using namespace std; namespace Cantera { - // Empty Constructor + // Default empty Constructor IdealGasPhase::IdealGasPhase(): m_mm(0), m_tmin(0.0), @@ -34,7 +34,130 @@ namespace Cantera { { } - // Molar Thermodynamic Properties of the Solution ---------- + // Copy Constructor + IdealGasPhase::IdealGasPhase(const IdealGasPhase& right): + m_mm(right.m_mm), + m_tmin(right.m_tmin), + m_tmax(right.m_tmax), + m_p0(right.m_p0), + m_tlast(right.m_tlast), + m_logc0(right.m_logc0) + { + /* + * Use the assignment operator to do the brunt + * of the work for the copy construtor. + */ + *this = right; + } + + // Asignment operator + /* + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + IdealGasPhase& IdealGasPhase:: + operator=(const IdealGasPhase &right) { + if (&right != this) { + ThermoPhase::operator=(right); + m_mm = right.m_mm; + m_tmin = right.m_tmin; + m_tmax = right.m_tmax; + m_p0 = right.m_p0; + m_tlast = right.m_tlast; + m_logc0 = right.m_logc0; + m_h0_RT = right.m_h0_RT; + m_cp0_R = right.m_cp0_R; + m_g0_RT = right.m_g0_RT; + m_s0_R = right.m_s0_R; + m_expg0_RT= right.m_expg0_RT; + m_pe = right.m_pe; + m_pp = right.m_pp; + } + return *this; + } + + // Duplicator from the %ThermoPhase parent class + /* + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a %ThermoPhase + */ + ThermoPhase *IdealGasPhase::duplMyselfAsThermoPhase() const { + ThermoPhase *igp = new IdealGasPhase(*this); + return (ThermoPhase *) igp; + } + + // Molar Thermodynamic Properties of the Solution ------------------ + + /* + * Molar internal energy. J/kmol. For an ideal gas mixture, + * \f[ + * \hat u(T) = \sum_k X_k \hat h^0_k(T) - \hat R T, + * \f] + * and is a function only of temperature. + * The reference-state pure-species enthalpies + * \f$ \hat h^0_k(T) \f$ are computed by the species thermodynamic + * property manager. + * @see SpeciesThermo + */ + doublereal IdealGasPhase::intEnergy_mole() const { + return GasConstant * temperature() + * ( mean_X(&enthalpy_RT_ref()[0]) - 1.0); + } + + /* + * Molar entropy. Units: J/kmol/K. + * For an ideal gas mixture, + * \f[ + * \hat s(T, P) = \sum_k X_k \hat s^0_k(T) - \hat R \log (P/P^0). + * \f] + * The reference-state pure-species entropies + * \f$ \hat s^0_k(T) \f$ are computed by the species thermodynamic + * property manager. + * @see SpeciesThermo + */ + doublereal IdealGasPhase::entropy_mole() const { + return GasConstant * (mean_X(&entropy_R_ref()[0]) - + sum_xlogx() - std::log(pressure()/m_spthermo->refPressure())); + } + + /* + * Molar Gibbs free Energy for an ideal gas. + * Units = J/kmol. + */ + doublereal IdealGasPhase::gibbs_mole() const { + return enthalpy_mole() - temperature() * entropy_mole(); + } + + /* + * Molar heat capacity at constant pressure. Units: J/kmol/K. + * For an ideal gas mixture, + * \f[ + * \hat c_p(t) = \sum_k \hat c^0_{p,k}(T). + * \f] + * The reference-state pure-species heat capacities + * \f$ \hat c^0_{p,k}(T) \f$ are computed by the species thermodynamic + * property manager. + * @see SpeciesThermo + */ + doublereal IdealGasPhase::cp_mole() const { + return GasConstant * mean_X(&cp_R_ref()[0]); + } + + /* + * Molar heat capacity at constant volume. Units: J/kmol/K. + * For an ideal gas mixture, + * \f[ \hat c_v = \hat c_p - \hat R. \f] + */ + doublereal IdealGasPhase::cv_mole() const { + return cp_mole() - GasConstant; + } + // Mechanical Equation of State ---------------------------- // Chemical Potentials and Activities ---------------------- diff --git a/Cantera/src/thermo/IdealGasPhase.h b/Cantera/src/thermo/IdealGasPhase.h index d67481bb0..1c0cf6bf4 100644 --- a/Cantera/src/thermo/IdealGasPhase.h +++ b/Cantera/src/thermo/IdealGasPhase.h @@ -313,12 +313,44 @@ namespace Cantera { public: - //! Empty Constructor + //! Default empty Constructor IdealGasPhase(); + //! Copy Constructor + /*! + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + IdealGasPhase(const IdealGasPhase &right); + + //! Asignment operator + /*! + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + IdealGasPhase& operator=(const IdealGasPhase& right); + //! Destructor virtual ~IdealGasPhase() {} - + + //! Duplicator from the %ThermoPhase parent class + /*! + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the inherited copy constructor. + * + * @return returns a pointer to a %ThermoPhase object, containing + * a copy of the current object + */ + ThermoPhase *duplMyselfAsThermoPhase() const; + //! Equation of state flag. /*! * Returns the value cIdealGas, defined in mix_defs.h. @@ -360,10 +392,7 @@ namespace Cantera { * property manager. * @see SpeciesThermo */ - virtual doublereal intEnergy_mole() const { - return GasConstant * temperature() - * ( mean_X(&enthalpy_RT_ref()[0]) - 1.0); - } + virtual doublereal intEnergy_mole() const; /** * Molar entropy. Units: J/kmol/K. @@ -376,19 +405,13 @@ namespace Cantera { * property manager. * @see SpeciesThermo */ - virtual doublereal entropy_mole() const { - return GasConstant * (mean_X(&entropy_R_ref()[0]) - - sum_xlogx() - std::log(pressure()/m_spthermo->refPressure())); - } + virtual doublereal entropy_mole() const; /** * Molar Gibbs free Energy for an ideal gas. * Units = J/kmol. */ - virtual doublereal gibbs_mole() const { - return enthalpy_mole() - temperature() * entropy_mole(); - } - + virtual doublereal gibbs_mole() const; /** * Molar heat capacity at constant pressure. Units: J/kmol/K. @@ -401,18 +424,14 @@ namespace Cantera { * property manager. * @see SpeciesThermo */ - virtual doublereal cp_mole() const { - return GasConstant * mean_X(&cp_R_ref()[0]); - } + virtual doublereal cp_mole() const; /** * Molar heat capacity at constant volume. Units: J/kmol/K. * For an ideal gas mixture, * \f[ \hat c_v = \hat c_p - \hat R. \f] */ - virtual doublereal cv_mole() const { - return cp_mole() - GasConstant; - } + virtual doublereal cv_mole() const; //@} diff --git a/Cantera/src/thermo/StoichSubstance.cpp b/Cantera/src/thermo/StoichSubstance.cpp index 3a8f04fd8..b8b08cf78 100644 --- a/Cantera/src/thermo/StoichSubstance.cpp +++ b/Cantera/src/thermo/StoichSubstance.cpp @@ -16,6 +16,78 @@ namespace Cantera { + + // Default empty constructor + StoichSubstance::StoichSubstance() : + m_kk(0), + m_tmin(0.0), + m_tmax(0.0), + m_press(OneAtm), + m_p0(OneAtm), + m_tlast(-1.0) { + } + + // Copy Constructor + /* + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + StoichSubstance::StoichSubstance(const StoichSubstance &right) : + m_kk(0), + m_tmin(0.0), + m_tmax(0.0), + m_press(OneAtm), + m_p0(OneAtm), + m_tlast(-1.0) { + *this = operator=(right); + } + + // Asignment operator + /* + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + StoichSubstance& StoichSubstance:: + operator=(const StoichSubstance &right) { + if (&right != this) { + ThermoPhase::operator=(right); + m_kk = right.m_kk; + m_tmin = right.m_tmin; + m_tmax = right.m_tmax; + m_press = right.m_press; + m_p0 = right.m_p0; + m_tlast = right.m_tlast; + m_h0_RT = right.m_h0_RT; + m_cp0_R = right.m_cp0_R; + m_s0_R = right.m_s0_R; + } + return *this; + } + + // Duplicator from the %ThermoPhase parent class + /* + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a %ThermoPhase + */ + ThermoPhase *StoichSubstance::duplMyselfAsThermoPhase() const { + ThermoPhase *igp = new StoichSubstance(*this); + return (ThermoPhase *) igp; + } + + // Destructor + StoichSubstance::~StoichSubstance() { + } + void StoichSubstance::initThermo() { m_kk = nSpecies(); if (m_kk > 1) { diff --git a/Cantera/src/thermo/StoichSubstance.h b/Cantera/src/thermo/StoichSubstance.h index 7b35751af..1437bf0fa 100644 --- a/Cantera/src/thermo/StoichSubstance.h +++ b/Cantera/src/thermo/StoichSubstance.h @@ -36,15 +36,42 @@ namespace Cantera { public: - StoichSubstance(): - m_kk(0), - m_tmin(0.0), - m_tmax(0.0), - m_press(OneAtm), - m_p0(OneAtm), - m_tlast(-1.0) {} + //! Default empty constructor + StoichSubstance(); - virtual ~StoichSubstance() {} + //! Copy Constructor + /*! + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + StoichSubstance(const StoichSubstance &right); + + //! Asignment operator + /*! + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + StoichSubstance& operator=(const StoichSubstance &right); + + //! Destructor + virtual ~StoichSubstance(); + + //! Duplicator from the %ThermoPhase parent class + /* + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a %ThermoPhase + */ + ThermoPhase *duplMyselfAsThermoPhase() const; /** * diff --git a/Cantera/src/thermo/SurfPhase.cpp b/Cantera/src/thermo/SurfPhase.cpp index 0f9ed4864..ceecdde15 100644 --- a/Cantera/src/thermo/SurfPhase.cpp +++ b/Cantera/src/thermo/SurfPhase.cpp @@ -33,19 +33,18 @@ using namespace std; namespace Cantera { - SurfPhase:: - SurfPhase(doublereal n0): - ThermoPhase(), - m_n0(n0), - m_logn0(0.0), - m_tmin(0.0), - m_tmax(0.0), - m_press(OneAtm), - m_tlast(0.0) - { - if (n0 > 0.0) m_logn0 = log(n0); - setNDim(2); - } + SurfPhase::SurfPhase(doublereal n0): + ThermoPhase(), + m_n0(n0), + m_logn0(0.0), + m_tmin(0.0), + m_tmax(0.0), + m_press(OneAtm), + m_tlast(0.0) + { + if (n0 > 0.0) m_logn0 = log(n0); + setNDim(2); + } SurfPhase::SurfPhase(XML_Node& xmlphase) { const XML_Node& th = xmlphase.child("thermo"); @@ -57,6 +56,66 @@ namespace Cantera { importPhase(xmlphase, this); } + // Copy Constructor + /* + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + SurfPhase::SurfPhase(const SurfPhase &right) : + m_n0(right.m_n0), + m_logn0(right.m_logn0), + m_tmin(right.m_tmin), + m_tmax(right.m_tmax), + m_press(right.m_press), + m_tlast(right.m_tlast) + { + *this = operator=(right); + } + + // Asignment operator + /* + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + SurfPhase& SurfPhase:: + operator=(const SurfPhase &right) { + if (&right != this) { + ThermoPhase::operator=(right); + m_n0 = right.m_n0; + m_logn0 = right.m_logn0; + m_tmin = right.m_tmin; + m_tmax = right.m_tmax; + m_press = right.m_press; + m_tlast = right.m_tlast; + m_h0 = right.m_h0; + m_cp0 = right.m_cp0; + m_mu0 = right.m_mu0; + m_work = right.m_work; + m_pe = right.m_pe; + m_logsize = right.m_logsize; + } + return *this; + } + + // Duplicator from the %ThermoPhase parent class + /* + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a %ThermoPhase + */ + ThermoPhase *SurfPhase::duplMyselfAsThermoPhase() const { + ThermoPhase *igp = new SurfPhase(*this); + return (ThermoPhase *) igp; + } doublereal SurfPhase:: enthalpy_mole() const { diff --git a/Cantera/src/thermo/SurfPhase.h b/Cantera/src/thermo/SurfPhase.h index d828ffa30..99b24dbac 100644 --- a/Cantera/src/thermo/SurfPhase.h +++ b/Cantera/src/thermo/SurfPhase.h @@ -167,10 +167,40 @@ namespace Cantera { */ SurfPhase(XML_Node& xmlphase); + //! Copy Constructor + /*! + * Copy constructor for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * This is a wrapper around the assignment operator + * + * @param right Object to be copied. + */ + SurfPhase(const SurfPhase &right); + + //! Asignment operator + /*! + * Assignment operator for the object. Constructed + * object will be a clone of this object, but will + * also own all of its data. + * + * @param right Object to be copied. + */ + SurfPhase& operator=(const SurfPhase &right); //! Destructor. virtual ~SurfPhase(); + //! Duplicator from the %ThermoPhase parent class + /* + * Given a pointer to a %ThermoPhase object, this function will + * duplicate the %ThermoPhase object and all underlying structures. + * This is basically a wrapper around the copy constructor. + * + * @return returns a pointer to a %ThermoPhase + */ + ThermoPhase *duplMyselfAsThermoPhase() const; + //----- reimplimented methods of class ThermoPhase ------ //! Equation of state type flag. diff --git a/Cantera/src/thermo/ThermoPhase.h b/Cantera/src/thermo/ThermoPhase.h index d613b6634..e6492dc20 100755 --- a/Cantera/src/thermo/ThermoPhase.h +++ b/Cantera/src/thermo/ThermoPhase.h @@ -264,16 +264,15 @@ namespace Cantera { */ ThermoPhase& operator=(const ThermoPhase &right); - /** - * Duplication routine for objects which inherit from - * ThermoPhase. - * - * This virtual routine can be used to duplicate thermophase objects - * inherited from ThermoPhase even if the application only has - * a pointer to ThermoPhase to work with. + //! Duplication routine for objects which inherit from + //! ThermoPhase. + /*! + * This virtual routine can be used to duplicate %ThermoPhase objects + * inherited from %ThermoPhase even if the application only has + * a pointer to %ThermoPhase to work with. * - * Currently, this is not fully implemented. If called, an - * exception will be called. + * These routines are basically wrappers around the derived copy + * constructor. */ virtual ThermoPhase *duplMyselfAsThermoPhase() const;