Exemplo n.º 1
0
    def computeMean(self, grid, alpha, newGridPoints):
        gs = grid.getStorage()
        opEval = createOperationEval(grid)

        newGs = grid.getStorage()
        newNodalValues = dehierarchize(grid, alpha)
        p = DataVector(gs.dim())

        for newGp in newGridPoints:
            # run over all grid points of current level
            newGp.getCoords(p)
            pp = DataVector(p)
            i = newGs.seq(newGp)
            newNodalValues[i] = 0.
            for d in xrange(gs.dim()):
                # get current index
                index = newGp.getIndex(d)
                level = newGp.getLevel(d)
                # bounds
                xlow, xhigh = getBoundsOfSupport(level, index)
                # compute function values at bounds
                pp[d] = xlow
                fxlow = opEval.eval(alpha, pp)
                pp[d] = xhigh
                fxhigh = opEval.eval(alpha, pp)
                # interpolate linearly
                a = (fxhigh - fxlow) / (xlow - xhigh)
                newNodalValues[i] += a * (p[d] - xlow) + fxhigh
                # reset pp and set sumWeights
                pp[d] = p[d]
            newNodalValues[i] /= gs.dim()

        return newNodalValues
Exemplo n.º 2
0
    def computeMean(self, grid, alpha, newGridPoints):
        gs = grid.getStorage()
        opEval = createOperationEval(grid)

        newGs = grid.getStorage()
        newNodalValues = dehierarchize(grid, alpha)
        p = DataVector(gs.dim())

        for newGp in newGridPoints:
            # run over all grid points of current level
            newGp.getCoords(p)
            pp = DataVector(p)
            i = newGs.seq(newGp)
            newNodalValues[i] = 0.
            for d in xrange(gs.dim()):
                # get current index
                index = newGp.getIndex(d)
                level = newGp.getLevel(d)
                # bounds
                xlow, xhigh = getBoundsOfSupport(level, index)
                # compute function values at bounds
                pp[d] = xlow
                fxlow = opEval.eval(alpha, pp)
                pp[d] = xhigh
                fxhigh = opEval.eval(alpha, pp)
                # interpolate linearly
                a = (fxhigh - fxlow) / (xlow - xhigh)
                newNodalValues[i] += a * (p[d] - xlow) + fxhigh
                # reset pp and set sumWeights
                pp[d] = p[d]
            newNodalValues[i] /= gs.dim()

        return newNodalValues
