| 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 |
// Does a few steps of natural continuation and checks the number of |
| 11 |
// residual fills for bug #3013 |
| 12 |
/* Solves continuation problem (Parameter c="Right BC") |
| 13 |
* |
| 14 |
* d2u |
| 15 |
* --- + a * u**3 = 0 |
| 16 |
* dx2 |
| 17 |
* |
| 18 |
* subject to @ x=0, u=b |
| 19 |
* subject to @ x=1, u=c |
| 20 |
*/ |
| 21 |
|
| 22 |
// LOCA Objects |
| 23 |
#include "LOCA.H" |
| 24 |
#include "LOCA_Epetra.H" |
| 25 |
#include "NOX_TestCompare.H" |
| 26 |
|
| 27 |
// Trilinos Objects |
| 28 |
#ifdef HAVE_MPI |
| 29 |
#include "Epetra_MpiComm.h" |
| 30 |
#else |
| 31 |
#include "Epetra_SerialComm.h" |
| 32 |
#endif |
| 33 |
#include "Epetra_Map.h" |
| 34 |
#include "Epetra_Vector.h" |
| 35 |
#include "Epetra_RowMatrix.h" |
| 36 |
#include "Epetra_CrsMatrix.h" |
| 37 |
#include "Epetra_Map.h" |
| 38 |
#include "Epetra_LinearProblem.h" |
| 39 |
#include "AztecOO.h" |
| 40 |
|
| 41 |
// User's application specific files |
| 42 |
#include "Problem_Interface.H" // Interface file to NOX |
| 43 |
#include "Tcubed_FiniteElementProblem.H" |
| 44 |
|
| 45 |
using namespace std; |
| 46 |
|
| 47 |
int main(int argc, char *argv[]) |
| 48 |
{ |
| 49 |
int ierr = 0; |
| 50 |
int MyPID = 0; |
| 51 |
|
| 52 |
try { |
| 53 |
|
| 54 |
// scale factor to test arc-length scaling |
| 55 |
double scale = 1.0; |
| 56 |
|
| 57 |
// Initialize MPI |
| 58 |
#ifdef HAVE_MPI |
| 59 |
MPI_Init(&argc,&argv); |
| 60 |
#endif |
| 61 |
|
| 62 |
// Create a communicator for Epetra objects |
| 63 |
#ifdef HAVE_MPI |
| 64 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 65 |
#else |
| 66 |
Epetra_SerialComm Comm; |
| 67 |
#endif |
| 68 |
|
| 69 |
// Get the process ID and the total number of processors |
| 70 |
MyPID = Comm.MyPID(); |
| 71 |
int NumProc = Comm.NumProc(); |
| 72 |
|
| 73 |
// Check for verbose output |
| 74 |
bool verbose = false; |
| 75 |
if (argc>1) |
| 76 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 77 |
verbose = true; |
| 78 |
|
| 79 |
// Get the number of elements from the command line |
| 80 |
int NumGlobalElements = 0; |
| 81 |
if ((argc > 2) && (verbose)) |
| 82 |
NumGlobalElements = atoi(argv[2]) + 1; |
| 83 |
else if ((argc > 1) && (!verbose)) |
| 84 |
NumGlobalElements = atoi(argv[1]) + 1; |
| 85 |
else |
| 86 |
NumGlobalElements = 101; |
| 87 |
|
| 88 |
// The number of unknowns must be at least equal to the |
| 89 |
// number of processors. |
| 90 |
if (NumGlobalElements < NumProc) { |
| 91 |
std::cout << "numGlobalBlocks = " << NumGlobalElements |
| 92 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 93 |
exit(1); |
| 94 |
} |
| 95 |
|
| 96 |
// Create the FiniteElementProblem class. This creates all required |
| 97 |
// Epetra objects for the problem and allows calls to the |
| 98 |
// function (RHS) and Jacobian evaluation routines. |
| 99 |
Tcubed_FiniteElementProblem Problem(NumGlobalElements, Comm, scale); |
| 100 |
|
| 101 |
// Get the vector from the Problem |
| 102 |
Epetra_Vector& soln = Problem.getSolution(); |
| 103 |
|
| 104 |
// Initialize Solution |
| 105 |
soln.PutScalar(1.0); |
| 106 |
|
| 107 |
// Begin LOCA Solver ************************************ |
| 108 |
|
| 109 |
// Create parameter list |
| 110 |
Teuchos::RCP<Teuchos::ParameterList> paramList = |
| 111 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 112 |
|
| 113 |
// Create LOCA sublist |
| 114 |
Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA"); |
| 115 |
|
| 116 |
// Create the stepper sublist and set the stepper parameters |
| 117 |
Teuchos::ParameterList& locaStepperList = |
| 118 |
locaParamsList.sublist("Stepper"); |
| 119 |
locaStepperList.set("Continuation Method", "Natural"); |
| 120 |
locaStepperList.set("Skip Parameter Derivative", false); |
| 121 |
locaStepperList.set("Continuation Parameter", "Right BC"); |
| 122 |
locaStepperList.set("Initial Value", 0.1); |
| 123 |
locaStepperList.set("Max Value", 0.3); |
| 124 |
locaStepperList.set("Min Value", 0.0); |
| 125 |
locaStepperList.set("Max Steps", 5); |
| 126 |
locaStepperList.set("Max Nonlinear Iterations", 15); |
| 127 |
locaStepperList.set("Skip df/dp", false); |
| 128 |
|
| 129 |
// Create predictor sublist |
| 130 |
Teuchos::ParameterList& predictorList = |
| 131 |
locaParamsList.sublist("Predictor"); |
| 132 |
predictorList.set("Method", "Constant"); |
| 133 |
|
| 134 |
// Create step size sublist |
| 135 |
Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size"); |
| 136 |
stepSizeList.set("Method", "Constant"); |
| 137 |
stepSizeList.set("Initial Step Size", 0.1); |
| 138 |
stepSizeList.set("Min Step Size", 0.1); |
| 139 |
stepSizeList.set("Max Step Size", 0.1); |
| 140 |
|
| 141 |
// Create the "Solver" parameters sublist to be used with NOX Solvers |
| 142 |
Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); |
| 143 |
|
| 144 |
// Create the NOX printing parameter list |
| 145 |
Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); |
| 146 |
nlPrintParams.set("MyPID", MyPID); |
| 147 |
if (verbose) |
| 148 |
nlPrintParams.set("Output Information", |
| 149 |
NOX::Utils::OuterIteration + |
| 150 |
NOX::Utils::OuterIterationStatusTest + |
| 151 |
NOX::Utils::InnerIteration + |
| 152 |
//NOX::Utils::Details + |
| 153 |
NOX::Utils::Warning + |
| 154 |
NOX::Utils::TestDetails + |
| 155 |
NOX::Utils::Error + |
| 156 |
NOX::Utils::StepperIteration + |
| 157 |
NOX::Utils::StepperDetails + |
| 158 |
NOX::Utils::StepperParameters); |
| 159 |
else |
| 160 |
nlPrintParams.set("Output Information", NOX::Utils::Error); |
| 161 |
|
| 162 |
// Create the "Linear Solver" sublist |
| 163 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 164 |
Teuchos::ParameterList& newParams = dirParams.sublist("Newton"); |
| 165 |
Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver"); |
| 166 |
lsParams.set("Aztec Solver", "GMRES"); |
| 167 |
lsParams.set("Max Iterations", 100); |
| 168 |
lsParams.set("Tolerance", 1e-4); |
| 169 |
if (verbose) |
| 170 |
lsParams.set("Output Frequency", 1); |
| 171 |
else |
| 172 |
lsParams.set("Output Frequency", 0); |
| 173 |
lsParams.set("Scaling", "None"); |
| 174 |
lsParams.set("Preconditioner", "Ifpack"); |
| 175 |
|
| 176 |
// Create and initialize the parameter vector |
| 177 |
LOCA::ParameterVector pVector; |
| 178 |
pVector.addParameter("Nonlinear Factor",1.0); |
| 179 |
pVector.addParameter("Left BC", 0.0); |
| 180 |
pVector.addParameter("Right BC", 0.1); |
| 181 |
|
| 182 |
// Create the interface between the test problem and the nonlinear solver |
| 183 |
// This is created by the user using inheritance of the abstract base |
| 184 |
// class: |
| 185 |
Teuchos::RCP<Problem_Interface> interface = |
| 186 |
Teuchos::rcp(new Problem_Interface(Problem)); |
| 187 |
Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface; |
| 188 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface; |
| 189 |
|
| 190 |
// Create the Epetra_RowMatrixfor the Jacobian/Preconditioner |
| 191 |
Teuchos::RCP<Epetra_RowMatrix> Amat = |
| 192 |
Teuchos::rcp(&Problem.getJacobian(),false); |
| 193 |
|
| 194 |
// Create the linear systems |
| 195 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys = |
| 196 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams, |
| 197 |
lsParams, iReq, iJac, |
| 198 |
Amat, soln)); |
| 199 |
|
| 200 |
// Create the loca vector |
| 201 |
NOX::Epetra::Vector locaSoln(soln); |
| 202 |
|
| 203 |
// Create Epetra factory |
| 204 |
Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory = |
| 205 |
Teuchos::rcp(new LOCA::Epetra::Factory); |
| 206 |
|
| 207 |
// Create global data object |
| 208 |
Teuchos::RCP<LOCA::GlobalData> globalData = |
| 209 |
LOCA::createGlobalData(paramList, epetraFactory); |
| 210 |
|
| 211 |
// Create the Group |
| 212 |
Teuchos::RCP<LOCA::Epetra::Group> grp = |
| 213 |
Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, |
| 214 |
iReq, locaSoln, |
| 215 |
linsys, pVector)); |
| 216 |
grp->computeF(); |
| 217 |
|
| 218 |
// Create the Solver convergence test |
| 219 |
Teuchos::RCP<NOX::StatusTest::NormF> wrms = |
| 220 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 221 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 222 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(15)); |
| 223 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 224 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 225 |
combo->addStatusTest(wrms); |
| 226 |
combo->addStatusTest(maxiters); |
| 227 |
|
| 228 |
// Create the stepper |
| 229 |
LOCA::Stepper stepper(globalData, grp, combo, paramList); |
| 230 |
LOCA::Abstract::Iterator::IteratorStatus status = stepper.run(); |
| 231 |
|
| 232 |
if (status != LOCA::Abstract::Iterator::Finished) { |
| 233 |
ierr = 1; |
| 234 |
if (globalData->locaUtils->isPrintType(NOX::Utils::Error)) |
| 235 |
globalData->locaUtils->out() |
| 236 |
<< "Stepper failed to converge!" << std::endl; |
| 237 |
} |
| 238 |
|
| 239 |
// Get the final solution from the stepper |
| 240 |
Teuchos::RCP<const LOCA::Epetra::Group> finalGroup = |
| 241 |
Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup()); |
| 242 |
NOX::Epetra::Vector finalSolution(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX())); |
| 243 |
|
| 244 |
// Check some statistics on the solution |
| 245 |
NOX::TestCompare testCompare(globalData->locaUtils->out(), |
| 246 |
*(globalData->locaUtils)); |
| 247 |
|
| 248 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 249 |
globalData->locaUtils->out() |
| 250 |
<< std::endl |
| 251 |
<< "***** Checking solution statistics *****" |
| 252 |
<< std::endl; |
| 253 |
|
| 254 |
// Check number of residual fills |
| 255 |
int numFills = interface->numFillsF; |
| 256 |
int numFills_expected = 20; |
| 257 |
ierr += testCompare.testValue(numFills, numFills_expected, 0.0, |
| 258 |
"number of residual fills", |
| 259 |
NOX::TestCompare::Absolute); |
| 260 |
|
| 261 |
// Re-run the problem skipping df/dp computations |
| 262 |
|
| 263 |
locaStepperList.set("Skip Parameter Derivative", true); |
| 264 |
interface->numFillsF = 0; |
| 265 |
stepper.reset(globalData, grp, combo, paramList); |
| 266 |
status = stepper.run(); |
| 267 |
if (status != LOCA::Abstract::Iterator::Finished) { |
| 268 |
ierr = 1; |
| 269 |
if (globalData->locaUtils->isPrintType(NOX::Utils::Error)) |
| 270 |
globalData->locaUtils->out() |
| 271 |
<< "Stepper failed to converge!" << std::endl; |
| 272 |
} |
| 273 |
|
| 274 |
// Get the final solution from the stepper |
| 275 |
finalGroup = |
| 276 |
Teuchos::rcp_dynamic_cast<const LOCA::Epetra::Group>(stepper.getSolutionGroup()); |
| 277 |
NOX::Epetra::Vector finalSolutionS(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup->getX())); |
| 278 |
|
| 279 |
if (globalData->locaUtils->isPrintType(NOX::Utils::TestDetails)) |
| 280 |
globalData->locaUtils->out() |
| 281 |
<< std::endl |
| 282 |
<< "***** Checking solution statistics *****" |
| 283 |
<< std::endl; |
| 284 |
|
| 285 |
// Check number of residual fills |
| 286 |
int numFillsS = interface->numFillsF; |
| 287 |
int numFillsS_expected = 9; |
| 288 |
ierr += testCompare.testValue(numFillsS, numFillsS_expected, 0.0, |
| 289 |
"number of residual fills skipping df/dp", |
| 290 |
NOX::TestCompare::Absolute); |
| 291 |
|
| 292 |
// Check final solution |
| 293 |
ierr += testCompare.testVector(finalSolutionS, finalSolution, |
| 294 |
1.0e-13, 1.0e-13, |
| 295 |
"final solution"); |
| 296 |
|
| 297 |
LOCA::destroyGlobalData(globalData); |
| 298 |
} |
| 299 |
|
| 300 |
catch (std::exception& e) { |
| 301 |
std::cout << e.what() << std::endl; |
| 302 |
ierr = 1; |
| 303 |
} |
| 304 |
catch (const char *s) { |
| 305 |
std::cout << s << std::endl; |
| 306 |
ierr = 1; |
| 307 |
} |
| 308 |
catch (...) { |
| 309 |
std::cout << "Caught unknown exception!" << std::endl; |
| 310 |
ierr = 1; |
| 311 |
} |
| 312 |
|
| 313 |
if (MyPID == 0) { |
| 314 |
if (ierr == 0) |
| 315 |
std::cout << "All tests passed!" << std::endl; |
| 316 |
else |
| 317 |
std::cout << ierr << " test(s) failed!" << std::endl; |
| 318 |
} |
| 319 |
|
| 320 |
#ifdef HAVE_MPI |
| 321 |
MPI_Finalize() ; |
| 322 |
#endif |
| 323 |
|
| 324 |
/* end main |
| 325 |
*/ |
| 326 |
return ierr ; |
| 327 |
} |