## Initialize OMopt = np.zeros((len(PE), len(N))) # optimal omega array P = np.zeros((len(PE), 2)) # fitting poly. coeff. for OMopt Ni = np.round(np.logspace(1, 4, 100)) # cell-number vector for tendency curves TC = np.zeros((len(PE), len(Ni))) # tendency curves for OMopt-results ## Compute optimal omega_SOR for coarse grids for i in range(0, len(PE)): for j in range(0, len(N)): n = N[j] # sparse system matrix A, _ = FVConvDiff2D.preprocess(n, L, PE[i], problem, FVSCHEME[i]) # obtain iteration matrix for Jacobi method _, _, _, _, G = SIMPy.solve(A, np.ones(n**2), "jacobi", 1, 0, 1, np.ones(n**2), True) evals_large, _ = eigs(G, 6, which='LM') # 6 largest eigenvalues srGJ = np.max(evals_large) # magnitude of largest is spectral radius OMopt[i, j] = 2. / (1 + np.sqrt(1 - srGJ**2)) # theoretical optimal ## Fit omega_opt(n) = c1/(1+c2*sin(pi/n)) using weighted-least-squares polyfit Ne = np.hstack([N, Nasymp]) # add large number of cells to N # add corresponding ~2 to OMopt OMopte = np.hstack([OMopt, Oasymp * np.ones((len(PE), 1))]) w = Ne # weight factors for polyfit legendStrArray = [] # empty array of strings for the plot legend for i in range(0, len(PE)): fitX = -OMopte[i, :] * np.sin(np.pi / Ne) # "x" in linear relation fitY = OMopte[i, :] # "y" in linear relation
OMopt = np.zeros((len(PE), len(N))) # optimal omega array P = np.zeros((len(PE), 2)) # fitting poly. coeff. for OMopt Ni = np.round(np.logspace(1, 4, 100)) # cell-number vector for tendency curves TC = np.zeros((len(PE), len(Ni))) # tendency curves for OMopt-results ## Compute optimal omega_SOR for coarse grids for i in range(0, len(PE)): for j in range(0, len(N)): n = N[j] # sparse system matrix A, _ = FVConvDiff2D.preprocess(n, L, PE[i], problem, FVSCHEME[i]) # obtain iteration matrix for Jacobi method _, _, _, _, G = SIMPy.solve( A, np.ones(n**2), "jacobi", 1, 0, 1, np.ones(n**2), True) evals_large, _ = eigs(G, 6, which='LM') # 6 largest eigenvalues srGJ = np.max(evals_large) # magnitude of largest is spectral radius OMopt[i, j] = 2. / (1 + np.sqrt(1 - srGJ**2)) # theoretical optimal ## Fit omega_opt(n) = c1/(1+c2*sin(pi/n)) using weighted-least-squares polyfit Ne = np.hstack([N, Nasymp]) # add large number of cells to N # add corresponding ~2 to OMopt OMopte = np.hstack([OMopt, Oasymp*np.ones((len(PE), 1))]) w = Ne # weight factors for polyfit legendStrArray = [] # empty array of strings for the plot legend for i in range(0, len(PE)): fitX = -OMopte[i, :] * np.sin(np.pi / Ne) # "x" in linear relation
tol = 1e-4 # tolerance for solution imax = 1000 # max number of iterations method = "SOR" # SIM method omega = 1.2 # factor for SOR x = np.zeros(N) # starting guess for solution z = np.linspace(0, 1, N) # generate grid # Assemble tri-diagonal system matrix for CDS operator data = np.zeros((3, N)) data[0, 0:N-1] = 1 # super diagonal data[1, :] = -2 # diagonal data[2, 1:N] = 1 # sub diagonal offsets = np.array([-1, 0, 1]) A = sp.spdiags(data, offsets, N, N, format="csc") # Assemble source vector b = np.zeros(N) b[1:N] = -8/(N-1) ^ 2 u1 = spsolve(A, b) # direct solution u2, _, iter, _, G = SIMPy.solve(A, b, "sor", 500, 1e-4, 1, np.ones(N), False) # iterative solution ## Plotting plt.plot(u1, z, '--o', linewidth=2, label="direct solution") plt.plot(u2, z, '--o', linewidth=2, label="iterative solution") plt.legend() plt.show() print("done!")
imax = 1000 # max number of iterations method = "SOR" # SIM method omega = 1.2 # factor for SOR x = np.zeros(N) # starting guess for solution z = np.linspace(0, 1, N) # generate grid # Assemble tri-diagonal system matrix for CDS operator data = np.zeros((3, N)) data[0, 0:N - 1] = 1 # super diagonal data[1, :] = -2 # diagonal data[2, 1:N] = 1 # sub diagonal offsets = np.array([-1, 0, 1]) A = sp.spdiags(data, offsets, N, N, format="csc") # Assemble source vector b = np.zeros(N) b[1:N] = -8 / (N - 1) ^ 2 u1 = spsolve(A, b) # direct solution u2, _, iter, _, G = SIMPy.solve(A, b, "sor", 500, 1e-4, 1, np.ones(N), False) # iterative solution ## Plotting plt.plot(u1, z, '--o', linewidth=2, label="direct solution") plt.plot(u2, z, '--o', linewidth=2, label="iterative solution") plt.legend() plt.show() plt.ion() print("done!")