Debian Codegraph @

packages/nox/test/epetra/MultiPhysics/HMX_PDE.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 "Problem_Manager.H"
20 #include "HMX_PDE.H"
21
22 // Constructor - creates the Epetra objects (maps and vectors)
23 HMX_PDE::HMX_PDE(Epetra_Comm& comm,
24 double diffCoef_,
25 double Const_R_,
26 double Steric_C_,
27 double PreExp_A_,
28 double ActEnergy_,
29 map<string, double> SrcTermExponent_,
30 map<string, double> SrcTermWeight_,
31 int numGlobalNodes,
32 std::string name_) :
33 GenericEpetraProblem(comm, numGlobalNodes, name_),
34 xmin(0.0),
35 xmax(1.0),
36 dt(2.0e-1),
37 diffCoef(diffCoef_),
38 Const_R(Const_R_),
39 StericCoef(Steric_C_),
40 PreExp_A(PreExp_A_),
41 ActEnergy(ActEnergy_),
42 SrcTermExponent(SrcTermExponent_),
43 SrcTermWeight(SrcTermWeight_)
44 {
45 // Create mesh and solution vectors
46
47 // We first initialize the mesh and then the solution since the latter
48 // can depend on the mesh.
49 xptr = Teuchos::rcp( new Epetra_Vector(*StandardMap) );
50 double Length= xmax - xmin;
51 dx=Length/((double) NumGlobalNodes-1);
52 for (int i=0; i < NumMyNodes; i++) {
53 (*xptr)[i]=xmin + dx*((double) StandardMap->MinMyGID()+i);
54 }
55
56 // Create extra vector needed for this transient problem
57 oldSolution = new Epetra_Vector(*StandardMap);
58
59 // Next we create and initialize (using default provided) the solution vector
60 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
61 initializeSolution();
62
63 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
64 AA = Teuchos::rcp( new Epetra_CrsGraph(Copy, *StandardMap, 0) );
65 generateGraph();
66
67 #ifdef DEBUG
68 AA->Print(cout);
69 #endif
70
71 // Create a matrix using the graph just created - this creates a
72 // static graph so we can refill the new matirx after FillComplete()
73 // is called.
74 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
75 A->FillComplete();
76
77 // Create the Importer needed for FD coloring
78 ColumnToOverlapImporter = new Epetra_Import(A->ColMap(),*OverlapMap);
79 }
80
81 //-----------------------------------------------------------------------------
82
83 // Destructor
84 HMX_PDE::~HMX_PDE()
85 {
86 delete oldSolution; oldSolution = 0;
87 delete ColumnToOverlapImporter; ColumnToOverlapImporter = 0;
88 }
89
90 //-----------------------------------------------------------------------------
91
92 // Reset function
93 void
94 HMX_PDE::reset(const Epetra_Vector& x)
95 {
96 *oldSolution = x;
97 }
98
99 //-----------------------------------------------------------------------------
100
101 // Empty Reset function
102 void
103 HMX_PDE::reset()
104 {
105 std::cout << "WARNING: reset called without passing any update vector !!"
106 << std::endl;
107 }
108
109 //-----------------------------------------------------------------------------
110
111 // Set initialSolution to desired initial condition
112 void
113 HMX_PDE::initializeSolution(double val)
114 {
115 // Aliases for convenience
116 Epetra_Vector& soln = *initialSolution;
117
118 soln.PutScalar(val);
119
120 *oldSolution = soln;
121 }
122
123 //-----------------------------------------------------------------------------
124
125 // Matrix and Residual Fills
126 bool
127 HMX_PDE::evaluate(
128 NOX::Epetra::Interface::Required::FillType flag,
129 const Epetra_Vector* soln,
130 Epetra_Vector* tmp_rhs)
131 {
132 // Determine what to fill (F or Jacobian)
133 bool fillF = false;
134 bool fillMatrix = false;
135 if (tmp_rhs != 0) {
136 fillF = true;
137 rhs = tmp_rhs;
138 }
139 else {
140 fillMatrix = true;
141 }
142
143 // "flag" can be used to determine how accurate your fill of F should be
144 // depending on why we are calling evaluate (Could be using computeF to
145 // populate a Jacobian or Preconditioner).
146 if (flag == NOX::Epetra::Interface::Required::Residual) {
147 // Do nothing for now
148 }
149 else if (flag == NOX::Epetra::Interface::Required::Jac) {
150 // Do nothing for now
151 }
152 else if (flag == NOX::Epetra::Interface::Required::Prec) {
153 // Do nothing for now
154 }
155 else if (flag == NOX::Epetra::Interface::Required::User) {
156 // Do nothing for now
157 }
158
159 int numDep = depProblems.size();
160
161 // Create the overlapped solution and position vectors
162 Epetra_Vector u(*OverlapMap);
163 Epetra_Vector uold(*OverlapMap);
164 std::vector<Epetra_Vector*> dep(numDep);
165 for( int i = 0; i<numDep; i++)
166 dep[i] = new Epetra_Vector(*OverlapMap);
167 Epetra_Vector xvec(*OverlapMap);
168
169 // Export Solution to Overlap vector
170 // If the vector to be used in the fill is already in the Overlap form,
171 // we simply need to map on-processor from column-space indices to
172 // OverlapMap indices. Note that the old solution is simply fixed data that
173 // needs to be sent to an OverlapMap (ghosted) vector. The conditional
174 // treatment for the current soution vector arises from use of
175 // FD coloring in parallel.
176 uold.Import(*oldSolution, *Importer, Insert);
177 for( int i = 0; i<numDep; i++ )
178 (*dep[i]).Import(*( (*(depSolutions.find(depProblems[i]))).second ),
179 *Importer, Insert);
180 xvec.Import(*xptr, *Importer, Insert);
181 if( flag == NOX::Epetra::Interface::Required::FD_Res)
182 // Overlap vector for solution received from FD coloring, so simply reorder
183 // on processor
184 u.Export(*soln, *ColumnToOverlapImporter, Insert);
185 else // Communication to Overlap vector is needed
186 u.Import(*soln, *Importer, Insert);
187
188 // Declare required variables
189 int OverlapNumMyNodes = OverlapMap->NumMyElements();
190
191 int OverlapMinMyNodeGID;
192 if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID();
193 else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1;
194
195 // Setup iterators for looping over each problem source term contribution
196 // to this one's PDE
197 map<string, double>::iterator srcTermIter;
198 map<string, double>::iterator srcTermEnd = SrcTermWeight.end();
199
200 // Bundle up the dependent variables in the way needed for computing
201 // the source terms of each reaction
202 /*
203 Epetra_Vector debugSrcTerm(*OverlapMap);
204 map<string, Epetra_Vector*> debugDepVars;
205 debugDepVars.insert( pair<string, Epetra_Vector*>(getName(), &u) );
206 for( int i = 0; i<numDep; i++ )
207 debugDepVars.insert( pair<string, Epetra_Vector*>
208 (myManager->getName(depProblems[i]), &dep[i]) );
209
210 for( srcTermIter = SrcTermWeight.begin();
211 srcTermIter != srcTermEnd; srcTermIter++) {
212 HMX_PDE &srcTermProb =
213 dynamic_cast<HMX_PDE&>(
214 myManager->getProblem(srcTermIter->first) );
215 std::cout << "Inside problem: \"" << getName() << "\" calling to get source term "
216 << "from problem: \"" << srcTermIter->first << "\" :" << std::endl;
217 srcTermProb.computeSourceTerm(debugDepVars, debugSrcTerm);
218 std::cout << "Resulting source term :" << debugSrcTerm << std::endl;
219 }
220 */
221
222 int row;
223 double alpha = 500.0;
224 double xx[2];
225 double uu[2];
226 double uuold[2];
227 std::vector<double*> ddep(numDep);
228 for( int i = 0; i<numDep; i++)
229 ddep[i] = new double[2];
230 double *srcTerm = new double[2];
231 Basis basis;
232
233 // Bundle up the dependent variables in the way needed for computing
234 // the source terms of each reaction
235 map<string, double*> depVars;
236 depVars.insert( pair<string, double*>(getName(), uu) );
237 for( int i = 0; i<numDep; i++ )
238 depVars.insert( pair<string, double*>
239 (myManager->getProblemName(depProblems[i]), ddep[i]) );
240 // Do a check on this fill
241 // map<string, double*>::iterator iter;
242 // for( iter = depVars.begin(); iter != depVars.end(); iter++)
243 // std::cout << "Inserted ... " << iter->first << "\t" << iter->second << std::endl;
244 // std::cout << "--------------------------------------------------" << std::endl;
245 // for( iter = depVars.begin(); iter != depVars.end(); iter++)
246 // std::cout << iter->first << "\t" << (iter->second)[0] << ", "
247 // << (iter->second)[1] << std::endl;
248 // std::cout << "--------------------------------------------------" << std::endl;
249
250 // Zero out the objects that will be filled
251 if ( fillMatrix ) A->PutScalar(0.0);
252 if ( fillF ) rhs->PutScalar(0.0);
253
254 // Loop Over # of Finite Elements on Processor
255 for (int ne=0; ne < OverlapNumMyNodes-1; ne++)
256 {
257 // Loop Over Gauss Points
258 for(int gp=0; gp < 2; gp++)
259 {
260 // Get the solution and coordinates at the nodes
261 xx[0]=xvec[ne];
262 xx[1]=xvec[ne+1];
263 uu[0] = u[ne];
264 uu[1] = u[ne+1];
265 uuold[0] = uold[ne];
266 uuold[1] = uold[ne+1];
267 for( int i = 0; i<numDep; i++ ) {
268 ddep[i][0] = (*dep[i])[ne];
269 ddep[i][1] = (*dep[i])[ne+1];
270 }
271 // Calculate the basis function and variables at the gauss points
272 basis.getBasis(gp, xx, uu, uuold, ddep);
273
274 // Loop over Nodes in Element
275 for (int i=0; i< 2; i++) {
276 row=OverlapMap->GID(ne+i);
277 if (StandardMap->MyGID(row)) {
278 if ( fillF ) {
279
280 // First do time derivative and diffusion operator
281 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
282 +basis.wt*basis.dx
283 *((basis.uu - basis.uuold)/dt * basis.phi[i]
284 +(1.0/(basis.dx*basis.dx))*diffCoef*basis.duu*basis.dphide[i]);
285
286 // Then do source term contributions
287 //
288 for( srcTermIter = SrcTermWeight.begin();
289 srcTermIter != srcTermEnd; srcTermIter++) {
290 HMX_PDE &srcTermProb =
291 dynamic_cast<HMX_PDE&>(
292 myManager->getProblem((*srcTermIter).first) );
293 srcTermProb.computeSourceTerm(2, depVars, srcTerm);
294 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
295 +basis.wt*basis.dx
296 *( basis.phi[i] * ( - (*srcTermIter).second * srcTerm[i] ));
297 }
298 //
299 }
300 }
301 // Loop over Trial Functions
302 if ( fillMatrix ) {
303 /*
304 for(j=0;j < 2; j++) {
305 if (StandardMap->MyGID(row)) {
306 column=OverlapMap->GID(ne+j);
307 jac=basis.wt*basis.dx*(
308 basis.phi[j]/dt*basis.phi[i]
309 +(1.0/(basis.dx*basis.dx))*diffCoef*basis.dphide[j]*
310 basis.dphide[i]
311 + basis.phi[i] * ( (beta+1.0)*basis.phi[j]
312 - 2.0*basis.uu*basis.phi[j]*basis.ddep[id_spec]) );
313 ierr=A->SumIntoGlobalValues(row, 1, &jac, &column);
314 }
315 }
316 */
317 }
318 }
319 }
320 }
321
322 // Apply Dirichlet BC for Temperature problem only (for now); this implies
323 // no-flux across domain boundary for all species.
324 if( getName() == tempFieldName )
325 {
326 // Insert Boundary Conditions and modify Jacobian and function (F)
327 // U(0)=1
328 if (MyPID==0)
329 {
330 if ( fillF )
331 (*rhs)[0]= (*soln)[0] - alpha;
332 if ( fillMatrix )
333 {
334 int column=0;
335 double jac=1.0;
336 A->ReplaceGlobalValues(0, 1, &jac, &column);
337 column=1;
338 jac=0.0;
339 A->ReplaceGlobalValues(0, 1, &jac, &column);
340 }
341 }
342 // U(1)=1
343 if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 )
344 {
345 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
346 if ( fillF )
347 (*rhs)[lastDof] = (*soln)[lastDof] - alpha;
348 if ( fillMatrix )
349 {
350 int row=StandardMap->MaxAllGID();
351 int column = row;
352 double jac = 1.0;
353 A->ReplaceGlobalValues(row, 1, &jac, &column);
354 jac=0.0;
355 column--;
356 A->ReplaceGlobalValues(row, 1, &jac, &column);
357 }
358 }
359 }
360
361 // Sync up processors to be safe
362 Comm->Barrier();
363
364 A->FillComplete();
365
366 #ifdef DEBUG
367 A->Print(cout);
368
369 if( fillF )
370 std::cout << "For residual fill :" << std::endl << *rhs << std::endl;
371
372 if( fillMatrix )
373 {
374 std::cout << "For jacobian fill :" << std::endl;
375 A->Print(cout);
376 }
377 #endif
378
379 // Cleanup
380 for( int i = 0; i < numDep; ++i)
381 {
382 delete [] ddep[i];
383 delete dep[i];
384 }
385 delete [] srcTerm;
386
387 return true;
388 }
389
390 //-----------------------------------------------------------------------------
391
392 Epetra_Vector &
393 HMX_PDE::getOldSoln()
394 {
395 return *oldSolution;
396 }
397
398 //-----------------------------------------------------------------------------
399
400 double
401 HMX_PDE::getdt() const
402 {
403 return dt;
404 }
405
406 //-----------------------------------------------------------------------------
407
408 void
409 HMX_PDE::generateGraph()
410 {
411
412 // Declare required variables
413 int i,j;
414 int row, column;
415 int OverlapNumMyNodes = OverlapMap->NumMyElements();
416 int OverlapMinMyNodeGID;
417 if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID();
418 else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1;
419
420 // Loop Over # of Finite Elements on Processor
421 for (int ne=0; ne < OverlapNumMyNodes-1; ne++)
422 {
423 // Loop over Nodes in Element
424 for (i=0; i<2; i++)
425 {
426 // If this node is owned by current processor, add indices
427 if (StandardMap->MyGID(OverlapMap->GID(ne+i)))
428 {
429 // Loop over unknowns in Node
430 row=OverlapMap->GID(ne+i);
431
432 // Loop over supporting nodes
433 for(j=0; j<2; j++)
434 {
435 // Loop over unknowns at supporting nodes
436 column=OverlapMap->GID(ne+j);
437 //printf("\t\tWould like to insert -> (%d, %d)\n",row,column);
438 AA->InsertGlobalIndices(row, 1, &column);
439 }
440 }
441 }
442 }
443 AA->FillComplete();
444
445 return;
446 }
447
448 //-----------------------------------------------------------------------------
449
450 void
451 HMX_PDE::computeSourceTerm(map<string, Epetra_Vector*> fields,
452 Epetra_Vector& result)
453 {
454 // All dependent variables should be in place before now
455
456 // We assume all other registered dependent problems are species which
457 // affect the reaction rate of this specie
458
459 Epetra_Vector *TvecPtr = 0;
460 map<string, Epetra_Vector*>::iterator iter = fields.find(tempFieldName);
461 if( iter == fields.end() )
462 {
463 std::cout << "ERROR: Cannot find Temperature field \"" << tempFieldName
464 << "\" for use in computeSourceTerm for problem \""
465 << getName() << std::endl;
466 throw "HMX_PDE ERROR";
467 }
468 else
469 TvecPtr = (*iter).second;
470
471 Epetra_Vector &T = *TvecPtr;
472
473 // If this problem is the temperature equation, don't compute a source
474 // term. This would be where a volumetric heating term would go.
475 if( getName() == tempFieldName )
476 {
477 result.PutScalar(0.0);
478 return;
479 }
480 else
481 {
482 double rateK;
483
484 map<string, double>::iterator requiredFieldIter;
485 map<string, double>::iterator requiredFieldEnd = SrcTermExponent.end();
486
487 for( int i = 0; i<result.MyLength(); i++ ) {
488
489 rateK = pow(T[i],StericCoef) * PreExp_A *
490 exp( -ActEnergy / (Const_R * T[i]) );
491
492 result[i] = 1.0; // Start point for product
493
494 // Loop over all required fields and contribute to product
495 for( requiredFieldIter = SrcTermExponent.begin();
496 requiredFieldIter != requiredFieldEnd; requiredFieldIter++)
497 {
498 iter = fields.find( (*requiredFieldIter).first );
499 if( iter == fields.end() )
500 {
501 std::cout << "ERROR: Cannot find required field \""
502 << (*requiredFieldIter).first
503 << "\" for use in computeSourceTerm for problem \""
504 << getName() << std::endl;
505 throw "HMX_PDE ERROR";
506 }
507 Epetra_Vector &reqFieldVec = *((*iter).second);
508
509 result[i] *= pow( reqFieldVec[i], (*requiredFieldIter).second );
510 }
511 result[i] *= rateK;
512 }
513 }
514 }
515
516 //-----------------------------------------------------------------------------
517
518 void
519 HMX_PDE::computeSourceTerm(int arraySize,
520 map<string, double*> fields,
521 double*& result)
522 {
523 // All dependent variables should be in place before now
524
525 // We assume all other registered dependent problems are species which
526 // affect the reaction rate of this specie
527
528 double *T = 0;
529 map<string, double*>::iterator iter = fields.find(tempFieldName);
530 if( iter == fields.end() ) {
531 std::cout << "ERROR: Cannot find Temperature field \"" << tempFieldName
532 << "\" for use in computeSourceTerm for problem \""
533 << getName() << std::endl;
534 throw "HMX_PDE ERROR";
535 }
536 else
537 T = (*iter).second;
538
539 // If this problem is the temperature equation, don't compute a source
540 // term. This would be where a volumetric heating term would go.
541 if( getName() == tempFieldName ) {
542 for( int i = 0; i<arraySize; i++)
543 result[i] = 0.0;
544
545 return;
546 }
547 else {
548
549 double rateK;
550
551 map<string, double>::iterator requiredFieldIter;
552 map<string, double>::iterator requiredFieldEnd = SrcTermExponent.end();
553
554 for( int i = 0; i<arraySize; i++ ) {
555
556 rateK = pow(T[i],StericCoef) * PreExp_A *
557 exp( -ActEnergy / (Const_R * T[i]) );
558
559 result[i] = 1.0; // Start point for product
560
561 // Loop over all required fields and contribute to product
562 for( requiredFieldIter = SrcTermExponent.begin();
563 requiredFieldIter != requiredFieldEnd; requiredFieldIter++) {
564
565 iter = fields.find( (*requiredFieldIter).first );
566 if( iter == fields.end() ) {
567 std::cout << "ERROR: Cannot find required field \""
568 << (*requiredFieldIter).first
569 << "\" for use in computeSourceTerm for problem \""
570 << getName() << "\"" << std::endl;
571 throw "HMX_PDE ERROR";
572 }
573 double *reqFieldVec = (*iter).second;
574
575 result[i] *= pow( reqFieldVec[i], (*requiredFieldIter).second );
576 }
577 result[i] *= rateK;
578 }
579 }
580 }
581
582 //-----------------------------------------------------------------------------
583
584 void
585 HMX_PDE::compute_dRdT()
586 {
587 // Compute dependence of Rxn rate on Temperature
588 }
589
590 //-----------------------------------------------------------------------------
591
592 void
593 HMX_PDE::compute_dRdN()
594 {
595 // Compute dependence of Rxn rate on Self-Species
596 }
597
598 //-----------------------------------------------------------------------------
599