def test_compare_analytic(self):
        E = 5
        v = 0.1
        N_list = 2 ** np.arange(2, 7)
        test_values = N_list ** 2 * 2
        rel_errors = np.zeros(len(N_list))
        u_max = 1
        print("\n\nComparing homogeneous dirichlet elastic solution to analytical result")
        for i, N in enumerate(N_list):
            # 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 = gp.getPlate(N)
            u_max = np.max(np.abs(u(p.T).T))
            edge -= 1
            U = solve_elastic(p, tri, edge, C=get_C(E, v), f=get_f(E, v))

            max_error = np.max(np.abs(U - u(p.T).T))
            print(f"f = {test_values[i]}, max error:", max_error)
            self.assertAlmostEqual(max_error, 0, delta=10 / test_values[i])
            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 Dirichlet")
        plt.ylabel("Relative error")
        plt.xlabel("Degrees of freedom")
        plt.savefig("figures/convergence_homogeneous_dirichlet_elastic.pdf")
        plt.clf()
def get_solution(N, E, v):
    # 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 = gp.getPlate(N)
    edge -= 1

    # Using time.perf_counter() to find the runtime of solve_elastic()
    t1 = time.perf_counter()
    A, F = get_elasticity_A_F(p, tri, edge, C=get_C(E, v), f=get_f(E, v))
    t2 = time.perf_counter()
    make_time = t2 - t1

    t1 = time.perf_counter()
    U = solve_LU(A, F)
    t2 = time.perf_counter()
    lu_time = t2 - t1

    t1 = time.perf_counter()
    U = solve_sparse(A, F)
    t2 = time.perf_counter()
    sparse_time = t2 - t1

    t1 = time.perf_counter()
    U = solve_cg(A, F)
    t2 = time.perf_counter()
    cg_time = t2 - t1

    return U, make_time, lu_time, sparse_time, cg_time
    def test_naive_stress_calculation_analytic(self):
        # Test calculated stresss from analytical displacement u

        N_list = 2**np.arange(2, 7)
        test_values = N_list**2 * 2
        print("\n\nComparing calculated stress to analytical stress:")
        E = 200
        v = 0.1

        for i, N in enumerate(N_list):
            p, tri, edge = getPlate(N)
            edge -= 1  # The edge indexes seem to be off
            U = np.moveaxis(u(p.T), -1, 0)
            C = get_C(E, v)

            Sigma = get_naive_stress_recovery(U, p, tri, C)
            Sigma_exact = np.moveaxis(sigma(p.T, C), -1, 0)

            max_value = np.max(abs(Sigma_exact))
            max_error = np.max(abs(Sigma_exact - Sigma))
            rel_error = max_error / max_value

            print(f"f = {test_values[i]}, rel error:", rel_error)
            self.assertAlmostEqual(rel_error,
                                   0,
                                   delta=10 / test_values[i]**0.5)
