00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "NOX_Direction_NonlinearCG.H"
00043 #include "NOX_Abstract_Vector.H"
00044 #include "NOX_Abstract_Group.H"
00045 #include "NOX_Common.H"
00046 #include "Teuchos_ParameterList.hpp"
00047 #include "NOX_Solver_Generic.H"
00048 #include "NOX_Utils.H"
00049 #include "NOX_GlobalData.H"
00050
00051 using namespace NOX;
00052 using namespace NOX::Direction;
00053
00054 NonlinearCG::NonlinearCG(const Teuchos::RCP<NOX::GlobalData>& gd,
00055 Teuchos::ParameterList& params) :
00056 paramsPtr(0)
00057 {
00058 reset(gd, params);
00059 }
00060
00061
00062 bool NonlinearCG::
00063 reset(const Teuchos::RCP<NOX::GlobalData>& gd,
00064 Teuchos::ParameterList& params)
00065 {
00066 globalDataPtr = gd;
00067 utils = gd->getUtils();
00068 paramsPtr = ¶ms;
00069 Teuchos::ParameterList& nlcgParams = paramsPtr->sublist("Nonlinear CG");
00070 restartFrequency = nlcgParams.get("Restart Frequency", 10);
00071 doPrecondition = false;
00072 if( nlcgParams.get("Precondition", "Off") == "On" )
00073 doPrecondition = true;
00074 usePRbeta = false;
00075 if( nlcgParams.get("Orthogonalize", "Fletcher-Reeves") == "Polak-Ribiere" )
00076 usePRbeta = true;
00077
00078 return true;
00079 }
00080
00081 NonlinearCG::~NonlinearCG()
00082 {
00083 }
00084
00085
00086 bool NonlinearCG::compute(Abstract::Vector& dir, Abstract::Group& soln,
00087 const Solver::Generic& solver)
00088 {
00089 Abstract::Group::ReturnType ok;
00090
00091
00092 if(Teuchos::is_null(oldDirPtr))
00093 oldDirPtr = soln.getX().clone(NOX::ShapeCopy);
00094 if(Teuchos::is_null(oldDescentDirPtr))
00095 oldDescentDirPtr = soln.getX().clone(NOX::ShapeCopy);
00096
00097 if(Teuchos::is_null(diffVecPtr) && usePRbeta)
00098 diffVecPtr = soln.getX().clone(NOX::ShapeCopy);
00099 if(Teuchos::is_null(tmpVecPtr) && doPrecondition)
00100 tmpVecPtr = soln.getX().clone(NOX::ShapeCopy);
00101
00102
00103 oldSolnPtr = &solver.getPreviousSolutionGroup();
00104 const Abstract::Group& oldSoln(*oldSolnPtr);
00105
00106 niter = solver.getNumIterations();
00107
00108
00109
00110
00111 ok = soln.computeF();
00112 if (ok != Abstract::Group::Ok)
00113 {
00114 if (utils->isPrintType(Utils::Warning))
00115 utils->out() << "NOX::Direction::NonlinearCG::compute - Unable to compute F." << endl;
00116 return false;
00117 }
00118
00119 dir = soln.getF();
00120
00121 if(doPrecondition)
00122 {
00123 if(!soln.isJacobian())
00124 ok = soln.computeJacobian();
00125 if (ok != Abstract::Group::Ok)
00126 {
00127 if (utils->isPrintType(Utils::Warning))
00128 utils->out() << "NOX::Direction::NonlinearCG::compute - Unable to compute Jacobian." << endl;
00129 return false;
00130 }
00131
00132 *tmpVecPtr = dir;
00133
00134 ok = soln.applyRightPreconditioning(false, paramsPtr->sublist("Nonlinear CG").sublist("Linear Solver"), *tmpVecPtr, dir);
00135 if( ok != Abstract::Group::Ok )
00136 {
00137 if (utils->isPrintType(Utils::Warning))
00138 utils->out() << "NOX::Direction::NonlinearCG::compute - Unable to apply Right Preconditioner." << endl;
00139 return false;
00140 }
00141 }
00142
00143 dir.scale(-1.0);
00144
00145
00146
00147 beta = 0.0;
00148
00149 if( niter!=0 )
00150 {
00151
00152 if( usePRbeta )
00153 {
00154
00155 *diffVecPtr = dir;
00156 diffVecPtr->update(-1.0, *oldDescentDirPtr, 1.0);
00157
00158 double denominator = oldDescentDirPtr->innerProduct(oldSoln.getF());
00159
00160 beta = diffVecPtr->innerProduct(soln.getF()) / denominator;
00161
00162
00163 if( beta < 0.0 )
00164 {
00165 if (utils->isPrintType(Utils::OuterIteration))
00166 utils->out() << "BETA < 0, (" << beta << ") --> Resetting to zero" << endl;
00167 beta = 0.0;
00168 }
00169 }
00170 else
00171 {
00172
00173 double denominator = oldDescentDirPtr->innerProduct(oldSoln.getF());
00174
00175 beta = dir.innerProduct(soln.getF()) / denominator;
00176
00177 }
00178
00179
00180 if( (niter % restartFrequency) == 0 )
00181 {
00182 if( utils->isPrintType(Utils::OuterIteration) )
00183 utils->out() << "Resetting beta --> 0" << endl;
00184
00185 beta = 0 ;
00186 }
00187 }
00188
00189 *oldDescentDirPtr = dir;
00190
00191 dir.update(beta, *oldDirPtr, 1.0);
00192
00193 *oldDirPtr = dir;
00194
00195 return (ok == Abstract::Group::Ok);
00196 }
00197
00198 bool NonlinearCG::compute(Abstract::Vector& dir, Abstract::Group& soln,
00199 const Solver::LineSearchBased& solver)
00200 {
00201 return NOX::Direction::Generic::compute( dir, soln, solver );
00202 }