//=========================================================================== // CVS Information: // // $RCSfile: ilu.cpp,v $ $Revision: 1.3 $ $State: Exp $ // $Author: llee $ $Date: 2001/10/18 14:08:31 $ // $Locker: $ //--------------------------------------------------------------------------- // // DESCRIPTION // //--------------------------------------------------------------------------- // // LICENSE AGREEMENT // $COPYRIGHT$ //--------------------------------------------------------------------------- // // REVISION HISTORY: // // $Log: ilu.cpp,v $ // Revision 1.3 2001/10/18 14:08:31 llee // re-organize the directory structures // // Revision 1.2 2000/07/27 04:39:18 llee1 // *** empty log message *** // // Revision 1.1 2000/07/26 21:49:22 llee1 // change file extension from .cc to .cpp // // Revision 1.2 2000/07/17 15:44:04 llee1 // *** empty log message *** // // //=========================================================================== #include "mtl/matrix.h" #include "mtl/mtl.h" #include "mtl/utils.h" #include #include "itl/krylov/qmr.h" #include "itl/preconditioner/ilu.h" /* In thsi example, we show how to use ILU algorithm, the output should be: iteration 0: resid 2.23607 iteration 1: resid 4.37539 iteration 2: resid 0.338066 iteration 3: resid 1.60609e-12 finished! error code = 0 3 iterations 1.60609e-12 is actual final residual. 7.18266e-13 is actual relative tolerance achieved. Relative tol: 1e-06 Absolute tol: 0 1 2 0 0 3 x 0.205847 = 1 4 5 0 6 0 x 0.0419363 = 1 0 7 8 0 9 x -0.178049 = 1 0 0 10 11 12 x -0.00551162 = 1 0 0 13 0 14 x 0.23676 = 1 */ using namespace mtl; using namespace itl; typedef double Type; //begin typedef matrix< Type, rectangle<>, array< compressed<> >, row_major >::type Matrix; //end int main (int , char* []) { using std::cout; using std::endl; int max_iter = 50; //begin Matrix A(5, 5); //end A(0, 0) = 1.0; A(0, 1) = 2.0; A(0, 4) = 3.0; A(1, 0) = 4.0; A(1, 1) = 5.0; A(1, 3) = 6.0; A(2, 1) = 7.0; A(2, 2) = 8.0; A(2, 4) = 9.0; A(3, 2) = 10.; A(3, 3) = 11.; A(3, 4) = 12.; A(4, 2) = 13.; A(4, 4) = 14.; //begin dense1D x(A.nrows(), Type(0)); dense1D b(A.ncols()); for (dense1D::iterator i=b.begin(); i!=b.end(); i++) *i = 1.; // ilu preconditioner ILU precond(A); //iteration noisy_iteration iter(b, max_iter, 1.e-6); //qmr algorithm qmr(A, x, b, precond.left(), precond.right(), iter); //end //verify the result dense1D b1(A.ncols()); itl::mult(A, x, b1); itl::add(b1, itl::scaled(b, -1.), b1); if ( itl::two_norm(b1) < 1.e-6) { //output for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { cout.width(6); //!!! it is not recommended to use A(i,j) for sparse matrices cout << A(i, j) << " "; } cout << " x "; cout.width(16); cout << x[i] << " = "; cout.width(6); cout << b[i] << endl; } } else { cout << "Residual " << iter.resid() << endl; } return 0; }