Ejemplo n.º 1
0
def test_plot_basis_function():
    from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
    U = np.array([0, 0, 0, 0, 1,1,1, 2, 3, 4, 4, 4, 4])
    resolution = 1000
    degree = len(np.where(U == 0.)[0]) - 1
    N, der = basis_function_array_nurbs(U,
                                                       degree,
                                                       resolution,
                                                       None)

    fig = plt.figure()
    ax = plt.gca()
    ax.plot(N)
    fig.show()
Ejemplo n.º 2
0
    def get_basis_function(self, direction: int = 0, resolution=None):
        """
        Obtain the basis functions and derivatives of the knot vector at desired direction
        Args:
            direction: parametric direction of knot vector
            resolution: amount of points to form the basis function
        Returns:
        """

        from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
        N, dN = basis_function_array_nurbs(
            self.knots[direction], self.degree[direction],
            resolution if resolution is not None else self.resolution,
            self.weight)
        return N, dN
Ejemplo n.º 3
0
def test_make_NURB_biquadratic():
    from pygeoiga.nurb.cad import make_surface_biquadratic
    knots, B = make_surface_biquadratic()

    from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface
    fig, [ax2, ax] = plt.subplots(1, 2, sharey=True, figsize=(7, 3))
    ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k")
    ax = p_surface(knots,
                   B,
                   ax=ax,
                   dim=2,
                   color="blue",
                   border=False,
                   alpha=0.5)
    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)
    ax.set_xlabel("x")

    #ax.annotate("1", (-1, 1), fontsize=20)
    #ax.annotate("2", (1.6, 3), fontsize=20)
    ax.set_aspect(0.8)

    ax2 = p_cpoints(B,
                    ax=ax2,
                    dim=2,
                    color="blue",
                    linestyle="-",
                    point=False,
                    line=True)
    ax2 = p_cpoints(B,
                    ax=ax2,
                    dim=2,
                    color="red",
                    marker="s",
                    point=True,
                    line=False)
    n, m = B.shape[0], B.shape[1]

    P = np.asarray([(B[x, y, 0], B[x, y, 1]) for x in range(n)
                    for y in range(m)])
    for count, point in enumerate(P):
        ax2.annotate("$P_{%s}$" % str(count),
                     point,
                     fontsize=8,
                     xytext=(3, 7),
                     textcoords="offset points")

    ax2.spines["right"].set_visible(False)
    ax2.spines["top"].set_visible(False)
    ax2.set_xlabel("x")
    ax2.set_ylabel("y")
    ax2.set_aspect(0.8)
    fig.show()

    from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
    fig3 = plt.figure(constrained_layout=True)
    gs = fig3.add_gridspec(2,
                           2,
                           hspace=0,
                           wspace=0,
                           width_ratios=[0.2, 1],
                           height_ratios=[1, 0.2])
    (ax_v, ax3), (no, ax_u) = gs.subplots(sharex=True, sharey=True)
    no.remove()
    N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                               degree=2,
                                               resolution=100)
    N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                               degree=2,
                                               resolution=100)
    resol = np.linspace(0, 1, 100)

    ax_u.plot(resol, N_spline_u)
    ax_u.spines["top"].set_visible(False)
    ax_u.spines["right"].set_visible(False)

    ax_u.set_xlim(0, 1)
    ax_u.set_ylim(0, 1)

    ax_v.plot(N_spline_v, resol)
    ax_v.spines["top"].set_visible(False)
    ax_v.spines["right"].set_visible(False)
    ax_v.set_yticks(knots[2:-2])
    ax_v.set_xlim(1, 0)
    ax_v.set_ylim(0, 1)

    for i in knots[0]:
        ax3.vlines(i, 0, 1, 'k')
    for j in knots[1]:
        ax3.hlines(j, 0, 1, 'k')
    ax3.set_xlim(0, 1)
    ax3.set_ylim(0, 1)
    ax3.set_axis_off()

    ax_u.set_xlabel("$u$")
    ax_v.set_ylabel("$v$")
    ax_v.set_yticks(knots[1][2:-2])
    ax_u.set_xticks(knots[0][2:-2])
    for ax in ax_u, ax_v, ax3, no:
        ax.label_outer()
    fig3.show()

    save = True
    if save or save_all:
        fig.savefig(fig_folder + "B-spline_biquadratic.pdf", **kwargs_savefig)
        fig3.savefig(fig_folder + "B-spline_biquadratic_parameter.pdf",
                     **kwargs_savefig)