Exemplo n.º 3
0
    def computeLinearFormEntry(self, gs, gp, basis, d):
        val = 1
        err = 0.

        # get level index
        lid, iid = gp.getLevel(d), gp.getIndex(d)

        # compute left and right boundary of the support of both
        # basis functions
        xlow, xhigh = getBoundsOfSupport(gs, lid, iid, self._gridType)
        xcenter = gs.getCoordinate(gp, d)

        # ----------------------------------------------------
        # use gauss-legendre-quadrature
        bounds = self._U[d].getBounds()
        if self._U is None or (isinstance(self._U[d], Uniform) and \
                               np.abs(bounds[0]) < 1e-14 and \
                               np.abs(bounds[1] - 1.0) < 1e-14):

            def f(p):
                return basis.eval(lid, iid, p)
        else:

            def f(p):
                q = self._T[d].unitToProbabilistic(p)
                return basis.eval(lid, iid, p) * self._U[d].pdf(q)

        # compute the piecewise continuous parts separately
        deg = gp.getLevel(d) + 2
        sleft, err1dleft = self.quad(f, xlow, xcenter, deg=deg)
        sright, err1dright = self.quad(f, xcenter, xhigh, deg=deg)

        #             # -----------------------------------------
        #             # plot the basis
        #             import numpy as np
        #             import matplotlib.pyplot as plt
        #             x = np.linspace(0, 1, 100)
        #             pdf = [self._U[d].pdf(xi) for xi in x]
        #             plt.plot(x, pdf)
        #             x = np.linspace(xlow, xhigh, 100)
        #             b1 = [basis.eval(lid, iid, xi) for xi in x]
        #             res = [f(xi) for xi in x]
        #
        #             plt.plot(x, b1)
        #             plt.plot(x, res)
        #             plt.xlim(0, 1)
        #             plt.title("(%i, %i), %g" % (lid, iid, s))
        #             plt.show()
        #             # ----------------------------------------------------

        val = val * (sleft + sright)
        err += val * (err1dleft + err1dright)

        return val, err
    def computeLinearFormEntry(self, gp, basis, d):
        val = 1
        err = 0.

        # get level index
        lid, iid = gp.getLevel(d), gp.getIndex(d)

        # compute left and right boundary of the support of both
        # basis functions
        xlow, xhigh = getBoundsOfSupport(lid, iid)

        # ----------------------------------------------------
        # use gauss-legendre-quadrature
        if self._U is None:

            def f(p):
                return basis.eval(lid, iid, p)
        else:

            def f(p):
                q = self._T[d].unitToProbabilistic(p)
                return basis.eval(lid, iid, p) * self._U[d].pdf(q)

        # compute the piecewise continuous parts separately
        sleft, err1dleft = self.quad(f,
                                     xlow, (xlow + xhigh) / 2,
                                     deg=gp.getLevel(d) + 2)
        sright, err1dright = self.quad(f, (xlow + xhigh) / 2,
                                       xhigh,
                                       deg=gp.getLevel(d) + 2)

        #             # -----------------------------------------
        #             # plot the basis
        #             import numpy as np
        #             import matplotlib.pyplot as plt
        #             x = np.linspace(0, 1, 100)
        #             pdf = [self._U[d].pdf(xi) for xi in x]
        #             plt.plot(x, pdf)
        #             x = np.linspace(xlow, xhigh, 100)
        #             b1 = [basis.eval(lid, iid, xi) for xi in x]
        #             res = [f(xi) for xi in x]
        #
        #             plt.plot(x, b1)
        #             plt.plot(x, res)
        #             plt.xlim(0, 1)
        #             plt.title("(%i, %i), %g" % (lid, iid, s))
        #             plt.show()
        #             # ----------------------------------------------------

        val = val * (sleft + sright)
        err += val * (err1dleft + err1dright)

        return val, err
    def computeLinearFormEntry(self, gp, basis, d):
        val = 1
        err = 0.

        # get level index
        lid, iid = gp.getLevel(d), gp.getIndex(d)

        # compute left and right boundary of the support of both
        # basis functions
        xlow, xhigh = getBoundsOfSupport(lid, iid)

        # ----------------------------------------------------
        # use gauss-legendre-quadrature
        if self._U is None:
            def f(p):
                return basis.eval(lid, iid, p)
        else:
            def f(p):
                q = self._T[d].unitToProbabilistic(p)
                return basis.eval(lid, iid, p) * self._U[d].pdf(q)

        # compute the piecewise continuous parts separately
        sleft, err1dleft = self.quad(f, xlow, (xlow + xhigh) / 2,
                                     deg=gp.getLevel(d) + 2)
        sright, err1dright = self.quad(f, (xlow + xhigh) / 2, xhigh,
                                       deg=gp.getLevel(d) + 2)

#             # -----------------------------------------
#             # plot the basis
#             import numpy as np
#             import matplotlib.pyplot as plt
#             x = np.linspace(0, 1, 100)
#             pdf = [self._U[d].pdf(xi) for xi in x]
#             plt.plot(x, pdf)
#             x = np.linspace(xlow, xhigh, 100)
#             b1 = [basis.eval(lid, iid, xi) for xi in x]
#             res = [f(xi) for xi in x]
#
#             plt.plot(x, b1)
#             plt.plot(x, res)
#             plt.xlim(0, 1)
#             plt.title("(%i, %i), %g" % (lid, iid, s))
#             plt.show()
#             # ----------------------------------------------------

        val = val * (sleft + sright)
        err += val * (err1dleft + err1dright)

        return val, err
Exemplo n.º 6
0
    def computeBilinearFormEntry(self, gs, gpi, basisi, gpj, basisj, d):
        val = 1
        err = 0.

        # get level index
        lid, iid = gpi.getLevel(d), gpi.getIndex(d)
        ljd, ijd = gpj.getLevel(d), gpj.getIndex(d)

        # compute left and right boundary of the support of both
        # basis functions
        xlowi, xhighi = getBoundsOfSupport(gs, lid, iid, self._gridType)
        xlowj, xhighj = getBoundsOfSupport(gs, ljd, ijd, self._gridType)

        xlow = max(xlowi, xlowj)
        xhigh = min(xhighi, xhighj)

        # same level, different index
        if self._gridType not in bsplineGridTypes and lid == ljd and iid != ijd and lid > 0:
            val = err = 0
        # the support does not overlap
        elif self._gridType not in bsplineGridTypes and lid != ljd and xlow >= xhigh:
            val = err = 0
        else:
            # ----------------------------------------------------
            # use scipy for integration
            bounds = self._U[d].getBounds()
            if self._U is None or (isinstance(self._U[d], Uniform) and \
                                   np.abs(bounds[0]) < 1e-14 and \
                                   np.abs(bounds[1] - 1.0) < 1e-14):

                def f(p):
                    return basisi.eval(lid, iid, p) * \
                        basisj.eval(ljd, ijd, p)
            else:

                def f(p):
                    q = self._T[d].unitToProbabilistic(p)
                    return basisi.eval(lid, iid, p) * \
                        basisj.eval(ljd, ijd, p) * \
                        self._U[d].pdf(q)

            # compute the piecewise continuous parts separately
            if lid > ljd:
                xcenter = gs.getCoordinate(gpi, d)
            else:
                xcenter = gs.getCoordinate(gpj, d)

            deg = 2 * (lid + 1) + 1
            sleft, err1dleft = self.quad(f, xlow, xcenter, deg=deg)
            sright, err1dright = self.quad(f, xcenter, xhigh, deg=deg)

            val = val * (sleft + sright)
            err += val * (err1dleft + err1dright)

