| 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 "Epetra_ConfigDefs.h" |
| 11 |
#ifdef HAVE_MPI |
| 12 |
#include "mpi.h" |
| 13 |
#include "Epetra_MpiComm.h" |
| 14 |
#else |
| 15 |
#include "Epetra_SerialComm.h" |
| 16 |
#endif |
| 17 |
#include "Epetra_Map.h" |
| 18 |
#include "Epetra_Vector.h" |
| 19 |
#include "Epetra_RowMatrix.h" |
| 20 |
#include "Epetra_CrsMatrix.h" |
| 21 |
#include "NOX_Epetra_Interface_Required.H" |
| 22 |
#include "NOX_Epetra_Interface_Jacobian.H" |
| 23 |
|
| 24 |
#include "Laplace2D.H" |
| 25 |
|
| 26 |
// this is required to know the number of lower, upper, left and right |
| 27 |
// node for each node of the Cartesian grid (composed by nx \timex ny |
| 28 |
// elements) |
| 29 |
|
| 30 |
void |
| 31 |
Laplace2D::get_myNeighbours( const int i, const int nx, const int ny, |
| 32 |
int & left, int & right, |
| 33 |
int & lower, int & upper) |
| 34 |
{ |
| 35 |
|
| 36 |
int ix, iy; |
| 37 |
ix = i%nx; |
| 38 |
iy = (i - ix)/nx; |
| 39 |
|
| 40 |
if( ix == 0 ) |
| 41 |
left = -1; |
| 42 |
else |
| 43 |
left = i-1; |
| 44 |
if( ix == nx-1 ) |
| 45 |
right = -1; |
| 46 |
else |
| 47 |
right = i+1; |
| 48 |
if( iy == 0 ) |
| 49 |
lower = -1; |
| 50 |
else |
| 51 |
lower = i-nx; |
| 52 |
if( iy == ny-1 ) |
| 53 |
upper = -1; |
| 54 |
else |
| 55 |
upper = i+nx; |
| 56 |
|
| 57 |
return; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
// This function creates a CrsMatrix, whose elements corresponds |
| 62 |
// to the discretization of a Laplacian over a Cartesian grid, |
| 63 |
// with nx grid point along the x-axis and and ny grid points |
| 64 |
// along the y-axis. For the sake of simplicity, I suppose that |
| 65 |
// all the nodes in the matrix are internal nodes (Dirichlet |
| 66 |
// boundary nodes are supposed to have been already condensated) |
| 67 |
|
| 68 |
Epetra_CrsMatrix * |
| 69 |
Laplace2D::CreateLaplacian( const int nx, const int ny, const Epetra_Comm * Comm) |
| 70 |
{ |
| 71 |
|
| 72 |
int NumGlobalElements = nx * ny; |
| 73 |
|
| 74 |
// create a map |
| 75 |
Epetra_Map * Map = new Epetra_Map(NumGlobalElements,0,*Comm); |
| 76 |
// local number of rows |
| 77 |
int NumMyElements = Map->NumMyElements(); |
| 78 |
// get update list |
| 79 |
int * MyGlobalElements = Map->MyGlobalElements(); |
| 80 |
|
| 81 |
double hx = 1.0/(nx-1); |
| 82 |
double hy = 1.0/(ny-1); |
| 83 |
double off_left = -1.0/(hx*hx); |
| 84 |
double off_right = -1.0/(hx*hx); |
| 85 |
double off_lower = -1.0/(hy*hy); |
| 86 |
double off_upper = -1.0/(hy*hy); |
| 87 |
double diag = 2.0/(hx*hx) + 2.0/(hy*hy); |
| 88 |
|
| 89 |
int left, right, lower, upper; |
| 90 |
|
| 91 |
// a bit overestimated the nonzero per row |
| 92 |
|
| 93 |
Epetra_CrsMatrix * A = new Epetra_CrsMatrix(Copy,*Map,5); |
| 94 |
|
| 95 |
// Add rows one-at-a-time |
| 96 |
|
| 97 |
double * Values = new double[4]; |
| 98 |
int * Indices = new int[4]; |
| 99 |
|
| 100 |
for( int i = 0; i < NumMyElements; ++i ) |
| 101 |
{ |
| 102 |
int NumEntries=0; |
| 103 |
get_myNeighbours( MyGlobalElements[i], nx, ny, left, right, lower, upper ); |
| 104 |
if( left != -1 ) |
| 105 |
{ |
| 106 |
Indices[NumEntries] = left; |
| 107 |
Values[NumEntries] = off_left; |
| 108 |
++NumEntries; |
| 109 |
} |
| 110 |
if( right != -1 ) |
| 111 |
{ |
| 112 |
Indices[NumEntries] = right; |
| 113 |
Values[NumEntries] = off_right; |
| 114 |
++NumEntries; |
| 115 |
} |
| 116 |
if( lower != -1 ) |
| 117 |
{ |
| 118 |
Indices[NumEntries] = lower; |
| 119 |
Values[NumEntries] = off_lower; |
| 120 |
++NumEntries; |
| 121 |
} |
| 122 |
if( upper != -1 ) |
| 123 |
{ |
| 124 |
Indices[NumEntries] = upper; |
| 125 |
Values[NumEntries] = off_upper; |
| 126 |
++NumEntries; |
| 127 |
} |
| 128 |
// put the off-diagonal entries |
| 129 |
A->InsertGlobalValues(MyGlobalElements[i], NumEntries, Values, Indices); |
| 130 |
// Put in the diagonal entry |
| 131 |
A->InsertGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i); |
| 132 |
} |
| 133 |
|
| 134 |
// put matrix in local ordering |
| 135 |
A->FillComplete(); |
| 136 |
|
| 137 |
delete [] Indices; |
| 138 |
delete [] Values; |
| 139 |
delete Map; |
| 140 |
|
| 141 |
return A; |
| 142 |
|
| 143 |
} /* createJacobian */ |
| 144 |
|
| 145 |
// ========================================================================== |
| 146 |
// This class contians the main definition of the nonlinear problem at |
| 147 |
// hand. A method is provided to compute F(x) for a given x, and another |
| 148 |
// method to update the entries of the Jacobian matrix, for a given x. |
| 149 |
// As the Jacobian matrix J can be written as |
| 150 |
// J = L + diag(lambda*exp(x[i])), |
| 151 |
// where L corresponds to the discretization of a Laplacian, and diag |
| 152 |
// is a diagonal matrix with lambda*exp(x[i]). Basically, to update |
| 153 |
// the jacobian we simply update the diagonal entries. Similarly, to compute |
| 154 |
// F(x), we reset J to be equal to L, then we multiply it by the |
| 155 |
// (distributed) vector x, then we add the diagonal contribution |
| 156 |
// ========================================================================== |
| 157 |
|
| 158 |
// constructor. Requires the number of nodes along the x-axis |
| 159 |
// and y-axis, the value of lambda, and the Epetra_Communicator |
| 160 |
// (to define a Map, which is a linear map in this case) |
| 161 |
PDEProblem::PDEProblem(const int nx, const int ny, const double lambda, |
| 162 |
const Epetra_Comm * Comm) : |
| 163 |
nx_(nx), ny_(ny), lambda_(lambda) |
| 164 |
{ |
| 165 |
hx_ = 1.0/(nx_-1); |
| 166 |
hy_ = 1.0/(ny_-1); |
| 167 |
Matrix_ = Laplace2D::CreateLaplacian(nx_,ny_,Comm); |
| 168 |
} |
| 169 |
|
| 170 |
// destructor |
| 171 |
PDEProblem::~PDEProblem() |
| 172 |
{ |
| 173 |
delete Matrix_; |
| 174 |
} |
| 175 |
|
| 176 |
// compute F(x) |
| 177 |
void PDEProblem::ComputeF( const Epetra_Vector & x, Epetra_Vector & f ) |
| 178 |
{ |
| 179 |
// reset diagonal entries |
| 180 |
double diag = 2.0/(hx_*hx_) + 2.0/(hy_*hy_); |
| 181 |
|
| 182 |
int NumMyElements = Matrix_->Map().NumMyElements(); |
| 183 |
// get update list |
| 184 |
int * MyGlobalElements = Matrix_->Map().MyGlobalElements(); |
| 185 |
|
| 186 |
for( int i = 0; i < NumMyElements; ++i ) |
| 187 |
{ |
| 188 |
// Put in the diagonal entry |
| 189 |
Matrix_->ReplaceGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i); |
| 190 |
} |
| 191 |
// matrix-vector product (intra-processes communication occurs in this call) |
| 192 |
Matrix_->Multiply( false, x, f ); |
| 193 |
|
| 194 |
// add diagonal contributions |
| 195 |
for( int i = 0; i < NumMyElements; ++i ) |
| 196 |
{ |
| 197 |
// Put in the diagonal entry |
| 198 |
f[i] += lambda_*exp(x[i]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// update the Jacobian matrix for a given x |
| 203 |
void PDEProblem::UpdateJacobian( const Epetra_Vector & x ) |
| 204 |
{ |
| 205 |
double diag = 2.0/(hx_*hx_) + 2.0/(hy_*hy_); |
| 206 |
|
| 207 |
int NumMyElements = Matrix_->Map().NumMyElements(); |
| 208 |
// get update list |
| 209 |
int * MyGlobalElements = Matrix_->Map().MyGlobalElements(); |
| 210 |
|
| 211 |
for( int i = 0; i < NumMyElements; ++i ) |
| 212 |
{ |
| 213 |
// Put in the diagonal entry |
| 214 |
double newdiag = diag + lambda_*exp(x[i]); |
| 215 |
Matrix_->ReplaceGlobalValues(MyGlobalElements[i], 1, &newdiag, MyGlobalElements+i); |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|