| 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 PelletTransport 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 PelletTransport.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_Epetra.H" |
| 51 |
#include "../test/utils/NOX_Epetra_DebugTools.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 |
|
| 69 |
// For parsing command line |
| 70 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 71 |
|
| 72 |
// Added to allow timings |
| 73 |
#include "Epetra_Time.h" |
| 74 |
|
| 75 |
// Headers needed for FD coloring |
| 76 |
#include <vector> |
| 77 |
#ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos |
| 78 |
#include "EpetraExt_MapColoring.h" |
| 79 |
#include "EpetraExt_MapColoringIndex.h" |
| 80 |
#endif |
| 81 |
|
| 82 |
// User's application specific files |
| 83 |
#include "MaterialProps.H" // Interface file to NOX |
| 84 |
#include "Problem_Interface.H" // Interface file to NOX |
| 85 |
#include "PelletTransport.H" |
| 86 |
|
| 87 |
using namespace std; |
| 88 |
|
| 89 |
int main(int argc, char *argv[]) |
| 90 |
{ |
| 91 |
std::cout << log(10.0) << std::endl; |
| 92 |
|
| 93 |
// Initialize MPI |
| 94 |
#ifdef HAVE_MPI |
| 95 |
MPI_Init(&argc,&argv); |
| 96 |
#endif |
| 97 |
|
| 98 |
// Create a communicator for Epetra objects |
| 99 |
#ifdef HAVE_MPI |
| 100 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 101 |
#else |
| 102 |
Epetra_SerialComm Comm; |
| 103 |
#endif |
| 104 |
|
| 105 |
Teuchos::CommandLineProcessor clp( false ); |
| 106 |
|
| 107 |
// Default run-time options that can be changed from the command line |
| 108 |
bool verbose = true ; |
| 109 |
bool restart = false ; |
| 110 |
int NumGlobalElementsUO2 = 2001 ; |
| 111 |
int NumGlobalElementsHe = 2001 ; |
| 112 |
int NumGlobalElementsClad = 2001 ; |
| 113 |
// Physical parameters |
| 114 |
double qdot = 2.0e8 ; |
| 115 |
double xB = 0.02 ; |
| 116 |
double xminUO2 = 0.0 ; |
| 117 |
double xmaxUO2 = 0.0043 ; |
| 118 |
double xminHe = 0.0043 ; |
| 119 |
double xmaxHe = 0.00433 ; |
| 120 |
double xminClad = 0.00433 ; |
| 121 |
double xmaxClad = 0.00483 ; |
| 122 |
|
| 123 |
|
| 124 |
clp.setOption( "verbose", "no-verbose", &verbose, "Verbosity on or off." ); |
| 125 |
clp.setOption( "restart", "no-restart", &restart, "Restart from previous solution." ); |
| 126 |
clp.setOption( "nUO2", &NumGlobalElementsUO2, "Number of elements in UO2 domain" ); |
| 127 |
clp.setOption( "nHe", &NumGlobalElementsHe, "Number of elements in He domain" ); |
| 128 |
clp.setOption( "nClad", &NumGlobalElementsClad, "Number of elements in Cladding domain" ); |
| 129 |
clp.setOption( "qdot", &qdot, "Source term coefficient, Qdot" ); |
| 130 |
clp.setOption( "xB", &xB, "Non-stoichiometry at outer surface of fuel rod, xB" ); |
| 131 |
clp.setOption( "xminUO2", &xminUO2, "Left boundary location of UO2 domain (m)" ); |
| 132 |
clp.setOption( "xmaxUO2", &xmaxUO2, "Right boundary location of UO2 domain (m)" ); |
| 133 |
clp.setOption( "xminHe", &xminHe, "Left boundary location of He domain (m)" ); |
| 134 |
clp.setOption( "xmaxHe", &xmaxHe, "Right boundary location of He domain (m)" ); |
| 135 |
clp.setOption( "xminClad", &xminClad, "Left boundary location of Cladding domain (m)" ); |
| 136 |
clp.setOption( "xmaxClad", &xmaxClad, "Right boundary location of Cladding domain (m)" ); |
| 137 |
|
| 138 |
Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); |
| 139 |
|
| 140 |
if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) |
| 141 |
return parse_return; |
| 142 |
|
| 143 |
// Create and reset the Timer |
| 144 |
Epetra_Time myTimer(Comm); |
| 145 |
double startWallTime = myTimer.WallTime(); |
| 146 |
|
| 147 |
// Get the process ID and the total number of processors |
| 148 |
int MyPID = Comm.MyPID(); |
| 149 |
int NumProc = Comm.NumProc(); |
| 150 |
|
| 151 |
// The number of unknowns must be at least equal to the |
| 152 |
// number of processors. |
| 153 |
//if (NumGlobalNodes < NumProc) |
| 154 |
//{ |
| 155 |
// std::cout << "numGlobalNodes = " << NumGlobalNodes << " cannot be < number of processors = " << NumProc << std::endl; |
| 156 |
// std::cout << "Test failed!" << std::endl; |
| 157 |
// exit(1); |
| 158 |
//} |
| 159 |
|
| 160 |
// Create the PelletTransport problem class. This creates all required |
| 161 |
// Epetra objects for the problem and allows calls to the |
| 162 |
// function (F) and Jacobian evaluation routines. |
| 163 |
Teuchos::RCP<PelletTransport> Problem = |
| 164 |
Teuchos::rcp(new PelletTransport( |
| 165 |
NumGlobalElementsUO2 , xminUO2 , xmaxUO2 , |
| 166 |
NumGlobalElementsHe , xminHe , xmaxHe , |
| 167 |
NumGlobalElementsClad , xminClad , xmaxClad , |
| 168 |
Comm, restart) ); |
| 169 |
|
| 170 |
// Get the vector from the Problem |
| 171 |
Teuchos::RCP<Epetra_Vector> soln = Problem->getSolution(); |
| 172 |
NOX::Epetra::Vector noxSoln(soln, NOX::Epetra::Vector::CreateView); |
| 173 |
|
| 174 |
// Plug in the needed material models |
| 175 |
MaterialPropBase * propEnity; |
| 176 |
propEnity = MaterialPropFactory::factory().add_model( new MaterialProp_He() ); |
| 177 |
propEnity = MaterialPropFactory::factory().add_model( new MaterialProp_Clad() ); |
| 178 |
propEnity = MaterialPropFactory::factory().add_model( new MaterialProp_UO2() ); |
| 179 |
dynamic_cast<MaterialProp_UO2 *>(propEnity)->set_qdot( qdot ); |
| 180 |
dynamic_cast<MaterialProp_UO2 *>(propEnity)->set_xB( xB ); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
// Begin Nonlinear Solver ************************************ |
| 185 |
|
| 186 |
// Create the top level parameter list |
| 187 |
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr = |
| 188 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 189 |
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get()); |
| 190 |
|
| 191 |
// Set the nonlinear solver method |
| 192 |
nlParams.set("Nonlinear Solver", "Line Search Based"); |
| 193 |
//nlParams.set("Nonlinear Solver", "Trust Region Based"); |
| 194 |
|
| 195 |
// Set the printing parameters in the "Printing" sublist |
| 196 |
Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); |
| 197 |
if (verbose) { |
| 198 |
printParams.set("MyPID", MyPID); |
| 199 |
printParams.set("Output Precision", 3); |
| 200 |
printParams.set("Output Processor", 0); |
| 201 |
printParams.set("Output Information", |
| 202 |
NOX::Utils::OuterIteration + |
| 203 |
NOX::Utils::OuterIterationStatusTest + |
| 204 |
NOX::Utils::InnerIteration + |
| 205 |
NOX::Utils::LinearSolverDetails + |
| 206 |
NOX::Utils::Parameters + |
| 207 |
NOX::Utils::Details + |
| 208 |
NOX::Utils::Warning); |
| 209 |
} |
| 210 |
else |
| 211 |
printParams.set("Output Information", NOX::Utils::Error); |
| 212 |
|
| 213 |
// Create a print handle object |
| 214 |
NOX::Utils utils(printParams); |
| 215 |
|
| 216 |
// Sublist for line search |
| 217 |
Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); |
| 218 |
searchParams.set("Method", "Full Step"); |
| 219 |
|
| 220 |
// Sublist for direction |
| 221 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 222 |
dirParams.set("Method", "Newton"); |
| 223 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 224 |
newtonParams.set("Forcing Term Method", "Constant"); |
| 225 |
|
| 226 |
// Sublist for linear solver for the Newton method |
| 227 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 228 |
lsParams.set("Aztec Solver", "GMRES"); |
| 229 |
lsParams.set("Max Iterations", 800); |
| 230 |
lsParams.set("Tolerance", 1e-4); |
| 231 |
lsParams.set("Output Frequency", 100); |
| 232 |
lsParams.set("Preconditioner", "AztecOO"); |
| 233 |
|
| 234 |
// Create the interface between the test problem and the nonlinear solver |
| 235 |
Teuchos::RCP<Problem_Interface> interface = |
| 236 |
Teuchos::rcp(new Problem_Interface(*Problem)); |
| 237 |
|
| 238 |
#ifndef HAVE_NOX_EPETRAEXT |
| 239 |
utils.out() << "Cannot use Coloring without package epetraext !!!!" << std::endl; |
| 240 |
utils.out() << "Test passed!" << std::endl; |
| 241 |
exit(0); |
| 242 |
#else |
| 243 |
// Create a timer for performance |
| 244 |
Epetra_Time fillTime(Comm); |
| 245 |
|
| 246 |
EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 247 |
EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN; |
| 248 |
bool colorParallel = true; |
| 249 |
int reordering = 0; |
| 250 |
int verbosityLevel = 0; |
| 251 |
bool distance1 = false; |
| 252 |
EpetraExt::CrsGraph_MapColoring tmpMapColoring( |
| 253 |
algType, reordering, distance1, verbosityLevel); |
| 254 |
Teuchos::RCP<Epetra_MapColoring> colorMap = |
| 255 |
Teuchos::rcp(&tmpMapColoring(*(Problem->getGraph()))); |
| 256 |
EpetraExt::CrsGraph_MapColoringIndex colorMapIndex(*colorMap); |
| 257 |
Teuchos::RCP< std::vector<Epetra_IntVector> > columns = |
| 258 |
Teuchos::rcp(&colorMapIndex(*(Problem->getGraph()))); |
| 259 |
|
| 260 |
Teuchos::RCP<NOX::Epetra::Interface::Required> iReq = interface; |
| 261 |
Teuchos::RCP<NOX::Epetra::FiniteDifferenceColoring> FDC = |
| 262 |
Teuchos::rcp(new NOX::Epetra::FiniteDifferenceColoring(printParams, |
| 263 |
iReq, |
| 264 |
noxSoln, |
| 265 |
Problem->getGraph(), |
| 266 |
colorMap, |
| 267 |
columns, |
| 268 |
colorParallel, |
| 269 |
distance1)); |
| 270 |
|
| 271 |
if (verbose) |
| 272 |
printf("\n[%d]\tTime to color Jacobian --> %e sec. for %d colors.\n\n", |
| 273 |
MyPID,fillTime.ElapsedTime(), colorMap->NumColors()); |
| 274 |
|
| 275 |
FDC->setDifferenceMethod(NOX::Epetra::FiniteDifference::Centered); |
| 276 |
|
| 277 |
// -------------- End of block needed to use coloring --------------- */ |
| 278 |
|
| 279 |
|
| 280 |
// Create the Linear System |
| 281 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = FDC; |
| 282 |
Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = |
| 283 |
Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, |
| 284 |
iReq, iJac, FDC, |
| 285 |
noxSoln)); |
| 286 |
|
| 287 |
// Create the Group |
| 288 |
Teuchos::RCP<NOX::Epetra::Group> grpPtr = |
| 289 |
Teuchos::rcp(new NOX::Epetra::Group(printParams, |
| 290 |
iReq, |
| 291 |
noxSoln, |
| 292 |
linSys)); |
| 293 |
NOX::Epetra::Group& grp = *(grpPtr.get()); |
| 294 |
|
| 295 |
// Create the convergence tests |
| 296 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 297 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-5, NOX::StatusTest::NormF::Unscaled)); |
| 298 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 299 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(25)); |
| 300 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> finiteval = |
| 301 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue()); |
| 302 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 303 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 304 |
combo->addStatusTest(absresid); |
| 305 |
combo->addStatusTest(maxiters); |
| 306 |
combo->addStatusTest(finiteval); |
| 307 |
|
| 308 |
// Create the method |
| 309 |
Teuchos::RCP<NOX::Solver::Generic> solver = |
| 310 |
NOX::Solver::buildSolver(grpPtr, combo, nlParamsPtr); |
| 311 |
|
| 312 |
// Initialize time integration parameters |
| 313 |
int maxTimeSteps = 1; |
| 314 |
int timeStep = 0; |
| 315 |
double time = 100.; |
| 316 |
double dt = Problem->getdt(); |
| 317 |
|
| 318 |
// Print initial solution |
| 319 |
char file_name[25]; |
| 320 |
FILE *ifp; |
| 321 |
Epetra_Vector& xMesh = *(Problem->getMesh()); |
| 322 |
int NumMyNodes = xMesh.Map().NumMyElements(); |
| 323 |
(void) sprintf(file_name, "output.%d_%d",MyPID,timeStep); |
| 324 |
ifp = fopen(file_name, "w"); |
| 325 |
for( int i = 0; i < NumMyNodes; ++i ) |
| 326 |
fprintf(ifp, "%d %E %E %E\n", xMesh.Map().MinMyGID()+i, |
| 327 |
xMesh[i], (*soln)[2*i], (*soln)[2*i+1]); |
| 328 |
fclose(ifp); |
| 329 |
|
| 330 |
NOX::StatusTest::StatusType status = NOX::StatusTest::Unconverged; |
| 331 |
|
| 332 |
// Time integration loop |
| 333 |
while(timeStep < maxTimeSteps) { |
| 334 |
|
| 335 |
timeStep++; |
| 336 |
time += dt; |
| 337 |
|
| 338 |
if (verbose) |
| 339 |
utils.out() << "Time Step: " << timeStep << ",\tTime: " << time << std::endl; |
| 340 |
|
| 341 |
status = NOX::StatusTest::Unconverged; |
| 342 |
status = solver->solve(); |
| 343 |
|
| 344 |
if (verbose) |
| 345 |
if (status != NOX::StatusTest::Converged) |
| 346 |
if (MyPID==0) |
| 347 |
utils.out() << "Nonlinear solver failed to converge!" << std::endl; |
| 348 |
|
| 349 |
// Get the Epetra_Vector with the final solution from the solver |
| 350 |
const NOX::Epetra::Group& finalGroup = |
| 351 |
dynamic_cast<const NOX::Epetra::Group&>(solver->getSolutionGroup()); |
| 352 |
const Epetra_Vector& finalSolution = |
| 353 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getX())).getEpetraVector(); |
| 354 |
|
| 355 |
// Get the Epetra_Vector with the last residual from the solver |
| 356 |
const Epetra_Vector& finalResidual = |
| 357 |
(dynamic_cast<const NOX::Epetra::Vector&>(finalGroup.getF())).getEpetraVector(); |
| 358 |
|
| 359 |
// End Nonlinear Solver ************************************** |
| 360 |
|
| 361 |
if( 1 ) |
| 362 |
{ |
| 363 |
// Print solution |
| 364 |
(void) sprintf(file_name, "T_output.%03d_%05d",MyPID,timeStep); |
| 365 |
ifp = fopen(file_name, "w"); |
| 366 |
for( int i = 0; i < NumMyNodes; ++i ) |
| 367 |
fprintf(ifp, "%d %E %E\n", soln->Map().MinMyGID()+i, |
| 368 |
xMesh[i], finalSolution[2*i]); |
| 369 |
|
| 370 |
fclose(ifp); |
| 371 |
|
| 372 |
(void) sprintf(file_name, "x_output.%03d_%05d",MyPID,timeStep); |
| 373 |
ifp = fopen(file_name, "w"); |
| 374 |
for( int i = 0; i < NumGlobalElementsUO2 + 1; ++i ) |
| 375 |
fprintf(ifp, "%d %E %E\n", soln->Map().MinMyGID()+i, |
| 376 |
xMesh[i], finalSolution[2*i+1]); |
| 377 |
fclose(ifp); |
| 378 |
|
| 379 |
// Print residual |
| 380 |
(void) sprintf(file_name, "residual.%03d_%05d",MyPID,timeStep); |
| 381 |
ifp = fopen(file_name, "w"); |
| 382 |
for( int i = 0; i < NumMyNodes; ++i ) |
| 383 |
fprintf(ifp, "%d %E %E %E\n", soln->Map().MinMyGID()+i, |
| 384 |
xMesh[i], finalResidual[2*i], |
| 385 |
finalResidual[2*i+1]); |
| 386 |
fclose(ifp); |
| 387 |
|
| 388 |
NOX::Epetra::DebugTools::writeVector( "restartVec", finalSolution, NOX::Epetra::DebugTools::MATRIX_MARKET ); |
| 389 |
} |
| 390 |
|
| 391 |
utils.out() << "Time Step: " << timeStep << ",\tTime: " << time << ", Tmax = " << finalSolution[0] << ", Xmax = " << finalSolution[1] << std::endl; |
| 392 |
|
| 393 |
Problem->reset(finalSolution); |
| 394 |
grp.setX(finalSolution); |
| 395 |
solver->reset(grp.getX(), combo); |
| 396 |
grp.computeF(); |
| 397 |
|
| 398 |
} // end time step while loop |
| 399 |
|
| 400 |
const Epetra_Vector& finalSolution = (dynamic_cast<const NOX::Epetra::Vector&>(grp.getX())).getEpetraVector(); |
| 401 |
NOX::Epetra::DebugTools::writeVector( "transient_restartVec", finalSolution, NOX::Epetra::DebugTools::MATRIX_MARKET ); |
| 402 |
|
| 403 |
// Output the parameter list |
| 404 |
if (verbose) { |
| 405 |
if (utils.isPrintType(NOX::Utils::Parameters)) { |
| 406 |
utils.out() << std::endl << "Final Parameters" << std::endl |
| 407 |
<< "****************" << std::endl; |
| 408 |
solver->getList().print(utils.out()); |
| 409 |
utils.out() << std::endl; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
// Output timing info |
| 414 |
if (verbose) |
| 415 |
{ |
| 416 |
if(MyPID==0) |
| 417 |
utils.out() << "\nTimings :\n\tWallTime --> " << myTimer.WallTime() - startWallTime << " sec." |
| 418 |
<< "\n\tElapsedTime --> " << myTimer.ElapsedTime() |
| 419 |
<< " sec." << std::endl << std::endl; |
| 420 |
|
| 421 |
} |
| 422 |
|
| 423 |
#ifdef HAVE_MPI |
| 424 |
MPI_Finalize(); |
| 425 |
#endif |
| 426 |
|
| 427 |
// Final return value (0 = successfull, non-zero = failure) |
| 428 |
return 0; |
| 429 |
#endif // HAVE_NOX_EPETRAEXT |
| 430 |
} |