From 07eed363fe9f2d6287db674a44ef4353980b7256 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 15 May 2018 18:32:06 -0400 Subject: [PATCH] Use std::sort to eliminate need for vcs_optMax --- include/cantera/equil/vcs_internal.h | 11 ----------- src/equil/vcs_report.cpp | 24 +++++++----------------- src/equil/vcs_util.cpp | 25 ------------------------- 3 files changed, 7 insertions(+), 53 deletions(-) diff --git a/include/cantera/equil/vcs_internal.h b/include/cantera/equil/vcs_internal.h index 50b11d7f7..905784ef8 100644 --- a/include/cantera/equil/vcs_internal.h +++ b/include/cantera/equil/vcs_internal.h @@ -90,17 +90,6 @@ typedef double(*VCS_FUNC_PTR)(double xval, double Vtarget, */ double vcs_l2norm(const vector_fp& vec); -//! Finds the location of the maximum component in a double vector -/*! - * @param x pointer to a vector of doubles - * @param xSize pointer to a vector of doubles used as a multiplier to x[] - * before making the decision. Ignored if set to NULL. - * @param j lowest index to search from - * @param n highest index to search from - * @returns index of the greatest value on X(i) searched, j <= i < n - */ -size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n); - //! Returns a const char string representing the type of the species given by //! the first argument /*! diff --git a/src/equil/vcs_report.cpp b/src/equil/vcs_report.cpp index 54deabed6..c0718b568 100644 --- a/src/equil/vcs_report.cpp +++ b/src/equil/vcs_report.cpp @@ -12,24 +12,14 @@ namespace Cantera int VCS_SOLVE::vcs_report(int iconv) { bool inertYes = false; - std::vector sortindex(m_nsp, 0); - vector_fp xy(m_nsp, 0.0); // SORT DEPENDENT SPECIES IN DECREASING ORDER OF MOLES - for (size_t i = 0; i < m_nsp; ++i) { - sortindex[i] = i; - xy[i] = m_molNumSpecies_old[i]; - } - - // Sort the XY vector, the mole fraction vector, and the sort index vector, - // sortindex, according to the magnitude of the mole fraction vector. - for (size_t i = m_numComponents; i < m_numSpeciesRdc; ++i) { - size_t k = vcs_optMax(&xy[0], 0, i, m_numSpeciesRdc); - if (k != i) { - std::swap(xy[k], xy[i]); - std::swap(sortindex[k], sortindex[i]); - } + std::vector> x_order; + for (size_t i = 0; i < m_nsp; i++) { + x_order.push_back({-m_molNumSpecies_old[i], i}); } + std::sort(x_order.begin() + m_numComponents, + x_order.begin() + m_numSpeciesRdc); vcs_setFlagsVolPhases(false, VCS_STATECALC_OLD); vcs_dfe(VCS_STATECALC_OLD, 0, 0, m_nsp); @@ -71,7 +61,7 @@ int VCS_SOLVE::vcs_report(int iconv) plogf("\n"); } for (size_t i = m_numComponents; i < m_numSpeciesRdc; ++i) { - size_t j = sortindex[i]; + size_t j = x_order[i].second; plogf(" %-12.12s", m_speciesName[j]); writeline(' ', 13, false); @@ -239,7 +229,7 @@ int VCS_SOLVE::vcs_report(int iconv) plogf("| (MolNum ChemPot)|"); writeline('-', 147, true, true); for (size_t i = 0; i < m_nsp; ++i) { - size_t j = sortindex[i]; + size_t j = x_order[i].second; size_t pid = m_phaseID[j]; plogf(" %-12.12s", m_speciesName[j]); plogf(" %14.7E ", m_molNumSpecies_old[j]); diff --git a/src/equil/vcs_util.cpp b/src/equil/vcs_util.cpp index 66c60eb9b..6f77d5b43 100644 --- a/src/equil/vcs_util.cpp +++ b/src/equil/vcs_util.cpp @@ -30,31 +30,6 @@ double vcs_l2norm(const vector_fp& vec) return std::sqrt(sum / vec.size()); } -size_t vcs_optMax(const double* x, const double* xSize, size_t j, size_t n) -{ - size_t largest = j; - double big = x[j]; - if (xSize) { - assert(xSize[j] > 0.0); - big *= xSize[j]; - for (size_t i = j + 1; i < n; ++i) { - assert(xSize[i] > 0.0); - if ((x[i] * xSize[i]) > big) { - largest = i; - big = x[i] * xSize[i]; - } - } - } else { - for (size_t i = j + 1; i < n; ++i) { - if (x[i] > big) { - largest = i; - big = x[i]; - } - } - } - return largest; -} - const char* vcs_speciesType_string(int speciesStatus, int length) { switch (speciesStatus) {