Debian Codegraph @

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