From d086be8d3db9b66a7a29dc2b9b89c9dc60ffbbe8 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 28 Jul 2015 18:50:12 -0400 Subject: [PATCH] Eliminate do-while loops in favor of regular while loops --- src/equil/BasisOptimize.cpp | 51 +++++++++++--------------------- src/equil/vcs_elem_rearrange.cpp | 30 ++++++++----------- src/equil/vcs_solve_TP.cpp | 23 +++++++------- src/numerics/IDA_Solver.cpp | 6 ++-- src/numerics/RootFind.cpp | 4 +-- src/thermo/HMWSoln_input.cpp | 4 +-- src/thermo/RedlichKwongMFTP.cpp | 4 +-- src/tpx/Sub.cpp | 6 ++-- 8 files changed, 52 insertions(+), 76 deletions(-) diff --git a/src/equil/BasisOptimize.cpp b/src/equil/BasisOptimize.cpp index 3b016ddb7..5e6f53ab2 100644 --- a/src/equil/BasisOptimize.cpp +++ b/src/equil/BasisOptimize.cpp @@ -29,7 +29,6 @@ size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn, MultiPhase* mphase, vector_fp& formRxnMatrix) { size_t j, jj, k=0, kk, l, i, jl, ml; - bool lindep; std::string ename; std::string sname; /* @@ -139,16 +138,15 @@ size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn, MultiPhase* mphase, } double molSave = 0.0; - size_t jr = npos; + size_t jr = 0; /* * Top of a loop of some sort based on the index JR. JR is the * current number of component species found. */ - do { - ++jr; + while (jr < nComponents) { /* - Top of another loop point based on finding a linearly */ /* - independent species */ - do { + while (true) { /* * Search the remaining part of the mole number vector, molNum * for the largest remaining species. Return its identity. @@ -233,12 +231,10 @@ size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn, MultiPhase* mphase, /* **************************************************** */ /* **** IF NORM OF NEW ROW .LT. 1E-3 REJECT ********** */ /* **************************************************** */ - if (sa[jr] < 1.0e-6) { - lindep = true; - } else { - lindep = false; + if (sa[jr] > 1.0e-6) { + break; } - } while (lindep); + } /* ****************************************** */ /* **** REARRANGE THE DATA ****************** */ /* ****************************************** */ @@ -255,13 +251,9 @@ size_t BasisOptimize(int* usedZeroedSpecies, bool doFormRxn, MultiPhase* mphase, std::swap(orderVectorSpecies[jr], orderVectorSpecies[k]); } - /* - * If we haven't found enough components, go back - * and find some more. (nc -1 is used below, because - * jr is counted from 0, via the C convention. - */ - } while (jr < (nComponents-1)); - + // If we haven't found enough components, go back and find some more + jr++; + } if (! doFormRxn) { return nComponents; @@ -426,7 +418,6 @@ void ElemRearrange(size_t nComponents, const vector_fp& elementAbundances, { size_t j, k, l, i, jl, ml, jr, ielem, jj, kk=0; - bool lindep = false; size_t nelements = mphase->nElements(); std::string ename; /* @@ -495,14 +486,13 @@ void ElemRearrange(size_t nComponents, const vector_fp& elementAbundances, * Top of a loop of some sort based on the index JR. JR is the * current number independent elements found. */ - jr = npos; - do { - ++jr; + jr = 0; + while (jr < nComponents) { /* * Top of another loop point based on finding a linearly * independent element */ - do { + while (true) { /* * Search the element vector. We first locate elements that * are present in any amount. Then, we locate elements that @@ -597,12 +587,10 @@ void ElemRearrange(size_t nComponents, const vector_fp& elementAbundances, /* **************************************************** */ /* **** IF NORM OF NEW ROW .LT. 1E-6 REJECT ********** */ /* **************************************************** */ - if (sa[jr] < 1.0e-6) { - lindep = true; - } else { - lindep = false; + if (sa[jr] > 1.0e-6) { + break; } - } while (lindep); + } /* ****************************************** */ /* **** REARRANGE THE DATA ****************** */ /* ****************************************** */ @@ -621,12 +609,9 @@ void ElemRearrange(size_t nComponents, const vector_fp& elementAbundances, std::swap(orderVectorElements[jr], orderVectorElements[k]); } - /* - * If we haven't found enough components, go back - * and find some more. (nc -1 is used below, because - * jr is counted from 0, via the C convention. - */ - } while (jr < (nComponents-1)); + // If we haven't found enough components, go back and find some more + jr++; + }; } } diff --git a/src/equil/vcs_elem_rearrange.cpp b/src/equil/vcs_elem_rearrange.cpp index da4c06f68..6e6cef0b8 100644 --- a/src/equil/vcs_elem_rearrange.cpp +++ b/src/equil/vcs_elem_rearrange.cpp @@ -37,9 +37,9 @@ int VCS_SOLVE::vcs_elem_rearrange(double* const aw, double* const sa, * Use a temporary work array for the element numbers * Also make sure the value of test is unique. */ - bool lindep = false; + bool lindep = true; double test = -1.0E10; - do { + while (lindep) { lindep = false; for (size_t i = 0; i < m_numElemConstraints; ++i) { test -= 1.0; @@ -48,21 +48,20 @@ int VCS_SOLVE::vcs_elem_rearrange(double* const aw, double* const sa, lindep = true; } } - } while (lindep); + } /* * Top of a loop of some sort based on the index JR. JR is the * current number independent elements found. */ - size_t jr = npos; - do { - ++jr; + size_t jr = 0; + while (jr < ncomponents) { size_t k; /* * Top of another loop point based on finding a linearly * independent species */ - do { + while (true) { /* * Search the remaining part of the mole fraction vector, AW, * for the largest remaining species. Return its identity in K. @@ -140,12 +139,10 @@ int VCS_SOLVE::vcs_elem_rearrange(double* const aw, double* const sa, /* **************************************************** */ /* **** IF NORM OF NEW ROW .LT. 1E-6 REJECT ********** */ /* **************************************************** */ - if (sa[jr] < 1.0e-6) { - lindep = true; - } else { - lindep = false; + if (sa[jr] > 1.0e-6) { + break; } - } while (lindep); + } /* ****************************************** */ /* **** REARRANGE THE DATA ****************** */ /* ****************************************** */ @@ -162,12 +159,9 @@ int VCS_SOLVE::vcs_elem_rearrange(double* const aw, double* const sa, std::swap(aw[jr], aw[k]); } - /* - * If we haven't found enough components, go back - * and find some more. (nc -1 is used below, because - * jr is counted from 0, via the C convention. - */ - } while (jr < (ncomponents-1)); + // If we haven't found enough components, go back and find some more. + jr++; + } return VCS_SUCCESS; } diff --git a/src/equil/vcs_solve_TP.cpp b/src/equil/vcs_solve_TP.cpp index d867f944c..0afa33e61 100644 --- a/src/equil/vcs_solve_TP.cpp +++ b/src/equil/vcs_solve_TP.cpp @@ -2478,7 +2478,6 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[], double ss[], double test, bool* const usedZeroedSpecies) { size_t k; - bool lindep; size_t juse = npos; size_t jlose = npos; double* scrxn_ptr; @@ -2547,16 +2546,15 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[], } } - size_t jr = npos; + size_t jr = 0; /* * Top of a loop of some sort based on the index JR. JR is the * current number of component species found. */ - do { - ++jr; + while (jr < ncTrial) { /* - Top of another loop point based on finding a linearly */ /* - independent species */ - do { + while (true) { /* * Search the remaining part of the mole fraction vector, AW, * for the largest remaining species. Return its identity in K. @@ -2732,8 +2730,10 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[], /* **************************************************** */ /* **** IF NORM OF NEW ROW .LT. 1E-3 REJECT ********** */ /* **************************************************** */ - lindep = (sa[jr] < 1.0e-6); - } while (lindep); + if (sa[jr] > 1.0e-6) { + break; + } + } /* ****************************************** */ /* **** REARRANGE THE DATA ****************** */ /* ****************************************** */ @@ -2768,12 +2768,9 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[], /* - entry point from up above */ L_END_LOOP: ; - /* - * If we haven't found enough components, go back - * and find some more. (nc -1 is used below, because - * jr is counted from 0, via the C convention. - */ - } while (jr < (ncTrial-1)); + // If we haven't found enough components, go back and find some more. + jr++; + } if (doJustComponents) { goto L_CLEANUP; diff --git a/src/numerics/IDA_Solver.cpp b/src/numerics/IDA_Solver.cpp index 3aaebbb8e..d83454d42 100644 --- a/src/numerics/IDA_Solver.cpp +++ b/src/numerics/IDA_Solver.cpp @@ -528,13 +528,13 @@ void IDA_Solver::correctInitial_YaYp_given_Yd(doublereal* y, doublereal* yp, dou int IDA_Solver::solve(double tout) { - double tretn; + double tretn = tout - 1000; int flag; flag = IDASetStopTime(m_ida_mem, tout); if (flag != IDA_SUCCESS) { throw IDA_Err(" IDA error encountered."); } - do { + while (tretn < tout) { if (tout <= m_tcurrent) { throw IDA_Err(" tout <= tcurrent"); } @@ -552,7 +552,7 @@ int IDA_Solver::solve(double tout) } m_tcurrent = tretn; m_deltat = m_tcurrent - m_told; - } while (tretn < tout); + }; if (flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) { throw IDA_Err(" IDA error encountered."); diff --git a/src/numerics/RootFind.cpp b/src/numerics/RootFind.cpp index 757abf556..25bd76818 100644 --- a/src/numerics/RootFind.cpp +++ b/src/numerics/RootFind.cpp @@ -381,7 +381,7 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun // --------------------------------------------------------------------------------------------- // MAIN LOOP // --------------------------------------------------------------------------------------------- - do { + while (!converged && its < itmax) { /* * Find an estimate of the next point, xnew, to try based on * a linear approximation from the last two points. @@ -910,7 +910,7 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun } } } - } while (! converged && its < itmax); + } done: if (converged) { diff --git a/src/thermo/HMWSoln_input.cpp b/src/thermo/HMWSoln_input.cpp index 894acdc20..e5a73c576 100644 --- a/src/thermo/HMWSoln_input.cpp +++ b/src/thermo/HMWSoln_input.cpp @@ -1567,7 +1567,7 @@ void HMWSoln::initThermoXML(XML_Node& phaseNode, const std::string& id_) getMoleFractions(DATA_PTR(mf)); bool notDone = true; - do { + while (notDone) { double sum = 0.0; size_t kMaxC = npos; double MaxC = 0.0; @@ -1626,7 +1626,7 @@ void HMWSoln::initThermoXML(XML_Node& phaseNode, const std::string& id_) } else { notDone = false; } - } while (notDone); + } } void HMWSoln::calcIMSCutoffParams_() diff --git a/src/thermo/RedlichKwongMFTP.cpp b/src/thermo/RedlichKwongMFTP.cpp index a072c33e4..152417343 100644 --- a/src/thermo/RedlichKwongMFTP.cpp +++ b/src/thermo/RedlichKwongMFTP.cpp @@ -956,7 +956,7 @@ doublereal RedlichKwongMFTP::liquidVolEst(doublereal TKelvin, doublereal& presGu bool foundLiq = false; int m = 0; - do { + while (m < 100 && !foundLiq) { int nsol = NicholsSolve(TKelvin, pres, atmp, btmp, Vroot); @@ -970,7 +970,7 @@ doublereal RedlichKwongMFTP::liquidVolEst(doublereal TKelvin, doublereal& presGu } else { foundLiq = true; } - } while ((m < 100) && (!foundLiq)); + } if (foundLiq) { v = Vroot[0]; diff --git a/src/tpx/Sub.cpp b/src/tpx/Sub.cpp index 87684558d..72cf1b7b7 100644 --- a/src/tpx/Sub.cpp +++ b/src/tpx/Sub.cpp @@ -88,8 +88,8 @@ double Substance::Tsat(double p) if (T >= Tcrit()) { T = 0.5*(Tcrit() - Tmin()); } - double dp; - do { + double dp = 10*tol; + while (fabs(dp) > tol) { if (T > Tcrit()) { T = Tcrit() - 0.001; } @@ -109,7 +109,7 @@ double Substance::Tsat(double p) T = Tsave; throw TPX_Error("Substance::Tsat", "No convergence"); } - } while (fabs(dp) > tol); + } double tsat = T; T = Tsave; return tsat;