| 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 "Basis.H" |
| 18 |
|
| 19 |
#include "Brusselator.H" |
| 20 |
|
| 21 |
// Constructor - creates the Epetra objects (maps and vectors) |
| 22 |
Brusselator::Brusselator(int numGlobalNodes, Epetra_Comm& comm) : |
| 23 |
xmin(0.0), |
| 24 |
xmax(1.0), |
| 25 |
Comm(&comm), |
| 26 |
NumGlobalNodes(numGlobalNodes), |
| 27 |
ColumnToOverlapImporter(0), |
| 28 |
AA(0), |
| 29 |
A(0), |
| 30 |
alpha(0.25), |
| 31 |
beta(1.5), |
| 32 |
D1(1.0/40.0), |
| 33 |
D2(1.0/40.0) |
| 34 |
{ |
| 35 |
|
| 36 |
// Commonly used variables |
| 37 |
int i; |
| 38 |
MyPID = Comm->MyPID(); // Process ID |
| 39 |
NumProc = Comm->NumProc(); // Total number of processes |
| 40 |
|
| 41 |
// Here we assume a 2-species Brusselator model, ie 2 dofs per node |
| 42 |
// Note that this needs to be echoed in thew anonymous enum for NUMSPECIES. |
| 43 |
NumGlobalUnknowns = NumSpecies * NumGlobalNodes; |
| 44 |
|
| 45 |
// Construct a Source Map that puts approximately the same |
| 46 |
// number of equations on each processor |
| 47 |
|
| 48 |
// Begin by distributing nodes fairly equally |
| 49 |
StandardNodeMap = new Epetra_Map(NumGlobalNodes, 0, *Comm); |
| 50 |
|
| 51 |
// Get the number of nodes owned by this processor |
| 52 |
NumMyNodes = StandardNodeMap->NumMyElements(); |
| 53 |
|
| 54 |
// Construct an overlap node map for the finite element fill |
| 55 |
// For single processor jobs, the overlap and standard maps are the same |
| 56 |
if (NumProc == 1) |
| 57 |
OverlapNodeMap = new Epetra_Map(*StandardNodeMap); |
| 58 |
else { |
| 59 |
|
| 60 |
int OverlapNumMyNodes; |
| 61 |
int OverlapMinMyNodeGID; |
| 62 |
|
| 63 |
OverlapNumMyNodes = NumMyNodes + 2; |
| 64 |
if ((MyPID == 0) || (MyPID == NumProc - 1)) |
| 65 |
OverlapNumMyNodes --; |
| 66 |
|
| 67 |
if (MyPID==0) |
| 68 |
OverlapMinMyNodeGID = StandardNodeMap->MinMyGID(); |
| 69 |
else |
| 70 |
OverlapMinMyNodeGID = StandardNodeMap->MinMyGID() - 1; |
| 71 |
|
| 72 |
int* OverlapMyGlobalNodes = new int[OverlapNumMyNodes]; |
| 73 |
|
| 74 |
for (i = 0; i < OverlapNumMyNodes; i ++) |
| 75 |
OverlapMyGlobalNodes[i] = OverlapMinMyNodeGID + i; |
| 76 |
|
| 77 |
OverlapNodeMap = new Epetra_Map(-1, OverlapNumMyNodes, |
| 78 |
OverlapMyGlobalNodes, 0, *Comm); |
| 79 |
|
| 80 |
delete [] OverlapMyGlobalNodes; |
| 81 |
} // End Overlap node map construction ******************************** |
| 82 |
|
| 83 |
// Now create the unknowns maps from the node maps |
| 84 |
NumMyUnknowns = NumSpecies * NumMyNodes; |
| 85 |
int* StandardMyGlobalUnknowns = new int[NumMyUnknowns]; |
| 86 |
for (int k=0; k<NumSpecies; k++) |
| 87 |
for (i=0; i<NumMyNodes; i++) { |
| 88 |
// For now, we employ an interleave of unknowns |
| 89 |
StandardMyGlobalUnknowns[ NumSpecies * i + k ] = |
| 90 |
NumSpecies * StandardNodeMap->GID(i) + k; |
| 91 |
} |
| 92 |
|
| 93 |
StandardMap = new Epetra_Map(-1, NumMyUnknowns, StandardMyGlobalUnknowns, |
| 94 |
0, *Comm); |
| 95 |
delete [] StandardMyGlobalUnknowns; |
| 96 |
|
| 97 |
assert(StandardMap->NumGlobalElements() == NumGlobalUnknowns); |
| 98 |
|
| 99 |
if (NumProc == 1) { |
| 100 |
OverlapMap = new Epetra_Map(*StandardMap); |
| 101 |
} |
| 102 |
else { |
| 103 |
int OverlapNumMyNodes = OverlapNodeMap->NumMyElements(); |
| 104 |
int OverlapNumMyUnknowns = NumSpecies * OverlapNumMyNodes; |
| 105 |
int* OverlapMyGlobalUnknowns = new int[OverlapNumMyUnknowns]; |
| 106 |
for (int k=0; k<NumSpecies; k++) |
| 107 |
for (i=0; i<OverlapNumMyNodes; i++) |
| 108 |
OverlapMyGlobalUnknowns[ NumSpecies * i + k ] = |
| 109 |
NumSpecies * OverlapNodeMap->GID(i) + k; |
| 110 |
|
| 111 |
OverlapMap = new Epetra_Map(-1, OverlapNumMyUnknowns, |
| 112 |
OverlapMyGlobalUnknowns, 0, *Comm); |
| 113 |
delete [] OverlapMyGlobalUnknowns; |
| 114 |
|
| 115 |
} // End Overlap unknowns map construction *************************** |
| 116 |
|
| 117 |
// Construct Linear Objects |
| 118 |
Importer = new Epetra_Import(*OverlapMap, *StandardMap); |
| 119 |
nodeImporter = new Epetra_Import(*OverlapNodeMap, *StandardNodeMap); |
| 120 |
initialSolution = new Epetra_Vector(*StandardMap); |
| 121 |
AA = new Epetra_CrsGraph(Copy, *StandardMap, 0); |
| 122 |
|
| 123 |
// Allocate the memory for a matrix dynamically (i.e. the graph is dynamic). |
| 124 |
generateGraph(*AA); |
| 125 |
|
| 126 |
// Create the matrix |
| 127 |
A = new Epetra_CrsMatrix (Copy, *AA); |
| 128 |
A->FillComplete(); |
| 129 |
|
| 130 |
// Create the nodal coordinates |
| 131 |
xptr = new Epetra_Vector(*StandardNodeMap); |
| 132 |
double Length= xmax - xmin; |
| 133 |
dx=Length/((double) NumGlobalNodes-1); |
| 134 |
for (i=0; i < NumMyNodes; i++) { |
| 135 |
(*xptr)[i]=xmin + dx*((double) StandardNodeMap->MinMyGID()+i); |
| 136 |
} |
| 137 |
|
| 138 |
initializeSoln(); |
| 139 |
} |
| 140 |
|
| 141 |
// Destructor |
| 142 |
Brusselator::~Brusselator() |
| 143 |
{ |
| 144 |
delete AA; |
| 145 |
delete xptr; |
| 146 |
delete initialSolution; |
| 147 |
delete Importer; |
| 148 |
delete OverlapMap; |
| 149 |
delete StandardMap; |
| 150 |
delete StandardNodeMap; |
| 151 |
delete OverlapNodeMap; |
| 152 |
delete nodeImporter; |
| 153 |
delete A; |
| 154 |
} |
| 155 |
|
| 156 |
// Set initialSolution to desired initial condition |
| 157 |
void Brusselator::initializeSoln() |
| 158 |
{ |
| 159 |
Epetra_Vector& soln = *initialSolution; |
| 160 |
Epetra_Vector& x = *xptr; |
| 161 |
for (int i=0; i<x.MyLength(); i++) { |
| 162 |
soln[2*i] = alpha; |
| 163 |
soln[2*i+1] = beta/alpha; |
| 164 |
} |
| 165 |
|
| 166 |
//soln.PutScalar(0.0); |
| 167 |
} |
| 168 |
|
| 169 |
void Brusselator::setParameters(double alpha_, double beta_, double D1_, |
| 170 |
double D2_) |
| 171 |
{ |
| 172 |
alpha = alpha_; |
| 173 |
beta = beta_; |
| 174 |
D1 = D1_; |
| 175 |
D2 = D2_; |
| 176 |
} |
| 177 |
|
| 178 |
// Matrix and Residual Fills |
| 179 |
bool Brusselator::evaluate(FillType fType, |
| 180 |
const Epetra_Vector* soln, |
| 181 |
Epetra_Vector* tmp_rhs, |
| 182 |
Epetra_RowMatrix* tmp_matrix, |
| 183 |
double jac_coeff, double mass_coeff) |
| 184 |
{ |
| 185 |
|
| 186 |
// Set the incoming linear objects |
| 187 |
if (fType == F_ONLY) { |
| 188 |
rhs = tmp_rhs; |
| 189 |
} |
| 190 |
else if (fType == MATRIX_ONLY) { |
| 191 |
A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix); |
| 192 |
} |
| 193 |
else if (fType == ALL) { |
| 194 |
rhs = tmp_rhs; |
| 195 |
A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix); |
| 196 |
} |
| 197 |
else { |
| 198 |
std::cout << "Brusselator::evaluate() - FillType fType is broken" << std::endl; |
| 199 |
throw; |
| 200 |
} |
| 201 |
|
| 202 |
// Create the overlapped solution and position vectors |
| 203 |
Epetra_Vector u(*OverlapMap); |
| 204 |
Epetra_Vector xvec(*OverlapNodeMap); |
| 205 |
|
| 206 |
// Export Solution to Overlap vector |
| 207 |
xvec.Import(*xptr, *nodeImporter, Insert); |
| 208 |
u.Import(*soln, *Importer, Insert); |
| 209 |
|
| 210 |
// Declare required variables |
| 211 |
int i,j; |
| 212 |
int OverlapNumMyNodes = OverlapNodeMap->NumMyElements(); |
| 213 |
|
| 214 |
int row1, row2, column1, column2; |
| 215 |
double term1, term2; |
| 216 |
double jac11, jac12, jac21, jac22; |
| 217 |
double xx[2]; |
| 218 |
double uu[2*NumSpecies] = {0.0}; |
| 219 |
Basis basis(NumSpecies); |
| 220 |
|
| 221 |
|
| 222 |
// Zero out the objects that will be filled |
| 223 |
if ((fType == MATRIX_ONLY) || (fType == ALL)) A->PutScalar(0.0); |
| 224 |
if ((fType == F_ONLY) || (fType == ALL)) rhs->PutScalar(0.0); |
| 225 |
|
| 226 |
// Loop Over # of Finite Elements on Processor |
| 227 |
for (int ne=0; ne < OverlapNumMyNodes - 1; ne++) { |
| 228 |
|
| 229 |
// Loop Over Gauss Points |
| 230 |
for(int gp=0; gp < 2; gp++) { |
| 231 |
// Get the solution and coordinates at the nodes |
| 232 |
xx[0]=xvec[ne]; |
| 233 |
xx[1]=xvec[ne+1]; |
| 234 |
for (int k=0; k<NumSpecies; k++) { |
| 235 |
uu[NumSpecies * 0 + k] = u[NumSpecies * ne + k]; |
| 236 |
uu[NumSpecies * 1 + k] = u[NumSpecies * (ne+1) + k]; |
| 237 |
} |
| 238 |
// Calculate the basis function at the gauss point |
| 239 |
basis.getBasis(gp, xx, uu); |
| 240 |
|
| 241 |
// Loop over Nodes in Element |
| 242 |
for (i=0; i< 2; i++) { |
| 243 |
row1=OverlapMap->GID(NumSpecies * (ne+i)); |
| 244 |
row2=OverlapMap->GID(NumSpecies * (ne+i) + 1); |
| 245 |
term1 = basis.wt*basis.dx |
| 246 |
*((1.0/(basis.dx*basis.dx))*D1*basis.duu[0]*basis.dphide[i] |
| 247 |
+ basis.phi[i] * ( -alpha + (beta+1.0)*basis.uu[0] |
| 248 |
- basis.uu[0]*basis.uu[0]*basis.uu[1]) ); |
| 249 |
term2 = basis.wt*basis.dx |
| 250 |
*((1.0/(basis.dx*basis.dx))*D2*basis.duu[1]*basis.dphide[i] |
| 251 |
+ basis.phi[i] * ( -beta*basis.uu[0] |
| 252 |
+ basis.uu[0]*basis.uu[0]*basis.uu[1]) ); |
| 253 |
//printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n", |
| 254 |
// MyPID, row, ne+i,StandardMap.MyGID(row)); |
| 255 |
if ((fType == F_ONLY) || (fType == ALL)) { |
| 256 |
if (StandardMap->MyGID(row1)) { |
| 257 |
(*rhs)[StandardMap->LID(OverlapMap->GID(NumSpecies*(ne+i)))]+= |
| 258 |
term1; |
| 259 |
(*rhs)[StandardMap->LID(OverlapMap->GID(NumSpecies*(ne+i)+1))]+= |
| 260 |
term2; |
| 261 |
} |
| 262 |
} |
| 263 |
// Loop over Trial Functions |
| 264 |
if ((fType == MATRIX_ONLY) || (fType == ALL)) { |
| 265 |
for(j=0;j < 2; j++) { |
| 266 |
if (StandardMap->MyGID(row1)) { |
| 267 |
column1=OverlapMap->GID(NumSpecies * (ne+j)); |
| 268 |
column2=OverlapMap->GID(NumSpecies * (ne+j) + 1); |
| 269 |
jac11=basis.wt*basis.dx |
| 270 |
*(-mass_coeff*basis.phi[j]*basis.phi[i] |
| 271 |
+jac_coeff* |
| 272 |
((1.0/(basis.dx*basis.dx))*D1*basis.dphide[j]* |
| 273 |
basis.dphide[i] + |
| 274 |
basis.phi[i]*((beta+1.0)*basis.phi[j] |
| 275 |
-2.0*basis.uu[0]*basis.phi[j]*basis.uu[1]))); |
| 276 |
jac12=basis.wt*basis.dx |
| 277 |
*(jac_coeff*basis.phi[i] |
| 278 |
*(-basis.uu[0]*basis.uu[0]*basis.phi[j])); |
| 279 |
jac21=basis.wt*basis.dx |
| 280 |
*(jac_coeff*basis.phi[i] |
| 281 |
*(-beta*basis.phi[j] |
| 282 |
+ 2.0*basis.uu[0]*basis.phi[j]*basis.uu[1]) ); |
| 283 |
jac22=basis.wt*basis.dx |
| 284 |
*(-mass_coeff*basis.phi[j]*basis.phi[i] |
| 285 |
+jac_coeff* |
| 286 |
((1.0/(basis.dx*basis.dx))*D2*basis.dphide[j]* |
| 287 |
basis.dphide[i] |
| 288 |
+ basis.phi[i] * basis.uu[0]*basis.uu[0]*basis.phi[j] )); |
| 289 |
A->SumIntoGlobalValues(row1, 1, &jac11, &column1); |
| 290 |
column1++; |
| 291 |
A->SumIntoGlobalValues(row1, 1, &jac12, &column1); |
| 292 |
A->SumIntoGlobalValues(row2, 1, &jac22, &column2); |
| 293 |
column2--; |
| 294 |
A->SumIntoGlobalValues(row2, 1, &jac21, &column2); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
// Insert Boundary Conditions and modify Jacobian and function (F) |
| 303 |
// T(0)=alpha, C(0) = beta/alpha |
| 304 |
if (MyPID==0) { |
| 305 |
if ((fType == F_ONLY) || (fType == ALL)) { |
| 306 |
(*rhs)[0]= (*soln)[0] - alpha; |
| 307 |
(*rhs)[1]= (*soln)[1] - beta/alpha; |
| 308 |
} |
| 309 |
if ((fType == MATRIX_ONLY) || (fType == ALL)) { |
| 310 |
int column=0; |
| 311 |
double jac=1.0*jac_coeff; |
| 312 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 313 |
column++; |
| 314 |
A->ReplaceGlobalValues(1, 1, &jac, &column); |
| 315 |
jac=0.0; |
| 316 |
column=0; |
| 317 |
A->ReplaceGlobalValues(1, 1, &jac, &column); |
| 318 |
column++; |
| 319 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 320 |
column++; |
| 321 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 322 |
A->ReplaceGlobalValues(1, 1, &jac, &column); |
| 323 |
column++; |
| 324 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 325 |
A->ReplaceGlobalValues(1, 1, &jac, &column); |
| 326 |
} |
| 327 |
} |
| 328 |
// T(1)=alpha, C(1) = beta/alpha |
| 329 |
if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 ) { |
| 330 |
int lastDof = StandardMap->LID(StandardMap->MaxAllGID()); |
| 331 |
if ((fType == F_ONLY) || (fType == ALL)) { |
| 332 |
(*rhs)[lastDof - 1] = (*soln)[lastDof - 1] - alpha; |
| 333 |
(*rhs)[lastDof] = (*soln)[lastDof] - beta/alpha; |
| 334 |
} |
| 335 |
if ((fType == MATRIX_ONLY) || (fType == ALL)) { |
| 336 |
int row=StandardMap->MaxAllGID() - 1; |
| 337 |
int column = row; |
| 338 |
double jac = 1.0*jac_coeff; |
| 339 |
A->ReplaceGlobalValues(row++, 1, &jac, &column); |
| 340 |
column++; |
| 341 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 342 |
jac=0.0; |
| 343 |
row = column - 1; |
| 344 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 345 |
column--; |
| 346 |
A->ReplaceGlobalValues(row+1, 1, &jac, &column); |
| 347 |
column--; |
| 348 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 349 |
A->ReplaceGlobalValues(row+1, 1, &jac, &column); |
| 350 |
column--; |
| 351 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 352 |
A->ReplaceGlobalValues(row+1, 1, &jac, &column); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// Sync up processors to be safe |
| 357 |
Comm->Barrier(); |
| 358 |
|
| 359 |
A->FillComplete(); |
| 360 |
|
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
Epetra_Vector& Brusselator::getSolution() |
| 365 |
{ |
| 366 |
return *initialSolution; |
| 367 |
} |
| 368 |
|
| 369 |
Epetra_CrsMatrix& Brusselator::getJacobian() |
| 370 |
{ |
| 371 |
return *A; |
| 372 |
} |
| 373 |
|
| 374 |
Epetra_Vector& Brusselator::getMesh() |
| 375 |
{ |
| 376 |
return *xptr; |
| 377 |
} |
| 378 |
|
| 379 |
Epetra_CrsGraph& Brusselator::getGraph() |
| 380 |
{ |
| 381 |
return *AA; |
| 382 |
} |
| 383 |
|
| 384 |
Epetra_CrsGraph& Brusselator::generateGraph(Epetra_CrsGraph& AA) |
| 385 |
{ |
| 386 |
|
| 387 |
// Declare required variables |
| 388 |
int i,j; |
| 389 |
int row, column; |
| 390 |
int OverlapNumMyNodes = OverlapNodeMap->NumMyElements(); |
| 391 |
|
| 392 |
// Loop Over # of Finite Elements on Processor |
| 393 |
for (int ne=0; ne < OverlapNumMyNodes-1; ne++) { |
| 394 |
|
| 395 |
// Loop over Nodes in Element |
| 396 |
for (i=0; i<2; i++) { |
| 397 |
|
| 398 |
// If this node is owned by current processor, add indices |
| 399 |
if (StandardNodeMap->MyGID(OverlapNodeMap->GID(ne+i))) { |
| 400 |
|
| 401 |
// Loop over unknowns in Node |
| 402 |
for (int k=0; k<NumSpecies; k++) { |
| 403 |
row=OverlapMap->GID( NumSpecies*(ne+i) + k); // Interleave scheme |
| 404 |
|
| 405 |
// Loop over supporting nodes |
| 406 |
for(j=0; j<2; j++) { |
| 407 |
|
| 408 |
// Loop over unknowns at supporting nodes |
| 409 |
for (int m=0; m<NumSpecies; m++) { |
| 410 |
column=OverlapMap->GID( NumSpecies*(ne+j) + m); |
| 411 |
//printf("\t\tWould like to insert -> (%d, %d)\n",row,column); |
| 412 |
AA.InsertGlobalIndices(row, 1, &column); |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
AA.FillComplete(); |
| 420 |
// AA.SortIndices(); |
| 421 |
// AA.RemoveRedundantIndices(); |
| 422 |
return AA; |
| 423 |
} |
| 424 |
|