Debian Codegraph @

packages/trilinoscouplings/examples/ml/NonlinML/FiniteElementProblem.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // ml objects
11 #include "ml_common.h"
12 #include "TrilinosCouplings_config.h"
13
14 #if defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT)
15 //#include "NOX_Common.H"
16 #include "Epetra_Comm.h"
17 #include "Epetra_Map.h"
18 #include "Epetra_Vector.h"
19 #include "Epetra_Import.h"
20 #include "Epetra_CrsGraph.h"
21 #include "Epetra_CrsMatrix.h"
22 #include "Basis.H"
23
24 #include "FiniteElementProblem.H"
25
26 // Constructor - creates the Epetra objects (maps and vectors)
27 FiniteElementProblem::FiniteElementProblem(int numGlobalElements, Epetra_Comm& comm) :
28 Comm(&comm),
29 NumGlobalElements(numGlobalElements)
30 {
31
32 // Commonly used variables
33 int i;
34 MyPID = Comm->MyPID(); // Process ID
35 NumProc = Comm->NumProc(); // Total number of processes
36
37 // Construct a Source Map that puts approximately the same
38 // Number of equations on each processor in uniform global ordering
39 StandardMap = new Epetra_Map(NumGlobalElements, 0, *Comm);
40
41 // Get the number of elements owned by this processor
42 NumMyElements = StandardMap->NumMyElements();
43
44 // Construct an overlaped map for the finite element fill *************
45 // For single processor jobs, the overlap and standard map are the same
46 if (NumProc == 1) {
47 OverlapMap = new Epetra_Map(*StandardMap);
48 } else {
49
50 int OverlapNumMyElements;
51 int OverlapMinMyGID;
52 OverlapNumMyElements = NumMyElements + 2;
53 if ((MyPID == 0) || (MyPID == NumProc - 1))
54 OverlapNumMyElements --;
55
56 if (MyPID==0)
57 OverlapMinMyGID = StandardMap->MinMyGID();
58 else
59 OverlapMinMyGID = StandardMap->MinMyGID() - 1;
60
61 int* OverlapMyGlobalElements = new int[OverlapNumMyElements];
62
63 for (i = 0; i < OverlapNumMyElements; i ++)
64 OverlapMyGlobalElements[i] = OverlapMinMyGID + i;
65
66 OverlapMap = new Epetra_Map(-1, OverlapNumMyElements,
67 OverlapMyGlobalElements, 0, *Comm);
68
69 delete [] OverlapMyGlobalElements;
70
71 } // End Overlap map construction *************************************
72
73 // Construct Linear Objects
74 Importer = new Epetra_Import(*OverlapMap, *StandardMap);
75 initialSolution = new Epetra_Vector(*StandardMap);
76 AA = new Epetra_CrsGraph(Copy, *StandardMap, 5);
77
78 // Allocate the memory for a matrix dynamically (i.e. the graph is dynamic).
79 generateGraph(*AA);
80
81 // Create a second matrix using graph of first matrix - this creates a
82 // static graph so we can refill the new matirx after FillComplete()
83 // is called.
84 A = new Epetra_CrsMatrix (Copy, *AA);
85 A->FillComplete();
86 A->OptimizeStorage();
87 }
88
89 // Destructor
90 FiniteElementProblem::~FiniteElementProblem()
91 {
92 delete AA;
93 delete A;
94 delete initialSolution;
95 delete Importer;
96 delete OverlapMap;
97 delete StandardMap;
98 }
99
100 // Matrix and Residual Fills
101 bool FiniteElementProblem::evaluate(FillType f,
102 const Epetra_Vector* soln,
103 Epetra_Vector* tmp_rhs,
104 Epetra_RowMatrix* tmp_matrix)
105 {
106 flag = f;
107
108 // Set the incoming linear objects
109 if (flag == F_ONLY) {
110 rhs = tmp_rhs;
111 } else if (flag == MATRIX_ONLY) {
112 // A was allocated in the constructor
113 // A = dynamic_cast<Epetra_CrsMatrix*> (tmp_matrix);
114 } else if (flag == ALL) {
115 rhs = tmp_rhs;
116 // A was allocated in the constructor
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;
132 #ifndef NDEBUG
133 int ierr;
134 #endif
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 //printf("Proc=%d GlobalRow=%d LocalRow=%d Owned=%d\n",
176 // MyPID, row, ne+i,StandardMap.MyGID(row));
177 if (StandardMap->MyGID(row)) {
178 if ((flag == F_ONLY) || (flag == ALL)) {
179 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
180 +basis.wt*basis.dx
181 *((1.0/(basis.dx*basis.dx))*basis.duu*
182 basis.dphide[i]+factor*basis.uu*basis.uu*basis.phi[i]);
183 }
184 }
185 // Loop over Trial Functions
186 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
187 for(j=0;j < 2; j++) {
188 if (StandardMap->MyGID(row)) {
189 column=OverlapMap->GID(ne+j);
190 jac=basis.wt*basis.dx*((1.0/(basis.dx*basis.dx))*
191 basis.dphide[j]*basis.dphide[i]
192 +2.0*factor*basis.uu*basis.phi[j]*
193 basis.phi[i]);
194 # ifndef NDEBUG
195 ierr=
196 # endif
197 A->SumIntoGlobalValues(row, 1, &jac, &column);
198 assert(ierr==0);
199 }
200 }
201 }
202 }
203 }
204 }
205
206 // Insert Boundary Conditions and modify Jacobian and function (F)
207 // U(0)=1
208 if (MyPID==0) {
209 if ((flag == F_ONLY) || (flag == ALL))
210 (*rhs)[0]= (*soln)[0] - 1.0;
211 if ((flag == MATRIX_ONLY) || (flag == ALL)) {
212 column=0;
213 jac=1.0;
214 A->ReplaceGlobalValues(0, 1, &jac, &column);
215 column=1;
216 jac=0.0;
217 A->ReplaceGlobalValues(0, 1, &jac, &column);
218 }
219 }
220
221 // Sync up processors to be safe
222 Comm->Barrier();
223
224 A->FillComplete();
225 A->OptimizeStorage();
226
227 return true;
228 }
229
230 Epetra_Vector& FiniteElementProblem::getSolution()
231 {
232 return *initialSolution;
233 }
234
235 Epetra_CrsGraph& FiniteElementProblem::generateGraph(Epetra_CrsGraph& AA)
236 {
237
238 // Declare required variables
239 int i,j;
240 int row, column;
241 int OverlapNumMyElements = OverlapMap->NumMyElements();
242 int OverlapMinMyGID;
243 if (MyPID==0) OverlapMinMyGID = StandardMap->MinMyGID();
244 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
245
246 if (OverlapMinMyGID < 0)
247 {
248 // this is really just here to keep the compiler from complaining
249 // about the unused variable, but I do want it to be there so
250 // the example includes what this function will return.
251 std::cout << "broken" << std::endl ;
252 }
253
254 // Loop Over # of Finite Elements on Processor
255 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
256
257 // Loop over Nodes in Element
258 for (i=0; i< 2; i++) {
259 row=OverlapMap->GID(ne+i);
260
261 // Loop over Trial Functions
262 for(j=0;j < 2; j++) {
263
264 // If this row is owned by current processor, add the index
265 if (StandardMap->MyGID(row)) {
266 column=OverlapMap->GID(ne+j);
267 AA.InsertGlobalIndices(row, 1, &column);
268 }
269 }
270 }
271 }
272 AA.FillComplete();
273 AA.OptimizeStorage();
274 // AA.SortIndices();
275 // AA.RemoveRedundantIndices();
276 return AA;
277 }
278 #endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO)