예제 #1
0
    def findStencilsOnStencilSets(self, level, bdyfcn=bdyfcn_null):
        from time import time
        print "icpm preparartion: diff"
        st = time()
        Lextend = self.Lextend[level]
        Levolve = self.Levolve[level]
        #Lghost = self.Lghost[level]
        G = self.Grids[level]
        for n in Levolve:
            n.diffpts = []
            gi = n.gridIndex
            for offsets in self.DiffStencil:
                gii = gi + offsets
                nn = G[tuple(gii)]
                #mm = Lextend.index(nn)
                # Previous works but is quadratic (this is significant
                # in practice).  Instead we find the indices just
                # after we build the stencils and cache them.
                mm = nn.IndexInLextend
                n.diffpts.append(mm)
        print "  time=" + str(time() - st)

        print "icpm preparartion: interp"
        st = time()
        for n in Lextend:
            n.interppts = []
            cpbaseptI = n.cpbaseptI
            Xgrid = G[tuple(cpbaseptI)].gridpt
            res = bdyfcn(n.whichBdy)
            if res == 'dirichlet_2nd_order':
                n.interpweights = -buildInterpWeights(Xgrid, n.cp, n.dx,
                                                      PAR_EXTSTENWIDTH)
            elif res == 'dirichlet_1st_order':
                # TODO: why bother with temp?  Just numpy.zeros?
                temp = buildInterpWeights(Xgrid, n.cp, n.dx, PAR_EXTSTENWIDTH)
                n.interpweights = [0] * len(temp)
            else:
                # includes "neumann_1st_order" and "neumann_2nd_order"
                n.interpweights = buildInterpWeights(Xgrid, n.cp, n.dx,
                                                     PAR_EXTSTENWIDTH)
            #gi = n.gridIndex
            for offsets in self.InterpStencil:
                # TODO: not right: find basept, UPDATE: this is
                # basept, should be correct now
                gii = cpbaseptI + offsets
                nn = G[tuple(gii)]
                #mm = Levolve.index(nn)
                # Previous works but is quadratic, see above in the
                # diff code.  Levolve and Lextend have the same
                # indices so we use index in extend here
                mm = nn.IndexInLextend
                n.interppts.append(mm)
        print "  time=" + str(time() - st)
예제 #2
0
    def findStencilsOnStencilSets(self, level, bdyfcn=bdyfcn_null):
        from time import time
        print "icpm preparartion: diff"
        st = time()
        Lextend = self.Lextend[level]
        Levolve = self.Levolve[level]
        #Lghost = self.Lghost[level]
        G = self.Grids[level]
        for n in Levolve:
            n.diffpts = []
            gi = n.gridIndex
            for offsets in self.DiffStencil:
                gii = gi + offsets
                nn = G[tuple(gii)]
                #mm = Lextend.index(nn)
                # Previous works but is quadratic (this is significant
                # in practice).  Instead we find the indices just
                # after we build the stencils and cache them.
                mm = nn.IndexInLextend
                n.diffpts.append(mm)
        print "  time=" + str(time()-st)

        print "icpm preparartion: interp"
        st = time()
        for n in Lextend:
            n.interppts = []
            cpbaseptI = n.cpbaseptI
            Xgrid = G[tuple(cpbaseptI)].gridpt
            res = bdyfcn(n.whichBdy)
            if res == 'dirichlet_2nd_order':
                n.interpweights = -buildInterpWeights(Xgrid, n.cp, n.dx, PAR_EXTSTENWIDTH)
            elif res == 'dirichlet_1st_order':
                # TODO: why bother with temp?  Just numpy.zeros?
                temp = buildInterpWeights(Xgrid, n.cp, n.dx, PAR_EXTSTENWIDTH)
                n.interpweights = [0]*len(temp)
            else:
                # includes "neumann_1st_order" and "neumann_2nd_order"
                n.interpweights = buildInterpWeights(Xgrid, n.cp, n.dx, PAR_EXTSTENWIDTH)
            #gi = n.gridIndex
            for offsets in self.InterpStencil:
                # TODO: not right: find basept, UPDATE: this is
                # basept, should be correct now
                gii = cpbaseptI + offsets
                nn = G[tuple(gii)]
                #mm = Levolve.index(nn)
                # Previous works but is quadratic, see above in the
                # diff code.  Levolve and Lextend have the same
                # indices so we use index in extend here
                mm = nn.IndexInLextend
                n.interppts.append(mm)
        print "  time=" + str(time()-st)
예제 #3
0
def buildExtensionMatrix(g, xy, degreep=3):
    r"""
    Generate the matrix E

    Notes:
    In the diff op case, the weights are fixed, here they depend on
    which node we're at.
    """
    from math import ceil,log10
    from scipy.sparse import coo_matrix
    from time import time
    from numpy import zeros, isscalar

    # TODO: dim hardcoded to 2 in some places in this function
    dim = xy.shape[1]
    dx = g.dx
    if (isscalar(dx)):
        mytype = type(dx)
    else:
        mytype = type(dx[0])

    relpt = a((g.x1d[0], g.y1d[0]))
    #stencilsize = len(Levolve[0].interppts)
    stencilsize = (degreep+1)**dim
    progout = 10 ** (ceil(log10(len(g.band)))-1)
    print "building E"
    st=time()

    # make empty lists the sparse matrix
    # TODO: is it faster to use numpy arrays here?
    ii = [];  jj = [];  aij = []

    # how many points to interpolate
    N = xy.shape[0]

    # TODO: order here must match code elsewhere: this is a very bad idea
    stencil = zeros( (stencilsize,dim) , dtype=int)
    c = 0
    for j in range(0,degreep+1):
        for i in range(0,degreep+1):
            stencil[c][0] = i
            stencil[c][1] = j
            c = c+1

    # can do all of them at once
    #bpij = findGridInterpBasePt(xy, dx, relpt, degreep)
    for c in range(0, N):
        if c % progout == 0:  print "  E row " + str(c)
        x = xy[c]
        bpij = findGridInterpBasePt(x, dx, relpt, degreep)
        bpx = relpt + bpij * dx
        interpweights = buildInterpWeights(bpx, x, dx, degreep+1)
        interppts = g.sub2ind(bpij + stencil)

        ii.extend([c]*stencilsize)
        jj.extend(interppts)
        aij.extend(interpweights)
    #TODO: does this work with float96?
    E = coo_matrix( (aij,(ii,jj)), shape=(len(g.band),g.nx*g.ny), dtype=mytype )

    # TODO: return this info as well:
    from numpy import unique
    band2 = unique(jj)
    print (len(jj),len(band2),len(g.band))
    print "  E row " + str(len(g.band))
    print "elapsed time = " + str(time() - st)
    #return E.tocsr()
    #E2 = E.tocsr()
    #EE = E2[:,band2]
    return (E.tocsr(), band2)