| 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 |
#include "Teuchos_VerboseObject.hpp" |
| 36 |
#include "Teuchos_TestingHelpers.hpp" |
| 37 |
|
| 38 |
#include "Stratimikos_DefaultLinearSolverBuilder.hpp" |
| 39 |
#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp" |
| 40 |
#include "Thyra_SpmdVectorBase.hpp" |
| 41 |
#include "ModelEvaluatorHeq.hpp" |
| 42 |
|
| 43 |
#include "NOX_Thyra_MatrixFreeJacobianOperator.hpp" |
| 44 |
#include "NOX_MatrixFree_ModelEvaluatorDecorator.hpp" |
| 45 |
|
| 46 |
using namespace std; |
| 47 |
|
| 48 |
int main(int argc, char *argv[]) |
| 49 |
{ |
| 50 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv); |
| 51 |
const Teuchos::RCP<Teuchos::FancyOStream> out = |
| 52 |
Teuchos::VerboseObjectBase::getDefaultOStream(); |
| 53 |
|
| 54 |
// Create a communicator for Epetra objects |
| 55 |
#ifdef HAVE_MPI |
| 56 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 57 |
#else |
| 58 |
Epetra_SerialComm Comm; |
| 59 |
#endif |
| 60 |
|
| 61 |
bool success = false; |
| 62 |
bool verbose = false; |
| 63 |
int StorageDepth = 2; |
| 64 |
double ParamC = 0.999; |
| 65 |
std::string LineSearch = "Full Step"; |
| 66 |
int Reorthogonalize = 0; |
| 67 |
try { |
| 68 |
// Parse the command line |
| 69 |
using Teuchos::CommandLineProcessor; |
| 70 |
CommandLineProcessor clp; |
| 71 |
clp.throwExceptions(false); |
| 72 |
clp.addOutputSetupOptions(true); |
| 73 |
clp.setOption( "v", "disable-verbosity", &verbose, "Enable verbosity" ); |
| 74 |
clp.setOption("storage-depth", &StorageDepth, "Anderson storage depth parameter"); |
| 75 |
clp.setOption("c-parameter", &ParamC, "H equation parameter, c"); |
| 76 |
clp.setOption("line-search", &LineSearch, "Line search type: Full Step, Backtracking, or Polynomial"); |
| 77 |
clp.setOption("ortho-frequency", &Reorthogonalize, "Reorthogonalization frequency"); |
| 78 |
|
| 79 |
CommandLineProcessor::EParseCommandLineReturn |
| 80 |
parse_return = clp.parse(argc,argv,&std::cerr); |
| 81 |
|
| 82 |
if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) |
| 83 |
return parse_return; |
| 84 |
|
| 85 |
if (verbose) |
| 86 |
*out << "Verbosity Activated" << std::endl; |
| 87 |
else |
| 88 |
*out << "Verbosity Disabled" << std::endl; |
| 89 |
|
| 90 |
const int num_elements = 400; |
| 91 |
|
| 92 |
// Check we have at least one unknown per processor |
| 93 |
if (Comm.NumProc() > num_elements) |
| 94 |
throw "Error! Number of elements must be greater than number of processors!"; |
| 95 |
|
| 96 |
// Create the model evaluator object |
| 97 |
Teuchos::RCP<ModelEvaluatorHeq<double> > model = |
| 98 |
modelEvaluatorHeq<double>(Teuchos::rcp(&Comm,false),num_elements,ParamC); |
| 99 |
|
| 100 |
::Stratimikos::DefaultLinearSolverBuilder builder; |
| 101 |
|
| 102 |
Teuchos::RCP<Teuchos::ParameterList> p = |
| 103 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 104 |
p->set("Linear Solver Type", "AztecOO"); |
| 105 |
p->sublist("Linear Solver Types").sublist("AztecOO").sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency",20); |
| 106 |
p->set("Preconditioner Type", "Ifpack"); |
| 107 |
builder.setParameterList(p); |
| 108 |
|
| 109 |
Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > |
| 110 |
lowsFactory = builder.createLinearSolveStrategy(""); |
| 111 |
|
| 112 |
model->set_W_factory(lowsFactory); |
| 113 |
|
| 114 |
// Create the initial guess |
| 115 |
Teuchos::RCP< ::Thyra::VectorBase<double> > |
| 116 |
initial_guess = model->getNominalValues().get_x()->clone_v(); |
| 117 |
|
| 118 |
Thyra::V_S(initial_guess.ptr(),Teuchos::ScalarTraits<double>::one()); |
| 119 |
|
| 120 |
Teuchos::RCP<NOX::Thyra::Group> nox_group = |
| 121 |
Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, model)); |
| 122 |
|
| 123 |
nox_group->computeF(); |
| 124 |
|
| 125 |
// Create the NOX status tests and the solver |
| 126 |
// Create the convergence tests |
| 127 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 128 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 129 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 130 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 131 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 132 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 133 |
converged->addStatusTest(absresid); |
| 134 |
converged->addStatusTest(wrms); |
| 135 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 136 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(100)); |
| 137 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 138 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 139 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 140 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 141 |
combo->addStatusTest(fv); |
| 142 |
combo->addStatusTest(converged); |
| 143 |
combo->addStatusTest(maxiters); |
| 144 |
|
| 145 |
// Create nox parameter list |
| 146 |
Teuchos::RCP<Teuchos::ParameterList> nl_params = |
| 147 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 148 |
nl_params->set("Nonlinear Solver", "Anderson Accelerated Fixed-Point"); |
| 149 |
nl_params->sublist("Anderson Parameters").set("Storage Depth", StorageDepth); |
| 150 |
nl_params->sublist("Anderson Parameters").set("Mixing Parameter", 1.0); |
| 151 |
nl_params->sublist("Anderson Parameters").set("Acceleration Start Iteration", 1); |
| 152 |
nl_params->sublist("Anderson Parameters").set("Reorthogonalization Frequency", Reorthogonalize); |
| 153 |
nl_params->sublist("Anderson Parameters").sublist("Preconditioning").set("Precondition", false); |
| 154 |
nl_params->sublist("Direction").sublist("Newton").sublist("Linear Solver").set("Tolerance", 1.0e-4); |
| 155 |
nl_params->sublist("Printing").sublist("Output Information").set("Details",true); |
| 156 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration",true); |
| 157 |
|
| 158 |
if (verbose){ |
| 159 |
nl_params->sublist("Printing").sublist("Output Information").set("Error",true); |
| 160 |
nl_params->sublist("Printing").sublist("Output Information").set("Test Details",true); |
| 161 |
nl_params->sublist("Printing").sublist("Output Information").set("Debug",true); |
| 162 |
nl_params->sublist("Printing").sublist("Output Information").set("Warning",true); |
| 163 |
nl_params->sublist("Printing").sublist("Output Information").set("Parameters",true); |
| 164 |
nl_params->sublist("Printing").sublist("Output Information").set("Linear Solver Details",true); |
| 165 |
nl_params->sublist("Printing").sublist("Output Information").set("Inner Iteration",true); |
| 166 |
nl_params->sublist("Printing").sublist("Output Information").set("Outer Iteration StatusTest",true); |
| 167 |
} |
| 168 |
|
| 169 |
// Line search parameters |
| 170 |
nl_params->sublist("Line Search").set("Method", LineSearch); |
| 171 |
|
| 172 |
// Create the solver |
| 173 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 174 |
NOX::Solver::buildSolver(nox_group, combo, nl_params); |
| 175 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 176 |
Teuchos::RCP<NOX::Abstract::Vector> diff = solver->getSolutionGroup().getX().clone(); |
| 177 |
|
| 178 |
// Test the reset function by resolving the same problem |
| 179 |
Teuchos::RCP<NOX::Abstract::Vector> newGuess = diff->clone(NOX::ShapeCopy); |
| 180 |
newGuess->init(1.0); |
| 181 |
solver->reset(*newGuess); |
| 182 |
solver->solve(); |
| 183 |
diff->update(1.0, solver->getSolutionGroup().getX(), -1.0); |
| 184 |
|
| 185 |
// Create a print class for controlling output below |
| 186 |
nl_params->sublist("Printing").set("MyPID", Comm.MyPID()); |
| 187 |
nl_params->sublist("Printing").set("Output Precision", 3); |
| 188 |
nl_params->sublist("Printing").set("Output Processor", 0); |
| 189 |
NOX::Utils printing(nl_params->sublist("Printing")); |
| 190 |
|
| 191 |
// Output the parameter list |
| 192 |
if (verbose) { |
| 193 |
if (printing.isPrintType(NOX::Utils::Parameters)) { |
| 194 |
printing.out() << std::endl << "Final Parameters" << std::endl |
| 195 |
<< "****************" << std::endl; |
| 196 |
solver->getList().print(printing.out()); |
| 197 |
printing.out() << std::endl; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
*out << "\nCheck for test pass/fail:\n"; |
| 202 |
|
| 203 |
bool loc_success = true; |
| 204 |
|
| 205 |
// 1. Convergence |
| 206 |
TEUCHOS_TEST_EQUALITY_CONST(solvStatus, NOX::StatusTest::Converged, *out, loc_success); |
| 207 |
// 2. Number of iterations |
| 208 |
int numIterations = 0; |
| 209 |
const int *numItersPtr = nullptr; |
| 210 |
if ( nullptr != (numItersPtr = Teuchos::getParameterPtr<int>( |
| 211 |
solver->getList().sublist("Output"), "Nonlinear Iterations")) ) |
| 212 |
{ |
| 213 |
numIterations = *numItersPtr; |
| 214 |
} |
| 215 |
TEUCHOS_TEST_EQUALITY_CONST(numIterations, 11, *out, loc_success); |
| 216 |
// 3. Same reset solution |
| 217 |
TEUCHOS_TEST_COMPARE_CONST(diff->norm(), <=, 1.0e-14, *out, loc_success); |
| 218 |
|
| 219 |
success = loc_success; |
| 220 |
|
| 221 |
if (success) |
| 222 |
*out << "\nTest passed!" << std::endl; |
| 223 |
else |
| 224 |
*out << "\nTest failed!" << std::endl; |
| 225 |
} |
| 226 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 227 |
|
| 228 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 229 |
} |