CppNoddy  0.92
Loading...
Searching...
No Matches
Functions
MatrixSparseSolve_petscd.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_petscd.cpp.

22{
23 PetscSession::getInstance(argc,argv);
24
25 cout << "\n";
26 cout << "=== Matrix: Example linear (double) sparse solver ===\n";
27 cout << "\n";
28
29 bool failed = false;
30 // tolerance for the test
31 const double tol = 1.e-10;
32
33 //
34 // SOLVE A SMALL "Sparse"(!) 2X2 REAL SYSTEM
35 cout << "=== Matrix: double ===============================\n";
36 //
37 SparseMatrix<double> A( 2, 2 );
38 DenseVector<double> B( 2, 0.0 );
39 A( 0, 0 ) = 1.;
40 A( 0, 1 ) = 2.;
41 A( 1, 0 ) = 3.;
42 A( 1, 1 ) = 4.;
43 B[ 0 ] = 5.;
44 B[ 1 ] = 11.;
45
46 SparseLinearSystem<double> small_system( &A, &B, "petsc" );
47
48 try
49 {
50 small_system.factorise();
51 small_system.solve_using_factorisation();
52 }
53 catch (const std::runtime_error &error )
54 {
55 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
56 return 1;
57 }
58 DenseVector<double> answer( 2, 0.0 );
59 answer[ 0 ] = 1.0;
60 answer[ 1 ] = 2.0;
61 B.sub( answer );
62 if ( B.inf_norm() > tol )
63 {
64 std::cout << "\033[1;31;48m Simple 2x2 double sparse system was not solved correctly\033[0m\n";
65 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
66 failed = true;
67 }
68 else
69 {
70 std::cout << " Simple 2x2 double sparse solve works.\n";
71 }
72
73 // reset B to be double the previous case
74 B[ 0 ] = 10.;
75 B[ 1 ] = 22.;
76 try
77 {
78 small_system.solve_using_factorisation();
79 }
80 catch (const std::runtime_error &error )
81 {
82 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
83 assert( false );
84 }
85 // double the RHS and double the solution
86 answer[ 0 ] = 2.0;
87 answer[ 1 ] = 4.0;
88 B.sub( answer );
89 if ( B.inf_norm() > tol )
90 {
91 std::cout << "\033[1;31;48m Simple 2x2 double sparse system was not solved correctly\033[0m\n";
92 std::cout << " residual vector's inf_norm = " << B.inf_norm() << "\n";
93 failed = true;
94 }
95 else
96 {
97 std::cout << " Simple 2x2 double sparse solve_using_factorisation works.\n";
98 }
99
100 //PetscFinalize();
101
102 // CONCLUDING PASS/FAIL
103 //
104 if ( failed )
105 {
106 cout << "\033[1;31;48m * FAILED \033[0m\n";
107 return 1;
108 }
109 else
110 {
111 cout << "\033[1;32;48m * PASSED \033[0m\n";
112 return 0;
113 }
114
115}
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 >::factorise(), CppNoddy::DenseVector< _Type >::inf_norm(), CppNoddy::SparseLinearSystem< _Type >::solve_using_factorisation(), and CppNoddy::DenseVector< _Type >::sub().

© 2012

R.E. Hewitt