Exemplo n.º 1
0
def main():
    # # get the discrete laplacian of size N-2
    # L = disc_lapl(N-2,h)
    # # flatten the matrices into vectors and set R = f - Lv
    # R = f.flatten() - L.dot(v.flatten())
    # # reshape R into a matrix
    # r = (R).reshape((int(2**power - 1),int(2**power - 1)))

    ARGS = PARSE_ARGS()
    norm = which_norm(ARGS.norm)
    condition = which_condition(ARGS.convergence)

    # initialize a guess
    u = np.zeros((2**ARGS.power - 1, 2**ARGS.power - 1))
    X, Y, N, h = get_mesh(ARGS.power)
    # f = RHS(X,Y)

    Ls = {}
    for i in xrange(1, 10):
        Ls[str(i)] = get_laplacian(2**i - 1)

    # Set the algebraic solution
    ALG_SOL = np.random.rand(2**ARGS.power - 1, 2**ARGS.power - 1)
    # get the discrete laplacian of size N-2
    L = get_laplacian(N - 2)
    # flatten the matrices into vectors and set R = f - Lv
    F = Ls[str(int(log(N - 1, 2)))].dot(ALG_SOL.flatten())
    # reshape R into a matrix
    f = (F).reshape((int(2**ARGS.power - 1), int(2**ARGS.power - 1)))

    t = tqdm(xrange(ARGS.max_iterations))
    for i in t:
        u_old = copy.deepcopy(u)
        u = V_cycle(ARGS.power, u_old, f, (1, 1), X, Y, N, h, Ls)
        E = u - u_old
        normE = norm(E)
        #     break
        if condition(normE, norm(u_old), ARGS.tolerance):
            break
        t.set_description("||E||=%.10f" % normE)

    print "\n\niterations = %d" % i

    pls_plot(X, Y, u)
Exemplo n.º 2
0
def main():
    # Parse command line arguments
    ARGS = PARSE_ARGS()
    print "power: ", ARGS.power
    print "tolerance: ", ARGS.tol
    print "precondition: ", ARGS.shampoo

    # define the mesh X,Y and spacing h
    X,Y,N,h = get_mesh(ARGS.power)

    Ss = {}
    fs = {}
    As = {}
    for i in xrange(1,ARGS.power+1):
        As[str(i)] = get_laplacian(2**i - 1)
    A = As[str(ARGS.power)]
    for i in xrange(1,ARGS.power+1):
        (A_spec, f_spec) = mmrc(int(2**i - 1))
        Ss[str(i)] = (1/(h**2))*A_spec
        fs[str(i)] = f_spec.reshape(int(2**i - 1),int(2**i - 1))
    S = Ss[str(ARGS.power)]
    f = fs[str(ARGS.power)]

    normf = np.amax(np.abs(f))

    # initialize u
    u = np.zeros((N-2,N-2))

    # if ARGS.SOR_TOO:
    #     t = tqdm(xrange(ARGS.max_iterations))
    #     for SOR_ITS in t:
    #         u_old = deepcopy(u)
    #         u = SOR(u_old, f, N, h)
    #         r = f - A.dot(u.flatten()).reshape(N-2,N-2)
    #         normr = np.amax(np.abs(r))
    #         if normr <= ARGS.tol*normf:
    #             SOR_ITS += 1
    #             break
    #         t.set_description("||res||=%.10f" % normr)
    #     print "SOR_ITS: ", SOR_ITS

    # initialize r by calculating the residual f - Au
    r = f - S.dot(u.flatten()).reshape(N-2,N-2)
    # solve Mz = r
    if ARGS.shampoo == "MG":
        z = V_cycle(ARGS.power,u,r,(1,1),X,Y,N,h,As)
    elif ARGS.shampoo == "SSOR":
        z = SSOR(r, N, h)
    elif ARGS.shampoo == "none":
        z = deepcopy(r)

    # initialize p
    p = deepcopy(z)
    # Do PCG
    U,its = PCG(u,p,z,r,S,ARGS,X,Y,N,h,Ss,normf,As)
    print ARGS.shampoo, "ITS: ", its
