| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// LOCA: Library of Continuation Algorithms Package |
| 4 |
// |
| 5 |
// Copyright 2001-2005 NTESS and the LOCA contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "LOCA.H" |
| 11 |
#include "LOCA_Epetra.H" |
| 12 |
|
| 13 |
#include "NOX_TestCompare.H" |
| 14 |
|
| 15 |
// Trilinos Objects |
| 16 |
#ifdef HAVE_MPI |
| 17 |
#include "Epetra_MpiComm.h" |
| 18 |
#else |
| 19 |
#include "Epetra_SerialComm.h" |
| 20 |
#endif |
| 21 |
#include "Epetra_Map.h" |
| 22 |
#include "Epetra_RowMatrix.h" |
| 23 |
#include "Epetra_CrsMatrix.h" |
| 24 |
|
| 25 |
// User's application specific files |
| 26 |
#include "Problem_Interface.H" |
| 27 |
#include "Tcubed_FiniteElementProblem.H" |
| 28 |
|
| 29 |
int testTransposeSolve( |
| 30 |
int NumGlobalElements, |
| 31 |
int nRHS, |
| 32 |
double reltol, |
| 33 |
double abstol, |
| 34 |
Epetra_Comm& Comm, |
| 35 |
const Teuchos::RCP<LOCA::GlobalData>& globalData, |
| 36 |
const Teuchos::RCP<Teuchos::ParameterList>& paramList) |
| 37 |
{ |
| 38 |
int ierr = 0; |
| 39 |
|
| 40 |
double left_bc = 0.0; |
| 41 |
double right_bc = 1.0; |
| 42 |
double nonlinear_factor = 1.0; |
| 43 |
|
| 44 |
// Create the FiniteElementProblem class. This creates all required |
| 45 |
// Epetra objects for the problem and allows calls to the |
| 46 |
// function (RHS) and Jacobian evaluation routines. |
| 47 |
Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm); |
| 48 |
|
| 49 |
// Get the vector from the Problem |
| 50 |
Epetra_Vector& soln = Problem.getSolution(); |
| 51 |
|
| 52 |
// Initialize Solution |
| 53 |
soln.PutScalar(0.0); |
| 54 |
|
| 55 |
// Create and initialize the parameter vector |
| 56 |
LOCA::ParameterVector pVector; |
| 57 |
pVector.addParameter("Nonlinear Factor",nonlinear_factor); |
| 58 |
pVector.addParameter("Left BC", left_bc); |
| 59 |
pVector.addParameter("Right BC", right_bc); |
| 60 |
|
| 61 |
// Create the interface between the test problem and the nonlinear solver |
| 62 |
// This is created by the user using inheritance of the abstract base |
| 63 |
// class: |
| 64 |
Teuchos::RCP<Problem_Interface> interface = |
| 65 |
Teuchos::rcp(new Problem_Interface(Problem)); |
| 66 |
Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface; |
| 67 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface; |
| 68 |
|
| 69 |
// Create the Epetra_RowMatrixfor the Jacobian/Preconditioner |
| 70 |
Teuchos::RCP<Epetra_RowMatrix> Amat = |
| 71 |
Teuchos::rcp(&Problem.getJacobian(),false); |
| 72 |
|
| 73 |
// Get sublists |
| 74 |
Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); |
| 75 |
Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); |
| 76 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 77 |
Teuchos::ParameterList& newParams = dirParams.sublist("Newton"); |
| 78 |
Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver"); |
| 79 |
|
| 80 |
// Create the linear systems |
| 81 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys = |
| 82 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams, |
| 83 |
lsParams, iReq, iJac, |
| 84 |
Amat, soln)); |
| 85 |
|
| 86 |
// Create the loca vector |
| 87 |
NOX::Epetra::Vector locaSoln(soln); |
| 88 |
|
| 89 |
// Create the Group |
| 90 |
Teuchos::RCP<LOCA::Epetra::Group> grp_tp = |
| 91 |
Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, |
| 92 |
iReq, locaSoln, |
| 93 |
linsys, pVector)); |
| 94 |
|
| 95 |
// Create the Group |
| 96 |
Teuchos::RCP<LOCA::Epetra::Group> grp_lp = |
| 97 |
Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, |
| 98 |
iReq, locaSoln, |
| 99 |
linsys, pVector)); |
| 100 |
|
| 101 |
// Create the Group |
| 102 |
Teuchos::RCP<LOCA::Epetra::Group> grp_ep = |
| 103 |
Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, |
| 104 |
iReq, locaSoln, |
| 105 |
linsys, pVector)); |
| 106 |
|
| 107 |
// Change initial guess to a random vector |
| 108 |
Teuchos::RCP<NOX::Abstract::Vector> xnew = |
| 109 |
grp_tp->getX().clone(); |
| 110 |
xnew->random(); |
| 111 |
grp_tp->setX(*xnew); |
| 112 |
grp_lp->setX(*xnew); |
| 113 |
grp_ep->setX(*xnew); |
| 114 |
|
| 115 |
// Check some statistics on the solution |
| 116 |
Teuchos::RCP<NOX::TestCompare> testCompare = |
| 117 |
Teuchos::rcp(new NOX::TestCompare(globalData->locaUtils->out(), |
| 118 |
*(globalData->locaUtils))); |
| 119 |
|
| 120 |
// Evaluate blocks |
| 121 |
grp_tp->computeF(); |
| 122 |
grp_lp->computeF(); |
| 123 |
grp_ep->computeF(); |
| 124 |
grp_tp->computeJacobian(); |
| 125 |
grp_lp->computeJacobian(); |
| 126 |
grp_ep->computeJacobian(); |
| 127 |
|
| 128 |
// Set up left- and right-hand sides |
| 129 |
Teuchos::RCP<NOX::Abstract::MultiVector> F = |
| 130 |
grp_tp->getX().createMultiVector(nRHS); |
| 131 |
F->random(); |
| 132 |
Teuchos::RCP<NOX::Abstract::MultiVector> X_tp = |
| 133 |
F->clone(NOX::ShapeCopy); |
| 134 |
X_tp->init(0.0); |
| 135 |
Teuchos::RCP<NOX::Abstract::MultiVector> X_lp = |
| 136 |
F->clone(NOX::ShapeCopy); |
| 137 |
X_lp->init(0.0); |
| 138 |
Teuchos::RCP<NOX::Abstract::MultiVector> X_ep = |
| 139 |
F->clone(NOX::ShapeCopy); |
| 140 |
X_ep->init(0.0); |
| 141 |
|
| 142 |
// Set up residuals |
| 143 |
Teuchos::RCP<NOX::Abstract::MultiVector> R_tp = |
| 144 |
F->clone(NOX::ShapeCopy); |
| 145 |
Teuchos::RCP<NOX::Abstract::MultiVector> R_lp = |
| 146 |
F->clone(NOX::ShapeCopy); |
| 147 |
Teuchos::RCP<NOX::Abstract::MultiVector> R_ep = |
| 148 |
F->clone(NOX::ShapeCopy); |
| 149 |
|
| 150 |
// Set up linear solver lists |
| 151 |
Teuchos::ParameterList lsParams_tp = lsParams; |
| 152 |
Teuchos::ParameterList lsParams_lp = lsParams; |
| 153 |
Teuchos::ParameterList lsParams_ep = lsParams; |
| 154 |
lsParams_tp.set("Transpose Solver Method", |
| 155 |
"Transpose Preconditioner"); |
| 156 |
lsParams_lp.set("Transpose Solver Method", |
| 157 |
"Left Preconditioning"); |
| 158 |
lsParams_ep.set("Transpose Solver Method", |
| 159 |
"Explicit Transpose"); |
| 160 |
|
| 161 |
NOX::Abstract::Group::ReturnType status; |
| 162 |
|
| 163 |
// Test transpose solve with transposed preconditioner |
| 164 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 165 |
globalData->locaUtils->out() << std::endl << |
| 166 |
"\t***** " << |
| 167 |
"Testing Transposed Preconditioner Residual" << |
| 168 |
" *****" << std::endl; |
| 169 |
|
| 170 |
status = grp_tp->applyJacobianTransposeInverseMultiVector(lsParams_tp, *F, |
| 171 |
*X_tp); |
| 172 |
if (status == NOX::Abstract::Group::Failed) |
| 173 |
++ierr; |
| 174 |
status = grp_tp->applyJacobianTransposeMultiVector(*X_tp, *R_tp); |
| 175 |
ierr += testCompare->testMultiVector(*R_tp, *F, reltol, abstol, |
| 176 |
"Residual"); |
| 177 |
|
| 178 |
// Test transpose solve with transposed left-preconditioner |
| 179 |
if (lsParams.get("Preconditioner", "None") != "None") { |
| 180 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 181 |
globalData->locaUtils->out() << std::endl << |
| 182 |
"\t***** " << |
| 183 |
"Testing Transposed Left Preconditioner Residual" << |
| 184 |
" *****" << std::endl; |
| 185 |
|
| 186 |
status = grp_lp->applyJacobianTransposeInverseMultiVector(lsParams_lp, *F, |
| 187 |
*X_lp); |
| 188 |
if (status == NOX::Abstract::Group::Failed) |
| 189 |
++ierr; |
| 190 |
status = grp_lp->applyJacobianTransposeMultiVector(*X_lp, *R_lp); |
| 191 |
ierr += testCompare->testMultiVector(*R_lp, *F, reltol, abstol, |
| 192 |
"Residual"); |
| 193 |
} |
| 194 |
|
| 195 |
#ifdef HAVE_NOX_EPETRAEXT |
| 196 |
// Test transpose solve with explicit preconditioner |
| 197 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 198 |
globalData->locaUtils->out() << std::endl << |
| 199 |
"\t***** " << |
| 200 |
"Testing Explicit Preconditioner Residual" << |
| 201 |
" *****" << std::endl; |
| 202 |
|
| 203 |
status = grp_ep->applyJacobianTransposeInverseMultiVector(lsParams_ep, *F, |
| 204 |
*X_ep); |
| 205 |
if (status == NOX::Abstract::Group::Failed) |
| 206 |
++ierr; |
| 207 |
status = grp_ep->applyJacobianTransposeMultiVector(*X_ep, *R_ep); |
| 208 |
ierr += testCompare->testMultiVector(*R_ep, *F, reltol, abstol, |
| 209 |
"Residual"); |
| 210 |
#endif |
| 211 |
|
| 212 |
|
| 213 |
// Compare solutions |
| 214 |
if (lsParams.get("Preconditioner", "None") != "None") { |
| 215 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 216 |
globalData->locaUtils->out() << std::endl << |
| 217 |
"\t***** " << |
| 218 |
"Comparing Transposed Preconditioner and Left Preconditioner Solutions" |
| 219 |
<< |
| 220 |
" *****" << std::endl; |
| 221 |
ierr += testCompare->testMultiVector(*X_lp, *X_tp, |
| 222 |
reltol, abstol, "Solution"); |
| 223 |
} |
| 224 |
|
| 225 |
#ifdef HAVE_NOX_EPETRAEXT |
| 226 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 227 |
globalData->locaUtils->out() << std::endl << |
| 228 |
"\t***** " << |
| 229 |
"Comparing Transposed Preconditioner and Explicit Preconditioner Solutions" |
| 230 |
<< |
| 231 |
" *****" << std::endl; |
| 232 |
ierr += testCompare->testMultiVector(*X_ep, *X_tp, |
| 233 |
reltol, abstol, "Solution"); |
| 234 |
#endif |
| 235 |
|
| 236 |
return ierr; |
| 237 |
} |
| 238 |
int main(int argc, char *argv[]) |
| 239 |
{ |
| 240 |
int ierr = 0; |
| 241 |
int MyPID = 0; |
| 242 |
|
| 243 |
int nRHS = 7; |
| 244 |
double reltol = 1.0e-8; |
| 245 |
double abstol = 1.0e-8; |
| 246 |
double lstol = 1.0e-11; |
| 247 |
int ls_its = 100; |
| 248 |
|
| 249 |
try { |
| 250 |
|
| 251 |
// Initialize MPI |
| 252 |
#ifdef HAVE_MPI |
| 253 |
MPI_Init(&argc,&argv); |
| 254 |
#endif |
| 255 |
|
| 256 |
// Create a communicator for Epetra objects |
| 257 |
#ifdef HAVE_MPI |
| 258 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 259 |
#else |
| 260 |
Epetra_SerialComm Comm; |
| 261 |
#endif |
| 262 |
|
| 263 |
// Get the total number of processors |
| 264 |
MyPID = Comm.MyPID(); |
| 265 |
int NumProc = Comm.NumProc(); |
| 266 |
|
| 267 |
bool verbose = false; |
| 268 |
// Check for verbose output |
| 269 |
if (argc>1) |
| 270 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 271 |
verbose = true; |
| 272 |
|
| 273 |
// Get the number of elements from the command line |
| 274 |
int NumGlobalElements = 0; |
| 275 |
if ((argc > 2) && (verbose)) |
| 276 |
NumGlobalElements = atoi(argv[2]) + 1; |
| 277 |
else if ((argc > 1) && (!verbose)) |
| 278 |
NumGlobalElements = atoi(argv[1]) + 1; |
| 279 |
else |
| 280 |
NumGlobalElements = 101; |
| 281 |
|
| 282 |
// The number of unknowns must be at least equal to the |
| 283 |
// number of processors. |
| 284 |
if (NumGlobalElements < NumProc) { |
| 285 |
std::cout << "numGlobalBlocks = " << NumGlobalElements |
| 286 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 287 |
exit(1); |
| 288 |
} |
| 289 |
|
| 290 |
// Create parameter list |
| 291 |
Teuchos::RCP<Teuchos::ParameterList> paramList = |
| 292 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 293 |
|
| 294 |
// Create the "Solver" parameters sublist to be used with NOX Solvers |
| 295 |
Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); |
| 296 |
|
| 297 |
Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); |
| 298 |
nlPrintParams.set("MyPID", MyPID); |
| 299 |
if (verbose) |
| 300 |
nlPrintParams.set("Output Information", |
| 301 |
NOX::Utils::Error + |
| 302 |
NOX::Utils::Details + |
| 303 |
NOX::Utils::OuterIteration + |
| 304 |
NOX::Utils::InnerIteration + |
| 305 |
NOX::Utils::Warning + |
| 306 |
NOX::Utils::TestDetails + |
| 307 |
NOX::Utils::StepperIteration + |
| 308 |
NOX::Utils::StepperDetails); |
| 309 |
else |
| 310 |
nlPrintParams.set("Output Information", NOX::Utils::Error); |
| 311 |
|
| 312 |
// Create the "Direction" sublist for the "Line Search Based" solver |
| 313 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 314 |
Teuchos::ParameterList& newParams = dirParams.sublist("Newton"); |
| 315 |
Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver"); |
| 316 |
lsParams.set("Aztec Solver", "GMRES"); |
| 317 |
lsParams.set("Max Iterations", ls_its); |
| 318 |
lsParams.set("Tolerance", lstol); |
| 319 |
if (verbose) |
| 320 |
lsParams.set("Output Frequency", 1); |
| 321 |
else |
| 322 |
lsParams.set("Output Frequency", 0); |
| 323 |
lsParams.set("Scaling", "None"); |
| 324 |
|
| 325 |
//lsParams.set("Overlap", 2); |
| 326 |
//lsParams.set("Fill Factor", 2.0); |
| 327 |
//lsParams.set("Drop Tolerance", 1.0e-12); |
| 328 |
|
| 329 |
// Create Epetra factory |
| 330 |
Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory = |
| 331 |
Teuchos::rcp(new LOCA::Epetra::Factory); |
| 332 |
|
| 333 |
// Create global data object |
| 334 |
Teuchos::RCP<LOCA::GlobalData> globalData = |
| 335 |
LOCA::createGlobalData(paramList, epetraFactory); |
| 336 |
|
| 337 |
// Test transpose solves with Ifpack preconditioner |
| 338 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 339 |
globalData->locaUtils->out() << std::endl << |
| 340 |
"********** " << |
| 341 |
"Testing Transpose Solves With Ifpack Preconditioner" << |
| 342 |
" **********" << std::endl; |
| 343 |
lsParams.set("Preconditioner", "Ifpack"); |
| 344 |
ierr += testTransposeSolve(NumGlobalElements, nRHS, reltol, abstol, |
| 345 |
Comm, globalData, paramList); |
| 346 |
|
| 347 |
// Test transpose solves with no preconditioner |
| 348 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 349 |
globalData->locaUtils->out() << std::endl << |
| 350 |
"********** " << |
| 351 |
"Testing Transpose Solves With No Preconditioner" << |
| 352 |
" **********" << std::endl; |
| 353 |
lsParams.set("Preconditioner", "None"); |
| 354 |
lsParams.set("Max Iterations", NumGlobalElements+1); |
| 355 |
ierr += testTransposeSolve(NumGlobalElements, nRHS, reltol, abstol, |
| 356 |
Comm, globalData, paramList); |
| 357 |
|
| 358 |
LOCA::destroyGlobalData(globalData); |
| 359 |
} |
| 360 |
|
| 361 |
catch (std::exception& e) { |
| 362 |
std::cout << e.what() << std::endl; |
| 363 |
ierr = 1; |
| 364 |
} |
| 365 |
catch (const char *s) { |
| 366 |
std::cout << s << std::endl; |
| 367 |
ierr = 1; |
| 368 |
} |
| 369 |
catch (...) { |
| 370 |
std::cout << "Caught unknown exception!" << std::endl; |
| 371 |
ierr = 1; |
| 372 |
} |
| 373 |
|
| 374 |
if (MyPID == 0) { |
| 375 |
if (ierr == 0) |
| 376 |
std::cout << "All tests passed!" << std::endl; |
| 377 |
else |
| 378 |
std::cout << ierr << " test(s) failed!" << std::endl; |
| 379 |
} |
| 380 |
|
| 381 |
#ifdef HAVE_MPI |
| 382 |
MPI_Finalize() ; |
| 383 |
#endif |
| 384 |
|
| 385 |
return ierr; |
| 386 |
} |