Debian Codegraph @

packages/nox/test/epetra/Brusselator/Brusselator.C

raw
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
19 #include "Brusselator.H"
20
21 // Constructor - creates the Epetra objects (maps and vectors)
22 Brusselator::Brusselator(int numGlobalNodes, Epetra_Comm& comm,
23 OverlapType OType_) :
24 xmin(0.0),
25 xmax(1.0),
26 dt(5.0e-1),
27 flag(F_ONLY),
28 overlapType(OType_),
29 ColumnToOverlapImporter(0),
30 rhs(NULL),
31 Comm(&comm),
32 NumGlobalNodes(numGlobalNodes)
33 {
34
35 // Commonly used variables
36 int i;
37 MyPID = Comm->MyPID(); // Process ID
38 NumProc = Comm->NumProc(); // Total number of processes
39
40 // Here we assume a 2-species Brusselator model, ie 2 dofs per node
41 // Note that this needs to be echoed in thew anonymous enum for NUMSPECIES.
42 NumSpecies = 2;
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 if( overlapType == ELEMENTS ) {
61
62 int OverlapNumMyNodes;
63 int OverlapMinMyNodeGID;
64
65 OverlapNumMyNodes = NumMyNodes + 2;
66 if ((MyPID == 0) || (MyPID == NumProc - 1))
67 OverlapNumMyNodes --;
68
69 if (MyPID==0)
70 OverlapMinMyNodeGID = StandardNodeMap->MinMyGID();
71 else
72 OverlapMinMyNodeGID = StandardNodeMap->MinMyGID() - 1;
73
74 int* OverlapMyGlobalNodes = new int[OverlapNumMyNodes];
75
76 for (i = 0; i < OverlapNumMyNodes; i ++)
77 OverlapMyGlobalNodes[i] = OverlapMinMyNodeGID + i;
78
79 OverlapNodeMap = new Epetra_Map(-1, OverlapNumMyNodes,
80 OverlapMyGlobalNodes, 0, *Comm);
81
82 delete [] OverlapMyGlobalNodes;
83 }
84
85 else { // overlapType == NODES
86
87 // Here we distribute elements such that nodes are ghosted, i.e. no
88 // overlapping elements but instead overlapping nodes
89 int OverlapNumMyNodes;
90 int OverlapMinMyNodeGID;
91
92 OverlapNumMyNodes = NumMyNodes + 1;
93 if ( MyPID == NumProc - 1 )
94 OverlapNumMyNodes --;
95
96 OverlapMinMyNodeGID = StandardNodeMap->MinMyGID();
97
98 int* OverlapMyGlobalNodes = new int[OverlapNumMyNodes];
99
100 for (i = 0; i < OverlapNumMyNodes; i ++)
101 OverlapMyGlobalNodes[i] = OverlapMinMyNodeGID + i;
102
103 OverlapNodeMap = new Epetra_Map(-1, OverlapNumMyNodes,
104 OverlapMyGlobalNodes, 0, *Comm);
105
106 delete [] OverlapMyGlobalNodes;
107 }
108 } // End Overlap node map construction ********************************
109
110 // Now create the unknowns maps from the node maps
111 NumMyUnknowns = NumSpecies * NumMyNodes;
112 int* StandardMyGlobalUnknowns = new int[NumMyUnknowns];
113 for (int k=0; k<NumSpecies; k++)
114 for (i=0; i<NumMyNodes; i++)
115
116 // For now, we employ an interleave of unknowns
117
118 StandardMyGlobalUnknowns[ NumSpecies * i + k ] =
119 NumSpecies * StandardNodeMap->GID(i) + k;
120
121 StandardMap = new Epetra_Map(-1, NumMyUnknowns, StandardMyGlobalUnknowns,
122 0, *Comm);
123 delete [] StandardMyGlobalUnknowns;
124
125 assert(StandardMap->NumGlobalElements() == NumGlobalUnknowns);
126
127 if (NumProc == 1) {
128 OverlapMap = new Epetra_Map(*StandardMap);
129 }
130 else {
131 int OverlapNumMyNodes = OverlapNodeMap->NumMyElements();
132 int OverlapNumMyUnknowns = NumSpecies * OverlapNumMyNodes;
133 int* OverlapMyGlobalUnknowns = new int[OverlapNumMyUnknowns];
134 for (int k=0; k<NumSpecies; k++)
135 for (i=0; i<OverlapNumMyNodes; i++)
136 OverlapMyGlobalUnknowns[ NumSpecies * i + k ] =
137 NumSpecies * OverlapNodeMap->GID(i) + k;
138
139 OverlapMap = new Epetra_Map(-1, OverlapNumMyUnknowns,
140 OverlapMyGlobalUnknowns, 0, *Comm);
141 delete [] OverlapMyGlobalUnknowns;
142
143 } // End Overlap unknowns map construction ***************************
144
145
146 #ifdef DEBUG_MAPS
147 // Output to check progress so far
148 printf("[%d] NumSpecies, NumMyNodes, NumGlobalNodes, NumMyUnknowns, NumGlobalUnknowns :\n %d\t%d\t%d\t%d\n",
149 MyPID,NumSpecies, NumMyNodes, NumGlobalNodes, NumMyUnknowns, NumGlobalUnknowns);
150
151 Comm->Barrier();
152 Comm->Barrier();
153 Comm->Barrier();
154 printf("[%d] StandardNodeMap :\n", MyPID);
155 Comm->Barrier();
156 std::cout << *StandardNodeMap << std::endl;
157 Comm->Barrier();
158 printf("[%d] OverlapNodeMap :\n", MyPID);
159 Comm->Barrier();
160 std::cout << *OverlapNodeMap << std::endl;
161 Comm->Barrier();
162 printf("[%d] StandardMap :\n", MyPID);
163 Comm->Barrier();
164 std::cout << *StandardMap << std::endl;
165 Comm->Barrier();
166 printf("[%d] StandardMap :\n", MyPID);
167 Comm->Barrier();
168 std::cout << *OverlapMap << std::endl;
169 Comm->Barrier();
170 #endif
171
172 // Construct Linear Objects
173 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
174 nodeImporter = new Epetra_Import(*OverlapNodeMap, *StandardNodeMap);
175 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
176 oldSolution = new Epetra_Vector(*StandardMap);
177 AA = Teuchos::rcp(new Epetra_CrsGraph(Copy, *StandardMap, 0));
178
179 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
180 if( overlapType == NODES )
181 generateGraphUsingNodes(*AA);
182 else
183 generateGraphUsingElements(*AA);
184
185 #ifdef DEBUG_GRAPH
186 AA->Print(cout);
187 #endif
188 #ifdef DEBUG_IMPORTER
189 Importer->Print(cout);
190 #endif
191
192 // Create the Importer needed for FD coloring using element overlap
193 // as well as the problem Jacobian matrix which may or may not be used
194 // depending on the choice of Jacobian operator
195 if( overlapType == ELEMENTS ) {
196 ColumnToOverlapImporter = new Epetra_Import(AA->ColMap(),*OverlapMap);
197 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
198 A->FillComplete();
199 }
200
201 // Create the nodal coordinates
202 xptr = Teuchos::rcp(new Epetra_Vector(*StandardNodeMap));
203 double Length= xmax - xmin;
204 dx=Length/((double) NumGlobalNodes-1);
205 for (i=0; i < NumMyNodes; i++) {
206 (*xptr)[i]=xmin + dx*((double) StandardNodeMap->MinMyGID()+i);
207 }
208
209 initializeSoln();
210
211 }
212
213 // Destructor
214 Brusselator::~Brusselator()
215 {
216 delete oldSolution;
217 delete nodeImporter;
218 delete Importer;
219 delete OverlapMap;
220 delete StandardMap;
221 }
222
223 // Reset function
224 void Brusselator::reset(const Epetra_Vector& x)
225 {
226 *oldSolution = x;
227 }
228
229 // Set initialSolution to desired initial condition
230 void Brusselator::initializeSoln()
231 {
232 Epetra_Vector& soln = *initialSolution;
233 Epetra_Vector& x = *xptr;
234
235 // Here we do a sinusoidal perturbation of the unstable
236 // steady state.
237
238 double pi = 4.*atan(1.0);
239
240 for (int i=0; i<x.MyLength(); i++) {
241 soln[2*i] = 0.6 + 1.e-1*sin(1.0*pi*x[i]);
242 soln[2*i+1] = 10.0/3.0 + 1.e-1*sin(1.0*pi*x[i]);
243 }
244 //soln.PutScalar(0.0);
245 *oldSolution = soln;
246 }
247
248 // Matrix and Residual Fills
249 bool Brusselator::evaluate(NOX::Epetra::Interface::Required::FillType fType,
250 const Epetra_Vector* soln,
251 Epetra_Vector* tmp_rhs,
252 Epetra_RowMatrix* tmp_matrix)
253 {
254
255 if( fType == NOX::Epetra::Interface::Required::Jac ) {
256 if( overlapType == NODES ) {
257 std::cout << "This problem only works for Finite-Difference Based Jacobians"
258 << std::endl << "when overlapping nodes." << std::endl
259 << "No analytic Jacobian fill available !!" << std::endl;
260 exit(1);
261 }
262 flag = MATRIX_ONLY;
263 }
264 else {
265 flag = F_ONLY;
266 if( overlapType == NODES )
267 rhs = new Epetra_Vector(*OverlapMap);
268 else
269 rhs = tmp_rhs;
270 }
271
272 // std::cout << "\nfTpye--> " << fType << std::endl
273 // << "Incoming soln vector :\n" << std::endl << *soln << std::endl;
274
275 // Create the overlapped solution and position vectors
276 Epetra_Vector u(*OverlapMap);
277 Epetra_Vector uold(*OverlapMap);
278 Epetra_Vector xvec(*OverlapNodeMap);
279
280 // Export Solution to Overlap vector
281 // If the vector to be used in the fill is already in the Overlap form,
282 // we simply need to map on-processor from column-space indices to
283 // OverlapMap indices. Note that the old solution is simply fixed data that
284 // needs to be sent to an OverlapMap (ghosted) vector. The conditional
285 // treatment for the current soution vector arises from use of
286 // FD coloring in parallel.
287 uold.Import(*oldSolution, *Importer, Insert);
288 xvec.Import(*xptr, *nodeImporter, Insert);
289 if( (overlapType == ELEMENTS) &&
290 (fType == NOX::Epetra::Interface::Required::FD_Res) )
291 // Overlap vector for solution received from FD coloring, so simply reorder
292 // on processor
293 u.Export(*soln, *ColumnToOverlapImporter, Insert);
294 else // Communication to Overlap vector is needed
295 u.Import(*soln, *Importer, Insert);
296
297 // Declare required variables
298 int i,j;
299 int OverlapNumMyNodes = OverlapNodeMap->NumMyElements();
300
301 int row1, row2, column1, column2;
302 double term1, term2;
303 double Dcoeff1 = 0.025;
304 double Dcoeff2 = 0.025;
305 double alpha = 0.6;
306 double beta = 2.0;
307 double jac11, jac12, jac21, jac22;
308 double xx[2];
309 double uu[2*NUMSPECIES] = {0.0}; // Use of the anonymous enum is needed for SGI builds
310 double uuold[2*NUMSPECIES] = {0.0};
311 Basis basis(NumSpecies);
312
313
314 // Zero out the objects that will be filled
315 if ((flag == MATRIX_ONLY) || (flag == ALL)) A->PutScalar(0.0);
316 if ((flag == F_ONLY) || (flag == ALL)) rhs->PutScalar(0.0);
317
318 // Loop Over # of Finite Elements on Processor
319 for (int ne=0; ne < OverlapNumMyNodes - 1; ne++) {
320
321 // Loop Over Gauss Points
322 for(int gp=0; gp < 2; gp++) {
323 // Get the solution and coordinates at the nodes
324 xx[0]=xvec[ne];
325 xx[1]=xvec[ne+1];
326 for (int k=0; k<NumSpecies; k++) {
327 uu[NumSpecies * 0 + k] = u[NumSpecies * ne + k];
328 uu[NumSpecies * 1 + k] = u[NumSpecies * (ne+1) + k];
329 uuold[NumSpecies * 0 + k] = uold[NumSpecies * ne + k];
330 uuold[NumSpecies * 1 + k] = uold[NumSpecies * (ne+1) + k];
331 }
332 // Calculate the basis function at the gauss point
333 basis.getBasis(gp, xx, uu, uuold);
334
335 // Loop over Nodes in Element
336 for (i=0; i< 2; i++) {
337 row1=OverlapMap->GID(NumSpecies * (ne+i));
338 row2=OverlapMap->GID(NumSpecies * (ne+i) + 1);
339 term1 = basis.wt*basis.dx
340 *((basis.uu[0] - basis.uuold[0])/dt * basis.phi[i]
341 +(1.0/(basis.dx*basis.dx))*Dcoeff1*basis.duu[0]*basis.dphide[i]
342 + basis.phi[i] * ( -alpha + (beta+1.0)*basis.uu[0]
343 - basis.uu[0]*basis.uu[0]*basis.uu[1]) );
344 term2 = basis.wt*basis.dx
345 *((basis.uu[1] - basis.uuold[1])/dt * basis.phi[i]
346 +(1.0/(basis.dx*basis.dx))*Dcoeff2*basis.duu[1]*basis.dphide[i]
347 + basis.phi[i] * ( -beta*basis.uu[0]
348 + basis.uu[0]*basis.uu[0]*basis.uu[1]) );
349 //printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n",
350 // MyPID, row, ne+i,StandardMap.MyGID(row));
351 if ((flag == F_ONLY) || (flag == ALL)) {
352 if( overlapType == NODES ) {
353 (*rhs)[NumSpecies*(ne+i)] += term1;
354 (*rhs)[NumSpecies*(ne+i)+1] += term2;
355 }
356 else
357 if (StandardMap->MyGID(row1)) {
358 (*rhs)[StandardMap->LID(OverlapMap->GID(NumSpecies*(ne+i)))]+=
359 term1;
360 (*rhs)[StandardMap->LID(OverlapMap->GID(NumSpecies*(ne+i)+1))]+=
361 term2;
362 }
363 }
364 // Loop over Trial Functions
365 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
366 for(j=0;j < 2; j++) {
367 if (StandardMap->MyGID(row1)) {
368 column1=OverlapMap->GID(NumSpecies * (ne+j));
369 column2=OverlapMap->GID(NumSpecies * (ne+j) + 1);
370 jac11=basis.wt*basis.dx*(
371 basis.phi[j]/dt*basis.phi[i]
372 +(1.0/(basis.dx*basis.dx))*Dcoeff1*basis.dphide[j]*
373 basis.dphide[i]
374 + basis.phi[i] * ( (beta+1.0)*basis.phi[j]
375 - 2.0*basis.uu[0]*basis.phi[j]*basis.uu[1]) );
376 jac12=basis.wt*basis.dx*(
377 basis.phi[i] * ( -basis.uu[0]*basis.uu[0]*basis.phi[j]) );
378 jac21=basis.wt*basis.dx*(
379 basis.phi[i] * ( -beta*basis.phi[j]
380 + 2.0*basis.uu[0]*basis.phi[j]*basis.uu[1]) );
381 jac22=basis.wt*basis.dx*(
382 basis.phi[j]/dt*basis.phi[i]
383 +(1.0/(basis.dx*basis.dx))*Dcoeff2*basis.dphide[j]*
384 basis.dphide[i]
385 + basis.phi[i] * basis.uu[0]*basis.uu[0]*basis.phi[j] );
386 A->SumIntoGlobalValues(row1, 1, &jac11, &column1);
387 column1++;
388 A->SumIntoGlobalValues(row1, 1, &jac12, &column1);
389 A->SumIntoGlobalValues(row2, 1, &jac22, &column2);
390 column2--;
391 A->SumIntoGlobalValues(row2, 1, &jac21, &column2);
392 }
393 }
394 }
395 }
396 }
397 }
398
399 // Insert Boundary Conditions and modify Jacobian and function (F)
400 // U(0)=1
401 if (MyPID==0) {
402 if ((flag == F_ONLY) || (flag == ALL)) {
403 (*rhs)[0]= (*soln)[0] - 0.6;
404 (*rhs)[1]= (*soln)[1] - 10.0/3.0;
405 }
406 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
407 int column=0;
408 double jac=1.0;
409 A->ReplaceGlobalValues(0, 1, &jac, &column);
410 column++;
411 A->ReplaceGlobalValues(1, 1, &jac, &column);
412 jac=0.0;
413 column=0;
414 A->ReplaceGlobalValues(1, 1, &jac, &column);
415 column++;
416 A->ReplaceGlobalValues(0, 1, &jac, &column);
417 column++;
418 A->ReplaceGlobalValues(0, 1, &jac, &column);
419 A->ReplaceGlobalValues(1, 1, &jac, &column);
420 column++;
421 A->ReplaceGlobalValues(0, 1, &jac, &column);
422 A->ReplaceGlobalValues(1, 1, &jac, &column);
423 }
424 }
425 // U(1)=1
426 if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 ) {
427 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
428 if ((flag == F_ONLY) || (flag == ALL)) {
429 (*rhs)[lastDof - 1] = (*soln)[lastDof - 1] - 0.6;
430 (*rhs)[lastDof] = (*soln)[lastDof] - 10.0/3.0;
431 }
432 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
433 int row=StandardMap->MaxAllGID() - 1;
434 int column = row;
435 double jac = 1.0;
436 A->ReplaceGlobalValues(row++, 1, &jac, &column);
437 column++;
438 A->ReplaceGlobalValues(row, 1, &jac, &column);
439 jac=0.0;
440 row = column - 1;
441 A->ReplaceGlobalValues(row, 1, &jac, &column);
442 column--;
443 A->ReplaceGlobalValues(row+1, 1, &jac, &column);
444 column--;
445 A->ReplaceGlobalValues(row, 1, &jac, &column);
446 A->ReplaceGlobalValues(row+1, 1, &jac, &column);
447 column--;
448 A->ReplaceGlobalValues(row, 1, &jac, &column);
449 A->ReplaceGlobalValues(row+1, 1, &jac, &column);
450 }
451 }
452
453 // Sync up processors to be safe
454 Comm->Barrier();
455
456 // Do an assemble for overlap nodes
457 if( overlapType == NODES )
458 tmp_rhs->Export(*rhs, *Importer, Add);
459
460 // Comm->Barrier();
461 // std::cout << "Returned tmp_rhs residual vector :\n" << std::endl << *tmp_rhs << std::endl;
462
463 return true;
464 }
465
466 Teuchos::RCP<Epetra_Vector> Brusselator::getSolution()
467 {
468 return initialSolution;
469 }
470
471 Teuchos::RCP<Epetra_CrsMatrix> Brusselator::getJacobian()
472 {
473 if( Teuchos::is_null(A) ) return A;
474 else {
475 std::cout << "No valid Jacobian matrix for this problem. This is likely the"
476 << " result of overlapping NODES rather than ELEMENTS.\n" << std::endl;
477 throw "Brusselator Error";
478 }
479 }
480
481 Teuchos::RCP<Epetra_Vector> Brusselator::getMesh()
482 {
483 return xptr;
484 }
485
486 Epetra_Vector& Brusselator::getOldSoln()
487 {
488 return *oldSolution;
489 }
490
491 double Brusselator::getdt()
492 {
493 return dt;
494 }
495
496 Teuchos::RCP<Epetra_CrsGraph> Brusselator::getGraph()
497 {
498 return AA;
499 }
500
501
502 Epetra_CrsGraph& Brusselator::generateGraphUsingNodes(Epetra_CrsGraph& AA)
503 {
504
505 int row, column;
506
507 int myMinNodeGID = StandardNodeMap->MinMyGID();
508 int myMaxNodeGID = StandardNodeMap->MaxMyGID();
509
510 int leftNodeGID, rightNodeGID;
511 for( int myNode = myMinNodeGID; myNode <= myMaxNodeGID; myNode++ ) {
512
513 leftNodeGID = myNode - 1;
514 rightNodeGID = myNode + 1;
515
516 if( leftNodeGID < StandardNodeMap->MinAllGID() )
517 leftNodeGID = StandardNodeMap->MinAllGID();
518
519 if( rightNodeGID > StandardNodeMap->MaxAllGID() )
520 rightNodeGID = StandardNodeMap->MaxAllGID();
521
522 for( int dependNode = leftNodeGID; dependNode <= rightNodeGID; dependNode++ ) {
523
524 // Loop over unknowns in Node
525 for (int j = 0; j < NumSpecies; j++) {
526 row = NumSpecies * myNode + j; // Interleave scheme
527
528 // Loop over unknowns at supporting nodes
529 for (int m = 0; m < NumSpecies; m++) {
530 column = NumSpecies * dependNode + m;
531 //printf("\t\tWould like to insert -> (%d, %d)\n",row,column);
532 AA.InsertGlobalIndices(row, 1, &column);
533 }
534 }
535 }
536 }
537
538 AA.FillComplete();
539 // AA.SortIndices();
540 // AA.RemoveRedundantIndices();
541 return AA;
542 }
543
544 Epetra_CrsGraph& Brusselator::generateGraphUsingElements(Epetra_CrsGraph& AA)
545 {
546
547 // Declare required variables
548 int i,j;
549 int row, column;
550 int OverlapNumMyNodes = OverlapNodeMap->NumMyElements();
551
552 // Loop Over # of Finite Elements on Processor
553 for (int ne=0; ne < OverlapNumMyNodes-1; ne++) {
554
555 // Loop over Nodes in Element
556 for (i=0; i<2; i++) {
557
558 // If this node is owned by current processor, add indices
559 if (StandardNodeMap->MyGID(OverlapNodeMap->GID(ne+i))) {
560
561 // Loop over unknowns in Node
562 for (int k=0; k<NumSpecies; k++) {
563 row=OverlapMap->GID( NumSpecies*(ne+i) + k); // Interleave scheme
564
565 // Loop over supporting nodes
566 for(j=0; j<2; j++) {
567
568 // Loop over unknowns at supporting nodes
569 for (int m=0; m<NumSpecies; m++) {
570 column=OverlapMap->GID( NumSpecies*(ne+j) + m);
571 //printf("\t\tWould like to insert -> (%d, %d)\n",row,column);
572 AA.InsertGlobalIndices(row, 1, &column);
573 }
574 }
575 }
576 }
577 }
578 }
579 AA.FillComplete();
580 // AA.SortIndices();
581 // AA.RemoveRedundantIndices();
582 return AA;
583 }
584