#             # -----------------------------------------
#             # plot the basis
#             import numpy as np
#             import matplotlib.pyplot as plt
#             x = np.linspace(0, 1, 100)
#             b1 = [basisi.eval(lid, iid, xi) for xi in x]
#             b2 = [basisj.eval(ljd, ijd, xi) for xi in x]
#             pdf = [self._U[d].pdf(self._T[d].unitToProbabilistic(xi))
#                    for xi in x]
#             res = [f(xi) for xi in x]
#
#             plt.plot(x, b1, label="basis 1")
#             plt.plot(x, b2, label="basis 2")
#             plt.plot(x, pdf, label="pdf")
#             plt.plot(x, res, label="product")
#             plt.scatter(xcenter, 0.0)
#             plt.xlim(0, 1)
#             plt.title("[%i, %i] -> (%i, %i), (%i, %i), %g" % (xlow, xhigh, lid, iid, ljd, ijd, val))
#             plt.legend()
#             plt.show()
#             # ----------------------------------------------------

        return val, err
Exemplo n.º 7
0
    def computeBilinearFormEntry(self, gpi, basisi, gpj, basisj, d):
        val = 1
        err = 0.

        # get level index
        lid, iid = gpi.getLevel(d), gpi.getIndex(d)
        ljd, ijd = gpj.getLevel(d), gpj.getIndex(d)

        # compute left and right boundary of the support of both
        # basis functions
        xlowi, xhighi = getBoundsOfSupport(lid, iid)
        xlowj, xhighj = getBoundsOfSupport(ljd, ijd)

        xlow = max(xlowi, xlowj)
        xhigh = min(xhighi, xhighj)

        # same level, different index
        if lid == ljd and iid != ijd and lid > 0:
            val = err = 0
        # the support does not overlap
        elif lid != ljd and xlow >= xhigh:
            val = err = 0
        else:
            # ----------------------------------------------------
            # use scipy for integration
            if self._U is None:
                def f(p):
                    return basisi.eval(lid, iid, p) * \
                        basisj.eval(ljd, ijd, p)
            else:
                def f(p):
                    q = self._T[d].unitToProbabilistic(p)
                    return basisi.eval(lid, iid, p) * \
                        basisj.eval(ljd, ijd, p) * \
                        self._U[d].pdf(q)

            # compute the piecewise continuous parts separately
            sleft, err1dleft = self.quad(f, xlow, (xlow + xhigh) / 2,
                                         deg=2 * (gpi.getLevel(d) + 1) + 1)
            sright, err1dright = self.quad(f, (xlow + xhigh) / 2, xhigh,
                                           deg=2 * (gpi.getLevel(d) + 1) + 1)
#                 # -----------------------------------------
#                 # plot the basis
#                 import numpy as np
#                 import matplotlib.pyplot as plt
#                 x = np.linspace(0, 1, 100)
#                 b1 = [basisi.eval(lid, iid, xi) for xi in x]
#                 b2 = [basisj.eval(ljd, ijd, xi) for xi in x]
#                 pdf = [self._U[d].pdf(self._T[d].unitToProbabilistic(xi))
#                        for xi in x]
#                 res = [f(xi) for xi in x]
#
#                 plt.plot(x, b1, label="basis 1")
#                 plt.plot(x, b2, label="basis 2")
#                 plt.plot(x, pdf, label="pdf")
#                 plt.plot(x, res, label="product")
#                 plt.xlim(0, 1)
#                 plt.title("[%i, %i] -> (%i, %i), (%i, %i), %g" % (xlow, xhigh, lid, iid, ljd, ijd, s))
#                 plt.legend()
#                 plt.show()
#                 # ----------------------------------------------------
            val = val * (sleft + sright)
            err += val * (err1dleft + err1dright)

        return val, err