Debian Codegraph @

packages/nox/test/epetra/FiniteDifferenceColoring/FiniteElementProblem.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 Comm(&comm),
24 NumGlobalElements(numGlobalElements)
25 {
26
27 // Commonly used variables
28 int i;
29 MyPID = Comm->MyPID(); // Process ID
30 NumProc = Comm->NumProc(); // Total number of processes
31
32 // Construct a Source Map that puts approximately the same
33 // Number of equations on each processor in uniform global ordering
34 StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm);
35
36 // Get the number of elements owned by this processor
37 NumMyElements = StandardMap->NumMyElements();
38
39 // Construct an overlaped map for the finite element fill *************
40 // For single processor jobs, the overlap and standard map are the same
41 if (NumProc == 1) {
42 OverlapMap = new Epetra_Map(*StandardMap);
43 } else {
44
45 int OverlapNumMyElements;
46 int OverlapMinMyGID;
47 OverlapNumMyElements = NumMyElements + 2;
48 if ((MyPID == 0) || (MyPID == NumProc - 1))
49 OverlapNumMyElements --;
50
51 if (MyPID==0)
52 OverlapMinMyGID = StandardMap->MinMyGID();
53 else
54 OverlapMinMyGID = StandardMap->MinMyGID() - 1;
55
56 int* OverlapMyGlobalElements = new int[OverlapNumMyElements];
57
58 for (i = 0; i < OverlapNumMyElements; i ++)
59 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
60
61 OverlapMap = new Epetra_Map(-1, OverlapNumMyElements,
62 OverlapMyGlobalElements, 0, *Comm);
63
64 delete [] OverlapMyGlobalElements;
65
66 } // End Overlap map construction *************************************
67
68 // Construct Linear Objects
69 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
70 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
71 AA = Teuchos::rcp(new Epetra_CrsGraph(Copy, *StandardMap, 5));
72
73 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
74 generateGraph(*AA);
75
76 // Create a second matrix using graph of first matrix - this creates a
77 // static graph so we can refill the new matirx after FillComplete()
78 // is called.
79 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
80 A->FillComplete();
81 ColumnToOverlapImporter = new Epetra_Import(A->ColMap(),*OverlapMap);
82 }
83
84 // Destructor
85 FiniteElementProblem::~FiniteElementProblem()
86 {
87 delete Importer;
88 delete OverlapMap;
89 delete StandardMap;
90 }
91
92 // Matrix and Residual Fills
93 bool FiniteElementProblem::
94 evaluate(FillType f,
95 const Epetra_Vector* soln,
96 Epetra_Vector* tmp_rhs,
97 Epetra_RowMatrix* tmp_matrix,
98 NOX::Epetra::Interface::Required::FillType fillFlag)
99 {
100 flag = f;
101
102 // Set the incoming linear objects
103 if (flag == F_ONLY) {
104 rhs = tmp_rhs;
105 } else if (flag == MATRIX_ONLY) {
106 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
107 } else if (flag == ALL) {
108 rhs = tmp_rhs;
109 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
110 } else {
111 std::cout << "ERROR: FiniteElementProblem::fillMatrix() - FillType flag is broken" << std::endl;
112 throw;
113 }
114
115 // Create the overlapped solution and position vectors
116 Epetra_Vector u(*OverlapMap);
117 Epetra_Vector x(*OverlapMap);
118
119 // Export Solution to Overlap vector
120 // If the vector to be used in the fill is already in the Overlap form,
121 // we simply need to map on-processor from column-space indices to
122 // OverlapMap indices.
123 if( fillFlag == NOX::Epetra::Interface::Required::FD_Res)
124 {
125 u.Export(*soln, *ColumnToOverlapImporter, Insert);
126 }
127 // If the incoming vector used in the fill has not been scattered to
128 // OverlapMap (ghosted) entities, do it now. This potentially involves
129 // interprocessor communication unlike the previous condition.
130 else
131 u.Import(*soln, *Importer, Insert);
132
133 // Declare required variables
134 int i,j;
135 int OverlapNumMyElements = OverlapMap->NumMyElements();
136
137 int OverlapMinMyGID;
138 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
139 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
140
141 int row, column;
142 double factor=1000.0;
143 double jac;
144 double xx[2];
145 double uu[2];
146 Basis basis;
147
148 // Create the nodal coordinates
149 double Length=1.0;
150 double dx=Length/((double) NumGlobalElements-1);
151 for (i=0; i < OverlapNumMyElements; i++) {
152 x[i]=dx*((double) OverlapMinMyGID+i);
153 }
154
155 // Zero out the objects that will be filled
156 if ((flag == MATRIX_ONLY) || (flag == ALL)) i=A->PutScalar(0.0);
157 if ((flag == F_ONLY) || (flag == ALL)) i=rhs->PutScalar(0.0);
158
159 // Loop Over # of Finite Elements on Processor
160 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
161
162 // Loop Over Gauss Points
163 for(int gp=0; gp < 2; gp++) {
164 // Get the solution and coordinates at the nodes
165 xx[0]=x[ne];
166 xx[1]=x[ne+1];
167 uu[0]=u[ne];
168 uu[1]=u[ne+1];
169 // Calculate the basis function at the gauss point
170 basis.getBasis(gp, xx, uu);
171
172 // Loop over Nodes in Element
173 for (i=0; i< 2; i++) {
174 row=OverlapMap->GID(ne+i);
175 if (StandardMap->MyGID(row)) {
176 if ((flag == F_ONLY) || (flag == ALL)) {
177 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
178 +basis.wt*basis.dx
179 *((1.0/(basis.dx*basis.dx))*basis.duu*
180 basis.dphide[i]+factor*basis.uu*basis.uu*basis.phi[i]);
181 }
182 }
183 // Loop over Trial Functions
184 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
185 for(j=0;j < 2; j++) {
186 if (StandardMap->MyGID(row)) {
187 column=OverlapMap->GID(ne+j);
188 jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))*
189 basis.dphide[j]*basis.dphide[i]
190 +2.0*factor*basis.uu*basis.phi[j]*
191 basis.phi[i]);
192 A->SumIntoGlobalValues(row, 1, &jac, &column);
193 }
194 }
195 }
196 }
197 }
198 }
199
200 // Insert Boundary Conditions and modify Jacobian and function (F)
201 // U(0)=1
202 if (MyPID==0) {
203 if ((flag == F_ONLY) || (flag == ALL))
204 (*rhs)[0]= (*soln)[0] - 1.0;
205 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
206 column=0;
207 jac=1.0;
208 A->ReplaceGlobalValues(0, 1, &jac, &column);
209 column=1;
210 jac=0.0;
211 A->ReplaceGlobalValues(0, 1, &jac, &column);
212 }
213 }
214
215 // Sync up processors to be safe
216 Comm->Barrier();
217
218 A->FillComplete();
219
220 return true;
221 }
222
223 Teuchos::RCP<Epetra_Vector> FiniteElementProblem::getSolution()
224 {
225 return initialSolution;
226 }
227
228 Teuchos::RCP<Epetra_CrsMatrix> FiniteElementProblem::getJacobian()
229 {
230 return A;
231 }
232
233 Teuchos::RCP<Epetra_CrsGraph> FiniteElementProblem::getGraph()
234 {
235 return AA;
236 }
237
238 Epetra_CrsGraph& FiniteElementProblem::generateGraph(Epetra_CrsGraph& AA)
239 {
240
241 // Declare required variables
242 int i,j;
243 int row, column;
244 int OverlapNumMyElements = OverlapMap->NumMyElements();
245
246 // Loop Over # of Finite Elements on Processor
247 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
248
249 // Loop over Nodes in Element
250 for (i=0; i< 2; i++) {
251 row=OverlapMap->GID(ne+i);
252
253 // Loop over Trial Functions
254 for(j=0;j < 2; j++) {
255
256 // If this row is owned by current processor, add the index
257 if (StandardMap->MyGID(row)) {
258 column=OverlapMap->GID(ne+j);
259 AA.InsertGlobalIndices(row, 1, &column);
260 }
261 }
262 }
263 }
264 AA.FillComplete();
265 // AA.SortIndices();
266 // AA.RemoveRedundantIndices();
267 return AA;
268 }