| 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 "petscksp.h" |
| 12 |
|
| 13 |
#include "DennisSchnabel.H" |
| 14 |
|
| 15 |
// Constructor - creates the Petsc objects (maps and vectors) |
| 16 |
DennisSchnabel::DennisSchnabel(int numGlobalElements) : |
| 17 |
NumGlobalElements(numGlobalElements) |
| 18 |
{ |
| 19 |
|
| 20 |
// Commonly used variables |
| 21 |
int ierr; |
| 22 |
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&MyPID); |
| 23 |
ierr = MPI_Comm_size(PETSC_COMM_WORLD,&NumProc); |
| 24 |
|
| 25 |
// Construct a Source Map that puts approximately the same |
| 26 |
// Number of equations on each processor in uniform global ordering |
| 27 |
//StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm); |
| 28 |
|
| 29 |
// Get the number of elements owned by this processor |
| 30 |
//NumMyElements = StandardMap->NumMyElements(); |
| 31 |
|
| 32 |
// Construct an overlaped map for the fill calls ********************** |
| 33 |
/* The overlap map is needed for multiprocessor jobs. The unknowns |
| 34 |
* are stored in a distributed vector where each node owns one unknown. |
| 35 |
* During a function or Jacobian evaluation, each processor needs to have |
| 36 |
* both of the unknown values. The overlapped vector can get this data |
| 37 |
* by importing the owned values from the other node to this overlapped map. |
| 38 |
* Actual solves must be done using the Standard map where everything is |
| 39 |
* distributed. |
| 40 |
*/ |
| 41 |
// For single processor jobs, the overlap and standard map are the same |
| 42 |
if (NumProc == 1) { |
| 43 |
//OverlapMap = new Epetra_Map(*StandardMap); |
| 44 |
} |
| 45 |
else { |
| 46 |
|
| 47 |
//int OverlapNumMyElements = 2; |
| 48 |
//int OverlapMyGlobalElements[2]; |
| 49 |
|
| 50 |
//for (i = 0; i < OverlapNumMyElements; i ++) |
| 51 |
// OverlapMyGlobalElements[i] = i; |
| 52 |
|
| 53 |
//OverlapMap = new Epetra_Map(-1, OverlapNumMyElements, |
| 54 |
// OverlapMyGlobalElements, 0, *Comm); |
| 55 |
} // End Overlap map construction ************************************* |
| 56 |
|
| 57 |
// Construct Linear Objects |
| 58 |
initialSolution = new Vec; |
| 59 |
ierr = VecCreate(PETSC_COMM_WORLD, initialSolution); |
| 60 |
ierr = VecSetSizes(*initialSolution, PETSC_DECIDE, 2); |
| 61 |
ierr = VecSetFromOptions(*initialSolution); |
| 62 |
|
| 63 |
A = new Mat; |
| 64 |
ierr = MatCreate(PETSC_COMM_SELF,A); |
| 65 |
ierr = MatSetSizes(*A,PETSC_DECIDE,PETSC_DECIDE,2,2); |
| 66 |
ierr = MatSetFromOptions(*A); |
| 67 |
ierr = MatSetUp(*A); |
| 68 |
|
| 69 |
// Create Mapping for overlap solution vector using Petsc IS |
| 70 |
overlapSolution = new Vec; |
| 71 |
VecCreateSeq(PETSC_COMM_SELF,2,overlapSolution); |
| 72 |
int indexMap[] = {0, 1}; |
| 73 |
IS from, to; |
| 74 |
ISCreateGeneral(PETSC_COMM_WORLD,2,indexMap,PETSC_COPY_VALUES,&from); |
| 75 |
ISCreateGeneral(PETSC_COMM_WORLD,2,indexMap,PETSC_COPY_VALUES,&to); |
| 76 |
petscMap = new VecScatter; |
| 77 |
VecScatterCreate(*initialSolution, from, *overlapSolution, to, petscMap); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
// Destructor |
| 82 |
DennisSchnabel::~DennisSchnabel() |
| 83 |
{ |
| 84 |
delete A; |
| 85 |
delete initialSolution; |
| 86 |
} |
| 87 |
|
| 88 |
// Matrix and Residual Fills |
| 89 |
bool DennisSchnabel::evaluate(FillType f, |
| 90 |
const Vec* soln, |
| 91 |
Vec* tmp_rhs, |
| 92 |
Mat* tmp_matrix) |
| 93 |
{ |
| 94 |
flag = f; |
| 95 |
|
| 96 |
// Set the incoming linear objects |
| 97 |
if (flag == RHS_ONLY) { |
| 98 |
rhs = tmp_rhs; |
| 99 |
} |
| 100 |
else if (flag == MATRIX_ONLY) { |
| 101 |
A = tmp_matrix; |
| 102 |
} |
| 103 |
else if (flag == ALL) { |
| 104 |
rhs = tmp_rhs; |
| 105 |
A = tmp_matrix; |
| 106 |
} |
| 107 |
else { |
| 108 |
std::cout << "ERROR: DennisSchnabel::fillMatrix() - No such flag as " |
| 109 |
<< flag << std::endl; |
| 110 |
throw; |
| 111 |
} |
| 112 |
|
| 113 |
// Create the overlapped solution |
| 114 |
VecScatterBegin(*petscMap, *soln, *overlapSolution, INSERT_VALUES, SCATTER_FORWARD); |
| 115 |
VecScatterEnd(*petscMap, *soln, *overlapSolution, INSERT_VALUES, SCATTER_FORWARD); |
| 116 |
// Export Solution to Overlap vector so we have all unknowns required |
| 117 |
// for function and Jacobian evaluations. |
| 118 |
double* u; |
| 119 |
VecGetArray(*overlapSolution, &u); |
| 120 |
// for(int i=0; i<2; i++) |
| 121 |
// std::cout << i << " :\t" << u[i] << std::endl; |
| 122 |
// cin.get(); |
| 123 |
|
| 124 |
|
| 125 |
// Declare required variables |
| 126 |
int ierr; |
| 127 |
double resid[2]; |
| 128 |
int* column = new int[2]; |
| 129 |
column[0] = 0; |
| 130 |
column[1] = 1; |
| 131 |
|
| 132 |
// Begin RHS fill |
| 133 |
if((flag == RHS_ONLY) || (flag == ALL)) { |
| 134 |
|
| 135 |
// Zero out the RHS vector |
| 136 |
//ierr = VecSet(&zero, *rhs); |
| 137 |
//ierr = VecAssemblyBegin(*rhs); |
| 138 |
//ierr = VecAssemblyEnd(*rhs); |
| 139 |
|
| 140 |
// Processor 0 always fills the first equation. |
| 141 |
if(NumProc==1) { |
| 142 |
resid[0] = u[0]*u[0] + u[1]*u[1] - 2.; |
| 143 |
resid[1] = exp(u[0]-1.) + u[1]*u[1]*u[1] - 2.; |
| 144 |
ierr = VecSetValues(*rhs,2,column,resid,INSERT_VALUES);CHKERRQ(ierr); |
| 145 |
} |
| 146 |
else { // NumProc==2 |
| 147 |
double value; |
| 148 |
if (MyPID==0) { |
| 149 |
value = u[0]*u[0] + u[1]*u[1] - 2.; |
| 150 |
} |
| 151 |
else { |
| 152 |
value = exp(u[0]-1.) + u[1]*u[1]*u[1] - 2.; |
| 153 |
} |
| 154 |
ierr = VecSetValues(*rhs,1,&column[MyPID],&value, |
| 155 |
INSERT_VALUES);CHKERRQ(ierr); |
| 156 |
} |
| 157 |
/* |
| 158 |
Assemble vector, using the 2-step process: |
| 159 |
VecAssemblyBegin(), VecAssemblyEnd() |
| 160 |
Computations can be done while messages are in transition, |
| 161 |
by placing code between these two statements. |
| 162 |
*/ |
| 163 |
ierr = VecAssemblyBegin(*rhs);CHKERRQ(ierr); |
| 164 |
ierr = VecAssemblyEnd(*rhs);CHKERRQ(ierr); |
| 165 |
|
| 166 |
// Check resid values from Vec rhs |
| 167 |
// Vec mask; |
| 168 |
// double dmask[] = {1.0, 0.0}; |
| 169 |
// ierr = VecCreate(PETSC_COMM_WORLD, &mask);CHKERRQ(ierr); |
| 170 |
// ierr = VecSetSizes(mask, PETSC_DECIDE, 2);CHKERRQ(ierr); |
| 171 |
// ierr = VecSetFromOptions(mask);CHKERRQ(ierr); |
| 172 |
// ierr = VecSetValues(mask,2,column,dmask, INSERT_VALUES);CHKERRQ(ierr); |
| 173 |
// ierr = VecAssemblyBegin(*rhs);CHKERRQ(ierr); |
| 174 |
// ierr = VecAssemblyEnd(*rhs);CHKERRQ(ierr); |
| 175 |
// |
| 176 |
// std::cout << "Checking residuals .... " << std::endl; |
| 177 |
// double val1, val2; |
| 178 |
// VecDot(*rhs, mask, &val1); |
| 179 |
// dmask[0] = 0.0; |
| 180 |
// dmask[1] = 1.0; |
| 181 |
// ierr = VecSetValues(mask,2,column,dmask, INSERT_VALUES);CHKERRQ(ierr); |
| 182 |
// ierr = VecAssemblyBegin(*rhs);CHKERRQ(ierr); |
| 183 |
// ierr = VecAssemblyEnd(*rhs);CHKERRQ(ierr); |
| 184 |
// VecDot(*rhs, mask, &val2); |
| 185 |
// std::cout << "rhs vals:\t" << val1 << "\t" << val2 << std::endl; |
| 186 |
// cin.get(); |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
// Here we fill a Jacobian double array on all active procs and |
| 191 |
// insert values |
| 192 |
double* jac = new double[4]; |
| 193 |
|
| 194 |
// The matrix is 2 x 2 and will always be 0 and 1 regardless of |
| 195 |
// the coordinates being local or global. |
| 196 |
|
| 197 |
// Begin Jacobian fill |
| 198 |
if((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 199 |
|
| 200 |
// Zero out Jacobian |
| 201 |
//i=A->PutScalar(0.0); |
| 202 |
|
| 203 |
jac[0] = 2.*u[0]; |
| 204 |
jac[1] = 2.*u[1]; |
| 205 |
jac[2] = exp(u[0]-1.); |
| 206 |
jac[3] = 3.*u[1]*u[1]; |
| 207 |
ierr = MatSetValues(*A,2,column,2,column,jac,INSERT_VALUES);CHKERRQ(ierr); |
| 208 |
/* |
| 209 |
Assemble matrix, using the 2-step process: |
| 210 |
MatAssemblyBegin(), MatAssemblyEnd() |
| 211 |
Computations can be done while messages are in transition |
| 212 |
by placing code between these two statements. |
| 213 |
*/ |
| 214 |
ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); |
| 215 |
ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); |
| 216 |
|
| 217 |
delete [] column; |
| 218 |
delete [] jac; |
| 219 |
} |
| 220 |
|
| 221 |
VecRestoreArray(*overlapSolution, &u); |
| 222 |
|
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
Vec& DennisSchnabel::getSolution() |
| 227 |
{ |
| 228 |
return *initialSolution; |
| 229 |
} |
| 230 |
|
| 231 |
Mat& DennisSchnabel::getJacobian() |
| 232 |
{ |
| 233 |
return *A; |
| 234 |
} |
| 235 |
|