Debian Codegraph @

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

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