Debian Codegraph @

packages/nox/test/epetra/MultiPhysics/Equation_B.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 "Equation_B.H"
21
22 // Constructor - creates the Epetra objects (maps and vectors)
23 Equation_B::Equation_B(Epetra_Comm& comm, int numGlobalNodes,
24 std::string name_) :
25 GenericEpetraProblem(comm, numGlobalNodes, name_),
26 xmin(0.0),
27 xmax(1.0),
28 dt(1.0e-3),
29 id_temp(-1),// Index for needed dependent Temperature vector
30 id_vel(-1), // Index for auxilliary velocity vector
31 useConvection(false)
32 {
33
34 // Create mesh and solution vectors
35
36 // We first initialize the mesh and then the solution since the latter
37 // can depend on the mesh.
38 xptr = Teuchos::rcp( new Epetra_Vector(*StandardMap) );
39 double Length= xmax - xmin;
40 dx=Length/((double) NumGlobalNodes-1);
41 for (int i=0; i < NumMyNodes; i++)
42 (*xptr)[i]=xmin + dx*((double) StandardMap->MinMyGID()+i);
43
44 // Create extra vector needed for this transient problem
45 oldSolution = new Epetra_Vector(*StandardMap);
46
47 // Next we create and initialize the solution vector
48 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
49 initializeSolution();
50 // initialSolution->PutScalar(0.0);
51
52 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
53 AA = Teuchos::rcp( new Epetra_CrsGraph(Copy, *StandardMap, 0) );
54 generateGraph();
55
56 #ifdef DEBUG
57 AA->Print(cout);
58 #endif
59
60 // Create a matrix using the graph just created - this creates a
61 // static graph so we can refill the new matirx after FillComplete()
62 // is called.
63 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
64 A->FillComplete();
65
66 // Create the Importer needed for FD coloring
67 ColumnToOverlapImporter = new Epetra_Import(A->ColMap(),*OverlapMap);
68 }
69
70 //-----------------------------------------------------------------------------
71
72 // Destructor
73 Equation_B::~Equation_B()
74 {
75 delete oldSolution; oldSolution = 0;
76 delete ColumnToOverlapImporter; ColumnToOverlapImporter = 0;
77 }
78
79 //-----------------------------------------------------------------------------
80
81 // Reset function
82 void Equation_B::reset(const Epetra_Vector& x)
83 {
84 *oldSolution = x;
85 }
86
87 //-----------------------------------------------------------------------------
88
89 // Initialize based on registrations
90 void Equation_B::initialize()
91 {
92 // Get id of required Species problem
93 map<string, int>::iterator id_ptr = nameToMyIndex.find("Temperature");
94 if( id_ptr == nameToMyIndex.end() )
95 {
96 std::string msg = "ERROR: Equation_B (\"" + myName + "\") could not get "
97 + "vector for problem \"Temperature\" !!";
98 throw msg;
99 }
100 else
101 id_temp = (*id_ptr).second;
102
103 // Check for dependence on velocity (convection)
104
105 // Rather than merely existing, we should change this to search the
106 // dependent problems vector
107 id_ptr = nameToMyIndex.find("Burgers");
108 if( id_ptr == nameToMyIndex.end() )
109 {
110 std::cout << "WARNING: Equation_B (\"" << myName << "\") could not get "
111 << "vector for problem \"Burgers\". Omitting convection." << std::endl;
112
113 useConvection = false;
114 }
115 else
116 {
117 id_vel = (*id_ptr).second;
118 useConvection = true;
119 }
120
121 return;
122 }
123
124 //-----------------------------------------------------------------------------
125
126 // Empty Reset function
127 void Equation_B::reset()
128 {
129 std::cout << "WARNING: reset called without passing any update vector !!"
130 << std::endl;
131 }
132
133 //-----------------------------------------------------------------------------
134
135 // Set initialSolution to desired initial condition
136 void Equation_B::initializeSolution()
137 {
138 // Aliases for convenience
139 Epetra_Vector& soln = *initialSolution;
140 Epetra_Vector& x = *xptr;
141
142 // Here we do a sinusoidal perturbation of the unstable
143 // steady state.
144
145 double pi = 4.*atan(1.0);
146
147 for (int i=0; i<x.MyLength(); i++)
148 soln[i] = 10.0/3.0 + 1.e-1*sin(1.0*pi*x[i]);
149
150 *oldSolution = soln;
151 }
152
153 //-----------------------------------------------------------------------------
154
155 // Matrix and Residual Fills
156 bool Equation_B::evaluate(
157 NOX::Epetra::Interface::Required::FillType fillType,
158 const Epetra_Vector* soln,
159 Epetra_Vector* rhs)
160 {
161 bool fillRes = false;
162 bool fillJac = false;
163
164 // We currently make no provision for combined Residual and Jacobian fills
165 // i.e. it is either or
166
167 if( NOX::Epetra::Interface::Required::Jac == fillType )
168 fillJac = true;
169 else
170 {
171 fillRes = true;
172 assert( NULL != rhs );
173 }
174
175 int numDep = depProblems.size();
176
177 // Create the overlapped solution and position vectors
178 Epetra_Vector u(*OverlapMap);
179 Epetra_Vector uold(*OverlapMap);
180 std::vector<Epetra_Vector*> dep(numDep);
181 for( int i = 0; i<numDep; i++)
182 dep[i] = new Epetra_Vector(*OverlapMap);
183 Epetra_Vector xvec(*OverlapMap);
184
185 // Export Solution to Overlap vector
186 // If the vector to be used in the fill is already in the Overlap form,
187 // we simply need to map on-processor from column-space indices to
188 // OverlapMap indices. Note that the old solution is simply fixed data that
189 // needs to be sent to an OverlapMap (ghosted) vector. The conditional
190 // treatment for the current soution vector arises from use of
191 // FD coloring in parallel.
192 uold.Import(*oldSolution, *Importer, Insert);
193 for( int i = 0; i<numDep; i++ )
194 dep[i]->Import(*( (*(depSolutions.find(depProblems[i]))).second ), *Importer, Insert);
195
196 xvec.Import(*xptr, *Importer, Insert);
197
198 if( NOX::Epetra::Interface::Required::FD_Res == fillType )
199 // Overlap vector for solution received from FD coloring, so simply reorder
200 // on processor
201 u.Export(*soln, *ColumnToOverlapImporter, Insert);
202 else // Communication to Overlap vector is needed
203 u.Import(*soln, *Importer, Insert);
204
205 int OverlapNumMyNodes = OverlapMap->NumMyElements();
206
207 int OverlapMinMyNodeGID;
208 if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID();
209 else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1;
210
211 int row, column;
212 double Dcoeff = 0.025;
213 double alpha = 0.6;
214 double beta = 2.0;
215 double jac;
216 double xx[2];
217 double uu[2];
218 double uuold[2];
219 std::vector<double*> ddep(numDep);
220 for( int i = 0; i<numDep; i++)
221 ddep[i] = new double[2];
222 Basis basis;
223
224 double convection = 0.0;
225
226 // Zero out the objects that will be filled
227 if ( fillJac ) A->PutScalar(0.0);
228 if ( fillRes ) rhs->PutScalar(0.0);
229
230 // Loop Over # of Finite Elements on Processor
231 for (int ne=0; ne < OverlapNumMyNodes-1; ne++)
232 {
233 // Loop Over Gauss Points
234 for(int gp=0; gp < 2; gp++)
235 {
236 // Get the solution and coordinates at the nodes
237 xx[0]=xvec[ne];
238 xx[1]=xvec[ne+1];
239 uu[0] = u[ne];
240 uu[1] = u[ne+1];
241 uuold[0] = uold[ne];
242 uuold[1] = uold[ne+1];
243 for( int i = 0; i<numDep; i++ )
244 {
245 ddep[i][0] = (*dep[i])[ne];
246 ddep[i][1] = (*dep[i])[ne+1];
247 }
248 // Calculate the basis function at the gauss point
249 basis.getBasis(gp, xx, uu, uuold, ddep);
250
251 // Loop over Nodes in Element
252 for (int i=0; i< 2; i++)
253 {
254 row=OverlapMap->GID(ne+i);
255 if (StandardMap->MyGID(row))
256 {
257 if ( fillRes )
258 {
259 convection = 0.0;
260 if( useConvection )
261 convection = basis.ddep[id_vel]*basis.duu/basis.dx;
262
263 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
264 + basis.wt*basis.dx
265 * ((basis.uu - basis.uuold)/dt * basis.phi[i]
266 + convection * basis.phi[i]
267 + (1.0/(basis.dx*basis.dx))*Dcoeff*basis.duu*basis.dphide[i]
268 + basis.phi[i] * ( -beta*basis.ddep[id_temp]
269 + basis.ddep[id_temp]*basis.ddep[id_temp]*basis.uu) );
270 }
271 }
272 // Loop over Trial Functions
273 if ( fillJac )
274 {
275 for( int j = 0; j < 2; ++j )
276 {
277 if (StandardMap->MyGID(row))
278 {
279 column=OverlapMap->GID(ne+j);
280 jac=basis.wt*basis.dx*(
281 basis.phi[j]/dt*basis.phi[i]
282 +(1.0/(basis.dx*basis.dx))*Dcoeff*basis.dphide[j]*
283 basis.dphide[i]
284 + basis.phi[i] * basis.ddep[id_temp]*basis.ddep[id_temp]*
285 basis.phi[j] );
286 A->SumIntoGlobalValues(row, 1, &jac, &column);
287 }
288 }
289 }
290 }
291 }
292 }
293
294 // Insert Boundary Conditions and modify Jacobian and function (F)
295 // U(0)=1
296 if (MyPID==0)
297 {
298 if ( fillRes )
299 (*rhs)[0]= (*soln)[0] - beta/alpha;
300 if ( fillJac )
301 {
302 int column=0;
303 double jac=1.0;
304 A->ReplaceGlobalValues(0, 1, &jac, &column);
305 column=1;
306 jac=0.0;
307 A->ReplaceGlobalValues(0, 1, &jac, &column);
308 }
309 }
310 // U(1)=1
311 if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 )
312 {
313 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
314 if ( fillRes )
315 (*rhs)[lastDof] = (*soln)[lastDof] - beta/alpha;
316 if ( fillJac )
317 {
318 int row=StandardMap->MaxAllGID();
319 int column = row;
320 double jac = 1.0;
321 A->ReplaceGlobalValues(row, 1, &jac, &column);
322 jac=0.0;
323 column--;
324 A->ReplaceGlobalValues(row, 1, &jac, &column);
325 }
326 }
327
328 // Sync up processors to be safe
329 Comm->Barrier();
330
331 A->FillComplete();
332
333 #ifdef DEBUG
334 A->Print(cout);
335
336 if( fillRes )
337 std::cout << "For residual fill :" << std::endl << *rhs << std::endl;
338
339 if( fillJac ) {
340 std::cout << "For jacobian fill :" << std::endl;
341 A->Print(cout);
342 }
343 #endif
344
345 // Cleanup
346 for( int i = 0; i<numDep; i++)
347 {
348 delete [] ddep[i];
349 delete dep[i];
350 }
351
352 return true;
353 }
354
355 //-----------------------------------------------------------------------------
356
357 Epetra_Vector& Equation_B::getOldSoln()
358 {
359 return *oldSolution;
360 }
361
362 //-----------------------------------------------------------------------------
363
364 void Equation_B::generateGraph()
365 {
366
367 // Declare required variables
368 int i;
369 int row, column;
370 int OverlapNumMyNodes = OverlapMap->NumMyElements();
371 int OverlapMinMyNodeGID;
372 if (MyPID==0) OverlapMinMyNodeGID = StandardMap->MinMyGID();
373 else OverlapMinMyNodeGID = StandardMap->MinMyGID()-1;
374
375 // Loop Over # of Finite Elements on Processor
376 for (int ne=0; ne < OverlapNumMyNodes-1; ne++)
377 {
378 // Loop over Nodes in Element
379 for (i=0; i<2; i++)
380 {
381 // If this node is owned by current processor, add indices
382 if (StandardMap->MyGID(OverlapMap->GID(ne+i)))
383 {
384 // Loop over unknowns in Node
385 row=OverlapMap->GID(ne+i);
386
387 // Loop over supporting nodes
388 for(int j = 0; j < 2; ++j )
389 {
390 // Loop over unknowns at supporting nodes
391 column=OverlapMap->GID(ne+j);
392 AA->InsertGlobalIndices(row, 1, &column);
393 }
394 }
395 }
396 }
397
398 AA->FillComplete();
399
400 return;
401 }