Ejemplo n.º 1
0
 def testNorm1(self):
     '''test Norm1.'''
     example = ( ( a( [1,1,1] ), 1 ) ,
                 ( a( [[1,3,4],[2,-2,1]] ), a( [4,2] )) 
                )
     for x,y in example:
         rslt = Band.norm1(x)
         npt.assert_allclose(rslt, y)
Ejemplo n.º 2
0
    
    comm = MPI.COMM_WORLD
    
    
    vsize = points.shape[0]
    vAssigned = vsize // comm.size + int(comm.rank < (vsize % comm.size))


    vstart = comm.exscan(vAssigned)
    if comm.rank == 0:
        vstart = 0
    for MBlock in MBlocklist:
        opt = {'M':MBlock,'m':5,'d':2}
        surface = Sphere(center=sp.array([0.0, 0.0]))
        
        band = Band(surface,comm,opt)
        la,lv,gv,wv = band.createGLVectors()
        v = band.getCoordinates() 
        dt = 0.1*band.dx**2
        vv = sp.array([[0,0],[1,0],[-1,0],[0,1],[0,-1]])
        weights = sp.array([-4,1,1,1,1])*(dt/band.dx**2)
        L = band.createAnyMat(vv, weights, (5,2))
        PETSc.Sys.Print('Laplacian')
    
        
        M = band.createExtensionMat()
        PETSc.Sys.Print('ExtensionMat built')
    
        band.initialu(initialu)
        PETSc.Sys.Print('Initial')
        nextt = 0.1
Ejemplo n.º 3
0
    return sp.ones(cp.shape[0])#cp[:,0]
def initialu(cp):
    return cp[:,0]

def plot3d(x,c,title = 'band'):
#    z = c-c.min()
#    z /= z.max()
    pl.figure()
    pl.points3d(x[:,0],x[:,1],x[:,2],c,mode='point')

if __name__ == '__main__':
    opt = {'M':50,'m':10,'d':3}
    surface = MeshWrapper()
    pl.triangular_mesh(surface.v[:,0],surface.v[:,1],surface.v[:,2],surface.f,opacity = 0.2)
    comm = MPI.COMM_WORLD
    band = Band(surface,comm,opt)
    la,lv,gv,wv = band.createGLVectors()
    v = band.getCoordinates() 
    dt = 0.3*band.dx**2
    vv = sp.array([[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]])
    weights = sp.array([-6,1,1,1,1,1,1])*(dt/band.dx**2)
    L = band.createAnyMat(vv, weights, (7,3))
    PETSc.Sys.Print('Laplacian')
    band.test_initialu(test_initialu)
    L.mult(gv,wv)
    c = wv.getArray()
#    wv.view()
    plot3d(v,c,title='Lu')
#    wv.view()
    
    M = band.createExtensionMat()
Ejemplo n.º 4
0
    points = sp.column_stack((sp.cos(l), sp.sin(l)))
    exactu = sp.cos(l)

    comm = MPI.COMM_WORLD

    vsize = points.shape[0]
    vAssigned = vsize // comm.size + int(comm.rank < (vsize % comm.size))

    vstart = comm.exscan(vAssigned)
    if comm.rank == 0:
        vstart = 0
    for MBlock in MBlocklist:
        opt = {'M': MBlock, 'm': 5, 'd': 2}
        surface = Sphere(center=sp.array([0.0, 0.0]))

        band = Band(surface, comm, opt)
        la, lv, gv, wv = band.createGLVectors()
        v = band.getCoordinates()
        dt = 0.1 * band.dx**2
        vv = sp.array([[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]])
        weights = sp.array([-4, 1, 1, 1, 1]) * (dt / band.dx**2)
        L = band.createAnyMat(vv, weights, (5, 2))
        PETSc.Sys.Print('Laplacian')

        M = band.createExtensionMat()
        PETSc.Sys.Print('ExtensionMat built')

        band.initialu(initialu)
        PETSc.Sys.Print('Initial')
        nextt = 0.1
        PETSc.Sys.Print('Begin to solve')
Ejemplo n.º 5
0
def outputBin(gv):
    cv = band.toZeroStatic(gv)
    if comm.rank == 0:
        viewer = PETSc.Viewer().createBinary('gv.dat',mode='w',comm = MPI.COMM_SELF)
        cv.view(viewer)