예제 #4
0
 def test_plot_getPlate_edes(self):
     print("\n\nPlotting meshes plate")
     n_list = 2 ** np.arange(2, 5)
     fig, axis = plt.subplots(1, len(n_list), figsize=plt.figaspect(1 / 3))
     for i in range(len(n_list)):
         p, tri, edge = gp.getPlate(n_list[i])
         edge-=1
         plt.sca(axis[i])
         plot_disc_with_edegs(p, tri, edge, plt)
     plt.savefig("figures/plot_meshes_plate_edges.pdf")
     plt.clf()
    def test_plot(self):
        N = 32
        E = 200
        v = 0.1

        p, tri, edge = gp.getPlate(N)
        edge -= 1  # The edge indexes seem to be off
        U = solve_elastic(p, tri, edge, C=get_C(E, v), f=get_f(E, v))

        print("\n\nGenrating plot for homogeneous linear elasticity problem")
        fig = plt.figure(figsize=plt.figaspect(2))
        ax = fig.add_subplot(2, 1, 1, projection='3d')
        ax.set_zlabel("$U_{i,j}$")
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.plot_trisurf(p[:, 0], p[:, 1], U[:, 0], cmap=cm.viridis)

        ax2 = fig.add_subplot(2, 1, 2, projection='3d')
        ax2.zaxis._axinfo['label']['space_factor'] = 4
        # ax2.set_title("Error")
        ax2.ticklabel_format(axis='z', style='sci', scilimits=(0, 0))

        ax2.set_zlabel("$e_{x,i,j}$")
        ax2.set_xlabel("x")
        ax2.set_ylabel("y")
        ax2.plot_trisurf(p[:, 0], p[:, 1], U[:, 0] - u(p.T)[0], cmap=cm.viridis)
        # ax.plot_trisurf(p[:, 0], p[:, 1], U)

        plt.savefig("figures/plot_homogeneous_dirichlet_elastic_x.pdf")

        plt.clf()

        # plotting 2nd dimention
        ax = fig.add_subplot(2, 1, 1, projection='3d')
        # ax.set_title("Numerical solution for Dirichlet")
        ax.set_zlabel("$U_{i,j}$")
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.plot_trisurf(p[:, 0], p[:, 1], U[:, 1], cmap=cm.viridis)

        ax2 = fig.add_subplot(2, 1, 2, projection='3d')
        # ax2.set_title("Error")
        ax2.ticklabel_format(axis='z', style='sci', scilimits=(0, 0))
        ax2.set_zlabel("$e_{y,i,j}$")
        ax2.set_xlabel("x")
        ax2.set_ylabel("y")
        ax2.plot_trisurf(p[:, 0], p[:, 1], U[:, 1] - u(p.T)[1], cmap=cm.viridis)
        # ax.plot_trisurf(p[:, 0], p[:, 1], U)

        plt.savefig("figures/plot_homogeneous_dirichlet_elastic_y.pdf")
        plt.close()
    def test_plot_naive_stress_recovery(self):
        N = 32
        E = 5
        v = 0.1
        dim = [0, 0]
        fig = plt.figure(figsize=plt.figaspect(2))
        C = get_C(E, v)

        p, tri, edge = getPlate(N)
        edge -= 1  # The edge indexes seem to be off
        U = solve_elastic(p, tri, edge, C, f=get_f(E, v))
        Sigma = get_naive_stress_recovery(U, p, tri, C)

        Sigma_exact = np.moveaxis(sigma(p.T, C), -1, 0)

        print("\n\nPlotting recovered stress")
        ax = fig.add_subplot(2, 1, 1, projection='3d')
        if dim != [0, 0]:
            ax.view_init(30, 120)
        ax.ticklabel_format(axis='x', style='sci')
        ax.set_zlabel("$\sigma_{xx,i,j}$")
        ax.set_xlabel("x")
        ax.set_ylabel("y")
        ax.plot_trisurf(p[:, 0],
                        p[:, 1],
                        Sigma[:, dim[0], dim[1]],
                        cmap=cm.viridis)

        ax2 = fig.add_subplot(2, 1, 2, projection='3d')
        if dim != [0, 0]:
            ax2.view_init(30, 120)
        ax2.set_zlabel("$\sigma_{xx,,i,j} - \sigma_{xx}(x_i,y_j)}$")
        ax2.set_xlabel("x")
        ax2.set_ylabel("y")
        ax2.plot_trisurf(p[:, 0],
                         p[:, 1],
                         Sigma[:, dim[0], dim[1]] -
                         Sigma_exact[:, dim[0], dim[1]],
                         cmap=cm.viridis)
        # ax.plot_trisurf(p[:, 0], p[:, 1], U)
        plt.savefig(
            f"figures/plot_homogeneous_dirichlet_elastic_sigma_dim_{dim[0]}_{dim[1]}.pdf"
        )
        plt.clf()