Exemplo n.º 3
0
def main():
    # Parse command line arguments
    ARGS = PARSE_ARGS()
    print "power: ", ARGS.power
    print "tolerance: ", ARGS.tol
    print "precondition: ", ARGS.shampoo

    # define the mesh X,Y and spacing h
    X,Y,N,h = get_mesh(ARGS.power)

    Ls = {}
    for i in xrange(1,ARGS.power+1):
        Ls[str(i)] = get_laplacian(2**i - 1)
    A = Ls[str(ARGS.power)]

    # # Define a known solution
    # np.random.seed(1)
    # u_sol = np.random.rand(N-2,N-2)
    # # define the right hand side f so that Au = f has solution u_sol
    # f = A.dot(u_sol.flatten()).reshape(N-2,N-2)

    f = RHS(X,Y)
    normf = np.amax(np.abs(f))

    # initialize u
    u = np.zeros((N-2,N-2))

    if ARGS.SOR_TOO:
        t = tqdm(xrange(ARGS.max_iterations))
        for SOR_ITS in t:
            u_old = deepcopy(u)
            u = SOR(u_old, f, N, h)
            r = f - A.dot(u.flatten()).reshape(N-2,N-2)
            normr = np.amax(np.abs(r))
            if normr <= ARGS.tol*normf:
                SOR_ITS += 1
                break
            t.set_description("||res||=%.10f" % normr)
        print "SOR_ITS: ", SOR_ITS

    # initialize r by calculating the residual f - Au
    r = f - A.dot(u.flatten()).reshape(N-2,N-2)
    # solve Mz = r
    if ARGS.shampoo == "MG":
        z = V_cycle(ARGS.power,u,r,(1,1),X,Y,N,h,Ls)
    elif ARGS.shampoo == "SSOR":
        z = SSOR(r, N, h)
    elif ARGS.shampoo == "none":
        z = deepcopy(r)

    # initialize p
    p = deepcopy(z)
    # Do PCG
    U,its = PCG(u,p,z,r,A,ARGS,X,Y,N,h,Ls,normf)
    print ARGS.shampoo, "ITS: ", its
Exemplo n.º 4
0
def main():
    ARGS = PARSE_ARGS()
    norm = which_norm(ARGS.norm)
    condition = which_condition(ARGS.convergence)

    nus = []
    for i in xrange(1, 5):
        for j in xrange(i + 1):
            nus.append((j, i - j))

    Ls = {}
    for i in xrange(1, 10):
        Ls[str(i)] = get_laplacian(2**i - 1)

    # Initialize u and get the right hand side of the equation
    u = np.zeros((2**ARGS.power - 1, 2**ARGS.power - 1))
    X, Y, N, h = get_mesh(ARGS.power)
    real_solution = np.random.rand(N - 2, N - 2)
    f = Ls[str(int(log(N - 1, 2)))].dot(real_solution.flatten()).reshape(
        real_solution.shape)
    normf = norm(f)
    e_0 = real_solution - u
    denom = norm(e_0)

    data = []
    for nu in nus:
        tic = clock()
        u = np.zeros((2**ARGS.power - 1, 2**ARGS.power - 1))
        FRACS = []
        for i in tqdm(xrange(ARGS.max_iterations),
                      desc="(%d,%d)" % (nu[0], nu[1])):
            u_old = u + 0
            u = V_cycle(ARGS.power, u_old, f, nu, X, Y, N, h, Ls)
            res = compute_residual(u, f, Ls)
            normres = norm(res)
            num = norm(real_solution - u)
            FRACS.append((num / denom)**(1 / (i + 1)))
            if condition(normres, normf, ARGS.tolerance):
                break
        toc = clock()

        data.append((nu[0], nu[1], FRACS[0], FRACS[1], FRACS[2], FRACS[3],
                     FRACS[4], i + 1, toc - tic))
    print "\n\n"
    print tabulate(data,
                   headers=[
                       "nu_1", "nu_2", "E_1", "E_2", "E_3", "E_4", "E_5",
                       "its", "time"
                   ],
                   tablefmt="latex")
