Debian Codegraph @

packages/aztecoo/test/GMRES_Tests/cxx_main.cpp

raw
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // AztecOO: An Object-Oriented Aztec Linear Solver Package
6 // Copyright (2002) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux ([email protected])
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43
44 #include "AztecOO.h"
45 #include "AztecOO_Version.h"
46
47 #include <cstdlib>
48 #include <cstdio>
49 #include <cmath>
50
51 #ifdef EPETRA_MPI
52 #include "mpi.h"
53 #include "Epetra_MpiComm.h"
54 #else
55 #include "Epetra_SerialComm.h"
56 #endif
57
58 #include "Epetra_Map.h"
59 #include "Epetra_Vector.h"
60 #include "Epetra_CrsMatrix.h"
61
62 int main(int argc, char *argv[]) {
63 #ifdef EPETRA_MPI
64 // Initialize MPI
65 MPI_Init(&argc,&argv);
66 Epetra_MpiComm Comm( MPI_COMM_WORLD );
67 #else
68 Epetra_SerialComm Comm;
69 #endif
70 bool verbose = false; // used to set verbose false on non-root processors
71 bool verbose1 = false; // user's command-line argument
72 // Check if we should print results to standard out
73 if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') {
74 verbose = true;
75 if (Comm.MyPID()==0) verbose1 = true;
76 }
77
78 if (verbose1)
79 std::cout << AztecOO_Version() << std::endl << std::endl;
80 if (verbose)
81 std::cout << Comm <<std::endl;
82
83 int NumGlobalElements = 5;
84 int IndexBase = 0;
85
86 Epetra_Map Map(NumGlobalElements, IndexBase, Comm);
87 int NumMyElements = Map.NumMyElements();
88 int * MyGlobalElements = Map.MyGlobalElements();
89
90 Epetra_CrsMatrix A(Copy,Map,0);
91 Epetra_Vector b(Map);
92 Epetra_Vector x(Map);
93 Epetra_Vector xx(Map);
94
95 if (verbose)
96 std::cout << "Proc = " << Comm.MyPID() << " NumMyElements=" << NumMyElements << std::endl;
97
98 for (int i=0; i<NumMyElements; i++) {
99 int index = MyGlobalElements[i]; // Global Diagonal location
100 double value = -pow(((double)10),((double) index));
101 if (index==0)
102 value = 1.0; // First value will be positive 1, reminder will be negative powers of 10
103 b[i] = value; // RHS has same value as diagonal
104 xx[i] = 1.0; // Makes solution all ones
105 x[i] = 0.0; // Start with zero guess.
106 A.InsertGlobalValues(index, 1, &value, &index);
107 }
108
109 A.FillComplete(); // Signal that data entries are complete.
110
111 if (verbose1) std::cout << "A = " << std::endl;
112 if (verbose) std::cout << A << std::endl;
113 if (verbose1) std::cout << "xx = " << std::endl;
114 if (verbose) std::cout << xx << std::endl;
115 if (verbose1) std::cout << "x = " << std::endl;
116 if (verbose) std::cout << x << std::endl;
117 if (verbose1) std::cout << "b = " << std::endl;
118 if (verbose) std::cout << b << std::endl;
119
120 // Want to solve Ax=b
121 Epetra_LinearProblem problem(&A, &x, &b);
122
123 AztecOO solver(problem);
124
125 solver.SetAztecOption(AZ_scaling, AZ_none); // We want pure GMRES
126 solver.SetAztecOption(AZ_precond, AZ_none);
127 solver.SetAztecOption(AZ_solver, AZ_gmres);
128 solver.SetAztecOption(AZ_max_iter, NumGlobalElements); // Set equal to global dimension
129 solver.SetAztecOption(AZ_kspace, NumGlobalElements);
130 solver.SetAztecOption(AZ_diagnostics, AZ_none);
131 if (!verbose)
132 solver.SetAztecOption(AZ_output, AZ_none);
133 #ifndef NDEBUG
134 double single_error = 0.0;
135 double double_error = 0.0;
136 #endif
137 for (int i=0; i<5; i++) {
138
139 if (i==0) {
140 solver.SetAztecOption(AZ_orthog, AZ_single_classic);
141 solver.Iterate(NumGlobalElements, 1.0E-14);
142 #ifndef NDEBUG
143 single_error =
144 #endif
145 solver.RecursiveResidual();
146 }
147 else if (i==1) {
148 solver.SetAztecOption(AZ_orthog, AZ_double_classic);
149 solver.Iterate(NumGlobalElements, 1.0E-14);
150 #ifndef NDEBUG
151 double_error =
152 #endif
153 solver.RecursiveResidual();
154 assert(double_error < single_error); // Error from double classic should be less than single
155 }
156 else if (i==2) {
157 solver.SetAztecOption(AZ_orthog, AZ_single_modified);
158 solver.Iterate(NumGlobalElements, 1.0E-14);
159 #ifndef NDEBUG
160 single_error =
161 #endif
162 solver.RecursiveResidual();
163 }
164 else if (i==3) {
165 solver.SetAztecOption(AZ_orthog, AZ_double_modified);
166 solver.Iterate(NumGlobalElements, 1.0E-14);
167 #ifndef NDEBUG
168 double_error =
169 #endif
170 solver.RecursiveResidual();
171 assert(double_error < single_error); // Error from double classic should be less than single
172 }
173 else if (i==4) {
174 solver.SetAztecOption(AZ_solver, AZ_bicgstab);
175 solver.Iterate(NumGlobalElements, 1.0E-14);
176 assert(solver.RecursiveResidual()>single_error); // BiCGSTAB should always be worse than any GMRES answer
177 }
178
179 if (verbose1)
180 std::cout << "Solver performed " << solver.NumIters() << " iterations." << std::endl
181 << "Norm of Recursive residual = " << solver.RecursiveResidual() << std::endl << std::endl;
182
183 assert(solver.NumIters()==NumGlobalElements);
184
185
186 // Print out the result
187 if (verbose1) std::cout << "Computed Solution = " << std::endl;
188 if (verbose) std::cout << x << std::endl;
189
190 x.PutScalar(0.0);
191 }
192
193 if (verbose1)
194 std::cout << "Computed solution for 5x5 matrix diag(1, -10, -100, -1000, -10000) using the following unpreconditioned unscaled methods:" << std::endl
195 << " 1) GMRES with single step classical Gram-Schmidt" << std::endl
196 << " 2) GMRES with double step classical Gram-Schmidt" << std::endl
197 << " 3) GMRES with single step modified Gram-Schmidt" << std::endl
198 << " 4) GMRES with double step modified Gram-Schmidt" << std::endl
199 << " 5) BiCGSTAB" << std::endl << std::endl
200 << "Confirmed that double steps provide superior answer to single step" << std::endl
201 << "and that GMRES is superior to BiCGSTAB" << std::endl << std::endl;
202 if (Comm.MyPID()==0)
203 std::cout << "All tests passed." << std::endl;
204
205 #ifdef EPETRA_MPI
206 MPI_Finalize() ;
207 #endif
208 return 0;
209 }
210