| 1 | 
#include "OOPSEMinimizer.hpp" | 
| 2 | 
#include "Utility.hpp" | 
| 3 | 
 | 
| 4 | 
SDMinimizer::SDMinimizer(SimInfo *theInfo, ForceFields* the_ff , | 
| 5 | 
                                                               MinimizerParameterSet * param) | 
| 6 | 
                         :OOPSEMinimizer(theInfo, the_ff, param){ | 
| 7 | 
 | 
| 8 | 
  direction.resize(ndim); | 
| 9 | 
  stepSize = paramSet->getStepSize();                          | 
| 10 | 
} | 
| 11 | 
 | 
| 12 | 
void SDMinimizer::init(){ | 
| 13 | 
 | 
| 14 | 
  calcG(); | 
| 15 | 
   | 
| 16 | 
  for(int i = 0; i < direction.size(); i++){     | 
| 17 | 
    direction[i] = -curG[i]; | 
| 18 | 
  }   | 
| 19 | 
}   | 
| 20 | 
 | 
| 21 | 
int SDMinimizer::step(){ | 
| 22 | 
  int lsStatus; | 
| 23 | 
  | 
| 24 | 
  prevF = curF; | 
| 25 | 
   | 
| 26 | 
  //optimize along the search direction and reset minimum point value | 
| 27 | 
    lsStatus = doLineSearch(direction, stepSize); | 
| 28 | 
 | 
| 29 | 
  if (lsStatus < 0) | 
| 30 | 
    return -1; | 
| 31 | 
  else | 
| 32 | 
    return 1; | 
| 33 | 
} | 
| 34 | 
 | 
| 35 | 
void SDMinimizer::prepareStep(){ | 
| 36 | 
   | 
| 37 | 
  for(int i = 0; i < direction.size(); i++){     | 
| 38 | 
    direction[i] = -curG[i]; | 
| 39 | 
  }   | 
| 40 | 
}   | 
| 41 | 
int SDMinimizer::checkConvg(){ | 
| 42 | 
  double fTol; | 
| 43 | 
  double relativeFTol;  // relative tolerance | 
| 44 | 
  double deltaF; | 
| 45 | 
  double gTol; | 
| 46 | 
  double relativeGTol; | 
| 47 | 
  double gnorm; | 
| 48 | 
   | 
| 49 | 
 | 
| 50 | 
  // test function tolerance test | 
| 51 | 
  fTol =paramSet->getFTol(); | 
| 52 | 
  relativeFTol = fTol * std::max(1.0,fabs(curF));  // relative tolerance | 
| 53 | 
  deltaF = prevF - curF; | 
| 54 | 
   | 
| 55 | 
  if (fabs(deltaF) <= relativeFTol) { | 
| 56 | 
 | 
| 57 | 
    if (bVerbose){ | 
| 58 | 
      cout << "function value tolerance test passed" << endl; | 
| 59 | 
      cout << "ftol = " << fTol | 
| 60 | 
             << "\tdeltaf = " << deltaF<< endl; | 
| 61 | 
    } | 
| 62 | 
    return CONVG_FTOL; | 
| 63 | 
  } | 
| 64 | 
   | 
| 65 | 
//gradient tolerance test | 
| 66 | 
  gTol = paramSet->getGTol(); | 
| 67 | 
  relativeGTol = gTol * std::max(1.0,fabs(curF)); | 
| 68 | 
 | 
| 69 | 
#ifndef IS_MPI | 
| 70 | 
  gnorm = sqrt(dotProduct(curG, curG)); | 
| 71 | 
#else | 
| 72 | 
  double localDP; | 
| 73 | 
  double globalDP; | 
| 74 | 
 | 
| 75 | 
  localDP = dotProduct(curG, curG); | 
| 76 | 
  MPI_Allreduce(&localDP, &globalDP, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD);   | 
| 77 | 
  gnorm  = sqrt(globalDP); | 
| 78 | 
#endif | 
| 79 | 
 | 
| 80 | 
  if (gnorm <= relativeGTol) { | 
| 81 | 
      cout << "gradient tolerance test" << endl; | 
| 82 | 
      cout << "gnorm = " << gnorm | 
| 83 | 
             << "\trelativeGTol = " << relativeGTol<< endl; | 
| 84 | 
    return CONVG_GTOL; | 
| 85 | 
  } | 
| 86 | 
   | 
| 87 | 
  //absolute gradient tolerance test | 
| 88 | 
 | 
| 89 | 
  if (gnorm <= gTol) { | 
| 90 | 
      cout << "absolute gradient tolerance test" << endl; | 
| 91 | 
      cout << "gnorm = " << gnorm | 
| 92 | 
             << "\tgTol = " << gTol<< endl; | 
| 93 | 
    return CONVG_ABSGTOL; | 
| 94 | 
  } | 
| 95 | 
 | 
| 96 | 
  return CONVG_UNCONVG; | 
| 97 | 
} | 
| 98 | 
 |