def mg3p(app_data): pipe_data = app_data['pipe_data'] z = pipe_data['z'] y = pipe_data['y'] x = pipe_data['x'] lt = app_data['lt'] lb = app_data['lb'] n = pipe_data['n'] U = Image(Double, "U", [n+2, n+2, n+2]) V = Image(Double, "V", [n+2, n+2, n+2]) R = Image(Double, "R", [n+2, n+2, n+2]) a = app_data['a'] c = app_data['c'] extent = pipe_data['extent'] interior = pipe_data['interior'] ghosts = pipe_data['ghosts'] # from finest to the coarsest level # restrict the residual to the next coarser level restr = {} restr[lt] = R for l in range(lt, lb, -1): restr[l-1] = restrict(restr[l], l, pipe_data, "restrict_"+str(l-1)) # compute an approximate solution on the coarsest grid smooth = {} smooth[lb] = psinv(restr[lb], None, lb, app_data, "psinv_"+str(lb)) residl = {} intrp = {} for l in range(lb+1, lt): # prolongate from level l-1 to l intrp[l] = \ interpolate(smooth[l-1], None, l, pipe_data, "interp_"+str(l)) # compute residual for level l residl[l] = \ residual(intrp[l], restr[l], l, app_data, "resid_"+str(l)) # apply smoother smooth[l] = \ psinv(residl[l], intrp[l], l, app_data, "psinv_"+str(l)) # endfor # prolongate from level lt-1 to lt intrp[lt] = interpolate(smooth[lt-1], U, lt, pipe_data, "interp_"+str(lt)) # compute residual for level lt residl[lt] = residual(intrp[lt], V, lt, app_data, "MG_R") # apply smoother smooth[lt] = psinv(residl[lt], intrp[lt], lt, app_data, "MG_U") return smooth[lt], residl[lt]
def rec_v_cycle(v, f, l): # coarsest level if l == 0: ''' COARSE-SMOOTHING ''' if nuc == 0: return v smooth_p1[l] = {} for t in range(0, nuc): if l == L and t == nuc-1: fname = app_data['cycle_name'] else: fname = "T"+str(t)+"_coarse" if t == 0: in_func = v else: in_func = smooth_p1[l][t-1] smooth_p1[l][t] = w_jacobi(in_func, f, l, fname, app_data) return smooth_p1[l][nuc-1] ################################################### # all other finer levels else: ''' PRE-SMOOTHING ''' smooth_p1[l] = {} for t in range(0, nu1): fname = "T"+str(t)+"_pre_L"+str(l) if t == 0: in_func = v else: in_func = smooth_p1[l][t-1] smooth_p1[l][t] = w_jacobi(in_func, f, l, fname, app_data) if nu1 <= 0: smooth_out = v else: smooth_out = smooth_p1[l][nu1-1] ############################################### ''' RESIDUAL ''' r_h[l] = defect(smooth_out, f, l, "defect_L"+str(l), pipe_data) ############################################### ''' RESTRICTION ''' r_2h[l] = restrict(r_h[l], l, "restrict_L"+str(l-1), pipe_data) ############################################### '''''''''''''''''' ''' NEXT LEVEL ''' '''''''''''''''''' # e_2h <- 0 e_2h[l] = rec_v_cycle(None, r_2h[l], l-1) ############################################### ''' INTERPOLATION & CORRECTION ''' if l == L and nu2 <= 0: fname = app_data['cycle_name'] else: fname = "interp_correct_L"+str(l) if nu1 <= 0: correct_in = v else: correct_in = smooth_p1[l][nu1-1] ec[l] = interpolate(e_2h[l], correct_in, l, fname, pipe_data) if nu2 <= 0: return ec[l] ############################################### ''' POST-SMOOTHING ''' smooth_p2[l] = {} for t in range(0, nu2): fname = "T"+str(t)+"_post_L"+str(l) if l == L and t == nu2-1: fname = app_data['cycle_name'] if t == 0: in_func = ec[l] else: in_func = smooth_p2[l][t-1] smooth_p2[l][t] = w_jacobi(in_func, f, l, fname, app_data) return smooth_p2[l][nu2-1]
def rec_w_cycle(v, f, l, visit): visit[l] += 1 # coarsest level if l == 0: ''' COARSE-SMOOTHING ''' if nuc == 0: return v if visit[l] == 1: smooth_p1[l] = {} smooth_p1[l][visit[l]] = {} for t in range(0, nuc): if l == L and t == nuc - 1: fname = "Wcycle" else: fname = "T" + str(t) + "_coarse" + "__" + str(visit[l]) if t == 0: in_func = v else: in_func = smooth_p1[l][visit[l]][t - 1] smooth_p1[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) return smooth_p1[l][visit[l]][nuc - 1] ################################################### # all other finer levels else: ''' PRE-SMOOTHING ''' if visit[l] == 1: smooth_p1[l] = {} smooth_p1[l][visit[l]] = {} for t in range(0, nu1): fname = "T" + str(t) + "_pre_L" + str(l) + "__" + str(visit[l]) if t == 0: in_func = v else: in_func = smooth_p1[l][visit[l]][t - 1] smooth_p1[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) if nu1 <= 0: smooth_out = v else: smooth_out = smooth_p1[l][visit[l]][nu1 - 1] ############################################### ''' RESIDUAL ''' if visit[l] == 1: r_h[l] = {} name = "defect_L" + str(l) + "__" + str(visit[l]) r_h[l][visit[l]] = defect(smooth_out, f, l, name, pipe_data) ############################################### ''' RESTRICTION ''' if visit[l] == 1: r_2h[l] = {} name = "restrict_L" + str(l - 1) + "__" + str(visit[l]) r_2h[l][visit[l]] = restrict(r_h[l][visit[l]], l, name, pipe_data) ############################################### '''''' '''''' '''''' ''' NEXT LEVEL ''' '''''' '''''' '''''' if visit[l] == 1: e_2h[l] = {} # e_2h <- 0 e_2h[l][visit[l]] = \ rec_w_cycle(None, r_2h[l][visit[l]], l-1, visit) e_2h[l][visit[l]] = \ rec_w_cycle(e_2h[l][visit[l]], r_2h[l][visit[l]], l-1, visit) ############################################### ''' INTERPOLATION & CORRECTION ''' if l == L and nu2 <= 0: fname = "Wcycle" else: fname = "interp_correct_L" + str(l) + "__" + str(visit[l]) if nu1 <= 0: correct_in = v else: correct_in = smooth_p1[l][visit[l]][nu1 - 1] if visit[l] == 1: ec[l] = {} ec[l][visit[l]] = \ interpolate(e_2h[l][visit[l]], correct_in, l, fname, pipe_data) if nu2 <= 0: return ec[l][visit[l]] ############################################### ''' POST-SMOOTHING ''' if visit[l] == 1: smooth_p2[l] = {} smooth_p2[l][visit[l]] = {} for t in range(0, nu2): fname = "T" + str(t) + "_post_L" + str(l) + "__" + str( visit[l]) if l == L and t == nu2 - 1: fname = "Wcycle" if t == 0: in_func = ec[l][visit[l]] else: in_func = smooth_p2[l][visit[l]][t - 1] smooth_p2[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) return smooth_p2[l][visit[l]][nu2 - 1]
def mg3p(app_data): pipe_data = app_data['pipe_data'] z = pipe_data['z'] y = pipe_data['y'] x = pipe_data['x'] lt = app_data['lt'] lb = app_data['lb'] n = pipe_data['n'] U = Image(Double, "U", [n + 2, n + 2, n + 2]) V = Image(Double, "V", [n + 2, n + 2, n + 2]) R = Image(Double, "R", [n + 2, n + 2, n + 2]) a = app_data['a'] c = app_data['c'] extent = pipe_data['extent'] interior = pipe_data['interior'] ghosts = pipe_data['ghosts'] # from finest to the coarsest level # restrict the residual to the next coarser level restr = {} restr[lt] = R for l in range(lt, lb, -1): restr[l - 1] = restrict(restr[l], l, pipe_data, "restrict_" + str(l - 1)) # compute an approximate solution on the coarsest grid smooth = {} smooth[lb] = psinv(restr[lb], None, lb, app_data, "psinv_" + str(lb)) residl = {} intrp = {} for l in range(lb + 1, lt): # prolongate from level l-1 to l intrp[l] = \ interpolate(smooth[l-1], None, l, pipe_data, "interp_"+str(l)) # compute residual for level l residl[l] = \ residual(intrp[l], restr[l], l, app_data, "resid_"+str(l)) # apply smoother smooth[l] = \ psinv(residl[l], intrp[l], l, app_data, "psinv_"+str(l)) # endfor # prolongate from level lt-1 to lt intrp[lt] = interpolate(smooth[lt - 1], U, lt, pipe_data, "interp_" + str(lt)) # compute residual for level lt residl[lt] = residual(intrp[lt], V, lt, app_data, "MG_R") # apply smoother smooth[lt] = psinv(residl[lt], intrp[lt], lt, app_data, "MG_U") return smooth[lt], residl[lt]
def rec_v_cycle(v, f, l): # coarsest level if l == 0: ''' COARSE-SMOOTHING ''' if nuc == 0: return v smooth_p1[l] = {} for t in range(0, nuc): if l == L and t == nuc - 1: fname = app_data['cycle_name'] else: fname = "T" + str(t) + "_coarse" if t == 0: in_func = v else: in_func = smooth_p1[l][t - 1] smooth_p1[l][t] = w_jacobi(in_func, f, l, fname, app_data) return smooth_p1[l][nuc - 1] ################################################### # all other finer levels else: ''' PRE-SMOOTHING ''' smooth_p1[l] = {} for t in range(0, nu1): fname = "T" + str(t) + "_pre_L" + str(l) if t == 0: in_func = v else: in_func = smooth_p1[l][t - 1] smooth_p1[l][t] = w_jacobi(in_func, f, l, fname, app_data) if nu1 <= 0: smooth_out = v else: smooth_out = smooth_p1[l][nu1 - 1] ############################################### ''' RESIDUAL ''' r_h[l] = defect(smooth_out, f, l, "defect_L" + str(l), pipe_data) ############################################### ''' RESTRICTION ''' r_2h[l] = restrict(r_h[l], l, "restrict_L" + str(l - 1), pipe_data) ############################################### '''''' '''''' '''''' ''' NEXT LEVEL ''' '''''' '''''' '''''' # e_2h <- 0 e_2h[l] = rec_v_cycle(None, r_2h[l], l - 1) ############################################### ''' INTERPOLATION & CORRECTION ''' if l == L and nu2 <= 0: fname = app_data['cycle_name'] else: fname = "interp_correct_L" + str(l) if nu1 <= 0: correct_in = v else: correct_in = smooth_p1[l][nu1 - 1] ec[l] = interpolate(e_2h[l], correct_in, l, fname, pipe_data) if nu2 <= 0: return ec[l] ############################################### ''' POST-SMOOTHING ''' smooth_p2[l] = {} for t in range(0, nu2): fname = "T" + str(t) + "_post_L" + str(l) if l == L and t == nu2 - 1: fname = app_data['cycle_name'] if t == 0: in_func = ec[l] else: in_func = smooth_p2[l][t - 1] smooth_p2[l][t] = w_jacobi(in_func, f, l, fname, app_data) return smooth_p2[l][nu2 - 1]
def rec_w_cycle(v, f, l, visit): visit[l] += 1 # coarsest level if l == 0: ''' COARSE-SMOOTHING ''' if nuc == 0: return v if visit[l] == 1: smooth_p1[l] = {} smooth_p1[l][visit[l]] = {} for t in range(0, nuc): if l == L and t == nuc-1: fname = "Wcycle" else: fname = "T"+str(t)+"_coarse"+"__"+str(visit[l]) if t == 0: in_func = v else: in_func = smooth_p1[l][visit[l]][t-1] smooth_p1[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) return smooth_p1[l][visit[l]][nuc-1] ################################################### # all other finer levels else: ''' PRE-SMOOTHING ''' if visit[l] == 1: smooth_p1[l] = {} smooth_p1[l][visit[l]] = {} for t in range(0, nu1): fname = "T"+str(t)+"_pre_L"+str(l)+"__"+str(visit[l]) if t == 0: in_func = v else: in_func = smooth_p1[l][visit[l]][t-1] smooth_p1[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) if nu1 <= 0: smooth_out = v else: smooth_out = smooth_p1[l][visit[l]][nu1-1] ############################################### ''' RESIDUAL ''' if visit[l] == 1: r_h[l] = {} name = "defect_L"+str(l)+"__"+str(visit[l]) r_h[l][visit[l]] = defect(smooth_out, f, l, name, pipe_data) ############################################### ''' RESTRICTION ''' if visit[l] == 1: r_2h[l] = {} name = "restrict_L"+str(l-1)+"__"+str(visit[l]) r_2h[l][visit[l]] = restrict(r_h[l][visit[l]], l, name, pipe_data) ############################################### '''''''''''''''''' ''' NEXT LEVEL ''' '''''''''''''''''' if visit[l] == 1: e_2h[l] = {} # e_2h <- 0 e_2h[l][visit[l]] = \ rec_w_cycle(None, r_2h[l][visit[l]], l-1, visit) e_2h[l][visit[l]] = \ rec_w_cycle(e_2h[l][visit[l]], r_2h[l][visit[l]], l-1, visit) ############################################### ''' INTERPOLATION & CORRECTION ''' if l == L and nu2 <= 0: fname = "Wcycle" else: fname = "interp_correct_L"+str(l)+"__"+str(visit[l]) if nu1 <= 0: correct_in = v else: correct_in = smooth_p1[l][visit[l]][nu1-1] if visit[l] == 1: ec[l] = {} ec[l][visit[l]] = \ interpolate(e_2h[l][visit[l]], correct_in, l, fname, pipe_data) if nu2 <= 0: return ec[l][visit[l]] ############################################### ''' POST-SMOOTHING ''' if visit[l] == 1: smooth_p2[l] = {} smooth_p2[l][visit[l]] = {} for t in range(0, nu2): fname = "T"+str(t)+"_post_L"+str(l)+"__"+str(visit[l]) if l == L and t == nu2-1: fname = "Wcycle" if t == 0: in_func = ec[l][visit[l]] else: in_func = smooth_p2[l][visit[l]][t-1] smooth_p2[l][visit[l]][t] = \ w_jacobi(in_func, f, l, fname, app_data) return smooth_p2[l][visit[l]][nu2-1]