CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixSparseSolve_petscd.cpp
Go to the documentation of this file.
1/// \file MatrixSparseSolves_petscd.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
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}
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.
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