| 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 |
#ifndef NOX_PROBLEM_MANAGER_H |
| 11 |
#define NOX_PROBLEM_MANAGER_H |
| 12 |
|
| 13 |
#include "NOX.H" |
| 14 |
#include "NOX_Multiphysics_DataExchange_Interface.H" |
| 15 |
#include "NOX_Epetra_SchurInterface.H" |
| 16 |
#include "NOX_Epetra.H" |
| 17 |
#include "NOX_Epetra_BroydenOperator.H" |
| 18 |
|
| 19 |
#include "Problem_Interface.H" |
| 20 |
#include "GenericEpetraProblem.H" |
| 21 |
#include "Teuchos_RCP.hpp" |
| 22 |
|
| 23 |
#ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos |
| 24 |
class OffBlock_Manager; |
| 25 |
#include "EpetraExt_MapColoring.h" |
| 26 |
#include "EpetraExt_MapColoringIndex.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
enum CouplingSolveMethod { LOOSE_HARDCODED, |
| 32 |
LOOSE_LIBRARY , |
| 33 |
MATRIX_FREE , |
| 34 |
SCHUR_BASED }; |
| 35 |
|
| 36 |
const CouplingSolveMethod SolveMethodValues[] = { LOOSE_HARDCODED, LOOSE_LIBRARY, MATRIX_FREE, SCHUR_BASED }; |
| 37 |
|
| 38 |
static const char * SolveMethodNames[] = { "loose-hardcoded" , |
| 39 |
"loose-library" , |
| 40 |
"matrix-free" , |
| 41 |
"schur-based" }; |
| 42 |
|
| 43 |
class Problem_Manager : public GenericEpetraProblem , |
| 44 |
public NOX::Multiphysics::DataExchange::Interface , |
| 45 |
public NOX::Epetra::SchurInterface , |
| 46 |
public NOX::Epetra::BroydenOperator::ReplacementInterface |
| 47 |
{ |
| 48 |
|
| 49 |
public: |
| 50 |
|
| 51 |
enum VectorType { SOLUTION, GROUP_F }; |
| 52 |
|
| 53 |
// Constructor |
| 54 |
Problem_Manager(Epetra_Comm& Comm, bool doOffBlocks = false, |
| 55 |
int NumGlobalElements = 0, bool useMatlab = false); |
| 56 |
|
| 57 |
// Destructor |
| 58 |
virtual ~Problem_Manager(); |
| 59 |
|
| 60 |
// Add a problem to the Manager |
| 61 |
void addProblem(GenericEpetraProblem& problem); |
| 62 |
|
| 63 |
// Get the number of registered problems |
| 64 |
int getProblemCount() { return problemCount; } |
| 65 |
|
| 66 |
// Get the problem names container |
| 67 |
map<int, std::string> & getNames() { return Names; } |
| 68 |
|
| 69 |
// Return my Problems container |
| 70 |
map<int, Teuchos::RCP<GenericEpetraProblem> > & getProblems() |
| 71 |
{ return Problems; } |
| 72 |
|
| 73 |
// Get a problem given its unique id |
| 74 |
GenericEpetraProblem & getProblem(int id); |
| 75 |
|
| 76 |
// Get a problem given its name |
| 77 |
GenericEpetraProblem & getProblem(std::string name); |
| 78 |
|
| 79 |
// Get a group given its unique id |
| 80 |
const NOX::Epetra::Group & getProblemSolutionGroup(int id); |
| 81 |
|
| 82 |
// Get the solution vector from a problem group |
| 83 |
const Epetra_Vector & getSolutionVec(int id); |
| 84 |
|
| 85 |
// Get an individual residual vector given an integer problem id |
| 86 |
const Epetra_Vector & getResidualVec(int i); |
| 87 |
|
| 88 |
// Get a group given its unique id |
| 89 |
NOX::Epetra::Group& getGroup(int id); |
| 90 |
|
| 91 |
// Get the compositeSoln vector |
| 92 |
Teuchos::RCP<Epetra_Vector> getCompositeSoln(); |
| 93 |
|
| 94 |
// Get the compositeSoln Map |
| 95 |
Teuchos::RCP<Epetra_Map> getCompositeMap() |
| 96 |
{ return compositeMap; } |
| 97 |
|
| 98 |
// Get the container of solver managers |
| 99 |
map<int, Teuchos::RCP<NOX::Solver::Generic> > & getSolvers() |
| 100 |
{ return Solvers; } |
| 101 |
|
| 102 |
// Get the composite NOX solver |
| 103 |
Teuchos::RCP<NOX::Solver::Generic> & getCompositeSolver() |
| 104 |
{ return compositeSolver; } |
| 105 |
|
| 106 |
// Get mapping of problem indices to composite |
| 107 |
map<int, Teuchos::RCP<Epetra_IntVector> > & getProblemToCompositeIndices() |
| 108 |
{ return ProblemToCompositeIndices; } |
| 109 |
|
| 110 |
// Get jacobian operator |
| 111 |
Teuchos::RCP<Epetra_Operator> & getJacobianOperator() |
| 112 |
{ return jacOperator; } |
| 113 |
|
| 114 |
// Create a dependence of Problem A equations on Problem B variables |
| 115 |
void createDependency(std::string nameA, std::string nameB, bool interfacialCoupling = false); |
| 116 |
|
| 117 |
// Create a dependence of Problem A equations on Problem B variables |
| 118 |
void createDependency(GenericEpetraProblem& problemA, GenericEpetraProblem& problemB, |
| 119 |
bool interfacialCoupling = false); |
| 120 |
|
| 121 |
// Register NLSolver parameter list |
| 122 |
void registerParameters(const Teuchos::RCP<Teuchos::ParameterList>& List); |
| 123 |
|
| 124 |
// Register Convergence Test(s) |
| 125 |
void registerStatusTest(const Teuchos::RCP<NOX::StatusTest::Combo>& comboTest); |
| 126 |
|
| 127 |
// Signal completion of problem, parameter, and convergence test |
| 128 |
// regitrations and trigger the problem setup |
| 129 |
void registerComplete(); |
| 130 |
|
| 131 |
// Have each problem receive dependent data from every other problem |
| 132 |
// on which it depends |
| 133 |
void syncAllProblems(); |
| 134 |
|
| 135 |
// Set all problems with the same time step size |
| 136 |
void setAlldt( double dt); |
| 137 |
|
| 138 |
// Set the solver group solution using the current solution contained |
| 139 |
// in the problem |
| 140 |
void setGroupX(int id); |
| 141 |
|
| 142 |
// Set the solver group solution using a provided vector |
| 143 |
void setGroupX(int id, Epetra_Vector &); |
| 144 |
|
| 145 |
// Set all groups with the current solution contained in each problem |
| 146 |
void setAllGroupX(); |
| 147 |
|
| 148 |
#ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos |
| 149 |
// Set all off-block groups with the current composite-size solution |
| 150 |
void setAllOffBlockGroupX(const Epetra_Vector&); |
| 151 |
#endif |
| 152 |
|
| 153 |
// Reset all problems by copying the current solution into the old solution |
| 154 |
// Needed for time-dependent problems |
| 155 |
void resetProblems(); |
| 156 |
|
| 157 |
// Compute residual (F) in solution groups for all registered problems |
| 158 |
void computeAllF(); |
| 159 |
|
| 160 |
// Compute residual (F) in solution group specified by id |
| 161 |
void computeGroupF(int id); |
| 162 |
|
| 163 |
// Compute Jacobian in solution groups for all registered problems |
| 164 |
void computeAllJacobian(); |
| 165 |
|
| 166 |
// Compute an individual block Jacobian given an integer pairing of problem and |
| 167 |
// the problem variables on which it depends |
| 168 |
void computeBlockJacobian(int i, int j = -1); |
| 169 |
|
| 170 |
// Get an individual block Jacobian given an integer pairing of problem and |
| 171 |
// the problem variables on which it depends |
| 172 |
Teuchos::RCP<Epetra_CrsMatrix> getBlockJacobianMatrix(int i, int j = -1); |
| 173 |
|
| 174 |
// Get an individual block Jacobian inverse operator |
| 175 |
Teuchos::RCP<Epetra_Operator> getBlockInverseOperator(int i); |
| 176 |
|
| 177 |
// Compute sum of norms of each problem residual |
| 178 |
double getNormSum(); |
| 179 |
|
| 180 |
// Copy final solution from NOX solver into the problem's solution vector |
| 181 |
void copyGroupCurrentXtoProblemX(int id); |
| 182 |
|
| 183 |
// Copy final solution from NOX solvers into each problem's solution vector |
| 184 |
void copyAllGroupXtoProblems(); |
| 185 |
|
| 186 |
// Reset specified solvers current solution group X vector with itself, thereby resetting all valid flags |
| 187 |
void resetCurrentGroupX(int probId); |
| 188 |
|
| 189 |
// Reset each solvers current solution group X vector with itself, thereby resetting all valid flags |
| 190 |
void resetAllCurrentGroupX(); |
| 191 |
|
| 192 |
// Copy a composite problem vector to each problem's vector |
| 193 |
void copyCompositeToProblems(const Epetra_Vector& compositeVec, VectorType); |
| 194 |
|
| 195 |
// Copy part of a composite problem vector to a problem's vector |
| 196 |
void copyCompositeToVector(const Epetra_Vector& compositeVec, int id, |
| 197 |
Epetra_Vector& problemVec); |
| 198 |
|
| 199 |
// Copy vectors from each problem into a composite problem vector |
| 200 |
void copyProblemsToComposite(Epetra_Vector& compositeVec, VectorType); |
| 201 |
|
| 202 |
// Copy a vector from a problem into part of a composite problem vector |
| 203 |
void copyVectorToComposite(Epetra_Vector& compositeVec, int id, |
| 204 |
const Epetra_Vector& problemVec); |
| 205 |
|
| 206 |
// Copy problem Jacobians as block diagonal contributions to |
| 207 |
// composite Jacobian |
| 208 |
void copyProblemJacobiansToComposite(); |
| 209 |
|
| 210 |
// Copy problem Jacobians as block diagonal contributions to |
| 211 |
// specified composite matrix |
| 212 |
void copyProblemJacobiansToComposite( Epetra_CrsMatrix & mat ); |
| 213 |
|
| 214 |
// Solve problem using fixed-point iteration |
| 215 |
bool solve(); |
| 216 |
|
| 217 |
// Solve problems using matrix-free coupling |
| 218 |
bool solveMF(); |
| 219 |
|
| 220 |
// Solve problems using schur-based coupling |
| 221 |
bool solveSchurBased(); |
| 222 |
|
| 223 |
// Output summary of copled problems |
| 224 |
void outputStatus( std::ostream & os ); |
| 225 |
|
| 226 |
// Create a filename for given problem, time-step, etc. |
| 227 |
std::string createIOname(GenericEpetraProblem &, int timeStep = 0); |
| 228 |
|
| 229 |
// Solve problems using matrix-free coupling |
| 230 |
void outputSolutions( const std::string outputDir, int timeStep = 0 ); |
| 231 |
|
| 232 |
// Query whether or not offBlocks ar used |
| 233 |
bool useOffBlocks() |
| 234 |
{ return doOffBlocks; } |
| 235 |
|
| 236 |
// Return my name |
| 237 |
std::string getName() const |
| 238 |
{ return GenericEpetraProblem::getName(); }; |
| 239 |
|
| 240 |
// Return the problem name queries |
| 241 |
std::string getProblemName(int id) |
| 242 |
{ return Names[id]; }; |
| 243 |
|
| 244 |
// These allow inheritance from GenericEpetraProblem base class |
| 245 |
|
| 246 |
bool evaluate(NOX::Epetra::Interface::Required::FillType type, |
| 247 |
const Epetra_Vector *solnVector, |
| 248 |
Epetra_Vector *rhsVector); |
| 249 |
|
| 250 |
// Inherited methods to allow use of library fixed-point |
| 251 |
virtual bool exchangeAllData(); |
| 252 |
|
| 253 |
virtual bool exchangeDataTo(int solverId); |
| 254 |
|
| 255 |
// Inherited method to allow matrix replacement with BroydenOperator |
| 256 |
virtual Teuchos::RCP<const Epetra_CrsMatrix> |
| 257 |
getReplacementValuesMatrix( const Epetra_Vector & x, NOX::Epetra::BroydenOperator::ReplacementInterface::FILL_TYPE ); |
| 258 |
|
| 259 |
// method to generate only a diagonal block composite graph |
| 260 |
virtual Teuchos::RCP<Epetra_CrsGraph> |
| 261 |
generateBlockDiagonalGraph( const Epetra_Map &, bool includeProblemOffContribs = false ); |
| 262 |
|
| 263 |
// method to generate only off-diagonal block composite graph |
| 264 |
virtual Teuchos::RCP<Epetra_CrsGraph> |
| 265 |
generateOffDiagonalBlockGraph( const Epetra_Map & ); |
| 266 |
|
| 267 |
// method to combine (compose) two composite graphs into a new one |
| 268 |
virtual Teuchos::RCP<Epetra_CrsGraph> |
| 269 |
composeGraphs( const Epetra_CrsGraph &, const Epetra_CrsGraph & ); |
| 270 |
|
| 271 |
// Inherited from NOX::Epetra::SchurInterface |
| 272 |
|
| 273 |
// method to compute the action of a block on a vector using a directional derivative |
| 274 |
virtual bool applyBlockAction( int probId, int depId, const Epetra_Vector &, Epetra_Vector & ); |
| 275 |
|
| 276 |
// method to compute the action of a block inverse on a vector |
| 277 |
virtual bool applyBlockInverseAction( int probId, int depId, const Epetra_Vector &, Epetra_Vector & ); |
| 278 |
|
| 279 |
// Accessor for the communicator |
| 280 |
virtual const Epetra_Comm & getComm() |
| 281 |
{ return *Comm; } |
| 282 |
|
| 283 |
//! Query availability of explicit operator |
| 284 |
virtual bool hasExplicitOperator( int rowBlock, int colBlock ); |
| 285 |
|
| 286 |
//! Access explicit operator |
| 287 |
virtual Teuchos::RCP<Epetra_Operator> getExplicitOperator( int rowBlock, int colBlock ); |
| 288 |
|
| 289 |
// method to create an inverse or its approximation to a diagonal block matrix |
| 290 |
virtual bool createBlockInverseOperator( int probId, Teuchos::ParameterList & ); |
| 291 |
|
| 292 |
protected: |
| 293 |
|
| 294 |
// Creates a composite graph from existing graphs of registered problems |
| 295 |
void generateGraph(); |
| 296 |
|
| 297 |
public: |
| 298 |
// Made these public for now |
| 299 |
Teuchos::RCP<Teuchos::ParameterList> nlParams; // NLSolver parameter list |
| 300 |
Teuchos::RCP<NOX::StatusTest::Combo> statusTest; // Convergence criterion (ia) |
| 301 |
|
| 302 |
private: |
| 303 |
|
| 304 |
int problemCount; // Number of registered Problems |
| 305 |
bool doOffBlocks; // Flag to specify whether or not to compute |
| 306 |
// off-diagonal composite Jacobian blocks via FDC |
| 307 |
bool useMatlab; // Flag to specify whether or not to use a Matlab interactive session |
| 308 |
|
| 309 |
Teuchos::RCP<Epetra_Map> compositeMap; // Composite map for all registered problems |
| 310 |
Teuchos::RCP<Epetra_Vector> compositeSoln; // Composite map for all registered problems |
| 311 |
map<int, Teuchos::RCP<GenericEpetraProblem> > Problems; // Problem container |
| 312 |
map<int, std::string> Names; // Problem names |
| 313 |
map<string, int> NameLookup; // Needed to lookup problems by name |
| 314 |
map<int, Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> > LinearSystems; |
| 315 |
// Linear Systems container |
| 316 |
map<int, Teuchos::RCP<NOX::Epetra::Group> >Groups; // Groups container |
| 317 |
map<int, Teuchos::RCP<Problem_Interface> >Interfaces; // Interfaces container |
| 318 |
map<int, Teuchos::RCP<NOX::Solver::Generic> > Solvers; // Solvers container |
| 319 |
map<int, Teuchos::RCP<Epetra_IntVector> > ProblemToCompositeIndices; |
| 320 |
// Essentially a mapping from a problems indices to the composite indices |
| 321 |
|
| 322 |
Teuchos::RCP<Epetra_Operator> jacOperator ; // The composite Jacobian operator |
| 323 |
Teuchos::RCP<Epetra_Operator> precOperator ; // The composite Preconditioner operator |
| 324 |
Teuchos::RCP<NOX::Epetra::Interface::Jacobian> jacInterface; |
| 325 |
Teuchos::RCP<NOX::Epetra::Interface::Preconditioner> precInterface; |
| 326 |
Teuchos::RCP<NOX::Epetra::Group> compositeGroup ; // The composite solution group |
| 327 |
Teuchos::RCP<NOX::Solver::Generic> compositeSolver ; // The composite solver |
| 328 |
|
| 329 |
#ifdef HAVE_NOX_EPETRAEXT |
| 330 |
// Additional containers to allow each problem to use FD Coloring |
| 331 |
map<int, Teuchos::RCP<EpetraExt::CrsGraph_MapColoring> > TmpMapColorings; |
| 332 |
map<int, Teuchos::RCP<Epetra_MapColoring> > ColorMaps; |
| 333 |
map<int, Teuchos::RCP<EpetraExt::CrsGraph_MapColoringIndex> > ColorMapIndexSets; |
| 334 |
map<int, Teuchos::RCP<vector<Epetra_IntVector> > > ColumnsSets; |
| 335 |
map<int, Teuchos::RCP<Epetra_Operator> > MatrixOperators; |
| 336 |
|
| 337 |
// These containers allow off-diagonal block contributions representing |
| 338 |
// inter-problem coupling |
| 339 |
map<int, std::vector<OffBlock_Manager*> > OffBlock_Managers; |
| 340 |
#endif |
| 341 |
|
| 342 |
map<int, Teuchos::RCP<Epetra_Operator> > BlockInverseOperators; // inverses (or approximations) to diagonal blocks |
| 343 |
|
| 344 |
}; |
| 345 |
#endif |