Debian Codegraph @

packages/aztecoo/example/simple_epetra_aztecoo_LL/cxx_main.cpp

raw
1 //@HEADER
2 // ***********************************************************************
3 //
4 // AztecOO: An Object-Oriented Aztec Linear Solver Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux ([email protected])
38 //
39 // ***********************************************************************
40 //@HEADER
41
42 #include "AztecOO.h"
43 #include "AztecOO_Version.h"
44
45 /* The next two ifdefs should be removed when when the revision of
46 az_aztec_defs.h is complete. */
47 #ifndef TRILINOS_NO_CONFIG_H
48
49 #ifdef HAVE_CSTDLIB
50 #include <cstdlib>
51 #else
52 #include <stdlib.h>
53 #endif
54
55 #ifdef HAVE_CSTDIO
56 #include <cstdio>
57 #else
58 #include <stdio.h>
59 #endif
60
61 #else
62 #include <cstdio>
63 #include <cstdlib>
64 #endif
65
66 #ifdef EPETRA_MPI
67 #include "mpi.h"
68 #include "Epetra_MpiComm.h"
69 #else
70 #include "Epetra_SerialComm.h"
71 #endif
72
73 #include "Epetra_Map.h"
74 #include "Epetra_Vector.h"
75 #include "Epetra_CrsMatrix.h"
76
77 int main(int argc, char *argv[])
78 {
79 #ifdef EPETRA_MPI
80 // Initialize MPI
81 MPI_Init(&argc,&argv);
82 Epetra_MpiComm Comm( MPI_COMM_WORLD );
83 #else
84 Epetra_SerialComm Comm;
85 #endif
86 int NumMyElements = 1000;
87
88 if (Comm.MyPID()==0)
89 std::cout << AztecOO_Version() << std::endl << std::endl;
90
91 std::cout << Comm <<std::endl;
92
93 // Construct a Map that puts same number of equations on each processor
94 Epetra_Map Map((long long) -1, NumMyElements, 0LL, Comm);
95 long long NumGlobalElements = Map.NumGlobalElements64();
96
97 // Create a Epetra_Matrix
98 Epetra_CrsMatrix A(Copy, Map, 3);
99
100 // Add rows one-at-a-time
101 double negOne = -1.0;
102 double posTwo = 2.0;
103 for (int i=0; i<NumMyElements; i++) {
104 long long GlobalRow = A.GRID64(i); long long RowLess1 = GlobalRow - 1; long long RowPlus1 = GlobalRow + 1;
105
106 if (RowLess1!=-1) A.InsertGlobalValues(GlobalRow, 1, &negOne, &RowLess1);
107 if (RowPlus1!=NumGlobalElements) A.InsertGlobalValues(GlobalRow, 1, &negOne, &RowPlus1);
108 A.InsertGlobalValues(GlobalRow, 1, &posTwo, &GlobalRow);
109 }
110
111 // Finish up
112 A.FillComplete();
113
114 // Create x and b vectors
115 Epetra_Vector x(Map);
116 Epetra_Vector b(Map);
117
118 b.Random();
119
120 // Create Linear Problem
121 Epetra_LinearProblem problem(&A, &x, &b);
122 // Create AztecOO instance
123 AztecOO solver(problem);
124
125 solver.SetAztecOption(AZ_precond, AZ_Jacobi);
126 solver.Iterate(1000, 1.0E-8);
127
128 std::cout << "Solver performed " << solver.NumIters() << " iterations." << std::endl
129 << "Norm of true residual = " << solver.TrueResidual() << std::endl;
130 #ifdef EPETRA_MPI
131 MPI_Finalize() ;
132 #endif
133 return 0;
134 }
135