CppNoddy  0.92
Loading...
Searching...
No Matches
Functions
MatrixSparseSolve_petscz.cpp File Reference
#include <cassert>
#include <Timer.h>
#include <Types.h>
#include <Utility.h>
#include <SparseLinearSystem.h>
#include <PetscSession.h>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 21 of file MatrixSparseSolve_petscz.cpp.

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}
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A linear system class for vector right-hand sides.
A matrix class that constructs a SPARSE matrix as a row major std::vector of SparseVectors.
Definition: SparseMatrix.h:31
double A(1.0)
initial hump amplitude

References CppNoddy::SparseLinearSystem< _Type >::cleanup(), CppNoddy::SparseLinearSystem< _Type >::factorise(), CppNoddy::DenseVector< _Type >::inf_norm(), CppNoddy::SparseLinearSystem< _Type >::solve_using_factorisation(), and CppNoddy::DenseVector< _Type >::sub().

© 2012

R.E. Hewitt