Пример #1
0
    def discretize1d_linear(self):
        # discretize the product of both
        grid1, alpha1 = self.interpolate(1, 3, 2)
        grid2, alpha2 = self.interpolate(1, 4, 6)
        jgrid, jalpha = discretizeProduct(grid1, alpha1, grid2, alpha2)

        # get reference values
        n = 200
        x = np.linspace(0, 1, n)
        y1 = [self.f([xi])**2 for xi in x]
        y2 = np.array([
            evalSGFunction(grid1, alpha1, np.array([xi])) *
            evalSGFunction(grid2, alpha2, np.array([xi])) for xi in x
        ])
        y3 = np.array(
            [evalSGFunction(jgrid, jalpha, np.array([xi])) for xi in x])

        assert np.sum(abs(y3 - y2)) < 1e-13

        plt.plot(x, y1, label="solution")
        plt.plot(x, y2, label="product")
        plt.plot(x, y3, label="poly")
        plt.title(
            "2 linear grids different level (maxlevel=%i, deg=%i), err = %g" %
            (jgrid.getStorage().getMaxLevel(), getDegree(jgrid),
             np.max(abs(y3 - y2))))
        plt.legend()
        plt.show()
Пример #2
0
def discretizeProduct(grid1, alpha1, grid2, alpha2):
    """
    Discretizes the product of two sparse grid functions:

        h(x) := f(x) * g(x)

    on a full grid with piecewise polynomial basis. Therefore
    a maximum number of grid points 10^6 is allowed.

    @param grid1: Grid, grid of f
    @param alpha1: DataVector, hierarchical coefficients of f
    @param grid2: Grid, grid of g
    @param alpha2: DataVector, hierarchical coefficients of g
    """
    # make sure that the grids are either piece wise linear
    # or piecewise polynomial
    if grid1.getType() not in [GridType_Linear, GridType_Poly]:
        raise AttributeError("grid type '%s' not supported" % grid1.getType())
    if grid2.getType() not in [GridType_Linear, GridType_Poly]:
        raise AttributeError("grid type '%s' not supported" % grid2.getType())

    # get the degrees of the grid
    maxlevelGrid1 = grid1.getStorage().getMaxLevel()
    maxlevelGrid2 = grid2.getStorage().getMaxLevel()
    deg1 = min(getDegree(grid1), maxlevelGrid1 + 1)
    deg2 = min(getDegree(grid2), maxlevelGrid2 + 1)
    deg = deg1 + deg2
    maxlevel = max(maxlevelGrid1 + deg2, maxlevelGrid2 + deg1)

    # check if maximum number of grid points is goint to be exceeded
    n = 2**((deg - 1) * grid1.getStorage().getDimension())
    if n > 1e6:
        raise AttributeError(
            "Can not create a full grid of level %i and dimensionality %i. The number of grid points %i would exceed 10^6"
            % (deg - 1, grid1.getStorage().getDimension(), n))

    # join the two grids
    joinedGrid = Grid.createPolyGrid(grid1.getStorage().getDimension(), deg)
    joinedGrid.getGenerator().full(maxlevel)
    # interpolate the product on the new grid
    joinedAlpha = interpolateProduct(grid1, alpha1, grid2, alpha2, joinedGrid)

    return joinedGrid, joinedAlpha
Пример #3
0
 def fromGrid(self, grid):
     """
     Indicates that all grid points in grid should also be in the
     new grid
     @param grid:
     """
     self.__grid = grid
     self.__dim = grid.getStorage().dim()
     self.__deg = getDegree(grid)
     self.__border = hasBorder(grid)
     if self.__border:
         self.level = 0
     return self
Пример #4
0
    def fromGrid(self, grid):
        """
        Indicates that all grid points in grid should also be in the
        new grid
        @param grid:
        """
        self.__grid = grid
        self.__dim = grid.getStorage().getDimension()
        self.__deg = getDegree(grid)
        self.__gridType = grid.getType()
        if hasBorder(grid.getType()):
            self.__boundaryLevel = 1
            self.level = 0

        return self
Пример #5
0
    def discretize1d_identity(self):
        # discretize the product of both
        grid, alpha = self.interpolate(1, 3, 4)
        jgrid, jalpha = discretizeProduct(grid, alpha, grid, alpha)

        # get reference values
        n = 200
        x = np.linspace(0, 1, n)
        y1 = np.array([self.f([xi])**2 for xi in x])
        y2 = np.array(
            [evalSGFunction(grid, alpha, np.array([xi]))**2 for xi in x])
        y3 = np.array(
            [evalSGFunction(jgrid, jalpha, np.array([xi])) for xi in x])

        assert np.sum(abs(y3 - y2)) < 1e-13

        plt.plot(x, y1, label="solution")
        plt.plot(x, y2, label="product")
        plotSG1d(jgrid, jalpha, n=n, label="poly")
        plt.title("1 linear grid same level (maxlevel=%i, deg=%i), err = %g" %
                  (jgrid.getStorage().getMaxLevel(), getDegree(jgrid),
                   np.sum(abs(y3 - y2))))
        plt.legend()
        plt.show()
Пример #6
0
    def discretize2d_linear(self):
        # discretize the product of both
        grid1, alpha1 = self.interpolate(2, 3, 2)
        grid2, alpha2 = self.interpolate(2, 3, 3)
        jgrid, jalpha = discretizeProduct(grid1, alpha1, grid2, alpha2)

        # get reference values
        def f(x):
            return evalSGFunction(grid1, alpha1, x) * evalSGFunction(
                grid2, alpha2, x)

        n = 50
        fig, ax, y1 = plotFunction3d(f, n=n)
        ax.set_title("product")
        fig.show()
        fig, ax, y2 = plotSG3d(jgrid, jalpha, n=n)
        ax.set_title(
            "(size=%i, maxlevel=%i, deg=%i), err = %g" %
            (jgrid.getStorage().getSize(), jgrid.getStorage().getMaxLevel(),
             getDegree(jgrid), np.max(abs(y1 - y2))))
        fig.show()

        assert np.max(np.abs(y1 - y2)) < 1e-13
        plt.show()