Пример #1
0
    private static void test05(int dim_num, int level_max, int degree_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests a Clenshaw Curtis sparse grid rule for monomial exactness.
    //
    //  Discussion:
    //
    //    This test is going to check EVERY monomial of total degree DEGREE_MAX
    //    or less.  Even for a moderately high dimension of DIM_NUM = 10, you
    //    do NOT want to use a large value of DEGREE_MAX, since there are
    //
    //      1         monomials of total degree 0,
    //      DIM_NUM   monomials of total degree 1,
    //      DIM_NUM^2 monomials of total degree 2,
    //      DIM_NUM^3 monomials of total degree 3, and so on.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 July 2008
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, the level.
    //
    //    Input, int DEGREE_MAX, the maximum monomial total degree to check.
    //
    {
        int degree;
        int dim;
        int h = 0;
        int point;
        int t = 0;

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  Check the exactness of a Clenshaw Curtis sparse grid quadrature rule,");
        Console.WriteLine("  applied to all monomials of orders 0 to DEGREE_MAX.");
        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        Console.WriteLine("");
        Console.WriteLine("  The maximum total degree to be checked is DEGREE_MAX = " + degree_max + "");
        Console.WriteLine("");
        Console.WriteLine("  We expect this rule to be accurate up to and including total degree "
                          + 2 * level_max + 1 + "");
        //
        //  Determine the number of points in the rule.
        //
        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        Console.WriteLine("");
        Console.WriteLine("  Number of unique points in the grid = " + point_num + "");
        //
        //  Allocate space for the weights and points.
        //
        double[] grid_weight = new double[point_num];
        double[] grid_point  = new double[dim_num * point_num];
        //
        //  Compute the weights and points.
        //
        Grid_ClenshawCurtis.sparse_grid_cc(dim_num, level_max, point_num, ref grid_weight,
                                           ref grid_point);
        //
        //  Rescale the weights, and translate the abscissas.
        //
        double volume = Math.Pow(2.0, dim_num);

        for (point = 0; point < point_num; point++)
        {
            grid_weight[point] /= volume;
        }

        for (dim = 0; dim < dim_num; dim++)
        {
            for (point = 0; point < point_num; point++)
            {
                grid_point[dim + point * dim_num] = (grid_point[dim + point * dim_num] + 1.0)
                                                    / 2.0;
            }
        }

        //
        //  Explore the monomials.
        //
        int[] expon = new int[dim_num];

        Console.WriteLine("");
        Console.WriteLine("      Error      Total   Monomial");
        Console.WriteLine("                 Degree  Exponents");
        Console.WriteLine("");

        for (degree = 0; degree <= degree_max; degree++)
        {
            bool more = false;

            for (;;)
            {
                Comp.comp_next(degree, dim_num, ref expon, ref more, ref h, ref t);

                double quad_error = MonomialQuadrature.monomial_quadrature(dim_num, expon, point_num,
                                                                           grid_weight, grid_point);

                string cout = "  " + quad_error.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                              + "     " + degree.ToString(CultureInfo.InvariantCulture).PadLeft(2)
                              + "      ";

                for (dim = 0; dim < dim_num; dim++)
                {
                    cout += expon[dim].ToString(CultureInfo.InvariantCulture).PadLeft(2);
                }

                Console.WriteLine(cout);

                if (!more)
                {
                    break;
                }
            }

            Console.WriteLine("");
        }
    }
Пример #2
0
    private static void test06(int dim_num, int level_max)

    //***************************************************************************80
    //
    //  Purpose:
    //
    //    TEST06 creates a sparse Clenshaw-Curtis grid and writes it to a file.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 July 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, the level.
    //
    {
        int dim;

        Console.WriteLine("");
        Console.WriteLine("TEST06:");
        Console.WriteLine("  Call SPARSE_GRID_CC to make a sparse Clenshaw-Curtis grid.");
        Console.WriteLine("  Write the data to a set of quadrature files.");

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        //
        //  Allocate space for the weights and points.
        //
        double[] r = new double[dim_num * 2];
        double[] w = new double[point_num];
        double[] x = new double[dim_num * point_num];
        //
        //  Compute the weights and points.
        //
        for (dim = 0; dim < dim_num; dim++)
        {
            r[dim + 0 * dim_num] = -1.0;
            r[dim + 1 * dim_num] = +1.0;
        }

        Grid_ClenshawCurtis.sparse_grid_cc(dim_num, level_max, point_num, ref w, ref x);
        //
        //  Write the data out.
        //
        string r_filename = "cc_d" + dim_num + "_level"
                            + level_max + "_r.txt";
        string w_filename = "cc_d" + dim_num + "_level"
                            + level_max + "_w.txt";
        string x_filename = "cc_d" + dim_num + "_level"
                            + level_max + "_x.txt";

        typeMethods.r8mat_write(r_filename, dim_num, 2, r);
        typeMethods.r8mat_write(w_filename, 1, point_num, w);
        typeMethods.r8mat_write(x_filename, dim_num, point_num, x);

        Console.WriteLine("");
        Console.WriteLine("  R data written to \"" + r_filename + "\".");
        Console.WriteLine("  W data written to \"" + w_filename + "\".");
        Console.WriteLine("  X data written to \"" + x_filename + "\".");
    }
Пример #3
0
    private static void test04(int dim_num, int level_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST04 sums the weights and compares them to 2^DIM_NUM.
    //
    //  Discussion:
    //
    //    This routine gets the sparse grid indices and determines the
    //    corresponding sparse grid abscissas.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 November 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, the level.
    //
    {
        int point;

        Console.WriteLine("");
        Console.WriteLine("TEST04:");
        Console.WriteLine("  Compute the weights of a Clenshaw Curtis sparse grid .");
        Console.WriteLine("");
        Console.WriteLine("  As a simple test, sum these weights.");
        Console.WriteLine("  They should sum to exactly 2^DIM_NUM.");
        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        Console.WriteLine("");
        Console.WriteLine("  Number of unique points in the grid = " + point_num + "");
        //
        //  Allocate space for the weights and points.
        //
        double[] grid_weight = new double[point_num];
        double[] grid_point  = new double[dim_num * point_num];
        //
        //  Compute the weights and points.
        //
        Grid_ClenshawCurtis.sparse_grid_cc(dim_num, level_max, point_num, ref grid_weight, ref grid_point);
        //
        //  Sum the weights.
        //
        double weight_sum = 0.0;

        for (point = 0; point < point_num; point++)
        {
            weight_sum += grid_weight[point];
        }

        double weight_sum_exact = Math.Pow(2.0, dim_num);

        double weight_sum_error = Math.Abs(weight_sum - weight_sum_exact);

        Console.WriteLine("");
        Console.WriteLine("    Weight sum     Exact sum    Difference");
        Console.WriteLine("");
        Console.WriteLine("  " + weight_sum.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                          + "  " + weight_sum_exact.ToString(CultureInfo.InvariantCulture).PadLeft(12)
                          + "  " + weight_sum_error.ToString(CultureInfo.InvariantCulture).PadLeft(12) + "");
    }
Пример #4
0
    private static void test03(int dim_num, int level_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 call SPARSE_GRID_CC to create a Clenshaw Curtis grid.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 November 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, the level.
    //
    {
        int point;

        Console.WriteLine("");
        Console.WriteLine("TEST03:");
        Console.WriteLine("  SPARSE_GRID_CC makes a sparse Clenshaw Curtis grid.");
        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        Console.WriteLine("");
        Console.WriteLine("  Number of unique points in the grid = " + point_num + "");
        //
        //  Allocate space for the weights and points.
        //
        double[] grid_weight = new double[point_num];
        double[] grid_point  = new double[dim_num * point_num];
        //
        //  Compute the weights and points.
        //
        Grid_ClenshawCurtis.sparse_grid_cc(dim_num, level_max, point_num, ref grid_weight, ref grid_point);
        //
        //  Print them out.
        //
        Console.WriteLine("");
        Console.WriteLine("  Grid weights:");
        Console.WriteLine("");
        for (point = 0; point < point_num; point++)
        {
            Console.WriteLine("  " + point.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + " " + grid_weight[point].ToString(CultureInfo.InvariantCulture).PadLeft(10) + "");
        }

        Console.WriteLine("");
        Console.WriteLine("  Grid points:");
        Console.WriteLine("");
        for (point = 0; point < point_num; point++)
        {
            string cout = "  " + point.ToString(CultureInfo.InvariantCulture).PadLeft(4);
            int    dim;
            for (dim = 0; dim < dim_num; dim++)
            {
                cout += " " + grid_point[dim + point * dim_num].ToString(CultureInfo.InvariantCulture).PadLeft(10);
            }

            Console.WriteLine(cout);
        }
    }
Пример #5
0
    private static void test02(int dim_num, int level_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests SPARSE_GRID_CC_INDEX.
    //
    //  Discussion:
    //
    //    The routine computes the indices of the unique points used in a sparse
    //    multidimensional grid whose size is controlled by a parameter LEVEL_MAX.
    //
    //    Once these indices are returned, they can be converted into
    //    Clenshaw Curtis points.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 November 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_NUM, the spatial dimension.
    //
    //    Input, int LEVEL_MAX, the level.
    //
    {
        int point;

        Console.WriteLine("");
        Console.WriteLine("TEST02:");
        Console.WriteLine("  SPARSE_GRID_CC_INDEX returns all grid indexes");
        Console.WriteLine("  whose level value satisfies");
        Console.WriteLine("    0 <= LEVEL <= LEVEL_MAX.");
        Console.WriteLine("  Here, LEVEL is the sum of the levels of the 1D rules,");
        Console.WriteLine("  and the order of the rule is 2**LEVEL + 1.");

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");

        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        Console.WriteLine("");
        Console.WriteLine("  Number of unique points in the grid = " + point_num + "");
        //
        //  Compute the orders and points.
        //
        int[] grid_index = Grid_ClenshawCurtis.sparse_grid_cc_index(dim_num, level_max, point_num);
        //
        //  Now we're done.  Print the merged grid data.
        //
        Console.WriteLine("");
        Console.WriteLine("  Grid index:");
        Console.WriteLine("");
        for (point = 0; point < point_num; point++)
        {
            string cout = "  " + point.ToString(CultureInfo.InvariantCulture).PadLeft(4) + "  ";
            int    dim;
            for (dim = 0; dim < dim_num; dim++)
            {
                cout += grid_index[dim + point * dim_num].ToString(CultureInfo.InvariantCulture).PadLeft(6);
            }

            Console.WriteLine(cout);
        }
    }
Пример #6
0
    private static void test015(int dim_min, int dim_max, int level_max_min, int level_max_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST015 tests SPARSE_GRID_CCS_SIZE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    22 December 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int DIM_MIN, the minimum spatial dimension to consider.
    //
    //    Input, int DIM_MAX, the maximum spatial dimension to consider.
    //
    //    Input, int LEVEL_MAX_MIN, the minimum value of LEVEL_MAX to consider.
    //
    //    Input, int LEVEL_MAX_MAX, the maximum value of LEVEL_MAX to consider.
    //
    {
        int dim_num;
        int level_max;

        Console.WriteLine("");
        Console.WriteLine("TEST01");
        Console.WriteLine("  SPARSE_GRID_CCS_SIZE returns the number of distinct");
        Console.WriteLine("  points in a Clenshaw Curtis Slow-Growth sparse grid.");
        Console.WriteLine("");
        Console.WriteLine("  Each sparse grid is of spatial dimension DIM,");
        Console.WriteLine("  and is made up of all product grids of levels up to LEVEL_MAX.");
        Console.WriteLine("");
        string cout = "   DIM: ";

        for (dim_num = dim_min; dim_num <= dim_max; dim_num++)
        {
            cout += "  " + dim_num.ToString(CultureInfo.InvariantCulture).PadLeft(8);
        }

        Console.WriteLine(cout);
        Console.WriteLine("");
        Console.WriteLine("   LEVEL_MAX");
        Console.WriteLine("");

        for (level_max = level_max_min; level_max <= level_max_max; level_max++)
        {
            cout = "    " + level_max.ToString(CultureInfo.InvariantCulture).PadLeft(4);
            for (dim_num = dim_min; dim_num <= dim_max; dim_num++)
            {
                int point_num = Grid_ClenshawCurtis.sparse_grid_ccs_size(dim_num, level_max);
                cout += "  " + point_num.ToString(CultureInfo.InvariantCulture).PadLeft(8);
            }

            Console.WriteLine(cout);
        }
    }
Пример #7
0
    private static void Main(string[] args)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for SPARSE_GRID_CC_DATASET.
    //
    //  Discussion:
    //
    //    This program computes a sparse grid quadrature rule based on 1D
    //    Clenshaw-Curtis rules and writes it to a file.
    //
    //    The user specifies:
    //    * the spatial dimension of the quadrature region,
    //    * the level that defines the Smolyak grid.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    09 July 2009
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Fabio Nobile, Raul Tempone, Clayton Webster,
    //    A Sparse Grid Stochastic Collocation Method for Partial Differential
    //    Equations with Random Input Data,
    //    SIAM Journal on Numerical Analysis,
    //    Volume 46, Number 5, 2008, pages 2309-2345.
    //
    {
        int dim;
        int dim_num;
        int level_max;
        int point;

        Console.WriteLine("");
        Console.WriteLine("SPARSE_GRID_CC_DATASET");
        Console.WriteLine("");
        Console.WriteLine("  Compute the abscissas and weights of a quadrature rule");
        Console.WriteLine("  associated with a sparse grid derived from a Smolyak");
        Console.WriteLine("  construction based on 1D Clenshaw-Curtis rules.");
        Console.WriteLine("");
        Console.WriteLine("  Inputs to the program include:");
        Console.WriteLine("");
        Console.WriteLine("    DIM_NUM, the spatial dimension.");
        Console.WriteLine("    (typically in the range of 2 to 10)");
        Console.WriteLine("");
        Console.WriteLine("    LEVEL_MAX, the level of the sparse grid.");
        Console.WriteLine("    (typically in the range of 0, 1, 2, 3, ...");
        Console.WriteLine("");
        Console.WriteLine("  Output from the program includes:");
        Console.WriteLine("");
        Console.WriteLine("    * A printed table of the abscissas and weights.");
        Console.WriteLine("");
        Console.WriteLine("    * A set of 3 files that define the quadrature rule.");
        Console.WriteLine("");
        Console.WriteLine("    (1) cc_d?_level?_r.txt, the ranges;");
        Console.WriteLine("    (2) cc_d?_level?_w.txt, the weights;");
        Console.WriteLine("    (3) cc_d?_level?_x.txt, the abscissas.");
        //
        //  Get the spatial dimension.
        //
        try
        {
            dim_num = Convert.ToInt32(args[0]);
        }
        catch
        {
            Console.WriteLine("");
            Console.WriteLine("  Enter the value of DIM_NUM (1 or greater)");
            dim_num = Convert.ToInt32(Console.ReadLine());
        }

        Console.WriteLine("");
        Console.WriteLine("  Spatial dimension requested is = " + dim_num + "");
        //
        //  Get the level.
        //
        try
        {
            level_max = Convert.ToInt32(args[1]);
        }
        catch
        {
            Console.WriteLine("");
            Console.WriteLine("  Enter the value of LEVEL_MAX (0 or greater).");
            level_max = Convert.ToInt32(Console.ReadLine());
        }

        int level_min = Math.Max(0, level_max + 1 - dim_num);

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MIN is = " + level_min + "");
        Console.WriteLine("  LEVEL_MAX is = " + level_max + "");
        //
        //  How many distinct points will there be?
        //
        int point_num = Grid_ClenshawCurtis.sparse_grid_cfn_size(dim_num, level_max);

        Console.WriteLine("");
        Console.WriteLine("  The number of distinct abscissas in the");
        Console.WriteLine("  quadrature rule is determined from the spatial");
        Console.WriteLine("  dimension DIM_NUM and the level LEVEL_MAX.");
        Console.WriteLine("  For the given input, this value will be = " + point_num + "");
        //
        //  Allocate memory.
        //
        double[] r = new double[dim_num * 2];
        double[] w = new double[point_num];
        double[] x = new double[dim_num * point_num];
        //
        //  Compute the weights and points.
        //
        for (dim = 0; dim < dim_num; dim++)
        {
            r[dim + 0 * dim_num] = -1.0;
            r[dim + 1 * dim_num] = +1.0;
        }

        Grid_ClenshawCurtis.sparse_grid_cc(dim_num, level_max, point_num, ref w, ref x);

        typeMethods.r8mat_transpose_print_some(dim_num, point_num, x, 1, 1, dim_num,
                                               10, "  First 10 grid points:");

        typeMethods.r8vec_print_some(point_num, w, 1, 10, "  First 10 grid weights:");

        double weight_sum = 0.0;

        for (point = 0; point < point_num; point++)
        {
            weight_sum += w[point];
        }

        Console.WriteLine("");
        Console.WriteLine("  Weights sum to   " + weight_sum + "");
        Console.WriteLine("  Correct value is " + Math.Pow(2.0, dim_num) + "");
        //
        //  Construct appropriate file names.
        //
        string r_filename = "cc_d" + dim_num
                            + "_level" + level_max + "_r.txt";
        string w_filename = "cc_d" + dim_num
                            + "_level" + level_max + "_w.txt";
        string x_filename = "cc_d" + dim_num
                            + "_level" + level_max + "_x.txt";

        //
        //  Write the rule to files.
        //
        Console.WriteLine("");
        Console.WriteLine("  Creating R file = \"" + r_filename + "\".");

        typeMethods.r8mat_write(r_filename, dim_num, 2, r);

        Console.WriteLine("  Creating W file = \"" + w_filename + "\".");

        typeMethods.r8mat_write(w_filename, 1, point_num, w);

        Console.WriteLine("  Creating X file = \"" + x_filename + "\".");

        typeMethods.r8mat_write(x_filename, dim_num, point_num, x);

        Console.WriteLine("");
        Console.WriteLine("SPARSE_GRID_CC_DATASET");
        Console.WriteLine("  Normal end of execution.");

        Console.WriteLine("");
    }