| 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 |
// NOX Objects |
| 11 |
#include "NOX.H" |
| 12 |
#include "NOX_Thyra.H" |
| 13 |
#include "NOX_SolverStats.hpp" |
| 14 |
#include "NOX_Observer_Print.hpp" |
| 15 |
#include "NOX_Utils.H" |
| 16 |
#include "Teuchos_TestingHelpers.hpp" |
| 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 |
|
| 31 |
#include "Teuchos_ParameterList.hpp" |
| 32 |
#include "Teuchos_RCP.hpp" |
| 33 |
#include "Teuchos_GlobalMPISession.hpp" |
| 34 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 35 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 36 |
|
| 37 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 38 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 39 |
#include "ModelEvaluator2DSim.hpp" |
| 40 |
|
| 41 |
using namespace std; |
| 42 |
|
| 43 |
int main(int argc, char *argv[]) |
| 44 |
{ |
| 45 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 46 |
|
| 47 |
bool success = false; |
| 48 |
bool verbose = false; |
| 49 |
try { |
| 50 |
// Parse the command line |
| 51 |
using Teuchos::CommandLineProcessor; |
| 52 |
CommandLineProcessor clp; |
| 53 |
clp.throwExceptions(false); |
| 54 |
clp.addOutputSetupOptions(true); |
| 55 |
clp.setOption( "v", "disable-verbosity", &verbose, "Enable verbosity" ); |
| 56 |
|
| 57 |
CommandLineProcessor::EParseCommandLineReturn |
| 58 |
parse_return = clp.parse(argc,argv,&std::cerr); |
| 59 |
|
| 60 |
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) |
| 61 |
return parse_return; |
| 62 |
|
| 63 |
if (verbose) |
| 64 |
std::cout << "Verbosity Activated" << std::endl; |
| 65 |
else |
| 66 |
std::cout << "Verbosity Disabled" << std::endl; |
| 67 |
|
| 68 |
// Create a communicator for Epetra objects |
| 69 |
#ifdef HAVE_MPI |
| 70 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 71 |
#else |
| 72 |
Epetra_SerialComm Comm; |
| 73 |
#endif |
| 74 |
|
| 75 |
int status = 0; |
| 76 |
|
| 77 |
// Check we have only one processor since this problem doesn't work |
| 78 |
// for more than one proc |
| 79 |
if (Comm.NumProc() > 1) |
| 80 |
throw "Error! Problem can only be run with at most 1 processor!"; |
| 81 |
|
| 82 |
// Create the model evaluator object |
| 83 |
double d = 10.0; |
| 84 |
double p0 = 2.0; |
| 85 |
double p1 = 0.0; |
| 86 |
double x00 = 0.0; |
| 87 |
double x01 = 1.0; |
| 88 |
Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel = |
| 89 |
Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false), |
| 90 |
d,p0,p1,x00,x01)); |
| 91 |
|
| 92 |
// Create the linear solver type with Stratimikos |
| 93 |
//Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > |
| 94 |
//lowsFactory = rcp(new Thyra::AmesosLinearOpWithSolveFactory()); |
| 95 |
|
| 96 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 97 |
|
| 98 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 99 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 100 |
p->set("Linear Solver Type", "AztecOO"); |
| 101 |
p->set("Preconditioner Type", "Ifpack"); |
| 102 |
//p->set("Enable Delayed Solver Construction", true); |
| 103 |
builder.setParameterList(p); |
| 104 |
|
| 105 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 106 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 107 |
|
| 108 |
thyraModel->set_W_factory(lowsFactory); |
| 109 |
|
| 110 |
// Create the initial guess |
| 111 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 112 |
initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); |
| 113 |
|
| 114 |
// Create the NOX::Thyra::Group |
| 115 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 116 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel)); |
| 117 |
|
| 118 |
// nox_group->computeF(); |
| 119 |
// std::cout << "ComputedF!" << std::endl; |
| 120 |
// const NOX::Thyra::Vector& t_vec = |
| 121 |
// dynamic_cast<const NOX::Thyra::Vector&>(nox_group->getF()); |
| 122 |
|
| 123 |
// t_vec.print(std::cout); |
| 124 |
// exit(0); |
| 125 |
|
| 126 |
// Create the NOX status tests and the solver |
| 127 |
// Create the convergence tests |
| 128 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 129 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 130 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 131 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 132 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 133 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 134 |
converged->addStatusTest(absresid); |
| 135 |
converged->addStatusTest(wrms); |
| 136 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 137 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 138 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 139 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 140 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 141 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 142 |
combo->addStatusTest(fv); |
| 143 |
combo->addStatusTest(converged); |
| 144 |
combo->addStatusTest(maxiters); |
| 145 |
|
| 146 |
// Create nox parameter list |
| 147 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 148 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 149 |
nl_params->set("Nonlinear Solver", "Line Search Based"); |
| 150 |
|
| 151 |
// Register Print Observer (and disable normal printing) |
| 152 |
nl_params->sublist("Printing").set("Output Information",NOX::Utils::Error); |
| 153 |
Teuchos::RCP<NOX::Utils> os = Teuchos::rcp(new NOX::Utils(nl_params->sublist("Printing"))); |
| 154 |
Teuchos::RCP<NOX::Observer> printObserver = Teuchos::rcp(new NOX::ObserverPrint(os)); |
| 155 |
nl_params->sublist("Solver Options").set("Observer",printObserver); |
| 156 |
|
| 157 |
// Create the solver |
| 158 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 159 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 160 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 161 |
|
| 162 |
success = status==0; |
| 163 |
|
| 164 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearSolves,1,std::cout,success); |
| 165 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numNonlinearIterations,7,std::cout,success); |
| 166 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->numTotalNonlinearIterations,7,std::cout,success); |
| 167 |
|
| 168 |
TEUCHOS_TEST_ASSERT(solver->getSolverStatistics()->linearSolve.lastLinearSolve_Converged,std::cout,success); |
| 169 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.lastLinearSolve_NumIterations,1,std::cout,success); |
| 170 |
TEUCHOS_TEST_ASSERT(solver->getSolverStatistics()->linearSolve.lastLinearSolve_AchievedTolerance < 1.0e-4,std::cout,success); |
| 171 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.lastNonlinearSolve_NumLinearSolves,7,std::cout,success); |
| 172 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.lastNonlinearSolve_NumLinearIterations,7,std::cout,success); |
| 173 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.allNonlinearSolves_NumLinearSolves,7,std::cout,success); |
| 174 |
TEUCHOS_TEST_EQUALITY(solver->getSolverStatistics()->linearSolve.allNonlinearSolves_NumLinearIterations,7,std::cout,success); |
| 175 |
|
| 176 |
if (solvStatus == NOX::StatusTest::Converged) |
| 177 |
std::cout << "Test passed!" << std::endl; |
| 178 |
} |
| 179 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 180 |
|
| 181 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 182 |
} |