| 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 Brusselator Test Problem |
| 11 |
|
| 12 |
/* Solves the nonlinear equation: |
| 13 |
* |
| 14 |
* dT d2T |
| 15 |
* --- - D1 --- - alpha + (beta+1)*T - C*T**2 = 0 |
| 16 |
* dt dx2 |
| 17 |
* |
| 18 |
* T(t,0) = T(t,1) = alpha = 0.6 |
| 19 |
* T(0,x) = alpha + sinusoidal perturbation |
| 20 |
* |
| 21 |
* |
| 22 |
* dC d2C |
| 23 |
* --- - D2 --- - beta*T + C*T**2 = 0 |
| 24 |
* dt dx2 |
| 25 |
* |
| 26 |
* C(t,0) = C(t,1) = beta / alpha = 2.0 / 0.6 |
| 27 |
* C(0,x) = beta / alpha + sinusoidal perturbation |
| 28 |
* |
| 29 |
* and |
| 30 |
* D1 = D2 = 0.025 |
| 31 |
* |
| 32 |
* with d representing partial differentiation. |
| 33 |
* |
| 34 |
* This problem is examined with a variety of time integration schemes in: |
| 35 |
* "Studies on the Convergence of Various Time-Integration Schemes for the |
| 36 |
* Radiation-Diffusion Problem," Curtis C. Ober & John N. Shadid, in prep. |
| 37 |
* |
| 38 |
* In this example, only a 1st-order fully implicit (backward Euler) |
| 39 |
* time integration scheme is considered currently. |
| 40 |
* |
| 41 |
* Values for time step size and finite spatial extent are specified in |
| 42 |
* the constructor initialization list in Brusselator.C using |
| 43 |
* variables dt and xmin,xmax, respectively. |
| 44 |
* The number of time steps to be taken is specified by variable |
| 45 |
* maxTimeSteps below. |
| 46 |
*/ |
| 47 |
|
| 48 |
// NOX Objects |
| 49 |
#include "NOX.H" |
| 50 |
#include "NOX_SolverStats.hpp" |
| 51 |
#include "NOX_Epetra.H" |
| 52 |
|
| 53 |
// Trilinos Objects |
| 54 |
#ifdef HAVE_MPI |
| 55 |
#include "Epetra_MpiComm.h" |
| 56 |
#else |
| 57 |
#include "Epetra_SerialComm.h" |
| 58 |
#endif |
| 59 |
#include "Epetra_Map.h" |
| 60 |
#include "Epetra_MapColoring.h" |
| 61 |
#include "Epetra_Vector.h" |
| 62 |
#include "Epetra_IntVector.h" |
| 63 |
#include "Epetra_RowMatrix.h" |
| 64 |
#include "Epetra_CrsMatrix.h" |
| 65 |
#include "Epetra_Map.h" |
| 66 |
#include "Epetra_LinearProblem.h" |
| 67 |
#include "AztecOO.h" |
| 68 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 69 |
|
| 70 |
// Added to allow timings |
| 71 |
#include "Epetra_Time.h" |
| 72 |
|
| 73 |
// Headers needed for FD coloring |
| 74 |
#include <vector> |
| 75 |
#ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos |
| 76 |
#include "EpetraExt_MapColoring.h" |
| 77 |
#include "EpetraExt_MapColoringIndex.h" |
| 78 |
#endif |
| 79 |
|
| 80 |
// User's application specific files |
| 81 |
#include "Problem_Interface.H" // Interface file to NOX |
| 82 |
#include "Brusselator.H" |
| 83 |
|
| 84 |
using namespace std; |
| 85 |
|
| 86 |
int main(int argc, char *argv[]) |
| 87 |
{ |
| 88 |
// Initialize MPI |
| 89 |
#ifdef HAVE_MPI |
| 90 |
MPI_Init(&argc,&argv); |
| 91 |
#endif |
| 92 |
|
| 93 |
// Create a communicator for Epetra objects |
| 94 |
#ifdef HAVE_MPI |
| 95 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 96 |
#else |
| 97 |
Epetra_SerialComm Comm; |
| 98 |
#endif |
| 99 |
|
| 100 |
bool verbose = true; |
| 101 |
bool success = false; |
| 102 |
try { |
| 103 |
// Create and reset the Timer |
| 104 |
Epetra_Time myTimer(Comm); |
| 105 |
double startWallTime = myTimer.WallTime(); |
| 106 |
|
| 107 |
// Get the process ID and the total number of processors |
| 108 |
int MyPID = Comm.MyPID(); |
| 109 |
int NumProc = Comm.NumProc(); |
| 110 |
|
| 111 |
// Check for verbose option |
| 112 |
if (argc > 1) |
| 113 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 114 |
verbose = true; |
| 115 |
// Set the problem size (1000 elements) |
| 116 |
int NumGlobalNodes = 1001; |
| 117 |
|
| 118 |
// The number of unknowns must be at least equal to the |
| 119 |
// number of processors. |
| 120 |
if (NumGlobalNodes < NumProc) { |
| 121 |
std::cout << "numGlobalNodes = " << NumGlobalNodes |
| 122 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 123 |
std::cout << "Test failed!" << std::endl; |
| 124 |
exit(1); |
| 125 |
} |
| 126 |
|
| 127 |
// Create the Brusselator problem class. This creates all required |
| 128 |
// Epetra objects for the problem and allows calls to the |
| 129 |
// function (F) and Jacobian evaluation routines. |
| 130 |
Brusselator::OverlapType OType = Brusselator::NODES; |
| 131 |
Teuchos::RCP<Brusselator> Problem = |
| 132 |
Teuchos::rcp(new Brusselator(NumGlobalNodes, Comm, OType)); |
| 133 |
|
| 134 |
// Get the vector from the Problem |
| 135 |
Teuchos::RCP<Epetra_Vector> soln = Problem->getSolution(); |
| 136 |
NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView); |
| 137 |
|
| 138 |
// Begin Nonlinear Solver ************************************ |
| 139 |
|
| 140 |
// Create the top level parameter list |
| 141 |
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr = |
| 142 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 143 |
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get()); |
| 144 |
|
| 145 |
// Set the nonlinear solver method |
| 146 |
nlParams.set("Nonlinear Solver", "Line Search Based"); |
| 147 |
//nlParams.set("Nonlinear Solver", "Trust Region Based"); |
| 148 |
|
| 149 |
// Set the printing parameters in the "Printing" sublist |
| 150 |
Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); |
| 151 |
if (verbose) { |
| 152 |
printParams.set("MyPID", MyPID); |
| 153 |
printParams.set("Output Precision", 3); |
| 154 |
printParams.set("Output Processor", 0); |
| 155 |
printParams.set("Output Information", |
| 156 |
NOX::Utils::OuterIteration + |
| 157 |
NOX::Utils::OuterIterationStatusTest + |
| 158 |
NOX::Utils::InnerIteration + |
| 159 |
NOX::Utils::Parameters + |
| 160 |
NOX::Utils::Details + |
| 161 |
NOX::Utils::Warning); |
| 162 |
} |
| 163 |
else |
| 164 |
printParams.set("Output Information", NOX::Utils::Error); |
| 165 |
|
| 166 |
// Create a print handle object |
| 167 |
NOX::Utils utils(printParams); |
| 168 |
|
| 169 |
// Sublist for line search |
| 170 |
Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); |
| 171 |
searchParams.set("Method", "Full Step"); |
| 172 |
|
| 173 |
// Sublist for direction |
| 174 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 175 |
dirParams.set("Method", "Newton"); |
| 176 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 177 |
newtonParams.set("Forcing Term Method", "Constant"); |
| 178 |
|
| 179 |
// Sublist for linear solver for the Newton method |
| 180 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 181 |
lsParams.set("Aztec Solver", "GMRES"); |
| 182 |
lsParams.set("Max Iterations", 800); |
| 183 |
lsParams.set("Tolerance", 1e-4); |
| 184 |
lsParams.set("Output Frequency", 0); |
| 185 |
lsParams.set("Preconditioner", "AztecOO"); |
| 186 |
|
| 187 |
// Create the interface between the test problem and the nonlinear solver |
| 188 |
Teuchos::RCP<Problem_Interface> interface = |
| 189 |
Teuchos::rcp(new Problem_Interface(*Problem)); |
| 190 |
|
| 191 |
#ifndef HAVE_NOX_EPETRAEXT |
| 192 |
utils.out() << "Cannot use Coloring without package epetraext !!!!" << std::endl; |
| 193 |
if (verbose) { |
| 194 |
if(MyPID==0) |
| 195 |
utils.out() << "\nTimings :\n\tWallTime --> " << |
| 196 |
myTimer.WallTime() - startWallTime << " sec." |
| 197 |
<< "\n\tElapsedTime --> " << myTimer.ElapsedTime() |
| 198 |
<< " sec." << std::endl << std::endl; |
| 199 |
} |
| 200 |
utils.out() << "Test passed!" << std::endl; |
| 201 |
success = true; |
| 202 |
exit( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 203 |
} |
| 204 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 205 |
#else |
| 206 |
|
| 207 |
// Create a timer for performance |
| 208 |
Epetra_Time fillTime(Comm); |
| 209 |
|
| 210 |
EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 211 |
EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN; |
| 212 |
bool colorParallel = true; |
| 213 |
int reordering = 0; |
| 214 |
int verbosityLevel = 0; |
| 215 |
bool distance1 = false; |
| 216 |
EpetraExt::CrsGraph_MapColoring tmpMapColoring( |
| 217 |
algType, reordering, distance1, verbosityLevel); |
| 218 |
Teuchos::RCP<Epetra_MapColoring> colorMap = |
| 219 |
Teuchos::rcp(&tmpMapColoring(*(Problem->getGraph()))); |
| 220 |
EpetraExt::CrsGraph_MapColoringIndex colorMapIndex(*colorMap); |
| 221 |
Teuchos::RCP< std::vector<Epetra_IntVector> > columns = |
| 222 |
Teuchos::rcp(&colorMapIndex(*(Problem->getGraph()))); |
| 223 |
|
| 224 |
Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface; |
| 225 |
Teuchos::RCP<NOX::Epetra::FiniteDifferenceColoring> FDC = |
| 226 |
Teuchos::rcp(new NOX::Epetra::FiniteDifferenceColoring(printParams, |
| 227 |
iReq, |
| 228 |
noxSoln, |
| 229 |
Problem->getGraph(), |
| 230 |
colorMap, |
| 231 |
columns, |
| 232 |
colorParallel, |
| 233 |
distance1)); |
| 234 |
|
| 235 |
if (verbose) |
| 236 |
printf("\n[%d]\tTime to color Jacobian --> %e sec. for %d colors.\n\n", |
| 237 |
MyPID,fillTime.ElapsedTime(), colorMap->NumColors()); |
| 238 |
|
| 239 |
FDC->setDifferenceMethod(NOX::Epetra::FiniteDifference::Centered); |
| 240 |
|
| 241 |
// -------------- End of block needed to use coloring --------------- */ |
| 242 |
|
| 243 |
|
| 244 |
// Create the Linear System |
| 245 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = FDC; |
| 246 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = |
| 247 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, |
| 248 |
iReq, iJac, FDC, |
| 249 |
noxSoln)); |
| 250 |
|
| 251 |
// Create the Group |
| 252 |
Teuchos::RCP<NOX::Epetra::Group> grpPtr = |
| 253 |
Teuchos::rcp(new NOX::Epetra::Group(printParams, |
| 254 |
iReq, |
| 255 |
noxSoln, |
| 256 |
linSys)); |
| 257 |
NOX::Epetra::Group& grp = *(grpPtr.get()); |
| 258 |
|
| 259 |
// Create the convergence tests |
| 260 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 261 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8, NOX::StatusTest::NormF::Unscaled)); |
| 262 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 263 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(25)); |
| 264 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 265 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 266 |
combo->addStatusTest(absresid); |
| 267 |
combo->addStatusTest(maxiters); |
| 268 |
|
| 269 |
// Create the method |
| 270 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 271 |
NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr); |
| 272 |
|
| 273 |
// Initialize time integration parameters |
| 274 |
int maxTimeSteps = 3; |
| 275 |
int timeStep = 0; |
| 276 |
double time = 0.; |
| 277 |
double dt = Problem->getdt(); |
| 278 |
|
| 279 |
// Print initial solution |
| 280 |
char file_name[25]; |
| 281 |
FILE *ifp; |
| 282 |
Epetra_Vector& xMesh = *(Problem->getMesh()); |
| 283 |
int NumMyNodes = xMesh.Map().NumMyElements(); |
| 284 |
(void) sprintf(file_name, "output.%d_%d",MyPID,timeStep); |
| 285 |
ifp = fopen(file_name, "w"); |
| 286 |
for (int i=0; i<NumMyNodes; i++) |
| 287 |
fprintf(ifp, "%d %E %E %E\n", xMesh.Map().MinMyGID()+i, |
| 288 |
xMesh[i], (*soln)[2*i], (*soln)[2*i+1]); |
| 289 |
fclose(ifp); |
| 290 |
|
| 291 |
NOX::StatusTest::StatusType status = NOX::StatusTest::Unconverged; |
| 292 |
|
| 293 |
// Time integration loop |
| 294 |
while(timeStep < maxTimeSteps) { |
| 295 |
|
| 296 |
if (timeStep > 0) { |
| 297 |
solver->reset(grp.getX(), combo); |
| 298 |
grp.computeF(); |
| 299 |
} |
| 300 |
|
| 301 |
timeStep++; |
| 302 |
time += dt; |
| 303 |
|
| 304 |
if (verbose) |
| 305 |
utils.out() << "Time Step: " << timeStep << ",\tTime: " << time << std::endl; |
| 306 |
|
| 307 |
status = NOX::StatusTest::Unconverged; |
| 308 |
status = solver->solve(); |
| 309 |
|
| 310 |
if (verbose) |
| 311 |
if (status != NOX::StatusTest::Converged) |
| 312 |
if (MyPID==0) |
| 313 |
utils.out() << "Nonlinear solver failed to converge!" << std::endl; |
| 314 |
|
| 315 |
// Get the Epetra_Vector with the final solution from the solver |
| 316 |
const NOX::Epetra::Group& finalGroup = |
| 317 |
dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup()); |
| 318 |
const Epetra_Vector& finalSolution = |
| 319 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector(); |
| 320 |
|
| 321 |
// End Nonlinear Solver ************************************** |
| 322 |
|
| 323 |
// Print solution |
| 324 |
(void) sprintf(file_name, "output.%03d_%05d",MyPID,timeStep); |
| 325 |
ifp = fopen(file_name, "w"); |
| 326 |
for (int i=0; i<NumMyNodes; i++) |
| 327 |
fprintf(ifp, "%d %E %E %E\n", soln->Map().MinMyGID()+i, |
| 328 |
xMesh[i], finalSolution[2*i], |
| 329 |
finalSolution[2*i+1]); |
| 330 |
fclose(ifp); |
| 331 |
|
| 332 |
Problem->reset(finalSolution); |
| 333 |
grp.setX(finalSolution); |
| 334 |
|
| 335 |
} // end time step while loop |
| 336 |
|
| 337 |
// Output the parameter list |
| 338 |
if (verbose) { |
| 339 |
if (utils.isPrintType(NOX::Utils::Parameters)) { |
| 340 |
utils.out() << std::endl << "Final Parameters" << std::endl |
| 341 |
<< "****************" << std::endl; |
| 342 |
solver->getList().print(utils.out()); |
| 343 |
utils.out() << std::endl; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
// Output timing info |
| 348 |
if (verbose) { |
| 349 |
if(MyPID==0) |
| 350 |
utils.out() << "\nTimings :\n\tWallTime --> " << |
| 351 |
myTimer.WallTime() - startWallTime << " sec." |
| 352 |
<< "\n\tElapsedTime --> " << myTimer.ElapsedTime() |
| 353 |
<< " sec." << std::endl << std::endl; |
| 354 |
} |
| 355 |
|
| 356 |
// Report results |
| 357 |
|
| 358 |
int testStatus = 0; // Converged |
| 359 |
|
| 360 |
// 1. Convergence |
| 361 |
if (status != NOX::StatusTest::Converged) { |
| 362 |
if (MyPID==0) utils.out() << "Nonlinear solver failed to converge!" << std::endl; |
| 363 |
testStatus = 1; |
| 364 |
} |
| 365 |
// 2. Nonlinear Iterations (3) |
| 366 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Output").get("Nonlinear Iterations",0) != 3) { |
| 367 |
testStatus = 2; |
| 368 |
} |
| 369 |
if (solver->getSolverStatistics()->numNonlinearIterations != 3) { |
| 370 |
testStatus = 2; |
| 371 |
} |
| 372 |
if (NumProc == 1) { |
| 373 |
// 3. Linear Iterations (9) |
| 374 |
if (const_cast<Teuchos::ParameterList&>(solver->getList()).sublist("Direction").sublist("Newton").sublist("Linear Solver").sublist("Output").get("Total Number of Linear Iterations",0) != 9) { |
| 375 |
testStatus = 3; |
| 376 |
} |
| 377 |
if (solver->getSolverStatistics()->linearSolve.allNonlinearSolves_NumLinearIterations != 9) { |
| 378 |
testStatus = 3; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
utils.out() << "testStatus = " << testStatus << std::endl; |
| 383 |
|
| 384 |
success = (testStatus == 0); |
| 385 |
// Summarize test results |
| 386 |
if (success) |
| 387 |
utils.out() << "Test passed!" << std::endl; |
| 388 |
else |
| 389 |
utils.out() << "Test failed!" << std::endl; |
| 390 |
} |
| 391 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 392 |
|
| 393 |
#ifdef HAVE_MPI |
| 394 |
MPI_Finalize(); |
| 395 |
#endif |
| 396 |
|
| 397 |
// Final return value (0 = successfull, non-zero = failure) |
| 398 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 399 |
#endif // HAVE_NOX_EPETRAEXT |
| 400 |
} |