def balance(grid): gs = grid.getStorage() newgps = [] for gp in [gs.getPoint(i) for i in range(gs.getSize())]: for dim in range(gs.getDimension()): # left child in dimension dim lgp = HashGridPoint(gp) lgp.getLeftChild(dim) # right child in dimension dim rgp = HashGridPoint(gp) rgp.getRightChild(dim) if gs.isContaining(lgp) and not gs.isContaining(rgp): inserted = insertPoint(grid, rgp) elif gs.isContaining(rgp) and not gs.isContaining(lgp): inserted = insertPoint(grid, lgp) else: inserted = [] newgps += inserted gs.recalcLeafProperty() return newgps
def addChildren(self, grid, gp): gs = grid.getStorage() for d in range(gs.getDimension()): # check left child in d gpl = HashGridPoint(gp) gpl.getLeftChild(d) if not gs.isContaining(gpl) and isValid(grid, gpl) and \ self.checkRange(gpl, self.maxLevel): self.addCollocationNode(grid, gpl) # check right child in d gpr = HashGridPoint(gp) gpr.getRightChild(d) if not gs.isContaining(gpr) and isValid(grid, gpr) and \ self.checkRange(gpr, self.maxLevel): self.addCollocationNode(grid, gpr)
def isRefineable(grid, gp): gs = grid.getStorage() for d in range(gs.getDimension()): # left child in dimension dim gpl = HashGridPoint(gp) gpl.getLeftChild(d) if not gs.isContaining(gpl) and isValid(grid, gpl): return True # right child in dimension dim gpr = HashGridPoint(gp) gpr.getRightChild(d) if not gs.isContaining(gpr) and isValid(grid, gpr): return True return False
def insert_children(grid, gp, d): cnt = [] gs = grid.getStorage() # left child in dimension dim gpl = HashGridPoint(gp) gpl.getLeftChild(d) if not gs.isContaining(gpl) and isValid(grid, gpl): success = gs.insert(gpl) > -1 cnt += 1 if success else 0 # right child in dimension dim gpr = HashGridPoint(gp) gpr.getRightChild(d) if not gs.isContaining(gpr) and isValid(grid, gpr): success = gs.insert(gpr) > -1 cnt += 1 if success else 0 return cnt
def getMaxLevelOfChildrenUpToMaxLevel(self, gp, grid, idim): gs = grid.getStorage() children = [] gps = [gp] while len(gps) > 0: currentgp = gps.pop() children.append(getLevel(currentgp)) if currentgp.getLevel(idim) < self.maxLevel: gpl = HashGridPoint(currentgp) gpl.getLeftChild(idim) if gs.isContaining(gpl): gps.append(gpl) # get right child gpr = HashGridPoint(currentgp) gs.right_child(gpr, idim) if gs.isContaining(gpr): gps.append(gpr) return children
def refine(self, grid, gp): ans = [] gs = grid.getStorage() for d in range(gs.getDimension()): gpl = HashGridPoint(gp) gpl.getLeftChild(d) if isValid(grid, gpl): ans += insertPoint(grid, gpl) if hasBorder(grid.getType()): ans += insertTruncatedBorder(grid, gpl) gpr = HashGridPoint(gp) gpr.getRightChild(d) if isValid(grid, gpr): ans += insertPoint(grid, gpr) if hasBorder(grid.getType()): ans += insertTruncatedBorder(grid, gpr) gs.recalcLeafProperty() return ans
def split(self, idim): # split the current grid up into three new ones gs = self.grid.getStorage() # central grid centralFullGrid = LocalFullGrid.copy(self) centralFullGrid.fullGridLevels[idim] = 1 # left grid gpLeft = HashGridPoint(self.gp) gpLeft.getLeftChild(idim) fullGridLevels = np.array(self.fullGridLevels) fullGridLevels[idim] -= 1 leftFullGrid = LocalFullGrid(self.grid, gpLeft, fullGridLevels) # right grid gpRight = HashGridPoint(self.gp) gpRight.getRightChild(idim) fullGridLevels = np.array(self.fullGridLevels) fullGridLevels[idim] -= 1 rightFullGrid = LocalFullGrid(self.grid, gpRight, fullGridLevels) return centralFullGrid, leftFullGrid, rightFullGrid
def getMaxLevelOfChildrenUpToMaxLevel(self, gp, grid, idim): gs = grid.getStorage() children = [] maxSteps = 0 gps = [(0, gp)] while len(gps) > 0: currentSteps, currentgp = gps.pop() if maxSteps < currentSteps: maxSteps = currentSteps gpl = HashGridPoint(currentgp) gpl.getLeftChild(idim) if gs.isContaining(gpl): gps.append((currentSteps + 1, gpl)) # get right child gpr = HashGridPoint(currentgp) gpr.getRightChild(idim) if gs.isContaining(gpr): gps.append((currentSteps + 1, gpr)) return maxSteps
def hierarchizeBruteForce(grid, nodalValues, ignore=None): if hasBorder(grid): print( 'brute force hierarchization is not supported for boundary grids') return nodalValues alpha = DataVector(nodalValues) gs = grid.getStorage() basis = getBasis(grid) # hierarchize dimension-wise for d in range(gs.getDimension()): # compute starting points by level sum ixs = {} for i in range(gs.getSize()): accLevel = gs.getPoint(i).getLevel(d) if accLevel in ixs: ixs[accLevel].append(i) else: ixs[accLevel] = [i] # collect all possible starting points starting_points = [] for key in sorted(ixs.keys()): starting_points += ixs[key] while len(starting_points) > 0: # get next starting node ix = starting_points.pop(0) gp = gs.getPoint(ix) # append left and right child gpl = HashGridPoint(gp) gpl.getLeftChild(d) gpr = HashGridPoint(gp) gpr.getRightChild(d) gps = [] if gs.isContaining(gpr): gps.append(gpr) if gs.isContaining(gpl): gps.append(gpl) while len(gps) > 0: gpc = gps.pop() ix = gs.getSequenceNumber(gpc) # removeSample point from possible starting points starting_points.remove(ix) diff = evalHierToTop(basis, grid, alpha, gpc, d) # print( "%i: %.20f - %.20f = %.20f" % (ix, alpha[ix], diff, alpha[ix] - diff) ) alpha[ix] -= diff # append left and right child gpl = HashGridPoint(gpc) gpl.getLeftChild(d) gpr = HashGridPoint(gpc) gpr.getRightChild(d) if gs.isContaining(gpr): gps.append(gpr) if gs.isContaining(gpl): gps.append(gpl) return alpha
p = DataVector(gs.getDimension()) for i in range(gs.getSize()): gs.getCoordinates(gs.getPoint(i), p) nodalValues[i] = f(p.array()) v = hierarchize(grid, nodalValues) ipar = 1 assert ipar < len(v) gp = gs.getPoint(ipar) print("Grand father:", gs.getCoordinate(gs.getPoint(0), 0), v[0]) print("Father:", gs.getCoordinate(gs.getPoint(0), 0), v[ipar]) gpl = HashGridPoint(gp) gpl.getLeftChild(0) gpr = HashGridPoint(gp) gpr.getRightChild(0) assert gsc.isContaining(gpl) assert gsc.isContaining(gpr) print(gs.getCoordinate(gpl, 0), \ estimateSurplus(grid, gpl, v), \ estimateConvergence(grid, gpl, v), \ w[gsc.getSequenceNumber(gpl)]) print(gs.getCoordinate(gpr, 0), \ estimateSurplus(grid, gpr, v), \ estimateConvergence(grid, gpr, v), \ w[gsc.getSequenceNumber(gpr)])