示例#1
0
def plotGrid(dim,level):
    sg = pysg.sparseGrid(dim=dim,level=level)
    sg.generatePoints()
    points = sg.indices
    pts = len(points)
    xpts = np.empty(pts)
    if dim==1:
        for i in xrange(pts):
            pt = tuple(points[i])
            xpts[i] = sg.gP[pt].pointPosition(pt)[0]
        plt.plot(xpts,np.ones_like(xpts),'*')
    elif dim==2:
        ypts = np.empty(pts)
        for i in xrange(pts):
            pt = tuple(points[i])
            xpts[i], ypts[i] = sg.gP[pt].pointPosition(pt)
        plt.plot(xpts,ypts,'*')
    else:
        if dim > 3:
            print "Showing first three dimensions only"
        from mpl_toolkits.mplot3d import Axes3D
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ypts = np.empty(pts)
        zpts = np.empty(pts)
        for i in xrange(pts):
            pt = tuple(points[i])
            xpts[i], ypts[i], zpts[i] = sg.gP[pt].pointPosition(pt)[:3]
        ax.scatter(xpts, ypts, zpts)    
示例#2
0
def example():
    # There is an error here. the coefficient for loc [0.25,0.5] should be 0.
    def f(x):
        return x[0]
    dim = 2
    level = 3
    sg = pysg.sparseGrid(dim=dim,level=level)
    sg.setFunctionValues(f)
    sg.nodal2Hier()
    sg.setCoefficients()
    total = 0
    for i in xrange(len(sg.indices)):
        total += sg.gP[tuple(sg.indices[i])].coeff
    total /= 2**((level-1)*dim)*(dim+1)
    print total
示例#3
0
def example():
    # There is an error here. the coefficient for loc [0.25,0.5] should be 0.
    def f(x):
        return x[0]

    dim = 2
    level = 3
    sg = pysg.sparseGrid(dim=dim, level=level)
    sg.setFunctionValues(f)
    sg.nodal2Hier()
    sg.setCoefficients()
    total = 0
    for i in xrange(len(sg.indices)):
        total += sg.gP[tuple(sg.indices[i])].coeff
    total /= 2**((level - 1) * dim) * (dim + 1)
    print total
示例#4
0
 def testSGNoBound3D(self):
   sg = pysg.sparseGrid(3,3)
   sg.generatePoints()
   # right number of grid points
   self.assertEqual(len(sg.indices),31)
   for i in range(len(sg.indices)):
     sum = 1.0
     pos = sg.gP[tuple(sg.indices[i])].pos
     for j in range(len(pos)):
       sum *= 4.*pos[j]*(1.0-pos[j])
     sg.gP[tuple(sg.indices[i])].fv = sum
   # convert to hierarchical values
   sg.nodal2Hier()
   # does the evaluation of sparse grid function in
   # hierarchical values give the correct value gv
   for i in range(len(sg.indices)):
     self.assertEqual(sg.gP[tuple(sg.indices[i])].fv,\
       sg.evalFunct(sg.gP[tuple(sg.indices[i])].pos))
def sparseGridPosList(dim, levels):
    sg = pysg.sparseGrid(dim, levels)
    sg.generatePoints()
    n = len(sg.indices)
    positions = np.zeros((dim, n))
    for i in range(n):
        positions[:, i] = sg.gP[tuple(sg.indices[i])].pos
    return (positions)


#positions = sparseGridPosList(dim, levels)
#fig2, axs2 = plt.subplots(1, 1, figsize=(10, 10))
#axs2.plot(positions[0,:], positions[1,:], 'x')

#
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
#ax.scatter(positions[0,:], positions[1,:], positions[2,:])
示例#6
0
 def testSGNoBound3D(self):
     sg = pysg.sparseGrid(3, 3)
     sg.generatePoints()
     # right number of grid points
     self.assertEqual(len(sg.indices), 31)
     for i in range(len(sg.indices)):
         sum = 1.0
         pos = sg.gP[tuple(sg.indices[i])].pos
         for j in range(len(pos)):
             sum *= 4. * pos[j] * (1.0 - pos[j])
         sg.gP[tuple(sg.indices[i])].fv = sum
     # convert to hierarchical values
     sg.nodal2Hier()
     # does the evaluation of sparse grid function in
     # hierarchical values give the correct value gv
     for i in range(len(sg.indices)):
         self.assertEqual(sg.gP[tuple(sg.indices[i])].fv,\
           sg.evalFunct(sg.gP[tuple(sg.indices[i])].pos))
示例#7
0
  def testSGNoBound3D(self):
#
#  SG is a sparse grid of dimension 3 and level 3.
#  Create sg.indices which stores level and position for each point.
#
    sg = pysg.sparseGrid(3,3)
#
#  Determine sg.gP with the coordinates of the points 
#  associated with the sparse grid index set.
#
    sg.generatePoints()
#
#  Print the points in the grid.
#
    print ""
    print "Coordinates of points in 3D sparse grid of level 3."
    print ""
    for i in range ( len(sg.indices) ):
      sg.gP[tuple(sg.indices[i])].printPoint()
#
#  Did we compute the right number of grid points?
#
    self.assertEqual ( len ( sg.indices), 31 )
#
#  Evaluate 4x(1-x)*4y(1-y)*4z(1-z) at each grid point.
#
    for i in range ( len(sg.indices) ):
      sum = 1.0
      pos = sg.gP[tuple(sg.indices[i])].pos
      for j in range(len(pos)):
        sum *= 4.*pos[j]*(1.0-pos[j])
      sg.gP[tuple(sg.indices[i])].fv = sum
#
#  Convert the sparse grid from nodal to hierarchical values.
#
    sg.nodal2Hier()
#
#  Does the evaluation of the sparse grid function in
#  hierarchical values give the correct value gv?
#
    for i in range(len(sg.indices)):
      self.assertEqual(sg.gP[tuple(sg.indices[i])].fv,\
        sg.evalFunct(sg.gP[tuple(sg.indices[i])].pos))
示例#8
0
def prob4():
    """Plot the grid points for a 2-D grid of level 5.
    """
    grid = pysg.sparseGrid(2,5)
    grid.plotGrid()
示例#9
0
def prob4():
    """Plot the grid points for a 2-D grid of level 5.
    """
    grid = pysg.sparseGrid(2, 5)
    grid.plotGrid()