Example #1
0
    private static void test04()

    //******************************************************************************
    //
    //  Purpose:
    //
    //    TEST04 tests PMGMRES_ILU_CR on a simple 5 by 5 matrix.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    25 July 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N      = 5;
        const int NZ_NUM = 9;

        double[] a =
        {
            1.0, 2.0, 1.0,
            2.0,
            3.0, 3.0,
            4.0,
            1.0, 5.0
        };
        int i;

        int[] ia      = { 0, 3, 4, 6, 7, 9 };
        int   itr_max = 0;

        int[] ja =
        {
            0, 3, 4,
            1,
            0, 2,
            3,
            1, 4
        };
        int mr = 0;

        double[] rhs = { 14.0, 4.0, 12.0, 16.0, 27.0 };
        int      test;

        double[] x_estimate = new double[N];
        double[] x_exact    = { 1.0, 2.0, 3.0, 4.0, 5.0 };

        Console.WriteLine("");
        Console.WriteLine("TEST04");
        Console.WriteLine("  Test PMGMRES_ILU_CR on a simple 5 x 5 matrix.");

        Console.WriteLine("");
        for (i = 0; i < N + 1; i++)
        {
            Console.WriteLine("  ia[" + i + "] = " + ia[i] + "");
        }

        for (test = 1; test <= 3; test++)
        {
            //
            //  Set the initial solution estimate.
            //
            for (i = 0; i < N; i++)
            {
                x_estimate[i] = 0.0;
            }

            double x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            switch (test)
            {
            case 1:
                itr_max = 1;
                mr      = 20;
                break;

            case 2:
                itr_max = 2;
                mr      = 10;
                break;

            case 3:
                itr_max = 5;
                mr      = 4;
                break;
            }

            const double tol_abs = 1.0E-08;
            const double tol_rel = 1.0E-08;

            Console.WriteLine("");
            Console.WriteLine("  Test " + test + "");
            Console.WriteLine("  Matrix order N = " + N + "");
            Console.WriteLine("  Inner iteration limit = " + mr + "");
            Console.WriteLine("  Outer iteration limit = " + itr_max + "");
            Console.WriteLine("  Initial X_ERROR = " + x_error + "");

            RestartedGeneralizedMinimumResidual.pmgmres_ilu_cr(N, NZ_NUM, ia, ja, a, ref x_estimate, rhs, itr_max,
                                                               mr,
                                                               tol_abs, tol_rel);

            x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            Console.WriteLine("  Final X_ERROR = " + x_error + "");
        }
    }
Example #2
0
    private static void test03()

    //******************************************************************************
    //
    //  Purpose:
    //
    //    TEST03 tests PMGMRES_ILU_CR on the simple -1,2-1 matrix.
    //
    //  Discussion:
    //
    //    This is a very weak test, since the matrix has such a simple
    //    structure, is diagonally dominant (though not strictly),
    //    and is symmetric.
    //
    //    To make the matrix bigger, simply increase the value of N.
    //
    //    Note that PGMRES_ILU_CR expects the matrix to be stored using the
    //    sparse compressed row format.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    25 July 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N      = 20;
        const int NZ_NUM = 3 * N - 2;

        double[] a = new double[NZ_NUM];
        int      i;

        int[] ia      = new int[N + 1];
        int   itr_max = 0;

        int[] ja = new int[NZ_NUM];
        int   mr = 0;

        double[] rhs = new double[N];
        int      test;

        double[] x_estimate = new double[N];
        double[] x_exact    = new double[N];

        Console.WriteLine("");
        Console.WriteLine("TEST03");
        Console.WriteLine("  Test PMGMRES_ILU_CR on the simple -1,2-1 matrix.");
        //
        //  Set the matrix.
        //  Note that we use zero based index valuesin IA and JA.
        //
        int k = 0;

        ia[0] = 0;

        Console.WriteLine("");
        Console.WriteLine("  ia[" + 0 + "] = " + ia[0] + "");
        for (i = 0; i < N; i++)
        {
            ia[i + 1] = ia[i];
            switch (i)
            {
            case > 0:
                ia[i + 1] += 1;
                ja[k]      = i - 1;
                a[k]       = -1.0;
                k         += 1;
                break;
            }

            ia[i + 1] += 1;
            ja[k]      = i;
            a[k]       = 2.0;
            k         += 1;

            if (i < N - 1)
            {
                ia[i + 1] += 1;
                ja[k]      = i + 1;
                a[k]       = -1.0;
                k         += 1;
            }

            Console.WriteLine("  ia[" + i + 1 + "] = " + ia[i + 1] + "");
        }

        //
        //  Set the right hand side:
        //
        for (i = 0; i < N - 1; i++)
        {
            rhs[i] = 0.0;
        }

        rhs[N - 1] = N + 1;
        //
        //  Set the exact solution.
        //
        for (i = 0; i < N; i++)
        {
            x_exact[i] = i + 1;
        }

        for (test = 1; test <= 3; test++)
        {
            //
            //  Set the initial solution estimate.
            //
            for (i = 0; i < N; i++)
            {
                x_estimate[i] = 0.0;
            }

            double x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            switch (test)
            {
            case 1:
                itr_max = 1;
                mr      = 20;
                break;

            case 2:
                itr_max = 2;
                mr      = 10;
                break;

            case 3:
                itr_max = 5;
                mr      = 4;
                break;
            }

            const double tol_abs = 1.0E-08;
            const double tol_rel = 1.0E-08;

            Console.WriteLine("");
            Console.WriteLine("  Test " + test + "");
            Console.WriteLine("  Matrix order N = " + N + "");
            Console.WriteLine("  Inner iteration limit = " + mr + "");
            Console.WriteLine("  Outer iteration limit = " + itr_max + "");
            Console.WriteLine("  Initial X_ERROR = " + x_error + "");

            RestartedGeneralizedMinimumResidual.pmgmres_ilu_cr(N, NZ_NUM, ia, ja, a, ref x_estimate, rhs, itr_max,
                                                               mr,
                                                               tol_abs, tol_rel);

            x_error = 0.0;
            for (i = 0; i < N; i++)
            {
                x_error += Math.Pow(x_exact[i] - x_estimate[i], 2);
            }

            x_error = Math.Sqrt(x_error);

            Console.WriteLine("  Final X_ERROR = " + x_error + "");
        }
    }