Added a copy constructor and an assignment operator

This commit is contained in:
Harry Moffat 2011-01-12 20:55:07 +00:00
parent bab66a8b89
commit 60194831c1
2 changed files with 127 additions and 53 deletions

View file

@ -21,7 +21,8 @@ using namespace std;
namespace Cantera {
/// Constructor.
//====================================================================================================================
// Constructor.
MultiPhase::MultiPhase() :
m_np(0),
m_temp(0.0),
@ -33,8 +34,67 @@ namespace Cantera {
m_Tmin(1.0),
m_Tmax(100000.0)
{
}
//====================================================================================================================
// Copy Constructor
/*
* @param right Object to be copied
*/
MultiPhase::MultiPhase(const MultiPhase &right) :
m_np(0),
m_temp(0.0),
m_press(0.0),
m_nel(0),
m_nsp(0),
m_init(false),
m_eloc(-1),
m_Tmin(1.0),
m_Tmax(100000.0)
{
operator=(right);
}
//====================================================================================================================
// Destructor.
/*
* Does nothing. Class MultiPhase does not take
* "ownership" (i.e. responsibility for destroying) the
* phase objects.
*/
MultiPhase::~MultiPhase()
{
}
//====================================================================================================================
// Assignment operator
/*
* @param right Object to be copied
*/
MultiPhase& MultiPhase::operator=(const MultiPhase& right)
{
if (&right != this) {
m_moles = right.m_moles;
// shallow copy of phase pointers
m_phase = right.m_phase;
m_atoms = right.m_atoms;
m_moleFractions = right.m_moleFractions;
m_spphase = right.m_spphase;
m_spstart = right.m_spstart;
m_enames = right.m_enames;
m_enamemap = right.m_enamemap;
m_np = right.m_np;
m_temp = right.m_temp;
m_press = right.m_press;
m_nel = right.m_nel;
m_nsp = right.m_nsp;
m_init = right.m_init;
m_eloc = right.m_eloc;
m_temp_OK = right.m_temp_OK;
m_Tmin = right.m_Tmin;
m_Tmax = right.m_Tmax;
m_elemAbundances = right.m_elemAbundances;
}
return *this;
}
//====================================================================================================================
void MultiPhase::
addPhases(MultiPhase& mix) {
index_t n;
@ -42,7 +102,7 @@ namespace Cantera {
addPhase(mix.m_phase[n], mix.m_moles[n]);
}
}
//====================================================================================================================
void MultiPhase::
addPhases(phase_list& phases, const vector_fp& phaseMoles) {
index_t np = phases.size();
@ -52,7 +112,7 @@ namespace Cantera {
}
init();
}
//====================================================================================================================
void MultiPhase::
addPhase(phase_t* p, doublereal moles) {
if (m_init) {
@ -121,8 +181,7 @@ namespace Cantera {
if (t < m_Tmax) m_Tmax = t;
}
}
//====================================================================================================================
// Process phases and build atomic composition array. This method
// must be called after all phases are added, before doing
// anything else with the mixture. After init() has been called,
@ -186,7 +245,7 @@ namespace Cantera {
updatePhases();
}
//====================================================================================================================
// Return a reference to phase n. The state of phase n is
// also updated to match the state stored locally in the
// mixture object.
@ -197,15 +256,17 @@ namespace Cantera {
m_phase[n]->setPressure(m_press);
return *m_phase[n];
}
//====================================================================================================================
/// Moles of species \c k.
doublereal MultiPhase::speciesMoles(index_t k) const {
index_t ip = m_spphase[k];
return m_moles[ip]*m_moleFractions[k];
}
/// Total moles of element m, summed over all
/// phases
//====================================================================================================================
// Total moles of global element \a m, summed over all phases.
/*
* @param m Index of the global element
*/
doublereal MultiPhase::elementMoles(index_t m) const {
doublereal sum = 0.0, phasesum;
index_t i, k = 0, ik, nsp;
@ -220,8 +281,8 @@ namespace Cantera {
}
return sum;
}
/// Total charge, summed over all phases
//====================================================================================================================
// Total charge, summed over all phases
doublereal MultiPhase::charge() const {
doublereal sum = 0.0;
index_t i;
@ -230,7 +291,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
int MultiPhase::speciesIndex(std::string speciesName, std::string phaseName) {
int p = phaseIndex(phaseName);
if (p < 0) {
@ -242,7 +303,7 @@ namespace Cantera {
}
return m_spstart[p] + k;
}
//====================================================================================================================
/// Net charge of one phase (Coulombs). The net charge is computed as
/// \f[ Q_p = N_p \sum_k F z_k X_k \f]
/// where the sum runs only over species in phase \a p.
@ -256,7 +317,7 @@ namespace Cantera {
}
return Faraday*phasesum*m_moles[p];
}
//====================================================================================================================
/// Get the chemical potentials of all species in all phases.
void MultiPhase::getChemPotentials(doublereal* mu) const {
@ -267,7 +328,7 @@ namespace Cantera {
loc += m_phase[i]->nSpecies();
}
}
//====================================================================================================================
// Get chemical potentials of species with valid thermo
// data. This method is designed for use in computing chemical
// equilibrium by Gibbs minimization. For solution phases (more
@ -313,7 +374,7 @@ namespace Cantera {
loc += m_phase[i]->nSpecies();
}
}
//====================================================================================================================
/// True if species \a k belongs to a solution phase.
bool MultiPhase::solutionSpecies(index_t k) const {
if (m_phase[m_spphase[k]]->nSpecies() > 1)
@ -334,7 +395,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
/// The enthalpy of the mixture (J).
doublereal MultiPhase::enthalpy() const {
index_t i;
@ -347,7 +408,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
/// The internal energy of the mixture (J).
doublereal MultiPhase::IntEnergy() const {
index_t i;
@ -360,7 +421,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
/// The entropy of the mixture (J/K).
doublereal MultiPhase::entropy() const {
index_t i;
@ -373,7 +434,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
/// The specific heat at constant pressure and composition (J/K).
/// Note that this does not account for changes in composition of
/// the mixture with temperature.
@ -389,7 +450,7 @@ namespace Cantera {
return sum;
}
//====================================================================================================================
/// Set the mole fractions of phase \a n to the values in
/// array \a x.
@ -402,7 +463,7 @@ namespace Cantera {
m_moleFractions[istart+k] = x[k];
}
}
//====================================================================================================================
// Set the species moles using a map. The map \a xMap maps
// species name strings to mole numbers. Mole numbers that are
// less than or equal to zero will be set to zero.
@ -416,7 +477,7 @@ namespace Cantera {
}
setMoles(DATA_PTR(moles));
}
//====================================================================================================================
// Set the species moles using a string. Unspecified species are
// set to zero.
void MultiPhase::setMolesByName(const std::string& x) {
@ -435,7 +496,7 @@ namespace Cantera {
parseCompString(x, xx);
setMolesByName(xx);
}
//====================================================================================================================
// Get the mole numbers of all species in the multiphase
// object
void MultiPhase::getMoles(doublereal * molNum) const {
@ -454,7 +515,7 @@ namespace Cantera {
}
}
}
//====================================================================================================================
/// Set the species moles to the values in array \a n. The state
/// of each phase object is also updated to have the specified
/// composition and the mixture temperature and pressure.
@ -486,7 +547,7 @@ namespace Cantera {
loc += nsp;
}
}
//====================================================================================================================
void MultiPhase::addSpeciesMoles(const int indexS, const doublereal addedMoles) {
vector_fp tmpMoles(m_nsp, 0.0);
getMoles(DATA_PTR(tmpMoles));
@ -496,21 +557,21 @@ namespace Cantera {
}
setMoles(DATA_PTR(tmpMoles));
}
//====================================================================================================================
void MultiPhase::setState_TP(const doublereal T, const doublereal Pres) {
if (!m_init) init();
m_temp = T;
m_press = Pres;
updatePhases();
}
//====================================================================================================================
void MultiPhase::setState_TPMoles(const doublereal T, const doublereal Pres,
const doublereal *n) {
m_temp = T;
m_press = Pres;
setMoles(n);
}
//====================================================================================================================
void MultiPhase::getElemAbundances(doublereal *elemAbundances) const {
index_t eGlobal;
calcElemAbundances();
@ -518,7 +579,7 @@ namespace Cantera {
elemAbundances[eGlobal] = m_elemAbundances[eGlobal];
}
}
//====================================================================================================================
// Internal routine to calculate the element abundance vector
void MultiPhase::calcElemAbundances() const {
index_t loc = 0;
@ -542,7 +603,7 @@ namespace Cantera {
loc += nspPhase;
}
}
//====================================================================================================================
/// The total mixture volume [m^3].
doublereal MultiPhase::volume() const {
int i;
@ -553,7 +614,7 @@ namespace Cantera {
}
return sum;
}
//====================================================================================================================
doublereal MultiPhase::equilibrate(int XY, doublereal err,
int maxsteps, int maxiter, int loglevel) {
doublereal error;
@ -867,18 +928,18 @@ namespace Cantera {
}
}
#endif
//====================================================================================================================
void MultiPhase::setTemperature(const doublereal T) {
if (!m_init) init();
m_temp = T;
updatePhases();
}
//====================================================================================================================
// Name of element \a m.
std::string MultiPhase::elementName(int m) const {
return m_enames[m];
}
//====================================================================================================================
// Index of element with name \a name.
int MultiPhase::elementIndex(std::string name) const {
for (size_t e = 0; e < m_nel; e++) {
@ -888,25 +949,25 @@ namespace Cantera {
}
return -1;
}
//====================================================================================================================
// Name of species with global index \a k.
std::string MultiPhase::speciesName(const int k) const {
return m_snames[k];
}
//====================================================================================================================
doublereal MultiPhase::nAtoms(const int kGlob, const int mGlob) const {
return m_atoms(mGlob, kGlob);
}
//====================================================================================================================
void MultiPhase::getMoleFractions(doublereal* const x) const {
std::copy(m_moleFractions.begin(), m_moleFractions.end(), x);
}
//====================================================================================================================
std::string MultiPhase::phaseName(const index_t iph) const {
const phase_t *tptr = m_phase[iph];
return tptr->id();
}
//====================================================================================================================
int MultiPhase::phaseIndex(const std::string &pName) const {
std::string tmp;
for (int iph = 0; iph < (int) m_np; iph++) {
@ -918,32 +979,33 @@ namespace Cantera {
}
return -1;
}
//====================================================================================================================
doublereal MultiPhase::phaseMoles(const index_t n) const {
return m_moles[n];
}
//====================================================================================================================
void MultiPhase::setPhaseMoles(const index_t n, const doublereal moles) {
m_moles[n] = moles;
}
//====================================================================================================================
int MultiPhase::speciesPhaseIndex(const index_t kGlob) const {
return m_spphase[kGlob];
}
//====================================================================================================================
doublereal MultiPhase::moleFraction(const index_t kGlob) const{
return m_moleFractions[kGlob];
}
//====================================================================================================================
bool MultiPhase::tempOK(const index_t p) const {
return m_temp_OK[p];
}
//====================================================================================================================
/// Update the locally-stored species mole fractions.
void MultiPhase::updateMoleFractions() {
uploadMoleFractionsFromPhases();
}
//====================================================================================================================
/// Update the locally-stored species mole fractions.
void MultiPhase::uploadMoleFractionsFromPhases() {
index_t ip, loc = 0;
@ -954,7 +1016,7 @@ namespace Cantera {
}
calcElemAbundances();
}
//====================================================================================================================
//-------------------------------------------------------------
//
// protected methods
@ -981,6 +1043,6 @@ namespace Cantera {
}
}
}
//====================================================================================================================
}

View file

@ -85,13 +85,25 @@ namespace Cantera {
*/
MultiPhase();
//! Copy Constructor
/*!
* @param right Object to be copied
*/
MultiPhase(const MultiPhase &right);
//! Destructor.
/*!
* Does nothing. Class MultiPhase does not take
* "ownership" (i.e. responsibility for destroying) the
* phase objects.
*/
virtual ~MultiPhase() {}
virtual ~MultiPhase();
//! Assignment operator
/*!
* @param right Object to be copied
*/
MultiPhase& operator=(const MultiPhase& right);
//! Add a vector of phases to the mixture
/*!
@ -258,7 +270,7 @@ namespace Cantera {
/// conditions for which they are stable.
doublereal maxTemp() const { return m_Tmax; }
/// Total charge (Coulombs).
//! Total charge summed over all phases (Coulombs).
doublereal charge() const;
/// Charge (Coulombs) of phase with index \a p.
@ -267,7 +279,7 @@ namespace Cantera {
*/
doublereal phaseCharge(index_t p) const;
/// Total moles of global element \a m, summed over all phases.
//! Total moles of global element \a m, summed over all phases.
/*!
* @param m Index of the global element
*/