示例#1
0
    private static void test06(int dim_num, int level_max)

    //***************************************************************************80
    //
    //  Purpose:
    //
    //    TEST06 creates a sparse Gauss-Hermite grid and writes it to a file.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 December 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_HERMITE to make a sparse Gauss-Hermite grid.");
        Console.WriteLine("  Write the data to a set of quadrature files.");

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

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MIN = " + level_min + "");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_Hermite.sparse_grid_hermite_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] = -typeMethods.r8_huge();
            r[dim + 1 * dim_num] = +typeMethods.r8_huge();
        }

        Grid_Hermite.sparse_grid_hermite(dim_num, level_max, point_num, ref w, ref x);
        //
        //  Write the data out.
        //
        string r_filename = "gh_d" + dim_num
                            + "_level" + level_max + "_r.txt";
        string w_filename = "gh_d" + dim_num
                            + "_level" + level_max + "_w.txt";
        string x_filename = "gh_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 + "\".");
    }
示例#2
0
    private static void Main(string[] args)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    MAIN is the main program for SPARSE_GRID_HERMITE_DATASET.
    //
    //  Discussion:
    //
    //    This program computes a sparse grid quadrature rule based on a 1D
    //    Gauss-Hermite rule 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:
    //
    //    10 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_HERMITE_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 a 1D Gauss-Hermite rule.");
        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) herm_d?_level?_r.txt, the ranges;");
        Console.WriteLine("    (2) herm_d?_level?_w.txt, the weights;");
        Console.WriteLine("    (3) herm_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_Hermite.sparse_grid_hermite_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] = -typeMethods.r8_huge();
            r[dim + 1 * dim_num] = +typeMethods.r8_huge();
        }

        Grid_Hermite.sparse_grid_hermite(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.Sqrt(Math.Pow(Math.PI, dim_num)) + "");
        //
        //  Construct appropriate file names.
        //
        string r_filename = "herm_d" + dim_num
                            + "_level" + level_max + "_r.txt";
        string w_filename = "herm_d" + dim_num
                            + "_level" + level_max + "_w.txt";
        string x_filename = "herm_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_HERMITE_DATASET");
        Console.WriteLine("  Normal end of execution.");

        Console.WriteLine("");
    }
示例#3
0
    private static void test05(int dim_num, int level_max, int degree_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST05 tests a Gauss-Hermite 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:
    //
    //    05 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;

        Console.WriteLine("");
        Console.WriteLine("TEST05");
        Console.WriteLine("  Check the exactness of a Gauss-Hermite sparse");
        Console.WriteLine("  grid quadrature rule, applied to all monomials ");
        Console.WriteLine("  of orders 0 to DEGREE_MAX.");

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

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MIN = " + level_min + "");
        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 + "");
        //
        //  Determine the number of points in the rule.
        //
        int point_num = Grid_Hermite.sparse_grid_hermite_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_Hermite.sparse_grid_hermite(dim_num, level_max, point_num, ref grid_weight, ref grid_point);
        //
        //  Explore the monomials.
        //
        int[] expon = new int[dim_num];

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

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

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

                double quad_error = HermiteQuadrature.monomial_quadrature_hermite(dim_num, expon, point_num,
                                                                                  grid_weight, grid_point);

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

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

                Console.WriteLine(cout);

                if (!more)
                {
                    break;
                }
            }
        }
    }
示例#4
0
    private static void test04(int dim_num, int level_max)

    //***************************************************************************80
    //
    //  Purpose:
    //
    //    TEST04 sums the weights and compares them to SQRT(PI)^DIM_NUM.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 October 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 Gauss-Hermite sparse grid .");
        Console.WriteLine("");
        Console.WriteLine("  As a simple test, sum these weights.");
        Console.WriteLine("  They should sum to exactly Math.Sqrt ( pi^DIM_NUM ).");

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

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MIN = " + level_min + "");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_Hermite.sparse_grid_hermite_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_Hermite.sparse_grid_hermite(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.Sqrt(Math.Pow(Math.PI, 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(14)
                          + "  " + weight_sum_exact.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                          + "  " + weight_sum_error.ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
    }
示例#5
0
    private static void test03(int dim_num, int level_max)

    //***************************************************************************80
    //
    //  Purpose:
    //
    //    TEST03 call SPARSE_GRID_HERMITE to create a sparse Gauss-Hermite grid.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 October 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_HERMITE makes a sparse Gauss-Hermite grid.");

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

        Console.WriteLine("");
        Console.WriteLine("  LEVEL_MIN = " + level_min + "");
        Console.WriteLine("  LEVEL_MAX = " + level_max + "");
        Console.WriteLine("  Spatial dimension DIM_NUM = " + dim_num + "");
        //
        //  Determine the number of points.
        //
        int point_num = Grid_Hermite.sparse_grid_hermite_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_Hermite.sparse_grid_hermite(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("0.######").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("0.######").PadLeft(9);
            }

            Console.WriteLine(cout);
        }
    }
示例#6
0
    private static void test02(int dim_num, int level_max)

    //***************************************************************************80
    //
    //  Purpose:
    //
    //    TEST02 tests SPARSE_GRID_HERMITE_INDEX.
    //
    //  Discussion:
    //
    //    The routine computes abstract indices that describe the sparse grid
    //    of Gauss-Hermite points.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    07 October 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_HERMITE_INDEX returns abstract indices for the");
        Console.WriteLine("  points that make up a Gauss-Hermite sparse grid.");

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

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

        int point_num = Grid_Hermite.sparse_grid_hermite_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 = new int[dim_num * point_num];
        int[] grid_base  = new int[dim_num * point_num];

        Grid_Hermite.sparse_grid_hermite_index(dim_num, level_max, point_num, ref grid_index,
                                               ref grid_base);
        //
        //  Now we're done.  Print the merged grid data.
        //
        Console.WriteLine("");
        Console.WriteLine("  Grid index/base:");
        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);
            cout = "        ";
            for (dim = 0; dim < dim_num; dim++)
            {
                cout += grid_base[dim + point * dim_num].ToString(CultureInfo.InvariantCulture).PadLeft(6);
            }

            Console.WriteLine(cout);
        }
    }
示例#7
0
    private static void test01(int dim_min, int dim_max, int level_max_min, int level_max_max)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests SPARSE_GRID_HERMITE_SIZE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 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_HERMITE_SIZE returns the number of distinct");
        Console.WriteLine("  points in a Gauss-Hermite sparse grid.");
        Console.WriteLine("");
        Console.WriteLine("  Note that, unlike most sparse grids, a sparse grid based on");
        Console.WriteLine("  Gauss-Hermite points is almost entirely NOT nested.");
        Console.WriteLine("");
        Console.WriteLine("  Hence the point counts should be much higher than for a grid of");
        Console.WriteLine("  the same level, but using rules such as Fejer1 or Fejer2 or");
        Console.WriteLine("  Gauss-Patterson or Newton-Cotes-Open or Newton-Cotes-Open-Half.");
        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(10);
        }

        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(10);
            for (dim_num = dim_min; dim_num <= dim_max; dim_num++)
            {
                int point_num = Grid_Hermite.sparse_grid_hermite_size(dim_num, level_max);
                cout += "  " + point_num.ToString(CultureInfo.InvariantCulture).PadLeft(10);
            }

            Console.WriteLine(cout);
        }
    }