CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixBandedSolves_lapack.cpp
Go to the documentation of this file.
1/// \file MatrixSolves_lapack.cpp
2/// \ingroup Test
3/// \ingroup Matrix
4/// Example of the simple linear solvers implemented
5/// for dense, banded and sparse matrix objects. To begin
6/// with a simple \f$ 2 \times 2 \f$ matrix problem is solved.
7/// Then a penta-diagonal problem is solved using the
8/// dense, banded and sparse containers. The native linear Gaussian
9/// elimination solvers are used unless the PETSC_D/Z compiler
10/// options are used, in which case the linear solver phase
11/// calls the PETSc library as appropriate.
12
13#include <cassert>
14
15#include <Timer.h>
16#include <Types.h>
17#include <Utility.h>
18#include <DenseLinearSystem.h>
19#include <BandedLinearSystem.h>
20#include "../Utils_Fill.h"
21
22using namespace CppNoddy;
23using namespace std;
24
25int main()
26{
27
28 cout << "\n";
29 cout << "=== Matrix: Example linear banded solver ============\n";
30 cout << "\n";
31
32 bool failed = false;
33 // tolerance for the test
34 const double tol = 1.e-10;
35
36 //
37 // SOLVE A BANDED REAL SYSTEM using LAPACK or native
38 //
39 const unsigned offdiag = 2;
40 // N = size of some of the larger tests
41 const unsigned N = 511;
42 const double D = 12 * ( 1. / ( N - 1 ) ) * ( 1. / ( N - 1 ) );
43
44 BandedMatrix<double> AB( N, offdiag, 0.0 );
45 DenseVector<double> BB( N, D );
46 Utils_Fill::fill_band( AB, 0, -30.0 );
47 Utils_Fill::fill_band( AB, -1, 16.0 );
48 Utils_Fill::fill_band( AB, 1, 16.0 );
49 Utils_Fill::fill_band( AB, -2, -1.0 );
50 Utils_Fill::fill_band( AB, 2, -1.0 );
51 DenseMatrix<double> AD( AB );
52 DenseVector<double> BD( BB );
53
54 cout << " Using dense matrix solver : " << N << "x" << N << " system \n";
55 cout << " Using the LAPACK LU dense routine\n";
56 DenseLinearSystem<double> dense_system( &AD, &BD, "lapack" );
57
58 try
59 {
60 dense_system.solve();
61 }
62 catch ( const std::runtime_error &error )
63 {
64 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
65 return 1;
66 }
67
68 cout << " * Not checking.\n";
69 cout << "\n";
70 cout << " Comparing the banded matrix solver solution : ";
71 cout << N << "x" << 2 * AB.noffdiag() + 1 << " system \n";
72
73 cout << " Using the LAPACK LU banded routine\n";
74 BandedLinearSystem<double> banded_system( &AB, &BB, "lapack" );
75
76 try
77 {
78 banded_system.solve();
79 }
80 catch ( const std::runtime_error &error )
81 {
82 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
83 return 1;
84 }
85
86 BB.sub( BD );
87 if ( std::abs( BB.two_norm() ) > tol )
88 {
89 failed = true;
90 cout << " \033[1;31;48m * Banded solver does not give same result as dense solver \033[0m\n";
91 }
92 else
93 {
94 cout << " Banded solver agrees with the dense solver.\n";
95 }
96
97
98 //
99 // CONCLUDING PASS/FAIL
100 //
101 if ( failed )
102 {
103 cout << "\033[1;31;48m * FAILED \033[0m\n";
104 return 1;
105 }
106 else
107 {
108 cout << "\033[1;32;48m * PASSED \033[0m\n";
109 return 0;
110 }
111}
Specification of the linear system class.
Specification of the linear system class.
A spec for the CppNoddy Timer object.
A spec for a collection of utility functions.
A linear system class for vector right-hand sides.
void solve()
Solve the banded system.
A matrix class that constructs a BANDED matrix.
Definition: BandedMatrix.h:16
std::size_t noffdiag() const
Get the number of off-diagonal elements where the total INPUT band width is 2*noffdiag+1 since the ba...
Definition: BandedMatrix.h:227
A linear system class for vector right-hand sides.
void solve()
Solve the sparse system.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
void sub(const DenseVector< _Type > &x)
Subtract a vector, element wise, equivalent to -=.
Definition: DenseVector.cpp:44
double two_norm() const
l2-norm.
Definition: DenseVector.cpp:54
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
void fill_band(CppNoddy::Sequential_Matrix_base< _Type > &A, const int &offset, const _Type &value)
Fill a diagonal band of a matrix.
Definition: Utils_Fill.h:34

© 2012

R.E. Hewitt