if __name__ == '__main__':
    opt = {'M':100,'m':5,'d':3}
    try:
        surface = MeshWrapper('eight.ply')
        #surface = MeshWrapper('eight_refined.ply')
    except IOError:
        surface = MeshWrapper('cp/tests/data/eight.ply')
        #surface = MeshWrapper('cp/tests/data/eight_refined.ply')
    comm = MPI.COMM_WORLD
    band = Band(surface,comm,opt)
    la,lv,gv,wv = band.createGLVectors()
    v = band.getCoordinates()
    centers = band.BlockInd2CenterCarWithoutBand(band.gindBlockWBand.getArray())
    dt = 0.1*band.dx**2
    vv = sp.array([[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]])
    weights = sp.array([-6,1,1,1,1,1,1])*(dt/band.dx**2)
    L = band.createAnyMat(vv, weights, (7,3))
    PETSc.Sys.Print('Laplacian')

    M = band.createExtensionMatForLoop()
    band.initialu(initialu)
    PETSc.Sys.Print('Initial')
    plot3d_total(v,gv)
    nextt = 0.01
    PETSc.Sys.Print('Begin to solve.\n dt is {0}'.format(dt))
Ejemplo n.º 6
0
 def setUp(self):
     self.comm = MPI.COMM_WORLD
     surface = Surface()
     self.bnd = Band(surface,self.comm)
     self.bnd.createGLVectors()
Ejemplo n.º 7
0
class TestBand(unittest.TestCase):


    def setUp(self):
        self.comm = MPI.COMM_WORLD
        surface = Surface()
        self.bnd = Band(surface,self.comm)
        self.bnd.createGLVectors()

    def tearDown(self):
        pass


    def testBlockInd2SubWithoutBand(self):
        '''test BlockInd2SubWithoutBand'''
        if 1 == 1:return 
        example = ( (5, a([5,0,0]) ),
                    (21,a([1,1,0]) ),
                    (a([401,402]),a([[1,0,1],[2,0,1]])) )
        for ind,sub in example:
            result = self.bnd.BlockInd2SubWithoutBand(ind)
            npt.assert_array_equal(result,sub,'{0} is wrong '.format(ind))
        ExceptionExample = (10000,8000)
        for i in ExceptionExample:
            self.assertRaises(exceptions.IndexError, self.bnd.BlockInd2SubWithoutBand,i)
            
    def testBlockSub2CenterCarWithoutBand(self):
        '''test BlockSub2CenterCarWithoutBand'''
        if 1 == 1: return
        example = ( ( 0, a([-1.9,-1.9,-1.9])), 
                    ( 1, a([-1.7,-1.9,-1.9])) )
        for ind,right in example:
            sub = self.bnd.BlockInd2SubWithoutBand(ind)
            rslt = self.bnd.BlockSub2CenterCarWithoutBand(sub)
            npt.assert_array_equal(rslt, right)

   
    @attr(visual=1)
    def testVisually(self):
        '''blocks selected visually.'''
#        if self.comm.rank == 0:
        g2z,zvec = PETSc.Scatter().toZero(self.bnd.gindBlockWBand)
        g2z.scatter(self.bnd.gindBlockWBand,zvec, PETSc.InsertMode.INSERT)
        x = self.bnd.BlockSub2CenterCarWithoutBand(\
                                                   self.bnd.BlockInd2SubWithoutBand(zvec.getArray()) )
        lx = self.bnd.BlockSub2CenterCarWithoutBand(\
                                                    self.bnd.BlockInd2SubWithoutBand(self.bnd.gindBlockWBand.getArray()))
        try:
            try:
                from mayavi import mlab
            except ImportError:
                from enthought.mayavi import mlab

            if self.comm.rank == 0:
                mlab.figure()
                mlab.points3d(x[:,0],x[:,1],x[:,2])
            mlab.figure()
            mlab.points3d(lx[:,0],lx[:,1],lx[:,2])
            mlab.show()
            #fig.add(pts1)
            #fig.add(pts2)
        except ImportError:
            import pylab as pl
            from mpl_toolkits.mplot3d import Axes3D #@UnusedImport
            fig = pl.figure()
            ax = fig.add_subplot(111, projection='3d')
            ax.scatter3D(x[:,0],x[:,1],x[:,2],c='blue',marker='o')
            ax.scatter3D(lx[:,0],lx[:,1],lx[:,2],c='red',marker='D')
            pl.savefig('testVis{0}.png'.format(self.comm.rank))
            pl.show()
        
    def testNorm1(self):
        '''test Norm1.'''
        example = ( ( a( [1,1,1] ), 1 ) ,
                    ( a( [[1,3,4],[2,-2,1]] ), a( [4,2] )) 
                   )
        for x,y in example:
            rslt = Band.norm1(x)
            npt.assert_allclose(rslt, y)
            
            
    def testgetCoordinatesWithGhost(self):
        '''What the return values of this function look like.'''
        #This test takes time.
        if 1 == 1 :return
        x = self.bnd.getCoordinatesWithGhost()
        try:
            from mayavi import mlab
            mlab.figure()
            mlab.points3d(x[:,0],x[:,1],x[:,2])
            #fig.add(pts)
            mlab.show()
            
        except ImportError:
            import pylab as pl
            from mpl_toolkits.mplot3d import Axes3D #@UnusedImport
            fig = pl.figure()
            ax = fig.add_subplot(111, projection='3d')
            ax.scatter3D(x[:,0],x[:,1],x[:,2])
            pl.savefig('x{0}.png'.format(self.comm.rank))
            pl.show()
            
    def testgetCoordinates(self):
        '''What the return values of this function look like.'''
        #This test takes time.
        if 1 == 1 :return
        x = self.bnd.getCoordinates()
        try:
            from mayavi import mlab
            mlab.figure()
            mlab.points3d(x[:,0],x[:,1],x[:,2])
            #fig.add(pts)
            mlab.show()
            
        except ImportError:
            import pylab as pl
            from mpl_toolkits.mplot3d import Axes3D #@UnusedImport
            fig = pl.figure()
            ax = fig.add_subplot(111, projection='3d')
            ax.scatter3D(x[:,0],x[:,1],x[:,2])
            pl.savefig('x{0}.png'.format(self.comm.rank))
            pl.show()
        
    def testBuildExtMat(self):
        self.bnd.createExtensionMat()
        
    def testCreateAnyMat(self):
        v = a([[0,0,0],[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]])
        weights = a([-6,1,1,1,1,1,1])
        self.bnd.createAnyMat(v, weights, (7,3))
