Debian Codegraph @

packages/nox/test/epetra/LOCA_TestProblems/FiniteElementProblem.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // LOCA: Library of Continuation Algorithms Package
4 //
5 // Copyright 2001-2005 NTESS and the LOCA 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,
23 Epetra_Comm& comm,
24 double s) :
25 Comm(&comm),
26 NumGlobalElements(numGlobalElements),
27 scale(s)
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 = new Epetra_Vector(*StandardMap);
74 AA = 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 = new Epetra_CrsMatrix (Copy, *AA);
83 A->FillComplete();
84
85 // Set default bifurcation values
86 factor = 0.1;
87 leftBC = 1.0;
88 rightBC = 1.0;
89 }
90
91 // Destructor
92 FiniteElementProblem::~FiniteElementProblem()
93 {
94 delete AA;
95 delete A;
96 delete initialSolution;
97 delete Importer;
98 delete OverlapMap;
99 delete StandardMap;
100 }
101
102 // Matrix and Residual Fills
103 bool FiniteElementProblem::evaluate(FillType f,
104 const Epetra_Vector* soln,
105 Epetra_Vector* tmp_rhs,
106 Epetra_RowMatrix* tmp_matrix)
107 {
108 flag = f;
109
110 // Set the incoming linear objects
111 if (flag == F_ONLY) {
112 rhs = tmp_rhs;
113 } else if (flag == MATRIX_ONLY) {
114 A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
115 } else if (flag == ALL) {
116 rhs = tmp_rhs;
117 A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
118 } else {
119 std::cout << "ERROR: FiniteElementProblem::fillMatrix() - FillType flag is broken" << std::endl;
120 throw;
121 }
122
123 // Create the overlapped solution and position vectors
124 Epetra_Vector u(*OverlapMap);
125 Epetra_Vector x(*OverlapMap);
126
127 // Export Solution to Overlap vector
128 u.Import(*soln, *Importer, Insert);
129
130 // Declare required variables
131 int i,j,ierr;
132 int OverlapNumMyElements = OverlapMap->NumMyElements();
133
134 int OverlapMinMyGID;
135 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
136 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
137
138 int row, column;
139 double jac;
140 double xx[2];
141 double uu[2];
142 Basis basis;
143
144 // Create the nodal coordinates
145 double Length=1.0;
146 double dx=Length/((double) NumGlobalElements-1);
147 for (i=0; i < OverlapNumMyElements; i++) {
148 x[i]=dx*((double) OverlapMinMyGID+i);
149 }
150
151 // Zero out the objects that will be filled
152 if ((flag == MATRIX_ONLY) || (flag == ALL)) i=A->PutScalar(0.0);
153 if ((flag == F_ONLY) || (flag == ALL)) i=rhs->PutScalar(0.0);
154
155 // Loop Over # of Finite Elements on Processor
156 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
157
158 // Loop Over Gauss Points
159 for(int gp=0; gp < 2; gp++) {
160 // Get the solution and coordinates at the nodes
161 xx[0]=x[ne];
162 xx[1]=x[ne+1];
163 uu[0]=u[ne];
164 uu[1]=u[ne+1];
165 // Calculate the basis function at the gauss point
166 basis.getBasis(gp, xx, uu);
167
168 // Loop over Nodes in Element
169 for (i=0; i< 2; i++) {
170 row=OverlapMap->GID(ne+i);
171 //printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n",
172 // MyPID, row, ne+i,StandardMap.MyGID(row));
173 if (StandardMap->MyGID(row)) {
174 if ((flag == F_ONLY) || (flag == ALL)) {
175 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
176 +basis.wt*basis.dx
177 *((-1.0/(basis.dx*basis.dx))*basis.duu*
178 basis.dphide[i]+factor*basis.uu*basis.uu*basis.uu*basis.phi[i]);
179 }
180 }
181 // Loop over Trial Functions
182 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
183 for(j=0;j < 2; j++) {
184 if (StandardMap->MyGID(row)) {
185 column=OverlapMap->GID(ne+j);
186 jac=basis.wt*basis.dx*((-1.0/(basis.dx*basis.dx))*
187 basis.dphide[j]*basis.dphide[i]
188 +3.0*factor*basis.uu*basis.uu*basis.phi[j]*
189 basis.phi[i]);
190 ierr=A->SumIntoGlobalValues(row, 1, &jac, &column);
191 }
192 }
193 }
194 }
195 }
196 }
197
198 // Insert Boundary Conditions and modify Jacobian and function (F)
199 // U(0)=1
200 if (MyPID==0) {
201 if ((flag == F_ONLY) || (flag == ALL))
202 (*rhs)[0]= (*soln)[0] - leftBC;
203 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
204 column=0;
205 jac=1.0;
206 A->ReplaceGlobalValues(0, 1, &jac, &column);
207 column=1;
208 jac=0.0;
209 A->ReplaceGlobalValues(0, 1, &jac, &column);
210 }
211 }
212
213 if ( StandardMap->LID(StandardMap->MaxAllGID()) >= 0 ) {
214 int lastDof = StandardMap->LID(StandardMap->MaxAllGID());
215 if ((flag == F_ONLY) || (flag == ALL))
216 (*rhs)[lastDof] = (*soln)[lastDof] - rightBC;
217 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
218 int row = StandardMap->MaxAllGID();
219 column = row;
220 jac=1.0;
221 A->ReplaceGlobalValues(row, 1, &jac, &column);
222 column=row-1;
223 jac=0.0;
224 A->ReplaceGlobalValues(row, 1, &jac, &column);
225 }
226 }
227 // Sync up processors to be safe
228 Comm->Barrier();
229
230 A->FillComplete();
231
232 return true;
233 }
234
235 Epetra_Vector& FiniteElementProblem::getSolution()
236 {
237 return *initialSolution;
238 }
239
240 Epetra_CrsMatrix& FiniteElementProblem::getJacobian()
241 {
242 return *A;
243 }
244
245 bool FiniteElementProblem::setParameter(std::string label, double value)
246 {
247 if (label == "Nonlinear Factor")
248 factor = value;
249 else if (label == "Left BC")
250 leftBC = value;
251 else if (label == "Right BC")
252 rightBC = value*scale;
253 else if (label == "Homotopy Continuation Parameter") {
254 // do nothing for now
255 }
256 else {
257 std::cout << "ERROR: FiniteElementProblem::setParameter() - label is invalid "
258 << "for this problem!" << std::endl;
259 exit(-1);
260 }
261 return true;
262 }
263
264 Epetra_CrsGraph& FiniteElementProblem::generateGraph(Epetra_CrsGraph& AAA)
265 {
266
267 // Declare required variables
268 int i,j;
269 int row, column;
270 int OverlapNumMyElements = OverlapMap->NumMyElements();
271 int OverlapMinMyGID;
272 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
273 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
274
275 // Loop Over # of Finite Elements on Processor
276 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
277
278 // Loop over Nodes in Element
279 for (i=0; i< 2; i++) {
280 row=OverlapMap->GID(ne+i);
281
282 // Loop over Trial Functions
283 for(j=0;j < 2; j++) {
284
285 // If this row is owned by current processor, add the index
286 if (StandardMap->MyGID(row)) {
287 column=OverlapMap->GID(ne+j);
288 AAA.InsertGlobalIndices(row, 1, &column);
289 }
290 }
291 }
292 }
293 AAA.FillComplete();
294 // AAA.SortIndices();
295 // AAA.RemoveRedundantIndices();
296 return AAA;
297 }