CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixSparseSolve_petscz.cpp
Go to the documentation of this file.
1/// \file MatrixSparseSolves_petscz.cpp
2/// \ingroup Test
3/// \ingroup Matrix
4/// Example of the simple linear solvers implemented
5/// for sparse matrix objects. A simple
6/// \f$ 2 \times 2 \f$ matrix problem is solved using
7/// the PETSC_D/Z compiler definitions.
8
9
10#include <cassert>
11
12#include <Timer.h>
13#include <Types.h>
14#include <Utility.h>
15#include <SparseLinearSystem.h>
16#include <PetscSession.h>
17
18using namespace CppNoddy;
19using namespace std;
20
21int main(int argc, char *argv[])
22{
23 PetscSession::getInstance(argc,argv);
24 //auto session = PetscSession(argc,argv);
25 //PetscInitialize(NULL,NULL,(char*)0,(char*)0);
26 //
27 cout << "\n";
28 cout << "=== Matrix: Example linear sparse solver ===========\n";
29 cout << "\n";
30
31 bool failed = false;
32 // tolerance for the test
33 const double tol = 1.e-10;
34
35 //
36 // SOLVE A SMALL "Sparse"(!) 2X2 COMPLEX SYSTEM
37 cout << "=== Matrix: complex ==============================\n";
38 //
40 DenseVector<D_complex> B( 2, 0.0 );
41 A( 0, 0 ) = 1.;
42 A( 0, 1 ) = 2.;
43 A( 1, 0 ) = 3.;
44 A( 1, 1 ) = 4.;
45 B[ 0 ] = 5.;
46 B[ 1 ] = 11.;
47 //
48 SparseLinearSystem<D_complex> small_system( &A, &B, "petsc" );
49
50 try
51 {
52 // below is equivalent to small_system.solve();
53 cout << "Prefact\n";
54 small_system.factorise();
55 cout << "Postfact\n";
56 small_system.solve_using_factorisation();
57 cout << "Postsolve\n";
58 }
59 catch ( const std::runtime_error &error )
60 {
61 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
62 return 1;
63 }
64 DenseVector<D_complex> answer( 2, 0.0 );
65 answer[ 0 ] = 1.0;
66 answer[ 1 ] = 2.0;
67 B.sub( answer );
68 if ( B.inf_norm() > tol )
69 {
70 std::cout << "\033[1;31;48m Simple 2x2 complex sparse system was not solved correctly\033[0m\n";
71 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
72 failed = true;
73 }
74 else
75 {
76 std::cout << " Simple 2x2 complex sparse solve works.\n";
77 }
78
79 // double the RHS and try a resolve
80 B[ 0 ] = 10.;
81 B[ 1 ] = 22.;
82 try
83 {
84 small_system.solve_using_factorisation();
85 }
86 catch ( const std::runtime_error &error )
87 {
88 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
89 return 1;
90 }
91 answer[ 0 ] = 2.0;
92 answer[ 1 ] = 4.0;
93 B.sub( answer );
94 if ( B.inf_norm() > tol )
95 {
96 std::cout << "\033[1;31;48m Simple 2x2 complex sparse system was not solved correctly\033[0m\n";
97 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
98 failed = true;
99 }
100 else
101 {
102 std::cout << " Simple 2x2 complex sparse solve_using_factorisation works.\n";
103 }
104
105 // Note: dtor will call cleanup anyway
106 small_system.cleanup();
107 // CONCLUDING PASS/FAIL
108 //
109 if ( failed )
110 {
111 cout << "\033[1;31;48m * FAILED \033[0m\n";
112 return 1;
113 }
114 else
115 {
116 cout << "\033[1;32;48m * PASSED \033[0m\n";
117 return 0;
118 }
119
120}
int main()
Definition: ArcCircle.cpp:39
Specification of a sparse-storage linear system class.
A spec for the CppNoddy Timer object.
A spec for a collection of utility functions.
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
double inf_norm() const
Infinity norm.
Definition: DenseVector.cpp:59
void sub(const DenseVector< _Type > &x)
Subtract a vector, element wise, equivalent to -=.
Definition: DenseVector.cpp:44
A linear system class for vector right-hand sides.
void factorise()
Factorise the Ax=B system.
void solve_using_factorisation()
Resolve the same system using the same factorisation.
void cleanup()
deallocates some objects
A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.
Definition: SparseMatrix.h:31
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt