Debian Codegraph @

packages/muelu/test/navierstokes/Navier2D_Helpers.h

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10
11 namespace MueLuTests {
12
13 // helper routines
14 bool SplitMatrix2x2(Teuchos::RCP<const Epetra_CrsMatrix> A,
15 const Epetra_Map& A11rowmap,
16 const Epetra_Map& A22rowmap,
17 Teuchos::RCP<Epetra_CrsMatrix>& A11,
18 Teuchos::RCP<Epetra_CrsMatrix>& A12,
19 Teuchos::RCP<Epetra_CrsMatrix>& A21,
20 Teuchos::RCP<Epetra_CrsMatrix>& A22)
21 {
22 if (A==Teuchos::null) {
23 std::cout << "ERROR: SplitMatrix2x2: A==null on entry" << std::endl;
24 return false;
25 }
26
27 const Epetra_Comm& Comm = A->Comm();
28 const Epetra_Map& A22map = A22rowmap;
29 const Epetra_Map& A11map = A11rowmap;
30
31 //----------------------------- create a parallel redundant map of A22map
32 std::map<int,int> a22gmap;
33 {
34 std::vector<int> a22global(A22map.NumGlobalElements());
35 int count=0;
36 for (int proc=0; proc<Comm.NumProc(); ++proc) {
37 int length = 0;
38 if (proc==Comm.MyPID()) {
39 for (int i=0; i<A22map.NumMyElements(); ++i) {
40 a22global[count+length] = A22map.GID(i);
41 ++length;
42 }
43 }
44 Comm.Broadcast(&length,1,proc);
45 Comm.Broadcast(&a22global[count],length,proc);
46 count += length;
47 }
48 if (count != A22map.NumGlobalElements()) {
49 std::cout << "ERROR SplitMatrix2x2: mismatch in dimensions" << std::endl;
50 return false;
51 }
52
53 // create the map
54 for (int i=0; i<count; ++i)
55 a22gmap[a22global[i]] = 1;
56 a22global.clear();
57 }
58
59 //--------------------------------------------------- create matrix A22
60 A22 = Teuchos::rcp(new Epetra_CrsMatrix(Copy,A22map,100));
61 {
62 std::vector<int> a22gcindices(100);
63 std::vector<double> a22values(100);
64 for (int i=0; i<A->NumMyRows(); ++i) {
65 const int grid = A->GRID(i);
66 if (A22map.MyGID(grid)==false)
67 continue;
68 int numentries;
69 double* values;
70 int* cindices;
71 int err = A->ExtractMyRowView(i,numentries,values,cindices);
72 if (err) {
73 std::cout << "ERROR: SplitMatrix2x2: A->ExtractMyRowView returned " << err << std::endl;
74 return false;
75 }
76
77 if (numentries>(int)a22gcindices.size()) {
78 a22gcindices.resize(numentries);
79 a22values.resize(numentries);
80 }
81 int count=0;
82 for (int j=0; j<numentries; ++j) {
83 const int gcid = A->ColMap().GID(cindices[j]);
84 // see whether we have gcid in a22gmap
85 std::map<int,int>::iterator curr = a22gmap.find(gcid);
86 if (curr==a22gmap.end()) continue;
87 //cout << gcid << " ";
88 a22gcindices[count] = gcid;
89 a22values[count] = values[j];
90 ++count;
91 }
92 //cout << endl; fflush(stdout);
93 // add this filtered row to A22
94 err = A22->InsertGlobalValues(grid,count,&a22values[0],&a22gcindices[0]);
95 if (err<0) {
96 std::cout << "ERROR: SplitMatrix2x2: A->InsertGlobalValues returned " << err << std::endl;
97 return false;
98 }
99
100 } //for (int i=0; i<A->NumMyRows(); ++i)
101 a22gcindices.clear();
102 a22values.clear();
103 }
104 A22->FillComplete();
105 A22->OptimizeStorage();
106
107 //----------------------------------------------------- create matrix A11
108 A11 = Teuchos::rcp(new Epetra_CrsMatrix(Copy,A11map,100));
109 {
110 std::vector<int> a11gcindices(100);
111 std::vector<double> a11values(100);
112 for (int i=0; i<A->NumMyRows(); ++i)
113 {
114 const int grid = A->GRID(i);
115 if (A11map.MyGID(grid)==false) continue;
116 int numentries;
117 double* values;
118 int* cindices;
119 int err = A->ExtractMyRowView(i,numentries,values,cindices);
120 if (err) {
121 std::cout << "ERROR: SplitMatrix2x2: A->ExtractMyRowView returned " << err << std::endl;
122 return false;
123 }
124
125 if (numentries>(int)a11gcindices.size()) {
126 a11gcindices.resize(numentries);
127 a11values.resize(numentries);
128 }
129 int count=0;
130 for (int j=0; j<numentries; ++j) {
131 const int gcid = A->ColMap().GID(cindices[j]);
132 // see whether we have gcid as part of a22gmap
133 std::map<int,int>::iterator curr = a22gmap.find(gcid);
134 if (curr!=a22gmap.end()) continue;
135 a11gcindices[count] = gcid;
136 a11values[count] = values[j];
137 ++count;
138 }
139 err = A11->InsertGlobalValues(grid,count,&a11values[0],&a11gcindices[0]);
140 if (err<0) {
141 std::cout << "ERROR: SplitMatrix2x2: A->InsertGlobalValues returned " << err << std::endl;
142 return false;
143 }
144
145 } // for (int i=0; i<A->NumMyRows(); ++i)
146 a11gcindices.clear();
147 a11values.clear();
148 }
149 A11->FillComplete();
150 A11->OptimizeStorage();
151
152 //---------------------------------------------------- create matrix A12
153 A12 = Teuchos::rcp(new Epetra_CrsMatrix(Copy,A11map,100));
154 {
155 std::vector<int> a12gcindices(100);
156 std::vector<double> a12values(100);
157 for (int i=0; i<A->NumMyRows(); ++i) {
158 const int grid = A->GRID(i);
159 if (A11map.MyGID(grid)==false) continue;
160 int numentries;
161 double* values;
162 int* cindices;
163 int err = A->ExtractMyRowView(i,numentries,values,cindices);
164 if (err) {
165 std::cout << "ERROR: SplitMatrix2x2: A->ExtractMyRowView returned " << err << std::endl;
166 return false;
167 }
168
169 if (numentries>(int)a12gcindices.size()) {
170 a12gcindices.resize(numentries);
171 a12values.resize(numentries);
172 }
173 int count=0;
174 for (int j=0; j<numentries; ++j) {
175 const int gcid = A->ColMap().GID(cindices[j]);
176 // see whether we have gcid as part of a22gmap
177 std::map<int,int>::iterator curr = a22gmap.find(gcid);
178 if (curr==a22gmap.end()) continue;
179 a12gcindices[count] = gcid;
180 a12values[count] = values[j];
181 ++count;
182 }
183 err = A12->InsertGlobalValues(grid,count,&a12values[0],&a12gcindices[0]);
184 if (err<0) {
185 std::cout << "ERROR: SplitMatrix2x2: A->InsertGlobalValues returned " << err << std::endl;
186 return false;
187 }
188
189 } // for (int i=0; i<A->NumMyRows(); ++i)
190 a12values.clear();
191 a12gcindices.clear();
192 }
193 A12->FillComplete(A22map,A11map);
194 A12->OptimizeStorage();
195
196 //----------------------------------------------------------- create A21
197 A21 = Teuchos::rcp(new Epetra_CrsMatrix(Copy,A22map,100));
198 {
199 std::vector<int> a21gcindices(100);
200 std::vector<double> a21values(100);
201 for (int i=0; i<A->NumMyRows(); ++i) {
202 const int grid = A->GRID(i);
203 if (A22map.MyGID(grid)==false) continue;
204 int numentries;
205 double* values;
206 int* cindices;
207 int err = A->ExtractMyRowView(i,numentries,values,cindices);
208 if (err) {
209 std::cout << "ERROR: SplitMatrix2x2: A->ExtractMyRowView returned " << err << std::endl;
210 return false;
211 }
212
213 if (numentries>(int)a21gcindices.size()) {
214 a21gcindices.resize(numentries);
215 a21values.resize(numentries);
216 }
217 int count=0;
218 for (int j=0; j<numentries; ++j) {
219 const int gcid = A->ColMap().GID(cindices[j]);
220 // see whether we have gcid as part of a22gmap
221 std::map<int,int>::iterator curr = a22gmap.find(gcid);
222 if (curr!=a22gmap.end()) continue;
223 a21gcindices[count] = gcid;
224 a21values[count] = values[j];
225 ++count;
226 }
227 err = A21->InsertGlobalValues(grid,count,&a21values[0],&a21gcindices[0]);
228 if (err<0) {
229 std::cout << "ERROR: SplitMatrix2x2: A->InsertGlobalValues returned " << err << std::endl;
230 return false;
231 }
232
233 } // for (int i=0; i<A->NumMyRows(); ++i)
234 a21values.clear();
235 a21gcindices.clear();
236 }
237 A21->FillComplete(A11map,A22map);
238 A21->OptimizeStorage();
239
240 //-------------------------------------------------------------- tidy up
241 a22gmap.clear();
242 return true;
243 }
244
245 }