Ejemplo n.º 4
0
    def plot(B, knots, file_name):
        from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface
        fig, ax = plt.subplots(constrained_layout=True)

        ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k")
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)
        ax.set_xlabel("$x$")
        ax.set_ylabel("$y$")
        ax.set_aspect(0.8)
        ax.plot(0.7, 3, "r*", markersize=10)
        fig.tight_layout(pad=0, h_pad=0, w_pad=0)
        fig.show()

        from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
        fig2 = plt.figure(constrained_layout=True)
        gs = fig2.add_gridspec(2,
                               2,
                               hspace=0,
                               wspace=0,
                               width_ratios=[0.2, 1],
                               height_ratios=[1, 0.2])
        (ax_v, ax2), (no, ax_u) = gs.subplots(sharex=True, sharey=True)
        no.remove()
        N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                                   degree=2,
                                                   resolution=100)
        N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                                   degree=2,
                                                   resolution=100)
        resol = np.linspace(0, 1, 100)

        ax_u.plot(resol, N_spline_u)
        ax_u.spines["top"].set_visible(False)
        ax_u.spines["right"].set_visible(False)

        ax_u.set_xlim(0, 1)
        ax_u.set_ylim(0, 1)

        ax_v.plot(N_spline_v, resol)
        ax_v.spines["top"].set_visible(False)
        ax_v.spines["right"].set_visible(False)
        ax_v.set_yticks(knots[2:-2])
        ax_v.set_xlim(1, 0)
        ax_v.set_ylim(0, 1)

        for i in knots[0]:
            ax2.vlines(i, 0, 1, 'k')
        for j in knots[1]:
            ax2.hlines(j, 0, 1, 'k')
        ax2.set_xlim(0, 1)
        ax2.set_ylim(0, 1)
        ax2.set_axis_off()
        ax2.plot(0.65, 0.45, "r*", markersize=10)

        ax_u.set_xlabel("$u$")
        ax_v.set_ylabel("$v$")
        ax_v.set_yticks(knots[1][2:-2])
        ax_u.set_xticks(knots[0][2:-2])
        for ax in ax_u, ax_v, ax2, no:
            ax.label_outer()
        fig2.show()

        fig3, ax3 = plt.subplots()
        ax3.vlines(-1, -1, 1, 'k')
        ax3.vlines(1, -1, 1, 'k')
        ax3.hlines(-1, -1, 1, 'k')
        ax3.hlines(1, -1, 1, 'k')
        ax3.spines['left'].set_position('center')
        ax3.spines['bottom'].set_position('center')

        # Eliminate upper and right axes
        ax3.spines['right'].set_visible(False)
        ax3.spines['top'].set_visible(False)

        # Show ticks in the left and lower axes only
        ax3.xaxis.set_ticks_position('bottom')
        ax3.yaxis.set_ticks_position('left')
        ax3.set_xlabel(r"$\xi $", fontsize=15)
        ax3.set_ylabel(r"$\eta$", rotation=0, fontsize=15)
        ax3.xaxis.set_label_coords(1.05, 0.475)
        ax3.yaxis.set_label_coords(0.475, 1.05)
        ax3.set_yticks([-1, -0.5, 0.5, 1])
        ax3.set_xticks([-1, -0.5, 0.5, 1])
        ax3.set_aspect("equal")
        fig3.show()
        save = True
        if save or save_all:
            fig.savefig(fig_folder + file_name, **kwargs_savefig)
            fig2.savefig(
                fig_folder + file_name.split(".")[0] + "_parameter.pdf",
                **kwargs_savefig)
            fig3.savefig(fig_folder + file_name.split(".")[0] + "_element.pdf",
                         **kwargs_savefig)
