Debian Codegraph @

packages/nox/test/epetra/1Dfem/1DfemInterface.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 // NOX include (for iostream, cmath, etc...)
11 #include "NOX_Common.H"
12 #include "Teuchos_ParameterList.hpp"
13
14 // Class Definition
15 #include "1DfemInterface.H"
16
17 // Epetra includes
18 #include "Epetra_Comm.h"
19 #include "Epetra_Map.h"
20 #include "Epetra_Vector.h"
21 #include "Epetra_Import.h"
22 #include "Epetra_CrsGraph.h"
23 #include "Epetra_CrsMatrix.h"
24
25 // Constructor - creates the Epetra objects (maps and vectors)
26 Interface::Interface(int numGlobalElements, Epetra_Comm& comm, double xmin_,
27 double xmax_) :
28 NumGlobalElements(numGlobalElements),
29 NumMyElements(0), // gets set after map creation
30 MyPID(comm.MyPID()),
31 NumProc(comm.NumProc()),
32 xmin(xmin_),
33 xmax(xmax_),
34 factor(1.0),
35 Comm(&comm),
36 StandardMap(0),
37 OverlapMap(0),
38 Importer(0),
39 rhs(0),
40 Graph(0)
41 {
42
43 // Construct a Source Map that puts approximately the same
44 // Number of equations on each processor in uniform global ordering
45 StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm);
46
47 // Get the number of elements owned by this processor
48 NumMyElements = StandardMap->NumMyElements();
49
50 // Construct an overlaped map for the finite element fill *************
51 // For single processor jobs, the overlap and standard map are the same
52 if (NumProc == 1) {
53 OverlapMap = new Epetra_Map(*StandardMap);
54 } else {
55
56 int OverlapNumMyElements;
57 int OverlapMinMyGID;
58 OverlapNumMyElements = NumMyElements + 2;
59 if ((MyPID == 0) || (MyPID == NumProc - 1))
60 OverlapNumMyElements --;
61
62 if (MyPID==0)
63 OverlapMinMyGID = StandardMap->MinMyGID();
64 else
65 OverlapMinMyGID = StandardMap->MinMyGID() - 1;
66
67 int* OverlapMyGlobalElements = new int[OverlapNumMyElements];
68
69 for (int i = 0; i < OverlapNumMyElements; i ++)
70 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
71
72 OverlapMap = new Epetra_Map(-1, OverlapNumMyElements,
73 OverlapMyGlobalElements, 0, *Comm);
74
75 delete [] OverlapMyGlobalElements;
76
77 } // End Overlap map construction *************************************
78
79 // Construct Linear Objects
80 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
81 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
82
83 // Assign non-zero entries in the graph
84 createGraph();
85
86 // Construct a matrix
87 jacobian = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *Graph));
88
89 // Clean-up
90 jacobian->FillComplete();
91
92 // Create the nodal coordinates
93 xptr = Teuchos::rcp(new Epetra_Vector(*StandardMap));
94 double Length = xmax - xmin;
95 double dx = Length/((double) NumGlobalElements-1);
96 for (int i=0; i < NumMyElements; i++) {
97 (*xptr)[i] = xmin + dx*((double) StandardMap->MinMyGID()+i);
98 }
99
100 initializeSoln();
101
102 }
103
104 // Destructor
105 Interface::~Interface()
106 {
107 delete Graph;
108 delete Importer;
109 delete OverlapMap;
110 delete StandardMap;
111 }
112
113 bool Interface::computeF(const Epetra_Vector& x,
114 Epetra_Vector& FVec,
115 NOX::Epetra::Interface::Required::FillType fillType)
116 {
117 //static int count=0; count++; std::cout << "Resid ComputeF Call Number: " << count << std::endl;
118 return evaluate(fillType, &x, &FVec, 0);
119 }
120
121 bool Interface::computeJacobian(const Epetra_Vector& x,
122 Epetra_Operator& Jac)
123 {
124 return evaluate(NOX::Epetra::Interface::Required::Jac, &x, 0, 0);
125 }
126
127 bool Interface::computePreconditioner(const Epetra_Vector& x,
128 Epetra_Operator& Prec,
129 Teuchos::ParameterList* precParams)
130 {
131 std::cout << "ERROR: Interface::preconditionVector() - "
132 << "Use Explicit Jacobian only for this test problem!" << std::endl;
133 throw "Interface Error";
134 }
135
136 // Matrix and Residual Fills
137 bool Interface::evaluate(NOX::Epetra::Interface::Required::FillType flag,
138 const Epetra_Vector* soln,
139 Epetra_Vector* tmp_rhs,
140 Epetra_RowMatrix* tmp_matrix)
141 {
142 //Determine what to fill (F or Jacobian)
143 bool fillF = false;
144 bool fillMatrix = false;
145 if (tmp_rhs != 0) {
146 fillF = true;
147 rhs = tmp_rhs;
148 }
149 else {
150 fillMatrix = true;
151 }
152
153 // "flag" can be used to determine how accurate your fill of F should be
154 // depending on why we are calling evaluate (Could be using computeF to
155 // populate a Jacobian or Preconditioner).
156 if (flag == NOX::Epetra::Interface::Required::Residual) {
157 // Do nothing for now
158 }
159 else if (flag == NOX::Epetra::Interface::Required::Jac) {
160 // Do nothing for now
161 }
162 else if (flag == NOX::Epetra::Interface::Required::Prec) {
163 // Do nothing for now
164 }
165 else if (flag == NOX::Epetra::Interface::Required::User) {
166 // Do nothing for now
167 }
168
169
170 // Create the overlapped solution and position vectors
171 Epetra_Vector u(*OverlapMap);
172 Epetra_Vector x(*OverlapMap);
173
174 // Export Solution to Overlap vector
175 u.Import(*soln, *Importer, Insert);
176 x.Import(*xptr, *Importer, Insert);
177
178 // Declare required variables
179 int OverlapNumMyElements = OverlapMap->NumMyElements();
180
181 int row, column;
182 double jac;
183 double xx[2];
184 double uu[2];
185 Basis basis;
186
187 // Zero out the objects that will be filled
188 if (fillF)
189 rhs->PutScalar(0.0);
190 if (fillMatrix)
191 jacobian->PutScalar(0.0);
192
193 // Loop Over # of Finite Elements on Processor
194 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
195
196 // Loop Over Gauss Points
197 for(int gp=0; gp < 2; gp++) {
198 // Get the solution and coordinates at the nodes
199 xx[0]=x[ne];
200 xx[1]=x[ne+1];
201 uu[0]=u[ne];
202 uu[1]=u[ne+1];
203 // Calculate the basis function at the gauss point
204 basis.computeBasis(gp, xx, uu);
205
206 // Loop over Nodes in Element
207 for (int i=0; i< 2; i++) {
208 row=OverlapMap->GID(ne+i);
209 //printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n",
210 // MyPID, row, ne+i,StandardMap.MyGID(row));
211 if (StandardMap->MyGID(row)) {
212 if (fillF) {
213 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
214 +basis.wt*basis.dx
215 *((1.0/(basis.dx*basis.dx))*basis.duu*
216 basis.dphide[i]+factor*basis.uu*basis.uu*basis.phi[i]);
217 }
218 }
219 // Loop over Trial Functions
220 if (fillMatrix) {
221 for(int j=0;j < 2; j++) {
222 if (StandardMap->MyGID(row)) {
223 column=OverlapMap->GID(ne+j);
224 jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))*
225 basis.dphide[j]*basis.dphide[i]
226 +2.0*factor*basis.uu*basis.phi[j]*
227 basis.phi[i]);
228 jacobian->SumIntoGlobalValues(row, 1, &jac, &column);
229 }
230 }
231 }
232 }
233 }
234 }
235
236 // Insert Boundary Conditions and modify Jacobian and function (F)
237 // U(0)=1
238 if (MyPID==0) {
239 if (fillF)
240 (*rhs)[0]= (*soln)[0] - 1.0;
241 if (fillMatrix) {
242 column=0;
243 jac=1.0;
244 jacobian->ReplaceGlobalValues(0, 1, &jac, &column);
245 column=1;
246 jac=0.0;
247 jacobian->ReplaceGlobalValues(0, 1, &jac, &column);
248 }
249 }
250
251 // Sync up processors to be safe
252 Comm->Barrier();
253
254 jacobian->FillComplete();
255
256 return true;
257 }
258
259 Teuchos::RCP<Epetra_Vector> Interface::getSolution()
260 {
261 return initialSolution;
262 }
263
264 Teuchos::RCP<Epetra_Vector> Interface::getMesh()
265 {
266 return xptr;
267 }
268
269 Teuchos::RCP<Epetra_CrsMatrix> Interface::getJacobian()
270 {
271 return jacobian;
272 }
273
274 bool Interface::createGraph()
275 {
276 if (Graph != 0) {
277 delete Graph;
278 Graph = 0;
279 }
280
281 // Create the shell for the
282 Graph = new Epetra_CrsGraph(Copy, *StandardMap, 5);
283
284 // Declare required variables
285 int row, column;
286 int OverlapNumMyElements = OverlapMap->NumMyElements();
287
288 // Loop Over # of Finite Elements on Processor
289 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
290
291 // Loop over Nodes in Element
292 for (int i=0; i< 2; i++) {
293 row=OverlapMap->GID(ne+i);
294
295 // Loop over Trial Functions
296 for(int j=0;j < 2; j++) {
297
298 // If this row is owned by current processor, add the index
299 if (StandardMap->MyGID(row)) {
300 column=OverlapMap->GID(ne+j);
301 Graph->InsertGlobalIndices(row, 1, &column);
302 }
303 }
304 }
305 }
306 Graph->FillComplete();
307 return true;
308 }
309
310 // Set initialSolution to desired initial condition
311 bool Interface::initializeSoln()
312 {
313 initialSolution->PutScalar(1.0); // Default initialization
314 return true;
315 }
316
317 //====================================================================
318 // Basis vector
319
320 // Constructor
321 Basis::Basis():
322 uu(0.0),
323 xx(0.0),
324 duu(0.0),
325 eta(0.0),
326 wt(0.0),
327 dx(0.0),
328 uuold(0.0),
329 duuold(0.0)
330 {
331 phi = new double[2];
332 dphide = new double[2];
333 }
334
335 // Destructor
336 Basis::~Basis() {
337 delete [] phi;
338 delete [] dphide;
339 }
340
341 // Calculates a linear 1D basis
342 void Basis::computeBasis(int gp, double *x, double *u, double *uold) {
343 int N = 2;
344 if (gp==0) {eta=-1.0/sqrt(3.0); wt=1.0;}
345 if (gp==1) {eta=1.0/sqrt(3.0); wt=1.0;}
346
347 // Calculate basis function and derivatives at nodel pts
348 phi[0]=(1.0-eta)/2.0;
349 phi[1]=(1.0+eta)/2.0;
350 dphide[0]=-0.5;
351 dphide[1]=0.5;
352
353 // Caculate basis function and derivative at GP.
354 dx=0.5*(x[1]-x[0]);
355 xx=0.0;
356 uu=0.0;
357 duu=0.0;
358 uuold=0.0;
359 duuold=0.0;
360 for (int i=0; i < N; i++) {
361 xx += x[i] * phi[i];
362 uu += u[i] * phi[i];
363 duu += u[i] * dphide[i];
364 if (uold) {
365 uuold += uold[i] * phi[i];
366 duuold += uold[i] * dphide[i];
367 }
368 }
369
370 return;
371 }