| 1 |
#include <math.h> |
| 2 |
|
| 3 |
#include "minimizers/OOPSEMinimizer.hpp" |
| 4 |
#include "utils/Utility.hpp" |
| 5 |
|
| 6 |
CGFamilyMinmizer::CGFamilyMinmizer(SimInfo *theInfo, ForceFields* the_ff , |
| 7 |
MinimizerParameterSet * param) |
| 8 |
:OOPSEMinimizer(theInfo, the_ff, param){ |
| 9 |
prevG.resize(ndim); |
| 10 |
prevX.resize(ndim); |
| 11 |
direction.resize(ndim); |
| 12 |
|
| 13 |
stepSize = paramSet->getStepSize(); |
| 14 |
|
| 15 |
} |
| 16 |
int CGFamilyMinmizer::checkConvg(){ |
| 17 |
double fTol; |
| 18 |
double relativeFTol; // relative tolerance |
| 19 |
double deltaF; |
| 20 |
double gTol; |
| 21 |
double relativeGTol; |
| 22 |
double gnorm; |
| 23 |
|
| 24 |
|
| 25 |
// test function tolerance test |
| 26 |
fTol =paramSet->getFTol(); |
| 27 |
relativeFTol = fTol * std::max(1.0,fabs(curF)); // relative tolerance |
| 28 |
deltaF = prevF - curF; |
| 29 |
|
| 30 |
if (fabs(deltaF) <= relativeFTol) { |
| 31 |
|
| 32 |
if (bVerbose){ |
| 33 |
cout << "function value tolerance test passed" << endl; |
| 34 |
cout << "ftol = " << fTol |
| 35 |
<< "\tdeltaf = " << deltaF<< endl; |
| 36 |
} |
| 37 |
return CONVG_FTOL; |
| 38 |
} |
| 39 |
|
| 40 |
//gradient tolerance test |
| 41 |
gTol = paramSet->getGTol(); |
| 42 |
relativeGTol = gTol * std::max(1.0,fabs(curF)); |
| 43 |
|
| 44 |
#ifndef IS_MPI |
| 45 |
gnorm = sqrt(dotProduct(curG, curG)); |
| 46 |
#else |
| 47 |
double localDP; |
| 48 |
double globalDP; |
| 49 |
|
| 50 |
localDP = dotProduct(curG, curG); |
| 51 |
MPI_Allreduce(&localDP, &globalDP, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
| 52 |
gnorm = sqrt(globalDP); |
| 53 |
#endif |
| 54 |
|
| 55 |
if (gnorm <= relativeGTol) { |
| 56 |
cout << "gradient tolerance test" << endl; |
| 57 |
cout << "gnorm = " << gnorm |
| 58 |
<< "\trelativeGTol = " << relativeGTol<< endl; |
| 59 |
return CONVG_GTOL; |
| 60 |
} |
| 61 |
|
| 62 |
//absolute gradient tolerance test |
| 63 |
|
| 64 |
if (gnorm <= gTol) { |
| 65 |
cout << "absolute gradient tolerance test" << endl; |
| 66 |
cout << "gnorm = " << gnorm |
| 67 |
<< "\tgTol = " << gTol<< endl; |
| 68 |
return CONVG_ABSGTOL; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
//test step tolerance test |
| 73 |
|
| 74 |
double stepTol = paramSet->getStepTol(); |
| 75 |
double snorm = stepTolNorm(); |
| 76 |
double xnorm = Norm2(curX); |
| 77 |
double sTol = stepTol*max(1.0, xnorm); |
| 78 |
if (snorm <= sTol) { |
| 79 |
cout << "step tolerance test passed" << endl; |
| 80 |
cout << "stol = " << sTol |
| 81 |
<< "\tsnorm = " << snorm<< endl; |
| 82 |
return CONVG_STEPTOL; |
| 83 |
} |
| 84 |
*/ |
| 85 |
// did not converge yet |
| 86 |
return CONVG_UNCONVG; |
| 87 |
} |
| 88 |
|