| 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 |
// 1D Finite Element Test Problem |
| 11 |
/* Solves the nonlinear equation: |
| 12 |
* |
| 13 |
* d2u |
| 14 |
* --- - k * u**2 = 0 |
| 15 |
* dx2 |
| 16 |
* |
| 17 |
* subject to @ x=0, u=1 |
| 18 |
*/ |
| 19 |
|
| 20 |
// NOX Objects |
| 21 |
#include "NOX.H" |
| 22 |
#include "NOX_Epetra.H" |
| 23 |
|
| 24 |
// Trilinos Objects |
| 25 |
#ifdef HAVE_MPI |
| 26 |
#include "Epetra_MpiComm.h" |
| 27 |
#else |
| 28 |
#include "Epetra_SerialComm.h" |
| 29 |
#endif |
| 30 |
#include "Epetra_Map.h" |
| 31 |
#include "Epetra_Vector.h" |
| 32 |
#include "Epetra_RowMatrix.h" |
| 33 |
#include "Epetra_CrsMatrix.h" |
| 34 |
#include "Epetra_Map.h" |
| 35 |
#include "Epetra_LinearProblem.h" |
| 36 |
#include "AztecOO.h" |
| 37 |
|
| 38 |
// User's application specific files |
| 39 |
#include "1DfemInterface.H" |
| 40 |
|
| 41 |
#include "Teuchos_ParameterList.hpp" |
| 42 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 43 |
|
| 44 |
using namespace std; |
| 45 |
|
| 46 |
int main(int argc, char *argv[]) |
| 47 |
{ |
| 48 |
// Initialize MPI |
| 49 |
#ifdef HAVE_MPI |
| 50 |
MPI_Init(&argc,&argv); |
| 51 |
#endif |
| 52 |
|
| 53 |
// Create a communicator for Epetra objects |
| 54 |
#ifdef HAVE_MPI |
| 55 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 56 |
#else |
| 57 |
Epetra_SerialComm Comm; |
| 58 |
#endif |
| 59 |
|
| 60 |
// Get the process ID and the total number of processors |
| 61 |
int MyPID = Comm.MyPID(); |
| 62 |
int NumProc = Comm.NumProc(); |
| 63 |
|
| 64 |
// Check verbosity level |
| 65 |
bool verbose = false; |
| 66 |
if (argc > 1) |
| 67 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 68 |
verbose = true; |
| 69 |
|
| 70 |
// Get the number of elements from the command line |
| 71 |
int NumGlobalElements = 0; |
| 72 |
if ((argc > 2) && (verbose)) |
| 73 |
NumGlobalElements = atoi(argv[2]) + 1; |
| 74 |
else if ((argc > 1) && (!verbose)) |
| 75 |
NumGlobalElements = atoi(argv[1]) + 1; |
| 76 |
else |
| 77 |
NumGlobalElements = 101; |
| 78 |
|
| 79 |
bool success = false; |
| 80 |
try { |
| 81 |
|
| 82 |
// The number of unknowns must be at least equal to the |
| 83 |
// number of processors. |
| 84 |
if (NumGlobalElements < NumProc) { |
| 85 |
std::cout << "numGlobalBlocks = " << NumGlobalElements |
| 86 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 87 |
std::cout << "Test failed!" << std::endl; |
| 88 |
throw std::runtime_error("NOX Error"); |
| 89 |
} |
| 90 |
|
| 91 |
// Create the interface between NOX and the application |
| 92 |
// This object is derived from NOX::Epetra::Interface |
| 93 |
Teuchos::RCP<Interface> interface = |
| 94 |
Teuchos::rcp(new Interface(NumGlobalElements, Comm)); |
| 95 |
|
| 96 |
// Get the vector from the Problem |
| 97 |
Teuchos::RCP<Epetra_Vector> soln = interface->getSolution(); |
| 98 |
Teuchos::RCP<NOX::Epetra::Vector> noxSoln = |
| 99 |
Teuchos::rcp(new NOX::Epetra::Vector(soln, |
| 100 |
NOX::Epetra::Vector::CreateView)); |
| 101 |
|
| 102 |
// Set the PDE factor (for nonlinear forcing term). This could be specified |
| 103 |
// via user input. |
| 104 |
interface->setPDEfactor(1000.0); |
| 105 |
|
| 106 |
// Set the initial guess |
| 107 |
soln->PutScalar(1.0); |
| 108 |
|
| 109 |
// Begin Nonlinear Solver ************************************ |
| 110 |
|
| 111 |
// Create the top level parameter list |
| 112 |
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr = |
| 113 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 114 |
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get()); |
| 115 |
|
| 116 |
// Set the nonlinear solver method |
| 117 |
nlParams.set("Nonlinear Solver", "Line Search Based"); |
| 118 |
|
| 119 |
// Set the printing parameters in the "Printing" sublist |
| 120 |
Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); |
| 121 |
printParams.set("MyPID", MyPID); |
| 122 |
printParams.set("Output Precision", 3); |
| 123 |
printParams.set("Output Processor", 0); |
| 124 |
if (verbose) |
| 125 |
printParams.set("Output Information", |
| 126 |
NOX::Utils::OuterIteration + |
| 127 |
NOX::Utils::OuterIterationStatusTest + |
| 128 |
NOX::Utils::InnerIteration + |
| 129 |
NOX::Utils::LinearSolverDetails + |
| 130 |
NOX::Utils::Parameters + |
| 131 |
NOX::Utils::Details + |
| 132 |
NOX::Utils::Warning + |
| 133 |
NOX::Utils::Debug + |
| 134 |
NOX::Utils::TestDetails + |
| 135 |
NOX::Utils::Error); |
| 136 |
else |
| 137 |
printParams.set("Output Information", NOX::Utils::Error + |
| 138 |
NOX::Utils::TestDetails); |
| 139 |
|
| 140 |
// Create a print class for controlling output below |
| 141 |
NOX::Utils printing(printParams); |
| 142 |
|
| 143 |
// Sublist for line search |
| 144 |
Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); |
| 145 |
searchParams.set("Method", "Full Step"); |
| 146 |
|
| 147 |
// Sublist for direction |
| 148 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 149 |
dirParams.set("Method", "Newton"); |
| 150 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 151 |
newtonParams.set("Forcing Term Method", "Constant"); |
| 152 |
|
| 153 |
// Sublist for linear solver for the Newton method |
| 154 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 155 |
lsParams.set("Aztec Solver", "GMRES"); |
| 156 |
lsParams.set("Max Iterations", 800); |
| 157 |
lsParams.set("Tolerance", 1e-4); |
| 158 |
|
| 159 |
// Various Preconditioner options |
| 160 |
lsParams.set("Preconditioner", "New Ifpack"); |
| 161 |
lsParams.set("Preconditioner Reuse Policy", "Reuse"); |
| 162 |
lsParams.set("Max Age Of Prec", 5); |
| 163 |
|
| 164 |
// Let's force all status tests to do a full check |
| 165 |
nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete"); |
| 166 |
|
| 167 |
// Create all possible Epetra_Operators. |
| 168 |
// 1. User supplied (Epetra_RowMatrix) |
| 169 |
Teuchos::RCP<Epetra_RowMatrix> Analytic = interface->getJacobian(); |
| 170 |
// 2. Matrix-Free (Epetra_Operator) |
| 171 |
Teuchos::RCP<NOX::Epetra::MatrixFree> MF = |
| 172 |
Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface, |
| 173 |
*noxSoln)); |
| 174 |
// 3. Finite Difference (Epetra_RowMatrix) |
| 175 |
Teuchos::RCP<NOX::Epetra::FiniteDifference> FD = |
| 176 |
Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface, |
| 177 |
*soln)); |
| 178 |
|
| 179 |
// Create the linear system |
| 180 |
Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface; |
| 181 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF; |
| 182 |
Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD; |
| 183 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = |
| 184 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, |
| 185 |
//interface, |
| 186 |
iJac, MF, |
| 187 |
iPrec, FD, |
| 188 |
*soln)); |
| 189 |
|
| 190 |
// Create the Group |
| 191 |
NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView); |
| 192 |
Teuchos::RCP<NOX::Epetra::Group> grpPtr = |
| 193 |
Teuchos::rcp(new NOX::Epetra::Group(printParams, |
| 194 |
iReq, |
| 195 |
initialGuess, |
| 196 |
linSys)); |
| 197 |
NOX::Epetra::Group& grp = *grpPtr; |
| 198 |
|
| 199 |
// uncomment the following for loca supergroups |
| 200 |
//MF->setGroupForComputeF(*grpPtr); |
| 201 |
//FD->setGroupForComputeF(*grpPtr); |
| 202 |
|
| 203 |
// Create the convergence tests |
| 204 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 205 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 206 |
Teuchos::RCP<NOX::StatusTest::NormF> relresid = |
| 207 |
Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2)); |
| 208 |
Teuchos::RCP<NOX::StatusTest::NormUpdate> update = |
| 209 |
Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5)); |
| 210 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 211 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 212 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 213 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 214 |
converged->addStatusTest(absresid); |
| 215 |
converged->addStatusTest(relresid); |
| 216 |
converged->addStatusTest(wrms); |
| 217 |
converged->addStatusTest(update); |
| 218 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 219 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 220 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 221 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 222 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 223 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 224 |
combo->addStatusTest(fv); |
| 225 |
combo->addStatusTest(converged); |
| 226 |
combo->addStatusTest(maxiters); |
| 227 |
|
| 228 |
// Create the solver |
| 229 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 230 |
NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr); |
| 231 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 232 |
|
| 233 |
// End Nonlinear Solver ************************************** |
| 234 |
|
| 235 |
// Get the Epetra_Vector with the final solution from the solver |
| 236 |
const NOX::Epetra::Group& finalGroup = |
| 237 |
dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup()); |
| 238 |
const Epetra_Vector& finalSolution = |
| 239 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())). |
| 240 |
getEpetraVector(); |
| 241 |
|
| 242 |
// Output the parameter list |
| 243 |
if (verbose) { |
| 244 |
if (printing.isPrintType(NOX::Utils::Parameters)) { |
| 245 |
printing.out() << std::endl << "Final Parameters" << std::endl |
| 246 |
<< "****************" << std::endl; |
| 247 |
solver->getList().print(printing.out()); |
| 248 |
printing.out() << std::endl; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// Print solution |
| 253 |
char file_name[25]; |
| 254 |
FILE *ifp; |
| 255 |
int NumMyElements = soln->Map().NumMyElements(); |
| 256 |
(void) sprintf(file_name, "output.%d",MyPID); |
| 257 |
ifp = fopen(file_name, "w"); |
| 258 |
for (int i=0; i<NumMyElements; i++) |
| 259 |
fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]); |
| 260 |
fclose(ifp); |
| 261 |
|
| 262 |
|
| 263 |
// Tests |
| 264 |
int status = 0; // Converged |
| 265 |
|
| 266 |
// 1. Convergence |
| 267 |
if (solvStatus != NOX::StatusTest::Converged) { |
| 268 |
status = 1; |
| 269 |
if (printing.isPrintType(NOX::Utils::Error)) |
| 270 |
printing.out() << "Nonlinear solver failed to converge!" << std::endl; |
| 271 |
} |
| 272 |
#ifndef HAVE_MPI |
| 273 |
// 2. Linear solve iterations (53) - SERIAL TEST ONLY! |
| 274 |
// The number of linear iterations changes with # of procs. |
| 275 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 53) { |
| 276 |
status = 2; |
| 277 |
} |
| 278 |
#endif |
| 279 |
// 3. Nonlinear solve iterations (10) |
| 280 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 10) |
| 281 |
status = 3; |
| 282 |
|
| 283 |
success = status==0; |
| 284 |
|
| 285 |
// Summarize test results |
| 286 |
if (success) |
| 287 |
printing.out() << "Test passed!" << std::endl; |
| 288 |
else |
| 289 |
printing.out() << "Test failed!" << std::endl; |
| 290 |
|
| 291 |
printing.out() << "Status = " << status << std::endl; |
| 292 |
} |
| 293 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 294 |
|
| 295 |
#ifdef HAVE_MPI |
| 296 |
MPI_Finalize(); |
| 297 |
#endif |
| 298 |
|
| 299 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 300 |
} |