| 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 |
|
| 43 |
using namespace std; |
| 44 |
|
| 45 |
int main(int argc, char *argv[]) |
| 46 |
{ |
| 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 |
// The number of unknowns must be at least equal to the number of processors. |
| 80 |
if (NumGlobalElements < NumProc) { |
| 81 |
std::cout << "numGlobalBlocks = " << NumGlobalElements |
| 82 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 83 |
std::cout << "Test failed!" << std::endl; |
| 84 |
throw std::runtime_error("NOX Error"); |
| 85 |
} |
| 86 |
|
| 87 |
// Create the interface between NOX and the application |
| 88 |
// This object is derived from NOX::Epetra::Interface |
| 89 |
Teuchos::RCP<Interface> interface = Teuchos::rcp(new Interface(NumGlobalElements, Comm)); |
| 90 |
|
| 91 |
// Get the vector from the Problem |
| 92 |
Teuchos::RCP<Epetra_Vector> soln = interface->getSolution(); |
| 93 |
Teuchos::RCP<NOX::Epetra::Vector> noxSoln = |
| 94 |
Teuchos::rcp(new NOX::Epetra::Vector(soln, NOX::Epetra::Vector::CreateView)); |
| 95 |
|
| 96 |
// Set the PDE factor (for nonlinear forcing term). |
| 97 |
interface->setPDEfactor(1000.0); |
| 98 |
|
| 99 |
// Set the initial guess - this particular choice activates the linesearch |
| 100 |
soln->PutScalar(0.0); |
| 101 |
|
| 102 |
// Begin Nonlinear Solver ************************************ |
| 103 |
|
| 104 |
// Create the top level parameter list |
| 105 |
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr = Teuchos::rcp(new Teuchos::ParameterList); |
| 106 |
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get()); |
| 107 |
|
| 108 |
// Set the nonlinear solver method |
| 109 |
nlParams.set("Nonlinear Solver", "Line Search Based"); |
| 110 |
|
| 111 |
// Set the printing parameters in the "Printing" sublist |
| 112 |
Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); |
| 113 |
printParams.set("MyPID", MyPID); |
| 114 |
printParams.set("Output Precision", 3); |
| 115 |
printParams.set("Output Processor", 0); |
| 116 |
if (verbose) |
| 117 |
printParams.set("Output Information", |
| 118 |
NOX::Utils::OuterIteration + |
| 119 |
NOX::Utils::OuterIterationStatusTest + |
| 120 |
NOX::Utils::InnerIteration + |
| 121 |
NOX::Utils::LinearSolverDetails + |
| 122 |
NOX::Utils::Parameters + |
| 123 |
NOX::Utils::Details + |
| 124 |
NOX::Utils::Warning + |
| 125 |
NOX::Utils::Debug + |
| 126 |
NOX::Utils::TestDetails + |
| 127 |
NOX::Utils::Error); |
| 128 |
else |
| 129 |
printParams.set("Output Information", NOX::Utils::Error + |
| 130 |
NOX::Utils::TestDetails); |
| 131 |
|
| 132 |
// Create a print class for controlling output below |
| 133 |
NOX::Utils printing(printParams); |
| 134 |
|
| 135 |
// Sublist for line search |
| 136 |
Teuchos::ParameterList& linesearchParams = nlParams.sublist("Line Search"); |
| 137 |
linesearchParams.set("Method", "Polynomial"); |
| 138 |
|
| 139 |
// Sublist for direction |
| 140 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 141 |
dirParams.set("Method", "Newton"); |
| 142 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 143 |
newtonParams.set("Forcing Term Method", "Constant"); |
| 144 |
|
| 145 |
// Sublist for linear solver for the Newton method |
| 146 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 147 |
lsParams.set("Aztec Solver", "GMRES"); |
| 148 |
lsParams.set("Max Iterations", 800); |
| 149 |
lsParams.set("Tolerance", 1e-4); |
| 150 |
|
| 151 |
// Setup a reasonable preconditioner ... |
| 152 |
lsParams.set("Preconditioner", "New Ifpack"); |
| 153 |
lsParams.set("Preconditioner Reuse Policy", "Reuse"); |
| 154 |
lsParams.set("Output Frequency", 1); |
| 155 |
// ... and some resonable specifics for our particular choice |
| 156 |
Teuchos::ParameterList& ifpackParams = lsParams.sublist("Ifpack"); |
| 157 |
ifpackParams.set("fact: level-of-fill", 3); |
| 158 |
lsParams.set("Overlap", 1); |
| 159 |
|
| 160 |
// Let's force all status tests to do a full check |
| 161 |
nlParams.sublist("Solver Options").set("Status Test Check Type", "Complete"); |
| 162 |
|
| 163 |
// Create out Jacobian and preconditnioer opertors |
| 164 |
// ... Matrix-Free Jacobian |
| 165 |
Teuchos::RCP<NOX::Epetra::MatrixFree> MF = |
| 166 |
Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface, *noxSoln)); |
| 167 |
// ... Finite Difference Preconditioner |
| 168 |
Teuchos::RCP<NOX::Epetra::FiniteDifference> FD = |
| 169 |
Teuchos::rcp(new NOX::Epetra::FiniteDifference(printParams, interface, *soln)); |
| 170 |
|
| 171 |
// Create the linear system |
| 172 |
Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface; |
| 173 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = MF; |
| 174 |
Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> iPrec = FD; |
| 175 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = |
| 176 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, |
| 177 |
iJac, MF, |
| 178 |
iPrec, FD, |
| 179 |
*soln)); |
| 180 |
|
| 181 |
// Create the Group |
| 182 |
NOX::Epetra::Vector initialGuess(soln, NOX::Epetra::Vector::CreateView); |
| 183 |
Teuchos::RCP<NOX::Epetra::Group> grpPtr = |
| 184 |
Teuchos::rcp(new NOX::Epetra::Group(printParams, iReq, initialGuess, linSys)); |
| 185 |
NOX::Epetra::Group& grp = *grpPtr; |
| 186 |
|
| 187 |
// Create the convergence tests |
| 188 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 189 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 190 |
Teuchos::RCP<NOX::StatusTest::NormF> relresid = |
| 191 |
Teuchos::rcp(new NOX::StatusTest::NormF(grp, 1.0e-2)); |
| 192 |
Teuchos::RCP<NOX::StatusTest::NormUpdate> update = |
| 193 |
Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5)); |
| 194 |
Teuchos::RCP<NOX::StatusTest::NormWRMS> wrms = |
| 195 |
Teuchos::rcp(new NOX::StatusTest::NormWRMS(1.0e-2, 1.0e-8)); |
| 196 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 197 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 198 |
converged->addStatusTest(absresid); |
| 199 |
converged->addStatusTest(relresid); |
| 200 |
converged->addStatusTest(wrms); |
| 201 |
converged->addStatusTest(update); |
| 202 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 203 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 204 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> fv = |
| 205 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 206 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 207 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 208 |
combo->addStatusTest(fv); |
| 209 |
combo->addStatusTest(converged); |
| 210 |
combo->addStatusTest(maxiters); |
| 211 |
|
| 212 |
// Create the solver |
| 213 |
Teuchos::RCP<NOX::Solver::Generic> solver = NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr); |
| 214 |
|
| 215 |
// ... and fire off the solve |
| 216 |
NOX::StatusTest::StatusType solvStatus = solver->solve(); |
| 217 |
|
| 218 |
// End Nonlinear Solver ************************************** |
| 219 |
|
| 220 |
// Get the Epetra_Vector with the final solution from the solver |
| 221 |
const NOX::Epetra::Group& finalGroup = |
| 222 |
dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup()); |
| 223 |
const Epetra_Vector& finalSolution = |
| 224 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector(); |
| 225 |
|
| 226 |
// Output the parameter list |
| 227 |
if (verbose) |
| 228 |
{ |
| 229 |
if (printing.isPrintType(NOX::Utils::Parameters)) |
| 230 |
{ |
| 231 |
printing.out() << std::endl << "Final Parameters" << std::endl |
| 232 |
<< "****************" << std::endl; |
| 233 |
solver->getList().print(printing.out()); |
| 234 |
printing.out() << std::endl; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
// Print solution |
| 239 |
char file_name[25]; |
| 240 |
FILE *ifp; |
| 241 |
int NumMyElements = soln->Map().NumMyElements(); |
| 242 |
(void) sprintf(file_name, "output.%d",MyPID); |
| 243 |
ifp = fopen(file_name, "w"); |
| 244 |
for (int i=0; i<NumMyElements; i++) |
| 245 |
fprintf(ifp, "%d %E\n", soln->Map().MinMyGID()+i, finalSolution[i]); |
| 246 |
fclose(ifp); |
| 247 |
|
| 248 |
|
| 249 |
// Tests |
| 250 |
int status = 0; // Converged |
| 251 |
|
| 252 |
// 1. Convergence |
| 253 |
if (solvStatus != NOX::StatusTest::Converged) { |
| 254 |
status = 1; |
| 255 |
if (printing.isPrintType(NOX::Utils::Error)) |
| 256 |
printing.out() << "Nonlinear solver failed to converge!" << std::endl; |
| 257 |
} |
| 258 |
// 2. Nonlinear solve iterations (16) |
| 259 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations", 0) != 16) |
| 260 |
status = 2; |
| 261 |
// 3. Polynomial Linesearch inner iterations (13) |
| 262 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Line Search").sublist("Output"). |
| 263 |
get("Total Number of Line Search Inner Iterations", 0) != 13) |
| 264 |
status = 3; |
| 265 |
// Summarize test results |
| 266 |
if (status == 0) |
| 267 |
printing.out() << "Test passed!" << std::endl; |
| 268 |
else |
| 269 |
printing.out() << "Test failed!" << std::endl; |
| 270 |
|
| 271 |
#ifdef HAVE_MPI |
| 272 |
MPI_Finalize(); |
| 273 |
#endif |
| 274 |
|
| 275 |
printing.out() << "Status = " << status << std::endl; |
| 276 |
|
| 277 |
// Final return value (0 = successfull, non-zero = failure) |
| 278 |
return status; |
| 279 |
} |