Debian Codegraph @

packages/nox/examples/epetra/NOX_1DFEMNonlinear/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 "Teuchos_Assert.hpp"
18 #include "Basis.H"
19
20 #include "FiniteElementProblem.H"
21
22 // Constructor - creates the Epetra objects (maps and vectors)
23 FiniteElementProblem::FiniteElementProblem(int numGlobalElements, Epetra_Comm& comm) :
24 Comm(&comm),
25 NumGlobalElements(numGlobalElements)
26 {
27
28 // Commonly used variables
29 int i;
30 MyPID = Comm->MyPID(); // Process ID
31 NumProc = Comm->NumProc(); // Total number of processes
32
33 // Construct a Source Map that puts approximately the same
34 // Number of equations on each processor in uniform global ordering
35 StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm);
36
37 // Get the number of elements owned by this processor
38 NumMyElements = StandardMap->NumMyElements();
39
40 // Construct an overlaped map for the finite element fill *************
41 // For single processor jobs, the overlap and standard map are the same
42 if (NumProc == 1) {
43 OverlapMap = new Epetra_Map(*StandardMap);
44 } else {
45
46 int OverlapNumMyElements;
47 int OverlapMinMyGID;
48 OverlapNumMyElements = NumMyElements + 2;
49 if ((MyPID == 0) || (MyPID == NumProc - 1))
50 OverlapNumMyElements --;
51
52 if (MyPID==0)
53 OverlapMinMyGID = StandardMap->MinMyGID();
54 else
55 OverlapMinMyGID = StandardMap->MinMyGID() - 1;
56
57 int* OverlapMyGlobalElements = new int[OverlapNumMyElements];
58
59 for (i = 0; i < OverlapNumMyElements; i ++)
60 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
61
62 OverlapMap = new Epetra_Map(-1, OverlapNumMyElements,
63 OverlapMyGlobalElements, 0, *Comm);
64
65 delete [] OverlapMyGlobalElements;
66
67 } // End Overlap map construction *************************************
68
69 // Construct Linear Objects
70 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
71 initialSolution = Teuchos::rcp(new Epetra_Vector(*StandardMap));
72 AA = new Epetra_CrsGraph(Copy, *StandardMap, 5);
73
74 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
75 generateGraph(*AA);
76
77 // Create a second matrix using graph of first matrix - this creates a
78 // static graph so we can refill the new matirx after FillComplete()
79 // is called.
80 A = Teuchos::rcp(new Epetra_CrsMatrix (Copy, *AA));
81 A->FillComplete();
82 }
83
84 // Destructor
85 FiniteElementProblem::~FiniteElementProblem()
86 {
87 delete AA;
88 delete Importer;
89 delete OverlapMap;
90 delete StandardMap;
91 }
92
93 // Matrix and Residual Fills
94 bool FiniteElementProblem::evaluate(FillType f,
95 const Epetra_Vector* soln,
96 Epetra_Vector* tmp_rhs,
97 Epetra_RowMatrix* tmp_matrix)
98 {
99 flag = f;
100
101 // Set the incoming linear objects
102 if (flag == F_ONLY) {
103 rhs = tmp_rhs;
104 } else if (flag == MATRIX_ONLY) {
105 // RPP: A doesn't need to be set, we are implicitly filling tmp_matrix
106 // with a shared jacobian.
107 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
108 } else if (flag == ALL) {
109 rhs = tmp_rhs;
110 //A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
111 } else {
112 std::cout << "ERROR: FiniteElementProblem::fillMatrix() - FillType flag is broken" << std::endl;
113 throw;
114 }
115
116 // Create the overlapped solution and position vectors
117 Epetra_Vector u(*OverlapMap);
118 Epetra_Vector x(*OverlapMap);
119
120 // Export Solution to Overlap vector
121 u.Import(*soln, *Importer, Insert);
122
123 // Declare required variables
124 int i,j,ierr;
125 int OverlapNumMyElements = OverlapMap->NumMyElements();
126
127 int OverlapMinMyGID;
128 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
129 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
130
131 int row, column;
132 double factor=1000.0;
133 double jac;
134 double xx[2];
135 double uu[2];
136 Basis basis;
137
138 // Create the nodal coordinates
139 double Length=1.0;
140 double dx=Length/((double) NumGlobalElements-1);
141 for (i=0; i < OverlapNumMyElements; i++) {
142 x[i]=dx*((double) OverlapMinMyGID+i);
143 }
144
145 // Zero out the objects that will be filled
146 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
147 i = A->PutScalar(0.0);
148 assert(i == 0);
149 }
150 if ((flag == F_ONLY) || (flag == ALL)) {
151 i = rhs->PutScalar(0.0);
152 assert(i == 0);
153 }
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.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 +2.0*factor*basis.uu*basis.phi[j]*
189 basis.phi[i]);
190 ierr=A->SumIntoGlobalValues(row, 1, &jac, &column);
191 TEUCHOS_ASSERT(ierr == 0);
192 }
193 }
194 }
195 }
196 }
197 }
198
199 // Insert Boundary Conditions and modify Jacobian and function (F)
200 // U(0)=1
201 if (MyPID==0) {
202 if ((flag == F_ONLY) || (flag == ALL))
203 (*rhs)[0]= (*soln)[0] - 1.0;
204 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
205 column=0;
206 jac=1.0;
207 A->ReplaceGlobalValues(0, 1, &jac, &column);
208 column=1;
209 jac=0.0;
210 A->ReplaceGlobalValues(0, 1, &jac, &column);
211 }
212 }
213
214 // Sync up processors to be safe
215 Comm->Barrier();
216
217 A->FillComplete();
218
219 return true;
220 }
221
222 Teuchos::RCP<Epetra_Vector> FiniteElementProblem::getSolution()
223 {
224 return initialSolution;
225 }
226
227 Teuchos::RCP<Epetra_CrsMatrix> FiniteElementProblem::getJacobian()
228 {
229 return A;
230 }
231
232 Epetra_CrsGraph& FiniteElementProblem::generateGraph(Epetra_CrsGraph& AA)
233 {
234
235 // Declare required variables
236 int i,j;
237 int row, column;
238 int OverlapNumMyElements = OverlapMap->NumMyElements();
239
240 // Loop Over # of Finite Elements on Processor
241 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
242
243 // Loop over Nodes in Element
244 for (i=0; i< 2; i++) {
245 row=OverlapMap->GID(ne+i);
246
247 // Loop over Trial Functions
248 for(j=0;j < 2; j++) {
249
250 // If this row is owned by current processor, add the index
251 if (StandardMap->MyGID(row)) {
252 column=OverlapMap->GID(ne+j);
253 AA.InsertGlobalIndices(row, 1, &column);
254 }
255 }
256 }
257 }
258 AA.FillComplete();
259 // AA.SortIndices();
260 // AA.RemoveRedundantIndices();
261 return AA;
262 }