def test_precision(ref): precision = [] A, b, nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., False, True, 'numpy') A = np.array(A) b = np.array(b) precision.append(np.linalg.norm(A @ sol - b) / np.linalg.norm(b)) A, b, nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., False, True, 'LU') A = np.array(A) b = np.array(b) precision.append(np.linalg.norm(A @ sol - b) / np.linalg.norm(b)) A, b, nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., False, True, 'LUcsr') A = np.array(A) b = np.array(b) precision.append(np.linalg.norm(A @ sol - b) / np.linalg.norm(b)) A, b, nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., False, True, 'LUcsr-rcmk') A = np.array(A) b = np.array(b) precision.append(np.linalg.norm(A @ sol - b) / np.linalg.norm(b)) print(precision)
def test_complexity_maillage(start, target, step, num): num_nodes = [] times_QR = [] times_LU = [] for raff in np.arange(start, target, step): times_loop = [] for i in range(num): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 0, 0, 100., False, True, 'QR') times_loop.append(tictoc) num_nodes.append(nodes) times_QR.append(np.mean(times_loop)) for raff in np.arange(start, target, step): times_loop = [] for i in range(num): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 0, 0, 100., False, True, 'LU') times_loop.append(tictoc) # num_nodes.append(nodes) times_LU.append(np.mean(times_loop)) plt.title( "Temps d\'exécution de LUsolve et QRsolve (optimisés)\n%s" % "\n".join(wrap(" en fonction de la taille du maillage", width=60))) plt.ylabel('Temps [s]') plt.xlabel('Nombre de noeuds [/]') plt.plot(num_nodes, times_QR, label='QRsolve') plt.plot(num_nodes, times_LU, label='LUsolve') vec = np.linspace(num_nodes[0], num_nodes[-1], 200) plt.plot(vec, np.polyval(np.polyfit(num_nodes, times_LU, 3), vec), label='Polyfit LU degré 3') # plt.plot(vec, np.polyval(np.polyfit(num_nodes, times_LU, 2), vec), label='Polyfit LU degré 2') # plt.plot(vec, np.polyval(np.polyfit(num_nodes, times_QR, 3), vec), label='Polyfit QR degré 3') # plt.plot(vec, np.polyval(np.polyfit(num_nodes, times_QR, 2), vec), label='Polyfit QR degré 2') plt.grid() plt.legend() plt.show() print( np.linalg.norm( np.polyval(np.polyfit(num_nodes, times_QR, 3), num_nodes) - times_QR)) print( np.linalg.norm( np.polyval(np.polyfit(num_nodes, times_QR, 2), num_nodes) - times_QR)) print( np.linalg.norm( np.polyval(np.polyfit(num_nodes, times_LU, 3), num_nodes) - times_LU)) print( np.linalg.norm( np.polyval(np.polyfit(num_nodes, times_LU, 2), num_nodes) - times_LU)) """
def test_complexity_maillage(start, target, step, num): num_nodes = [] times_LUcsr_rcmk = [] for raff in np.arange(start, target, step): times_loop = [] for i in range(num): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, False, 'LUcsr-rcmk') times_loop.append(tictoc) num_nodes.append(nodes) times_LUcsr_rcmk.append(np.mean(times_loop)) print("Times LUcsr RCMK = ", times_LUcsr_rcmk) times_LUcsr = [] for raff in np.arange(start, target, step): times_loop = [] for i in range(num): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, False, 'LUcsr') times_loop.append(tictoc) times_LUcsr.append(np.mean(times_loop)) print("Times LUcsr = ", times_LUcsr) times_LU = [] for raff in np.arange(start, target, step): times_loop = [] for i in range(num): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, False, 'LU-no-pivot') times_loop.append(tictoc) times_LU.append(np.mean(times_loop)) print("Times LU = ", times_LU) plt.title( "Temps d\'exécution des solveurs (lents)\n%s" % "\n".join(wrap(" en fonction de la taille du maillage", width=60))) plt.ylabel('Temps [s]') plt.xlabel('Nombre de noeuds [/]') print("num nodes ", num_nodes) print("times LUcsr rcmk", times_LUcsr_rcmk) print("times LUcsr", times_LUcsr) print("times LU", times_LU) print("rapport", np.array(times_LUcsr) / np.array(times_LU)) plt.plot(num_nodes, times_LUcsr_rcmk, label='LUcsr avec RCMK') plt.plot(num_nodes, times_LUcsr, label='LUcsr sans RCMK') plt.plot(num_nodes, times_LU, label='LU plein') plt.yscale('log') plt.xscale('log') plt.grid() plt.legend() plt.show()
def test_precision_and_time(ref): precision_LU = [] precision_np = [] times = [] order = ['statique', 'stationnaire', 'harmonique', 'dynamique'] SolverType = 'LUcsr-rcmk' A, b, nodes, x_lu, cond, tictoc = ndtfun(0.2, ref, 0, 0, 100., False, True, SolverType) A2 = A.copy() times.append(tictoc) precision_LU.append( np.linalg.norm(np.dot(A2, x_lu) - b) / np.linalg.norm(b)) x_np = np.linalg.solve(A, b) precision_np.append( np.linalg.norm(np.dot(A, x_np) - b) / np.linalg.norm(b)) A, b, nodes, x_lu, cond, tictoc = ndtfun(0.2, ref, 0, 100, 100., False, True, SolverType) A2 = A.copy() times.append(tictoc) precision_LU.append( np.linalg.norm(np.dot(A2, x_lu) - b) / np.linalg.norm(b)) x_np = np.linalg.solve(A, b) precision_np.append( np.linalg.norm(np.dot(A, x_np) - b) / np.linalg.norm(b)) A, b, nodes, x_lu, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., False, True, SolverType) A2 = A.copy() times.append(tictoc) precision_LU.append( np.linalg.norm(np.dot(A2, x_lu) - b) / np.linalg.norm(b)) x_np = np.linalg.solve(A, b) precision_np.append( np.linalg.norm(np.dot(A, x_np) - b) / np.linalg.norm(b)) A, b, nodes, x_lu, cond, tictoc = ndtfun(0.2, ref, 30, 100, 100., False, True, SolverType) A2 = A.copy() times.append(tictoc) precision_LU.append( np.linalg.norm(np.dot(A2, x_lu) - b) / np.linalg.norm(b)) x_np = np.linalg.solve(A, b) precision_np.append( np.linalg.norm(np.dot(A, x_np) - b) / np.linalg.norm(b)) print(order) print(precision_LU) # print(precision_np) print(times) print(nodes)
def test_rcmk_matrix(): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 0, 0, 100, False, True, 'LUcsr-rcmk') A2 = A.copy() sA, iA, jA = CSRformat(A) R = RCMK(iA, jA) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4.5)) ax1.spy(A, precision=1e-15) ax1.set_ylabel('Lignes de la matrice') ax1.set_xlabel('Colonnes de la matrice') LUres = LU_no_pivot(A) ax2.spy(LUres, precision=1e-15) ax2.set_ylabel('Lignes de la matrice') ax2.set_xlabel('Colonnes de la matrice') plt.show() fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4.5)) ax1.spy(A2[np.ix_(R, R)], precision=1e-15) ax1.set_ylabel('Lignes de la matrice') ax1.set_xlabel('Colonnes de la matrice') LUres = LU_no_pivot(A2[np.ix_(R, R)]) ax2.spy(LUres, precision=1e-15) ax2.set_ylabel('Lignes de la matrice') ax2.set_xlabel('Colonnes de la matrice') plt.show()
def condMinvA(freq, vel): A, b, nodes, x_lu, cond, tictoc = ndtfun(0.2, 1, freq, vel, 100., False, True) A2 = A.copy() LUres, P = ILU0(np.array(A)) LUres = LUres[P[:-1]] U = np.triu(LUres) L = np.eye(len(LUres), dtype=complex) + LUres - U M = L.conjugate() @ U Minv = np.linalg.inv(M) return np.linalg.cond(Minv.conjugate() @ A2), cond
def show_two_mats(): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, 2, 50, 0, 100., False, True, 'numpy') A = np.array(A) plt.spy(A) plt.title("Masque de A avant permutation par RCMK") plt.show() sA, iA, jA = CSRformat(A) R = RCMK(iA, jA) plt.spy(A[np.ix_(R, R)]) plt.title("Masque de A après permutation par RCMK") plt.show()
def test_max_neighbors(start, target, step): neighbors = [] num_nodes = [] for raff in np.arange(start, target, step): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, True, 'numpy') # num_nodes.append(nodes) neighbors_it = [np.count_nonzero(A[i]) for i in range(len(A))] num_nodes.append(nodes) neighbors.append(np.max(neighbors_it)) print(neighbors) print(num_nodes)
def test_solve(): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 50, 1, 100., run=True, copy=True, SolverType='GMRES', rtol=1e-17, prec=True) print("Precision = ", np.linalg.norm(A @ sol - b) / np.linalg.norm(b))
def plot_prec(ref): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-20, prec=False) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), 1e-20, True) print(res) plt.plot(np.arange(len(res)), res, label='Préconditionné') A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-17, prec=False) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), 1e-17, False) plt.plot(np.arange(len(res)), res, label='Non-préconditionné') plt.title( "Précision relative de la solution en fonction du nombre d'itérations") plt.yscale('log') plt.xlabel("Nombre d'itérations [/]") plt.ylabel("Précision relative [/]") plt.legend() plt.grid() plt.show()
def plot_eig(): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 50, 0, 100., run=False, copy=True, SolverType='GMRES', rtol=1e-7, prec=True) A2 = A.copy() LUres = ILU0_slow(np.array(A)) U = np.triu(LUres) L = np.eye(len(LUres), dtype=complex) + LUres - U M = L.conjugate() @ U Minv = np.linalg.inv(M) fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(4, 7)) s = np.linalg.eigvals(A2) ticks_x = ticker.FuncFormatter(lambda x, pos: '{:5.1e}'.format(x)) ax1.xaxis.set_major_formatter(ticks_x) ticks_y = ticker.FuncFormatter(lambda x, pos: '{:5.1e}'.format(x)) ax1.yaxis.set_major_formatter(ticks_y) ax1.scatter(s.real, s.imag) ax1.set_xlabel("Axe Réel") ax1.set_ylabel("Axe Imaginaire") ax1.set_title("Valeurs propres de A") ax1.set_yscale('log') ax1.set_xscale('log') s2 = np.linalg.eigvals(Minv @ A2) ax2.scatter(s2.real, s2.imag) ax2.set_xlabel("Axe Réel") ax2.set_ylabel("Axe Imaginaire") ax2.set_title("Valeurs propres de M^(-1) A") ax2.set_yscale('log') ax2.set_xscale('log') plt.show()
def test_sparsity(start, stop, step): num_nodes = [] band_before_rcmk = [] band_after_rcmk = [] percent_zeros_before = [] percent_zeros_no_rcmk = [] percent_zeros_with_rcmk = [] for raff in np.arange(start, stop, step): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, True, 'numpy') num_nodes.append(nodes) A = np.array(A) A2 = np.copy(A) sA, iA, jA = CSRformat(np.array(A)) r = RCMK(iA, jA) r_inv = invert_r(r) sAr, iAr, jAr = reduce_bands(sA, iA, jA, r, r_inv) band_before_rcmk.append(max(compute_bands(iA, jA, len(A)))) band_after_rcmk.append(max(compute_bands(iAr, jAr, len(A)))) percent_zeros_before.append( (np.count_nonzero(np.array(A)) / (len(A) * len(A))) * 100) LUres = LU_no_pivot(A) percent_zeros_no_rcmk.append( (np.count_nonzero(np.array(LUres)) / (len(A) * len(A))) * 100) LUres_rcmk = LU_no_pivot(A2[np.ix_(r, r)]) percent_zeros_with_rcmk.append( (np.count_nonzero(LUres_rcmk) / (len(A) * len(A))) * 100) if raff == 1: fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4.5)) # fig.suptitle("Génération du fill-in avec et sans RCMK") ax1.spy(LUres) ax2.spy(LUres_rcmk) plt.show() print(num_nodes) print(band_before_rcmk) print(band_after_rcmk) print(percent_zeros_before) print(percent_zeros_no_rcmk) print(percent_zeros_with_rcmk)
def test_perf_convert(start, target, step): num_nodes = [] times_slow = [] times_fast = [] for raff in np.arange(start, target, step): A, b, nodes, sol, cond, tictoc = ndtfun(0.2, raff, 50, 0, 100., False, True, 'numpy') num_nodes.append(nodes) tic = time.time() sA, iA, jA = CSRformat_slow(A) toc = time.time() times_slow.append(toc - tic) tic = time.time() sA, iA, jA = CSRformat(A) toc = time.time() times_fast.append(toc - tic) print(num_nodes) print(times_slow) print(times_fast) fig, ax1 = plt.subplots() line, = ax1.plot(num_nodes, times_slow, color='b') ax1.set_yscale('log') ax1.set_ylabel('temps [s]', color='b') ax1.set_xscale('log') ax1.set_xlabel('Nombre de noeuds [/]') ax2 = ax1.twinx() line2, = ax2.plot(num_nodes, times_fast, color='r') ax2.set_yscale('log') ax2.set_ylabel('temps [s]', color='r') ax2.set_xscale('log') plt.legend((line, line2), ('Convertisseur lent', 'Convertisseur rapide')) plt.title('Performances des convertisseurs en format creux') plt.grid() plt.show()
def plot_prec_iter(): prec = [] iter = [] for n in range(1, 41, 1): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=False) b = b.copy() sA, iA, jA = CSRformat(np.array(A)) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol=1e-14, prec=True, max_iter=n) prec.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) iter.append(len(res)) prec = np.array(prec) iter = np.array(iter) idx = np.where(np.logical_and(prec >= 1e-9, prec <= 1e-1)) plt.plot(iter[idx], prec[idx], label="Préconditionné") prec = [] iter = [] for n in range(1, 242, 6): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=False) b = b.copy() sA, iA, jA = CSRformat(np.array(A)) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol=1e-7, prec=False, max_iter=n) prec.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) iter.append(len(res)) prec = np.array(prec) iter = np.array(iter) idx = np.where(np.logical_and(prec >= 1e-9, prec <= 1e-1)) plt.plot(iter[idx], prec[idx], label="Sans préconditionnement") plt.title( "Précision relative de la solution en fonction du nombre d'itérations") plt.ylabel("Précision relative [/]") plt.xlabel("Nombre d'itérations [/]") plt.legend() plt.yscale('log') plt.grid() plt.show()
def test_solve(): A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, 1, 50, 0, 100., True, False, 'numpy')
def get_conv(ref, max_iter, rtol): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3)) prec = False word = 'sans' # STATIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax1.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Statique') print(len(res)) # STATIONNAIRE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax1.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Stationnaire') # HARMONIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax1.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Harmonique') # DYNAMIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax1.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Dynamique') ax1.set_yscale('log') ax1.set_title("Convergence de GMRES " + word + " préconditionnement") ax1.set_xlabel("Nombre d'itérations [/]") ax1.set_ylabel("||r||/||b|| [/]") ax1.legend() ax1.grid() prec = True word = 'avec' # STATIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax2.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Statique') # STATIONNAIRE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax2.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Stationnaire') # HARMONIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax2.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Harmonique') # DYNAMIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) ax2.plot(np.arange(len(res)), res / np.linalg.norm(b), label='Dynamique') ax2.set_yscale('log') ax2.set_title("Convergence de GMRES " + word + " préconditionnement") ax2.set_xlabel("Nombre d'itérations [/]") ax2.set_ylabel("||r||/||b|| [/]") ax2.legend() ax2.grid() plt.show()
def get_iter_prec(rtol, prec, ref, max_iter): precision = [] niter = [] # STATIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) precision.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) niter.append(len(res)) # STATIONNAIRE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 0, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) precision.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) niter.append(len(res)) # HARMONIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 0, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) precision.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) niter.append(len(res)) # DYNAMIQUE A, b, num_nodes, sol, cond, tictoc = ndtfun(0.2, ref, 50, 1, 100., run=False, copy=True, SolverType='numpy', rtol=1e-7, prec=prec) sA, iA, jA = CSRformat(A) u, res = csrGMRES(sA, iA, jA, np.array(b), rtol, prec, max_iter=max_iter) precision.append(np.linalg.norm(np.dot(A, u) - b) / np.linalg.norm(b)) niter.append(len(res)) print(precision) print(niter)