def make_matrix_rhs_circleproblem(N):
    # N: grid size

    # Get grid spacing
    dx = 1 / (N + 1)
    # create a meshgrid
    x = [i * dx for i in xrange(1, N + 1)]
    X, Y = np.meshgrid(x, x)
    # Create the 2D discrete Laplacian
    A = (dx**2) * get_laplacian(N)
    # initialize the right hand side
    f = np.zeros(N**2)
    # Define the boundary condition on the circle
    Ub = 1
    # Define the center and radius of the circle
    xc = 0.3
    yc = 0.4
    rad = 0.15
    # precompute the distances of all points on the meshgrid
    # to the center of the circle
    phi = np.sqrt((X - xc)**2 + (Y - yc)**2) - rad
    # Predefine common index additions (up,down,left,right)
    IJ = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    # Loop through the grid
    for j in xrange(1, N - 1):
        for i in xrange(1, N - 1):
            # Don't do anything inside the circle
            if phi[i][j] < 0:
                continue
            # loop through common indicies (up,down,left,right)
            for k in xrange(4):
                # only update matrix for points on the boundary
                if phi[i + IJ[k][0]][j + IJ[k][1]] < 0:
                    # Get the appropriate distance to the boundary
                    alpha_num = phi[i][j]
                    alpha_den = phi[i][j] - phi[i + IJ[k][0]][j + IJ[k][1]]
                    alpha = phi[i][j] / alpha_den
                    # Get the distance to the boundary
                    kr = sub2ind([i, j], (N, N))
                    kc = sub2ind([i + IJ[k][0], j + IJ[k][1]], (N, N))
                    # adjust the right hand side
                    f[kr] = f[kr] - Ub / alpha
                    # adjust the diagonal entry
                    A[kr, kr] = A[kr, kr] + 1 - 1 / alpha
                    # adjust the off-diagonal entries
                    A[kr, kc] = 0
                    A[kc, kr] = 0
    # return the matrix A and the right hand side f
    return (A, f)
Exemplo n.º 6
0
def main():
    ARGS = PARSE_ARGS()
    print "grid size: 2^%d-1" % ARGS.power
    print "tolerance:", ARGS.tolerance
    norm = which_norm(ARGS.norm)
    condition = which_condition(ARGS.convergence)

    Ls = {}
    for i in xrange(1, 10):
        Ls[str(i)] = get_laplacian(2**i - 1)

    # Initialize u and get the right hand side of the equation
    u = np.zeros((2**ARGS.power - 1, 2**ARGS.power - 1))
    X, Y, N, h = get_mesh(ARGS.power)
    f = RHS(Y, X)
    normf = norm(f)

    t = tqdm(xrange(ARGS.max_iterations))
    residual_norms = []
    for i in t:
        u_old = u + 0
        u = V_cycle(ARGS.power, u_old, f, ARGS.nus, X, Y, N, h, Ls)
        res = compute_residual(u, f, Ls)
        normres = norm(res)
        residual_norms.append(normres)
        if condition(normres, normf, ARGS.tolerance):
            its = i + 1
            break
        t.set_description("||res||=%.10f" % normres)

    print "\n\niterations = %d" % its

    ratios = ["-"] + [
        residual_norms[i] / residual_norms[i - 1]
        for i in xrange(1, len(residual_norms))
    ]
    table = [[i + 1, residual_norms[i], ratios[i]]
             for i in xrange(len(ratios))]
    print tabulate(
        table,
        headers=["\$\\norm{r_k}_\infty\$", r"$\frac{||r_k||}{||r_{k-1}||}$"],
        tablefmt="latex")