| 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_Epetra.H" |
| 51 |
#include "NOX_TestCompare.H" |
| 52 |
#include "NOX_Epetra_DebugTools.H" |
| 53 |
|
| 54 |
// For parsing command line |
| 55 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 56 |
|
| 57 |
// Trilinos Objects |
| 58 |
#ifdef HAVE_MPI |
| 59 |
#include "Epetra_MpiComm.h" |
| 60 |
#else |
| 61 |
#include "Epetra_SerialComm.h" |
| 62 |
#endif |
| 63 |
#include "Epetra_Map.h" |
| 64 |
#include "Epetra_Vector.h" |
| 65 |
#include "Epetra_RowMatrix.h" |
| 66 |
#include "Epetra_CrsMatrix.h" |
| 67 |
#include "Epetra_Map.h" |
| 68 |
#include "Epetra_LinearProblem.h" |
| 69 |
#include "AztecOO.h" |
| 70 |
|
| 71 |
#ifdef HAVE_NOX_ML_EPETRA |
| 72 |
#include "Teuchos_ParameterList.hpp" |
| 73 |
#endif |
| 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 |
// New coupling library headers |
| 83 |
#include "NOX_Multiphysics_Solver_Manager.H" |
| 84 |
|
| 85 |
// User's application specific files |
| 86 |
#include "Problem_Manager.H" |
| 87 |
#include "Problem_Interface.H" |
| 88 |
#include "Equation_A.H" |
| 89 |
#include "Equation_B.H" |
| 90 |
#include "Burgers.H" |
| 91 |
|
| 92 |
// Added to allow timings |
| 93 |
#include "Epetra_Time.h" |
| 94 |
|
| 95 |
using namespace std; |
| 96 |
|
| 97 |
int main(int argc, char *argv[]) |
| 98 |
{ |
| 99 |
|
| 100 |
// Initialize MPI |
| 101 |
#ifdef HAVE_MPI |
| 102 |
MPI_Init(&argc,&argv); |
| 103 |
#endif |
| 104 |
|
| 105 |
// Create a communicator for Epetra objects |
| 106 |
#ifdef HAVE_MPI |
| 107 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 108 |
#else |
| 109 |
Epetra_SerialComm Comm; |
| 110 |
#endif |
| 111 |
|
| 112 |
Teuchos::CommandLineProcessor clp( false ); |
| 113 |
|
| 114 |
// Default run-time options that can be changed from the command line |
| 115 |
bool verbose = true ; |
| 116 |
int NumGlobalNodes = 20 ; |
| 117 |
bool runMF = true ; |
| 118 |
bool useMatlab = false ; |
| 119 |
bool doOffBlocks = false ; |
| 120 |
bool convection = true ; |
| 121 |
bool libloose = true ; |
| 122 |
std::string outputDir = "." ; |
| 123 |
std::string goldDir = "." ; |
| 124 |
|
| 125 |
clp.setOption( "verbose", "no-verbose", &verbose, "Verbosity on or off." ); |
| 126 |
clp.setOption( "n", &NumGlobalNodes, "Number of elements" ); |
| 127 |
clp.setOption( "runMF", "loose", &runMF, "Use Matrix-Free strong coupling" ); |
| 128 |
clp.setOption( "offblocks", "no-offblocks", &doOffBlocks, "Include off-diagonal blocks in preconditioning matrix" ); |
| 129 |
clp.setOption( "burgers", "no-burgers", &convection, "Include Burgers equation coupling" ); |
| 130 |
clp.setOption( "matlab", "no-matlab", &useMatlab, "Use Matlab debugging engine" ); |
| 131 |
clp.setOption( "noxlib", "no-noxlib", &libloose, "Perform loose coupling using NOX's library (as opposed to hard-coded test driver)." ); |
| 132 |
clp.setOption( "outputdir", &outputDir, "Directory to output mesh and results into. Default is \"./\"" ); |
| 133 |
clp.setOption( "golddir", &goldDir, "Directory to read gold test from. Default is \"./\"" ); |
| 134 |
|
| 135 |
Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); |
| 136 |
|
| 137 |
if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) |
| 138 |
return parse_return; |
| 139 |
|
| 140 |
outputDir += "/"; |
| 141 |
goldDir += "/"; |
| 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 |
NumGlobalNodes++; // convert #elements to #nodes |
| 152 |
|
| 153 |
// The number of unknowns must be at least equal to the number of processors. |
| 154 |
if (NumGlobalNodes < NumProc) |
| 155 |
{ |
| 156 |
std::cout << "numGlobalNodes = " << NumGlobalNodes |
| 157 |
<< " cannot be < number of processors = " << NumProc << std::endl; |
| 158 |
exit(1); |
| 159 |
} |
| 160 |
|
| 161 |
// Begin Nonlinear Solver ************************************ |
| 162 |
|
| 163 |
// NOTE: For now these parameters apply to all problems handled by |
| 164 |
// Problem_Manager. Each problem could be made to have its own |
| 165 |
// parameter list as wwell as its own convergence test(s). |
| 166 |
|
| 167 |
// Create the top level parameter list |
| 168 |
Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr = |
| 169 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 170 |
Teuchos::ParameterList& nlParams = *(nlParamsPtr.get()); |
| 171 |
|
| 172 |
// Set the nonlinear solver method |
| 173 |
nlParams.set("Nonlinear Solver", "Line Search Based"); |
| 174 |
|
| 175 |
// Set the printing parameters in the "Printing" sublist |
| 176 |
Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); |
| 177 |
printParams.set("MyPID", MyPID); |
| 178 |
printParams.set("Output Precision", 3); |
| 179 |
printParams.set("Output Processor", 0); |
| 180 |
printParams.set("Output Information", |
| 181 |
NOX::Utils::Warning + |
| 182 |
NOX::Utils::OuterIteration + |
| 183 |
NOX::Utils::InnerIteration + |
| 184 |
NOX::Utils::Parameters + |
| 185 |
NOX::Utils::Details + |
| 186 |
NOX::Utils::OuterIterationStatusTest + |
| 187 |
NOX::Utils::LinearSolverDetails + |
| 188 |
NOX::Utils::TestDetails ); |
| 189 |
|
| 190 |
NOX::Utils outputUtils(printParams); |
| 191 |
|
| 192 |
// Sublist for line search |
| 193 |
Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); |
| 194 |
searchParams.set("Method", "Full Step"); |
| 195 |
|
| 196 |
// Sublist for direction |
| 197 |
Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); |
| 198 |
dirParams.set("Method", "Newton"); |
| 199 |
Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); |
| 200 |
newtonParams.set("Forcing Term Method", "Constant"); |
| 201 |
|
| 202 |
// Sublist for linear solver for the Newton method |
| 203 |
Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); |
| 204 |
lsParams.set("Aztec Solver", "GMRES"); |
| 205 |
lsParams.set("Max Iterations", 800); |
| 206 |
lsParams.set("Tolerance", 1e-4); |
| 207 |
lsParams.set("Output Frequency", 1); |
| 208 |
lsParams.set("Preconditioner", "AztecOO"); |
| 209 |
|
| 210 |
// Create the convergence tests |
| 211 |
// Note: as for the parameter list, both (all) problems use the same |
| 212 |
// convergence test(s) for now, but each could have its own. |
| 213 |
Teuchos::RCP<NOX::StatusTest::NormF> absresid = |
| 214 |
Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8)); |
| 215 |
Teuchos::RCP<NOX::StatusTest::NormUpdate> update = |
| 216 |
Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5)); |
| 217 |
Teuchos::RCP<NOX::StatusTest::Combo> converged = |
| 218 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND)); |
| 219 |
converged->addStatusTest(absresid); |
| 220 |
//converged->addStatusTest(update); |
| 221 |
Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = |
| 222 |
Teuchos::rcp(new NOX::StatusTest::MaxIters(20)); |
| 223 |
Teuchos::RCP<NOX::StatusTest::FiniteValue> finiteValue = |
| 224 |
Teuchos::rcp(new NOX::StatusTest::FiniteValue); |
| 225 |
Teuchos::RCP<NOX::StatusTest::Combo> combo = |
| 226 |
Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); |
| 227 |
combo->addStatusTest(converged); |
| 228 |
combo->addStatusTest(maxiters); |
| 229 |
combo->addStatusTest(finiteValue); |
| 230 |
|
| 231 |
// Create the Problem Manager |
| 232 |
Problem_Manager problemManager(Comm, doOffBlocks, 0, useMatlab); |
| 233 |
|
| 234 |
// Note that each problem could contain its own nlParams list as well as |
| 235 |
// its own convergence test(s). |
| 236 |
problemManager.registerParameters(nlParamsPtr); |
| 237 |
problemManager.registerStatusTest(combo); |
| 238 |
|
| 239 |
// Create each part of the Brusselator problem class. |
| 240 |
Equation_A ProblemA(Comm, NumGlobalNodes, "Temperature" ); |
| 241 |
Equation_B ProblemB(Comm, NumGlobalNodes, "Species" ); |
| 242 |
Burgers burgers (Comm, NumGlobalNodes, "Burgers" ); |
| 243 |
|
| 244 |
// An interesting note: the order of solving each problem is based on the |
| 245 |
// order of adding. For this decoupled problem, problem B is linear |
| 246 |
// with respect to its variables, whereas problem A is nonlinear wrt to its |
| 247 |
// variables. The order of solution appears to strongly affect the rate |
| 248 |
// of convergence of the decoupled Brusselator. Solving problem A first |
| 249 |
// dramatically reduces the number of total iterations. |
| 250 |
problemManager.addProblem(ProblemA); |
| 251 |
problemManager.addProblem(ProblemB); |
| 252 |
if( convection ) |
| 253 |
problemManager.addProblem(burgers); |
| 254 |
|
| 255 |
// problemManager.createDependency("Temperature", "Species"); |
| 256 |
problemManager.createDependency(ProblemA, ProblemB); |
| 257 |
problemManager.createDependency(ProblemB, ProblemA); |
| 258 |
|
| 259 |
if( convection ) |
| 260 |
{ |
| 261 |
problemManager.createDependency(ProblemA, burgers); |
| 262 |
problemManager.createDependency(ProblemB, burgers); |
| 263 |
problemManager.createDependency(burgers, ProblemA); |
| 264 |
} |
| 265 |
|
| 266 |
// Initialize time integration parameters |
| 267 |
int maxTimeSteps = 3; |
| 268 |
int timeStep = 0; |
| 269 |
double time = 0.; |
| 270 |
double dt = 0.100; |
| 271 |
problemManager.setAlldt(dt); |
| 272 |
|
| 273 |
problemManager.registerComplete(); // Trigger setup of groups, solvers, etc. |
| 274 |
|
| 275 |
problemManager.outputStatus(std::cout); |
| 276 |
|
| 277 |
// Print initial solution |
| 278 |
if( verbose ) |
| 279 |
problemManager.outputSolutions( outputDir ); |
| 280 |
|
| 281 |
// Identify the test problem |
| 282 |
if( outputUtils.isPrintType(NOX::Utils::TestDetails) ) |
| 283 |
outputUtils.out() << "Starting epetra/MultiPhysics/example_brusselator.exe" << std::endl; |
| 284 |
|
| 285 |
// Identify processor information |
| 286 |
#ifdef HAVE_MPI |
| 287 |
outputUtils.out() << "This test is broken in parallel." << std::endl; |
| 288 |
outputUtils.out() << "Test failed!" << std::endl; |
| 289 |
MPI_Finalize(); |
| 290 |
return -1; |
| 291 |
#else |
| 292 |
if (outputUtils.isPrintType(NOX::Utils::TestDetails)) |
| 293 |
outputUtils.out() << "Serial Run" << std::endl; |
| 294 |
#endif |
| 295 |
|
| 296 |
// Create a TestCompare class |
| 297 |
int status = 0; |
| 298 |
NOX::TestCompare tester( outputUtils.out(), outputUtils ); |
| 299 |
double abstol = 1.e-4; |
| 300 |
double reltol = 1.e-4 ; |
| 301 |
|
| 302 |
// Time integration loop |
| 303 |
while(timeStep < maxTimeSteps) |
| 304 |
{ |
| 305 |
timeStep++; |
| 306 |
time += dt; |
| 307 |
|
| 308 |
std::cout << "Time Step: " << timeStep << ",\tTime: " << time << std::endl; |
| 309 |
|
| 310 |
// Solve the coupled problem |
| 311 |
if( runMF ) |
| 312 |
problemManager.solveMF(); // Need a status test check here .... |
| 313 |
else if( !libloose ) |
| 314 |
problemManager.solve(); // Hard-coded loose coupling |
| 315 |
else // Loose coupling via NOX library |
| 316 |
{ |
| 317 |
// Create the loose coupling solver manager |
| 318 |
Teuchos::RCP<vector<Teuchos::RCP<NOX::Solver::Generic> > > solversVec = |
| 319 |
Teuchos::rcp( new std::vector<Teuchos::RCP<NOX::Solver::Generic> > ); |
| 320 |
|
| 321 |
map<int, Teuchos::RCP<NOX::Solver::Generic> >::iterator iter = problemManager.getSolvers().begin(), |
| 322 |
iter_end = problemManager.getSolvers().end() ; |
| 323 |
for( ; iter_end != iter; ++iter ) |
| 324 |
{ |
| 325 |
std::cout << " ........ registered Solver::Manager # " << (*iter).first << std::endl; |
| 326 |
solversVec->push_back( (*iter).second ); |
| 327 |
} |
| 328 |
|
| 329 |
// Package the Problem_Manager as the DataExchange::Intreface |
| 330 |
Teuchos::RCP<NOX::Multiphysics::DataExchange::Interface> dataExInterface = |
| 331 |
Teuchos::rcp( &problemManager, false ); |
| 332 |
|
| 333 |
NOX::Multiphysics::Solver::Manager cplSolv( solversVec, dataExInterface, combo, nlParamsPtr ); |
| 334 |
|
| 335 |
cplSolv.solve(); |
| 336 |
|
| 337 |
// Refresh all problems with solutions from solver |
| 338 |
problemManager.copyAllGroupXtoProblems(); |
| 339 |
|
| 340 |
// Reset all solver groups to force recomputation of residuals |
| 341 |
problemManager.resetAllCurrentGroupX(); |
| 342 |
} |
| 343 |
|
| 344 |
if( verbose ) |
| 345 |
problemManager.outputSolutions( outputDir, timeStep); |
| 346 |
|
| 347 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator iter = problemManager.getProblems().begin(), |
| 348 |
iter_end = problemManager.getProblems().end() ; |
| 349 |
for( ; iter_end != iter; ++iter ) |
| 350 |
{ |
| 351 |
GenericEpetraProblem & problem = *(*iter).second; |
| 352 |
std::string msg = "Numerical-to-Gold Solution comparison for problem \"" + problem.getName() + "\""; |
| 353 |
|
| 354 |
// Get the gold copy to comapre against current solution |
| 355 |
std::string baseFileame = problemManager.createIOname( problem, timeStep ); |
| 356 |
std::string goldFileame = goldDir + "gold_brusselator/" + baseFileame; |
| 357 |
|
| 358 |
Epetra_Vector * tmpVec = NULL; |
| 359 |
int ierr = NOX::Epetra::DebugTools::readVector( goldFileame, Comm, tmpVec ); |
| 360 |
if( ierr != 0 ) |
| 361 |
{ |
| 362 |
outputUtils.out() << "ERROR opening gold copy file \"" << goldFileame << "\"." << std::endl; |
| 363 |
status = ierr; |
| 364 |
break; |
| 365 |
} |
| 366 |
|
| 367 |
Teuchos::RCP<Epetra_Vector> goldSoln = Teuchos::rcp( tmpVec ); |
| 368 |
|
| 369 |
// Need NOX::Epetra::Vectors for tests |
| 370 |
NOX::Epetra::Vector numerical ( problem.getSolution() , NOX::Epetra::Vector::CreateView ); |
| 371 |
NOX::Epetra::Vector goldVec ( goldSoln , NOX::Epetra::Vector::CreateView ); |
| 372 |
|
| 373 |
status += tester.testVector( numerical, goldVec, reltol, abstol, msg ); |
| 374 |
|
| 375 |
// Quit if test fails |
| 376 |
if( status != 0 ) |
| 377 |
break; |
| 378 |
} |
| 379 |
|
| 380 |
// Quit if test fails |
| 381 |
if( status != 0 ) |
| 382 |
break; |
| 383 |
|
| 384 |
// Reset problems by copying solution into old solution |
| 385 |
problemManager.resetProblems(); |
| 386 |
|
| 387 |
} // end time step while loop |
| 388 |
|
| 389 |
// Output timing info |
| 390 |
if(MyPID==0) |
| 391 |
std::cout << "\nTimings :\n\tWallTime --> " << myTimer.WallTime() - startWallTime << " sec." |
| 392 |
<< "\n\tElapsedTime --> " << myTimer.ElapsedTime() << " sec." << std::endl << std::endl; |
| 393 |
|
| 394 |
if( 1 ) // this will be turned on later |
| 395 |
{ |
| 396 |
// Summarize test results |
| 397 |
if( status == 0 ) |
| 398 |
outputUtils.out() << "Test passed!" << std::endl; |
| 399 |
else |
| 400 |
outputUtils.out() << "Test failed!" << std::endl; |
| 401 |
} |
| 402 |
else // force this test to pass for now, but at least warn of failure |
| 403 |
{ |
| 404 |
// Summarize test results |
| 405 |
if( status == 0 ) |
| 406 |
outputUtils.out() << "Test passed!" << std::endl; |
| 407 |
else |
| 408 |
{ |
| 409 |
outputUtils.out() << "This test actually F-A-I-L-E-D." << std::endl; |
| 410 |
outputUtils.out() << "Test passed!" << std::endl; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
#ifdef HAVE_MPI |
| 416 |
MPI_Finalize() ; |
| 417 |
#endif |
| 418 |
|
| 419 |
return 0 ; |
| 420 |
} |
| 421 |
|