Ejemplo n.º 5
0
    def plot(B, knots, file_name):
        from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface
        fig, [ax2, ax] = plt.subplots(1,
                                      2,
                                      figsize=(10, 5),
                                      constrained_layout=True)
        ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k")
        ax = p_surface(knots,
                       B,
                       ax=ax,
                       dim=2,
                       color="blue",
                       border=False,
                       alpha=0.5)
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)
        ax.set_xlabel("$x$")
        ax.set_ylabel("$y$")
        ax.set_aspect(0.8)

        ax2 = p_cpoints(B,
                        ax=ax2,
                        dim=2,
                        color="blue",
                        linestyle="-",
                        point=False,
                        line=True)
        ax2 = p_cpoints(B,
                        ax=ax2,
                        dim=2,
                        color="red",
                        marker="o",
                        point=True,
                        line=False)
        n, m = B.shape[0], B.shape[1]
        ax2.spines["right"].set_visible(False)
        ax2.spines["top"].set_visible(False)
        ax2.set_xlabel("$x$")
        ax2.set_ylabel("$y$", rotation=90)
        ax2.set_aspect(0.8)

        ax.set_title("Physical space ($x,y$)", fontsize=20)
        ax2.set_title("Control net", fontsize=20)
        fig.tight_layout(pad=0, h_pad=0, w_pad=0)
        fig.show()

        from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
        fig3 = plt.figure(constrained_layout=True)
        gs = fig3.add_gridspec(2,
                               2,
                               hspace=0,
                               wspace=0,
                               width_ratios=[0.2, 1],
                               height_ratios=[1, 0.2])
        (ax_v, ax3), (no, ax_u) = gs.subplots(sharex=True, sharey=True)
        no.remove()
        N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                                   degree=2,
                                                   resolution=100)
        N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                                   degree=2,
                                                   resolution=100)
        resol = np.linspace(0, 1, 100)

        ax_u.plot(resol, N_spline_u)
        ax_u.spines["top"].set_visible(False)
        ax_u.spines["right"].set_visible(False)

        ax_u.set_xlim(0, 1)
        ax_u.set_ylim(0, 1)

        ax_v.plot(N_spline_v, resol)
        ax_v.spines["top"].set_visible(False)
        ax_v.spines["right"].set_visible(False)
        ax_v.set_yticks(knots[2:-2])
        ax_v.set_xlim(1, 0)
        ax_v.set_ylim(0, 1)

        for i in knots[0]:
            ax3.vlines(i, 0, 1, 'k')
        for j in knots[1]:
            ax3.hlines(j, 0, 1, 'k')
        ax3.set_xlim(0, 1)
        ax3.set_ylim(0, 1)
        ax3.set_axis_off()
        ax3.set_title("Parametric space ($u,v$)", fontsize=20)

        ax_u.set_xlabel("$u$")
        ax_v.set_ylabel("$v$")
        ax_v.set_yticks(knots[1][2:-2])
        ax_u.set_xticks(knots[0][2:-2])
        for ax in ax_u, ax_v, ax3, no:
            ax.label_outer()
        fig3.show()

        save = False
        if save or save_all:
            fig.savefig(fig_folder + file_name, **kwargs_savefig)
            fig3.savefig(
                fig_folder + file_name.split(".")[0] + "_parameter.pdf",
                **kwargs_savefig)
