| 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 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 35 |
|
| 36 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 37 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 38 |
#include "Thyra_SpmdVectorBase.hpp" |
| 39 |
#include "ModelEvaluatorHeq.hpp" |
| 40 |
|
| 41 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 42 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 43 |
|
| 44 |
using namespace std; |
| 45 |
|
| 46 |
int main(int argc, char *argv[]) |
| 47 |
{ |
| 48 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 49 |
|
| 50 |
bool success = false; |
| 51 |
bool verbose = false; |
| 52 |
try { |
| 53 |
// Parse the command line |
| 54 |
using Teuchos::CommandLineProcessor; |
| 55 |
CommandLineProcessor clp; |
| 56 |
clp.throwExceptions(false); |
| 57 |
clp.addOutputSetupOptions(true); |
| 58 |
clp.setOption( "v", "disable-verbosity", &verbose, "Enable verbosity" ); |
| 59 |
|
| 60 |
CommandLineProcessor::EParseCommandLineReturn |
| 61 |
parse_return = clp.parse(argc,argv,&std::cerr); |
| 62 |
|
| 63 |
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) |
| 64 |
return parse_return; |
| 65 |
|
| 66 |
if (verbose) |
| 67 |
std::cout << "Verbosity Activated" << std::endl; |
| 68 |
else |
| 69 |
std::cout << "Verbosity Disabled" << std::endl; |
| 70 |
|
| 71 |
// Create a communicator for Epetra objects |
| 72 |
#ifdef HAVE_MPI |
| 73 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 74 |
#else |
| 75 |
Epetra_SerialComm Comm; |
| 76 |
#endif |
| 77 |
|
| 78 |
const int num_elements = 400; |
| 79 |
|
| 80 |
// Check we have at least one unknown per processor |
| 81 |
if (Comm.NumProc() > num_elements) { |
| 82 |
std::cerr << "Error! Number of elements must be greate than number of processors!" |
| 83 |
<< std::endl; |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
|
| 87 |
// Create the model evaluator object |
| 88 |
double paramC = 0.99; |
| 89 |
Teuchos::RCP<ModelEvaluatorHeq<double> > model = |
| 90 |
modelEvaluatorHeq<double>(Teuchos::rcp(&Comm,false),num_elements,paramC); |
| 91 |
|
| 92 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 93 |
|
| 94 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 95 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 96 |
p->set("Linear Solver Type", "AztecOO"); |
| 97 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 98 |
p->set("Preconditioner Type", "Ifpack"); |
| 99 |
builder.setParameterList(p); |
| 100 |
|
| 101 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 102 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 103 |
|
| 104 |
model->set_W_factory(lowsFactory); |
| 105 |
|
| 106 |
// Create the initial guess |
| 107 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 108 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 109 |
|
| 110 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 111 |
|
| 112 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 113 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model)); |
| 114 |
//Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model, model->create_W_op(), lowsFactory, Teuchos::null, Teuchos::null)); |
| 115 |
|
| 116 |
nox_group->computeF(); |
| 117 |
|
| 118 |
// Create the NOX status tests and the solver |
| 119 |
// Create the convergence tests |
| 120 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 121 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 122 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 123 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 124 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 125 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 126 |
converged->addStatusTest(absresid); |
| 127 |
converged->addStatusTest(wrms); |
| 128 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 129 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 130 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 131 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 132 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 133 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 134 |
combo->addStatusTest(fv); |
| 135 |
combo->addStatusTest(converged); |
| 136 |
combo->addStatusTest(maxiters); |
| 137 |
|
| 138 |
// Create nox parameter list |
| 139 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 140 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 141 |
nl_params->set("Nonlinear Solver", "Anderson Accelerated Fixed-Point"); |
| 142 |
nl_params->sublist("Anderson Parameters").set("Storage Depth", 5); |
| 143 |
nl_params->sublist("Anderson Parameters").set("Mixing Parameter", 1.0); |
| 144 |
nl_params->sublist("Anderson Parameters").set("Acceleration Start Iteration", 5); |
| 145 |
nl_params->sublist("Anderson Parameters").sublist("Preconditioning").set("Precondition", false); |
| 146 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 147 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 148 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 149 |
//nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 150 |
|
| 151 |
// Create the solver |
| 152 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 153 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 154 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 155 |
|
| 156 |
// 1. Convergence |
| 157 |
int status = 0; |
| 158 |
if (solvStatus != NOX::StatusTest::Converged) |
| 159 |
status = 1; |
| 160 |
// 2. Number of iterations |
| 161 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 14) |
| 162 |
status = 2; |
| 163 |
|
| 164 |
success = status==0; |
| 165 |
|
| 166 |
if (Comm.MyPID() == 0) { |
| 167 |
if (success) |
| 168 |
std::cout << "Test passed!" << std::endl; |
| 169 |
else |
| 170 |
std::cout << "Test failed!" << std::endl; |
| 171 |
} |
| 172 |
} |
| 173 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 174 |
|
| 175 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 176 |
} |
| 177 |
|