| 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 |
#include "NOX.H" |
| 11 |
#include "NOX_Epetra.H" |
| 12 |
// Trilinos Objects |
| 13 |
#ifdef HAVE_MPI |
| 14 |
#include "Epetra_MpiComm.h" |
| 15 |
#else |
| 16 |
#include "Epetra_SerialComm.h" |
| 17 |
#endif |
| 18 |
#include "Epetra_Map.h" |
| 19 |
#include "Epetra_Vector.h" |
| 20 |
#include "Epetra_RowMatrix.h" |
| 21 |
#include "Epetra_CrsMatrix.h" |
| 22 |
#include "Epetra_Map.h" |
| 23 |
#include "Epetra_LinearProblem.h" |
| 24 |
#include "AztecOO.h" |
| 25 |
|
| 26 |
#include "Problem_Manager.H" |
| 27 |
#include "GenericEpetraProblem.H" |
| 28 |
#include "Xfer_Operator.H" |
| 29 |
|
| 30 |
GenericEpetraProblem::GenericEpetraProblem(const Epetra_Comm& comm, |
| 31 |
int numGlobalNodes, |
| 32 |
std::string name_) : |
| 33 |
myId(0), |
| 34 |
myName(name_), |
| 35 |
OverlapMap(0), |
| 36 |
Importer(0), |
| 37 |
Comm(&comm), |
| 38 |
StandardMap(0), |
| 39 |
NumGlobalNodes(numGlobalNodes) |
| 40 |
{ |
| 41 |
|
| 42 |
// Commonly used variables |
| 43 |
MyPID = Comm->MyPID(); // Process ID |
| 44 |
NumProc = Comm->NumProc(); // Total number of processes |
| 45 |
|
| 46 |
// Construct data layout |
| 47 |
createMaps(); |
| 48 |
|
| 49 |
// Construction and initialization of mesh and solution vectors are |
| 50 |
// left to the derived problem class. |
| 51 |
} |
| 52 |
|
| 53 |
//----------------------------------------------------------------------------- |
| 54 |
|
| 55 |
// Destructor |
| 56 |
GenericEpetraProblem::~GenericEpetraProblem() |
| 57 |
{ |
| 58 |
delete Importer; Importer = 0; |
| 59 |
delete OverlapMap; OverlapMap = 0; |
| 60 |
delete StandardMap; StandardMap = 0; |
| 61 |
|
| 62 |
for( map<int, Epetra_Vector*>::iterator iter = depSolutions.begin(); |
| 63 |
iter != depSolutions.end(); delete (*iter).second, ++iter ); |
| 64 |
for( map<int, XferOp*>::iterator iter = xferOperators.begin(); |
| 65 |
iter != xferOperators.end(); delete (*iter).second, ++iter ); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
//----------------------------------------------------------------------------- |
| 70 |
|
| 71 |
void |
| 72 |
GenericEpetraProblem::createMaps() |
| 73 |
{ |
| 74 |
|
| 75 |
if (NumGlobalNodes > 0) |
| 76 |
{ |
| 77 |
// Construct a Source Map that puts approximately the same |
| 78 |
// number of equations on each processor |
| 79 |
|
| 80 |
// Begin by distributing nodes fairly equally |
| 81 |
StandardMap = new Epetra_Map(NumGlobalNodes, 0, *Comm); |
| 82 |
|
| 83 |
// Get the number of nodes owned by this processor |
| 84 |
NumMyNodes = StandardMap->NumMyElements(); |
| 85 |
|
| 86 |
// Construct an overlap node map for the finite element fill |
| 87 |
// For single processor jobs, the overlap and standard maps are the same |
| 88 |
if (NumProc == 1) { |
| 89 |
OverlapMap = new Epetra_Map(*StandardMap); |
| 90 |
} else { |
| 91 |
|
| 92 |
int OverlapNumMyNodes; |
| 93 |
int OverlapMinMyNodeGID; |
| 94 |
OverlapNumMyNodes = NumMyNodes + 2; |
| 95 |
if ((MyPID == 0) || (MyPID == NumProc - 1)) |
| 96 |
OverlapNumMyNodes --; |
| 97 |
|
| 98 |
if (MyPID==0) |
| 99 |
OverlapMinMyNodeGID = StandardMap->MinMyGID(); |
| 100 |
else |
| 101 |
OverlapMinMyNodeGID = StandardMap->MinMyGID() - 1; |
| 102 |
|
| 103 |
int* OverlapMyGlobalNodes = new int[OverlapNumMyNodes]; |
| 104 |
|
| 105 |
for (int i = 0; i < OverlapNumMyNodes; i ++) |
| 106 |
OverlapMyGlobalNodes[i] = OverlapMinMyNodeGID + i; |
| 107 |
|
| 108 |
OverlapMap = new Epetra_Map(-1, OverlapNumMyNodes, |
| 109 |
OverlapMyGlobalNodes, 0, *Comm); |
| 110 |
|
| 111 |
delete [] OverlapMyGlobalNodes; |
| 112 |
|
| 113 |
} // End Overlap node map construction ******************************** |
| 114 |
|
| 115 |
Importer = new Epetra_Import(*OverlapMap, *StandardMap); |
| 116 |
|
| 117 |
#ifdef DEBUG |
| 118 |
// Output to check progress so far |
| 119 |
printf("NumMyNodes, NumGlobalNodes --> %d\t%d\n",NumMyNodes, |
| 120 |
NumGlobalNodes); |
| 121 |
std::cout << *StandardMap << std::endl; |
| 122 |
std::cout << *OverlapMap << std::endl; |
| 123 |
Importer->Print(cout); |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
//----------------------------------------------------------------------------- |
| 131 |
|
| 132 |
void |
| 133 |
GenericEpetraProblem::outputResults(const NOX::Solver::Generic& solver, |
| 134 |
Teuchos::ParameterList& printParams) |
| 135 |
{ |
| 136 |
// Output the parameter list |
| 137 |
NOX::Utils utils(printParams); |
| 138 |
if (utils.isPrintType(NOX::Utils::Parameters)) { |
| 139 |
std::cout << std::endl << "Final Parameters" << std::endl |
| 140 |
<< "****************" << std::endl; |
| 141 |
solver.getList().print(cout); |
| 142 |
std::cout << std::endl; |
| 143 |
} |
| 144 |
|
| 145 |
// Get the Epetra_Vector with the final solution from the solver |
| 146 |
const NOX::Epetra::Group& finalGroup = |
| 147 |
dynamic_cast<const NOX::Epetra::Group&>(solver.getSolutionGroup()); |
| 148 |
const Epetra_Vector& finalSolution = |
| 149 |
(dynamic_cast<const NOX::Epetra::Vector&> |
| 150 |
(finalGroup.getX())).getEpetraVector(); |
| 151 |
|
| 152 |
// Print solution |
| 153 |
char file_name[25]; |
| 154 |
FILE *ifp; |
| 155 |
int NumMyElements = finalSolution.Map().NumMyElements(); |
| 156 |
(void) sprintf(file_name, "output.%d",finalSolution.Map().Comm().MyPID()); |
| 157 |
ifp = fopen(file_name, "w"); |
| 158 |
for (int i=0; i<NumMyElements; i++) |
| 159 |
fprintf(ifp,"%d %E\n",finalSolution.Map().MinMyGID()+i,finalSolution[i]); |
| 160 |
fclose(ifp); |
| 161 |
} |
| 162 |
|
| 163 |
//----------------------------------------------------------------------------- |
| 164 |
|
| 165 |
void |
| 166 |
GenericEpetraProblem::outputSolutionStatus( std::ostream & os ) const |
| 167 |
{ |
| 168 |
// Output all solution vectors |
| 169 |
os << "\nProblem Status for " << myName << std::endl; |
| 170 |
os << " ----------------------------------" << std::endl; |
| 171 |
os << *initialSolution << std::endl; |
| 172 |
|
| 173 |
for( unsigned int i = 0; i < depProblems.size(); ++i) { |
| 174 |
int depId = depProblems[i]; |
| 175 |
os << "\tDependent Status for " << myManager->getProblem(depId).getName() << std::endl; |
| 176 |
os << "\t----------------------------------" << std::endl; |
| 177 |
Epetra_Vector & depVec = *( (*(depSolutions.find(depId))).second ); |
| 178 |
os << depVec << std::endl; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
//----------------------------------------------------------------------------- |
| 183 |
|
| 184 |
void |
| 185 |
GenericEpetraProblem::setdt( double dt ) |
| 186 |
{ |
| 187 |
std::cout << "No-op : Implement time dependence in inherited problem !!" << std::endl; |
| 188 |
} |
| 189 |
|
| 190 |
//----------------------------------------------------------------------------- |
| 191 |
|
| 192 |
double |
| 193 |
GenericEpetraProblem::getdt() const |
| 194 |
{ |
| 195 |
std::cout << "No-op : Implement time dependence in inherited problem !!" << std::endl; |
| 196 |
return 0.0; |
| 197 |
} |
| 198 |
|
| 199 |
//----------------------------------------------------------------------------- |
| 200 |
|
| 201 |
Epetra_Vector & |
| 202 |
GenericEpetraProblem::getMesh() |
| 203 |
{ |
| 204 |
assert( !Teuchos::is_null(xptr) ); // Mesh vector had better exist |
| 205 |
return *xptr; |
| 206 |
} |
| 207 |
|
| 208 |
//----------------------------------------------------------------------------- |
| 209 |
|
| 210 |
Teuchos::RCP<Epetra_Vector> |
| 211 |
GenericEpetraProblem::getSolution() |
| 212 |
{ |
| 213 |
return initialSolution; |
| 214 |
} |
| 215 |
|
| 216 |
//----------------------------------------------------------------------------- |
| 217 |
|
| 218 |
Teuchos::RCP<Epetra_CrsMatrix> |
| 219 |
GenericEpetraProblem::getJacobian() |
| 220 |
{ |
| 221 |
if(A.get()) |
| 222 |
return A; |
| 223 |
else |
| 224 |
{ |
| 225 |
std::cout << "ERROR: No valid Jacobian exists for this problem !!" << std::endl; |
| 226 |
return A; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
//----------------------------------------------------------------------------- |
| 231 |
|
| 232 |
void |
| 233 |
GenericEpetraProblem::setSolution(const Epetra_Vector& data) |
| 234 |
{ |
| 235 |
// Ensure that the derived problem class created the solution vector |
| 236 |
if(!initialSolution.get()) |
| 237 |
{ |
| 238 |
std::cout << "ERROR: No solution vector exists for this problem !!" << std::endl; |
| 239 |
throw "GenericEpetraProblem ERROR"; |
| 240 |
} |
| 241 |
|
| 242 |
(*initialSolution.get()) = data; |
| 243 |
} |
| 244 |
|
| 245 |
//----------------------------------------------------------------------------- |
| 246 |
|
| 247 |
void |
| 248 |
GenericEpetraProblem::createDependentVectors() |
| 249 |
{ |
| 250 |
// Create the dependent vectors needed to receive data from other problems |
| 251 |
if( !initialSolution.get() ) |
| 252 |
{ |
| 253 |
std::cout << "ERROR: Cannot create dependent data without an existing solution " |
| 254 |
<< "vector for this problem !!" << std::endl; |
| 255 |
throw "GenericEpetraProblem ERROR"; |
| 256 |
} |
| 257 |
for( unsigned int i = 0; i<depProblems.size(); i++ ) |
| 258 |
{ |
| 259 |
#ifdef DEBUG |
| 260 |
std::cout << "For problem : " << myId << " Creating DepVec for problem : " |
| 261 |
<< depProblems[i] << std::endl; |
| 262 |
#endif |
| 263 |
depSolutions[ depProblems[i] ] = new Epetra_Vector(*initialSolution); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
//----------------------------------------------------------------------------- |
| 268 |
|
| 269 |
void |
| 270 |
GenericEpetraProblem::addProblemDependence( const GenericEpetraProblem& problemB ) |
| 271 |
{ |
| 272 |
// Add a problem to the list of those this one depends on |
| 273 |
depProblems.push_back(problemB.getId()); |
| 274 |
|
| 275 |
// Add to the Name-to-My-Index lookup map |
| 276 |
nameToMyIndex.insert( pair<string, int>(problemB.getName(), |
| 277 |
depProblems.size() - 1) ); |
| 278 |
} |
| 279 |
|
| 280 |
//----------------------------------------------------------------------------- |
| 281 |
|
| 282 |
void |
| 283 |
GenericEpetraProblem::addTransferOp( const GenericEpetraProblem& problemB ) |
| 284 |
{ |
| 285 |
// Add a transfer operator to get fields from another problem |
| 286 |
xferOperators[problemB.getId()] = new XferOp(*this, problemB); |
| 287 |
} |
| 288 |
|
| 289 |
//----------------------------------------------------------------------------- |
| 290 |
|
| 291 |
void |
| 292 |
GenericEpetraProblem::doTransfer() |
| 293 |
{ |
| 294 |
// Do transfers from each dependent problem to this one |
| 295 |
for( unsigned int i = 0; i < depProblems.size(); ++i) |
| 296 |
{ |
| 297 |
int depId = depProblems[i]; |
| 298 |
XferOp* xfer = (*xferOperators.find(depId)).second; |
| 299 |
|
| 300 |
if( !xfer ) |
| 301 |
{ |
| 302 |
std::cout << "ERROR: doTransfer: No valid transfer operator !!" << std::endl; |
| 303 |
throw "GenericEpetraProblem ERROR"; |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
// NOTE that we are transferring (by default) to/from each problem's |
| 308 |
// solution vector which may be different data than in each respective |
| 309 |
// group. |
| 310 |
Epetra_Vector & fromVec = *(myManager->getProblem(depId).getSolution()); |
| 311 |
Epetra_Vector & toVec = *( (*(depSolutions.find(depId))).second ); |
| 312 |
xfer->transferField( toVec, fromVec ); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
//----------------------------------------------------------------------------- |
| 318 |
|
| 319 |
bool |
| 320 |
GenericEpetraProblem::computePrecMatrix( const Epetra_Vector& solnVector, Epetra_RowMatrix& matrix ) |
| 321 |
{ |
| 322 |
std::cout << "WARNING: computePrecMatrix not implemented for this problem !!" |
| 323 |
<< std::endl; |
| 324 |
|
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
//----------------------------------------------------------------------------- |
| 329 |
|
| 330 |
bool |
| 331 |
GenericEpetraProblem::computePreconditioner( const Epetra_Vector& solnVector, Epetra_Operator& precOperator ) |
| 332 |
{ |
| 333 |
std::cout << "WARNING: computePreconditioner not implemented for this problem !!" |
| 334 |
<< std::endl; |
| 335 |
|
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
//----------------------------------------------------------------------------- |
| 340 |
|