├── .gitignore ├── Codes ├── Miscellaneous.pyc ├── IterativeMethods.pyc ├── Miscellaneous.py └── IterativeMethods.py ├── FigStyle └── plot.mplstyle └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | **/.ipynb_checkpoints/ 2 | **/.directory 3 | **/__pycache__ -------------------------------------------------------------------------------- /Codes/Miscellaneous.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/barbagroup/MG_tutorial/master/Codes/Miscellaneous.pyc -------------------------------------------------------------------------------- /Codes/IterativeMethods.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/barbagroup/MG_tutorial/master/Codes/IterativeMethods.pyc -------------------------------------------------------------------------------- /FigStyle/plot.mplstyle: -------------------------------------------------------------------------------- 1 | axes.titlesize: 20 2 | axes.labelsize: 18 3 | axes.grid : True 4 | axes.linewidth: 2.0 5 | 6 | figure.dpi : 100 7 | 8 | lines.linewidth : 3 9 | 10 | legend.fontsize : x-large 11 | 12 | xtick.labelsize : 16 13 | ytick.labelsize : 16 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multigrid Methods Tutorial 2 | ----------------------------- 3 | 4 | ## Progress (finished lessons): 5 | * Lesson 0: Convergence of Errors 6 | * Lesson 1: Basic Multigrid Scheme 7 | 8 | 9 | 10 | ## Reference: 11 | W.L. Briggs, V.E. Henson and S.F. McCormick, A Multigrid Tutorial, 2nd. ed., SIAM, Philadelphia, 2000 12 | -------------------------------------------------------------------------------- /Codes/Miscellaneous.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Oct 21 01:24:13 2014 4 | 5 | @author: pychuang 6 | """ 7 | import numpy as np 8 | 9 | 10 | def LInfty(V): 11 | return np.amax(np.abs(V)) 12 | 13 | 14 | def L2norm(V): 15 | return np.sqrt(np.sum(V**2)) 16 | 17 | 18 | def Init1DLaplaceAb(N): 19 | ''' 20 | Initialize matrix A and vector b 21 | for the 1D Laplace equation in centeral difference form. 22 | Dirchlet boundary conditions are included 23 | ''' 24 | A = np.zeros((N, N)) 25 | b = np.zeros(N) 26 | dx = 1. / float(N - 1) 27 | 28 | A[0, 0] = 1.0 29 | for i in range(1, N-1): 30 | A[i, i-1:i+2] = [-1.0, 2.0, -1.0] 31 | A[N-1, N-1] = 1.0 32 | return A, b 33 | 34 | 35 | def InitValue(N, k): 36 | ''' 37 | Return initial guess of v => v0 = sin(i*k*pi/N), i=0~N 38 | ''' 39 | return np.array([np.sin(float(i * k) * np.pi / float(N-1)) 40 | for i in range(N)]) 41 | 42 | 43 | def plotAssist(title='', xlmt='', ylmt='', 44 | xlbl=r"$x$", ylbl=r"$v$", legArg={'loc':0, 'numpoints':1}): 45 | plt.title(title); 46 | if xlmt != '': 47 | plt.xlim(xlmt); 48 | if ylmt != '': 49 | plt.ylim(ylmt); 50 | plt.xlabel(xlbl); 51 | plt.ylabel(ylbl); 52 | plt.legend(**legArg); -------------------------------------------------------------------------------- /Codes/IterativeMethods.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Oct 23 12:39:30 2014 4 | 5 | @author: pychuang 6 | """ 7 | import numpy as np 8 | 9 | 10 | # Jacobi Method 11 | def Jacobi(N, A, v0, b, Niter=1): 12 | ''' 13 | Inputs: 14 | N: the dimension of A, v0, and b (integer) 15 | A: coefficient matrix (N by N array) 16 | v0: initial guess (N by 1 array) 17 | b: constant matrix (N by 1 array) 18 | Niter: the number of iterations needed to be performed 19 | Return: 20 | The approximated solution v after Niter iterations 21 | (N by 1 array) 22 | ''' 23 | v = np.empty(N) 24 | DA = np.diag(A) 25 | for ITER in range(Niter): 26 | v = v0 + (b - np.dot(A, v0)) / DA 27 | v0 = v.copy() 28 | return v 29 | 30 | 31 | # Weighted Jacobi Method 32 | def WJacobi(N, A, v0, b, omg=2./3., Niter=1): 33 | ''' 34 | Inputs: 35 | N: the dimension of A, v0, and b (integer) 36 | A: coefficient matrix (N by N array) 37 | v0: initial guess (N by 1 array) 38 | b: constant matrix (N by 1 array) 39 | omg: relaxation parameter (float) 40 | Niter: the number of iterations needed to be performed 41 | Return: 42 | The approximated solution v after Niter iterations 43 | (N by 1 array) 44 | ''' 45 | v1 = np.empty(N) 46 | DA = np.diag(A) 47 | for ITER in range(Niter): 48 | v1 = v0 + omg * (b - np.dot(A, v0)) / DA 49 | v0 = v1.copy() 50 | return v0 51 | 52 | 53 | # Gauss-Seidel Method 54 | def GaussSeidel(N, A, v0, b, Niter=1): 55 | ''' 56 | Inputs: 57 | N: the dimension of A, v0, and b (integer) 58 | A: coefficient matrix (N by N array) 59 | v0: initial guess (N by 1 array) 60 | b: constant matrix (N by 1 array) 61 | Niter: the number of iterations needed to be performed 62 | Return: 63 | The approximated solution v after Niter iterations 64 | (N by 1 array) 65 | ''' 66 | Ad = np.diag(A) 67 | for ITER in range(Niter): 68 | for i in range(N): 69 | v0[i] = v0[i] + (b[i] - np.dot(A[i, :], v0)) / Ad[i] 70 | return v0 71 | 72 | 73 | # Red-Black Gauss-Seidel Method 74 | def RBGaussSeidel(N, A, v0, b, Niter=1): 75 | ''' 76 | Inputs: 77 | N: the dimension of A, v0, and b (integer) 78 | A: coefficient matrix (N by N array) 79 | v0: initial guess (N by 1 array) 80 | b: constant matrix (N by 1 array) 81 | Niter: the number of iterations needed to be performed 82 | Return: 83 | The approximated solution v after Niter iterations 84 | (N by 1 array) 85 | ''' 86 | Ad = np.diag(A) 87 | for ITER in range(Niter): 88 | v0[0::2] = v0[0::2] + \ 89 | (b[0::2] - np.dot(A[0::2, :], v0)) / Ad[0::2] 90 | v0[1::2] = v0[1::2] + \ 91 | (b[1::2] - np.dot(A[1::2, :], v0)) / Ad[1::2] 92 | return v0 93 | --------------------------------------------------------------------------------