| 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 |
// NOX Test Lacplace 2D Problem |
| 11 |
// ---------------------------- |
| 12 |
// Simple nonlinear PDE problem. |
| 13 |
// This file provides the problem |
| 14 |
// |
| 15 |
// -\Delta u + \lambda e^u = 0 in \Omega = (0,1) \times (0,1) |
| 16 |
// u = 0 on \partial \Omega |
| 17 |
// |
| 18 |
// for use as a driver for various nox-epetra tests |
| 19 |
|
| 20 |
#include "Epetra_ConfigDefs.h" |
| 21 |
#ifdef HAVE_MPI |
| 22 |
#include <mpi.h> |
| 23 |
#include "Epetra_MpiComm.h" |
| 24 |
#else |
| 25 |
#include "Epetra_SerialComm.h" |
| 26 |
#endif |
| 27 |
#include "Epetra_Map.h" |
| 28 |
#include "Epetra_Vector.h" |
| 29 |
#include "Epetra_RowMatrix.h" |
| 30 |
#include "Epetra_CrsMatrix.h" |
| 31 |
#include "NOX.H" |
| 32 |
#include "NOX_Epetra_Interface_Required.H" |
| 33 |
#include "NOX_Epetra_Interface_Jacobian.H" |
| 34 |
#include "NOX_Epetra_Interface_Preconditioner.H" |
| 35 |
#include "NOX_Epetra_LinearSystem_AztecOO.H" |
| 36 |
|
| 37 |
namespace Laplace2D { |
| 38 |
|
| 39 |
// this is required to know the number of lower, upper, left and right |
| 40 |
// node for each node of the Cartesian grid (composed by nx \timex ny |
| 41 |
// elements) |
| 42 |
|
| 43 |
void |
| 44 |
get_myNeighbours( const int i, const int nx, const int ny, |
| 45 |
int & left, int & right, |
| 46 |
int & lower, int & upper); |
| 47 |
|
| 48 |
|
| 49 |
// This function creates a CrsMatrix, whose elements corresponds |
| 50 |
// to the discretization of a Laplacian over a Cartesian grid, |
| 51 |
// with nx grid point along the x-axis and and ny grid points |
| 52 |
// along the y-axis. For the sake of simplicity, I suppose that |
| 53 |
// all the nodes in the matrix are internal nodes (Dirichlet |
| 54 |
// boundary nodes are supposed to have been already condensated) |
| 55 |
|
| 56 |
Epetra_CrsMatrix * |
| 57 |
CreateLaplacian( const int nx, const int ny, const Epetra_Comm * Comm); |
| 58 |
|
| 59 |
// ========================================================================== |
| 60 |
// This class contians the main definition of the nonlinear problem at |
| 61 |
// hand. A method is provided to compute F(x) for a given x, and another |
| 62 |
// method to update the entries of the Jacobian matrix, for a given x. |
| 63 |
// As the Jacobian matrix J can be written as |
| 64 |
// J = L + diag(lambda*exp(x[i])), |
| 65 |
// where L corresponds to the discretization of a Laplacian, and diag |
| 66 |
// is a diagonal matrix with lambda*exp(x[i]). Basically, to update |
| 67 |
// the jacobian we simply update the diagonal entries. Similarly, to compute |
| 68 |
// F(x), we reset J to be equal to L, then we multiply it by the |
| 69 |
// (distributed) vector x, then we add the diagonal contribution |
| 70 |
// ========================================================================== |
| 71 |
|
| 72 |
} // namespace Laplace2D |
| 73 |
|
| 74 |
|
| 75 |
class PDEProblem |
| 76 |
{ |
| 77 |
|
| 78 |
public: |
| 79 |
|
| 80 |
// constructor. Requires the number of nodes along the x-axis |
| 81 |
// and y-axis, the value of lambda, and the Epetra_Communicator |
| 82 |
// (to define a Map, which is a linear map in this case) |
| 83 |
PDEProblem(const int nx, const int ny, const double lambda, |
| 84 |
const Epetra_Comm * Comm); |
| 85 |
|
| 86 |
// destructor |
| 87 |
~PDEProblem(); |
| 88 |
|
| 89 |
// compute F(x) |
| 90 |
void ComputeF(const Epetra_Vector & x, Epetra_Vector & f); |
| 91 |
|
| 92 |
// update the Jacobian matrix for a given x |
| 93 |
void UpdateJacobian(const Epetra_Vector & x); |
| 94 |
|
| 95 |
// returns a pointer to the internally stored matrix |
| 96 |
Epetra_CrsMatrix * GetMatrix() |
| 97 |
{ |
| 98 |
return Matrix_; |
| 99 |
} |
| 100 |
|
| 101 |
private: |
| 102 |
|
| 103 |
int nx_, ny_; |
| 104 |
double hx_, hy_; |
| 105 |
Epetra_CrsMatrix * Matrix_; |
| 106 |
double lambda_; |
| 107 |
|
| 108 |
}; /* class PDEProblem */ |
| 109 |
|
| 110 |
// ========================================================================== |
| 111 |
// This is the main NOX class for this example. Here we define |
| 112 |
// the interface between the nonlinear problem at hand, and NOX. |
| 113 |
// The constructor accepts a PDEProblem object. Using a pointer |
| 114 |
// to this object, we can update the Jacobian and compute F(x), |
| 115 |
// using the definition of our problem. This interface is bit |
| 116 |
// crude: For instance, no PrecMatrix nor Preconditioner is specified. |
| 117 |
// ========================================================================== |
| 118 |
|
| 119 |
class SimpleProblemInterface : public NOX::Epetra::Interface::Required , |
| 120 |
public NOX::Epetra::Interface::Jacobian , |
| 121 |
public NOX::Epetra::Interface::Preconditioner |
| 122 |
{ |
| 123 |
|
| 124 |
public: |
| 125 |
|
| 126 |
//! Constructor |
| 127 |
SimpleProblemInterface( PDEProblem * Problem ) : |
| 128 |
Problem_(Problem) {}; |
| 129 |
|
| 130 |
//! Destructor |
| 131 |
~SimpleProblemInterface() |
| 132 |
{ |
| 133 |
}; |
| 134 |
|
| 135 |
bool computeF(const Epetra_Vector & x, Epetra_Vector & f, |
| 136 |
NOX::Epetra::Interface::Required::FillType F ) |
| 137 |
{ |
| 138 |
Problem_->ComputeF(x,f); |
| 139 |
return true; |
| 140 |
}; |
| 141 |
|
| 142 |
bool computeJacobian(const Epetra_Vector & x, Epetra_Operator & Jac) |
| 143 |
{ |
| 144 |
|
| 145 |
Problem_->UpdateJacobian(x); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
bool computePreconditioner(const Epetra_Vector & x, Epetra_Operator & Op, Teuchos::ParameterList *) |
| 150 |
{ |
| 151 |
|
| 152 |
Problem_->UpdateJacobian(x); |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
bool computePrecMatrix(const Epetra_Vector & x, Epetra_RowMatrix & M) |
| 157 |
{ |
| 158 |
std::cout << "*ERR* SimpleProblem::preconditionVector()\n"; |
| 159 |
std::cout << "*ERR* don't use explicit preconditioning" << std::endl; |
| 160 |
throw 1; |
| 161 |
} |
| 162 |
|
| 163 |
private: |
| 164 |
|
| 165 |
PDEProblem * Problem_; |
| 166 |
|
| 167 |
}; /* class SimpleProblemInterface */ |
| 168 |
|