Debian Codegraph @

packages/nox/test/epetra/FiniteDifferenceColoring/FiniteElementProblem_OwnedOnly.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 "FiniteElementProblem.H"
20
21 // Constructor - creates the Epetra objects (maps and vectors)
22 FiniteElementProblem::FiniteElementProblem(int numGlobalElements, Epetra_Comm& comm) :
23 flag(F_ONLY),
24 ColumnToOverlapImporter(NULL),
25 rhs(NULL),
26 Comm(&comm),
27 NumGlobalElements(numGlobalElements)
28 {
29
30 // Commonly used variables
31 int i;
32 MyPID = Comm->MyPID(); // Process ID
33 NumProc = Comm->NumProc(); // Total number of processes
34
35 // Construct a Source Map that puts approximately the same
36 // Number of equations on each processor in uniform global ordering
37 StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm);
38
39 // Get the number of elements owned by this processor
40 NumMyElements = StandardMap->NumMyElements();
41
42 // Construct an overlaped map for the finite element fill *************
43 // For single processor jobs, the overlap and standard map are the same
44 if (NumProc == 1) {
45 OverlapMap = new Epetra_Map(*StandardMap);
46 } else {
47
48 int OverlapNumMyElements;
49 int OverlapMinMyGID;
50 OverlapNumMyElements = NumMyElements + 2;
51 if ((MyPID == 0) || (MyPID == NumProc - 1))
52 OverlapNumMyElements --;
53
54 if (MyPID==0)
55 OverlapMinMyGID = StandardMap->MinMyGID();
56 else
57 OverlapMinMyGID = StandardMap->MinMyGID() - 1;
58
59 int* OverlapMyGlobalElements = new int[OverlapNumMyElements];
60
61 for (i = 0; i < OverlapNumMyElements; i ++)
62 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
63
64 OverlapMap = new Epetra_Map(-1, OverlapNumMyElements,
65 OverlapMyGlobalElements, 0, *Comm);
66
67 delete [] OverlapMyGlobalElements;
68
69 } // End Overlap map construction *************************************
70
71 // Construct Linear Objects
72 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
73 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
74 AA = Teuchos::rcp(new Epetra_CrsGraph(Copy, *StandardMap, 5));
75
76 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
77 generateGraph(*AA);
78
79 // Create a second matrix using graph of first matrix - this creates a
80 // static graph so we can refill the new matirx after FillComplete()
81 // is called.
82 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
83 A->FillComplete();
84
85 // Do setup for linear unknown info
86 int NumMyElements = StandardMap->NumMyElements();
87 int MinMyGID = StandardMap->MinMyGID();
88
89 double dx=1.0/((double) NumGlobalElements-1);
90 int lid=0;
91 N_nonlinearUnknowns=0;
92 for(i=0; i < NumMyElements; i++)
93 if(dx*((double) MinMyGID+i) >= 0.4)
94 N_nonlinearUnknowns++;
95
96 nonlinearUnknowns=new int[N_nonlinearUnknowns];
97 for(i=0; i < NumMyElements; i++)
98 if(dx*((double) MinMyGID+i) >= 0.4)
99 nonlinearUnknowns[lid++]=i;
100
101 // Use .4 to include enough of the non-linear zone in non-linear list to avoid missing entries
102 }
103
104 // Destructor
105 FiniteElementProblem::~FiniteElementProblem()
106 {
107 delete Importer;
108 delete OverlapMap;
109 delete StandardMap;
110 delete [] nonlinearUnknowns;
111 }
112
113 // Matrix and Residual Fills
114 bool FiniteElementProblem::
115 evaluate(FillType f,
116 const Epetra_Vector* soln,
117 Epetra_Vector* tmp_rhs,
118 Epetra_RowMatrix* tmp_matrix,
119 NOX::Epetra::Interface::Required::FillType fillFlag)
120 {
121 flag = f;
122
123 // Set the incoming linear objects
124 if (flag == F_ONLY) {
125 rhs = tmp_rhs;
126 } else if (flag == MATRIX_ONLY) {
127 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
128 } else if (flag == ALL) {
129 rhs = tmp_rhs;
130 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
131 } else {
132 std::cout << "ERROR: FiniteElementProblem::fillMatrix() - FillType flag is broken" << std::endl;
133 throw;
134 }
135
136 // Create the overlapped solution and position vectors
137 Epetra_Vector u(*OverlapMap);
138 Epetra_Vector x(*OverlapMap);
139
140 // Scatter OverlapMap (ghosted) entities.
141 u.Import(*soln, *Importer, Insert);
142
143 // Declare required variables
144 int i, j;
145 #ifdef HAVE_NOX_DEBUG
146 int ierr;
147 #endif
148 int OverlapNumMyElements = OverlapMap->NumMyElements();
149
150 int OverlapMinMyGID;
151 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
152 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
153
154 int row, column;
155 double factor=1000.0;
156 double jac;
157 double xx[2];
158 double uu[2];
159 Basis basis;
160
161 // Create the nodal coordinates
162 double Length=1.0;
163 double dx=Length/((double) NumGlobalElements-1);
164 for (i=0; i < OverlapNumMyElements; i++) {
165 x[i]=dx*((double) OverlapMinMyGID+i);
166 }
167
168 // Zero out the objects that will be filled
169 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
170 i = A->PutScalar(0.0);
171 assert(i == 0);
172 }
173 if ((flag == F_ONLY) || (flag == ALL)) {
174 i = rhs->PutScalar(0.0);
175 assert(i == 0);
176 }
177
178
179 /* Solves the nonlinear equation:
180 *
181 * d2u
182 * --- - k * u = 0 on x=[0.0 to 0.5)
183 * dx2
184 *
185 * d2u
186 * --- - k * u**2 = 0 on x=[0.5 to 1.0]
187 * dx2
188 *
189 */
190
191 // Loop Over # of Finite Elements on Processor
192 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
193
194 // Loop Over Gauss Points
195 for(int gp=0; gp < 2; gp++) {
196 // Get the solution and coordinates at the nodes
197 xx[0]=x[ne];
198 xx[1]=x[ne+1];
199 uu[0]=u[ne];
200 uu[1]=u[ne+1];
201 // Calculate the basis function at the gauss point
202 basis.getBasis(gp, xx, uu);
203
204 // Loop over Nodes in Element
205 for (i=0; i< 2; i++) {
206 row=OverlapMap->GID(ne+i);
207
208 if(xx[i]<.5) {
209 /* 1st half of PDE */
210 if (StandardMap->MyGID(row)) {
211 if ((flag == F_ONLY) || (flag == ALL)) {
212 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
213 +basis.wt*basis.dx
214 *((1.0/(basis.dx*basis.dx))*basis.duu*
215 basis.dphide[i]+factor*basis.uu*basis.phi[i]);
216 }
217 }
218 // Loop over Trial Functions
219 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
220 for(j=0;j < 2; j++) {
221 if (StandardMap->MyGID(row)) {
222 column=OverlapMap->GID(ne+j);
223 jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))*
224 basis.dphide[j]*basis.dphide[i]
225 +factor*basis.phi[j]*
226 basis.phi[i]);
227 #ifdef HAVE_NOX_DEBUG
228 ierr=A->SumIntoGlobalValues(row, 1, &jac, &column);
229 TEUCHOS_ASSERT(ierr == 0);
230 #else
231 A->SumIntoGlobalValues(row, 1, &jac, &column);
232 #endif
233 }
234 }
235 }
236 }
237 else{
238 /* 2nd half of PDE */
239 if (StandardMap->MyGID(row)) {
240 if ((flag == F_ONLY) || (flag == ALL)) {
241 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
242 +basis.wt*basis.dx
243 *((1.0/(basis.dx*basis.dx))*basis.duu*
244 basis.dphide[i]+factor*basis.uu*basis.uu*basis.phi[i]);
245 }
246 }
247 // Loop over Trial Functions
248 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
249 for(j=0;j < 2; j++) {
250 if (StandardMap->MyGID(row)) {
251 column=OverlapMap->GID(ne+j);
252 jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))*
253 basis.dphide[j]*basis.dphide[i]
254 +2.0*factor*basis.uu*basis.phi[j]*
255 basis.phi[i]);
256 #ifdef HAVE_NOX_DEBUG
257 ierr=A->SumIntoGlobalValues(row, 1, &jac, &column);
258 assert(ierr == 0);
259 #else
260 A->SumIntoGlobalValues(row, 1, &jac, &column);
261 #endif
262 }
263 }
264 }
265 }
266
267
268 }
269 }
270 }
271
272 // Insert Boundary Conditions and modify Jacobian and function (F)
273 // U(0)=1
274 if (MyPID==0) {
275 if ((flag == F_ONLY) || (flag == ALL))
276 (*rhs)[0]= (*soln)[0] - 1.0;
277 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
278 column=0;
279 jac=1.0;
280 A->ReplaceGlobalValues(0, 1, &jac, &column);
281 column=1;
282 jac=0.0;
283 A->ReplaceGlobalValues(0, 1, &jac, &column);
284 }
285 }
286
287 // Sync up processors to be safe
288 Comm->Barrier();
289
290 A->FillComplete();
291
292 return true;
293 }
294
295 Teuchos::RCP<Epetra_Vector> FiniteElementProblem::getSolution()
296 {
297 return initialSolution;
298 }
299
300 Teuchos::RCP<Epetra_CrsMatrix> FiniteElementProblem::getJacobian()
301 {
302 return A;
303 }
304
305 Teuchos::RCP<Epetra_CrsGraph> FiniteElementProblem::getGraph()
306 {
307 return AA;
308 }
309
310 Epetra_CrsGraph& FiniteElementProblem::generateGraph(Epetra_CrsGraph& AA)
311 {
312
313 // Declare required variables
314 int i,j;
315 int row, column;
316 int OverlapNumMyElements = OverlapMap->NumMyElements();
317
318 // Loop Over # of Finite Elements on Processor
319 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
320
321 // Loop over Nodes in Element
322 for (i=0; i< 2; i++) {
323 row=OverlapMap->GID(ne+i);
324
325 // Loop over Trial Functions
326 for(j=0;j < 2; j++) {
327
328 // If this row is owned by current processor, add the index
329 if (StandardMap->MyGID(row)) {
330 column=OverlapMap->GID(ne+j);
331 AA.InsertGlobalIndices(row, 1, &column);
332 }
333 }
334 }
335 }
336 AA.FillComplete();
337 // AA.SortIndices();
338 // AA.RemoveRedundantIndices();
339 return AA;
340 }