| 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 |
|
| 10 |
// Unit test objects |
| 11 |
#include <Teuchos_ConfigDefs.hpp> |
| 12 |
#include <Teuchos_UnitTestHarness.hpp> |
| 13 |
|
| 14 |
// NOX Objects |
| 15 |
#include "NOX.H" |
| 16 |
#include "NOX_Thyra.H" |
| 17 |
#include "NOX_SolverStats.hpp" |
| 18 |
|
| 19 |
// Trilinos Objects |
| 20 |
#ifdef HAVE_MPI |
| 21 |
#include "Epetra_MpiComm.h" |
| 22 |
#else |
| 23 |
#include "Epetra_SerialComm.h" |
| 24 |
#endif |
| 25 |
#include "Epetra_Map.h" |
| 26 |
#include "Epetra_Vector.h" |
| 27 |
#include "Epetra_RowMatrix.h" |
| 28 |
#include "Epetra_CrsMatrix.h" |
| 29 |
#include "Epetra_Map.h" |
| 30 |
#include "Epetra_LinearProblem.h" |
| 31 |
#include "AztecOO.h" |
| 32 |
|
| 33 |
#include "Teuchos_ParameterList.hpp" |
| 34 |
#include "Teuchos_RCP.hpp" |
| 35 |
#include "Teuchos_GlobalMPISession.hpp" |
| 36 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 37 |
#include "Teuchos_TimeMonitor.hpp" |
| 38 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 39 |
|
| 40 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 41 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 42 |
#include "ModelEvaluator1DPoisson.hpp" |
| 43 |
|
| 44 |
TEUCHOS_UNIT_TEST(SingleStepSolver, WithResetModel) |
| 45 |
{ |
| 46 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 47 |
|
| 48 |
// Create a communicator for Epetra objects |
| 49 |
#ifdef HAVE_MPI |
| 50 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 51 |
#else |
| 52 |
Epetra_SerialComm Comm; |
| 53 |
#endif |
| 54 |
|
| 55 |
// Create the model evaluator object |
| 56 |
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 |
double a = 1.0; |
| 62 |
double b = -1.0; |
| 63 |
Teuchos::RCP<ModelEvaluator1DPoisson<double> > thyraModel = |
| 64 |
Teuchos::rcp(new ModelEvaluator1DPoisson<double>(Teuchos::rcp(&Comm,false), |
| 65 |
num_elements,a,b)); |
| 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->set("Preconditioner Type", "Ifpack"); |
| 73 |
builder.setParameterList(p); |
| 74 |
|
| 75 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 76 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 77 |
|
| 78 |
thyraModel->set_W_factory(lowsFactory); |
| 79 |
|
| 80 |
// Create nox parameter list |
| 81 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 82 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 83 |
nl_params->set("Nonlinear Solver", "Single Step"); |
| 84 |
nl_params->sublist("Single Step Solver").set("Print Norms", true); |
| 85 |
|
| 86 |
// Create a Thyra nonlinear solver |
| 87 |
Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver = |
| 88 |
Teuchos::rcp(new ::Thyra::NOXNonlinearSolver); |
| 89 |
|
| 90 |
solver->setParameterList(nl_params); |
| 91 |
solver->setModel(thyraModel); |
| 92 |
|
| 93 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 94 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 95 |
|
| 96 |
::Thyra::SolveCriteria<double> solve_criteria; |
| 97 |
::Thyra::SolveStatus<double> solve_status; |
| 98 |
|
| 99 |
solve_status = solver->solve(initial_guess.get(), &solve_criteria); |
| 100 |
|
| 101 |
TEST_ASSERT(solve_status.extraParameters->isType<int>("Number of Iterations")); |
| 102 |
TEST_EQUALITY(solve_status.extraParameters->get<int>("Number of Iterations"), 1); |
| 103 |
TEST_EQUALITY(solve_status.solveStatus, ::Thyra::SOLVE_STATUS_CONVERGED); |
| 104 |
|
| 105 |
nl_params->print(std::cout); |
| 106 |
|
| 107 |
Teuchos::TimeMonitor::summarize(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
TEUCHOS_UNIT_TEST(SingleStepSolver, ignoreLinearSolverFailures) |
| 112 |
{ |
| 113 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 114 |
|
| 115 |
// Create a communicator for Epetra objects |
| 116 |
#ifdef HAVE_MPI |
| 117 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 118 |
#else |
| 119 |
Epetra_SerialComm Comm; |
| 120 |
#endif |
| 121 |
|
| 122 |
// Create the model evaluator object |
| 123 |
int num_elements = 100; |
| 124 |
|
| 125 |
// Make sure there is at least one element on each core |
| 126 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 127 |
|
| 128 |
double a = 1.0; |
| 129 |
double b = -1.0; |
| 130 |
Teuchos::RCP<ModelEvaluator1DPoisson<double> > thyraModel = |
| 131 |
Teuchos::rcp(new ModelEvaluator1DPoisson<double>(Teuchos::rcp(&Comm,false), |
| 132 |
num_elements,a,b)); |
| 133 |
|
| 134 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 135 |
|
| 136 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 137 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 138 |
p->set("Linear Solver Type", "Belos"); |
| 139 |
p->set("Preconditioner Type", "None"); |
| 140 |
// Pick params to make sure linear solver fails (we are testing to ignore this failure at the nonlinear solver level) |
| 141 |
p->sublist("Linear Solver Types").sublist("Belos").set("Solver Type","Block GMRES"); |
| 142 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Convergence Tolerance",1.0e-8); |
| 143 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Maximum Iterations",2); |
| 144 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Verbosity",33); |
| 145 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Output Frequency",1); |
| 146 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("VerboseObject").set("Verbosity Level","medium"); |
| 147 |
builder.setParameterList(p); |
| 148 |
|
| 149 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 150 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 151 |
|
| 152 |
thyraModel->set_W_factory(lowsFactory); |
| 153 |
|
| 154 |
// Create nox parameter list |
| 155 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 156 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 157 |
nl_params->set("Nonlinear Solver", "Single Step"); |
| 158 |
nl_params->sublist("Single Step Solver").set("Ignore Linear Solver Failures", true); |
| 159 |
|
| 160 |
// Create a Thyra nonlinear solver |
| 161 |
Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver = |
| 162 |
Teuchos::rcp(new ::Thyra::NOXNonlinearSolver); |
| 163 |
|
| 164 |
solver->setParameterList(nl_params); |
| 165 |
solver->setModel(thyraModel); |
| 166 |
|
| 167 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 168 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 169 |
|
| 170 |
::Thyra::SolveCriteria<double> solve_criteria; |
| 171 |
::Thyra::SolveStatus<double> solve_status; |
| 172 |
|
| 173 |
solve_status = solver->solve(initial_guess.get(), &solve_criteria); |
| 174 |
|
| 175 |
TEST_ASSERT(solve_status.extraParameters->isType<int>("Number of Iterations")); |
| 176 |
TEST_EQUALITY(solve_status.extraParameters->get<int>("Number of Iterations"), 1); |
| 177 |
TEST_EQUALITY(solve_status.solveStatus, ::Thyra::SOLVE_STATUS_CONVERGED); |
| 178 |
|
| 179 |
Teuchos::TimeMonitor::summarize(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
TEUCHOS_UNIT_TEST(SingleStepSolver, reuseJacobian) |
| 184 |
{ |
| 185 |
Teuchos::TimeMonitor::zeroOutTimers(); |
| 186 |
|
| 187 |
// Create a communicator for Epetra objects |
| 188 |
#ifdef HAVE_MPI |
| 189 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 190 |
#else |
| 191 |
Epetra_SerialComm Comm; |
| 192 |
#endif |
| 193 |
|
| 194 |
// Create the model evaluator object |
| 195 |
int num_elements = 100; |
| 196 |
|
| 197 |
// Make sure there is at least one element on each core |
| 198 |
TEST_ASSERT(Comm.NumProc() < num_elements+1); |
| 199 |
|
| 200 |
double a = 1.0; |
| 201 |
double b = -1.0; |
| 202 |
Teuchos::RCP<ModelEvaluator1DPoisson<double> > thyraModel = |
| 203 |
Teuchos::rcp(new ModelEvaluator1DPoisson<double>(Teuchos::rcp(&Comm,false), |
| 204 |
num_elements,a,b)); |
| 205 |
|
| 206 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 207 |
|
| 208 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 209 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 210 |
p->set("Linear Solver Type", "Belos"); |
| 211 |
p->set("Preconditioner Type", "None"); |
| 212 |
p->sublist("Linear Solver Types").sublist("Belos").set("Solver Type","Block GMRES"); |
| 213 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Convergence Tolerance",1.0e-8); |
| 214 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Maximum Iterations",100); |
| 215 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Verbosity",33); |
| 216 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("Solver Types").sublist("Block GMRES").set("Output Frequency",1); |
| 217 |
p->sublist("Linear Solver Types").sublist("Belos").sublist("VerboseObject").set("Verbosity Level","medium"); |
| 218 |
builder.setParameterList(p); |
| 219 |
|
| 220 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 221 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 222 |
|
| 223 |
thyraModel->set_W_factory(lowsFactory); |
| 224 |
|
| 225 |
// Create nox parameter list |
| 226 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 227 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 228 |
nl_params->set("Nonlinear Solver", "Single Step"); |
| 229 |
nl_params->sublist("Single Step Solver").set("Ignore Linear Solver Failures", false); |
| 230 |
nl_params->sublist("Single Step Solver").set("Update Jacobian", false); |
| 231 |
nl_params->sublist("Single Step Solver").set("Print Norms", true); |
| 232 |
nl_params->sublist("Single Step Solver").set("Compute Relative Norm", true); |
| 233 |
nl_params->sublist("Single Step Solver").sublist("Linear Solver").set("Tolerance", 1.0e-7); |
| 234 |
|
| 235 |
// Create a Thyra nonlinear solver |
| 236 |
Teuchos::RCP< ::Thyra::NOXNonlinearSolver> solver = |
| 237 |
Teuchos::rcp(new ::Thyra::NOXNonlinearSolver); |
| 238 |
|
| 239 |
solver->setParameterList(nl_params); |
| 240 |
solver->setModel(thyraModel); |
| 241 |
|
| 242 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 243 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 244 |
|
| 245 |
// This test exists to check that we can take an externally |
| 246 |
// evaluated Jacobian and set it on the solver and reuse it in the |
| 247 |
// SingleStep solver. So let's create the Jacobian, evaluate it and |
| 248 |
// set it on the solver. |
| 249 |
auto inArgs = thyraModel->createInArgs(); |
| 250 |
auto outArgs = thyraModel->createOutArgs(); |
| 251 |
auto J = thyraModel->create_W_op(); |
| 252 |
inArgs.set_x(initial_guess); |
| 253 |
outArgs.set_W_op(J); |
| 254 |
thyraModel->evalModel(inArgs,outArgs); |
| 255 |
const bool jacobianIsEvaluated = true; |
| 256 |
Teuchos::RCP<NOX::Thyra::Group> group = |
| 257 |
Teuchos::rcp(new NOX::Thyra::Group(initial_guess, |
| 258 |
thyraModel, |
| 259 |
J, |
| 260 |
lowsFactory, |
| 261 |
Teuchos::null, |
| 262 |
Teuchos::null, |
| 263 |
Teuchos::null, |
| 264 |
Teuchos::null, |
| 265 |
Teuchos::null, |
| 266 |
false, |
| 267 |
false, |
| 268 |
jacobianIsEvaluated)); |
| 269 |
TEST_ASSERT(group->isJacobian()); |
| 270 |
solver->setGroup(group); // Set specially constructed group with Jacobian already evaluated |
| 271 |
|
| 272 |
::Thyra::SolveCriteria<double> solve_criteria; |
| 273 |
::Thyra::SolveStatus<double> solve_status; |
| 274 |
|
| 275 |
solve_status = solver->solve(initial_guess.get(), &solve_criteria); |
| 276 |
|
| 277 |
TEST_ASSERT(solve_status.extraParameters->isType<int>("Number of Iterations")); |
| 278 |
TEST_EQUALITY(solve_status.extraParameters->get<int>("Number of Iterations"), 1); |
| 279 |
TEST_EQUALITY(solve_status.solveStatus, ::Thyra::SOLVE_STATUS_CONVERGED); |
| 280 |
// Value is 50 for 4 mpi processes. Pad for different process counts |
| 281 |
TEST_ASSERT(solver->getNOXSolver()->getSolverStatistics()->linearSolve.lastLinearSolve_NumIterations < 55); |
| 282 |
TEST_EQUALITY(solver->getNOXSolver()->getSolverStatistics()->linearSolve.lastLinearSolve_Converged, true); |
| 283 |
TEST_ASSERT(solver->getNOXSolver()->getSolverStatistics()->linearSolve.lastLinearSolve_AchievedTolerance < 1.0e-7); |
| 284 |
|
| 285 |
if (Comm.MyPID() == 0) |
| 286 |
std::cout << "Final Parameters\n****************\n" << *nl_params << std::endl; |
| 287 |
|
| 288 |
Teuchos::TimeMonitor::summarize(); |
| 289 |
} |