| 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 "ml_nox_linearsystem.H" |
| 27 |
#include "ml_nox_preconditioner.H" |
| 28 |
|
| 29 |
/*----------------------------------------------------------------------* |
| 30 |
| ctor (public) m.gee 12/04| |
| 31 |
*----------------------------------------------------------------------*/ |
| 32 |
ML_NOX::Ml_Nox_LinearSystem::Ml_Nox_LinearSystem( |
| 33 |
NOX::EpetraNew::Interface::Jacobian& iJac, |
| 34 |
Epetra_Operator& J, |
| 35 |
NOX::EpetraNew::Interface::Preconditioner& iPrec, |
| 36 |
ML_NOX::Nox_CoarseProblem_Interface* coarseinterface, |
| 37 |
Epetra_Operator& Prec, |
| 38 |
const Epetra_Vector& soln, |
| 39 |
bool ismatrixfree, |
| 40 |
int level, |
| 41 |
int printlevel) : |
| 42 |
iJac_(iJac), |
| 43 |
J_(J), |
| 44 |
iPrec_(iPrec), |
| 45 |
soln_(soln), |
| 46 |
coarseinterface_(coarseinterface) |
| 47 |
{ |
| 48 |
level_ = level; |
| 49 |
ismatrixfree_ = ismatrixfree; |
| 50 |
printlevel_ = printlevel; |
| 51 |
Precptr_ = 0; |
| 52 |
|
| 53 |
// check whether the correct type of preconditioner was supplied |
| 54 |
Epetra_Operator* tmp = dynamic_cast<Epetra_Operator*>(&Prec); |
| 55 |
if (!tmp) |
| 56 |
{ |
| 57 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::Ml_Nox_LinearSystem:\n" |
| 58 |
<< "**ERR**: supplied preconditioner is not an Epetra_Operator!\n" |
| 59 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 60 |
} |
| 61 |
|
| 62 |
// find out a little bit more detailed, just to know.... |
| 63 |
ML_NOX::ML_Nox_Preconditioner* tmp1 = dynamic_cast<ML_NOX::ML_Nox_Preconditioner*>(&Prec); |
| 64 |
if (tmp1 && printlevel>9 && tmp->Comm().MyPID()==0) |
| 65 |
cout << "ML (level " << level << "): Preconditioner is a ML_NOX::ML_Nox_Preconditioner\n"; |
| 66 |
|
| 67 |
ML_Epetra::MultiLevelOperator* tmp2 = dynamic_cast<ML_Epetra::MultiLevelOperator*>(&Prec); |
| 68 |
if (tmp2 && printlevel>9 && tmp->Comm().MyPID()==0) |
| 69 |
cout << "ML (level " << level << "): Preconditioner is a ML_Epetra::MultiLevelOperator\n"; |
| 70 |
|
| 71 |
ML_NOX::ML_Nox_ConstrainedMultiLevelOperator* tmp3 = dynamic_cast<ML_NOX::ML_Nox_ConstrainedMultiLevelOperator*>(&Prec); |
| 72 |
if (tmp3 && printlevel>9 && tmp->Comm().MyPID()==0) |
| 73 |
cout << "ML (level " << level << "): Preconditioner is a ML_NOX::ML_Nox_ConstrainedMultiLevelOperator\n"; |
| 74 |
|
| 75 |
Precptr_ = tmp; |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
/*----------------------------------------------------------------------* |
| 81 |
| dtor (public) m.gee 12/04| |
| 82 |
*----------------------------------------------------------------------*/ |
| 83 |
ML_NOX::Ml_Nox_LinearSystem::~Ml_Nox_LinearSystem() |
| 84 |
{ |
| 85 |
coarseinterface_ = NULL; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/*----------------------------------------------------------------------* |
| 90 |
| applyRightPreconditioning (public) m.gee 12/04| |
| 91 |
*----------------------------------------------------------------------*/ |
| 92 |
bool ML_NOX::Ml_Nox_LinearSystem::applyRightPreconditioning( |
| 93 |
bool useTranspose, |
| 94 |
NOX::Parameter::List& params, |
| 95 |
const NOX::Epetra::Vector& input, |
| 96 |
NOX::Epetra::Vector& result) const |
| 97 |
{ |
| 98 |
int ierr=1; |
| 99 |
if (!Precptr_) |
| 100 |
{ |
| 101 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyRightPreconditioning:\n" |
| 102 |
<< "**ERR**: ptr to Preconditioner is NULL\n" |
| 103 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 104 |
} |
| 105 |
if (useTranspose) |
| 106 |
{ |
| 107 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyRightPreconditioning:\n" |
| 108 |
<< "**ERR**: apply transposed of preconditioner is not a good idea\n" |
| 109 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 110 |
} |
| 111 |
|
| 112 |
const Epetra_Vector& in = input.getEpetraVector(); |
| 113 |
Epetra_Vector& out = result.getEpetraVector(); |
| 114 |
|
| 115 |
#if 0 |
| 116 |
cout << "ML_NOX::Ml_Nox_LinearSystem::applyRightPreconditioning:\n"; |
| 117 |
cout << "in on input:\n"; |
| 118 |
cout << in; |
| 119 |
cout << "out on input:\n"; |
| 120 |
cout << out; |
| 121 |
#endif |
| 122 |
|
| 123 |
// Note that this preconditioner is either a ML_NOX::ML_Nox_Preconditioner |
| 124 |
// or a ML_Epetra::MultiLevelOperator |
| 125 |
// both implement an Epetra_Operator so we are happy here anyway |
| 126 |
|
| 127 |
#if 0 |
| 128 |
Epetra_Operator* tmp = dynamic_cast<Epetra_Operator*>(Precptr_); |
| 129 |
if (!tmp) |
| 130 |
{ |
| 131 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyRightPreconditioning:\n" |
| 132 |
<< "**ERR**: supplied preconditioner is not a Epetra_Operator!\n" |
| 133 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
// find out a little bit more detailed, just to know.... |
| 138 |
ML_NOX::ML_Nox_Preconditioner* tmp1 = dynamic_cast<ML_NOX::ML_Nox_Preconditioner*>(Precptr_); |
| 139 |
if (tmp1 && printlevel_>9 && tmp1->Comm().MyPID()==0) |
| 140 |
cout << "ML (level " << level_ << "): Preconditioner is a ML_NOX::ML_Nox_Preconditioner\n"; |
| 141 |
|
| 142 |
if (tmp1) |
| 143 |
{ |
| 144 |
ierr = tmp1->ApplyInverse(in,out); |
| 145 |
if (!ierr) |
| 146 |
return true; |
| 147 |
else |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
ML_Epetra::MultiLevelOperator* tmp2 = dynamic_cast<ML_Epetra::MultiLevelOperator*>(Precptr_); |
| 152 |
if (tmp2 && printlevel_>9 && tmp2->Comm().MyPID()==0) |
| 153 |
cout << "ML (level " << level_ << "): Preconditioner is a ML_Epetra::MultiLevelOperator\n"; |
| 154 |
|
| 155 |
if (tmp2) |
| 156 |
{ |
| 157 |
ierr = tmp2->ApplyInverse(in,out); |
| 158 |
if (!ierr) |
| 159 |
return true; |
| 160 |
else |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
ML_NOX::ML_Nox_ConstrainedMultiLevelOperator* tmp3 = dynamic_cast<ML_NOX::ML_Nox_ConstrainedMultiLevelOperator*>(Precptr_); |
| 165 |
if (tmp3 && printlevel_>9 && tmp3->Comm().MyPID()==0) |
| 166 |
cout << "ML (level " << level_ << "): Preconditioner is a ML_Epetra::MultiLevelOperator\n"; |
| 167 |
|
| 168 |
if (tmp3) |
| 169 |
{ |
| 170 |
ierr = tmp3->ApplyInverse(in,out); |
| 171 |
if (!ierr) |
| 172 |
return true; |
| 173 |
else |
| 174 |
return false; |
| 175 |
} |
| 176 |
else |
| 177 |
{ |
| 178 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyRightPreconditioning:\n" |
| 179 |
<< "**ERR**: unknown type of preconditioning operator\n" |
| 180 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 181 |
} |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
/*----------------------------------------------------------------------* |
| 186 |
| computeJacobian (public) m.gee 12/04| |
| 187 |
*----------------------------------------------------------------------*/ |
| 188 |
bool ML_NOX::Ml_Nox_LinearSystem::computeJacobian(Epetra_Vector& x) |
| 189 |
{ |
| 190 |
if (ismatrixfree_==false) |
| 191 |
{ |
| 192 |
bool status = iJac_.computeJacobian(x); |
| 193 |
return status; |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
NOX::EpetraNew::MatrixFree* Jac = |
| 198 |
dynamic_cast<NOX::EpetraNew::MatrixFree*>(&J_); |
| 199 |
if (!Jac) |
| 200 |
{ |
| 201 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::computeJacobian:\n" |
| 202 |
<< "**ERR**: Jacobian is not a NOX::EpetraNew::MatrixFree object\n" |
| 203 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 204 |
} |
| 205 |
bool status = Jac->computeJacobian(x); |
| 206 |
return status; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/*----------------------------------------------------------------------* |
| 211 |
| createPreconditioner (public) m.gee 12/04| |
| 212 |
*----------------------------------------------------------------------*/ |
| 213 |
bool ML_NOX::Ml_Nox_LinearSystem::createPreconditioner( |
| 214 |
Epetra_Vector& x, |
| 215 |
NOX::Parameter::List& p, |
| 216 |
bool recomputeGraph) const |
| 217 |
{ |
| 218 |
if (level_ == 0) |
| 219 |
{ |
| 220 |
// on the fine level, the preconditioner can either be a ML_NOX::ML_Nox_Preconditioner |
| 221 |
// or a ML_Epetra::MultiLevelOperator |
| 222 |
ML_NOX::ML_Nox_Preconditioner* Prec1 = dynamic_cast<ML_NOX::ML_Nox_Preconditioner*>(Precptr_); |
| 223 |
if (Prec1) |
| 224 |
{ |
| 225 |
bool status = Prec1->computePreconditioner(x); |
| 226 |
return status; |
| 227 |
} |
| 228 |
ML_Epetra::MultiLevelOperator* Prec2 = dynamic_cast<ML_Epetra::MultiLevelOperator*>(Precptr_); |
| 229 |
if (Prec2) |
| 230 |
return true; |
| 231 |
ML_NOX::ML_Nox_ConstrainedMultiLevelOperator* Prec3 = dynamic_cast<ML_NOX::ML_Nox_ConstrainedMultiLevelOperator*>(Precptr_); |
| 232 |
if (Prec3) |
| 233 |
return true; |
| 234 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::createPreconditioner:\n" |
| 235 |
<< "**ERR**: type of preconditioner unknown\n" |
| 236 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 237 |
return false; |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
// on a coarse level the preconditioner can only be a ML_Epetra::MultiLevelOperator |
| 242 |
// there is no need to compute it here, it's ready |
| 243 |
ML_Epetra::MultiLevelOperator* Prec2 = dynamic_cast<ML_Epetra::MultiLevelOperator*>(Precptr_); |
| 244 |
if (Prec2) |
| 245 |
return true; |
| 246 |
ML_NOX::ML_Nox_ConstrainedMultiLevelOperator* Prec3 = dynamic_cast<ML_NOX::ML_Nox_ConstrainedMultiLevelOperator*>(Precptr_); |
| 247 |
if (Prec3) |
| 248 |
return true; |
| 249 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::createPreconditioner:\n" |
| 250 |
<< "**ERR**: type of preconditioner unknown\n" |
| 251 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 252 |
return false; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/*----------------------------------------------------------------------* |
| 257 |
| destroyPreconditioner (public) m.gee 12/04| |
| 258 |
*----------------------------------------------------------------------*/ |
| 259 |
bool ML_NOX::Ml_Nox_LinearSystem::destroyPreconditioner() const |
| 260 |
{ |
| 261 |
// do nothing, don't want NOX to destroy my preconditioner, as |
| 262 |
// I prefer doing it myself (using an offset for recomputing the preconditioner) |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
/*----------------------------------------------------------------------* |
| 267 |
| getJacobianOperator (public) m.gee 12/04| |
| 268 |
*----------------------------------------------------------------------*/ |
| 269 |
const Epetra_Operator& ML_NOX::Ml_Nox_LinearSystem::getJacobianOperator() const |
| 270 |
{ |
| 271 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::getJacobianOperator:\n" |
| 272 |
<< "**ERR**: not impl.\n" |
| 273 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 274 |
return J_; |
| 275 |
} |
| 276 |
|
| 277 |
/*----------------------------------------------------------------------* |
| 278 |
| getJacobianOperator (public) m.gee 12/04| |
| 279 |
*----------------------------------------------------------------------*/ |
| 280 |
Epetra_Operator& ML_NOX::Ml_Nox_LinearSystem::getJacobianOperator() |
| 281 |
{ |
| 282 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::getJacobianOperator:\n" |
| 283 |
<< "**ERR**: not impl.\n" |
| 284 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 285 |
return J_; |
| 286 |
} |
| 287 |
|
| 288 |
/*----------------------------------------------------------------------* |
| 289 |
| getGeneratedPrecOperator (public) m.gee 12/04| |
| 290 |
*----------------------------------------------------------------------*/ |
| 291 |
const Epetra_Operator& ML_NOX::Ml_Nox_LinearSystem::getGeneratedPrecOperator() const |
| 292 |
{ |
| 293 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::getGeneratedPrecOperator:\n" |
| 294 |
<< "**ERR**: not impl.\n" |
| 295 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 296 |
return *Precptr_; |
| 297 |
} |
| 298 |
|
| 299 |
/*----------------------------------------------------------------------* |
| 300 |
| getGeneratedPrecOperator (public) m.gee 12/04| |
| 301 |
*----------------------------------------------------------------------*/ |
| 302 |
Epetra_Operator& ML_NOX::Ml_Nox_LinearSystem::getGeneratedPrecOperator() |
| 303 |
{ |
| 304 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::getGeneratedPrecOperator:\n" |
| 305 |
<< "**ERR**: not impl.\n" |
| 306 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 307 |
return *Precptr_; |
| 308 |
} |
| 309 |
|
| 310 |
/*----------------------------------------------------------------------* |
| 311 |
| setJacobianOperatorForSolve (public) m.gee 12/04| |
| 312 |
*----------------------------------------------------------------------*/ |
| 313 |
void ML_NOX::Ml_Nox_LinearSystem::setJacobianOperatorForSolve(const Epetra_Operator& solveJacOp) |
| 314 |
{ |
| 315 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::setJacobianOperatorForSolve:\n" |
| 316 |
<< "**ERR**: not impl.\n" |
| 317 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
/*----------------------------------------------------------------------* |
| 322 |
| setPrecOperatorForSolve (public) m.gee 08/05| |
| 323 |
*----------------------------------------------------------------------*/ |
| 324 |
void ML_NOX::Ml_Nox_LinearSystem::setPrecOperatorForSolve(const Epetra_Operator& solvePrecOp) |
| 325 |
{ |
| 326 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::setPrecOperatorForSolve:\n" |
| 327 |
<< "**ERR**: not impl.\n" |
| 328 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
/*----------------------------------------------------------------------* |
| 333 |
| applyJacobian (public) m.gee 12/04| |
| 334 |
*----------------------------------------------------------------------*/ |
| 335 |
bool ML_NOX::Ml_Nox_LinearSystem::applyJacobian(const NOX::Epetra::Vector& input, |
| 336 |
NOX::Epetra::Vector& result) const |
| 337 |
{ |
| 338 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyJacobian:\n" |
| 339 |
<< "**ERR**: not supposed to be called???????\n" |
| 340 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
/*----------------------------------------------------------------------* |
| 345 |
| applyJacobianTranspose (public) m.gee 12/04| |
| 346 |
*----------------------------------------------------------------------*/ |
| 347 |
bool ML_NOX::Ml_Nox_LinearSystem::applyJacobianTranspose(const NOX::Epetra::Vector& input, |
| 348 |
NOX::Epetra::Vector& result) const |
| 349 |
{ |
| 350 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyJacobianTranspose:\n" |
| 351 |
<< "**ERR**: not supposed to be called???????\n" |
| 352 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
/*----------------------------------------------------------------------* |
| 357 |
| applyJacobianInverse (public) m.gee 12/04| |
| 358 |
*----------------------------------------------------------------------*/ |
| 359 |
bool ML_NOX::Ml_Nox_LinearSystem::applyJacobianInverse(NOX::Parameter::List ¶ms, |
| 360 |
const NOX::Epetra::Vector &input, |
| 361 |
NOX::Epetra::Vector &result) |
| 362 |
{ |
| 363 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::applyJacobianInverse:\n" |
| 364 |
<< "**ERR**: not supposed to be called???????\n" |
| 365 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
/*----------------------------------------------------------------------* |
| 370 |
| resetScaling (public) m.gee 12/04| |
| 371 |
*----------------------------------------------------------------------*/ |
| 372 |
void ML_NOX::Ml_Nox_LinearSystem::resetScaling(NOX::EpetraNew::Scaling& scalingObject) |
| 373 |
{ |
| 374 |
cout << "**ERR**: ML_Epetra::Ml_Nox_LinearSystem::resetScaling:\n" |
| 375 |
<< "**ERR**: not supposed to be called???????\n" |
| 376 |
<< "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1; |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
#endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) |