| 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 |
|
| 18 |
// Trilinos Objects |
| 19 |
#ifdef HAVE_MPI |
| 20 |
#include "Epetra_MpiComm.h" |
| 21 |
#else |
| 22 |
#include "Epetra_SerialComm.h" |
| 23 |
#endif |
| 24 |
#include "Epetra_Map.h" |
| 25 |
#include "Epetra_Vector.h" |
| 26 |
#include "Epetra_RowMatrix.h" |
| 27 |
#include "Epetra_CrsMatrix.h" |
| 28 |
#include "Epetra_Map.h" |
| 29 |
#include "Epetra_LinearProblem.h" |
| 30 |
#include "AztecOO.h" |
| 31 |
|
| 32 |
#include "Teuchos_ParameterList.hpp" |
| 33 |
#include "Teuchos_RCP.hpp" |
| 34 |
#include "Teuchos_GlobalMPISession.hpp" |
| 35 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 36 |
#include "Teuchos_TimeMonitor.hpp" |
| 37 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 38 |
|
| 39 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 40 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 41 |
#include "ModelEvaluator2DSim.hpp" |
| 42 |
|
| 43 |
using namespace std; |
| 44 |
|
| 45 |
TEUCHOS_UNIT_TEST(Thyra_NonlinearSolver, WithResetModel) |
| 46 |
{ |
| 47 |
// Create a communicator for Epetra objects |
| 48 |
#ifdef HAVE_MPI |
| 49 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 50 |
#else |
| 51 |
Epetra_SerialComm Comm; |
| 52 |
#endif |
| 53 |
|
| 54 |
// Problem size only supports 1 mpi process |
| 55 |
TEST_EQUALITY(Comm.NumProc(), 1); |
| 56 |
|
| 57 |
// Create the model evaluator object |
| 58 |
double d = 10.0; |
| 59 |
double p0 = 2.0; |
| 60 |
double p1 = 0.0; |
| 61 |
double x00 = 0.0; |
| 62 |
double x01 = 1.0; |
| 63 |
Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel = |
| 64 |
Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false), |
| 65 |
d,p0,p1,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->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", "Line Search Based"); |
| 84 |
|
| 85 |
// Create a Thyra nonlinear solver |
| 86 |
Teuchos::RCP< ::Thyra::NonlinearSolverBase<double> > solver = |
| 87 |
Teuchos::rcp(new ::Thyra::NOXNonlinearSolver); |
| 88 |
|
| 89 |
solver->setParameterList(nl_params); |
| 90 |
solver->setModel(thyraModel); |
| 91 |
|
| 92 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 93 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 94 |
|
| 95 |
::Thyra::SolveCriteria<double> solve_criteria; |
| 96 |
::Thyra::SolveStatus<double> solve_status; |
| 97 |
|
| 98 |
solve_status = solver->solve(initial_guess.get(), &solve_criteria); |
| 99 |
|
| 100 |
TEST_ASSERT(solve_status.extraParameters->isType<int>("Number of Iterations")); |
| 101 |
TEST_EQUALITY(solve_status.extraParameters->get<int>("Number of Iterations"), 7); |
| 102 |
TEST_EQUALITY(solve_status.solveStatus, ::Thyra::SOLVE_STATUS_CONVERGED); |
| 103 |
|
| 104 |
// Test the reset capability for using a new model evalautor with a |
| 105 |
// different set of parameters |
| 106 |
p1 = 2.0; |
| 107 |
thyraModel = |
| 108 |
Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false), |
| 109 |
d,p0,p1,x00,x01)); |
| 110 |
thyraModel->set_W_factory(lowsFactory); |
| 111 |
|
| 112 |
solver->setModel(thyraModel); |
| 113 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 114 |
solve_status = solver->solve(initial_guess.get(), &solve_criteria); |
| 115 |
TEST_ASSERT(solve_status.extraParameters->isType<int>("Number of Iterations")); |
| 116 |
TEST_EQUALITY(solve_status.extraParameters->get<int>("Number of Iterations"), 9); |
| 117 |
TEST_EQUALITY(solve_status.solveStatus, ::Thyra::SOLVE_STATUS_CONVERGED); |
| 118 |
|
| 119 |
Teuchos::TimeMonitor::summarize(); |
| 120 |
} |