Eliminate do-while loops in favor of regular while loops

This commit is contained in:
Ray Speth 2015-07-28 18:50:12 -04:00
parent a6e390dfc5
commit d086be8d3d
8 changed files with 52 additions and 76 deletions

View file

@ -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++;
};
}
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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.");

View file

@ -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) {

View file

@ -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_()

View file

@ -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];

View file

@ -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;