Debian Codegraph @

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