| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/* ******************************************************************** */ |
| 11 |
/* See the file COPYRIGHT for a complete copyright notice, contact */ |
| 12 |
/* person and disclaimer. */ |
| 13 |
/* ******************************************************************** */ |
| 14 |
// ML-headers |
| 15 |
#include "ml_common.h" |
| 16 |
#include "TrilinosCouplings_config.h" |
| 17 |
|
| 18 |
#if defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT) |
| 19 |
|
| 20 |
// ---------- Includes ---------- |
| 21 |
#include <ctime> |
| 22 |
#include <iostream> |
| 23 |
|
| 24 |
|
| 25 |
// ---------- User Defined Includes ---------- |
| 26 |
#include "nlnml_linearsystem.H" |
| 27 |
#include "nlnml_preconditioner.H" |
| 28 |
#include "nlnml_ConstrainedMultiLevelOperator.H" |
| 29 |
|
| 30 |
using namespace std; |
| 31 |
|
| 32 |
/*----------------------------------------------------------------------* |
| 33 |
| ctor (public) m.gee 3/06| |
| 34 |
*----------------------------------------------------------------------*/ |
| 35 |
NLNML::NLNML_LinearSystem::NLNML_LinearSystem( |
| 36 |
RefCountPtr<NOX::Epetra::Interface::Jacobian> iJac, |
| 37 |
RefCountPtr<Epetra_Operator> J, |
| 38 |
RefCountPtr<NOX::Epetra::Interface::Preconditioner> iPrec, |
| 39 |
RefCountPtr<NLNML::NLNML_CoarseLevelNoxInterface> coarseinterface, |
| 40 |
RefCountPtr<Epetra_Operator> Prec, |
| 41 |
//const Epetra_Vector& soln, |
| 42 |
bool ismatrixfree, |
| 43 |
int level, |
| 44 |
int printlevel) : |
| 45 |
iJac_(iJac), |
| 46 |
J_(J), |
| 47 |
iPrec_(iPrec), |
| 48 |
coarseinterface_(coarseinterface), |
| 49 |
Prec_(null), |
| 50 |
//soln_(soln), |
| 51 |
ismatrixfree_(ismatrixfree), |
| 52 |
level_(level), |
| 53 |
printlevel_(printlevel) |
| 54 |
{ |
| 55 |
// check corrct type of preconditioner |
| 56 |
Epetra_Operator* tmp = dynamic_cast<Epetra_Operator*>(Prec.get()); |
| 57 |
if (!tmp) |
| 58 |
{ |
| 59 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::NLNML_LinearSystem:\n" |
| 60 |
<< "**ERR**: supplied preconditioner is not an Epetra_Operator!\n" |
| 61 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 62 |
} |
| 63 |
|
| 64 |
// incoming type are allowed to be |
| 65 |
// NLNML::NLNML_Preconditioner |
| 66 |
// ML_Epetra::MultiLevelOperator |
| 67 |
// NLNML::NLNML_ConstrainedMultiLevelOperator |
| 68 |
NLNML::NLNML_Preconditioner* tmp1 = dynamic_cast<NLNML::NLNML_Preconditioner*>(Prec.get()); |
| 69 |
if (tmp1 && printlevel_>9 && tmp->Comm().MyPID()==0) |
| 70 |
cout << "ML (level " << level << "): Preconditioner in linear system is a NLNML::NLNML_Preconditioner\n"; |
| 71 |
|
| 72 |
ML_Epetra::MultiLevelOperator* tmp2 = dynamic_cast<ML_Epetra::MultiLevelOperator*>(Prec.get()); |
| 73 |
if (tmp2 && printlevel>9 && tmp->Comm().MyPID()==0) |
| 74 |
cout << "ML (level " << level << "): Preconditioner in linear system is a ML_Epetra::MultiLevelOperator\n"; |
| 75 |
|
| 76 |
NLNML::NLNML_ConstrainedMultiLevelOperator* tmp3 = dynamic_cast<NLNML::NLNML_ConstrainedMultiLevelOperator*>(Prec.get()); |
| 77 |
if (tmp3 && printlevel>9 && tmp->Comm().MyPID()==0) |
| 78 |
cout << "ML (level " << level << "): Preconditioner in linear system is a NLNML::NLNML_ConstrainedMultiLevelOperator\n"; |
| 79 |
|
| 80 |
if (!tmp1 && !tmp2 && !tmp3) |
| 81 |
{ |
| 82 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::NLNML_LinearSystem:\n" |
| 83 |
<< "**ERR**: supplied preconditioner is not of any recognized type\n" |
| 84 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 85 |
} |
| 86 |
Prec_ = Prec; |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
/*----------------------------------------------------------------------* |
| 91 |
| m.gee 3/06| |
| 92 |
*----------------------------------------------------------------------*/ |
| 93 |
bool NLNML::NLNML_LinearSystem::applyRightPreconditioning( |
| 94 |
bool useTranspose, |
| 95 |
Teuchos::ParameterList& params, |
| 96 |
const NOX::Epetra::Vector& input, |
| 97 |
NOX::Epetra::Vector& result) const |
| 98 |
{ |
| 99 |
const Epetra_Vector& in = input.getEpetraVector(); |
| 100 |
Epetra_Vector& out = result.getEpetraVector(); |
| 101 |
|
| 102 |
int ierr = Prec_->ApplyInverse(in,out); |
| 103 |
if (!ierr) return true; |
| 104 |
else return false; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/*----------------------------------------------------------------------* |
| 109 |
| m.gee 3/06| |
| 110 |
*----------------------------------------------------------------------*/ |
| 111 |
bool NLNML::NLNML_LinearSystem::computeJacobian(const NOX::Epetra::Vector& x) |
| 112 |
{ |
| 113 |
const Epetra_Vector& ex = x.getEpetraVector(); |
| 114 |
if (!ismatrixfree_) |
| 115 |
{ |
| 116 |
bool err = iJac_->computeJacobian(ex,*J_); |
| 117 |
return err; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
NOX::Epetra::MatrixFree* Jac = |
| 122 |
dynamic_cast<NOX::Epetra::MatrixFree*>(J_.get()); |
| 123 |
if (!Jac) |
| 124 |
{ |
| 125 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::computeJacobian:\n" |
| 126 |
<< "**ERR**: Jacobian is not a NOX::Epetra::MatrixFree operator\n" |
| 127 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 128 |
} |
| 129 |
Jac->computeJacobian(ex,*Jac); |
| 130 |
} |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
/*----------------------------------------------------------------------* |
| 137 |
| m.gee 3/06| |
| 138 |
*----------------------------------------------------------------------*/ |
| 139 |
bool NLNML::NLNML_LinearSystem::createPreconditioner( |
| 140 |
const NOX::Epetra::Vector& x, |
| 141 |
Teuchos::ParameterList& p, |
| 142 |
bool recomputeGraph) const |
| 143 |
{ |
| 144 |
if (level_==0) |
| 145 |
{ |
| 146 |
// on the fine level, the preconditioner can either be |
| 147 |
// NLNML::NLNML_Preconditioner or |
| 148 |
// ML_Epetra::MultiLevelOperator or |
| 149 |
// NLNML::NLNML_ConstrainedMultiLevelOperator |
| 150 |
NLNML::NLNML_Preconditioner* Prec1 = |
| 151 |
dynamic_cast<NLNML::NLNML_Preconditioner*>(Prec_.get()); |
| 152 |
if (Prec1) |
| 153 |
{ |
| 154 |
const Epetra_Vector& ex = x.getEpetraVector(); |
| 155 |
bool err = Prec1->computePreconditioner(ex,*Prec1,NULL); |
| 156 |
return err; |
| 157 |
} |
| 158 |
|
| 159 |
ML_Epetra::MultiLevelOperator* Prec2 = |
| 160 |
dynamic_cast<ML_Epetra::MultiLevelOperator*>(Prec_.get()); |
| 161 |
if (Prec2) return true; |
| 162 |
|
| 163 |
NLNML::NLNML_ConstrainedMultiLevelOperator* Prec3 = |
| 164 |
dynamic_cast<NLNML::NLNML_ConstrainedMultiLevelOperator*>(Prec_.get()); |
| 165 |
if (Prec3) return true; |
| 166 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::createPreconditioner:\n" |
| 167 |
<< "**ERR**: Preconditioning operator is of unknown type\n" |
| 168 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 169 |
} |
| 170 |
else |
| 171 |
{ |
| 172 |
ML_Epetra::MultiLevelOperator* Prec2 = |
| 173 |
dynamic_cast<ML_Epetra::MultiLevelOperator*>(Prec_.get()); |
| 174 |
if (Prec2) return true; |
| 175 |
|
| 176 |
NLNML::NLNML_ConstrainedMultiLevelOperator* Prec3 = |
| 177 |
dynamic_cast<NLNML::NLNML_ConstrainedMultiLevelOperator*>(Prec_.get()); |
| 178 |
if (Prec3) return true; |
| 179 |
|
| 180 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::createPreconditioner:\n" |
| 181 |
<< "**ERR**: Preconditioning operator is of unknown type\n" |
| 182 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 183 |
} |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/*----------------------------------------------------------------------* |
| 189 |
| m.gee 3/06| |
| 190 |
*----------------------------------------------------------------------*/ |
| 191 |
void NLNML::NLNML_LinearSystem::setJacobianOperatorForSolve( |
| 192 |
const Teuchos::RefCountPtr<const Epetra_Operator>& solveJacOp) |
| 193 |
{ |
| 194 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::setJacobianOperatorForSolve:\n" |
| 195 |
<< "**ERR**: not supposed to be called???????\n" |
| 196 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
/*----------------------------------------------------------------------* |
| 202 |
| m.gee 3/06| |
| 203 |
*----------------------------------------------------------------------*/ |
| 204 |
void NLNML::NLNML_LinearSystem::setPrecOperatorForSolve( |
| 205 |
const Teuchos::RefCountPtr<const Epetra_Operator>& solvePrecOp) |
| 206 |
{ |
| 207 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::setPrecOperatorForSolve:\n" |
| 208 |
<< "**ERR**: not supposed to be called???????\n" |
| 209 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
/*----------------------------------------------------------------------* |
| 214 |
| m.gee 3/06| |
| 215 |
*----------------------------------------------------------------------*/ |
| 216 |
bool NLNML::NLNML_LinearSystem::applyJacobian( |
| 217 |
const NOX::Epetra::Vector& input, |
| 218 |
NOX::Epetra::Vector& result) const |
| 219 |
{ |
| 220 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::applyJacobian:\n" |
| 221 |
<< "**ERR**: not supposed to be called???????\n" |
| 222 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/*----------------------------------------------------------------------* |
| 228 |
| m.gee 3/06| |
| 229 |
*----------------------------------------------------------------------*/ |
| 230 |
bool NLNML::NLNML_LinearSystem::applyJacobianTranspose( |
| 231 |
const NOX::Epetra::Vector& input, |
| 232 |
NOX::Epetra::Vector& result) const |
| 233 |
{ |
| 234 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::applyJacobianTranspose:\n" |
| 235 |
<< "**ERR**: not supposed to be called???????\n" |
| 236 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
/*----------------------------------------------------------------------* |
| 241 |
| m.gee 3/06| |
| 242 |
*----------------------------------------------------------------------*/ |
| 243 |
bool NLNML::NLNML_LinearSystem::applyJacobianInverse( |
| 244 |
Teuchos::ParameterList ¶ms, |
| 245 |
const NOX::Epetra::Vector &input, |
| 246 |
NOX::Epetra::Vector &result) |
| 247 |
{ |
| 248 |
cout << "**ERR**: NLNML::NLNML_LinearSystem::applyJacobianInverse:\n" |
| 249 |
<< "**ERR**: not supposed to be called???????\n" |
| 250 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 251 |
return false; |
| 252 |
} |
| 253 |
#endif |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|