Debian Codegraph @

packages/trilinoscouplings/examples/epetraext/EpetraExt_AmesosBTF_GraphTrans_Ex.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 // CrsGraph_BTF Test routine
11
12 #include <EpetraExt_ConfigDefs.h>
13 #include "EpetraExt_Version.h"
14
15 #include "Epetra_Time.h"
16 #ifdef EPETRA_MPI
17 #include "Epetra_MpiComm.h"
18 #include <mpi.h>
19 #endif
20
21 #include "Epetra_SerialComm.h"
22 #include "Epetra_Map.h"
23 #include "Epetra_CrsGraph.h"
24 #include "Epetra_CrsMatrix.h"
25 #include "Epetra_MultiVector.h"
26 #include "Epetra_LinearProblem.h"
27 #include "EpetraExt_AmesosBTF_CrsGraph.h"
28 #include "EpetraExt_LPTrans_From_GraphTrans.h"
29 #include "EpetraExt_Reindex_LinearProblem.h"
30
31 int main(int argc, char *argv[]) {
32
33 int returnierr=0;
34
35 using std::cout;
36 using std::endl;
37 using std::flush;
38
39 #ifdef EPETRA_MPI
40
41 // Initialize MPI
42
43 MPI_Init(&argc,&argv);
44 int size; // Number of MPI processes, My process ID
45
46 MPI_Comm_size(MPI_COMM_WORLD, &size);
47
48 if (size > 1) {
49 cout << "This example cannot be run on more than one processor!" << endl;
50 MPI_Finalize();
51 returnierr = -1;
52 return returnierr;
53 }
54
55 #endif
56
57 bool verbose = false;
58
59 // Check if we should print results to standard out
60 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
61
62
63 #ifdef EPETRA_MPI
64 Epetra_MpiComm Comm(MPI_COMM_WORLD);
65 #else
66 Epetra_SerialComm Comm;
67 #endif
68 if (!verbose) Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
69
70 if (verbose) {
71 cout << EpetraExt::EpetraExt_Version() << endl << endl;
72 cout << Comm << endl << flush;
73 }
74
75 Comm.Barrier();
76
77 int NumMyElements = 3;
78
79 Epetra_Map Map( NumMyElements, 0, Comm );
80
81 Epetra_CrsGraph Graph( Copy, Map, 1 );
82
83 int index[2];
84 index[0] = 2;
85 Graph.InsertGlobalIndices( 0, 1, &index[0] );
86 index[0] = 0;
87 index[1] = 2;
88 Graph.InsertGlobalIndices( 1, 2, &index[0] );
89 index[0] = 1;
90 Graph.InsertGlobalIndices( 2, 1, &index[0] );
91
92 Graph.FillComplete();
93 if (verbose) {
94 cout << "***************** PERFORMING BTF TRANSFORM ON CRS_GRAPH *****************" <<endl<<endl;
95 cout << "CrsGraph *before* BTF transform: " << endl << endl;
96 cout << Graph << endl;
97 }
98
99 EpetraExt::AmesosBTF_CrsGraph BTFTrans( true, verbose );
100 Epetra_CrsGraph & NewBTFGraph = BTFTrans( Graph );
101
102 if (verbose) {
103 cout << "CrsGraph *after* BTF transform: " << endl << endl;
104 cout << NewBTFGraph << endl;
105 }
106
107 // Use BTF graph transformation to solve linear system.
108 // Create an Epetra::CrsMatrix
109 Epetra_CrsMatrix Matrix( Copy, Graph );
110 double value[2];
111 index[0] = 2; value[0] = 3.0;
112 Matrix.ReplaceMyValues( 0, 1, &value[0], &index[0] );
113 index[0] = 0; index[1] = 2;
114 value[0] = 2.0; value[1] = 2.5;
115 Matrix.ReplaceMyValues( 1, 2, &value[0], &index[0] );
116 index[0] = 1; value[0] = 1.0;
117 Matrix.ReplaceMyValues( 2, 1, &value[0], &index[0] );
118 Matrix.FillComplete();
119
120 // Create the solution and right-hand side vectors.
121 Epetra_MultiVector RHS( Map, 1 ), LHS( Map, 1 );
122 LHS.PutScalar( 0.0 );
123 RHS.ReplaceMyValue( 0, 0, 3.0 );
124 RHS.ReplaceMyValue( 1, 0, 4.5 );
125 RHS.ReplaceMyValue( 2, 0, 1.0 );
126 Epetra_LinearProblem problem( &Matrix, &LHS, &RHS );
127
128 if (verbose) {
129 cout << "*************** PERFORMING BTF TRANSFORM ON LINEAR_PROBLEM **************" <<endl<<endl;
130 cout << "CrsMatrix *before* BTF transform: " << endl << endl;
131 cout << Matrix << endl;
132 cout << "MultiVector RHS *before* BTF transform: " << endl << endl;
133 RHS.Print( cout );
134 }
135
136 // Create the linear problem transform.
137 EpetraExt::LinearProblem_GraphTrans * LPTrans =
138 new EpetraExt::LinearProblem_GraphTrans(
139 *(dynamic_cast<EpetraExt::StructuralSameTypeTransform<Epetra_CrsGraph>*>(&BTFTrans)) );
140 Epetra_LinearProblem* tProblem = &((*LPTrans)( problem ));
141 LPTrans->fwd();
142
143 if (verbose) {
144 cout << "CrsMatrix *after* BTF transform: " << endl << endl;
145 dynamic_cast<Epetra_CrsMatrix*>(tProblem->GetMatrix())->Print( cout );
146 cout << "MultiVector RHS *after* BTF transform: " << endl << endl;
147 tProblem->GetRHS()->Print( cout );
148 }
149
150 if (verbose) {
151 cout << endl << "*************** PERFORMING REINDEXING ON LINEAR_PROBLEM **************" <<endl<<endl;
152 }
153 EpetraExt::ViewTransform<Epetra_LinearProblem> * ReIdx_LPTrans =
154 new EpetraExt::LinearProblem_Reindex( 0 );
155
156 Epetra_LinearProblem* tProblem2 = &((*ReIdx_LPTrans)( *tProblem ));
157 ReIdx_LPTrans->fwd();
158
159 if (verbose) {
160 cout << endl << "CrsMatrix *after* BTF transform *and* reindexing: " << endl << endl;
161 dynamic_cast<Epetra_CrsMatrix*>(tProblem2->GetMatrix())->Print( cout );
162 cout << endl <<"Column Map *before* reindexing: " << endl << endl;
163 cout << dynamic_cast<Epetra_CrsMatrix*>(tProblem->GetMatrix())->ColMap() << endl;
164 cout << "Column Map *after* reindexing: " << endl << endl;
165 cout << dynamic_cast<Epetra_CrsMatrix*>(tProblem2->GetMatrix())->ColMap() << endl;
166 }
167
168 #ifdef EPETRA_MPI
169 MPI_Finalize();
170 #endif
171
172 return returnierr;
173 }
174