Ejemplo n.º 8
0
    return cp[:, 0]


def plot3d(x, c, title="band"):
    #    z = c-c.min()
    #    z /= z.max()
    pl.figure()
    pl.points3d(x[:, 0], x[:, 1], x[:, 2], c, mode="point")


if __name__ == "__main__":
    opt = {"M": 50, "m": 10, "d": 3}
    surface = MeshWrapper()
    pl.triangular_mesh(surface.v[:, 0], surface.v[:, 1], surface.v[:, 2], surface.f, opacity=0.2)
    comm = MPI.COMM_WORLD
    band = Band(surface, comm, opt)
    la, lv, gv, wv = band.createGLVectors()
    v = band.getCoordinates()
    dt = 0.3 * band.dx ** 2
    vv = sp.array([[0, 0, 0], [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0], [0, 0, 1], [0, 0, -1]])
    weights = sp.array([-6, 1, 1, 1, 1, 1, 1]) * (dt / band.dx ** 2)
    L = band.createAnyMat(vv, weights, (7, 3))
    PETSc.Sys.Print("Laplacian")
    band.test_initialu(test_initialu)
    L.mult(gv, wv)
    c = wv.getArray()
    #    wv.view()
    plot3d(v, c, title="Lu")
    #    wv.view()

    M = band.createExtensionMat()
Ejemplo n.º 9
0
        pl.figure(bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5))
        pl.triangular_mesh(x[:, 0], x[:, 1], x[:, 2], f, scalars=v2)


if __name__ == '__main__':
    opt = {'M': 80, 'm': 5, 'd': 3}
    surface = MeshWrapper('eight.ply')
    pl.figure(bgcolor=(1, 1, 1), fgcolor=(0.5, 0.5, 0.5))
    pl.triangular_mesh(surface.v[:, 0],
                       surface.v[:, 1],
                       surface.v[:, 2],
                       surface.f,
                       scalars=surface.v[:, 0],
                       opacity=0.5)
    comm = MPI.COMM_WORLD
    band = Band(surface, comm, opt)
    la, lv, gv, wv = band.createGLVectors()
    v = band.getCoordinates()
    centers = band.BlockInd2CenterCarWithoutBand(
        band.gindBlockWBand.getArray())
    pl.points3d(centers[:, 0], centers[:, 1], centers[:, 2], mode='point')
    dt = 0.1 * band.dx**2
    vv = sp.array([[0, 0, 0], [1, 0, 0], [-1, 0, 0], [0, 1, 0], [0, -1, 0],
                   [0, 0, 1], [0, 0, -1]])
    weights = sp.array([-6, 1, 1, 1, 1, 1, 1]) * (dt / band.dx**2)
    L = band.createAnyMat(vv, weights, (7, 3))
    PETSc.Sys.Print('Laplacian')

    M = band.createExtensionMatForLoop()

    band.initialu(initialu)