| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// NOX: An Object-Oriented Nonlinear Solver Package |
| 4 |
// |
| 5 |
// Copyright 2002 NTESS and the NOX contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
#include "Teuchos_ConfigDefs.hpp" |
| 10 |
#include "Teuchos_UnitTestHarness.hpp" |
| 11 |
|
| 12 |
// NOX Objects |
| 13 |
#include "NOX.H" |
| 14 |
#include "NOX_Thyra.H" |
| 15 |
|
| 16 |
// Trilinos Objects |
| 17 |
#include "Teuchos_Comm.hpp" |
| 18 |
#include "Teuchos_ParameterList.hpp" |
| 19 |
#include "Teuchos_RCP.hpp" |
| 20 |
#include "Teuchos_FancyOStream.hpp" |
| 21 |
#include "Teuchos_AbstractFactoryStd.hpp" |
| 22 |
|
| 23 |
#include "Tpetra_Core.hpp" |
| 24 |
#include "Tpetra_Vector.hpp" |
| 25 |
|
| 26 |
#include "BelosTypes.hpp" |
| 27 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 28 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 29 |
#include "ME_Tpetra_1DFEM.hpp" |
| 30 |
|
| 31 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 32 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 33 |
#include "NOX_Observer_ReusePreconditioner.hpp" |
| 34 |
#include "NOX_Observer_ReusePreconditionerFactory.hpp" |
| 35 |
#include "NOX_Observer_Print.hpp" |
| 36 |
#include "NOX_Observer_Vector.hpp" |
| 37 |
#include "Thyra_VectorBase.hpp" |
| 38 |
#include "Thyra_VectorStdOps.hpp" |
| 39 |
|
| 40 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, AnalyticJacobian_NoPrec) |
| 41 |
{ |
| 42 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 43 |
|
| 44 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 45 |
|
| 46 |
// Get default Tpetra template types |
| 47 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 48 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 49 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 50 |
typedef Tpetra::Vector<>::node_type Node; |
| 51 |
|
| 52 |
// Create the model evaluator object |
| 53 |
Scalar x00 = 0.0; |
| 54 |
Scalar x01 = 1.0; |
| 55 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 56 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 57 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 58 |
|
| 59 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 60 |
|
| 61 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 62 |
p->set("Linear Solver Type", "Belos"); |
| 63 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 64 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 65 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 66 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 67 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 68 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 69 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 70 |
p->set("Preconditioner Type", "None"); |
| 71 |
builder.setParameterList(p); |
| 72 |
|
| 73 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 74 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 75 |
|
| 76 |
model->set_W_factory(lowsFactory); |
| 77 |
|
| 78 |
// Create the initial guess |
| 79 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 80 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 81 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 82 |
|
| 83 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 84 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 85 |
|
| 86 |
nox_group->computeF(); |
| 87 |
|
| 88 |
// Create the NOX status tests and the solver |
| 89 |
// Create the convergence tests |
| 90 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 91 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 92 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 93 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 94 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 95 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 96 |
converged->addStatusTest(absresid); |
| 97 |
converged->addStatusTest(wrms); |
| 98 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 99 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 100 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 101 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 102 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 103 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 104 |
combo->addStatusTest(fv); |
| 105 |
combo->addStatusTest(converged); |
| 106 |
combo->addStatusTest(maxiters); |
| 107 |
|
| 108 |
// Create nox parameter list |
| 109 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 110 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 111 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 112 |
|
| 113 |
// Set output parameters |
| 114 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",true); |
| 115 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",true); |
| 116 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",true); |
| 117 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",true); |
| 118 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 119 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",true); |
| 120 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",true); |
| 121 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",true); |
| 122 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 123 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 124 |
|
| 125 |
// Create the solver |
| 126 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 127 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 128 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 129 |
|
| 130 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 131 |
|
| 132 |
Teuchos::TimeMonitor::summarize(); |
| 133 |
} |
| 134 |
|
| 135 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, AnalyticJacobian_Ifpack2Prec) |
| 136 |
{ |
| 137 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 138 |
|
| 139 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 140 |
|
| 141 |
// Get default Tpetra template types |
| 142 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 143 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 144 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 145 |
typedef Tpetra::Vector<>::node_type Node; |
| 146 |
|
| 147 |
// Create the model evaluator object |
| 148 |
Scalar x00 = 0.0; |
| 149 |
Scalar x01 = 1.0; |
| 150 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 151 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 152 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 153 |
|
| 154 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 155 |
|
| 156 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 157 |
p->set("Linear Solver Type", "Belos"); |
| 158 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 159 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 160 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 161 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 162 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 163 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 164 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 165 |
p->set("Preconditioner Type", "Ifpack2"); |
| 166 |
Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2"); |
| 167 |
ifpackList.set("Prec Type", "ILUT"); |
| 168 |
|
| 169 |
builder.setParameterList(p); |
| 170 |
|
| 171 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 172 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 173 |
|
| 174 |
model->set_W_factory(lowsFactory); |
| 175 |
|
| 176 |
// Create the initial guess |
| 177 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 178 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 179 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 180 |
|
| 181 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 182 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 183 |
|
| 184 |
nox_group->computeF(); |
| 185 |
|
| 186 |
// Create the NOX status tests and the solver |
| 187 |
// Create the convergence tests |
| 188 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 189 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 190 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 191 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 192 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 193 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 194 |
converged->addStatusTest(absresid); |
| 195 |
converged->addStatusTest(wrms); |
| 196 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 197 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 198 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 199 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 200 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 201 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 202 |
combo->addStatusTest(fv); |
| 203 |
combo->addStatusTest(converged); |
| 204 |
combo->addStatusTest(maxiters); |
| 205 |
|
| 206 |
// Create nox parameter list |
| 207 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 208 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 209 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 210 |
|
| 211 |
// Set output parameters |
| 212 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",true); |
| 213 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",true); |
| 214 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",true); |
| 215 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",true); |
| 216 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 217 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",true); |
| 218 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",true); |
| 219 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",true); |
| 220 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 221 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 222 |
|
| 223 |
// Create the solver |
| 224 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 225 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 226 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 227 |
|
| 228 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 229 |
|
| 230 |
Teuchos::TimeMonitor::summarize(); |
| 231 |
} |
| 232 |
|
| 233 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, JFNK_NoPrec) |
| 234 |
{ |
| 235 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 236 |
|
| 237 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 238 |
|
| 239 |
// Get default Tpetra template types |
| 240 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 241 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 242 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 243 |
typedef Tpetra::Vector<>::node_type Node; |
| 244 |
|
| 245 |
// Create the model evaluator object |
| 246 |
Scalar x00 = 0.0; |
| 247 |
Scalar x01 = 1.0; |
| 248 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 249 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 250 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 251 |
|
| 252 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 253 |
|
| 254 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 255 |
p->set("Linear Solver Type", "Belos"); |
| 256 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 257 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 258 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 259 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 260 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 261 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 262 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 263 |
p->set("Preconditioner Type", "None"); |
| 264 |
builder.setParameterList(p); |
| 265 |
|
| 266 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 267 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 268 |
|
| 269 |
model->set_W_factory(lowsFactory); |
| 270 |
|
| 271 |
// Create the initial guess |
| 272 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 273 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 274 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 275 |
|
| 276 |
// Create the JFNK operator |
| 277 |
Teuchos::ParameterList printParams; |
| 278 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 279 |
jfnkParams->set("Difference Type","Forward"); |
| 280 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 281 |
jfnkParams->set("lambda",1.0e-4); |
| 282 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<Scalar> > jfnkOp = |
| 283 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<Scalar>(printParams)); |
| 284 |
jfnkOp->setParameterList(jfnkParams); |
| 285 |
jfnkParams->print(out); |
| 286 |
|
| 287 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 288 |
Teuchos::RCP<Thyra::ModelEvaluator<Scalar> > thyraModel = |
| 289 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<Scalar>(model)); |
| 290 |
|
| 291 |
// Create the NOX::Thyra::Group |
| 292 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 293 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, jfnkOp, lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 294 |
|
| 295 |
nox_group->computeF(); |
| 296 |
|
| 297 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 298 |
// This creates a circular dependency, so use a weak pointer. |
| 299 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 300 |
|
| 301 |
// Create a preconditioner |
| 302 |
|
| 303 |
// Create the NOX status tests and the solver |
| 304 |
// Create the convergence tests |
| 305 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 306 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 307 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 308 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 309 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 310 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 311 |
converged->addStatusTest(absresid); |
| 312 |
converged->addStatusTest(wrms); |
| 313 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 314 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 315 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 316 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 317 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 318 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 319 |
combo->addStatusTest(fv); |
| 320 |
combo->addStatusTest(converged); |
| 321 |
combo->addStatusTest(maxiters); |
| 322 |
|
| 323 |
// Create nox parameter list |
| 324 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 325 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 326 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 327 |
|
| 328 |
// Set output parameters |
| 329 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",true); |
| 330 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",true); |
| 331 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",true); |
| 332 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",true); |
| 333 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 334 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",true); |
| 335 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",true); |
| 336 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",true); |
| 337 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 338 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 339 |
|
| 340 |
// Create the solver |
| 341 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 342 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 343 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 344 |
|
| 345 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 346 |
|
| 347 |
Teuchos::TimeMonitor::summarize(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, JFNK_UserPrec) |
| 352 |
{ |
| 353 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 354 |
|
| 355 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 356 |
|
| 357 |
// Get default Tpetra template types |
| 358 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 359 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 360 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 361 |
typedef Tpetra::Vector<>::node_type Node; |
| 362 |
|
| 363 |
// Create the model evaluator object |
| 364 |
Scalar x00 = 0.0; |
| 365 |
Scalar x01 = 1.0; |
| 366 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 367 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 368 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 369 |
|
| 370 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 371 |
|
| 372 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 373 |
p->set("Linear Solver Type", "Belos"); |
| 374 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 375 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 376 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 377 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 378 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 379 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 380 |
belosList.sublist("VerboseObject").set("Verbosity Level", "medium"); |
| 381 |
p->set("Preconditioner Type", "None"); |
| 382 |
builder.setParameterList(p); |
| 383 |
|
| 384 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 385 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 386 |
|
| 387 |
model->set_W_factory(lowsFactory); |
| 388 |
|
| 389 |
// Create the initial guess |
| 390 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 391 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 392 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 393 |
|
| 394 |
// Create the JFNK operator |
| 395 |
Teuchos::ParameterList printParams; |
| 396 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 397 |
jfnkParams->set("Difference Type","Forward"); |
| 398 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 399 |
jfnkParams->set("lambda",1.0e-4); |
| 400 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<Scalar> > jfnkOp = |
| 401 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<Scalar>(printParams)); |
| 402 |
jfnkOp->setParameterList(jfnkParams); |
| 403 |
jfnkParams->print(out); |
| 404 |
|
| 405 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 406 |
Teuchos::RCP<Thyra::ModelEvaluator<Scalar> > thyraModel = |
| 407 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<Scalar>(model)); |
| 408 |
|
| 409 |
// Create the Preconditioner operator |
| 410 |
Teuchos::RCP<Thyra::PreconditionerBase<Scalar> > precOp = |
| 411 |
thyraModel->create_W_prec(); |
| 412 |
|
| 413 |
// Create the NOX::Thyra::Group |
| 414 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 415 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, jfnkOp, lowsFactory, precOp, Teuchos::null, Teuchos::null)); |
| 416 |
|
| 417 |
nox_group->computeF(); |
| 418 |
|
| 419 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 420 |
// This creates a circular dependency, so use a weak pointer. |
| 421 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 422 |
|
| 423 |
// Create the NOX status tests and the solver |
| 424 |
// Create the convergence tests |
| 425 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 426 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 427 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 428 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 429 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 430 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 431 |
converged->addStatusTest(absresid); |
| 432 |
converged->addStatusTest(wrms); |
| 433 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 434 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 435 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 436 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 437 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 438 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 439 |
combo->addStatusTest(fv); |
| 440 |
combo->addStatusTest(converged); |
| 441 |
combo->addStatusTest(maxiters); |
| 442 |
|
| 443 |
// Create nox parameter list |
| 444 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 445 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 446 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 447 |
|
| 448 |
// Set output parameters |
| 449 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",true); |
| 450 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",true); |
| 451 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",true); |
| 452 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",true); |
| 453 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 454 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",true); |
| 455 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",true); |
| 456 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",true); |
| 457 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 458 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 459 |
|
| 460 |
// Create the solver |
| 461 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 462 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 463 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 464 |
|
| 465 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 466 |
|
| 467 |
Teuchos::TimeMonitor::summarize(); |
| 468 |
} |
| 469 |
|
| 470 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, AnalyticJacobian_UserUpdatedIfpack2Prec) |
| 471 |
{ |
| 472 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 473 |
|
| 474 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 475 |
|
| 476 |
auto utils = Teuchos::rcp(new NOX::Utils(0,comm->getRank(),0,8)); |
| 477 |
utils->out() << "\n\n"; |
| 478 |
|
| 479 |
// Get default Tpetra template types |
| 480 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 481 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 482 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 483 |
typedef Tpetra::Vector<>::node_type Node; |
| 484 |
|
| 485 |
// Create the model evaluator object |
| 486 |
Scalar x00 = 0.0; |
| 487 |
Scalar x01 = 1.0; |
| 488 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 489 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 490 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 491 |
|
| 492 |
// Set the source term high to make the problem more difficult |
| 493 |
::Thyra::put_scalar<double>(10.0,Teuchos::rcp_const_cast<::Thyra::VectorBase<double>>(model->getNominalValues().get_p(2)).ptr()); |
| 494 |
|
| 495 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 496 |
|
| 497 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 498 |
p->set("Linear Solver Type", "Belos"); |
| 499 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 500 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 501 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 502 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 503 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 504 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 505 |
belosList.sublist("VerboseObject").set("Verbosity Level", "none"); |
| 506 |
p->set("Preconditioner Type", "Ifpack2"); |
| 507 |
Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2"); |
| 508 |
ifpackList.set("Prec Type", "ILUT"); |
| 509 |
ifpackList.set("Overlap", 3); |
| 510 |
ifpackList.sublist("Ifpack2 Settings").set("fact: ilut level-of-fill",2.0); |
| 511 |
|
| 512 |
builder.setParameterList(p); |
| 513 |
|
| 514 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 515 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 516 |
|
| 517 |
model->set_W_factory(lowsFactory); |
| 518 |
|
| 519 |
// Create the initial guess |
| 520 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 521 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 522 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 523 |
|
| 524 |
const bool updatePreconditioner = false; // User controlled updating of preconditioner |
| 525 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 526 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null, Teuchos::null, Teuchos::null, false, updatePreconditioner)); |
| 527 |
|
| 528 |
nox_group->computeF(); |
| 529 |
|
| 530 |
// Create the NOX status tests and the solver |
| 531 |
// Create the convergence tests |
| 532 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 533 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 534 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 535 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 536 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 537 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 538 |
converged->addStatusTest(absresid); |
| 539 |
converged->addStatusTest(wrms); |
| 540 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 541 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 542 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 543 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 544 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 545 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 546 |
combo->addStatusTest(fv); |
| 547 |
combo->addStatusTest(converged); |
| 548 |
combo->addStatusTest(maxiters); |
| 549 |
|
| 550 |
// Create nox parameter list |
| 551 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 552 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 553 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 554 |
|
| 555 |
// Add in Observsers. This test specifically adds in a |
| 556 |
// preconditioner control observer |
| 557 |
Teuchos::RCP<NOX::ObserverReusePreconditioner> reuse_prec_observer; |
| 558 |
{ |
| 559 |
Teuchos::ParameterList precControlPL; |
| 560 |
precControlPL.set("Update prec at start of nonlinear solve",true); |
| 561 |
precControlPL.set("Update prec after this many nonlinear iterations",2); |
| 562 |
precControlPL.set("Update prec after this many stalled linear solves",1); |
| 563 |
precControlPL.set("Max linear iterations for stall",80); |
| 564 |
auto pc_observer = NOX::createReusePreconditionerObserver(precControlPL); |
| 565 |
auto print_observer = Teuchos::rcp(new NOX::ObserverPrint(utils)); |
| 566 |
auto observers = Teuchos::rcp(new NOX::ObserverVector); |
| 567 |
observers->pushBack(pc_observer); |
| 568 |
observers->pushBack(print_observer); |
| 569 |
nl_params->sublist("Solver Options").set<Teuchos::RCP<NOX::Observer>>("Observer",observers); |
| 570 |
|
| 571 |
// Save off observer to for unit testing |
| 572 |
reuse_prec_observer = Teuchos::rcp_dynamic_cast<NOX::ObserverReusePreconditioner>(pc_observer); |
| 573 |
} |
| 574 |
|
| 575 |
// Set output parameters |
| 576 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",false); |
| 577 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",false); |
| 578 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",false); |
| 579 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",false); |
| 580 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",false); |
| 581 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",false); |
| 582 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",false); |
| 583 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",false); |
| 584 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",false); |
| 585 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",false); |
| 586 |
|
| 587 |
// Create the solver |
| 588 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 589 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 590 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 591 |
|
| 592 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 593 |
TEST_EQUALITY(reuse_prec_observer->getNumPreconditionerUpdates(), size_t(3)); |
| 594 |
TEST_EQUALITY(reuse_prec_observer->getNumNonlinearSolvesCount(), size_t(1)); |
| 595 |
|
| 596 |
utils->out() << "\n"; |
| 597 |
Teuchos::TimeMonitor::summarize(); |
| 598 |
} |
| 599 |
|
| 600 |
TEUCHOS_UNIT_TEST(NOX_Tpetra_1DFEM, AnalyticJacobian_UserUpdatedIfpack2PrecMultipleNonlinearSolves) |
| 601 |
{ |
| 602 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 603 |
|
| 604 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 605 |
|
| 606 |
auto utils = Teuchos::rcp(new NOX::Utils(0,comm->getRank(),0,8)); |
| 607 |
utils->out() << "\n\n"; |
| 608 |
|
| 609 |
// Get default Tpetra template types |
| 610 |
typedef Tpetra::Vector<>::scalar_type Scalar; |
| 611 |
typedef Tpetra::Vector<>::local_ordinal_type LO; |
| 612 |
typedef Tpetra::Vector<>::global_ordinal_type GO; |
| 613 |
typedef Tpetra::Vector<>::node_type Node; |
| 614 |
|
| 615 |
// Create the model evaluator object |
| 616 |
Scalar x00 = 0.0; |
| 617 |
Scalar x01 = 1.0; |
| 618 |
const Tpetra::global_size_t numGlobalElements = 100; |
| 619 |
Teuchos::RCP<EvaluatorTpetra1DFEM<Scalar,LO,GO,Node> > model = |
| 620 |
evaluatorTpetra1DFEM<Scalar,LO,GO,Node>(comm, numGlobalElements, x00, x01); |
| 621 |
|
| 622 |
// Set the source term high to make the problem more difficult |
| 623 |
::Thyra::put_scalar<double>(10.0,Teuchos::rcp_const_cast<::Thyra::VectorBase<double>>(model->getNominalValues().get_p(2)).ptr()); |
| 624 |
|
| 625 |
Stratimikos::DefaultLinearSolverBuilder builder; |
| 626 |
|
| 627 |
Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::parameterList(); |
| 628 |
p->set("Linear Solver Type", "Belos"); |
| 629 |
Teuchos::ParameterList& belosList = p->sublist("Linear Solver Types").sublist("Belos"); |
| 630 |
belosList.set("Solver Type", "Pseudo Block GMRES"); |
| 631 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Maximum Iterations", 200); |
| 632 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set<int>("Num Blocks", 200); |
| 633 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Verbosity", 0x7f); |
| 634 |
belosList.sublist("Solver Types").sublist("Pseudo Block GMRES").set("Output Frequency", 100); |
| 635 |
belosList.sublist("VerboseObject").set("Verbosity Level", "none"); |
| 636 |
p->set("Preconditioner Type", "Ifpack2"); |
| 637 |
Teuchos::ParameterList& ifpackList = p->sublist("Preconditioner Types").sublist("Ifpack2"); |
| 638 |
ifpackList.set("Prec Type", "ILUT"); |
| 639 |
ifpackList.set("Overlap", 3); |
| 640 |
ifpackList.sublist("Ifpack2 Settings").set("fact: ilut level-of-fill",2.0); |
| 641 |
|
| 642 |
builder.setParameterList(p); |
| 643 |
|
| 644 |
Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > |
| 645 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 646 |
|
| 647 |
model->set_W_factory(lowsFactory); |
| 648 |
|
| 649 |
// Create the initial guess |
| 650 |
Teuchos::RCP<Thyra::VectorBase<Scalar> > |
| 651 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 652 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<Scalar>::one()); |
| 653 |
|
| 654 |
const bool updatePreconditioner = false; // User controlled updating of preconditioner |
| 655 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 656 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null, Teuchos::null, Teuchos::null, false, updatePreconditioner)); |
| 657 |
|
| 658 |
nox_group->computeF(); |
| 659 |
|
| 660 |
// Create the NOX status tests and the solver |
| 661 |
// Create the convergence tests |
| 662 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 663 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 664 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 665 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 666 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 667 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 668 |
converged->addStatusTest(absresid); |
| 669 |
converged->addStatusTest(wrms); |
| 670 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 671 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 672 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 673 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 674 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 675 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 676 |
combo->addStatusTest(fv); |
| 677 |
combo->addStatusTest(converged); |
| 678 |
combo->addStatusTest(maxiters); |
| 679 |
|
| 680 |
// Create nox parameter list |
| 681 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 682 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 683 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 684 |
|
| 685 |
// Add in Observsers. This test specifically adds in a |
| 686 |
// preconditioner control observer |
| 687 |
Teuchos::RCP<NOX::ObserverReusePreconditioner> reuse_prec_observer; |
| 688 |
{ |
| 689 |
Teuchos::ParameterList precControlPL; |
| 690 |
precControlPL.set("Update prec at start of nonlinear solve",false); |
| 691 |
precControlPL.set("Update prec after this many nonlinear solves",2); |
| 692 |
precControlPL.set("Reset nonlinear solve count on failed solve",true); |
| 693 |
auto pc_observer = NOX::createReusePreconditionerObserver(precControlPL); |
| 694 |
auto print_observer = Teuchos::rcp(new NOX::ObserverPrint(utils)); |
| 695 |
auto observers = Teuchos::rcp(new NOX::ObserverVector); |
| 696 |
observers->pushBack(pc_observer); |
| 697 |
observers->pushBack(print_observer); |
| 698 |
nl_params->sublist("Solver Options").set<Teuchos::RCP<NOX::Observer>>("Observer",observers); |
| 699 |
|
| 700 |
// Save off observer to for unit testing |
| 701 |
reuse_prec_observer = Teuchos::rcp_dynamic_cast<NOX::ObserverReusePreconditioner>(pc_observer); |
| 702 |
} |
| 703 |
|
| 704 |
// Set output parameters |
| 705 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",false); |
| 706 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",false); |
| 707 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",false); |
| 708 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",false); |
| 709 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",false); |
| 710 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",false); |
| 711 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",false); |
| 712 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",false); |
| 713 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",false); |
| 714 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",false); |
| 715 |
|
| 716 |
// Create the solver |
| 717 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 718 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 719 |
utils->out() << "Solve #1:\n"; |
| 720 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 721 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 722 |
TEST_EQUALITY(reuse_prec_observer->getNumPreconditionerUpdates(), size_t(1)); |
| 723 |
TEST_EQUALITY(reuse_prec_observer->getNumNonlinearSolvesCount(), size_t(1)); |
| 724 |
|
| 725 |
utils->out() << "\nSolve #2:\n"; |
| 726 |
solvStatus = solver->solve(); |
| 727 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 728 |
TEST_EQUALITY(reuse_prec_observer->getNumPreconditionerUpdates(), size_t(1)); |
| 729 |
TEST_EQUALITY(reuse_prec_observer->getNumNonlinearSolvesCount(), size_t(2)); |
| 730 |
|
| 731 |
utils->out() << "\nSolve #3:\n"; |
| 732 |
solvStatus = solver->solve(); |
| 733 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 734 |
TEST_EQUALITY(reuse_prec_observer->getNumPreconditionerUpdates(), size_t(2)); |
| 735 |
TEST_EQUALITY(reuse_prec_observer->getNumNonlinearSolvesCount(), size_t(3)); |
| 736 |
|
| 737 |
utils->out() << "\nSolve #4:\n"; |
| 738 |
solvStatus = solver->solve(); |
| 739 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 740 |
TEST_EQUALITY(reuse_prec_observer->getNumPreconditionerUpdates(), size_t(2)); |
| 741 |
TEST_EQUALITY(reuse_prec_observer->getNumNonlinearSolvesCount(), size_t(4)); |
| 742 |
|
| 743 |
utils->out() << "\n"; |
| 744 |
Teuchos::TimeMonitor::summarize(); |
| 745 |
} |