| 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 |
#include "Basis.H" |
| 18 |
#include <math.h> |
| 19 |
|
| 20 |
#include "Problem_Manager.H" |
| 21 |
#include "Burgers.H" |
| 22 |
|
| 23 |
// Constructor - creates the Epetra objects (maps and vectors) |
| 24 |
Burgers::Burgers(Epetra_Comm& comm, int numGlobalNodes, std::string name_) : |
| 25 |
GenericEpetraProblem(comm, numGlobalNodes, name_), |
| 26 |
xFactor(5.0), |
| 27 |
viscosity(0.100), |
| 28 |
xmin(0.0), |
| 29 |
xmax(1.0), |
| 30 |
dt(1.0e-3) |
| 31 |
{ |
| 32 |
|
| 33 |
// Create the nodal coordinates |
| 34 |
xptr = Teuchos::rcp( new Epetra_Vector(*StandardMap) ); |
| 35 |
double Length= xmax - xmin; |
| 36 |
dx=Length/((double) NumGlobalNodes-1); |
| 37 |
for ( int i = 0; i < NumMyNodes; ++i ) |
| 38 |
(*xptr)[i] = xmin + dx*((double) StandardMap->MinMyGID()+i); |
| 39 |
|
| 40 |
// Create extra vector needed for this transient problem |
| 41 |
oldSolution = new Epetra_Vector(*StandardMap); |
| 42 |
exactSolution = new Epetra_Vector(*StandardMap); |
| 43 |
|
| 44 |
initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap)); |
| 45 |
initializeSoln(); |
| 46 |
|
| 47 |
// Allocate the memory for a matrix dynamically (i.e. the graph is dynamic). |
| 48 |
AA = Teuchos::rcp( new Epetra_CrsGraph(Copy, *StandardMap, 5) ); |
| 49 |
generateGraph(); |
| 50 |
|
| 51 |
// Create a second matrix using graph of first matrix - this creates a |
| 52 |
// static graph so we can refill the new matirx after FillComplete() |
| 53 |
// is called. |
| 54 |
A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA)); |
| 55 |
A->FillComplete(); |
| 56 |
|
| 57 |
// Create the Importer needed for FD coloring |
| 58 |
ColumnToOverlapImporter = new Epetra_Import(A->ColMap(),*OverlapMap); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
// Destructor |
| 63 |
Burgers::~Burgers() |
| 64 |
{ |
| 65 |
delete oldSolution; |
| 66 |
delete exactSolution; |
| 67 |
delete ColumnToOverlapImporter; |
| 68 |
} |
| 69 |
|
| 70 |
// Reset function |
| 71 |
void Burgers::reset(const Epetra_Vector& x) |
| 72 |
{ |
| 73 |
*oldSolution = x; |
| 74 |
} |
| 75 |
|
| 76 |
// Empty Reset function |
| 77 |
void Burgers::reset() |
| 78 |
{ |
| 79 |
std::cout << "WARNING: reset called without passing any update vector !!" |
| 80 |
<< std::endl; |
| 81 |
} |
| 82 |
|
| 83 |
// Set initialSolution to desired initial condition |
| 84 |
bool Burgers::initializeSoln() |
| 85 |
{ |
| 86 |
Epetra_Vector& x = *xptr; |
| 87 |
|
| 88 |
double arg; |
| 89 |
for( int i = 0; i < NumMyNodes; ++i) |
| 90 |
{ |
| 91 |
arg = ( 20.0 * x[i] - 10.0 ) / xFactor; |
| 92 |
(*initialSolution)[i] = (1.0 - ( exp(arg) - exp(-arg) ) / |
| 93 |
( exp(arg) + exp(-arg) )) - 1.0; |
| 94 |
// (*initialSolution)[i] = 1.0*sin(2.0*pi*x[i]); |
| 95 |
} |
| 96 |
|
| 97 |
*oldSolution = *initialSolution; |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
// Matrix and Residual Fills |
| 103 |
bool Burgers::evaluate( |
| 104 |
NOX::Epetra::Interface::Required::FillType fillType, |
| 105 |
const Epetra_Vector* soln, |
| 106 |
Epetra_Vector* rhs) |
| 107 |
{ |
| 108 |
bool fillRes = false; |
| 109 |
bool fillJac = false; |
| 110 |
|
| 111 |
// We currently make no provision for combined Residual and Jacobian fills |
| 112 |
// i.e. it is either or |
| 113 |
|
| 114 |
if( NOX::Epetra::Interface::Required::Jac == fillType ) |
| 115 |
fillJac = true; |
| 116 |
else |
| 117 |
{ |
| 118 |
fillRes = true; |
| 119 |
assert( NULL != rhs ); |
| 120 |
} |
| 121 |
|
| 122 |
int numDep = depProblems.size(); |
| 123 |
|
| 124 |
// Create the overlapped solution and position vectors |
| 125 |
Epetra_Vector u(*OverlapMap); |
| 126 |
Epetra_Vector uold(*OverlapMap); |
| 127 |
std::vector<Epetra_Vector*> dep(numDep); |
| 128 |
for( int i = 0; i < numDep; ++i) |
| 129 |
dep[i] = new Epetra_Vector(*OverlapMap); |
| 130 |
Epetra_Vector xvec(*OverlapMap); |
| 131 |
|
| 132 |
// Export Solution to Overlap vector |
| 133 |
// If the vector to be used in the fill is already in the Overlap form, |
| 134 |
// we simply need to map on-processor from column-space indices to |
| 135 |
// OverlapMap indices. Note that the old solution is simply fixed data that |
| 136 |
// needs to be sent to an OverlapMap (ghosted) vector. The conditional |
| 137 |
// treatment for the current soution vector arises from use of |
| 138 |
// FD coloring in parallel. |
| 139 |
uold.Import(*oldSolution, *Importer, Insert); |
| 140 |
for( int i = 0; i < numDep; ++i ) |
| 141 |
{ |
| 142 |
dep[i]->Import(*( (*(depSolutions.find(depProblems[i]))).second ), |
| 143 |
*Importer, Insert); |
| 144 |
//cout << "depSoln[" << i << "] :" << dep[i] << std::endl; |
| 145 |
} |
| 146 |
xvec.Import(*xptr, *Importer, Insert); |
| 147 |
if( NOX::Epetra::Interface::Required::FD_Res == fillType ) |
| 148 |
// Overlap vector for solution received from FD coloring, so simply reorder |
| 149 |
// on processor |
| 150 |
u.Export(*soln, *ColumnToOverlapImporter, Insert); |
| 151 |
else // Communication to Overlap vector is needed |
| 152 |
u.Import(*soln, *Importer, Insert); |
| 153 |
|
| 154 |
int OverlapNumMyNodes = OverlapMap->NumMyElements(); |
| 155 |
|
| 156 |
int OverlapMinMyNodeGID; |
| 157 |
if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID(); |
| 158 |
else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1; |
| 159 |
|
| 160 |
int row, column; |
| 161 |
double jac; |
| 162 |
double xx[2]; |
| 163 |
double uu[2]; |
| 164 |
double uuold[2]; |
| 165 |
std::vector<double*> ddep(numDep); |
| 166 |
for( int i = 0; i<numDep; i++) |
| 167 |
ddep[i] = new double[2]; |
| 168 |
Basis basis; |
| 169 |
|
| 170 |
int id_temp; // Index for needed dependent Temperature vector |
| 171 |
|
| 172 |
// |
| 173 |
map<string, int>::iterator id_ptr = nameToMyIndex.find("Temperature"); |
| 174 |
if( id_ptr == nameToMyIndex.end() ) { |
| 175 |
std::cout << "WARNING: Burgers (\"" << myName << "\") could not get " |
| 176 |
<< "vector for problem \"Temperature\" !!" << std::endl; |
| 177 |
throw "Burgers ERROR"; |
| 178 |
} |
| 179 |
else |
| 180 |
id_temp = (*id_ptr).second; |
| 181 |
// |
| 182 |
|
| 183 |
// Zero out the objects that will be filled |
| 184 |
if( fillJac ) |
| 185 |
A->PutScalar(0.0); |
| 186 |
if( fillRes ) |
| 187 |
rhs->PutScalar(0.0); |
| 188 |
|
| 189 |
// Loop Over # of Finite Elements on Processor |
| 190 |
for (int ne=0; ne < OverlapNumMyNodes-1; ne++) { |
| 191 |
|
| 192 |
// Loop Over Gauss Points |
| 193 |
for(int gp=0; gp < 2; gp++) { |
| 194 |
// Get the solution and coordinates at the nodes |
| 195 |
xx[0]=xvec[ne]; |
| 196 |
xx[1]=xvec[ne+1]; |
| 197 |
uu[0]=u[ne]; |
| 198 |
uu[1]=u[ne+1]; |
| 199 |
uuold[0]=uold[ne]; |
| 200 |
uuold[1]=uold[ne+1]; |
| 201 |
for( int i = 0; i<numDep; i++ ) |
| 202 |
{ |
| 203 |
ddep[i][0] = (*dep[i])[ne]; |
| 204 |
ddep[i][1] = (*dep[i])[ne+1]; |
| 205 |
} |
| 206 |
// Calculate the basis function at the gauss point |
| 207 |
basis.getBasis(gp, xx, uu, uuold, ddep); |
| 208 |
|
| 209 |
// Loop over Nodes in Element |
| 210 |
for ( int i = 0; i < 2; ++i ) |
| 211 |
{ |
| 212 |
row=OverlapMap->GID(ne+i); |
| 213 |
//printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n", |
| 214 |
// MyPID, row, ne+i,StandardMap.MyGID(row)); |
| 215 |
if (StandardMap->MyGID(row)) { |
| 216 |
if ( fillRes ) |
| 217 |
{ |
| 218 |
(*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+= |
| 219 |
+basis.wt*basis.dx*( |
| 220 |
(basis.uu-basis.uuold)/dt*basis.phi[i] + |
| 221 |
(1.0/(basis.dx*basis.dx))*viscosity*pow(1.0*(basis.ddep[id_temp]-0.2),1.5) |
| 222 |
//(1.0/(basis.dx*basis.dx))*viscosity |
| 223 |
*basis.duu*basis.dphide[i] - |
| 224 |
(1.0/basis.dx)*0.5*basis.uu*basis.uu*basis.dphide[i]); |
| 225 |
} |
| 226 |
} |
| 227 |
// Loop over Trial Functions |
| 228 |
if ( fillRes ) |
| 229 |
{ |
| 230 |
for(int j = 0; j < 2; ++j) |
| 231 |
{ |
| 232 |
if (StandardMap->MyGID(row)) { |
| 233 |
column=OverlapMap->GID(ne+j); |
| 234 |
jac=basis.wt*basis.dx*( |
| 235 |
basis.phi[j]/dt*basis.phi[i] + |
| 236 |
(1.0/(basis.dx*basis.dx))* |
| 237 |
basis.dphide[j]*basis.dphide[i] - |
| 238 |
8.0/xFactor/xFactor* |
| 239 |
(2.0*basis.uu-3.0*basis.uu*basis.uu)* |
| 240 |
basis.phi[j]*basis.phi[i]); |
| 241 |
A->SumIntoGlobalValues(row, 1, &jac, &column); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// Insert Boundary Conditions and modify Jacobian and function (F) |
| 250 |
// U(xmin)=1 |
| 251 |
if( MyPID == 0 ) |
| 252 |
{ |
| 253 |
if( fillRes ) |
| 254 |
(*rhs)[0]= (*soln)[0] - 1.0; |
| 255 |
if( fillJac ) |
| 256 |
{ |
| 257 |
column=0; |
| 258 |
jac=1.0; |
| 259 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 260 |
column=1; |
| 261 |
jac=0.0; |
| 262 |
A->ReplaceGlobalValues(0, 1, &jac, &column); |
| 263 |
} |
| 264 |
} |
| 265 |
// Insert Boundary Conditions and modify Jacobian and function (F) |
| 266 |
// U(xmax)=0 |
| 267 |
if( MyPID == NumProc-1 ) |
| 268 |
{ |
| 269 |
if ( fillRes ) |
| 270 |
(*rhs)[NumMyNodes-1]= (*soln)[OverlapNumMyNodes-1] - (-1.0); |
| 271 |
if( fillJac ) |
| 272 |
{ |
| 273 |
row=NumGlobalNodes-1; |
| 274 |
column=row; |
| 275 |
jac=1.0; |
| 276 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 277 |
column--; |
| 278 |
jac=0.0; |
| 279 |
A->ReplaceGlobalValues(row, 1, &jac, &column); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Sync up processors to be safe |
| 284 |
Comm->Barrier(); |
| 285 |
|
| 286 |
if ( fillJac ) |
| 287 |
A->FillComplete(); |
| 288 |
|
| 289 |
// Cleanup |
| 290 |
for( int i = 0; i < numDep; ++i ) |
| 291 |
{ |
| 292 |
delete [] ddep[i]; |
| 293 |
delete dep[i]; |
| 294 |
} |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
Teuchos::RCP<Epetra_Vector> Burgers::getSolution() |
| 300 |
{ |
| 301 |
return initialSolution; |
| 302 |
} |
| 303 |
|
| 304 |
Epetra_Vector& Burgers::getExactSoln(double time) |
| 305 |
{ |
| 306 |
Epetra_Vector& x = *xptr; |
| 307 |
|
| 308 |
for(int i=0; i<NumMyNodes; i++) |
| 309 |
(*exactSolution)[i] = (1.0 - tanh( (x[i]-2.0*time/xFactor)/xFactor )) / 2.0; |
| 310 |
|
| 311 |
return *exactSolution; |
| 312 |
} |
| 313 |
|
| 314 |
Epetra_Vector& Burgers::getOldSoln() |
| 315 |
{ |
| 316 |
return *oldSolution; |
| 317 |
} |
| 318 |
|
| 319 |
void Burgers::generateGraph() |
| 320 |
{ |
| 321 |
|
| 322 |
// Declare required variables |
| 323 |
int i; |
| 324 |
int row, column; |
| 325 |
int OverlapNumMyNodes = OverlapMap->NumMyElements(); |
| 326 |
int OverlapMinMyGID; |
| 327 |
if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID(); |
| 328 |
else OverlapMinMyGID = StandardMap->MinMyGID()-1; |
| 329 |
|
| 330 |
// Loop Over # of Finite Elements on Processor |
| 331 |
for (int ne=0; ne < OverlapNumMyNodes-1; ne++) { |
| 332 |
|
| 333 |
// Loop over Nodes in Element |
| 334 |
for (i=0; i< 2; i++) { |
| 335 |
row=OverlapMap->GID(ne+i); |
| 336 |
|
| 337 |
// Loop over Trial Functions |
| 338 |
for( int j = 0; j < 2; ++j ) |
| 339 |
{ |
| 340 |
// If this row is owned by current processor, add the index |
| 341 |
if (StandardMap->MyGID(row)) |
| 342 |
{ |
| 343 |
column=OverlapMap->GID(ne+j); |
| 344 |
AA->InsertGlobalIndices(row, 1, &column); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
AA->FillComplete(); |
| 350 |
} |