Eliminate redundant VCS vector functions

These functions just duplicated features of the standard library.
This commit is contained in:
Ray Speth 2014-05-30 19:23:25 +00:00
parent c6cefd6df8
commit 46a36c11cb
12 changed files with 44 additions and 245 deletions

View file

@ -223,128 +223,6 @@ int vcsUtil_root1d(double xmin, double xmax, size_t itmax, VCS_FUNC_PTR func,
*/
double vcs_second();
//! This define turns on using memset and memcpy. I have not run into
//! any systems where this is a problem. It's the fastest way to do
//! low lvl operations where applicable. There are alternative routines
//! available if this ever fails.
#define USE_MEMSET
#ifdef USE_MEMSET
//! Zero a double vector
/*!
* @param vec_to vector of doubles
* @param length length of the vector to zero.
*/
inline void vcs_dzero(double* const vec_to, const size_t length)
{
(void) memset((void*) vec_to, 0, length * sizeof(double));
}
//! Zero an int vector
/*!
* @param vec_to vector of ints
* @param length length of the vector to zero.
*/
inline void vcs_izero(int* const vec_to, const size_t length)
{
(void) memset((void*) vec_to, 0, length * sizeof(int));
}
//! Copy a double vector
/*!
* @param vec_to Vector to copy into. This vector must be dimensioned
* at least as large as the vec_from vector.
* @param vec_from Vector to copy from
* @param length Number of doubles to copy.
*/
inline void vcs_dcopy(double* const vec_to,
const double* const vec_from, const size_t length)
{
(void) memcpy((void*) vec_to, (const void*) vec_from,
(length) * sizeof(double));
}
//! Copy an int vector
/*!
* @param vec_to Vector to copy into. This vector must be dimensioned
* at least as large as the vec_from vector.
* @param vec_from Vector to copy from
* @param length Number of int to copy.
*/
inline void vcs_icopy(int* const vec_to,
const int* const vec_from, const size_t length)
{
(void) memcpy((void*) vec_to, (const void*) vec_from,
(length) * sizeof(int));
}
//! Zero a std double vector
/*!
* @param vec_to vector of doubles
* @param length length of the vector to zero.
*/
inline void vcs_vdzero(std::vector<double> &vec_to, const size_t length)
{
(void) memset((void*)VCS_DATA_PTR(vec_to), 0, (length) * sizeof(double));
}
//! Zero a std int vector
/*!
* @param vec_to vector of ints
* @param length length of the vector to zero.
*/
inline void vcs_vizero(std::vector<int> &vec_to, const size_t length)
{
(void) memset((void*)VCS_DATA_PTR(vec_to), 0, (length) * sizeof(int));
}
//! Copy one std double vector into another
/*!
* This is an inlined function that uses memcpy. memcpy is probably
* the fastest way to do this. This routine requires the vectors to be
* previously dimensioned appropriately. No error checking is done.
*
* @param vec_to Vector to copy into. This vector must be dimensioned
* at least as large as the vec_from vector.
* @param vec_from Vector to copy from
* @param length Number of doubles to copy.
*/
inline void vcs_vdcopy(std::vector<double> & vec_to,
const std::vector<double> & vec_from, size_t length)
{
(void) memcpy((void*)&(vec_to[0]), (const void*) &(vec_from[0]),
(length) * sizeof(double));
}
//! Copy one std integer vector into another
/*!
* This is an inlined function that uses memcpy. memcpy is probably
* the fastest way to do this.
*
* @param vec_to Vector to copy into. This vector must be dimensioned
* at least as large as the vec_from vector.
* @param vec_from Vector to copy from
* @param length Number of integers to copy.
*/
inline void vcs_vicopy(std::vector<int> & vec_to,
const std::vector<int> & vec_from, const int length)
{
(void) memcpy((void*)&(vec_to[0]), (const void*) &(vec_from[0]),
(length) * sizeof(int));
}
#else
extern void vcs_dzero(double* const, const int);
extern void vcs_izero(int* const , const int);
extern void vcs_dcopy(double* const, const double* const, const int);
extern void vcs_icopy(int* const, const int* const, const int);
extern void vcs_vdzero(std::vector<double> &vvv, const int len = -1);
extern void vcs_vizero(std::vector<double> &vvv, const int len = -1);
void vcs_vdcopy(std::vector<double> &vec_to,
const std::vector<double> vec_from, const int len = -1);
void vcs_vicopy(std::vector<int> &vec_to,
const std::vector<int> vec_from, const int len = -1);
#endif
//! determine the l2 norm of a vector of doubles
/*!
* @param vec vector of doubles

View file

@ -548,8 +548,7 @@ void vcs_VolPhase::setMolesFromVCS(const int stateCalc,
*/
if (stateCalc == VCS_STATECALC_OLD) {
if (v_totalMoles > 0.0) {
vcs_dcopy(VCS_DATA_PTR(creationMoleNumbers_), VCS_DATA_PTR(Xmol_), m_numSpecies);
creationMoleNumbers_ = Xmol_;
}
}
@ -794,7 +793,7 @@ void vcs_VolPhase::_updateLnActCoeffJac()
* Revert to the base case Xmol_, v_totalMoles
*/
v_totalMoles = TMoles_base;
vcs_vdcopy(Xmol_, Xmol_Base, m_numSpecies);
Xmol_ = Xmol_Base;
}
/*
* Go get base values for the activity coefficients.
@ -847,7 +846,7 @@ void vcs_VolPhase::setPtrThermoPhase(Cantera::ThermoPhase* tp_ptr)
resize(VP_ID_, nsp, nelem, PhaseName.c_str());
}
TP_ptr->getMoleFractions(VCS_DATA_PTR(Xmol_));
vcs_dcopy(VCS_DATA_PTR(creationMoleNumbers_), VCS_DATA_PTR(Xmol_), m_numSpecies);
creationMoleNumbers_ = Xmol_;
_updateMoleFractionDependencies();
/*
@ -897,7 +896,7 @@ double vcs_VolPhase::molefraction(size_t k) const
void vcs_VolPhase::setCreationMoleNumbers(const double* const n_k,
const std::vector<size_t> &creationGlobalRxnNumbers)
{
vcs_dcopy(VCS_DATA_PTR(creationMoleNumbers_), n_k, m_numSpecies);
creationMoleNumbers_.assign(n_k, n_k+m_numSpecies);
for (size_t k = 0; k < m_numSpecies; k++) {
creationGlobalRxnNumbers_[k] = creationGlobalRxnNumbers[k];
}

View file

@ -110,8 +110,7 @@ int VCS_SOLVE::vcs_elcorr(double aa[], double x[])
int retn = 0;
#ifdef DEBUG_MODE
std::vector<double> ga_save(m_numElemConstraints, 0.0);
vcs_dcopy(VCS_DATA_PTR(ga_save), VCS_DATA_PTR(m_elemAbundances), m_numElemConstraints);
std::vector<double> ga_save(m_elemAbundances);
if (m_debug_print_lvl >= 2) {
plogf(" --- vcsc_elcorr: Element abundances correction routine");
if (m_numElemConstraints != m_numComponents) {

View file

@ -65,7 +65,7 @@ void VCS_SOLVE::vcs_inest(double* const aw, double* const sa, double* const sm,
* Make sure all species have positive definite mole numbers
* Set voltages to zero for now, until we figure out what to do
*/
vcs_dzero(VCS_DATA_PTR(m_deltaMolNumSpecies), nspecies);
m_deltaMolNumSpecies.assign(m_deltaMolNumSpecies.size(), 0.0);
for (size_t kspec = 0; kspec < nspecies; ++kspec) {
if (m_speciesUnknownType[kspec] != VCS_SPECIES_TYPE_INTERFACIALVOLTAGE) {
if (m_molNumSpecies_old[kspec] <= 0.0) {
@ -113,14 +113,13 @@ void VCS_SOLVE::vcs_inest(double* const aw, double* const sa, double* const sm,
TMolesMultiphase += m_tPhaseMoles_new[iph];
}
}
vcs_dcopy(VCS_DATA_PTR(m_molNumSpecies_new), VCS_DATA_PTR(m_molNumSpecies_old), nspecies);
m_molNumSpecies_new = m_molNumSpecies_old;
for (size_t kspec = 0; kspec < m_numComponents; ++kspec) {
if (m_speciesUnknownType[kspec] != VCS_SPECIES_TYPE_MOLNUM) {
m_molNumSpecies_new[kspec] = 0.0;
}
}
vcs_dcopy(VCS_DATA_PTR(m_feSpecies_new), VCS_DATA_PTR(m_SSfeSpecies),
nspecies);
m_feSpecies_new = m_SSfeSpecies;
for (size_t kspec = 0; kspec < m_numComponents; ++kspec) {
if (m_speciesUnknownType[kspec] == VCS_SPECIES_TYPE_MOLNUM) {
@ -150,7 +149,7 @@ void VCS_SOLVE::vcs_inest(double* const aw, double* const sa, double* const sm,
/* ********************************************************** */
double* xtphMax = VCS_DATA_PTR(m_TmpPhase);
double* xtphMin = VCS_DATA_PTR(m_TmpPhase2);
vcs_dzero(VCS_DATA_PTR(m_deltaPhaseMoles), m_numPhases);
m_deltaPhaseMoles.assign(m_deltaPhaseMoles.size(), 0.0);
for (size_t iph = 0; iph < m_numPhases; iph++) {
xtphMax[iph] = log(m_tPhaseMoles_new[iph] * 1.0E32);
xtphMin[iph] = log(m_tPhaseMoles_new[iph] * 1.0E-32);

View file

@ -558,7 +558,7 @@ double VCS_SOLVE::vcs_phaseStabilityTest(const size_t iph)
vector<doublereal> fracDelta_old(nsp, 0.0);
vector<doublereal> fracDelta_raw(nsp, 0.0);
vector<size_t> creationGlobalRxnNumbers(nsp, npos);
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_Deficient), VCS_DATA_PTR(m_deltaGRxn_old), m_numRxnRdc);
m_deltaGRxn_Deficient = m_deltaGRxn_old;
vector<doublereal> m_feSpecies_Deficient(m_numComponents, 0.0);
doublereal damp = 1.0;

View file

@ -210,13 +210,13 @@ int VCS_SOLVE::vcs_prep()
/*
* Initialize various arrays in the data to zero
*/
vcs_vdzero(m_feSpecies_old, m_numSpeciesTot);
vcs_vdzero(m_feSpecies_new, m_numSpeciesTot);
vcs_vdzero(m_molNumSpecies_new, m_numSpeciesTot);
vcs_dzero(&(m_deltaMolNumPhase(0,0)), m_numSpeciesTot * m_numPhases);
m_feSpecies_old.assign(m_feSpecies_old.size(), 0.0);
m_feSpecies_new.assign(m_feSpecies_new.size(), 0.0);
m_molNumSpecies_new.assign(m_molNumSpecies_new.size(), 0.0);
m_deltaMolNumPhase.zero();
m_phaseParticipation.zero();
vcs_dzero(VCS_DATA_PTR(m_deltaPhaseMoles), m_numPhases);
vcs_dzero(VCS_DATA_PTR(m_tPhaseMoles_new), m_numPhases);
m_deltaPhaseMoles.assign(m_deltaPhaseMoles.size(), 0.0);
m_tPhaseMoles_new.assign(m_tPhaseMoles_new.size(), 0.0);
/*
* Calculate the total number of moles in all phases.
*/

View file

@ -156,8 +156,8 @@ void VCS_PROB::resizeElements(size_t nel, int force)
void VCS_PROB::set_gai()
{
gai.assign(gai.size(), 0.0);
double* ElemAbund = VCS_DATA_PTR(gai);
vcs_dzero(ElemAbund, ne);
for (size_t j = 0; j < ne; j++) {
for (size_t kspec = 0; kspec < nspecies; kspec++) {

View file

@ -710,7 +710,7 @@ double VCS_SOLVE::vcs_line_search(const size_t irxn, const double dx_orig, char*
return 0.0;
}
vcs_dcopy(VCS_DATA_PTR(m_molNumSpecies_new), molNumBase, m_numSpeciesRdc);
m_molNumSpecies_new = m_molNumSpecies_old;
double molSum = molNumBase[kspec];
m_molNumSpecies_new[kspec] = molNumBase[kspec] + dx_orig;
for (size_t k = 0; k < m_numComponents; k++) {

View file

@ -460,12 +460,12 @@ int VCS_SOLVE::vcs_prob_specifyFully(const VCS_PROB* pub)
/*
* Copy over the species molecular weights
*/
vcs_vdcopy(m_wtSpecies, pub->WtSpecies, nspecies);
m_wtSpecies = pub->WtSpecies;
/*
* Copy over the charges
*/
vcs_vdcopy(m_chargeSpecies, pub->Charge, nspecies);
m_chargeSpecies = pub->Charge;
/*
* Malloc and Copy the VCS_SPECIES_THERMO structures
@ -484,8 +484,7 @@ int VCS_SOLVE::vcs_prob_specifyFully(const VCS_PROB* pub)
/*
* Copy the species unknown type
*/
vcs_icopy(VCS_DATA_PTR(m_speciesUnknownType),
VCS_DATA_PTR(pub->SpeciesUnknownType), nspecies);
m_speciesUnknownType = pub->SpeciesUnknownType;
/*
* iest => Do we have an initial estimate of the species mole numbers ?
@ -496,10 +495,10 @@ int VCS_SOLVE::vcs_prob_specifyFully(const VCS_PROB* pub)
* w[] -> Copy the equilibrium mole number estimate if it exists.
*/
if (pub->w.size() != 0) {
vcs_vdcopy(m_molNumSpecies_old, pub->w, nspecies);
m_molNumSpecies_old = pub->w;
} else {
m_doEstimateEquil = -1;
vcs_dzero(VCS_DATA_PTR(m_molNumSpecies_old), nspecies);
m_molNumSpecies_old.assign(m_molNumSpecies_old.size(), 0.0);
}
/*
@ -747,7 +746,7 @@ int VCS_SOLVE::vcs_prob_specifyFully(const VCS_PROB* pub)
*/
m_totalVol = pub->Vol;
if (m_PMVolumeSpecies.size() != 0) {
vcs_dcopy(VCS_DATA_PTR(m_PMVolumeSpecies), VCS_DATA_PTR(pub->VolPM), nspecies);
m_PMVolumeSpecies = pub->VolPM;
}
/*

View file

@ -333,7 +333,7 @@ int VCS_SOLVE::vcs_solve_TP(int print_lvl, int printDetails, int maxit)
* Evaluate the final mole fractions
* storing them in wt[]
*/
vcs_vdzero(m_molNumSpecies_new, m_numSpeciesTot);
m_molNumSpecies_new.assign(m_molNumSpecies_new.size(), 0.0);
for (size_t kspec = 0; kspec < m_numSpeciesTot; ++kspec) {
if (m_SSPhase[kspec]) {
m_molNumSpecies_new[kspec] = 1.0;
@ -467,18 +467,18 @@ void VCS_SOLVE::solve_tp_inner(size_t& iti, size_t& it1,
/*
* Copy the old solution into the new solution as an initial guess
*/
vcs_dcopy(VCS_DATA_PTR(m_feSpecies_new), VCS_DATA_PTR(m_feSpecies_old), m_numSpeciesRdc);
vcs_dcopy(VCS_DATA_PTR(m_actCoeffSpecies_new), VCS_DATA_PTR(m_actCoeffSpecies_old), m_numSpeciesRdc);
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_new), VCS_DATA_PTR(m_deltaGRxn_old), m_numRxnRdc);
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_Deficient), VCS_DATA_PTR(m_deltaGRxn_old), m_numRxnRdc);
vcs_dcopy(VCS_DATA_PTR(m_tPhaseMoles_new), VCS_DATA_PTR(m_tPhaseMoles_old), m_numPhases);
m_feSpecies_new = m_feSpecies_old;
m_actCoeffSpecies_new = m_actCoeffSpecies_old;
m_deltaGRxn_new = m_deltaGRxn_old;
m_deltaGRxn_Deficient = m_deltaGRxn_old;
m_tPhaseMoles_new = m_tPhaseMoles_old;
/*
* Zero out the entire vector of updates. We sometimes would
* query these values below, and we want to be sure that no
* information is left from previous iterations.
*/
vcs_dzero(VCS_DATA_PTR(m_deltaMolNumSpecies), m_numSpeciesTot);
m_deltaMolNumSpecies.assign(m_deltaMolNumSpecies.size(), 0.0);
/*************************************************************************/
/************** DETERMINE IF DEAD PHASES POP INTO EXISTENCE **************/
@ -527,7 +527,7 @@ void VCS_SOLVE::solve_tp_inner(size_t& iti, size_t& it1,
/*
* Zero out the net change in moles of multispecies phases
*/
vcs_dzero(VCS_DATA_PTR(m_deltaPhaseMoles), m_numPhases);
m_deltaPhaseMoles.assign(m_deltaPhaseMoles.size(), 0.0);
/* ********************************************************************** */
/* ***************** MAIN LOOP IN CALCULATION *************************** */
@ -1280,13 +1280,11 @@ void VCS_SOLVE::solve_tp_inner(size_t& iti, size_t& it1,
* loop.
*/
vcs_updateMolNumVolPhases(VCS_STATECALC_NEW);
vcs_dcopy(VCS_DATA_PTR(m_tPhaseMoles_old), VCS_DATA_PTR(m_tPhaseMoles_new), m_numPhases);
vcs_dcopy(VCS_DATA_PTR(m_molNumSpecies_old), VCS_DATA_PTR(m_molNumSpecies_new),
m_numSpeciesRdc);
vcs_dcopy(VCS_DATA_PTR(m_actCoeffSpecies_old),
VCS_DATA_PTR(m_actCoeffSpecies_new), m_numSpeciesRdc);
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_old), VCS_DATA_PTR(m_deltaGRxn_new), m_numRxnRdc);
vcs_dcopy(VCS_DATA_PTR(m_feSpecies_old), VCS_DATA_PTR(m_feSpecies_new), m_numSpeciesRdc);
m_tPhaseMoles_old = m_tPhaseMoles_new;
m_molNumSpecies_old = m_molNumSpecies_new;
m_actCoeffSpecies_old = m_actCoeffSpecies_new;
m_deltaGRxn_old = m_deltaGRxn_new;
m_feSpecies_old = m_feSpecies_new;
vcs_setFlagsVolPhases(true, VCS_STATECALC_OLD);
/*
@ -2336,7 +2334,7 @@ size_t VCS_SOLVE::vcs_add_all_deleted()
* We are relying here on a old saved value of m_actCoeffSpecies_old[kspec]
* being sufficiently good. Note, we will recalculate everything at the end of the routine.
*/
vcs_dcopy(VCS_DATA_PTR(m_molNumSpecies_new), VCS_DATA_PTR(m_molNumSpecies_old), m_numSpeciesTot);
m_molNumSpecies_new = m_molNumSpecies_old;
for (int cits = 0; cits < 3; cits++) {
for (size_t kspec = m_numSpeciesRdc; kspec < m_numSpeciesTot; ++kspec) {
@ -2499,8 +2497,7 @@ bool VCS_SOLVE::vcs_globStepDamp()
/* **** ADJUST MOLE NUMBERS, CHEM. POT *************** */
/* *************************************************** */
if (DEBUG_MODE_ENABLED && m_debug_print_lvl >= 2) {
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_tmp), VCS_DATA_PTR(m_deltaGRxn_new),
m_numRxnRdc);
m_deltaGRxn_tmp = m_deltaGRxn_new;
}
dptr = VCS_DATA_PTR(m_molNumSpecies_new);
@ -2610,7 +2607,8 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[],
/*
* Use a temporary work array for the mole numbers, aw[]
*/
vcs_dcopy(aw, VCS_DATA_PTR(m_molNumSpecies_old), m_numSpeciesTot);
std::copy(m_molNumSpecies_old.begin(),
m_molNumSpecies_old.begin() + m_numSpeciesTot, aw);
/*
* Take out the Voltage unknowns from consideration
*/
@ -3076,7 +3074,7 @@ L_END_LOOP:
/*
* Zero out the change of Phase Moles array
*/
vcs_dzero(&m_deltaMolNumPhase(0,0), (NSPECIES0)*(NPHASE0));
m_deltaMolNumPhase.zero();
m_phaseParticipation.zero();
/*
* Loop over each reaction, creating the change in Phase Moles
@ -3486,7 +3484,7 @@ void VCS_SOLVE::vcs_dfe(const int stateCalc,
}
}
}
vcs_dzero(tlogMoles, m_numPhases);
m_TmpPhase.assign(m_TmpPhase.size(), 0.0);
for (size_t iph = 0; iph < m_numPhases; iph++) {
if (tPhMoles_ptr[iph] > 0.0) {
tlogMoles[iph] = log(tPhMoles_ptr[iph]);

View file

@ -170,7 +170,7 @@ int VCS_SOLVE::vcs_solve_phaseStability(const int iph, const int ifunc,
if (printLvl > 3) {
vcs_printDeltaG(VCS_STATECALC_OLD);
}
vcs_dcopy(VCS_DATA_PTR(m_deltaGRxn_Deficient), VCS_DATA_PTR(m_deltaGRxn_old), m_numRxnRdc);
m_deltaGRxn_Deficient = m_deltaGRxn_old;
funcVal = vcs_phaseStabilityTest(iph);
if (funcVal > 0.0) {
iStab = 1;

View file

@ -15,52 +15,6 @@ using namespace std;
namespace VCSnonideal
{
#ifndef USE_MEMSET
void vcs_dzero(double* vector, int length)
{
for (int i = 0; i < length; i++) {
vector[i] = 0.0;
}
}
#endif
#ifndef USE_MEMSET
void vcs_izero(int* vector, int length)
{
for (int i = 0; i < length; i++) {
vector[i] = 0;
}
}
#endif
#ifndef USE_MEMSET
void vcs_dcopy(double* const vec_to, const double* const vec_from, int length)
{
for (int i = 0; i < length; i++) {
vec_to[i] = vec_from[i];
}
}
#endif
#ifndef USE_MEMSET
void vcs_icopy(int* vec_to, int* vec_from, int length)
{
for (int i = 0; i < length; i++) {
vec_to[i] = vec_from[i];
}
}
#endif
#ifndef USE_MEMSET
void vcs_vdzero(std::vector<double> &vvv, int len)
{
if (len < 0) {
std::fill(vvv.begin(), vvv.end(), 0.0);
} else {
std::fill_n(vvv.begin(), len, 0.0);
}
}
#endif
double vcs_l2norm(const std::vector<double> vec)
{
@ -76,33 +30,6 @@ double vcs_l2norm(const std::vector<double> vec)
return std::sqrt(sum / len);
}
#ifndef USE_MEMSET
void vcs_vizero(std::vector<int> &vvv, int len)
{
if (len < 0) {
std::fill(vvv.begin(), vvv.end(), 0.0);
} else {
std::fill_n(vvv.begin(), len, 0.0);
}
}
#endif
#ifndef USE_MEMSET
void vcs_vdcopy(std::vector<double> &vec_to,
const std::vector<double> & vec_from, int length)
{
std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
}
#endif
#ifndef USE_MEMSET
void vcs_vicopy(std::vector<int> &vec_to,
const std::vector<int> & vec_from, int length)
{
std::copy(vec_from.begin(), vec_from.begin() + length, vec_to.begin());
}
#endif
size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n)
{
size_t largest = j;