| 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 "NOX_Common.H" |
| 11 |
#include "Epetra_Comm.h" |
| 12 |
#include "Epetra_Map.h" |
| 13 |
#include "Epetra_Vector.h" |
| 14 |
#include "Epetra_Import.h" |
| 15 |
#include "Epetra_CrsGraph.h" |
| 16 |
#include "Epetra_CrsMatrix.h" |
| 17 |
|
| 18 |
#include "DennisSchnabel.H" |
| 19 |
|
| 20 |
// Constructor - creates the Epetra objects (maps and vectors) |
| 21 |
DennisSchnabel::DennisSchnabel(int numGlobalElements, Epetra_Comm& comm) : |
| 22 |
flag(F_ONLY), |
| 23 |
soln(NULL), |
| 24 |
rhs(NULL), |
| 25 |
Comm(&comm), |
| 26 |
NumGlobalElements(numGlobalElements) |
| 27 |
{ |
| 28 |
|
| 29 |
// Commonly used variables |
| 30 |
int i; |
| 31 |
MyPID = Comm->MyPID(); // Process ID |
| 32 |
NumProc = Comm->NumProc(); // Total number of processes |
| 33 |
|
| 34 |
// Construct a Source Map that puts approximately the same |
| 35 |
// Number of equations on each processor in uniform global ordering |
| 36 |
StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm); |
| 37 |
|
| 38 |
// Get the number of elements owned by this processor |
| 39 |
NumMyElements = StandardMap->NumMyElements(); |
| 40 |
|
| 41 |
// Construct an overlaped map for the fill calls ********************** |
| 42 |
/* The overlap map is needed for multiprocessor jobs. The unknowns |
| 43 |
* are stored in a distributed vector where each node owns one unknown. |
| 44 |
* During a function or Jacobian evaluation, each processor needs to have |
| 45 |
* both of the unknown values. The overlapped vector can get this data |
| 46 |
* by importing the owned values from the other node to this overlapped map. |
| 47 |
* Actual solves must be done using the Standard map where everything is |
| 48 |
* distributed. |
| 49 |
*/ |
| 50 |
// For single processor jobs, the overlap and standard map are the same |
| 51 |
if (NumProc == 1) { |
| 52 |
OverlapMap = new Epetra_Map(*StandardMap); |
| 53 |
} |
| 54 |
else { |
| 55 |
|
| 56 |
int OverlapNumMyElements = 2; |
| 57 |
int OverlapMyGlobalElements[2]; |
| 58 |
|
| 59 |
for (i = 0; i < OverlapNumMyElements; i ++) |
| 60 |
OverlapMyGlobalElements[i] = i; |
| 61 |
|
| 62 |
OverlapMap = new Epetra_Map(-1, OverlapNumMyElements, |
| 63 |
OverlapMyGlobalElements, 0, *Comm); |
| 64 |
} // End Overlap map construction ************************************* |
| 65 |
|
| 66 |
// Construct Linear Objects |
| 67 |
Importer = new Epetra_Import(*OverlapMap, *StandardMap); |
| 68 |
initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap)); |
| 69 |
AA = new Epetra_CrsGraph(Copy, *StandardMap, 5); |
| 70 |
|
| 71 |
// Allocate the memory for a matrix dynamically (i.e. the graph is dynamic). |
| 72 |
generateGraph(*AA); |
| 73 |
|
| 74 |
// Use the graph AA to create a Matrix. |
| 75 |
A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA)); |
| 76 |
|
| 77 |
// Transform the global matrix coordinates to local so the matrix can |
| 78 |
// be operated upon. |
| 79 |
A->FillComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
// Destructor |
| 83 |
DennisSchnabel::~DennisSchnabel() |
| 84 |
{ |
| 85 |
delete AA; |
| 86 |
delete Importer; |
| 87 |
delete OverlapMap; |
| 88 |
delete StandardMap; |
| 89 |
} |
| 90 |
|
| 91 |
// Matrix and Residual Fills |
| 92 |
bool DennisSchnabel::evaluate( |
| 93 |
NOX::Epetra::Interface::Required::FillType fillType, |
| 94 |
const Epetra_Vector* soln, |
| 95 |
Epetra_Vector* tmp_rhs) |
| 96 |
{ |
| 97 |
flag = MATRIX_ONLY; |
| 98 |
|
| 99 |
if ( tmp_rhs ) { |
| 100 |
flag = F_ONLY; |
| 101 |
rhs = tmp_rhs; |
| 102 |
} |
| 103 |
|
| 104 |
// Create the overlapped solution |
| 105 |
Epetra_Vector u(*OverlapMap); |
| 106 |
|
| 107 |
// Export Solution to Overlap vector so we have all unknowns required |
| 108 |
// for function and Jacobian evaluations. |
| 109 |
u.Import(*soln, *Importer, Insert); |
| 110 |
|
| 111 |
// Begin F fill |
| 112 |
if((flag == F_ONLY) || (flag == ALL)) { |
| 113 |
|
| 114 |
// Zero out the F vector |
| 115 |
rhs->PutScalar(0.0); |
| 116 |
|
| 117 |
// Processor 0 always fills the first equation. |
| 118 |
if (MyPID==0) { |
| 119 |
(*rhs)[0]=(u[0]*u[0] + u[1]*u[1] - 2.); |
| 120 |
|
| 121 |
// If it's a single processor job, fill the second equation on proc 0. |
| 122 |
if (NumProc==1) |
| 123 |
(*rhs)[1]=(exp(u[0]-1.) + u[1]*u[1]*u[1] - 2.); |
| 124 |
} |
| 125 |
// Multiprocessor job puts the second equation on processor 1. |
| 126 |
else { |
| 127 |
(*rhs)[0]=(exp(u[0]-1.) + u[1]*u[1]*u[1] - 2.); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
int* column = new int[2]; |
| 133 |
double* jac = new double[2]; |
| 134 |
|
| 135 |
// The matrix is 2 x 2 and will always be 0 and 1 regardless of |
| 136 |
// the coordinates being local or global. |
| 137 |
column[0] = 0; |
| 138 |
column[1] = 1; |
| 139 |
|
| 140 |
// Begin Jacobian fill |
| 141 |
if((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 142 |
|
| 143 |
// Zero out Jacobian |
| 144 |
A->PutScalar(0.0); |
| 145 |
|
| 146 |
if (MyPID==0) { |
| 147 |
// Processor 0 always fills the first equation. |
| 148 |
jac[0] = 2.*u[0]; |
| 149 |
jac[1] = 2.*u[1]; |
| 150 |
A->ReplaceGlobalValues(0, 2, jac, column); |
| 151 |
|
| 152 |
// If it's a single processor job, fill the second equation on proc 0. |
| 153 |
if (NumProc==1) { |
| 154 |
jac[0] = exp(u[0]-1.); |
| 155 |
jac[1] = 3.*u[1]*u[1]; |
| 156 |
A->ReplaceGlobalValues(1, 2, jac, column); |
| 157 |
} |
| 158 |
} |
| 159 |
// Multiprocessor job puts the second equation on processor 1. |
| 160 |
else { |
| 161 |
jac[0] = exp(u[0]-1.); |
| 162 |
jac[1] = 3.*u[1]*u[1]; |
| 163 |
A->ReplaceGlobalValues(1, 2, jac, column); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
delete [] column; |
| 168 |
delete [] jac; |
| 169 |
|
| 170 |
// Sync up processors to be safe |
| 171 |
Comm->Barrier(); |
| 172 |
|
| 173 |
// Transform matrix so it can be operated upon. |
| 174 |
A->FillComplete(); |
| 175 |
|
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
Teuchos::RCP<Epetra_Vector> DennisSchnabel::getSolution() |
| 180 |
{ |
| 181 |
return initialSolution; |
| 182 |
} |
| 183 |
|
| 184 |
Teuchos::RCP<Epetra_CrsMatrix> DennisSchnabel::getJacobian() |
| 185 |
{ |
| 186 |
return A; |
| 187 |
} |
| 188 |
|
| 189 |
Epetra_CrsGraph& DennisSchnabel::generateGraph(Epetra_CrsGraph& AA) |
| 190 |
{ |
| 191 |
|
| 192 |
int* index = new int[2]; |
| 193 |
|
| 194 |
if (MyPID==0) { |
| 195 |
index[0]=0; |
| 196 |
index[1]=1; |
| 197 |
AA.InsertGlobalIndices(0, 2, index); |
| 198 |
|
| 199 |
if (NumProc==1) { |
| 200 |
index[0]=0; |
| 201 |
index[1]=1; |
| 202 |
AA.InsertGlobalIndices(1, 2, index); |
| 203 |
} |
| 204 |
} else { |
| 205 |
index[0]=0; |
| 206 |
index[1]=1; |
| 207 |
AA.InsertGlobalIndices(1, 2, index); |
| 208 |
} |
| 209 |
|
| 210 |
delete [] index; |
| 211 |
|
| 212 |
AA.FillComplete(); |
| 213 |
// AA.SortIndices(); |
| 214 |
// AA.RemoveRedundantIndices(); |
| 215 |
return AA; |
| 216 |
} |