btdmatrix.hh

Go to the documentation of this file.
00001 #ifndef DUNE_BLOCK_TRIDIAGONAL_MATRIX_HH
00002 #define DUNE_BLOCK_TRIDIAGONAL_MATRIX_HH
00003 
00004 #include <dune/istl/bcrsmatrix.hh>
00005 
00011 namespace Dune {
00021 template <class B, class A=ISTLAllocator>
00022 class BTDMatrix : public BCRSMatrix<B,A>
00023 {
00024 public:
00025 
00026     //===== type definitions and constants
00027     
00029     typedef typename B::field_type field_type;
00030     
00032     typedef B block_type;
00033     
00035     typedef A allocator_type;
00036     
00038     //typedef BCRSMatrix<B,A>::row_type row_type;
00039 
00041     typedef typename A::size_type size_type;
00042 
00044     enum {blocklevel = B::blocklevel+1};
00045 
00047     BTDMatrix() : BCRSMatrix<B,A>() {}
00048 
00049     explicit BTDMatrix(int size) 
00050         : BCRSMatrix<B,A>(size, size, BCRSMatrix<B,A>::random) 
00051     {
00052         // Set number of entries for each row
00053         this->BCRSMatrix<B,A>::setrowsize(0, 2);
00054 
00055         for (int i=1; i<size-1; i++)
00056             this->BCRSMatrix<B,A>::setrowsize(i, 3);
00057 
00058         this->BCRSMatrix<B,A>::setrowsize(size-1, 2);
00059 
00060         this->BCRSMatrix<B,A>::endrowsizes();
00061 
00062         // The actual entries for each row
00063         this->BCRSMatrix<B,A>::addindex(0, 0);
00064         this->BCRSMatrix<B,A>::addindex(0, 1);
00065 
00066         for (int i=1; i<size-1; i++) {
00067             this->BCRSMatrix<B,A>::addindex(i, i-1);
00068             this->BCRSMatrix<B,A>::addindex(i, i  );
00069             this->BCRSMatrix<B,A>::addindex(i, i+1);
00070         }
00071 
00072         this->BCRSMatrix<B,A>::addindex(size-1, size-2);
00073         this->BCRSMatrix<B,A>::addindex(size-1, size-1);
00074 
00075         this->BCRSMatrix<B,A>::endindices();
00076 
00077     }
00078 
00080     BTDMatrix& operator= (const BTDMatrix& other) {
00081         this->BCRSMatrix<B,A>::operator=(other);
00082         return *this;
00083     }
00084 
00086     BTDMatrix& operator= (const field_type& k) {
00087         this->BCRSMatrix<B,A>::operator=(k);
00088         return *this;
00089     }
00090 
00097     template <class V>
00098     void solve (V& x, const V& rhs) const {
00099 
00100         // Make copies of the rhs and the right matrix band
00101         V d = rhs;
00102         V c(this->N()-1);
00103         for (size_t i=0; i<this->N()-1; i++)
00104             c[i] = (*this)[i][i+1];
00105 
00106         /* Modify the coefficients. */
00107         c[0] /= (*this)[0][0];  /* Division by zero risk. */
00108         d[0] /= (*this)[0][0];  /* Division by zero would imply a singular matrix. */
00109 
00110         for (unsigned int i = 1; i < this->N(); i++) {
00111 
00112             double id = 1.0 / ((*this)[i][i] - c[i-1] * (*this)[i][i-1]);  /* Division by zero risk. */
00113             if (i<c.size())
00114                 c[i] *= id;                                            /* Last value calculated is redundant. */
00115             d[i] = (d[i] - d[i-1] * (*this)[i][i-1]) * id;
00116 
00117         }
00118  
00119         /* Now back substitute. */
00120         x[this->N() - 1] = d[this->N() - 1];
00121         for (int i = this->N() - 2; i >= 0; i--)
00122             x[i] = d[i] - c[i] * x[i + 1];
00123 
00124     }
00125 
00126 private:        
00127 
00128     // ////////////////////////////////////////////////////////////////////////////
00129     //   The following methods from the base class should now actually be called
00130     // ////////////////////////////////////////////////////////////////////////////
00131 
00132     // createbegin and createend should be in there, too, but I can't get it to compile
00133     //     BCRSMatrix<B,A>::CreateIterator createbegin () {}
00134     //     BCRSMatrix<B,A>::CreateIterator createend () {}
00135     void setrowsize (size_type i, size_type s) {}
00136     void incrementrowsize (size_type i) {}
00137     void endrowsizes () {}
00138     void addindex (size_type row, size_type col) {}
00139     void endindices () {}
00140 };
00143 }  // end namespace Dune
00144 
00145 #endif
Generated on Sat Apr 24 11:13:45 2010 for dune-istl by  doxygen 1.6.3