| 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 |
// NOX include (for iostream, cmath, etc...) |
| 11 |
#include "NOX_Common.H" |
| 12 |
|
| 13 |
#include "Teuchos_ParameterList.hpp" |
| 14 |
|
| 15 |
// Class Definition |
| 16 |
#include "1DfemModelEvaluator.H" |
| 17 |
|
| 18 |
// Epetra includes |
| 19 |
#include "Epetra_Comm.h" |
| 20 |
#include "Epetra_Map.h" |
| 21 |
#include "Epetra_Vector.h" |
| 22 |
#include "Epetra_Import.h" |
| 23 |
#include "Epetra_Operator.h" |
| 24 |
#include "Epetra_CrsGraph.h" |
| 25 |
#include "Epetra_RowMatrix.h" |
| 26 |
#include "Epetra_CrsMatrix.h" |
| 27 |
|
| 28 |
ModelEvaluatorInterface::ModelEvaluatorInterface(int numGlobalElements, |
| 29 |
Epetra_Comm& comm, |
| 30 |
double xmin_, |
| 31 |
double xmax_) : |
| 32 |
NumGlobalElements(numGlobalElements), |
| 33 |
NumMyElements(0), // gets set after map creation |
| 34 |
MyPID(comm.MyPID()), |
| 35 |
NumProc(comm.NumProc()), |
| 36 |
xmin(xmin_), |
| 37 |
xmax(xmax_), |
| 38 |
factor(1.0), |
| 39 |
Comm(&comm), |
| 40 |
Importer(0), |
| 41 |
rhs(0), |
| 42 |
Graph(0) |
| 43 |
{ |
| 44 |
|
| 45 |
// Construct a Source Map that puts approximately the same |
| 46 |
// Number of equations on each processor in uniform global ordering |
| 47 |
StandardMap = Teuchos::rcp(new Epetra_Map(NumGlobalElements, 0, *Comm)); |
| 48 |
|
| 49 |
// Get the number of elements owned by this processor |
| 50 |
NumMyElements = StandardMap->NumMyElements(); |
| 51 |
|
| 52 |
// Construct an overlaped map for the finite element fill ************* |
| 53 |
// For single processor jobs, the overlap and standard map are the same |
| 54 |
if (NumProc == 1) { |
| 55 |
OverlapMap = Teuchos::rcp(new Epetra_Map(*StandardMap)); |
| 56 |
} else { |
| 57 |
|
| 58 |
int OverlapNumMyElements; |
| 59 |
int OverlapMinMyGID; |
| 60 |
OverlapNumMyElements = NumMyElements + 2; |
| 61 |
if ((MyPID == 0) || (MyPID == NumProc - 1)) |
| 62 |
OverlapNumMyElements --; |
| 63 |
|
| 64 |
if (MyPID==0) |
| 65 |
OverlapMinMyGID = StandardMap->MinMyGID(); |
| 66 |
else |
| 67 |
OverlapMinMyGID = StandardMap->MinMyGID() - 1; |
| 68 |
|
| 69 |
int* OverlapMyGlobalElements = new int[OverlapNumMyElements]; |
| 70 |
|
| 71 |
for (int i = 0; i < OverlapNumMyElements; i ++) |
| 72 |
OverlapMyGlobalElements[i] = OverlapMinMyGID + i; |
| 73 |
|
| 74 |
OverlapMap = Teuchos::rcp(new Epetra_Map(-1, OverlapNumMyElements, |
| 75 |
OverlapMyGlobalElements, 0, |
| 76 |
*Comm)); |
| 77 |
|
| 78 |
delete [] OverlapMyGlobalElements; |
| 79 |
|
| 80 |
} // End Overlap map construction ************************************* |
| 81 |
|
| 82 |
// Construct Linear Objects |
| 83 |
Importer = new Epetra_Import(*OverlapMap, *StandardMap); |
| 84 |
initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap)); |
| 85 |
|
| 86 |
// Assign non-zero entries in the graph |
| 87 |
createGraph(); |
| 88 |
|
| 89 |
// Construct a matrix |
| 90 |
jacobian = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *Graph)); |
| 91 |
|
| 92 |
// Clean-up |
| 93 |
jacobian->FillComplete(); |
| 94 |
|
| 95 |
// Create the nodal coordinates |
| 96 |
xptr = Teuchos::rcp(new Epetra_Vector(*StandardMap)); |
| 97 |
double Length = xmax - xmin; |
| 98 |
double dx = Length/((double) NumGlobalElements-1); |
| 99 |
for (int i=0; i < NumMyElements; i++) { |
| 100 |
(*xptr)[i] = xmin + dx*((double) StandardMap->MinMyGID()+i); |
| 101 |
} |
| 102 |
|
| 103 |
initializeSoln(); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
ModelEvaluatorInterface::~ModelEvaluatorInterface() |
| 108 |
{ |
| 109 |
delete Graph; |
| 110 |
delete Importer; |
| 111 |
} |
| 112 |
|
| 113 |
void ModelEvaluatorInterface::evalModel(const InArgs& inArgs, |
| 114 |
const OutArgs& outArgs) const |
| 115 |
{ |
| 116 |
Epetra_RowMatrix* tmp = dynamic_cast<Epetra_RowMatrix*> |
| 117 |
(outArgs.get_W().get()); |
| 118 |
|
| 119 |
this->evaluate( inArgs.get_x().get(), outArgs.get_f().get(), tmp ); |
| 120 |
|
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
bool ModelEvaluatorInterface::evaluate(const Epetra_Vector* soln, |
| 125 |
Epetra_Vector* tmp_rhs, |
| 126 |
Epetra_RowMatrix* tmp_matrix) const |
| 127 |
{ |
| 128 |
//Determine what to fill (F or Jacobian) |
| 129 |
bool fillF = false; |
| 130 |
bool fillMatrix = false; |
| 131 |
if (tmp_rhs != 0) { |
| 132 |
fillF = true; |
| 133 |
rhs = tmp_rhs; |
| 134 |
} |
| 135 |
if (tmp_matrix != 0) { |
| 136 |
fillMatrix = true; |
| 137 |
} |
| 138 |
|
| 139 |
// Create the overlapped solution and position vectors |
| 140 |
Epetra_Vector u(*OverlapMap); |
| 141 |
Epetra_Vector x(*OverlapMap); |
| 142 |
|
| 143 |
// Export Solution to Overlap vector |
| 144 |
u.Import(*soln, *Importer, Insert); |
| 145 |
x.Import(*xptr, *Importer, Insert); |
| 146 |
|
| 147 |
// Declare required variables |
| 148 |
int OverlapNumMyElements = OverlapMap->NumMyElements(); |
| 149 |
|
| 150 |
int row, column; |
| 151 |
double jac; |
| 152 |
double xx[2]; |
| 153 |
double uu[2]; |
| 154 |
Basis basis; |
| 155 |
|
| 156 |
// Zero out the objects that will be filled |
| 157 |
if (fillF) |
| 158 |
rhs->PutScalar(0.0); |
| 159 |
if (fillMatrix) |
| 160 |
jacobian->PutScalar(0.0); |
| 161 |
|
| 162 |
// Loop Over # of Finite Elements on Processor |
| 163 |
for (int ne=0; ne < OverlapNumMyElements-1; ne++) { |
| 164 |
|
| 165 |
// Loop Over Gauss Points |
| 166 |
for(int gp=0; gp < 2; gp++) { |
| 167 |
// Get the solution and coordinates at the nodes |
| 168 |
xx[0]=x[ne]; |
| 169 |
xx[1]=x[ne+1]; |
| 170 |
uu[0]=u[ne]; |
| 171 |
uu[1]=u[ne+1]; |
| 172 |
// Calculate the basis function at the gauss point |
| 173 |
basis.computeBasis(gp, xx, uu); |
| 174 |
|
| 175 |
// Loop over Nodes in Element |
| 176 |
for (int i=0; i< 2; i++) { |
| 177 |
row=OverlapMap->GID(ne+i); |
| 178 |
//printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n", |
| 179 |
// MyPID, row, ne+i,StandardMap.MyGID(row)); |
| 180 |
if (StandardMap->MyGID(row)) { |
| 181 |
if (fillF) { |
| 182 |
(*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+= |
| 183 |
+basis.wt*basis.dx |
| 184 |
*((1.0/(basis.dx*basis.dx))*basis.duu* |
| 185 |
basis.dphide[i]+factor*basis.uu*basis.uu*basis.phi[i]); |
| 186 |
} |
| 187 |
} |
| 188 |
// Loop over Trial Functions |
| 189 |
if (fillMatrix) { |
| 190 |
for(int j=0;j < 2; j++) { |
| 191 |
if (StandardMap->MyGID(row)) { |
| 192 |
column=OverlapMap->GID(ne+j); |
| 193 |
jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))* |
| 194 |
basis.dphide[j]*basis.dphide[i] |
| 195 |
+2.0*factor*basis.uu*basis.phi[j]* |
| 196 |
basis.phi[i]); |
| 197 |
jacobian->SumIntoGlobalValues(row, 1, &jac, &column); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// Insert Boundary Conditions and modify Jacobian and function (F) |
| 206 |
// U(0)=1 |
| 207 |
if (MyPID==0) { |
| 208 |
if (fillF) |
| 209 |
(*rhs)[0]= (*soln)[0] - 1.0; |
| 210 |
if (fillMatrix) { |
| 211 |
column=0; |
| 212 |
jac=1.0; |
| 213 |
jacobian->ReplaceGlobalValues(0, 1, &jac, &column); |
| 214 |
column=1; |
| 215 |
jac=0.0; |
| 216 |
jacobian->ReplaceGlobalValues(0, 1, &jac, &column); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// Sync up processors to be safe |
| 221 |
Comm->Barrier(); |
| 222 |
|
| 223 |
jacobian->FillComplete(); |
| 224 |
|
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
Teuchos::RCP<const Epetra_Map> ModelEvaluatorInterface:: |
| 229 |
get_x_map() const |
| 230 |
{ |
| 231 |
return StandardMap; |
| 232 |
} |
| 233 |
|
| 234 |
Teuchos::RCP<const Epetra_Map> ModelEvaluatorInterface:: |
| 235 |
get_f_map() const |
| 236 |
{ |
| 237 |
return StandardMap; |
| 238 |
} |
| 239 |
|
| 240 |
Teuchos::RCP<const Epetra_Vector> ModelEvaluatorInterface:: |
| 241 |
get_x_init() const |
| 242 |
{ |
| 243 |
return initialSolution; |
| 244 |
} |
| 245 |
|
| 246 |
Teuchos::RCP<Epetra_Operator> ModelEvaluatorInterface:: |
| 247 |
create_W() const |
| 248 |
{ |
| 249 |
return jacobian; |
| 250 |
} |
| 251 |
|
| 252 |
EpetraExt::ModelEvaluator::InArgs ModelEvaluatorInterface:: |
| 253 |
createInArgs() const |
| 254 |
{ |
| 255 |
EpetraExt::ModelEvaluator::InArgsSetup inArgs; |
| 256 |
inArgs.setModelEvalDescription("NOX 1D Finite Element Test Problem"); |
| 257 |
inArgs.setSupports(IN_ARG_x,true); |
| 258 |
return inArgs; |
| 259 |
} |
| 260 |
|
| 261 |
EpetraExt::ModelEvaluator::OutArgs ModelEvaluatorInterface:: |
| 262 |
createOutArgs() const |
| 263 |
{ |
| 264 |
EpetraExt::ModelEvaluator::OutArgsSetup outArgs; |
| 265 |
outArgs.setModelEvalDescription("NOX 1D Finite Element Test Problem"); |
| 266 |
outArgs.setSupports(OUT_ARG_f,true); |
| 267 |
outArgs.setSupports(OUT_ARG_W,true); |
| 268 |
outArgs.set_W_properties( |
| 269 |
DerivativeProperties( |
| 270 |
DERIV_LINEARITY_NONCONST |
| 271 |
,DERIV_RANK_FULL |
| 272 |
,false // supportsAdjoint |
| 273 |
) |
| 274 |
); |
| 275 |
return outArgs; |
| 276 |
} |
| 277 |
|
| 278 |
bool ModelEvaluatorInterface::createGraph() |
| 279 |
{ |
| 280 |
if (Graph != 0) { |
| 281 |
delete Graph; |
| 282 |
Graph = 0; |
| 283 |
} |
| 284 |
|
| 285 |
// Create the shell for the |
| 286 |
Graph = new Epetra_CrsGraph(Copy, *StandardMap, 5); |
| 287 |
|
| 288 |
// Declare required variables |
| 289 |
int row, column; |
| 290 |
int OverlapNumMyElements = OverlapMap->NumMyElements(); |
| 291 |
|
| 292 |
// Loop Over # of Finite Elements on Processor |
| 293 |
for (int ne=0; ne < OverlapNumMyElements-1; ne++) { |
| 294 |
|
| 295 |
// Loop over Nodes in Element |
| 296 |
for (int i=0; i< 2; i++) { |
| 297 |
row=OverlapMap->GID(ne+i); |
| 298 |
|
| 299 |
// Loop over Trial Functions |
| 300 |
for(int j=0;j < 2; j++) { |
| 301 |
|
| 302 |
// If this row is owned by current processor, add the index |
| 303 |
if (StandardMap->MyGID(row)) { |
| 304 |
column=OverlapMap->GID(ne+j); |
| 305 |
Graph->InsertGlobalIndices(row, 1, &column); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
Graph->FillComplete(); |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
// Set initialSolution to desired initial condition |
| 315 |
bool ModelEvaluatorInterface::initializeSoln() |
| 316 |
{ |
| 317 |
initialSolution->PutScalar(1.0); // Default initialization |
| 318 |
return true; |
| 319 |
} |
| 320 |
|
| 321 |
//==================================================================== |
| 322 |
// Basis vector |
| 323 |
|
| 324 |
// Constructor |
| 325 |
Basis::Basis() { |
| 326 |
phi = new double[2]; |
| 327 |
dphide = new double[2]; |
| 328 |
} |
| 329 |
|
| 330 |
// Destructor |
| 331 |
Basis::~Basis() { |
| 332 |
delete [] phi; |
| 333 |
delete [] dphide; |
| 334 |
} |
| 335 |
|
| 336 |
// Calculates a linear 1D basis |
| 337 |
void Basis::computeBasis(int gp, double *x, double *u, double *uold) { |
| 338 |
int N = 2; |
| 339 |
if (gp==0) {eta=-1.0/sqrt(3.0); wt=1.0;} |
| 340 |
if (gp==1) {eta=1.0/sqrt(3.0); wt=1.0;} |
| 341 |
|
| 342 |
// Calculate basis function and derivatives at nodel pts |
| 343 |
phi[0]=(1.0-eta)/2.0; |
| 344 |
phi[1]=(1.0+eta)/2.0; |
| 345 |
dphide[0]=-0.5; |
| 346 |
dphide[1]=0.5; |
| 347 |
|
| 348 |
// Caculate basis function and derivative at GP. |
| 349 |
dx=0.5*(x[1]-x[0]); |
| 350 |
xx=0.0; |
| 351 |
uu=0.0; |
| 352 |
duu=0.0; |
| 353 |
uuold=0.0; |
| 354 |
duuold=0.0; |
| 355 |
for (int i=0; i < N; i++) { |
| 356 |
xx += x[i] * phi[i]; |
| 357 |
uu += u[i] * phi[i]; |
| 358 |
duu += u[i] * dphide[i]; |
| 359 |
if (uold) { |
| 360 |
uuold += uold[i] * phi[i]; |
| 361 |
duuold += uold[i] * dphide[i]; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
return; |
| 366 |
} |