예제 #1
0
def host(commun):
    """Define the host processor"""

    # Initialise the grid
    grid = Grid()

    # Build a 5*5 grid
    i = 6
    n = 2**i + 1

    # Specify the boundary conditions and the rhs
    true_soln = sin_soln
    rhs = sin_rhs

    # Find the number of processors
    p = commun.get_no_workers()

    # Specify the boundary conditions and the rhs

    # Create a square FEM grid of size n*n
    build_square_grid_matrix(n, grid, true_soln)

    # Calculate and store the A matrix and rhs vector
    build_equation_linear_2D(grid, Poisson_tri_integrate, rhs)

    # Subdivide the grids
    sub_grids = subdivide_grid(p, grid)

    #    for cell in sub_grids:
    #        plot_fem_grid(sub_grids[cell][0])

    # Send each subgrid to the specified worker processor
    for i in range(p):
        commun.send_grid(i, sub_grids[i][0])
예제 #2
0
파일: Test2.py 프로젝트: Fonglulu/Multigrid
"""

# Import appropriate information from other classes
from grid.Grid import Grid
from BuildSquare import build_square_grid_matrix
#from FemPlot import plot_fem_solution
from grid.NodeTable import node_iterator
from grid.ConnectTable import connect_iterator
from grid.function.FunctionStore import exp_soln, exp_rhs, sin_soln, sin_rhs
from copy import copy
from scipy import eye, zeros, linalg
from numpy import inf

# Initialise the grid
grid = Grid()

# Build a 5*5 grid
i = 6
n = 2**i

# Specify the boundary conditions and the rhs
true_soln = exp_soln
rhs = exp_rhs

# Create a square FEM grid of size n*n
build_square_grid_matrix(n, grid, true_soln)

# Loop through the grid and find all of the node in the interior of the
# domain. At the same time make a record of which row in the matrix
# corresponds to which node
예제 #3
0
 def createGrid(self,rows,cols):
     self.grid = Grid(rows,cols)
예제 #4
0
 def defineVariables(self,infoPasser):
     self.queue = infoPasser
     self.grid = Grid(10,10)
예제 #5
0
def Ultimate_MG(cyclenumber):
    
    from TPSFEM import h1, h2, h3, h4, Amatrix, G1, G2, Lmatrix,  dvector
    
    # Set up the grid size, this includes boundary

    grid = Grid()
    
    i = 3

    n= 2**i+1
    
    # Find the spacing
    h=1/float(n-1)
    
    # Set the mesh grid, that is interior
    #x1, y1 = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h))
    x1, y1 = np.meshgrid(np.arange(0, 1+h, h), np.arange(0, 1+h, h))
    #print x1, y1
    # Set the data space
    datax = np.linspace(0, 1.0,20)
    datay = np.linspace(0, 1.0,20)
  
    dataX, dataY = np.meshgrid(datax,datay)
    

    # Set the exact solution of c, g1, g2, w on every node
    cexact = Linear
    
    g1exact =  Xlinear
    
    g2exact = Ylinear
    
    wexact = Zero
    #Initial = Sin2
    
    # Set the shape of objective function
    Z = cexact(dataX, dataY)
    
    
    data = Z.flatten()
    


    alpha = 0.000001
    
    L_list = []
    
    A_list = []
    
    G1_list = []
    
    G2_list = []
    
    xdim = []
    
    xdim.append(n)
    
    levelnumber  = 0
    
    
    
    
    while ( (xdim[levelnumber]-1) % 2 == 0 and xdim[levelnumber]-1 >2) :
    
    
            levelnumber = levelnumber+1
            
            xdim.append((xdim[levelnumber - 1] -1) //2 +1)
    
  
      
    for i in xdim :
        
        if i == n:
        
            # Initialise grid for calculating matrices and boundaries
            grid = Grid()
            
            # Build square 
            build_square_grid(i, grid, zero)
            
            
            
            # Store matrices on grid
            build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY,  dataX, dataY)
            
            #print 'Set-up'
    
            
            # Find the boundary for Ac + Lw = d - h1
            h1 = np.reshape(h1(grid,cexact, wexact), (n-2,n-2))
            #h1 = h1(grid,cexact, wexact)
            
            # Find the boundary for alphaLg1 -G1^Tw = -h2
            h2 = np.reshape(h2(grid, g1exact, wexact, alpha), (n-2,n-2))

            #h2 = h2(grid, g1exact, wexact, alpha)
            
            # Find the boundary for alphaLg2 -G2^Tw = -h3
            h3 = np.reshape(h3(grid, g2exact, wexact, alpha), (n-2,n-2))
            #h3 = h3(grid, g2exact, wexact, alpha)
            
            # Find the boundary for Lc -G1g1 -G2g2 = -h4
            h4 = np.reshape(h4(grid, cexact, g1exact, g2exact), (n-2,n-2))
            #h4 = h4(grid, cexact, g1exact, g2exact)
            
            # Find d vector
            #print dvector(grid, data)
            
            #print '1'
            
            dvector = np.reshape(dvector(grid, data), (n-2,n-2))
            
            #print 'd', dvector
            
           
            
            L_list.append(Lmatrix(grid))
            
            
            A_list.append(Amatrix(grid))
            
            
            G1_list.append(G1(grid))
    
            
            G2_list.append(G2(grid))
#            

        
        else :
            
            # Initialise grid for calculating matrices and boundaries
            grid = Grid()
            
            # Build square 
            build_square_grid(i, grid, zero)
            
            # Store matrices on grid
            build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX, TPS_tri_intergrateY,  dataX, dataY)
            

            
            L_list.append(Lmatrix(grid))
            
            #print '2'
            
            A_list.append(Amatrix(grid))
            
            
            G1_list.append(G1(grid))
    
            
            G2_list.append(G2(grid))
            
    #print len(A_list)
            

    

    
    # Set the initial guess for interior nodes values
    u=np.zeros((4,n,n))
    #u[0]= Initial(x1,y1)
    #print u, u[0], 'u0'
    
    
    
    # Set RHS at intilisation
    #rhs = np.zeros((4, n-2, n-2))
    rhs = np.zeros((4,n,n))
    rhs[0][1:-1,1:-1] = -h4
    rhs[1][1:-1,1:-1] = -h2
    rhs[2][1:-1,1:-1] = -h3
    rhs[3][1:-1,1:-1]= dvector-h1
    #print rhs[3][1:-1,1:-1]

    
    # Set the boundary (also the exact solution in this case)
#    
#    u[0,0,:] = cexact(x1, y1)[0]
#    
#    u[0, -1,:] = cexact(x1, y1)[-1]
#    
#    u[0, :, 0] = cexact(x1, y1)[:, 0]
#    
#    u[0,:,-1] = cexact(x1, y1)[:,-1]
#    
#    u[1,0,:] = g1exact(x1, y1)[0]
#    
#    u[1, -1,:] = g1exact(x1, y1)[-1]
#    
#    u[1, :, 0] = g1exact(x1, y1)[:,0]
#    
#    u[1,:,-1] = g1exact(x1, y1)[:,-1]
#    
#    u[2,0,:] = g2exact(x1, y1)[0]
#    
#    u[2, -1,:] = g2exact(x1, y1)[-1]
#    
#    u[2, :, 0] = g2exact(x1, y1)[:,0]
#    
#    u[2,:,-1] = g2exact(x1, y1)[:,-1]
#    
#    u[3,0,:] = wexact(x1, y1)[0]
#    
#    u[3, -1,:] = wexact(x1, y1)[-1]
#    
#    u[3, :, 0] = wexact(x1, y1)[:,0]
#    
#    u[3,:,-1] = wexact(x1, y1)[:,-1]
    
#    u2 =copy(u)
    # Set the number of relax
    s1=8
    s2=8
#    s3=6
#    s4=6
    
    
#    #Initialise a list to record l2 norm of resudual 
    rnorm=[np.linalg.norm(residue(rhs, u, alpha)[0, 1:-1,1:-1])*h] #A_list[0], G1_list[0], G2_list[0])[0,2:-2,2:-2]) * h]
##    
##    # Initialise a list to record l2 norm of error
#    ecnorm = [np.linalg.norm(u[0]-cexact(x1, y1))*h]
#    eg1norm = [np.linalg.norm(u[1]-g1exact(x1, y1))*h]
##    egg1norm = [np.linalg.norm(u2[1]-g1exact(x1, y1))*h]
##    e3norm = [np.linalg.norm(u[2]-g2exact(x1, y1))*h]
#    ewnorm = [np.linalg.norm(u[3]-wexact(x1, y1))*h]
#    
    
    
    # Start V-cycle
    for cycle in range(1, cyclenumber+1):
        
        #print rhs[0], 'rhs'
        u = VCycle(u,rhs, s1, s2, alpha,A_list, G1_list, G2_list, L_list)
 #       eg1norm.append(np.linalg.norm((u[1]-g1exact(x1,y1))[2:-2,2:-2])*h)
#        u2 = VCycle(u2,rhs, s3, s4, alpha, A_list, G1_list, G2_list, L_list)
    
        
        #print rhs[0], 'RHS'
        rnorm.append(np.linalg.norm(residue(rhs, u, alpha)[0,2:-2,2:-2])*h) #A_list[0], G1_list[0], G2_list[0])[0,2:-2,2:-2])*h)
#    
#        ecnorm.append(np.linalg.norm((u[0]-cexact(x1,y1))[2:-2,2:-2])*h) 
#         eg1norm.append(np.linalg.norm((u[1]-g1exact(x1,y1))[2:-2,2:-2])*h)
##        egg1norm.append(np.linalg.norm((u2[1]-g1exact(x1,y1))[2:-2,2:-2])*h)
#        
#        
#        ewnorm.append(np.linalg.norm(u[3]-wexact(x1,y1))*h)
        


        
     #Plot the semi-log for resiudal and erro

    xline = np.arange(cyclenumber+1)
    plt.figure(figsize=(4,5))
    plt.semilogy(xline, rnorm, 'bo-', xline, rnorm, 'k',label='sdad')
    #plt.semilogy(xline, egg1norm, 'bo', xline, egg1norm, 'k',label='sdad')
    title('Convergence with Error(Jacobi)')
    xlabel('Number of cycles')
    ylabel('Error under l2 norm')
    plt.show()
    #print u, rnorm

    return u
예제 #6
0
from BuildPackman import  build_packman_grid
from BuildEquation import build_equation_linear_2D, Poisson_tri_integrate
from grid.NodeTable import node_iterator
from grid.EdgeTable import  endpt_iterator
from grid.ConnectTable import connect_iterator
from grid.function.FunctionStore import exp_soln, exp_rhs, sin_soln, sin_rhs,zero
from scipy import eye, zeros, linalg
from numpy import inf
from operator import itemgetter
from scipy.sparse import csc_matrix,csr_matrix
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve


# Initialise the grid
grid = Grid()


# Build a 5*5 grid


# Specify the boundary contiditions and the rhs
#true_soln = zeros
#rhs = sin_rhs

true_soln =exp_soln
rhs = exp_rhs

# Create a square FEM grid od size n*n

N_length=8
예제 #7
0
from BuildSquare import build_square_grid_matrix
from grid.NodeTable import node_iterator
from grid.ConnectTable import connect_iterator
from grid.EdgeTable import  endpt_iterator
from grid.function.FunctionStore import exp_soln, exp_rhs, sin_soln, sin_rhs,zero
from scipy import eye, zeros, linalg
from numpy import inf
from operator import itemgetter
from scipy.sparse import csc_matrix,csr_matrix
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve



# Initialise the grid
grid = Grid()


# Build a 5*5 grid
i = 2
n= 2**i+1

# Specify the boundary contiditions and the rhs
#true_soln = zeros
#rhs = sin_rhs

true_soln =sin_soln
rhs = sin_rhs

# Create a square FEM grid od size n*n
build_square_grid_matrix(n, grid, true_soln)
예제 #8
0
data = Z2.flatten()
coordx = X.flatten()
coordy = Y.flatten()
Coord = zip(coordx, coordy)

#data = bone[:,2]
#coordx = bone[:,0]
#coordy = bone[:,1]
#Coord=zip(coordx, coordy)


##############################################################################
##############################################################################

grid = Grid()
#
## Build a 9*9 grid
i =3
n = 2**i+1

h =1/float(n-1)


#
true_soln = zero
#

# Boundares
Crhs = Exy
#
예제 #9
0
def JacobiDown(u, rhs, totalrhs, levelnumber, dataX, dataY, data, alpha):
    """ Block Jacobi Method. On each level of grid (same size as initial grid), invoke corresponding matrices
    
    A, L, G1, G2, d and boundaries h1, h2, h3, h4 
    
    """

    print u[0], 'jacobi'
    from TPSFEM import Amatrix, Lmatrix, G1, G2, dvector

    # Set entire FEM grid on the level
    grid = Grid()

    #print levelnumber
    # The size of FEM grid that should be in alignment with the size grid on the each level
    n = 2**(levelnumber + 1) + 1

    h = 1 / float(n - 1)

    # Set the mesh grid, that is interior
    #x1, y1 = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h))
    x1, y1 = np.meshgrid(np.arange(0, 1 + h, h), np.arange(0, 1 + h, h))

    # Calculating the spacing
    #h=1/float(n-1)

    #
    #x1, y1 = np.meshgrid(np.arange(0, 1+h, h), np.arange(0, 1+h, h))

    true_soln = zero

    # Build the square
    build_square_grid(n, grid, true_soln)

    # Store the matrices on grid
    build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX,
                        TPS_tri_intergrateY, dataX, dataY)

    # Import matrices on each level of grid
    Amatrix = Amatrix(grid)

    Lmatrix = Lmatrix(grid).todense()
    #print Lmatrix

    dvector = dvector(grid, data)

    G1 = G1(grid)

    G2 = G2(grid)

    # Get the current size of RHS function
    [xdim, ydim] = rhs[0][1:-1, 1:-1].shape

    c = np.reshape(u[0][1:-1, 1:-1], (xdim**2, 1))

    g1 = np.reshape(u[1][1:-1, 1:-1], (xdim**2, 1))

    g2 = np.reshape(u[2][1:-1, 1:-1], (xdim**2, 1))
    #print g2, 'g2'

    w = np.reshape(u[3][1:-1, 1:-1], (xdim**2, 1))
    #print c, 'CCCCCCC'

    ##############################################################################
    # Sparse solver

    #Solving LC = f1+ G1g1+ G2g2 to get new c-grid
    crhs = zeros([xdim + 2, ydim + 2])

    #print rhs[0].shape[0], G1.shape[0], g1.shape[0]

    crhs[1:-1,
         1:-1] = (rhs[0][1:-1, 1:-1] + np.reshape(G1 * g1 + G2 * g2,
                                                  (xdim, ydim)))

    totalrhs[0] = copy(crhs)

    u[0][1:-1, 1:-1] = np.reshape(
        spsolve(Lmatrix, np.reshape(crhs[1:-1, 1:-1], (xdim**2, 1))),
        (xdim, ydim))

    # Get new g1-grid

    g1rhs = zeros([xdim + 2, ydim + 2])

    g1rhs[1:-1,
          1:-1] = (rhs[1][1:-1, 1:-1] + np.reshape(G1.T * w, (xdim, ydim)))

    totalrhs[1] = copy(g1rhs) / float(alpha)

    u[1][1:-1, 1:-1] = np.reshape(
        spsolve(alpha * Lmatrix, np.reshape(g1rhs[1:-1, 1:-1], (xdim**2, 1))),
        (xdim, ydim))

    # Get new g2-grid

    g2rhs = zeros([xdim + 2, ydim + 2])

    g2rhs[1:-1,
          1:-1] = (rhs[2][1:-1, 1:-1] + np.reshape(G2.T * w, (xdim, ydim)))

    totalrhs[2] = copy(g2rhs) / float(alpha)

    u[2][1:-1, 1:-1] = np.reshape(
        spsolve(alpha * Lmatrix, np.reshape(g2rhs[1:-1, 1:-1], (xdim**2, 1))),
        (xdim, ydim))

    # Get new w-grid

    wrhs = zeros([xdim + 2, ydim + 2])

    wrhs[1:-1,
         1:-1] = (rhs[3][1:-1, 1:-1] - np.reshape(Amatrix * c, (xdim, ydim)))

    totalrhs[3] = copy(wrhs)
    u[3][1:-1, 1:-1] = np.reshape(
        spsolve(Lmatrix, np.reshape(wrhs[1:-1, 1:-1], (xdim**2, 1))),
        (xdim, ydim))

    ###############################################################################
    # Multigrid solver

    #
    #    # Initialise rhs for c
    #    crhs = zeros([xdim+2, ydim+2])
    #
    #    # Compute  interior crhs (f1 + G1*g1 + G2*g2) * h**2
    #    crhs[1:-1, 1:-1] = (rhs[0][1:-1,1:-1]+ np.reshape(G1 * g1+ G2 * g2, (xdim, ydim)))*float(h**2)
    #
    #    # Store new crhs
    #    totalrhs[0]=copy(crhs)/float(h**2)
    #
    #    # Solve for c
    #    u[0] = MGsolverUP(5, crhs, u[0], levelnumber)
    #
    #    # Intialise rhs for new g1
    #    g1rhs = zeros([xdim+2, ydim+2])
    #
    #    # Compute  interior g1rhs (f2 + G1.T * w)  * h**2 / alpha
    #    g1rhs[1:-1, 1:-1] = (rhs[1][1:-1,1:-1]+ np.reshape( G1.T *  w ,(xdim, ydim)))*float(h**2)/float(alpha)
    #
    #    # Store g1rhs
    #    totalrhs[1]=copy(g1rhs)/float(h**2)
    #
    #    # Solve for new g1
    #    u[1] = MGsolverUP(5, g1rhs, u[1], levelnumber)
    #
    #    # Initialise rhs for g2
    #    g2rhs = zeros([xdim+2, ydim+2])
    #
    #    # Compute interior g2rhs (f3 + G2.T * w)** h**2 / alpha
    #    g2rhs[1:-1,1:-1] = ((rhs[2][1:-1,1:-1]+ np.reshape( G2.T *  w ,(xdim, ydim))))*float(h**2)/float(alpha)
    #
    #    # Store g2rhs
    #    totalrhs[2]=copy(g2rhs)/float(h**2)
    #
    #    # Solve for new g2
    #    u[2] = MGsolverUP(5, g2rhs, u[2], levelnumber)
    #
    #    # Initialise rhs for w
    #    wrhs = zeros([xdim+2, ydim+2])
    #
    #    # Compute interior wrhs (f4 - Ac) * h**2
    #    wrhs[1:-1,1:-1] = (rhs[3][1:-1,1:-1]- np.reshape(Amatrix * c, (xdim, ydim)))*float(h**2)
    #
    #    # Store wrhs
    #    totalrhs[3]=copy(wrhs)/float(h**2)
    #
    #    # Solve for new w
    #    u[3] = MGsolverUP(5, wrhs, u[3], levelnumber)
    #
    #
    #    print totalrhs[0], 'TOTALRIGHT'
    #    print u[0], 'g1after'
    #
    return u
예제 #10
0
from grid.Grid import Grid
from BuildSquare import build_square_grid
#from FemPlot import plot_fem_solution
from grid.NodeTable import node_iterator
from grid.ConnectTable import connect_iterator   
from grid.function.FunctionStore import exp_soln, exp_rhs, sin_soln, sin_rhs
from copy import copy
from scipy import eye, zeros, linalg
import numpy as np
from numpy import inf

from BuildEquation import build_equation_linear_2D, Poisson_tri_integrate, build_matrix_fem_2D

    
# Initialise the grid
grid = Grid()

# Build a 5*5 grid
i = 2
n = 2**i

x = np.linspace(0,1.1,11)
y = np.linspace(0,1.1,11)

X, Y = np.meshgrid(x,y)
# Specify the boundary conditions and the rhs
true_soln = sin_soln
rhs = sin_rhs

# Create a square FEM grid of size n*n
build_square_grid(n, grid, true_soln)
예제 #11
0
def JacobiUp(u, rhs, totalrhs, levelnumber, dataX, dataY, data, alpha):
    """ Block Jacobi Method. On each level of grid (same size as initial grid), invoke corresponding matrices
    
    A, L, G1, G2, d and boundaries h1, h2, h3, h4 
    
    """

    print u[0], 'Upjacobi'
    from TPSFEM import Amatrix, Lmatrix, G1, G2, dvector

    # Set entire FEM grid on the level
    grid = Grid()

    #print levelnumber
    # The size of FEM grid that should be in alignment with the size grid on the each level
    n = 2**(levelnumber + 1) + 1

    h = 1 / float(n - 1)

    # Set the mesh grid, that is interior
    #x1, y1 = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h))
    x1, y1 = np.meshgrid(np.arange(0, 1 + h, h), np.arange(0, 1 + h, h))

    # Calculating the spacing
    #h=1/float(n-1)

    #
    #x1, y1 = np.meshgrid(np.arange(0, 1+h, h), np.arange(0, 1+h, h))

    true_soln = zero

    # Build the square
    build_square_grid(n, grid, true_soln)

    # Store the matrices on grid
    build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX,
                        TPS_tri_intergrateY, dataX, dataY)

    # Import matrices on each level of grid
    Amatrix = Amatrix(grid)
    Lmatrix = Lmatrix(grid).todense()
    #print Amatrix

    dvector = dvector(grid, data)
    #print dvector

    #Lmatrix = Lmatrix(grid)
    #print Lmatrix.todense()

    G1 = G1(grid)

    G2 = G2(grid)

    # Get the current size of RHS function
    [xdim, ydim] = rhs[0][1:-1, 1:-1].shape
    c = np.reshape(u[0][1:-1, 1:-1], (xdim**2, 1))
    #print c, 'c'

    #print v[1]
    g1 = np.reshape(u[1][1:-1, 1:-1], (xdim**2, 1))
    #print g1
    #print np.size(g1, 0), 'g1'

    g2 = np.reshape(u[2][1:-1, 1:-1], (xdim**2, 1))
    #print g2, 'g2'

    w = np.reshape(u[3][1:-1, 1:-1], (xdim**2, 1))
    #print w, 'w'

    # Solving LC = f1+ G1g1+ G2g2 to get new c-grid
    #crhs = zeros([xdim+2, ydim+2])
    crhs = 300 * np.ones((xdim + 2, ydim + 2))
    #print crhs.shape[0]
    #rhs[0][1:-1,1:-1] = rhs[0][1:-1,1:-1]+ np.reshape(G1 * g1+ G2 * g2, (xdim, ydim))

    crhs[1:-1,
         1:-1] = (rhs[0][1:-1, 1:-1] + np.reshape(G1 * g1 + G2 * g2,
                                                  (xdim, ydim))) * float(h**2)
    #print totalrhs[0].shape[0], 'TOTALDIM'
    totalrhs[0] = copy(crhs)
    #crhs = np.reshape(rhs[0][1:-1,1:-1], (xdim**2,1))+ G1 * g1+ G2 * g2
    #print crhs, 'crhs'

    # Reshape approximation of c so that corresponds to nodes

    # Applying single multigrid solver
    #print crhs, 'CRHSSSSS'
    u[0] = MGsolverUP(5, crhs, u[0], levelnumber)
    #u[0][1:-1,1:-1] = np.reshape(spsolve(Lmatrix, crhs), (xdim, ydim))

    # Get new g1-grid
    # g1rhs = (np.reshape(rhs[1][1:-1,1:-1], (xdim**2,1)) + (G1.T)* w)/float(alpha)
    #g1rhs = zeros([xdim+2, ydim+2])
    g1rhs = 300 * np.ones((xdim + 2, ydim + 2))
    g1rhs[1:-1,
          1:-1] = ((rhs[1][1:-1, 1:-1] + np.reshape(G1.T * w, (xdim, ydim))) *
                   float(h**2)) / float(alpha)
    #rhs[1][1:-1,1:-1] = (rhs[1][1:-1,1:-1]+ np.reshape( G1.T *  w ,(xdim, ydim)))/float(alpha)
    totalrhs[1] = copy(g1rhs)
    u[1] = MGsolverUP(5, g1rhs, u[1], levelnumber)
    #print u[1], 'g1after'
    #u[1][1:-1,1:-1] = np.reshape(spsolve(Lmatrix, g1rhs), (xdim, ydim))
    #print u[1], 'u[1]'

    #print rhs[1], 'rhs[1]'

    # Get new g2-grid
    #g2rhs = (np.reshape(rhs[2][1:-1,1:-1], (xdim**2,1))+ (G2.T) * w)/float(alpha)
    #g2rhs = zeros([xdim+2, ydim+2])
    g2rhs = 300 * np.ones((xdim + 2, ydim + 2))
    #g2rhs[1:-1,1:-1] = ((rhs[2][1:-1,1:-1]+ np.reshape( G2.T *  w ,(xdim, ydim)))*float(h**2))/float(alpha)
    g2rhs[1:-1, 1:-1] = (rhs[2][1:-1, 1:-1] +
                         np.reshape(G2.T * w, (xdim, ydim))) / float(alpha)
    #rhs[2][1:-1,1:-1] = (rhs[2][1:-1,1:-1]+ np.reshape( G1.T *  w ,(xdim, ydim)))/float(alpha)
    #u[2][1:-1,1:-1] = np.reshape(spsolve(Lmatrix, g2rhs), (xdim, ydim))
    totalrhs[2] = copy(g2rhs)
    u[2] = MGsolverUP(5, g2rhs, u[2], levelnumber)

    # Get new w-grid
    #    wrhs = dvector - h1 - np.matmul(Amatrix, c)
    #    wrhs = np.reshape(wrhs, (xdim,ydim))
    #print rhs[3][1:-1,1:-1]
    #wrhs = zeros([xdim+2, ydim+2])
    wrhs = 300 * np.ones((xdim + 2, ydim + 2))
    wrhs[1:-1,
         1:-1] = (rhs[3][1:-1, 1:-1] - np.reshape(Amatrix * c,
                                                  (xdim, ydim)))  #*float(h**2)
    #rhs[3][1:-1,1:-1] = rhs[3][1:-1,1:-1]- np.reshape(Amatrix * c, (xdim, ydim))
    #wrhs = np.reshape(rhs[3][1:-1,1:-1], (xdim**2,1))- Amatrix * c
    totalrhs[3] = copy(wrhs)

    u[3] = MGsolverUP(5, wrhs, u[3], levelnumber)
    #u[3][1:-1,1:-1] = np.reshape(spsolve(Lmatrix, np.reshape(wrhs, (xdim**2,1)), (xdim, ydim)))

    #print u[0], 'g1after'

    return u
예제 #12
0
def Setup_Grid(i):
    #from quick_d import dvector

    global grid, Coord, Nl, h, Crhs, g1rhs, g2rhs, wrhs, data, intx, inty, nodes, quick_d

    x = np.linspace(0, 1.0, 50)
    y = np.linspace(0, 1.0, 50)
    X, Y = np.meshgrid(x, y)

    Z2 = Exy(X, Y)

    data = Z2.flatten()
    coordx = X.flatten()
    coordy = Y.flatten()
    Coord = zip(coordx, coordy)

    grid = Grid()

    n = 2**i + 1

    h = 1 / float(n - 1)

    intx, inty = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h))

    x1, y1 = np.meshgrid(np.arange(0, 1 + h, h), np.arange(0, 1 + h, h))
    nodes = np.vstack([x1.ravel(), y1.ravel()]).T

    true_soln = zero

    # Boundares
    Crhs = Exy

    g1rhs = Xexy

    g2rhs = Yexy

    wrhs = XYexy

    # Build node-eged structure that allows the connection
    build_square_grid(n, grid, true_soln)

    # Store the matrices values on grid nodes
    build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX,
                        TPS_tri_intergrateY, X, Y)

    # Set value for only interior node.

    Nl = []

    for node in (not_slave_node(grid)):

        Nl.append([node, node.get_node_id()])

    #print 'Start sort'
    #Sort by node id.
    Nl = sorted(Nl, key=itemgetter(1))
    #print 'Finish sort'

    #print 'START MAKE_LIST'

    Nl = [node[0] for node in Nl]

    #print 'END MAKE_LIST'

    #print 'START SETVALUE'

    for node in Nl:
        node.set_value(Nl.index(node))
예제 #13
0
파일: MgFEM.py 프로젝트: Fonglulu/Multigrid
def Ultimate_MG(cyclenumber):

    from TPSFEM import Amatrix, h1, h2, h3, h4, Lmatrix, G1, G2, dvector

    # Set up the grid size, this includes boundary

    grid = Grid()

    i = 2

    n = 2**i + 1

    # Find the spacing
    h = 1 / float(n - 1)

    # Set the mesh grid, that is interior
    #x1, y1 = np.meshgrid(np.arange(h, 1, h), np.arange(h, 1, h))
    x1, y1 = np.meshgrid(np.arange(0, 1 + h, h), np.arange(0, 1 + h, h))
    #print x1, y1
    # Set the data space
    datax = np.linspace(0, 1.0, 30)
    datay = np.linspace(0, 1.0, 30)

    dataX, dataY = np.meshgrid(datax, datay)

    # Set the exact solution of c, g1, g2, w on every node
    cexact = L3

    g1exact = x_3

    g2exact = y_3

    wexact = XYl_3

    # Set the shape of objective function
    Z = cexact(dataX, dataY)

    data = Z.flatten()

    # Set penalty term
    alpha = 0.01

    # Initialise grid for calculating matrices and boundaries
    grid = Grid()

    # Build square
    build_square_grid(n, grid, zero)

    # Store matrices on grid
    build_matrix_fem_2D(grid, Poisson_tri_integrate, TPS_tri_intergrateX,
                        TPS_tri_intergrateY, dataX, dataY)

    #    Nl=[]
    #    for node in (not_slave_node(grid)):
    #
    #        Nl.append([node,node.get_node_id()])
    #
    #    Nl=sorted(Nl, key = itemgetter(1))
    #
    #    Nl=[node[0] for node in Nl]

    #    for node in Nl:
    #        node.set_value(Nl.index(node))

    # Find the boundary for Ac + Lw = d - h1
    h1 = np.reshape(h1(grid, cexact, wexact), (n - 2, n - 2))
    #h1 = h1(grid,cexact, wexact)

    # Find the boundary for alphaLg1 -G1^Tw = -h2
    h2 = np.reshape(h2(grid, g1exact, wexact, alpha), (n - 2, n - 2))
    #h2 = h2(grid, g1exact, wexact, alpha)

    # Find the boundary for alphaLg2 -G2^Tw = -h3
    h3 = np.reshape(h3(grid, g2exact, wexact, alpha), (n - 2, n - 2))
    #h3 = h3(grid, g2exact, wexact, alpha)

    # Find the boundary for Lc -G1g1 -G2g2 = -h4
    h4 = np.reshape(h4(grid, cexact, g1exact, g2exact), (n - 2, n - 2))
    #h4 = h4(grid, cexact, g1exact, g2exact)

    # Find d vector
    #print dvector(grid, data)

    dvector = np.reshape(dvector(grid, data), (n - 2, n - 2))

    #dd = dvectorgrid, data

    #print dd

    # Set the initial guess for interior nodes values
    #u=np.zeros((4,n-2, n-2))
    u = 200 * np.ones((4, n, n))

    # Set RHS at intilisation
    #rhs = np.zeros((4, n-2, n-2))
    rhs = np.zeros((4, n, n))
    rhs[0][1:-1, 1:-1] = -h4
    #print rhs[0][1:-1,1:-1]
    rhs[1][1:-1, 1:-1] = -h2
    #print rhs[1], 'rhs[1]'
    rhs[2][1:-1, 1:-1] = -h3
    rhs[3][1:-1, 1:-1] = dvector - h1
    #print rhs[3][1:-1,1:-1]

    total = np.zeros((4, n, n))

    # Set the boundary (also the exact solution in this case)

    u[0, 0, :] = cexact(x1, y1)[0]

    u[0, -1, :] = cexact(x1, y1)[-1]

    u[0, :, 0] = cexact(x1, y1)[:, 0]

    u[0, :, -1] = cexact(x1, y1)[:, -1]

    u[1, 0, :] = g1exact(x1, y1)[0]

    u[1, -1, :] = g1exact(x1, y1)[-1]

    u[1, :, 0] = g1exact(x1, y1)[:, 0]

    u[1, :, -1] = g1exact(x1, y1)[:, -1]

    u[2, 0, :] = g2exact(x1, y1)[0]

    u[2, -1, :] = g2exact(x1, y1)[-1]

    u[2, :, 0] = g2exact(x1, y1)[:, 0]

    u[2, :, -1] = g2exact(x1, y1)[:, -1]

    u[3, 0, :] = wexact(x1, y1)[0]

    u[3, -1, :] = wexact(x1, y1)[-1]

    u[3, :, 0] = wexact(x1, y1)[:, 0]

    u[3, :, -1] = wexact(x1, y1)[:, -1]

    # Set the number of relax
    s1 = 2
    s2 = 2
    s3 = 2

    #Initialise a list to record l2 norm of resudual
    rnorm = [np.linalg.norm(residue(rhs, u, alpha)) * h]

    # Initialise a list to record l2 norm of error
    ecnorm = [np.linalg.norm(u[0] - cexact(x1, y1)) * h]
    #    e2norm = [np.linalg.norm(u[1]-g1exact(x1, y1))*h]
    #    e3norm = [np.linalg.norm(u[2]-g2exact(x1, y1))*h]
    #    e4norm = [np.linalg.norm(u[3]-cexact(x1, y1))*h]

    # Start V-cycle
    for cycle in range(1, cyclenumber + 1):

        u = VCycle(total, rhs, u, n, s1, s2, s3, dataX, dataY, data, alpha)

        rnorm.append(np.linalg.norm(residue(rhs, u, n)) * h)

        ecnorm.append(np.linalg.norm(u - cexact(x1, y1)) * h)

    print rnorm, 'rnorm'
    print ecnorm, 'ecnorm'
    return u