| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// LOCA: Library of Continuation Algorithms Package |
| 4 |
// |
| 5 |
// Copyright 2001-2005 NTESS and the LOCA 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 |
#include "Teuchos_Assert.hpp" |
| 18 |
#include "Basis.H" |
| 19 |
|
| 20 |
#include "Pitchfork_FiniteElementProblem.H" |
| 21 |
|
| 22 |
// Constructor - creates the Epetra objects (maps and vectors) |
| 23 |
Pitchfork_FiniteElementProblem::Pitchfork_FiniteElementProblem( |
| 24 |
int numGlobalElements, |
| 25 |
Epetra_Comm& comm) : |
| 26 |
flag(F_ONLY), |
| 27 |
StandardMap(NULL), |
| 28 |
OverlapMap(NULL), |
| 29 |
Importer(NULL), |
| 30 |
initialSolution(NULL), |
| 31 |
rhs(NULL), |
| 32 |
AA(NULL), |
| 33 |
A(NULL), |
| 34 |
Comm(&comm), |
| 35 |
MyPID(0), |
| 36 |
NumProc(0), |
| 37 |
NumMyElements(0), |
| 38 |
NumGlobalElements(numGlobalElements), |
| 39 |
lambda(0.0), |
| 40 |
alpha(0.0), |
| 41 |
beta(0.0) |
| 42 |
{ |
| 43 |
|
| 44 |
// Commonly used variables |
| 45 |
int i; |
| 46 |
MyPID = Comm->MyPID(); // Process ID |
| 47 |
NumProc = Comm->NumProc(); // Total number of processes |
| 48 |
|
| 49 |
// Construct a Source Map that puts approximately the same |
| 50 |
// Number of equations on each processor in uniform global ordering |
| 51 |
StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm); |
| 52 |
|
| 53 |
// Get the number of elements owned by this processor |
| 54 |
NumMyElements = StandardMap->NumMyElements(); |
| 55 |
|
| 56 |
// Construct an overlaped map for the finite element fill ************* |
| 57 |
// For single processor jobs, the overlap and standard map are the same |
| 58 |
if (NumProc == 1) { |
| 59 |
OverlapMap = new Epetra_Map(*StandardMap); |
| 60 |
} else { |
| 61 |
|
| 62 |
int OverlapNumMyElements; |
| 63 |
int OverlapMinMyGID; |
| 64 |
OverlapNumMyElements = NumMyElements + 2; |
| 65 |
if ((MyPID == 0) || (MyPID == NumProc - 1)) |
| 66 |
OverlapNumMyElements --; |
| 67 |
|
| 68 |
if (MyPID==0) |
| 69 |
OverlapMinMyGID = StandardMap->MinMyGID(); |
| 70 |
else |
| 71 |
OverlapMinMyGID = StandardMap->MinMyGID() - 1; |
| 72 |
|
| 73 |
int* OverlapMyGlobalElements = new int[OverlapNumMyElements]; |
| 74 |
|
| 75 |
for (i = 0; i < OverlapNumMyElements; i ++) |
| 76 |
OverlapMyGlobalElements[i] = OverlapMinMyGID + i; |
| 77 |
|
| 78 |
OverlapMap = new Epetra_Map(-1, OverlapNumMyElements, |
| 79 |
OverlapMyGlobalElements, 0, *Comm); |
| 80 |
|
| 81 |
delete [] OverlapMyGlobalElements; |
| 82 |
|
| 83 |
} // End Overlap map construction ************************************* |
| 84 |
|
| 85 |
// Construct Linear Objects |
| 86 |
Importer = new Epetra_Import(*OverlapMap, *StandardMap); |
| 87 |
initialSolution = new Epetra_Vector(*StandardMap); |
| 88 |
AA = new Epetra_CrsGraph(Copy, *StandardMap, 5); |
| 89 |
|
| 90 |
// Allocate the memory for a matrix dynamically (i.e. the graph is dynamic). |
| 91 |
generateGraph(*AA); |
| 92 |
|
| 93 |
// Create a second matrix using graph of first matrix - this creates a |
| 94 |
// static graph so we can refill the new matirx after FillComplete() |
| 95 |
// is called. |
| 96 |
A = new Epetra_CrsMatrix (Copy, *AA); |
| 97 |
A->FillComplete(); |
| 98 |
|
| 99 |
// Set default bifurcation values |
| 100 |
lambda = -2.25; |
| 101 |
alpha = 1.0; |
| 102 |
beta = 0.0; |
| 103 |
} |
| 104 |
|
| 105 |
// Destructor |
| 106 |
Pitchfork_FiniteElementProblem::~Pitchfork_FiniteElementProblem() |
| 107 |
{ |
| 108 |
delete AA; |
| 109 |
delete A; |
| 110 |
delete initialSolution; |
| 111 |
delete Importer; |
| 112 |
delete OverlapMap; |
| 113 |
delete StandardMap; |
| 114 |
} |
| 115 |
|
| 116 |
// Matrix and Residual Fills |
| 117 |
bool |
| 118 |
Pitchfork_FiniteElementProblem::evaluate(FillType f, |
| 119 |
const Epetra_Vector* soln, |
| 120 |
Epetra_Vector* tmp_rhs, |
| 121 |
Epetra_RowMatrix* tmp_matrix, |
| 122 |
double jac_coeff, |
| 123 |
double mass_coeff) |
| 124 |
{ |
| 125 |
flag = f; |
| 126 |
|
| 127 |
// Set the incoming linear objects |
| 128 |
if (flag == F_ONLY) { |
| 129 |
rhs = tmp_rhs; |
| 130 |
} else if (flag == MATRIX_ONLY) { |
| 131 |
A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix); |
| 132 |
assert(A != NULL); |
| 133 |
} else if (flag == ALL) { |
| 134 |
rhs = tmp_rhs; |
| 135 |
A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix); |
| 136 |
assert(A != NULL); |
| 137 |
} else { |
| 138 |
std::cout << "ERROR: Pitchfork_FiniteElementProblem::fillMatrix() - FillType flag is broken" << std::endl; |
| 139 |
throw; |
| 140 |
} |
| 141 |
|
| 142 |
// Create the overlapped solution and position vectors |
| 143 |
Epetra_Vector u(*OverlapMap); |
| 144 |
Epetra_Vector x(*OverlapMap); |
| 145 |
|
| 146 |
// Export Solution to Overlap vector |
| 147 |
u.Import(*soln, *Importer, Insert); |
| 148 |
|
| 149 |
// Declare required variables |
| 150 |
int i,j,ierr; |
| 151 |
int OverlapNumMyElements = OverlapMap->NumMyElements(); |
| 152 |
|
| 153 |
int OverlapMinMyGID; |
| 154 |
if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID(); |
| 155 |
else OverlapMinMyGID = StandardMap->MinMyGID()-1; |
| 156 |
|
| 157 |
int row, column; |
| 158 |
double jac; |
| 159 |
double xx[2]; |
| 160 |
double uu[2]; |
| 161 |
Basis basis; |
| 162 |
|
| 163 |
// Create the nodal coordinates |
| 164 |
double Length=2.0; |
| 165 |
double dx=Length/((double) NumGlobalElements-1); |
| 166 |
for (i=0; i < OverlapNumMyElements; i++) { |
| 167 |
x[i]=-1.0 + dx*((double) OverlapMinMyGID+i); |
| 168 |
} |
| 169 |
|
| 170 |
// Zero out the objects that will be filled |
| 171 |
if ((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 172 |
i = A->PutScalar(0.0); |
| 173 |
assert(i == 0); |
| 174 |
} |
| 175 |
if ((flag == F_ONLY) || (flag == ALL)) { |
| 176 |
i = rhs->PutScalar(0.0); |
| 177 |
assert(i == 0); |
| 178 |
} |
| 179 |
|
| 180 |
// Loop Over # of Finite Elements on Processor |
| 181 |
for (int ne=0; ne < OverlapNumMyElements-1; ne++) { |
| 182 |
|
| 183 |
// Loop Over Gauss Points |
| 184 |
for(int gp=0; gp < 2; gp++) { |
| 185 |
// Get the solution and coordinates at the nodes |
| 186 |
xx[0]=x[ne]; |
| 187 |
xx[1]=x[ne+1]; |
| 188 |
uu[0]=u[ne]; |
| 189 |
uu[1]=u[ne+1]; |
| 190 |
// Calculate the basis function at the gauss point |
| 191 |
basis.getBasis(gp, xx, uu); |
| 192 |
|
| 193 |
// Loop over Nodes in Element |
| 194 |
for (i=0; i< 2; i++) { |
| 195 |
row=OverlapMap->GID(ne+i); |
| 196 |
//printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n", |
| 197 |
// MyPID, row, ne+i,StandardMap.MyGID(row)); |
| 198 |
if (StandardMap->MyGID(row)) { |
| 199 |
if ((flag == F_ONLY) || (flag == ALL)) { |
| 200 |
(*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+= |
| 201 |
+basis.wt*basis.dx |
| 202 |
*((-1.0/(basis.dx*basis.dx))*basis.duu* |
| 203 |
basis.dphide[i]-source_term(basis.uu)*basis.phi[i]); |
| 204 |
} |
| 205 |
} |
| 206 |
// Loop over Trial Functions |
| 207 |
if ((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 208 |
for(j=0;j < 2; j++) { |
| 209 |
if (StandardMap->MyGID(row)) { |
| 210 |
column=OverlapMap->GID(ne+j); |
| 211 |
jac=jac_coeff*basis.wt*basis.dx* |
| 212 |
((-1.0/(basis.dx*basis.dx))*basis.dphide[j]*basis.dphide[i] |
| 213 |
-source_deriv(basis.uu)*basis.phi[j]*basis.phi[i]) + |
| 214 |
mass_coeff*basis.wt*basis.dx*basis.phi[j]*basis.phi[i]; |
| 215 |
ierr=A->SumIntoGlobalValues(row, 1, &jac, &column); |
| 216 |
TEUCHOS_ASSERT(ierr == 0); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Insert Boundary Conditions and modify Jacobian and function (F) |
| 225 |
// U(-1)=beta |
| 226 |
if (MyPID==0) { |
| 227 |
if ((flag == F_ONLY) || (flag == ALL)) |
| 228 |
(*rhs)[0]= (*soln)[0] - beta; |
| 229 |
if ((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 230 |
column=0; |
| 231 |
jac=1.0*jac_coeff; |
| 232 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 233 |
column=1; |
| 234 |
jac=0.0*jac_coeff; |
| 235 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// U(1)=beta |
| 240 |
if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 ) { |
| 241 |
int lastDof = StandardMap->LID(StandardMap->MaxAllGID()); |
| 242 |
if ((flag == F_ONLY) || (flag == ALL)) |
| 243 |
(*rhs)[lastDof] = (*soln)[lastDof] - beta; |
| 244 |
if ((flag == MATRIX_ONLY) || (flag == ALL)) { |
| 245 |
int row = StandardMap->MaxAllGID(); |
| 246 |
column = row; |
| 247 |
jac=1.0*jac_coeff; |
| 248 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 249 |
column=row-1; |
| 250 |
jac=0.0*jac_coeff; |
| 251 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 252 |
} |
| 253 |
} |
| 254 |
// Sync up processors to be safe |
| 255 |
Comm->Barrier(); |
| 256 |
|
| 257 |
A->FillComplete(); |
| 258 |
|
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
Epetra_Vector& |
| 263 |
Pitchfork_FiniteElementProblem::getSolution() |
| 264 |
{ |
| 265 |
return *initialSolution; |
| 266 |
} |
| 267 |
|
| 268 |
Epetra_CrsMatrix& |
| 269 |
Pitchfork_FiniteElementProblem::getJacobian() |
| 270 |
{ |
| 271 |
return *A; |
| 272 |
} |
| 273 |
|
| 274 |
bool |
| 275 |
Pitchfork_FiniteElementProblem::setParameter(std::string label, double value) |
| 276 |
{ |
| 277 |
if (label == "lambda") |
| 278 |
lambda = value; |
| 279 |
else if (label == "alpha") |
| 280 |
alpha = value; |
| 281 |
else if (label == "beta") |
| 282 |
beta = value; |
| 283 |
else if (label == "Homotopy Continuation Parameter") { |
| 284 |
// do nothing for now |
| 285 |
} |
| 286 |
else { |
| 287 |
// do nothing (may be a constraint parameter that we don't know about) |
| 288 |
} |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
Epetra_CrsGraph& |
| 293 |
Pitchfork_FiniteElementProblem::generateGraph(Epetra_CrsGraph& AAA) |
| 294 |
{ |
| 295 |
|
| 296 |
// Declare required variables |
| 297 |
int i,j; |
| 298 |
int row, column; |
| 299 |
int OverlapNumMyElements = OverlapMap->NumMyElements(); |
| 300 |
|
| 301 |
// Loop Over # of Finite Elements on Processor |
| 302 |
for (int ne=0; ne < OverlapNumMyElements-1; ne++) { |
| 303 |
|
| 304 |
// Loop over Nodes in Element |
| 305 |
for (i=0; i< 2; i++) { |
| 306 |
row=OverlapMap->GID(ne+i); |
| 307 |
|
| 308 |
// Loop over Trial Functions |
| 309 |
for(j=0;j < 2; j++) { |
| 310 |
|
| 311 |
// If this row is owned by current processor, add the index |
| 312 |
if (StandardMap->MyGID(row)) { |
| 313 |
column=OverlapMap->GID(ne+j); |
| 314 |
AAA.InsertGlobalIndices(row, 1, &column); |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
AAA.FillComplete(); |
| 320 |
return AAA; |
| 321 |
} |
| 322 |
|
| 323 |
double |
| 324 |
Pitchfork_FiniteElementProblem::source_term(double x) { |
| 325 |
return lambda*x - alpha*x*x + beta*x*x*x; |
| 326 |
} |
| 327 |
|
| 328 |
double |
| 329 |
Pitchfork_FiniteElementProblem::source_deriv(double x) { |
| 330 |
return lambda - 2.0*alpha*x + 3.0*beta*x*x; |
| 331 |
} |