def createExtensionMat(self,cp = None): '''create a real PETSc.Mat() for extension''' p = self.interpDegree + 1 d = self.Dim if cp is None: wvec = self.wvec cp = self.cp else: wvec = PETSc.Vec().createMPI((cp.shape[0],PETSc.DECIDE)) gvec = self.gvec extMat = PETSc.Mat().create(self.comm) extMat.setSizes((wvec.sizes,gvec.sizes)) extMat.setFromOptions() extMat.setPreallocationNNZ((p**d,p**d)) Xgrid,ind = self.findIndForIntpl(cp) weights = buildInterpWeights(Xgrid,cp,self.hGrid,p) (start,end) = extMat.getOwnershipRange() ranges = end - start for i in xrange(ranges): extMat[i+start,ind[i]] = weights[i] extMat.assemble() return extMat
def createExtensionMatForLoop(self,cp = None): '''create a real PETSc.Mat() for extension using for loop''' p = self.interpDegree + 1 d = self.Dim if cp is None: wvecsizes = self.wvec.sizes cp = self.cp else: wvecsizes = (cp.shape[0],PETSc.DECIDE) gvec = self.gvec extMat = PETSc.Mat().create(self.comm) extMat.setSizes((wvecsizes,gvec.sizes)) extMat.setFromOptions() extMat.setPreallocationNNZ((p**d,p**d)) (start,end) = extMat.getOwnershipRange() #@UnusedVariable bsize = 1000 for i in xrange(0,cp.shape[0],bsize): Xgrid,ind = self.findIndForIntpl(cp[i:i+bsize]) weights = buildInterpWeights(Xgrid,cp[i:i+bsize],self.hGrid,p) ranges = weights.shape[0] for j in xrange(ranges): extMat[j+start+i,ind[j]] = weights[j] extMat.assemble() return extMat
def build_interp_matrix(int_band, cp, dx, p, ll, shape): dim = len(shape) offsets = np.mgrid[tuple(slice(p + 1) for _ in xrange(dim))].reshape((dim, -1)) Ei = np.tile(np.arange(cp.shape[0])[:, np.newaxis], (p + 1) ** dim) Ej = np.zeros_like(Ei) Es = np.zeros_like(Ei, dtype=np.float) base_points = findGridInterpBasePt(cp, dx, ll, p) weights = buildInterpWeights(base_points * dx + ll, cp, dx, p + 1) Ej = np.ravel_multi_index( (base_points[..., np.newaxis] + offsets[np.newaxis, ...]).transpose((0, 2, 1)).reshape((-1, dim)).T, shape ) Es = weights E = coo_matrix((Es.ravel(), (Ei.ravel(), Ej.ravel())), (cp.shape[0], reduce(mul, shape))).tocsr() return E[:, np.ravel_multi_index(int_band.T, shape)]
def createExtensionMat(self, p = None, cp = None): '''create a PETSc.Mat() for the closest point extension''' if p is None: p = self.interpDegree if cp is None: wvecsizes = self.wvec.sizes cp = self.cp else: wvecsizes = (cp.shape[0],PETSc.DECIDE) gvec = self.gvec dim = self.Dim dx = (self.hGrid,)*dim M = self.M m = self.m # The lower left grid point 'll' is at the center of the lower left element 'self.ll' ll = np.array(self.ll) + self.hGrid/2; # find the sub-indices of the base points, 'subBasept' is a N*dim array (N:number of base-points). subBasept = findGridInterpBasePt(cp, dx, ll, p) # offsets is the sub-indices of the interpolation stencil, a dim*STENCIL array (STENCIL=(p+1)^dim). # If in 'C' order, should be the following line: offsets = np.mgrid[(slice(p+1),)*dim].reshape((dim, -1)) # or equivalently the following two lines: # x = np.arange((p+1)**dim) # offsets = np.column_stack(np.unravel_index(x,(p+1,)*dim,order='C')).T #If in the structure branch, it should be 'Fortran' order: # x = np.arange((p+1)**dim) # offsets = np.column_stack(np.unravel_index(x,(p+1,)*dim,order='F')).T # The sub indices of the whole interpolation stencil with Fortran order, a dim*N*STENCIL array. sub = ( subBasept[...,np.newaxis] + offsets[np.newaxis,...] ).transpose((1,0,2)) # Convert sub indices to global PETSc indices ind = self.subToPetscInd(sub) basept = subBasept*dx + ll weights = buildInterpWeights(basept,cp,dx,p+1) E = self.createMat((wvecsizes,gvec.sizes),ind,weights) return E