def test_image_example2():

    from pygeoiga.nurb.cad import make_surface_biquadratic
    knots, B = make_surface_biquadratic()

    from pygeoiga.nurb.refinement import knot_insertion
    knot_ins_1 = [0.3, 0.6]
    knot_ins_0 = [0.25, 0.75]
    B, knots = knot_insertion(B, (2, 2), knots, knot_ins_0, direction=0)
    B, knots = knot_insertion(B, (2, 2), knots, knot_ins_1, direction=1)
    before = B
    from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface
    from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
    fig = plt.figure(constrained_layout=True)
    gs = fig.add_gridspec(3,
                          3,
                          hspace=0,
                          wspace=0,
                          width_ratios=[0.2, 0.2, 1],
                          height_ratios=[1, 0.2, 0.2])
    (ax_bv, ax_v, ax2), (no1, no2, ax_u), (no3, no4,
                                           ax_bu) = gs.subplots(sharex=True,
                                                                sharey=True)
    for no in no1, no2, no3, no4:
        no.remove()

    N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                               degree=2,
                                               resolution=100)
    N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                               degree=2,
                                               resolution=100)
    resol = np.linspace(0, 1, 100)

    ax_u.plot(resol, N_spline_u)
    ax_u.spines["top"].set_visible(False)
    ax_u.spines["right"].set_visible(False)

    ax_u.set_xlim(0, 1)
    ax_u.set_ylim(0, 1)

    ax_v.plot(N_spline_v, resol)
    ax_v.spines["top"].set_visible(False)
    ax_v.spines["right"].set_visible(False)
    ax_v.set_yticks(knots[2:-2])
    ax_v.set_xlim(0, 1)
    ax_v.set_ylim(0, 1)

    ax_u.set_xlabel("$u$")
    ax_v.set_ylabel("$v$")

    ax_v.set_xlabel("$N(v)$")
    ax_u.set_ylabel("$N(u)$")

    ax_v.set_yticks(knots[1][2:-2])
    ax_u.set_xticks(knots[0][2:-2])

    for i in knots[0]:
        ax2.vlines(i, 0, 1, 'k')
    for j in knots[1]:
        ax2.hlines(j, 0, 1, 'k')
    ax2.set_xlim(0, 1)
    ax2.set_ylim(0, 1)
    ax2.set_axis_off()

    fig2, ax = plt.subplots(constrained_layout=True)
    ax = p_cpoints(B,
                   ax=ax,
                   dim=2,
                   point=False,
                   line=True,
                   color="black",
                   linestyle="-")
    ax = p_cpoints(B, ax=ax, dim=2, point=True, line=False, color="red")
    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)
    ax.set_xlabel("$x$")
    ax.set_ylabel("$y$")
    ax.set_aspect(0.8)
    fig2.tight_layout(pad=0, h_pad=0, w_pad=0)
    fig2.show()

    from pygeoiga.nurb.refinement import knot_insertion
    knot_ins_1 = [0.3, 0.6]
    knot_ins_0 = [0.25, 0.5, 0.75]
    B, knots = knot_insertion(B, (2, 2), knots, knot_ins_0, direction=0)
    B, knots = knot_insertion(B, (2, 2), knots, knot_ins_1, direction=1)
    new = B
    N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                               degree=2,
                                               resolution=100)
    N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                               degree=2,
                                               resolution=100)
    resol = np.linspace(0, 1, 100)

    ax_bu.plot(resol, N_spline_u)
    ax_bu.spines["top"].set_visible(False)
    ax_bu.spines["right"].set_visible(False)

    ax_bu.set_xlim(0, 1)
    ax_bu.set_ylim(0, 1)

    ax_bv.plot(N_spline_v, resol)
    ax_bv.spines["top"].set_visible(False)
    ax_bv.spines["right"].set_visible(False)
    ax_bv.set_yticks(knots[2:-2])
    ax_bv.set_xlim(0, 1)
    ax_bv.set_ylim(0, 1)

    ax_bu.set_xlabel("$u$")
    ax_bv.set_ylabel("$v$")
    ax_bv.set_xlabel("$B(v)$")
    ax_bu.set_ylabel("$B(u)$")
    ax_bv.set_yticks(knots[1][2:-2])
    ax_bu.set_xticks(knots[0][2:-2])

    #for ax in ax_u, ax_v, ax2, no, ax_bu, ax_bv:
    #    ax.label_outer()
    fig2.show()

    degree_u = len(np.where(knots[0] == 0.)[0]) - 1
    degree_v = len(np.where(knots[1] == 0.)[0]) - 1

    n_xi = len(knots[0]) - degree_u - 3
    n_eta = len(knots[1]) - degree_v - 3

    from pygeoiga.analysis.common import IEN_element_topology, transform_matrix_B
    IEN = IEN_element_topology(n_xi, n_eta, degree_u)
    P, W = transform_matrix_B(B)

    fig3, ax = plt.subplots(constrained_layout=True)
    for i in range(0, n_xi, 2):
        pos = IEN[i::n_xi]
        for e in pos[::2]:
            # cont = np.hstack([IEN[0][:degree_u+1], IEN[0][degree_u+3], np.flip(IEN[0][-degree_u-1:]), IEN[0][degree_u+1], IEN[0][0]])
            cont = np.hstack([
                e[:degree_u + 1], e[degree_u + 3],
                np.flip(e[-degree_u - 1:]), e[degree_u + 1], e[0]
            ])
            ax.plot(P[cont][:, 0],
                    P[cont][:, 1],
                    linestyle="-",
                    color="black",
                    marker=None)
    ax = p_cpoints(B, ax=ax, dim=2, point=True, line=False, color="red")
    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)
    ax.set_xlabel("$x$")
    ax.set_ylabel("$y$")
    ax.set_aspect(0.8)
    fig3.tight_layout(pad=0, h_pad=0, w_pad=0)
    fig.show()

    fig2.show()
    fig3.show()

    fig_0, ax = plt.subplots(constrained_layout=True)
    ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k")

    ax = p_surface(knots,
                   B,
                   ax=ax,
                   dim=2,
                   fill=True,
                   border=False,
                   color="gray",
                   alpha=0.5)
    ax.spines["right"].set_visible(False)
    ax.spines["top"].set_visible(False)
    ax.set_xlabel("$x$")
    ax.set_ylabel("$y$")
    ax.set_aspect(0.8)
    fig_0.tight_layout(pad=0, h_pad=0, w_pad=0)
    fig_0.show()

    save = False
    if save or save_all:
        fig.savefig(fig_folder + "bezier_extraction.pdf", **kwargs_savefig)
        fig_0.savefig(fig_folder + "curve_original.pdf", **kwargs_savefig)
        fig2.savefig(fig_folder + "control_original.pdf", **kwargs_savefig)
        fig3.savefig(fig_folder + "control_bezier.pdf", **kwargs_savefig)
    def plot(B, knots, file_name):
        from pygeoiga.plot.nrbplotting_mpl import create_figure, p_cpoints, p_knots, p_curve, p_surface
        fig, ax = plt.subplots(constrained_layout=True)

        ax = p_knots(knots, B, ax=ax, dim=2, point=False, line=True, color="k")
        ax = p_cpoints(B, ax=ax, dim=2, point=True, line=True, color="red")
        ax.spines["right"].set_visible(False)
        ax.spines["top"].set_visible(False)
        ax.set_xlabel("$x$")
        ax.set_ylabel("$y$")
        ax.set_aspect(0.8)
        fig.tight_layout(pad=0, h_pad=0, w_pad=0)
        fig.show()

        from pygeoiga.engine.NURB_engine import basis_function_array_nurbs
        fig2 = plt.figure(constrained_layout=True)
        gs = fig2.add_gridspec(2,
                               2,
                               hspace=0,
                               wspace=0,
                               width_ratios=[0.2, 1],
                               height_ratios=[1, 0.2])
        (ax_v, ax2), (no, ax_u) = gs.subplots(sharex=True, sharey=True)
        no.remove()
        N_spline_u, _ = basis_function_array_nurbs(knot_vector=knots[0],
                                                   degree=2,
                                                   resolution=100)
        N_spline_v, _ = basis_function_array_nurbs(knot_vector=knots[1],
                                                   degree=2,
                                                   resolution=100)
        resol = np.linspace(0, 1, 100)

        ax_u.plot(resol, N_spline_u)
        ax_u.spines["top"].set_visible(False)
        ax_u.spines["right"].set_visible(False)

        ax_u.set_xlim(0, 1)
        ax_u.set_ylim(0, 1)

        ax_v.plot(N_spline_v, resol)
        ax_v.spines["top"].set_visible(False)
        ax_v.spines["right"].set_visible(False)
        ax_v.set_yticks(knots[2:-2])
        ax_v.set_xlim(1, 0)
        ax_v.set_ylim(0, 1)

        for i in knots[0]:
            ax2.vlines(i, 0, 1, 'k')
        for j in knots[1]:
            ax2.hlines(j, 0, 1, 'k')
        ax2.set_xlim(0, 1)
        ax2.set_ylim(0, 1)
        ax2.set_axis_off()

        ax_u.set_xlabel("$u$")
        ax_v.set_ylabel("$v$")
        ax_v.set_yticks(knots[1][2:-2])
        ax_u.set_xticks(knots[0][2:-2])
        for ax in ax_u, ax_v, ax2, no:
            ax.label_outer()
        fig2.show()

        save = False
        if save or save_all:
            fig2.savefig(fig_folder + file_name, **kwargs_savefig)