| 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 |
|
| 11 |
#include "NOX.H" |
| 12 |
|
| 13 |
// Currently, this class can only be used with builds which include EpetraExt !! |
| 14 |
#ifdef HAVE_NOX_EPETRAEXT |
| 15 |
|
| 16 |
#include "NOX_Epetra.H" |
| 17 |
// Trilinos Objects |
| 18 |
#ifdef HAVE_MPI |
| 19 |
#include "Epetra_MpiComm.h" |
| 20 |
#else |
| 21 |
#include "Epetra_SerialComm.h" |
| 22 |
#endif |
| 23 |
#include "Epetra_Map.h" |
| 24 |
#include "Epetra_Vector.h" |
| 25 |
#include "Epetra_IntVector.h" |
| 26 |
#include "Epetra_RowMatrix.h" |
| 27 |
#include "Epetra_CrsMatrix.h" |
| 28 |
#include "Epetra_Map.h" |
| 29 |
#include "Epetra_LinearProblem.h" |
| 30 |
#include "AztecOO.h" |
| 31 |
|
| 32 |
#include "OffBlock_Manager.H" |
| 33 |
#include "Problem_Manager.H" |
| 34 |
#include "GenericEpetraProblem.H" |
| 35 |
#include "Xfer_Operator.H" |
| 36 |
|
| 37 |
// Headers needed for Coloring |
| 38 |
#ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos |
| 39 |
#include "Epetra_MapColoring.h" |
| 40 |
#include "EpetraExt_MapColoring.h" |
| 41 |
#include "EpetraExt_MapColoringIndex.h" |
| 42 |
#endif |
| 43 |
|
| 44 |
// Header for Timing info |
| 45 |
#include "Epetra_Time.h" |
| 46 |
|
| 47 |
// Simple macro for turning on debug code |
| 48 |
#undef DEBUG_BLOCKGRAPH |
| 49 |
#ifdef ENABLE_DEBUG_BLOCKGRAPH |
| 50 |
#define DEBUG_BLOCKGRAPH(a) a |
| 51 |
#else |
| 52 |
#define DEBUG_BLOCKGRAPH(a) |
| 53 |
#endif |
| 54 |
|
| 55 |
int OffBlock_Manager::idToFind = -1; |
| 56 |
|
| 57 |
OffBlock_Manager::OffBlock_Manager(Problem_Manager& problemMan_, |
| 58 |
Epetra_CrsGraph& graph_, int probEqId, int probVarId) : |
| 59 |
GenericEpetraProblem(graph_.Comm(), 0), |
| 60 |
problemEqId(probEqId), |
| 61 |
problemVarId(probVarId) |
| 62 |
{ |
| 63 |
|
| 64 |
setManager(&problemMan_); |
| 65 |
|
| 66 |
// Assign a meaningful name |
| 67 |
GenericEpetraProblem &problemEq = myManager->getProblem(problemEqId), |
| 68 |
&problemVar = myManager->getProblem(problemVarId); |
| 69 |
|
| 70 |
std::string myName = "OffBlock " + problemEq.getName() + " wrt " + problemVar.getName(); |
| 71 |
setName(myName); |
| 72 |
|
| 73 |
// Set our graph (held in base class) after converting from incoming |
| 74 |
// global indices to shifted block indices |
| 75 |
AA = Teuchos::rcp( &( createBlockGraphFromComposite(graph_) ) ); |
| 76 |
|
| 77 |
// Create a problem interface to the manager |
| 78 |
offBlockInterface = Teuchos::rcp(new Problem_Interface(*this)); |
| 79 |
|
| 80 |
// Use our graph to create FDC objects needed for off-diagonal block fills |
| 81 |
createFDobjects( true ); |
| 82 |
|
| 83 |
// Reset number of dofs (overwrites base constructor assignment) |
| 84 |
NumMyNodes = 0; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
// These methods are needed to allow inheritance from GenericEpetraProblem base |
| 89 |
|
| 90 |
bool |
| 91 |
OffBlock_Manager::evaluate( NOX::Epetra::Interface::Required::FillType flag, |
| 92 |
const Epetra_Vector *solnVector, |
| 93 |
Epetra_Vector *rhsVector) |
| 94 |
{ |
| 95 |
// Determine if fill call is valid |
| 96 |
if (rhsVector == 0 || flag != NOX::Epetra::Interface::Required::FD_Res) |
| 97 |
{ |
| 98 |
std::cout << "ERROR: Either invalid RHS vector or call made from other than " |
| 99 |
<< "NOX::Epetra::FiniteDifference to OffBlock fill !!" << std::endl; |
| 100 |
throw "OffBlock_Manager ERROR"; |
| 101 |
} |
| 102 |
|
| 103 |
GenericEpetraProblem &problemEq = myManager->getProblem(problemEqId), |
| 104 |
&problemVar = myManager->getProblem(problemVarId); |
| 105 |
|
| 106 |
// Copy relevant part of incoming solnVector into solution vector of |
| 107 |
// problemVarId |
| 108 |
Epetra_Vector &probVarSoln = *(problemVar.getSolution()); |
| 109 |
probVarSoln = *solnVector; |
| 110 |
|
| 111 |
problemEq.doTransfer(); // This does all transfers for this problem |
| 112 |
|
| 113 |
myManager->setGroupX(problemEqId); |
| 114 |
myManager->computeGroupF(problemEqId); |
| 115 |
|
| 116 |
const Epetra_Vector & probEqGrpF = dynamic_cast<const NOX::Epetra::Vector&> |
| 117 |
(myManager->getGroup(problemEqId).getF()).getEpetraVector(); |
| 118 |
|
| 119 |
*rhsVector = probEqGrpF; |
| 120 |
|
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
Teuchos::RCP<NOX::Epetra::Group> |
| 125 |
OffBlock_Manager::getGroup() |
| 126 |
{ |
| 127 |
if( Teuchos::is_null(group) ) |
| 128 |
{ |
| 129 |
std::cout << "ERROR: Unable to get off-block Group for " |
| 130 |
<< "dependence of problem " << problemEqId << " on problem " |
| 131 |
<< problemVarId << " !!" << std::endl; |
| 132 |
throw "Problem_Manager ERROR"; |
| 133 |
} |
| 134 |
|
| 135 |
return( group ); |
| 136 |
} |
| 137 |
|
| 138 |
Epetra_CrsMatrix& |
| 139 |
OffBlock_Manager::getMatrix() |
| 140 |
{ |
| 141 |
if( Teuchos::is_null(matrixOperator) ) |
| 142 |
{ |
| 143 |
std::cout << "ERROR: Unable to get FDColoring underlying matrix for " |
| 144 |
<< "dependence of problem " << problemEqId << " on problem " |
| 145 |
<< problemVarId << " !!" << std::endl; |
| 146 |
throw "Problem_Manager ERROR"; |
| 147 |
} |
| 148 |
|
| 149 |
return( matrixOperator->getUnderlyingMatrix() ); |
| 150 |
} |
| 151 |
|
| 152 |
Teuchos::RCP<Epetra_Vector> |
| 153 |
OffBlock_Manager::getRowMapVec() const |
| 154 |
{ |
| 155 |
if( Teuchos::is_null(rowMapVec) ) |
| 156 |
{ |
| 157 |
std::cout << "ERROR: Unable to get Row Map Vector for OffBlock " << getName() << std::endl; |
| 158 |
throw "Problem_Manager ERROR"; |
| 159 |
} |
| 160 |
|
| 161 |
return( rowMapVec ); |
| 162 |
} |
| 163 |
|
| 164 |
int |
| 165 |
OffBlock_Manager::getProblemEqId() const |
| 166 |
{ |
| 167 |
return problemEqId; |
| 168 |
} |
| 169 |
|
| 170 |
int |
| 171 |
OffBlock_Manager::getProblemVarId() const |
| 172 |
{ |
| 173 |
return problemVarId; |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
OffBlock_Manager::convertBlockRowIndicesToComposite(int numIndices, |
| 178 |
int * blockIndices, int * compositeIndices) |
| 179 |
{ |
| 180 |
for( int i = 0 ; i < numIndices; ++i ) |
| 181 |
{ |
| 182 |
// Add a check for index validity ? RWH |
| 183 |
compositeIndices[i] = rowBlockToComposite[ blockIndices[i] ]; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void |
| 188 |
OffBlock_Manager::convertBlockColIndicesToComposite(int numIndices, |
| 189 |
int * blockIndices, int * compositeIndices) |
| 190 |
{ |
| 191 |
for( int i = 0 ; i < numIndices; ++i ) |
| 192 |
{ |
| 193 |
// Add a check for index validity, RWH |
| 194 |
compositeIndices[i] = colBlockToComposite[ blockIndices[i] ]; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
OffBlock_Manager::createFDobjects( bool useColoring ) |
| 200 |
{ |
| 201 |
graph = AA; |
| 202 |
|
| 203 |
DEBUG_BLOCKGRAPH( std::cout << "OffBlock_Manager::createFDobjects() : incoming graph --> \n" |
| 204 |
<< *graph << std::endl << "\n\tDone." << std::endl;) |
| 205 |
|
| 206 |
// Note: We use a vector corresponding to compositeSoln |
| 207 |
//Epetra_Vector & compositeVec = myManager->getCompositeSoln(); |
| 208 |
rowMapVec = Teuchos::rcp( new Epetra_Vector(graph->RowMap()) ); |
| 209 |
|
| 210 |
noxVec = Teuchos::rcp( new NOX::Epetra::Vector(*rowMapVec) ); |
| 211 |
|
| 212 |
if( !useColoring ) |
| 213 |
{ |
| 214 |
// Now setup each FD Jacobian as its own group/linearsystem |
| 215 |
matrixOperator = Teuchos::rcp( new NOX::Epetra::FiniteDifference( |
| 216 |
myManager->nlParams->sublist("Printing"), |
| 217 |
offBlockInterface, |
| 218 |
*noxVec, |
| 219 |
//rowMapVec, |
| 220 |
// compositeVec, |
| 221 |
graph) ); |
| 222 |
|
| 223 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> jacInt = matrixOperator; |
| 224 |
|
| 225 |
// Here we create a linear system solely for the sake of filling an |
| 226 |
// off-diagonal block Jacobian contribution using FDC. The nlParams and |
| 227 |
// statusTest are irrelevant and so are taken as that of the overall |
| 228 |
// Problem_Manager object. |
| 229 |
linearSystem = Teuchos::rcp( new NOX::Epetra::LinearSystemAztecOO( |
| 230 |
myManager->nlParams->sublist("Printing"), |
| 231 |
myManager->nlParams->sublist("Direction").sublist("Newton").sublist("Linear Solver"), |
| 232 |
offBlockInterface, |
| 233 |
jacInt, |
| 234 |
matrixOperator, |
| 235 |
*rowMapVec) ); |
| 236 |
// compositeVec, |
| 237 |
|
| 238 |
//noxVec = new NOX::Epetra::Vector(*rowMapVec); |
| 239 |
// NOX::Epetra::Vector tmpNOXVec(compositeVec); |
| 240 |
|
| 241 |
group = Teuchos::rcp( new NOX::Epetra::Group( |
| 242 |
myManager->nlParams->sublist("Printing"), |
| 243 |
offBlockInterface, |
| 244 |
*noxVec, |
| 245 |
linearSystem) ); |
| 246 |
|
| 247 |
DEBUG_BLOCKGRAPH( |
| 248 |
std::cout << "OffBlock_Manager::createFDobjects .... " << myName << std::endl |
| 249 |
<< "---------------------------------------------------------------" |
| 250 |
<< "graph :" << *graph << std::endl |
| 251 |
<< "---------------------------------------------------------------" |
| 252 |
<< "rowMapVec :" << *rowMapVec << std::endl |
| 253 |
<< "---------------------------------------------------------------" << std::endl;) |
| 254 |
} |
| 255 |
else // use FDC |
| 256 |
{ |
| 257 |
|
| 258 |
#ifdef HAVE_NOX_EPETRAEXT |
| 259 |
// Create a timer for performance |
| 260 |
Epetra_Time colorTime(*Comm); |
| 261 |
// GREEDY dies when doing a transpose for some reason, RWH |
| 262 |
//EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 263 |
// EpetraExt::CrsGraph_MapColoring::GREEDY; |
| 264 |
EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType = |
| 265 |
EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN; |
| 266 |
int reordering = 0; |
| 267 |
bool useParallel = false; |
| 268 |
bool distance1 = false; |
| 269 |
int verbose = 0; |
| 270 |
|
| 271 |
DEBUG_BLOCKGRAPH( std::cout << "OffBlock_Manager::createFDobjects() : incoming graph --> \n" << *graph << std::endl;) |
| 272 |
|
| 273 |
colorTime.ResetStartTime(); |
| 274 |
|
| 275 |
// Just a dummy for now, but needs to be hooked up correctly. |
| 276 |
//Epetra_Vector &compositeVec = *(myManager->getCompositeSoln()); |
| 277 |
|
| 278 |
mapColoring = Teuchos::rcp( new EpetraExt::CrsGraph_MapColoring(algType, reordering, distance1, verbose) ); |
| 279 |
colorMap = Teuchos::rcp( & ((*mapColoring)(*graph)) ); |
| 280 |
colorMapIndex = Teuchos::rcp( new EpetraExt::CrsGraph_MapColoringIndex(*colorMap) ); |
| 281 |
columnSet = Teuchos::rcp( (& (*colorMapIndex)( *graph ))); |
| 282 |
|
| 283 |
if (MyPID == 0) { |
| 284 |
printf("\n\tTime to color Jacobian # %d (%d) --> %e sec. \n", |
| 285 |
problemEqId,problemVarId,colorTime.ElapsedTime()); |
| 286 |
std::cout << "\nUsing " << colorMap->NumColors() << " colors for " |
| 287 |
<< graph->NumMyRows() << " unknowns\n" << std::endl; |
| 288 |
} |
| 289 |
|
| 290 |
// Now setup each FDC Jacobian as its own group/linearsystem |
| 291 |
matrixOperator = Teuchos::rcp( new NOX::Epetra::FiniteDifferenceColoring( |
| 292 |
myManager->nlParams->sublist("Printing"), |
| 293 |
offBlockInterface, |
| 294 |
*noxVec, |
| 295 |
graph, |
| 296 |
colorMap, |
| 297 |
columnSet, |
| 298 |
useParallel, |
| 299 |
distance1 ) ); |
| 300 |
|
| 301 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> jacInt = matrixOperator; |
| 302 |
|
| 303 |
// Here we create a linear system solely for the sake of filling an |
| 304 |
// off-diagonal block Jacobian contribution using FDC. The nlParams and |
| 305 |
// statusTest are irrelevant and so are taken as that of the overall |
| 306 |
// Problem_Manager object. |
| 307 |
linearSystem = linearSystem = Teuchos::rcp( new NOX::Epetra::LinearSystemAztecOO( |
| 308 |
myManager->nlParams->sublist("Printing"), |
| 309 |
myManager->nlParams->sublist("Direction").sublist("Newton").sublist("Linear Solver"), |
| 310 |
offBlockInterface, |
| 311 |
jacInt, |
| 312 |
matrixOperator, |
| 313 |
*rowMapVec ) ); |
| 314 |
|
| 315 |
NOX::Epetra::Vector tmpNOXVec(*rowMapVec); |
| 316 |
|
| 317 |
group = Teuchos::rcp( new NOX::Epetra::Group( |
| 318 |
myManager->nlParams->sublist("Printing"), |
| 319 |
offBlockInterface, |
| 320 |
tmpNOXVec, |
| 321 |
linearSystem) ); |
| 322 |
#else |
| 323 |
if(MyPID==0) |
| 324 |
std::cout << "ERROR: Cannot use EpetraExt with this build !!" << std::endl; |
| 325 |
exit(0); |
| 326 |
#endif |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
Epetra_CrsGraph & |
| 331 |
OffBlock_Manager::createBlockGraphFromComposite(Epetra_CrsGraph & globalGraph) |
| 332 |
{ |
| 333 |
// here we simply go through the graph and assign contiguous indices for |
| 334 |
// all unique and meaningful entries |
| 335 |
|
| 336 |
int rowCount = 0; |
| 337 |
int colCount = 0; |
| 338 |
int numCols; |
| 339 |
int * indices; |
| 340 |
for( int lRow = 0; lRow < globalGraph.NumMyRows(); ++lRow ) |
| 341 |
{ |
| 342 |
globalGraph.ExtractMyRowView( lRow, numCols, indices ); // indices are local |
| 343 |
if( numCols > 0 ) |
| 344 |
{ |
| 345 |
rowBlockToComposite[ rowCount ] = globalGraph.GRID( lRow ); |
| 346 |
rowCompositeToBlock[ globalGraph.GRID( lRow ) ] = rowCount++ ; |
| 347 |
DEBUG_BLOCKGRAPH( std::cout << "[" << lRow << "] ";) |
| 348 |
for( int col = 0; col < numCols; ++col ) |
| 349 |
{ |
| 350 |
DEBUG_BLOCKGRAPH( std::cout << globalGraph.GCID(indices[col]) << " ";) |
| 351 |
if( indices[col] >= colCount ) |
| 352 |
{ |
| 353 |
colBlockToComposite[ colCount ] = globalGraph.GCID( indices[col] ); |
| 354 |
colCompositeToBlock[ globalGraph.GCID( indices[col] ) ] = colCount++ ; |
| 355 |
|
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
DEBUG_BLOCKGRAPH( std::cout << std::endl;) |
| 360 |
} |
| 361 |
|
| 362 |
// Now create the block-sized row and column maps |
| 363 |
int numGlobalRows = -1; |
| 364 |
int numGlobalCols = -1; |
| 365 |
|
| 366 |
blockRowMap = Teuchos::rcp( new Epetra_Map ( numGlobalRows, rowBlockToComposite.size(), 0, globalGraph.Comm()) ); |
| 367 |
blockColMap = Teuchos::rcp( new Epetra_Map ( numGlobalCols, colBlockToComposite.size(), 0, globalGraph.Comm()) ); |
| 368 |
|
| 369 |
DEBUG_BLOCKGRAPH( std::cout << "\n----> Block-sized Row Map : " << *blockRowMap << std::endl;) |
| 370 |
DEBUG_BLOCKGRAPH( std::cout << "\n----> Block-sized Col Map : " << *blockColMap << std::endl;) |
| 371 |
|
| 372 |
Epetra_CrsGraph * blockGraph = new Epetra_CrsGraph( Copy, *blockRowMap, *blockColMap, 0, false); |
| 373 |
|
| 374 |
// Finally, construct a block-sized graph corresponding to a shifted globalGraph |
| 375 |
for( int lRow = 0; lRow < globalGraph.NumMyRows(); ++lRow ) |
| 376 |
{ |
| 377 |
globalGraph.ExtractMyRowView( lRow, numCols, indices ); // indices are local |
| 378 |
if( numCols > 0 ) |
| 379 |
{ |
| 380 |
int blockLocalRow = rowCompositeToBlock[ globalGraph.GRID( lRow ) ]; |
| 381 |
std::vector<int> newIndices; |
| 382 |
newIndices.resize(numCols); |
| 383 |
for( int col = 0; col < numCols; ++col ) |
| 384 |
{ |
| 385 |
newIndices[col] = colCompositeToBlock[ globalGraph.GCID( indices[col] ) ]; |
| 386 |
} |
| 387 |
|
| 388 |
blockGraph->InsertMyIndices( blockLocalRow, numCols, &(newIndices[0]) ); |
| 389 |
|
| 390 |
} |
| 391 |
DEBUG_BLOCKGRAPH( std::cout << std::endl;) |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
DEBUG_BLOCKGRAPH( std::cout << "\n----> Block-Graph : " << *blockGraph << std::endl;) |
| 396 |
|
| 397 |
blockGraph->FillComplete(); |
| 398 |
|
| 399 |
return *blockGraph; |
| 400 |
} |
| 401 |
|
| 402 |
#endif |