def extend_grid_1d(grid, *args, **kws): gs = grid.getStorage() accLevel = gs.getMaxLevel() dim = gs.dim() # create dim+1 dimensional grid of level 0 new_grid = createGrid(grid, dim + 1, *args, **kws) # create 1 dimensional reference grid of level accLevel ref_grid = createGrid(grid, 1) ref_grid.createGridGenerator().regular(accLevel) # == full grid in dim = 1 ref_gs = ref_grid.getStorage() # create cross product between the 1d and the dimd-grid for i in xrange(gs.size()): gp = gs.get(i) new_gp = HashGridIndex(dim + 1) # copy level index vectors from old grid to the new one for d in xrange(gs.dim()): new_gp.set(d, gp.getLevel(d), gp.getIndex(d)) # get the indices in the missing dimension for j in xrange(ref_gs.size()): ref_gp = ref_gs.get(j) new_gp.set(dim, ref_gp.getLevel(0), ref_gp.getIndex(0)) insertPoint(new_grid, new_gp) return new_grid
def project(grid, dims): """ Project all grid points to the given dimensions @param grid: Grid sparse grid @param dims: list dimensions to which the grid points are projected """ gs = grid.getStorage() # create a new empty grid dim = len(dims) gps = [None] * gs.size() # run over all grid points in grid and # project them to the dimensions dims for i in xrange(gs.size()): gp = gs.get(i) ngp = HashGridIndex(dim) # copy level index to new grid point for k, d in enumerate(dims): ngp.set(k, gp.getLevel(d), gp.getIndex(d)) # insert it to the new grid gps[i] = ngp # compute new basis ngrid = createGrid(grid, len(dims)) basis = getBasis(ngrid) return gps, basis
def parent(grid, gp, d): # get parent level = gp.getLevel(d) - 1 index = gp.getIndex(d) / 2 + ((gp.getIndex(d) + 1) / 2) % 2 if isValid1d(grid, level, index): # create parent ans = HashGridIndex(gp) ans.set(d, level, index) return ans return None
def hasChildren(grid, gp): gs = grid.getStorage() d = 0 gpn = HashGridIndex(gp) while d < gs.dim(): # load level index level, index = gp.getLevel(d), gp.getIndex(d) # check left child in d gs.left_child(gp, d) if gs.has_key(gpn): return True # check right child in d gp.set(d, level, index) gs.right_child(gp, d) if gs.has_key(gpn): return True gpn.set(d, level, index) d += 1 return False
def insertHierarchicalAncestors(grid, gp): """ insert all hierarchical ancestors recursively to the grid @param grid: Grid @param gp: HashGridIndex @return: list of HashGridIndex, contains all the newly added grid points """ newGridPoints = [] gs = grid.getStorage() gps = [gp] while len(gps) > 0: gp = gps.pop() gpc = HashGridIndex(gp) for dim in xrange(gp.dim()): oldlevel, oldindex = gpc.getLevel(dim), gpc.getIndex(dim) # run up to the root node until you find one existing node level, index = oldlevel, oldindex while level > 1: level -= 1 index = index / 2 + ((index + 1) / 2) % 2 gpc.set(dim, level, index) if not gs.has_key(gpc): newGridPoints.append(HashGridIndex(gpc)) else: break # reset the point gpc.set(dim, oldlevel, oldindex) # insert the grid points in a list and add the hierarchical ancestors # of them for gp in newGridPoints: gps += insertPoint(grid, gp) return newGridPoints
def __doMarginalize(grid, alpha, dd, measure=None): gs = grid.getStorage() dim = gs.dim() if dim < 2: raise AttributeError("The grid has to be at least of dimension 2") if dd >= dim: raise AttributeError("The grid has only %i dimensions, so I can't \ integrate over %i" % (dim, dd)) # create new grid n_dim = dim - 1 n_grid = createGrid(grid, n_dim) n_gs = n_grid.getStorage() # insert grid points n_gp = HashGridIndex(n_dim) for i in xrange(gs.size()): gp = gs.get(i) for d in range(dim): if d == dd: # omit marginalization direction continue elif d < dd: n_gp.set(d, gp.getLevel(d), gp.getIndex(d)) else: n_gp.set(d - 1, gp.getLevel(d), gp.getIndex(d)) # insert grid point if not n_gs.has_key(n_gp): n_gs.insert(n_gp) n_gs.recalcLeafProperty() # create coefficient vector n_alpha = DataVector(n_gs.size()) n_alpha.setAll(0.0) # set function values for n_alpha for i in xrange(gs.size()): gp = gs.get(i) for d in range(dim): if d == dd: dd_level = gp.getLevel(d) dd_index = gp.getIndex(d) elif d < dd: n_gp.set(d, gp.getLevel(d), gp.getIndex(d)) else: n_gp.set(d - 1, gp.getLevel(d), gp.getIndex(d)) if not n_gs.has_key(n_gp): raise Exception("This should not happen!") # compute the integral of the given basis if measure is None: q, err = getIntegral(grid, dd_level, dd_index), 0. else: dist, trans = measure[0][dd], measure[1][dd] lf = LinearGaussQuadratureStrategy([dist], [trans]) basis = getBasis(grid) gpdd = HashGridIndex(1) gpdd.set(0, dd_level, dd_index) q, err = lf.computeLinearFormByList([gpdd], basis) q = q[0] # search for the corresponding index j = n_gs.seq(n_gp) n_alpha[j] += alpha[i] * q return n_grid, n_alpha, err