| 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 |
#ifdef HAVE_MPI |
| 18 |
#include "Epetra_MpiComm.h" |
| 19 |
#else |
| 20 |
#include "Epetra_SerialComm.h" |
| 21 |
#endif |
| 22 |
#include "Epetra_Map.h" |
| 23 |
#include "Epetra_Vector.h" |
| 24 |
#include "Epetra_RowMatrix.h" |
| 25 |
#include "Epetra_CrsMatrix.h" |
| 26 |
#include "Epetra_Map.h" |
| 27 |
#include "Epetra_LinearProblem.h" |
| 28 |
|
| 29 |
#include "Teuchos_ParameterList.hpp" |
| 30 |
#include "Teuchos_RCP.hpp" |
| 31 |
#include "Teuchos_GlobalMPISession.hpp" |
| 32 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 33 |
#include "Teuchos_FancyOStream.hpp" |
| 34 |
|
| 35 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 36 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 37 |
#include "Thyra_SpmdVectorBase.hpp" |
| 38 |
#include "ModelEvaluator1DFEM.hpp" |
| 39 |
|
| 40 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 41 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 42 |
|
| 43 |
using namespace std; |
| 44 |
|
| 45 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, AnalyticJacobian_NoPrec) |
| 46 |
{ |
| 47 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 48 |
|
| 49 |
// Create a communicator for Epetra objects |
| 50 |
#ifdef HAVE_MPI |
| 51 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 52 |
#else |
| 53 |
Epetra_SerialComm Comm; |
| 54 |
#endif |
| 55 |
|
| 56 |
const int num_elements = 100; |
| 57 |
|
| 58 |
// Make sure there is at least one element on each core |
| 59 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 60 |
|
| 61 |
// Create the model evaluator object |
| 62 |
double x00 = 0.0; |
| 63 |
double x01 = 1.0; |
| 64 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 65 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 66 |
|
| 67 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 68 |
|
| 69 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 70 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 71 |
p->set("Linear Solver Type", "AztecOO"); |
| 72 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 73 |
p->set("Preconditioner Type", "None"); |
| 74 |
builder.setParameterList(p); |
| 75 |
|
| 76 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 77 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 78 |
|
| 79 |
model->set_W_factory(lowsFactory); |
| 80 |
|
| 81 |
// Create the initial guess |
| 82 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 83 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 84 |
|
| 85 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 86 |
|
| 87 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 88 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 89 |
|
| 90 |
nox_group->computeF(); |
| 91 |
|
| 92 |
// Create the NOX status tests and the solver |
| 93 |
// Create the convergence tests |
| 94 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 95 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 96 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 97 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 98 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 99 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 100 |
converged->addStatusTest(absresid); |
| 101 |
converged->addStatusTest(wrms); |
| 102 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 103 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 104 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 105 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 106 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 107 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 108 |
combo->addStatusTest(fv); |
| 109 |
combo->addStatusTest(converged); |
| 110 |
combo->addStatusTest(maxiters); |
| 111 |
|
| 112 |
// Create nox parameter list |
| 113 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 114 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 115 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 116 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 117 |
|
| 118 |
// Create the solver |
| 119 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 120 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 121 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 122 |
|
| 123 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 124 |
|
| 125 |
Teuchos::TimeMonitor::summarize(); |
| 126 |
} |
| 127 |
|
| 128 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, AnalyticJacobian_IfpackPrec) |
| 129 |
{ |
| 130 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 131 |
|
| 132 |
// Create a communicator for Epetra objects |
| 133 |
#ifdef HAVE_MPI |
| 134 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 135 |
#else |
| 136 |
Epetra_SerialComm Comm; |
| 137 |
#endif |
| 138 |
|
| 139 |
const int num_elements = 100; |
| 140 |
|
| 141 |
// Make sure there is at least one element on each core |
| 142 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 143 |
|
| 144 |
// Create the model evaluator object |
| 145 |
double x00 = 0.0; |
| 146 |
double x01 = 1.0; |
| 147 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 148 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 149 |
|
| 150 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 151 |
|
| 152 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 153 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 154 |
p->set("Linear Solver Type", "AztecOO"); |
| 155 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 156 |
p->set("Preconditioner Type", "Ifpack"); |
| 157 |
builder.setParameterList(p); |
| 158 |
|
| 159 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 160 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 161 |
|
| 162 |
model->set_W_factory(lowsFactory); |
| 163 |
|
| 164 |
// Create the initial guess |
| 165 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 166 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 167 |
|
| 168 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 169 |
|
| 170 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 171 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 172 |
|
| 173 |
nox_group->computeF(); |
| 174 |
|
| 175 |
// Create the NOX status tests and the solver |
| 176 |
// Create the convergence tests |
| 177 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 178 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 179 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 180 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 181 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 182 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 183 |
converged->addStatusTest(absresid); |
| 184 |
converged->addStatusTest(wrms); |
| 185 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 186 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 187 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 188 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 189 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 190 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 191 |
combo->addStatusTest(fv); |
| 192 |
combo->addStatusTest(converged); |
| 193 |
combo->addStatusTest(maxiters); |
| 194 |
|
| 195 |
// Create nox parameter list |
| 196 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 197 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 198 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 199 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 200 |
|
| 201 |
// Create the solver |
| 202 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 203 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 204 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 205 |
|
| 206 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 207 |
|
| 208 |
Teuchos::TimeMonitor::summarize(); |
| 209 |
} |
| 210 |
|
| 211 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, JFNK_NoPrec) |
| 212 |
{ |
| 213 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 214 |
|
| 215 |
// Create a communicator for Epetra objects |
| 216 |
#ifdef HAVE_MPI |
| 217 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 218 |
#else |
| 219 |
Epetra_SerialComm Comm; |
| 220 |
#endif |
| 221 |
|
| 222 |
const int num_elements = 100; |
| 223 |
|
| 224 |
// Make sure there is at least one element on each core |
| 225 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 226 |
|
| 227 |
// Create the model evaluator object |
| 228 |
double x00 = 0.0; |
| 229 |
double x01 = 1.0; |
| 230 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 231 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 232 |
|
| 233 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 234 |
|
| 235 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 236 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 237 |
p->set("Linear Solver Type", "AztecOO"); |
| 238 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 239 |
p->set("Preconditioner Type", "None"); |
| 240 |
builder.setParameterList(p); |
| 241 |
|
| 242 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 243 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 244 |
|
| 245 |
model->set_W_factory(lowsFactory); |
| 246 |
|
| 247 |
// Create the initial guess |
| 248 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 249 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 250 |
|
| 251 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 252 |
|
| 253 |
// Create the JFNK operator |
| 254 |
Teuchos::ParameterList printParams; |
| 255 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 256 |
jfnkParams->set("Difference Type","Forward"); |
| 257 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 258 |
jfnkParams->set("lambda",1.0e-4); |
| 259 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 260 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 261 |
jfnkOp->setParameterList(jfnkParams); |
| 262 |
jfnkParams->print(out); |
| 263 |
|
| 264 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 265 |
Teuchos::RCP< ::Thyra::ModelEvaluator<double> > thyraModel = |
| 266 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<double>(model)); |
| 267 |
|
| 268 |
// Create the NOX::Thyra::Group |
| 269 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 270 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, jfnkOp, lowsFactory, Teuchos::null, Teuchos::null, Teuchos::null)); |
| 271 |
|
| 272 |
nox_group->computeF(); |
| 273 |
|
| 274 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 275 |
// This creates a circular dependency, so use a weak pointer. |
| 276 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 277 |
|
| 278 |
// Create a preconditioner |
| 279 |
|
| 280 |
// Create the NOX status tests and the solver |
| 281 |
// Create the convergence tests |
| 282 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 283 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 284 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 285 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 286 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 287 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 288 |
converged->addStatusTest(absresid); |
| 289 |
converged->addStatusTest(wrms); |
| 290 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 291 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 292 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 293 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 294 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 295 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 296 |
combo->addStatusTest(fv); |
| 297 |
combo->addStatusTest(converged); |
| 298 |
combo->addStatusTest(maxiters); |
| 299 |
|
| 300 |
// Create nox parameter list |
| 301 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 302 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 303 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 304 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 305 |
|
| 306 |
// Create the solver |
| 307 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 308 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 309 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 310 |
|
| 311 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 312 |
|
| 313 |
Teuchos::TimeMonitor::summarize(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, JFNK_UserPrec) |
| 318 |
{ |
| 319 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 320 |
|
| 321 |
// Create a communicator for Epetra objects |
| 322 |
#ifdef HAVE_MPI |
| 323 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 324 |
#else |
| 325 |
Epetra_SerialComm Comm; |
| 326 |
#endif |
| 327 |
|
| 328 |
const int num_elements = 100; |
| 329 |
|
| 330 |
// Make sure there is at least one element on each core |
| 331 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 332 |
|
| 333 |
// Create the model evaluator object |
| 334 |
double x00 = 0.0; |
| 335 |
double x01 = 1.0; |
| 336 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 337 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 338 |
|
| 339 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 340 |
|
| 341 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 342 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 343 |
p->set("Linear Solver Type", "AztecOO"); |
| 344 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 345 |
p->set("Preconditioner Type", "Ifpack"); |
| 346 |
builder.setParameterList(p); |
| 347 |
|
| 348 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 349 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 350 |
|
| 351 |
model->set_W_factory(lowsFactory); |
| 352 |
|
| 353 |
// Create the initial guess |
| 354 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 355 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 356 |
|
| 357 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 358 |
|
| 359 |
// Create the JFNK operator |
| 360 |
Teuchos::ParameterList printParams; |
| 361 |
Teuchos::RCP<Teuchos::ParameterList> jfnkParams = Teuchos::parameterList(); |
| 362 |
jfnkParams->set("Difference Type","Forward"); |
| 363 |
jfnkParams->set("Perturbation Algorithm","KSP NOX 2001"); |
| 364 |
jfnkParams->set("lambda",1.0e-4); |
| 365 |
Teuchos::RCP<NOX::Thyra::MatrixFreeJacobianOperator<double> > jfnkOp = |
| 366 |
Teuchos::rcp(new NOX::Thyra::MatrixFreeJacobianOperator<double>(printParams)); |
| 367 |
jfnkOp->setParameterList(jfnkParams); |
| 368 |
jfnkParams->print(out); |
| 369 |
|
| 370 |
// Wrap the model evaluator in a JFNK Model Evaluator |
| 371 |
Teuchos::RCP< ::Thyra::ModelEvaluator<double> > thyraModel = |
| 372 |
Teuchos::rcp(new NOX::MatrixFreeModelEvaluatorDecorator<double>(model)); |
| 373 |
|
| 374 |
// Create the Preconditioner operator |
| 375 |
Teuchos::RCP< ::Thyra::PreconditionerBase<double> > precOp = |
| 376 |
thyraModel->create_W_prec(); |
| 377 |
|
| 378 |
// Create the NOX::Thyra::Group |
| 379 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 380 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, jfnkOp, lowsFactory, precOp, Teuchos::null, Teuchos::null)); |
| 381 |
|
| 382 |
nox_group->computeF(); |
| 383 |
|
| 384 |
// VERY IMPORTANT!!! jfnk object needs base evaluation objects. |
| 385 |
// This creates a circular dependency, so use a weak pointer. |
| 386 |
jfnkOp->setBaseEvaluationToNOXGroup(nox_group.create_weak()); |
| 387 |
|
| 388 |
// Create the NOX status tests and the solver |
| 389 |
// Create the convergence tests |
| 390 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 391 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 392 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 393 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 394 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 395 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 396 |
converged->addStatusTest(absresid); |
| 397 |
converged->addStatusTest(wrms); |
| 398 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 399 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 400 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 401 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 402 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 403 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 404 |
combo->addStatusTest(fv); |
| 405 |
combo->addStatusTest(converged); |
| 406 |
combo->addStatusTest(maxiters); |
| 407 |
|
| 408 |
// Create nox parameter list |
| 409 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 410 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 411 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 412 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 413 |
|
| 414 |
// Create the solver |
| 415 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 416 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 417 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 418 |
|
| 419 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 420 |
|
| 421 |
Teuchos::TimeMonitor::summarize(); |
| 422 |
} |
| 423 |
|
| 424 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, AndersonAcceleration_UserPrec) |
| 425 |
{ |
| 426 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 427 |
|
| 428 |
// Create a communicator for Epetra objects |
| 429 |
#ifdef HAVE_MPI |
| 430 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 431 |
#else |
| 432 |
Epetra_SerialComm Comm; |
| 433 |
#endif |
| 434 |
|
| 435 |
const int num_elements = 100; |
| 436 |
|
| 437 |
// Make sure there is at least one element on each core |
| 438 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 439 |
|
| 440 |
// Create the model evaluator object |
| 441 |
double x00 = 0.0; |
| 442 |
double x01 = 1.0; |
| 443 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 444 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 445 |
|
| 446 |
// Create the initial guess |
| 447 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 448 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 449 |
|
| 450 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 451 |
|
| 452 |
// Create the Preconditioner operator |
| 453 |
Teuchos::RCP< ::Thyra::PreconditionerBase<double> > precOp = |
| 454 |
model->create_W_prec(); |
| 455 |
|
| 456 |
// Create the NOX::Thyra::Group |
| 457 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 458 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, Teuchos::null, Teuchos::null, precOp, Teuchos::null, Teuchos::null)); |
| 459 |
|
| 460 |
//nox_group->computeF(); |
| 461 |
|
| 462 |
// Create the NOX status tests and the solver |
| 463 |
// Create the convergence tests |
| 464 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 465 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 466 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 467 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 468 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 469 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 470 |
converged->addStatusTest(absresid); |
| 471 |
converged->addStatusTest(wrms); |
| 472 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 473 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(2000)); |
| 474 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 475 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 476 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 477 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 478 |
combo->addStatusTest(fv); |
| 479 |
combo->addStatusTest(converged); |
| 480 |
combo->addStatusTest(maxiters); |
| 481 |
|
| 482 |
// Create nox parameter list |
| 483 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 484 |
nl_params->set("Nonlinear Solver", "Anderson Accelerated Fixed-Point"); |
| 485 |
nl_params->sublist("Anderson Parameters").set("Storage Depth", 100); |
| 486 |
nl_params->sublist("Anderson Parameters").set("Mixing Parameter", -0.9); |
| 487 |
nl_params->sublist("Anderson Parameters").sublist("Preconditioning").set("Precondition", true); |
| 488 |
|
| 489 |
// Create the solver |
| 490 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 491 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 492 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 493 |
|
| 494 |
TEST_EQUALITY(solvStatus,NOX::StatusTest::Converged); |
| 495 |
TEST_EQUALITY(solver->getNumIterations(),103); |
| 496 |
|
| 497 |
Teuchos::TimeMonitor::summarize(); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
TEUCHOS_UNIT_TEST(NOX_Thyra_1DFEM, AndersonAcceleration_IfpackPrec) |
| 502 |
{ |
| 503 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 504 |
|
| 505 |
// Create a communicator for Epetra objects |
| 506 |
#ifdef HAVE_MPI |
| 507 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 508 |
#else |
| 509 |
Epetra_SerialComm Comm; |
| 510 |
#endif |
| 511 |
|
| 512 |
const int num_elements = 100; |
| 513 |
|
| 514 |
// Make sure there is at least one element on each core |
| 515 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 516 |
|
| 517 |
// Create the model evaluator object |
| 518 |
double x00 = 0.0; |
| 519 |
double x01 = 1.0; |
| 520 |
Teuchos::RCP<ModelEvaluator1DFEM<double> > model = |
| 521 |
modelEvaluator1DFEM<double>(Teuchos::rcp(&Comm,false),num_elements,x00,x01); |
| 522 |
|
| 523 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 524 |
|
| 525 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 526 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 527 |
p->set("Linear Solver Type", "AztecOO"); |
| 528 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 529 |
p->set("Preconditioner Type", "Ifpack"); |
| 530 |
builder.setParameterList(p); |
| 531 |
|
| 532 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 533 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 534 |
|
| 535 |
model->set_W_factory(lowsFactory); |
| 536 |
|
| 537 |
// Create the initial guess |
| 538 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 539 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 540 |
|
| 541 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 542 |
|
| 543 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 544 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, Teuchos::null)); |
| 545 |
|
| 546 |
nox_group->computeF(); |
| 547 |
|
| 548 |
// Create the NOX status tests and the solver |
| 549 |
// Create the convergence tests |
| 550 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 551 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 552 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 553 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 554 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 555 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 556 |
converged->addStatusTest(absresid); |
| 557 |
converged->addStatusTest(wrms); |
| 558 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 559 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 560 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 561 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 562 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 563 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 564 |
combo->addStatusTest(fv); |
| 565 |
combo->addStatusTest(converged); |
| 566 |
combo->addStatusTest(maxiters); |
| 567 |
|
| 568 |
// Create nox parameter list |
| 569 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::parameterList(); |
| 570 |
nl_params->set("Nonlinear Solver", "Anderson Accelerated Fixed-Point"); |
| 571 |
nl_params->sublist("Anderson Parameters").set("Storage Depth", 100); |
| 572 |
nl_params->sublist("Anderson Parameters").set("Mixing Parameter", -0.9); |
| 573 |
nl_params->sublist("Anderson Parameters").sublist("Preconditioning").set("Precondition", true); |
| 574 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 575 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 576 |
//nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 577 |
|
| 578 |
// Create the solver |
| 579 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 580 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 581 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 582 |
|
| 583 |
TEST_ASSERT(solvStatus == NOX::StatusTest::Converged); |
| 584 |
|
| 585 |
// Ifpack preconditioning affects convergence! |
| 586 |
if (Comm.NumProc() == 1) { |
| 587 |
TEST_EQUALITY(solver->getNumIterations(),5); |
| 588 |
} |
| 589 |
else if (Comm.NumProc() == 4) { |
| 590 |
TEST_EQUALITY(solver->getNumIterations(),15); |
| 591 |
} |
| 592 |
|
| 593 |
Teuchos::TimeMonitor::summarize(); |
| 594 |
} |