def test_compare_analytic(self): test_values = 2 ** np.arange(4, 11) rel_errors = np.zeros(len(test_values)) u_max = 1 print("\n\nComparing mixed Neumann to analytical result") for i, N in enumerate(test_values): # p is coordinates of all nodes # tri is a list of indicies (rows in p) of all nodes belonging to one element # edge is lists of all nodes on the edge p, tri, edge = gd.GetDisc(N) neumann_edges = edge[(p[edge[:, 0]][:, 1] > 0) & (p[edge[:, 1]][:, 1] > 0)] dirichlet_edges = edge[(p[edge[:, 0]][:, 1] <= 0) | (p[edge[:, 1]][:, 1] <= 0)] u_max = np.max(np.abs(u(p.T))) # numerical solution U = solve(p, tri, dirichlet_edges, f=f, g=g, neumann_edges=neumann_edges, Nq=4) max_error = np.max(np.abs(U - u(p.T))) print(f"N = {N}, max error:", max_error) self.assertAlmostEqual(max_error, 0, delta=1e2 / N) rel_errors[i] = max_error rel_errors /= u_max fig = plt.figure(figsize=plt.figaspect(1)) plt.loglog(test_values, rel_errors, marker="o") # plt.title("Convergence of relative error for partial Neumann") plt.ylabel("Relative error") plt.xlabel("$N$ nodes in mesh") plt.savefig("figures/convergence_mixed_neumann.pdf") plt.clf()
def test_plot(self): N = 1024 fig = plt.figure(figsize=plt.figaspect(2)) p, tri, edge = gd.GetDisc(N) neumann_edges = edge[(p[edge[:, 0]][:, 1] > 0) & (p[edge[:, 1]][:, 1] > 0)] dirichlet_edges = edge[(p[edge[:, 0]][:, 1] <= 0) | (p[edge[:, 1]][:, 1] <= 0)] U = solve(p, tri, dirichlet_edges, f, g, neumann_edges, Nq=4) ax = fig.add_subplot(2, 1, 1, projection='3d') # ax.set_title("Numerical solution for mixed Neumann") ax.set_zlabel("$U_{i,j}$") ax.plot_trisurf(p[:, 0], p[:, 1], U, cmap=cm.viridis) # ax = fig.add_subplot(3, 1, 2, projection='3d') # ax.set_title("Analytical solution") # ax.set_zlabel("$U_{i,j}$") # ax.plot_trisurf(p[:,0],p[:,1],u(p.T),cmap=cm.viridis) ax2 = fig.add_subplot(2, 1, 2, projection='3d') # ax2.set_title("Error") ax2.set_zlabel("$U_{i,j} - u(x_i,y_j)$") ax2.plot_trisurf(p[:, 0], p[:, 1], U - u(p.T), cmap=cm.viridis) # ax.plot_trisurf(p[:, 0], p[:, 1], U) plt.savefig("figures/plot_mixed_neumann.pdf") fig.clf()
def test_plot_disc_mesh(self): print("\n\nPlotting meshes") n_list = 2 ** np.array([4, 6, 8]) fig, axis = plt.subplots(1, len(n_list), figsize=plt.figaspect(1/3)) for i in range(len(n_list)): p, tri, edge = gd.GetDisc(n_list[i]) plt.sca(axis[i]) plot_disc(p, tri,plt) plt.savefig("figures/plot_meshes.pdf") plt.clf()
def test_singularity(self): test_values = 2 ** np.arange(4, 11) print("\n\nChecking matrix is singular before applying boundary conditions") for N in test_values: p, tri, edge = gd.GetDisc(N) A, F = get_A_F(p, tri, [], f, Nq=4) n = np.shape(A)[0] rank = np.linalg.matrix_rank(A) print("Rank of A: ", rank, ". Size of matrix A :(", n, " x ", n, ")") self.assertNotEqual(n, rank)
def test_plot(self): N = 1024 fig = plt.figure(figsize=plt.figaspect(2)) p, tri, edge = gd.GetDisc(N) U = solve(p, tri, edge, f, Nq=4) ax = fig.add_subplot(2, 1, 1, projection='3d') # ax.set_title("Numerical solution for Dirichlet") ax.set_zlabel("$U_{i,j}$") ax.plot_trisurf(p[:, 0], p[:, 1], U, cmap=cm.viridis) # ax = fig.add_subplot(3, 1, 2, projection='3d') # ax.set_title("Analytical solution") # ax.set_zlabel("$U_{i,j}$") # ax.plot_trisurf(p[:,0],p[:,1],u(p.T),cmap=cm.viridis) ax2 = fig.add_subplot(2, 1, 2, projection='3d') # ax2.set_title("Error") ax2.set_zlabel("$U_{i,j} - u(x_i,y_j)$") ax2.plot_trisurf(p[:, 0], p[:, 1], U - u(p.T), cmap=cm.viridis) # ax.plot_trisurf(p[:, 0], p[:, 1], U) plt.savefig("figures/plot_homogeneous_dirichlet.pdf") plt.clf()