def solve(self): one_domain = w1_one_domain.one_domain(self.n) one_domain.solve() u_simple = one_domain.get_solution() A = self.get_a() # A Global b = self.get_b() # b global self.A1 = self.get_A1() # D and N block matrix 2 by 2 for Omega 1 self.A2 = self.get_A2() # D and N block matrix 2 by 2 for Omega 2 solvetime = timeit.default_timer() for i in range(self.iterations): if i == 0: u1_itr = scipy.sparse.linalg.spsolve(self.A1,self.b11) #u1^0, ur^0 & u2^0 = 0 initial guess else: u1_itr = scipy.sparse.linalg.spsolve(self.A1,(self.b11+bl1)) u1 = u1_itr[:(self.n-2)**2] uG1 = u1_itr[(self.n-2)**2:] bl2 = self.get_bl2(u1,uG1) u2_itr = scipy.sparse.linalg.spsolve(self.A2,(self.b22+bl2)) uG2 = u2_itr[:(self.n-2)] u2 = u2_itr[(self.n-2):] bl1 = self.get_bl1(uG2,u2) self.set_solution(u1,uG2,u2) self.difference[i] = np.linalg.norm(self.get_solution()-u_simple,ord='fro') solvetime = timeit.default_timer() - solvetime print("iteration took {} seconds.".format(solvetime)) self.set_solution(u1, uG2, u2)
def solve(self): one_domain = w1_one_domain.one_domain(self.n) one_domain.solve() u_simple = one_domain.get_solution() A1 = self.get_a("Neumann") A2 = self.get_a("Dirichlet") solvetime = timeit.default_timer() for i in range(self.iterations): b1 = self.get_b("Neumann") u1_itr = np.reshape(scipy.sparse.linalg.spsolve(A1, b1), (self.n - 1, self.n - 2)) u1_itr = self.relax(i, "Neumann", u1_itr) self.u_gamma = u1_itr[-1, :] b2 = self.get_b("Dirichlet") u2_itr = np.reshape(scipy.sparse.linalg.spsolve(A2, b2), (self.n - 1, self.n - 2)) u2_itr = self.relax(i, "Dirichlet", u2_itr) self.aj = (u2_itr[1:, 0] - self.u_gamma) self.set_solution(u1_itr[:-1, :], u2_itr[1:, :]) #self.error[i] = np.linalg.norm(self.utrue - self.u_gamma,ord=inf) self.difference[i] = np.linalg.norm(self.solution - u_simple, ord='fro') #res1 = np.reshape(A1 * np.asarray(u1_itr).reshape(-1)-b1,(self.n-2,self.n-1)) #res2 = np.reshape(A2 * np.asarray(u2_itr).reshape(-1)-b2, (self.n - 2, self.n - 2)) #self.residual[i,0] = np.linalg.norm(res1[:,-1],ord=inf) #self.residual[i, 1] = np.linalg.norm(res2[:, 0], ord=inf) solvetime = timeit.default_timer() - solvetime print("It took {} seconds to iterate a solution.".format(solvetime)) self.set_solution(u1_itr[:-1, :], u2_itr[1:, :])
This script runs the overdetermined system solving using constrained least squares constraining internal points ''' n = 40 itr = 300 relaxation = 1 # iterated domain initialization and solving itr_domain = w07_ConstrainedLQ_Internal_2d.two_domain(n, itr, relaxation) itr_domain.solve() u_itr = itr_domain.get_solution() # single domain comparison one_domain = w1_one_domain.one_domain(n) one_domain.solve() u_simple = one_domain.get_solution() # calculating difference iterated and single domain diff = u_itr - u_simple # get residuals from iterated domain residuals = itr_domain.get_residual() # get difference ||u_itr(i)-u_simple||fro difference = itr_domain.get_difference() # plotting plt.figure(figsize=(16, 8))
import matplotlib.pyplot as plt from week_03 import w03_2d_block from week_two import w1_one_domain import numpy as np N = 30 itr = 300 rel = 0.2 onedomain = w1_one_domain.one_domain(n = N) onedomain.solve() od = onedomain.get_solution() twodomain = w03_2d_block.two_domain(n = N, iterations=itr, relaxation=rel) twodomain.set_utrue(np.flipud(od[1:-1,N-1])) twodomain.solve() td = twodomain.get_solution() err = twodomain.get_error() diff = td-od plt.subplot(221) plt.title("Iterated Domain solution, %d itr %g relax" % (itr,rel)) plt.pcolor(td) plt.colorbar() plt.subplot(222) plt.title("Simple Domain solution") plt.pcolor(od) plt.colorbar()
def solve(self): one_domain = w1_one_domain.one_domain(self.n) one_domain.solve() u_simple = one_domain.get_solution() A = self.get_a() # A Global b = self.get_b() # b global self.initiate_constrained_matrices() solvetime = timeit.default_timer() for i in range(self.iterations): if i == 0: u1_itr = scipy.sparse.linalg.spsolve( self.A1_constr, self.get_constrained_b1_0()) #u1_residual = u1_itr[1] #u1_itr = u1_itr[0] else: u1_itr = scipy.sparse.linalg.spsolve( self.A1_constr, self.get_constrained_b1(bl1)) #u1_residual = u1_itr[1] #u1_itr = u1_itr[0] u1_itr = u1_itr[:(self.n - 2)**2 + (self.n - 2)] # Temporär residual if i > 0: temp_res_top = self.A1 * u1_itr - (self.b11 + bl1) u1 = u1_itr[:(self.n - 2)**2] uG1 = u1_itr[(self.n - 2)**2:(self.n - 2)**2 + (self.n - 2)] # Relaxation if i > 0: uG1 = self.relaxation * uG1 + (1 - self.relaxation) * uG1_old uG1_old = uG1 bl2 = self.get_bl2(u1, uG1) u2_itr = scipy.sparse.linalg.spsolve(self.A2_constr, self.get_constrained_b2(bl2)) u2_itr = u2_itr[:(self.n - 2)**2 + (self.n - 2)] #Temporär residual temp_res_bot = self.A2 * u2_itr - (self.b22 + bl2) uG2 = u2_itr[:(self.n - 2)] u2 = u2_itr[(self.n - 2):(self.n - 2)**2 + (self.n - 2)] # Relaxation if i > 0: uG2 = self.relaxation * uG2 + (1 - self.relaxation) * uG2_old uG2_old = uG2 bl1 = self.get_bl1(uG2, u2) #self.residual[i, 0] = u1_residual #self.residual[i, 1] = u2_residual self.set_solution(u1, uG2, u2) self.difference[i] = np.linalg.norm(self.solution - u_simple, ord='fro') ''' if i == 20 or i == 50 or i == 100: plt.subplot(311) plt.plot(temp_res_top,'.') plt.title("Iteration %d" % i) plt.subplot(312) plt.plot(temp_res_bot, '.') plt.title("Residual $\Omega_2$") plt.subplot(325) plt.title("Current iterated solution") plt.pcolor(self.solution) plt.subplot(326) plt.title("Difference current iterated exact discrete") plt.pcolor(self.solution-u_simple) plt.colorbar() plt.show() ''' ''' if i == 20: plt.figure(figsize=(10,4)) plt.subplot(211) plt.plot(temp_res_top, '.') plt.title("Residual over $\Omega_1$") plt.subplot(212) plt.plot(temp_res_bot, '.') plt.title("Residual over $\Omega_2$") savefig('residual.pdf') plt.show() ''' solvetime = timeit.default_timer() - solvetime print("iteration took {} seconds.".format(solvetime)) self.set_solution(u1, uG2, u2)
def solve(self): one_domain = w1_one_domain.one_domain(self.n) one_domain.solve() u_simple = one_domain.get_solution() A = self.get_a() # A Global b = self.get_b() # b global self.A1 = self.get_A1() # D and N block matrix 2 by 2 for Omega 1 self.A2 = self.get_A2() # D and N block matrix 2 by 2 for Omega 2 solvetime = timeit.default_timer() for i in range(self.iterations): if i == 0: #u1_itr = scipy.sparse.linalg.spsolve(self.A1,self.b11) #u1^0, ur^0 & u2^0 = 0 initial guess u1_itr = np.linalg.lstsq(self.A1.toarray(), self.b11) u1_residual = u1_itr[1] u1_itr = u1_itr[0] else: u1_itr = np.linalg.lstsq(self.A1.toarray(), (self.b11 + bl1)) u1_residual = u1_itr[1] u1_itr = u1_itr[0] u1 = u1_itr[:(self.n - 2)**2] uG1 = u1_itr[(self.n - 2)**2:] if i > 0: uG1 = self.relaxation * uG1 + (1 - self.relaxation) * uG1_old uG1_old = uG1 bl2 = self.get_bl2(u1, uG1) u2_itr = np.linalg.lstsq(self.A2.toarray(), (self.b22 + bl2)) u2_residual = u2_itr[1] u2_itr = u2_itr[0] uG2 = u2_itr[:(self.n - 2)] u2 = u2_itr[(self.n - 2):] if i > 0: uG2 = self.relaxation * uG2 + (1 - self.relaxation) * uG2_old uG2_old = uG2 bl1 = self.get_bl1(uG2, u2) self.residual[i, 0] = u1_residual self.residual[i, 1] = u2_residual self.set_solution(u1, uG2, u2) self.difference[i] = np.linalg.norm(self.solution - u_simple, ord='fro') if i == 20 or i == 50 or i == 100: temp_res_top = self.A1 * u1_itr - (self.b11 + bl1) temp_res_bot = self.A2 * u2_itr - (self.b22 + bl2) plt.subplot(311) plt.plot(temp_res_top, '.') plt.title("Iteration %d" % i) plt.subplot(312) plt.plot(temp_res_bot, '.') plt.title("Residual $\Omega_2$") plt.subplot(325) plt.title("Current iterated solution") plt.pcolor(self.solution) plt.subplot(326) plt.title("Difference current iterated exact discrete") plt.pcolor(self.solution - u_simple) plt.colorbar() plt.show() solvetime = timeit.default_timer() - solvetime print("iteration took {} seconds.".format(solvetime)) self.set_solution(u1, uG2, u2)