Debian Codegraph @

packages/nox/test/epetra/MultiPhysics/ConvDiff_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 "ConvDiff_PDE.H"
21
22 // Constructor - creates the Epetra objects (maps and vectors)
23 ConvDiff_PDE::ConvDiff_PDE(
24 Epetra_Comm& Comm_ ,
25 double peclet_ ,
26 double radiation_ ,
27 double kappa_ ,
28 double bcWeight_ ,
29 double xmin_ ,
30 double xmax_ ,
31 double Tleft_ ,
32 double Tright_ ,
33 int NumGlobalUnknowns_ ,
34 std::string name_ ) :
35 GenericEpetraProblem(Comm_, NumGlobalUnknowns_, name_),
36 xmin ( xmin_ ) ,
37 xmax ( xmax_ ) ,
38 Tleft ( Tleft_ ) ,
39 Tright ( Tright_ ) ,
40 peclet ( peclet_ ) ,
41 radiation ( radiation_ ) ,
42 kappa ( kappa_ ) ,
43 bcWeight ( bcWeight_ ) ,
44 expandJacobian ( true ) ,
45 depProbPtr ( NULL )
46 {
47 // Create mesh and solution vectors
48
49 // We first initialize the mesh and then the solution since the latter can depend on the mesh.
50
51 xptr = Teuchos::rcp( new Epetra_Vector(*StandardMap) );
52 dx = (xmax - xmin) / ( (double) NumGlobalNodes - 1 );
53
54 for( int i = 0; i < NumMyNodes; ++i )
55 (*xptr)[i]=xmin + dx*((double) StandardMap->MinMyGID()+i);
56
57 // Create extra vector needed for transient problem interface
58 oldSolution = Teuchos::rcp( new Epetra_Vector(*StandardMap) );
59
60 // Create and initialize (using default provided) the solution vector
61 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
62 initializeSolution();
63
64 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
65 AA = Teuchos::rcp( new Epetra_CrsGraph(Copy, *StandardMap, 0) );
66 generateGraph();
67
68 #ifdef DEBUG
69 AA->Print(cout);
70 #endif
71
72 // Create a matrix using the graph just created - this creates a
73 // static graph so we can refill the new matirx after FillComplete()
74 // is called.
75 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
76 A->FillComplete();
77
78 // Create the Importer needed for FD coloring
79 ColumnToOverlapImporter = Teuchos::rcp( new Epetra_Import(A->ColMap(),*OverlapMap) );
80
81 }
82
83 //-----------------------------------------------------------------------------
84
85 // Destructor
86 ConvDiff_PDE::~ConvDiff_PDE()
87 {
88 }
89
90 //-----------------------------------------------------------------------------
91
92 // Initialize method
93 void
94 ConvDiff_PDE::initialize()
95 {
96 // Verify we have only one dependent problem and that it is of the appropriate type
97 if( 1 < depProblems.size() )
98 {
99 std::string msg = "ERROR: ConvDiff_PDE::initialize : Problem \"" + myName
100 + "\" depends on more than one other problem.";
101 throw msg;
102 }
103
104 GenericEpetraProblem & depProb = myManager->getProblem( depProblems[0] );
105 depProbPtr = dynamic_cast<ConvDiff_PDE *>(&depProb);
106 if( NULL == depProbPtr )
107 {
108 std::string msg = "ERROR: ConvDiff_PDE::initialize : Dependent problem \""
109 + depProb.getName() + "\" is not of type ConvDiff_PDE.";
110 throw msg;
111 }
112
113 // Determine relative location of coupling interface
114 if( (*xptr)[0] < depProbPtr->getMesh()[0] )
115 myInterface = RIGHT;
116 else
117 myInterface = LEFT;
118
119 // Set exact interface temperature accordingly
120 if( LEFT == myInterface )
121 T1_exact = Tleft ;
122 else
123 T1_exact = Tright;
124
125 // Choose appropriate element and nodes for interfacial BC enforcement
126 int OverlapNumMyNodes = OverlapMap->NumMyElements(); // this may break in parallel
127 if( LEFT == myInterface )
128 {
129 interface_elem = 0 ;
130 local_node = 0 ;
131 interface_node = 0 ;
132 opposite_node = OverlapNumMyNodes - 1 ;
133 dirScale = 1.0 ;
134 }
135 else
136 {
137 interface_elem = OverlapNumMyNodes - 2 ;
138 local_node = 1 ;
139 interface_node = OverlapNumMyNodes - 1 ;
140 opposite_node = 0 ;
141 dirScale = -1.0 ;
142 }
143
144 // Use this method to compute and output the analytic solution and its first derivative
145 exactSolution = Teuchos::rcp( new Epetra_Vector(*initialSolution) );
146 dTdx = Teuchos::rcp( new Epetra_Vector(*initialSolution) );
147 Epetra_Vector & x = *xptr;
148
149 if( 1.e-20 < fabs(peclet) )
150 {
151 for( int i = 0; i < NumMyNodes; ++i )
152 {
153 (*exactSolution)[i] = (T1_exact - Tleft*exp(peclet) + (Tleft - T1_exact)*exp(peclet*(x[i]-xmin))) /
154 ( 1.0 - exp(peclet) );
155 (*dTdx )[i] = peclet * (Tleft - T1_exact)*exp(peclet*(x[i]-xmin)) /
156 ( 1.0 - exp(peclet) );
157 }
158 }
159 else
160 {
161 for( int i = 0; i < NumMyNodes; ++i )
162 {
163 (*exactSolution)[i] = (Tright - T1_exact)*(x[i] - xmax) + Tright ;
164 (*dTdx )[i] = Tright - T1_exact ;
165 }
166 }
167
168 ostringstream sval;
169 sval << myId << flush;
170 std::string fileName1 = "analytic_" + sval.str();
171 std::string fileName2 = "dTdx_" + sval.str();
172
173 std::ofstream outFile1(fileName1.c_str());
174 std::ofstream outFile2(fileName2.c_str());
175 if( !outFile1 || !outFile2 )
176 {
177 std::string msg = "ERROR: Could not open one of files \"" + fileName1 + "\"" + " or \""
178 + fileName2 + "\"";
179 throw msg;
180 }
181
182 for( int i = 0; i < NumMyNodes; ++i )
183 {
184 outFile1 << i << " " << x[i] << " " << (*exactSolution)[i] << std::endl;
185 outFile2 << i << " " << x[i] << " " << (*dTdx )[i] << std::endl;
186 }
187 }
188
189 //-----------------------------------------------------------------------------
190
191 // Set initialSolution to desired initial condition
192 void
193 ConvDiff_PDE::initializeSolution(double val)
194 {
195 // Aliases for convenience
196 Epetra_Vector& soln = *initialSolution;
197
198 soln.PutScalar(val);
199
200 //Epetra_Vector perturb(soln);
201 //perturb.Random();
202 //perturb.Scale(0.20);
203
204 //soln.Update(1.0, perturb, 1.0);
205
206 *oldSolution = soln;
207 }
208
209 //-----------------------------------------------------------------------------
210
211 // Matrix and Residual Fills
212 bool
213 ConvDiff_PDE::evaluate(
214 NOX::Epetra::Interface::Required::FillType flag,
215 const Epetra_Vector * soln,
216 Epetra_Vector * rhs)
217 {
218
219 if( rhs == 0 )
220 {
221 std::string msg = "ERROR: ConvDiff_PDE::evaluate : callback appears to be other than a residual fill. Others are not support for this type.";
222 throw msg;
223 }
224
225 int numDep = depProblems.size();
226
227 // Create the overlapped solution and position vectors
228 Epetra_Vector u(*OverlapMap);
229 Epetra_Vector uold(*OverlapMap);
230 std::vector<Epetra_Vector*> dep(numDep);
231 for( int i = 0; i < numDep; ++i)
232 dep[i] = new Epetra_Vector(*OverlapMap);
233
234 Epetra_Vector xvec(*OverlapMap);
235
236 // Export Solution to Overlap vector
237 // If the vector to be used in the fill is already in the Overlap form,
238 // we simply need to map on-processor from column-space indices to
239 // OverlapMap indices. Note that the old solution is simply fixed data that
240 // needs to be sent to an OverlapMap (ghosted) vector. The conditional
241 // treatment for the current soution vector arises from use of
242 // FD coloring in parallel.
243
244 uold.Import(*oldSolution, *Importer, Insert);
245
246 for( int i = 0; i < numDep; ++i )
247 (*dep[i]).Import(*( (*(depSolutions.find(depProblems[i]))).second ), *Importer, Insert);
248
249 xvec.Import(*xptr, *Importer, Insert);
250
251 if( flag == NOX::Epetra::Interface::Required::FD_Res)
252 // Overlap vector for solution received from FD coloring, so simply reorder
253 // on processor
254 u.Export(*soln, *ColumnToOverlapImporter, Insert);
255 else // Communication to Overlap vector is needed
256 u.Import(*soln, *Importer, Insert);
257
258 // Declare required variables
259 int OverlapNumMyNodes = OverlapMap->NumMyElements();
260
261 int OverlapMinMyNodeGID;
262 if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID();
263 else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1;
264
265 int row;
266
267 double * xx = new double[2];
268 double * uu = new double[2];
269 double * uuold = new double[2];
270 std::vector<double*> ddep(numDep);
271 for( int i = 0; i < numDep; ++i)
272 ddep[i] = new double[2];
273
274 Basis basis;
275
276 // Bundle up the dependent variables in the way needed for computing
277 // the source terms of each reaction
278 map<string, double*> depVars;
279 depVars.insert( pair< std::string, double*>(getName(), uu) );
280 for( int i = 0; i < numDep; ++i )
281 depVars.insert( pair<string, double*>(myManager->getProblemName(depProblems[i]), ddep[i]) );
282
283 // Zero out the objects that will be filled
284 rhs->PutScalar(0.0);
285
286 // Loop Over # of Finite Elements on Processor
287 for( int ne = 0; ne < OverlapNumMyNodes-1; ++ne )
288 {
289 // Loop Over Gauss Points
290 for( int gp = 0; gp < 2; ++gp )
291 {
292 // Get the solution and coordinates at the nodes
293 xx[0]=xvec[ne];
294 xx[1]=xvec[ne+1];
295 uu[0] = u[ne];
296 uu[1] = u[ne+1];
297 uuold[0] = uold[ne];
298 uuold[1] = uold[ne+1];
299 for( int i = 0; i < numDep; ++i )
300 {
301 ddep[i][0] = (*dep[i])[ne];
302 ddep[i][1] = (*dep[i])[ne+1];
303 }
304 // Calculate the basis function and variables at the gauss points
305 basis.getBasis(gp, xx, uu, uuold, ddep);
306
307 // Loop over Nodes in Element
308 for( int i = 0; i < 2; ++i )
309 {
310 row = OverlapMap->GID(ne+i);
311 if( StandardMap->MyGID(row) )
312 {
313 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))] +=
314 + basis.wt * basis.dx
315 * ( peclet * (basis.duu / basis.dx) * basis.phi[i]
316 + kappa * (1.0/(basis.dx*basis.dx)) * basis.duu * basis.dphide[i] );
317 }
318 }
319 }
320 }
321
322 //if( NOX::Epetra::Interface::Required::Residual == flag )
323 //{
324 // int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
325 // std::cout << "\t\"" << myName << "\" u[0] = " << (*soln)[0]
326 // << "\tu[N] = " << (*soln)[lastDof] << std::endl;
327 // std::cout << "\t\"" << myName << "\" RHS[0] = " << (*rhs)[0]
328 // << "\tRHS[N] = " << (*rhs)[lastDof] << std::endl << std::endl;
329 //}
330
331
332 // Apply BCs
333
334 computeHeatFlux( soln );
335
336 double bcResidual = bcWeight * (myFlux - depProbPtr->getHeatFlux() ) -
337 (1.0 - bcWeight) * (u[interface_node] - depProbPtr->getInterfaceTemp() );
338
339 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
340
341 // "Left" boundary
342 if( LEFT == myInterface ) // this may break in parallel
343 {
344 (*rhs)[0] = bcResidual;
345 (*rhs)[lastDof] = (*soln)[lastDof] - Tright;
346 }
347 // "Right" boundary
348 else
349 {
350 (*rhs)[0] = (*soln)[0] - Tleft;
351 (*rhs)[lastDof] = bcResidual;
352 }
353
354 // Sync up processors to be safe
355 Comm->Barrier();
356
357 A->FillComplete();
358
359 #ifdef DEBUG
360 std::cout << "For residual fill :" << std::endl << *rhs << std::endl;
361 #endif
362
363 // Cleanup
364 for( int i = 0; i < numDep; ++i)
365 {
366 delete [] ddep[i];
367 delete dep[i];
368 }
369
370 delete [] xx ;
371 delete [] uu ;
372 delete [] uuold ;
373
374 return true;
375 }
376
377 //-----------------------------------------------------------------------------
378
379 // Initialize method
380 void
381 ConvDiff_PDE::getOffBlockIndices( map<int, std::vector<int> > & indices )
382 {
383
384 // Get the equation map from the other problem
385 Epetra_Map & otherMap = *(depProbPtr->StandardMap);
386 int otherMinEqID = otherMap.MinAllGID();
387 int otherMaxEqID = otherMap.MaxAllGID();
388
389 std::vector<int> offBlockColumns(2);
390
391 if( LEFT == myInterface )
392 {
393 offBlockColumns[0] = otherMaxEqID - 1 ;
394 offBlockColumns[1] = otherMaxEqID ;
395 indices[interface_node] = offBlockColumns ;
396 }
397 else
398 {
399 offBlockColumns[0] = otherMinEqID ;
400 offBlockColumns[1] = otherMinEqID + 1 ;
401 indices[interface_node] = offBlockColumns ;
402 }
403
404 return;
405 }
406
407 //-----------------------------------------------------------------------------
408
409 void
410 ConvDiff_PDE::prepare_data_for_transfer()
411 {
412 // Redirect to compute heat fluxes
413 return computeHeatFlux();
414 }
415
416 //-----------------------------------------------------------------------------
417
418 // A fill specialized to the single node at the coupling interface
419 void
420 ConvDiff_PDE::computeHeatFlux( const Epetra_Vector * soln )
421 {
422
423 int numDep = depProblems.size();
424
425 // Create the overlapped solution and position vectors
426 Epetra_Vector u(*OverlapMap);
427 Epetra_Vector uold(*OverlapMap);
428 std::vector<Epetra_Vector*> dep(numDep);
429 for( int i = 0; i < numDep; ++i)
430 dep[i] = new Epetra_Vector(*OverlapMap);
431
432 Epetra_Vector xvec(*OverlapMap);
433
434 // Export Solution to Overlap vector
435 // If the vector to be used in the fill is already in the Overlap form,
436 // we simply need to map on-processor from column-space indices to
437 // OverlapMap indices. Note that the old solution is simply fixed data that
438 // needs to be sent to an OverlapMap (ghosted) vector. The conditional
439 // treatment for the current soution vector arises from use of
440 // FD coloring in parallel.
441
442 uold.Import(*oldSolution, *Importer, Insert);
443
444 for( int i = 0; i < numDep; ++i )
445 (*dep[i]).Import(*( (*(depSolutions.find(depProblems[i]))).second ), *Importer, Insert);
446
447 xvec.Import(*xptr, *Importer, Insert);
448
449 if( NULL == soln )
450 u.Import(*initialSolution, *Importer, Insert);
451 else
452 u.Import(*soln, *Importer, Insert);
453
454 // Declare required variables
455 int row;
456 double * xx = new double[2];
457 double * uu = new double[2];
458 double * uuold = new double[2];
459 std::vector<double*> ddep(numDep);
460 for( int i = 0; i < numDep; ++i)
461 ddep[i] = new double[2];
462
463 Basis basis;
464
465 // Bundle up the dependent variables in the way needed for computing
466 // the source terms of each reaction
467 map<string, double*> depVars;
468 depVars.insert( pair< std::string, double*>(getName(), uu) );
469 for( int i = 0; i < numDep; ++i )
470 depVars.insert( pair<string, double*>(myManager->getProblemName(depProblems[i]), ddep[i]) );
471
472 myFlux = 0.0;
473
474 // Loop Over Gauss Points
475 for( int gp = 0; gp < 2; ++gp )
476 {
477 // Get the solution and coordinates at the nodes
478 xx[0]=xvec[interface_elem];
479 xx[1]=xvec[interface_elem+1];
480 uu[0] = u[interface_elem];
481 uu[1] = u[interface_elem+1];
482 uuold[0] = uold[interface_elem];
483 uuold[1] = uold[interface_elem+1];
484 for( int i = 0; i < numDep; ++i )
485 {
486 ddep[i][0] = (*dep[i])[interface_elem];
487 ddep[i][1] = (*dep[i])[interface_elem+1];
488 }
489
490 // Calculate the basis function and variables at the gauss points
491 basis.getBasis(gp, xx, uu, uuold, ddep);
492
493 row = OverlapMap->GID( interface_elem + local_node );
494
495 if( StandardMap->MyGID(row) )
496 {
497 myFlux +=
498 + basis.wt * basis.dx
499 * ( peclet * (basis.duu / basis.dx) * basis.phi[local_node]
500 + kappa * (1.0/(basis.dx*basis.dx)) * basis.duu * basis.dphide[local_node] );
501 }
502 }
503
504 // Sync up processors to be safe
505 Comm->Barrier();
506
507 // Cleanup
508 for( int i = 0; i < numDep; ++i)
509 {
510 delete [] ddep[i];
511 delete dep[i];
512 }
513
514 delete [] xx ;
515 delete [] uu ;
516 delete [] uuold ;
517
518 //int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
519 //cout << "\t\"" << myName << "\" u[0] = " << u[0]
520 // << "\tu[N] = " << u[lastDof] << std::endl;
521 //cout << u << std::endl;
522 //cout << "\t\"" << myName << "\" myFlux = " << myFlux << std::endl << std::endl;
523
524 // Scale domain integration according to interface position
525 myFlux *= dirScale;
526
527 // Now add radiation contribution to flux
528 myFlux += radiation * ( pow(u[interface_node], 4) - pow(u[opposite_node], 4) );
529
530 return;
531 }
532
533 //-----------------------------------------------------------------------------
534
535 double
536 ConvDiff_PDE::getInterfaceTemp()
537 {
538
539 if( LEFT == myInterface )
540 return (*initialSolution)[0];
541 else
542 {
543 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
544 return (*initialSolution)[lastDof];
545 }
546 }
547
548 //-----------------------------------------------------------------------------
549
550 void
551 ConvDiff_PDE::process_transferred_data()
552 {
553 // Now that each problem has computed its fluxes, get these as well as boundary
554 // temperature from problems on which we depend
555
556 return;
557 }
558
559 //-----------------------------------------------------------------------------
560
561 void
562 ConvDiff_PDE::outputStatus( std::ostream & os )
563 {
564
565 std::string location = ( myInterface == LEFT ) ? "Left" : "Right";
566
567 os << "\"" << myName << "\" couples at an interface with Problem \"" << depProbPtr->getName()
568 << "\" on the " << location << std::endl;
569
570 return;
571 }
572
573 //-----------------------------------------------------------------------------
574
575 void
576 ConvDiff_PDE::generateGraph()
577 {
578
579 // Declare required variables
580 int row, column;
581 int OverlapNumMyNodes = OverlapMap->NumMyElements();
582
583 // Loop Over # of Finite Elements on Processor
584 for( int ne = 0; ne < OverlapNumMyNodes - 1; ++ne)
585 {
586 // Loop over Nodes in Element
587 for( int i = 0; i < 2; ++i )
588 {
589 // If this node is owned by current processor, add indices
590 if (StandardMap->MyGID(OverlapMap->GID(ne+i)))
591 {
592 // Loop over unknowns in Node
593 row = OverlapMap->GID(ne+i);
594
595 // Loop over supporting nodes
596 for( int j = 0; j < 2; ++j)
597 {
598 // Loop over unknowns at supporting nodes
599 column = OverlapMap->GID(ne+j);
600 //printf("\t\tWould like to insert -> (%d, %d)\n",row,column);
601 AA->InsertGlobalIndices(row, 1, &column);
602 }
603 }
604 }
605 }
606 AA->FillComplete();
607
608 return;
609 }
610
611 //-----------------------------------------------------------------------------
612
613 void
614 ConvDiff_PDE::doTransfer()
615 {
616
617 depProbPtr->computeHeatFlux();
618
619 return;
620 }
621
622 //-----------------------------------------------------------------------------
623
624 double
625 ConvDiff_PDE::computeAnalyticInterfaceTemp(
626 double radiation ,
627 double T_left ,
628 double T_right ,
629 double kappa ,
630 double peclet )
631 {
632
633 int nIters = 0; // iteration counter
634 double T_int = 1.0; // initial guess
635 double tol = 1.e-12; // solve tolerance
636 double residual = radiation*( pow(T_int, 4) - pow(T_right, 4) )
637 + (T_left - T_int)*peclet*exp(peclet)/(1.0 - exp(peclet))
638 - kappa*(T_right - T_int);
639 double dfdT = 0.0;
640
641 // Simple Newton loop
642 while( tol < fabs(residual) )
643 {
644 ++nIters;
645
646 dfdT = radiation*4.0*pow(T_int,3)
647 - peclet*exp(peclet)/(1.0 - exp(peclet))
648 + kappa;
649
650 if( 1.e-15 > fabs(dfdT) )
651 {
652 std::cout << "ConvDiff_PDE::computeAnalyticInterfaceTemp:\n"
653 "Warning: Obtained near-zero derivtative. Aborting calculation." << std::endl;
654 return(T_int);
655 }
656
657 T_int = T_int - residual/dfdT;
658
659 residual = radiation*( pow(T_int, 4) - pow(T_right, 4) )
660 + (T_left - T_int)*peclet*exp(peclet)/(1.0 - exp(peclet))
661 - kappa*(T_right - T_int);
662 }
663
664 std::cout << "Analytic interfacial temperature = " << setprecision(8) << T_int << std::endl;
665 std::cout << "Residual = " << residual << " in " << nIters << " iterations." << std::endl;
666
667 return T_int;
668 }
669
670 //-----------------------------------------------------------------------------