1 |
#include <cmath> |
2 |
|
3 |
#include "minimizers/OOPSEMinimizer.hpp" |
4 |
#include "utils/Utility.hpp" |
5 |
|
6 |
CGFamilyMinmizer::CGFamilyMinmizer(SimInfo *theInfo |
7 |
MinimizerParameterSet * param) |
8 |
:OOPSEMinimizer(theInfo, 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" << std::endl; |
34 |
cout << "ftol = " << fTol |
35 |
<< "\tdeltaf = " << deltaF<< std::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" << std::endl; |
57 |
cout << "gnorm = " << gnorm |
58 |
<< "\trelativeGTol = " << relativeGTol<< std::endl; |
59 |
return CONVG_GTOL; |
60 |
} |
61 |
|
62 |
//absolute gradient tolerance test |
63 |
|
64 |
if (gnorm <= gTol) { |
65 |
cout << "absolute gradient tolerance test" << std::endl; |
66 |
cout << "gnorm = " << gnorm |
67 |
<< "\tgTol = " << gTol<< std::endl; |
68 |
return CONVG_ABSGTOL; |
69 |
} |
70 |
|
71 |
// did not converge yet |
72 |
return CONVG_UNCONVG; |
73 |
} |
74 |
|