Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/nlnml_preconditioner_utils.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 /* ******************************************************************** */
11 /* See the file COPYRIGHT for a complete copyright notice, contact */
12 /* person and disclaimer. */
13 /* ******************************************************************** */
14 /*!
15 * \file nlnml_preconditioner_utils.cpp
16 *
17 * \brief ML nonlinear preconditioner and solver utilities
18 *
19 * \date Last update do Doxygen: 31-Mar-05
20 *
21 */
22 // ML-headers
23 #include "ml_common.h"
24 #include "TrilinosCouplings_config.h"
25
26 #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)
27
28 #include <ctime>
29 #include <cstdlib>
30 #include <iostream>
31 #include <string>
32 #include <vector>
33
34 #include "Epetra_Comm.h"
35
36 #include "nlnml_preconditioner_utils.H"
37
38 /*----------------------------------------------------------------------*
39 | make a deep copy of a graph m.gee 01/05|
40 | allocate the new graph |
41 *----------------------------------------------------------------------*/
42 Epetra_CrsGraph* NLNML::deepcopy_graph(const Epetra_CrsGraph* oldgraph)
43 {
44 int i;
45 int nrows = oldgraph->NumMyRows();
46 int* nIndicesperRow = new int[nrows];
47
48 for (i=0; i<nrows; i++)
49 nIndicesperRow[i] = oldgraph->NumMyIndices(i);
50 Epetra_CrsGraph* graph = new Epetra_CrsGraph(Copy,oldgraph->RowMap(),oldgraph->ColMap(),
51 &(nIndicesperRow[0]));
52 delete [] nIndicesperRow;
53 nIndicesperRow = 0;
54
55 for (i=0; i<nrows; i++)
56 {
57 int numIndices;
58 int* Indices=0;
59 (void) oldgraph->ExtractMyRowView(i,numIndices,Indices);
60 (void) graph->InsertMyIndices(i,numIndices,Indices);
61 }
62
63 graph->FillComplete();
64 return graph;
65 }
66
67 /*----------------------------------------------------------------------*
68 | color a graph in a collapsed way m.gee 05/05|
69 | note: |
70 | this routine makes 2 assumptions: |
71 | - the blocksize is constant everywhere |
72 | - degrees of freedom (rows) on a node(block) a contigous |
73 *----------------------------------------------------------------------*/
74 RefCountPtr<Epetra_MapColoring> NLNML::Collapsedcoloring(Epetra_CrsGraph* cgraph,
75 const int bsize,
76 bool diagonalonly,
77 int printlevel)
78 {
79 // create a new rangemap for the amalgamated graph
80 int new_nummyrows = cgraph->NumMyRows();
81 int new_numglobalrows = cgraph->NumGlobalRows();
82 int lok=1;
83 int gok=1;
84 if (new_nummyrows % bsize != 0 || new_numglobalrows % bsize != 0)
85 {
86 lok = 0;
87 if (printlevel>5)
88 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
89 << "**WRN**: cannot amalgamate cgraph\n"
90 << "**WRN**: numlocalrows%bsize= " << (new_nummyrows % bsize) << " numglobalrows%bsize= " << (new_numglobalrows % bsize) << std::endl;
91 }
92 cgraph->Comm().MinAll(&lok,&gok,1);
93 if (!gok)
94 return null;
95
96 lok=1;
97 gok=1;
98 // number of local and global rows
99 new_nummyrows /= bsize;
100 new_numglobalrows /= bsize;
101 //int* myRows = new int [new_nummyrows];
102 std::vector<int> myRows(new_nummyrows);
103 int counter=0;
104 // calculate which global rows are mine
105 lok=1;
106 gok=1;
107 for (int i=0; i<cgraph->RowMap().NumMyElements(); ++i)
108 {
109 int old_grow = cgraph->RowMap().GID(i);
110 if (old_grow<0)
111 {
112 if (printlevel>5)
113 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
114 << "**WRN**: cgraph->RowMap().GID() returned " << old_grow << std::endl
115 << "**WRN**: switching to none-collapsed coloring\n"
116 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
117 lok=0;
118 }
119 if (lok==0)
120 break;
121 if (old_grow % bsize != 0)
122 continue;
123 int new_grow = old_grow/bsize;
124 myRows[counter] = new_grow;
125 ++counter;
126 }
127 cgraph->Comm().MinAll(&lok,&gok,1);
128 if (!gok)
129 return null;
130
131 if (counter != new_nummyrows)
132 {
133 if (printlevel>5)
134 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
135 << "**WRN**: collapsed coloring problem, switching to standard coloring\n"
136 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
137 lok=0;
138 }
139 cgraph->Comm().MinAll(&lok,&gok,1);
140 if (!gok)
141 return null;
142
143 // create the BlockMap
144 Epetra_BlockMap newrowmap(new_numglobalrows,new_nummyrows,&myRows[0],1,0,cgraph->Comm());
145 myRows.clear();
146
147 // create the nodal cgraph
148 Epetra_CrsGraph nodegraph(Copy,newrowmap,27);
149
150 // loop over NumMyElements in old graph and insert every bsize row in
151 // new graph
152 int new_length=200;
153 std::vector<int> new_gindices(new_length);
154 //int* new_gindices = new int[new_length];
155
156 lok=1;
157 gok=1;
158 for (int i=0; i<cgraph->RowMap().NumMyElements(); ++i)
159 {
160 int old_grow = cgraph->RowMap().GID(i);
161 if (old_grow<0)
162 {
163 if (printlevel>5)
164 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
165 << "**WRN**: cgraph->RowMap().GID() returned " << old_grow << std::endl
166 << "**WRN**: switching to none-collapsed coloring\n"
167 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
168 lok=0;
169 }
170 if (old_grow % bsize != 0)
171 continue;
172 if (lok==0)
173 break;
174 int new_grow = old_grow/bsize;
175
176 // extract the row from the old cgraph
177 int old_numindices;
178 int* old_colindices;
179 int err = cgraph->ExtractMyRowView(i,old_numindices,old_colindices);
180 if (err)
181 {
182 if (printlevel>5)
183 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
184 << "**WRN**: cgraph->ExtractMyRowView returned " << err << std::endl
185 << "**WRN**: switching to none-collapsed coloring\n"
186 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
187 lok=0;
188 }
189 if (lok==0)
190 break;
191
192 // check whether the allocated row for the new cgraph is long enough
193 if (old_numindices>new_length)
194 {
195 new_length = old_numindices;
196 new_gindices.resize(new_length);
197 }
198
199 // calculate the row in global numbering for the new cgraph
200 int new_numindices=0;
201 for (int j=0; j<old_numindices; ++j)
202 {
203 int old_gcol = cgraph->ColMap().GID(old_colindices[j]);
204 if (old_gcol<0)
205 {
206 if (printlevel>5)
207 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
208 << "**WRN**: cgraph->ColMap().GID(old_colindices[j]) returned " << old_gcol << std::endl
209 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
210 }
211 if (lok==0)
212 break;
213 if (old_gcol % bsize != 0)
214 continue;
215
216 int new_gcol = old_gcol/bsize;
217 new_gindices[new_numindices] = new_gcol;
218 ++new_numindices;
219 }
220
221 // insert the row in the new graph
222 err = nodegraph.InsertGlobalIndices(new_grow,new_numindices,&new_gindices[0]);
223 if (err!=0 && err !=1)
224 {
225 if (printlevel>5)
226 std::cout << "**WRN**: NLNML::Collapsedcoloring:\n"
227 << "**WRN**: nodegraph.InsertGlobalIndices returned " << err << std::endl
228 << "**WRN**: switching to none-collapsed coloring\n"
229 << "**WRN**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
230 lok=0;
231 }
232 if (lok==0)
233 break;
234 }
235 // catch all errors that may have occurred in this loop
236 cgraph->Comm().MinAll(&lok,&gok,1);
237 if (!gok)
238 return null;
239
240 nodegraph.FillComplete();
241 nodegraph.OptimizeStorage();
242
243 // tidy up
244 new_gindices.clear();
245
246 EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType =
247 EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN;
248
249 RefCountPtr<EpetraExt::CrsGraph_MapColoring> node_MapColoring =
250 rcp(new EpetraExt::CrsGraph_MapColoring(algType,0,diagonalonly,0));
251
252 Epetra_MapColoring* node_colorMap = &(*node_MapColoring)(nodegraph);
253
254 // now get the colors out of the node_colorMap and create a colorMap for the original system
255 int node_ncolors = node_colorMap->NumColors();
256 int* node_colors = node_colorMap->ElementColors();
257 int* node_loc = node_colorMap->ListOfColors();
258 std::vector<int> col_colors(cgraph->ColMap().NumMyElements());
259
260 // node_ncolors is a local value, we need the global highest color number here
261 int lmax = 0;
262 int gmax = 0;
263 for (int i=0; i<node_ncolors; ++i)
264 if (lmax<node_loc[i]) lmax = node_loc[i];
265 cgraph->Comm().MaxAll(&lmax,&gmax,1);
266
267 // expand the colors to point wise columns
268 int j=0;
269 for (int i=0; i<nodegraph.ColMap().NumMyElements(); ++i)
270 for (int k=0; k<bsize; ++k)
271 {
272 col_colors[j] = node_colors[i]+k*gmax;
273 ++j;
274 }
275 lok=1;
276 gok=1;
277 if (j!=cgraph->ColMap().NumMyElements())
278 {
279 if (printlevel>5)
280 std::cout << "**ERR**: NLNML::Collapsedcoloring:\n"
281 << "**ERR**: error expanding node colors to column colors\n"
282 << "**WRN**: switching to none-collapsed coloring\n"
283 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n";
284 lok=0;
285 }
286 cgraph->Comm().MinAll(&lok,&gok,1);
287 if (!gok)
288 return null;
289
290 RefCountPtr<Epetra_MapColoring> colorMap =
291 rcp(new Epetra_MapColoring(cgraph->ColMap(),&col_colors[0]));
292
293 return colorMap;
294 }
295
296 /*----------------------------------------------------------------------*
297 | color a graph in a standard way m.gee 05/05|
298 *----------------------------------------------------------------------*/
299 RefCountPtr<Epetra_MapColoring> NLNML::Standardcoloring(
300 Epetra_CrsGraph* graph,
301 bool diagonalonly)
302 {
303 EpetraExt::CrsGraph_MapColoring::ColoringAlgorithm algType =
304 EpetraExt::CrsGraph_MapColoring::JONES_PLASSMAN;
305
306 RefCountPtr<EpetraExt::CrsGraph_MapColoring> MapColoring =
307 rcp(new EpetraExt::CrsGraph_MapColoring(algType,0,diagonalonly,0));
308
309 RefCountPtr<Epetra_MapColoring> colorMap = rcp(&(*MapColoring)(*graph));
310
311 return colorMap;
312 }
313
314 /*----------------------------------------------------------------------*
315 | m.gee 11/05|
316 *----------------------------------------------------------------------*/
317 #include <cstdio>
318 bool NLNML::Print_Epetra_CrsMatrix(Epetra_CrsMatrix& matrix)
319 {
320 using namespace std;
321 for (int i=0; i<matrix.NumMyRows(); ++i)
322 {
323 printf("Lrow %5d: ",i); fflush(stdout);
324 int numentries;
325 int* indices;
326 double* values;
327 matrix.ExtractMyRowView(i,numentries,values,indices);
328 for (int j=0; j<numentries; ++j)
329 printf("%5d %10.3e ",indices[j],values[j]);
330 printf("\n"); fflush(stdout);
331 }
332 return true;
333 }
334
335 /*----------------------------------------------------------------------*
336 | m.gee 11/05|
337 *----------------------------------------------------------------------*/
338 RefCountPtr<Epetra_CrsMatrix> NLNML::StripZeros(
339 RefCountPtr<Epetra_CrsMatrix> in, double eps)
340 {
341 RefCountPtr<Epetra_CrsMatrix> out =
342 rcp(new Epetra_CrsMatrix(Copy,in->RowMap(),200));
343 for (int lrow=0; lrow<in->NumMyRows(); ++lrow)
344 {
345 int grow = in->GRID(lrow);
346 if (grow<0) { std::cout << "ERROR: grow<0 \n"; exit(0); }
347 int numentries;
348 int* lindices;
349 double* values;
350 int err = in->ExtractMyRowView(lrow,numentries,values,lindices);
351 if (err) { std::cout << "ExtractMyRowView returned " << err << std::endl; exit(0);}
352 for (int j=0; j<numentries; ++j)
353 {
354 int lcol = lindices[j];
355 int gcol = in->GCID(lcol);
356 if (gcol<0) { std::cout << "ERROR: gcol<0 \n"; exit(0); }
357 if (fabs(values[j])<eps && gcol != grow)
358 continue;
359 int err = out->InsertGlobalValues(grow,1,&values[j],&gcol);
360 if (err) { std::cout << "InsertGlobalValues returned " << err << std::endl; exit(0);}
361 }
362 }
363 out->FillComplete();
364 out->OptimizeStorage();
365 return out;
366 }
367
368 #endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA)