CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixBandedSolves.cpp
Go to the documentation of this file.
1/// \file MatrixBandedSolves.cpp
2/// \ingroup Test
3/// \ingroup Matrix
4/// Example of a simple "banded" inear solver using
5/// \f$ 2 \times 2 \f$ matrix problem is solved.
6/// Then a penta-diagonal problem is solved using the
7/// dense, banded and sparse containers. The native linear Gaussian
8/// elimination solvers are used unless the PETSC_D/Z compiler
9/// options are used, in which case the linear solver phase
10/// calls the PETSc library as appropriate.
11
12#include <cassert>
13
14#include <Timer.h>
15#include <Types.h>
16#include <Utility.h>
17#include <DenseLinearSystem.h>
18#include <BandedLinearSystem.h>
19#include "../Utils_Fill.h"
20
21using namespace CppNoddy;
22using namespace std;
23
24int main()
25{
26
27 cout << "\n";
28 cout << "=== Matrix: Example linear banded 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 BANDED REAL SYSTEM using LAPACK or native
37 //
38 const unsigned offdiag = 2;
39 // N = size of some of the larger tests
40 const unsigned N = 511;
41 const double D = 12 * ( 1. / ( N - 1 ) ) * ( 1. / ( N - 1 ) );
42 //
43 BandedMatrix<double> AB( N, offdiag, 0.0 );
44 DenseVector<double> BB( N, D );
45 //
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
52 DenseMatrix<double> AD( AB );
53 DenseVector<double> BD( BB );
54
55 cout << " Using dense matrix solver : " << N << "x" << N << " system \n";
56 cout << " Using the native dense routine\n";
57 DenseLinearSystem<double> dense_system( &AD, &BD, "native" );
58
59 try
60 {
61 dense_system.solve();
62 }
63 catch (const std::runtime_error &error )
64 {
65 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
66 return 1;
67 }
68
69 cout << " * Not checking.\n";
70 cout << "\n";
71 cout << " Comparing the banded matrix solver solution : ";
72 cout << N << "x" << 2 * AB.noffdiag() + 1 << " system \n";
73
74 cout << " Using the native banded routine\n";
75 BandedLinearSystem<double> banded_system( &AB, &BB, "native" );
76
77 try
78 {
79 banded_system.solve();
80 }
81 catch ( const std::runtime_error &error )
82 {
83 cout << " \033[1;31;48m * FAILED THROUGH EXCEPTION BEING RAISED \033[0m\n";
84 return 1;
85 }
86
87 BB.sub( BD );
88 if ( std::abs( BB.two_norm() ) > tol )
89 {
90 failed = true;
91 cout << " \033[1;31;48m * Banded solver does not give same result as dense solver \033[0m\n";
92 }
93 else
94 {
95 cout << " Banded solver agrees with the dense solver.\n";
96 }
97
98
99 //
100 // CONCLUDING PASS/FAIL
101 //
102 if ( failed )
103 {
104 cout << "\033[1;31;48m * FAILED \033[0m\n";
105 return 1;
106 }
107 else
108 {
109 cout << "\033[1;32;48m * PASSED \033[0m\n";
110 return 0;
111 }
112
113}
Specification of the linear system class.
Specification of the linear system class.
int main()
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