def fe_basis_function_figure(d,
                             target_elm=[1],
                             N_e=3,
                             derivative=0,
                             filename='tmp.pdf',
                             labels=False):
    """
    Draw all basis functions (or their derivative), of degree d,
    associated with element target_elm (may be list of elements).
    Add a mesh with N_e elements.
    """
    nodes, elements = mesh_uniform(N_e, d)
    """
    x = 1.1
    print locate_element_vectorized(x, elements, nodes)
    print locate_element_scalar(x, elements, nodes)
    x = 0.1, 0.4, 0.8
    print locate_element_vectorized(x, elements, nodes)
    """
    if isinstance(target_elm, int):
        target_elm = [target_elm]  # wrap in list

    # Draw the basis functions for element 1
    colors = ['r-2', 'b-2', 'g-2', 'y-2', 'k-2', 'c-2']
    color_counter = 0
    phi_drawn = []  # list of already drawn phi functions
    ymin = ymax = 0
    for e in target_elm:
        for i in elements[e]:
            if not i in phi_drawn:
                x, y = phi_glob(i, elements, nodes, derivative=derivative)
                if x is None and y is None:
                    return  # abort
                ymax = max(ymax, max(y))
                ymin = min(ymin, min(y))
                plt.plot(x, y, colors[color_counter])
                color_counter += 1
                if color_counter > len(colors) - 1:
                    color_counter = 0  # restart colors

                if labels:
                    if derivative == 0:
                        plt.legend(r'\varphi_%d' % i, loc='upper right')
                    else:
                        plt.legend(r"\varphi_%d'(x)" % i, loc='upper right')
                phi_drawn.append(i)

    #plt.axis([nodes[0], nodes[-1], ymin-0.1, ymax+0.1])
    plot_fe_mesh(nodes, elements, element_marker=[ymin - 0.1, ymax + 0.1])
    plt.plot([0, 0], [0, 0], '-')
    plt.axis([nodes[0], nodes[-1], ymin - 0.1, ymax + 0.1])
    #plt.axes.set_aspect('')
    #plt.daspect([0.2, 1, 1])
    #plt.daspactmode('manual')
    plt.savefig(filename)
Beispiel #2
0
def fe_basis_function_figure(d,
                             target_elm=[1],
                             N_e=3,
                             derivative=0,
                             filename='tmp.pdf',
                             labels=False):
    """
    Draw all basis functions (or their derivative), of degree d,
    associated with element target_elm (may be list of elements).
    Add a mesh with N_e elements.
    """
    nodes, elements = mesh_uniform(N_e, d)
    """
    x = 1.1
    print locate_element_vectorized(x, elements, nodes)
    print locate_element_scalar(x, elements, nodes)
    x = 0.1, 0.4, 0.8
    print locate_element_vectorized(x, elements, nodes)
    """
    if isinstance(target_elm, int):
        target_elm = [target_elm]  # wrap in list

    # Draw the basis functions for element 1
    phi_drawn = []  # list of already drawn phi functions
    ymin = ymax = 0
    for e in target_elm:
        for i in elements[e]:
            if not i in phi_drawn:
                x, y = phi_glob(i, elements, nodes, derivative=derivative)
                if x is None and y is None:
                    return  # abort
                ymax = max(ymax, max(y))
                ymin = min(ymin, min(y))
                plt.plot(x, y, '-')
                plt.hold('on')
                if labels:
                    if plt.backend == 'gnuplot':
                        if derivative == 0:
                            plt.legend(r'basis func. %d' % i)
                        else:
                            plt.legend(r'derivative of basis func. %d' % i)
                    elif plt.backend == 'matplotlib':
                        if derivative == 0:
                            plt.legend(r'\varphi_%d' % i)
                        else:
                            plt.legend(r"\varphi_%d'(x)" % i)
                phi_drawn.append(i)

    plt.axis([nodes[0], nodes[-1], ymin - 0.1, ymax + 0.1])
    plot_fe_mesh(nodes, elements, element_marker=[ymin - 0.1, ymax + 0.1])
    plt.hold('off')
    plt.savefig(filename)
Beispiel #3
0
def fe_basis_function_figure(d, target_elm=[1], n_e=3,
                             derivative=0, filename='tmp.pdf',
                             labels=False):
    """
    Draw all basis functions (or their derivative), of degree d,
    associated with element target_elm (may be list of elements).
    Add a mesh with n_e elements.
    """
    nodes, elements = mesh_uniform(n_e, d)
    """
    x = 1.1
    print locate_element_vectorized(x, elements, nodes)
    print locate_element_scalar(x, elements, nodes)
    x = 0.1, 0.4, 0.8
    print locate_element_vectorized(x, elements, nodes)
    """
    if isinstance(target_elm, int):
        target_elm = [target_elm]  # wrap in list

    # Draw the basis functions for element 1
    phi_drawn = []  # list of already drawn phi functions
    ymin = ymax = 0
    for e in target_elm:
        for i in elements[e]:
            if not i in phi_drawn:
                x, y = phi_glob(i, elements, nodes,
                                derivative=derivative)
                if x is None and y is None:
                    return  # abort
                ymax = max(ymax, max(y))
                ymin = min(ymin, min(y))
                plt.plot(x, y, '-')
                plt.hold('on')
                if labels:
                    if plt.backend == 'gnuplot':
                        if derivative == 0:
                            plt.legend(r'basis function no. %d' % i)
                        else:
                            plt.legend(r'derivative of basis function no. %d' % i)
                    elif plt.backend == 'matplotlib':
                        if derivative == 0:
                            plt.legend(r'\varphi_%d' % i)
                        else:
                            plt.legend(r"\varphi_%d'(x)" % i)
                phi_drawn.append(i)

    plt.axis([nodes[0], nodes[-1], ymin-0.1, ymax+0.1])
    plot_fe_mesh(nodes, elements, element_marker=[ymin-0.1, ymax+0.1])
    plt.hold('off')
    plt.savefig(filename)
Beispiel #4
0
    plt.savefig('tmp_u_P1_welms.pdf')
    plt.show()


if __name__ == '__main__':
    import sys
    if len(sys.argv) == 1:
        print 'Usage: %s phi | pattern | u_sines | u_P1 [num_elements] [d] [uniform | random]' % sys.argv[0]
        sys.exit(1)
    if sys.argv[1] == 'phi':
        draw_basis_functions()
    elif sys.argv[1] == 'pattern':
        num_elements = int(sys.argv[2])
        d = int(sys.argv[3])
        uniform = sys.argv[4]
        nodes, elements = mesh_uniform(num_elements, d, [0, 1])
        num_nodes = len(nodes)

        # Shuffle nodes in random order if necessary
        if uniform == 'random':
            global_node_numbering = range(0, num_nodes)
            import random
            random.shuffle(global_node_numbering)
            for e in range(len(elements)):
                for r in range(len(elements[e])):
                    elements[e][r] = global_node_numbering[elements[e][r]]
        draw_sparsity_pattern(elements, num_nodes)
    elif sys.argv[1] == 'u_sines':
        u_sines()
    elif sys.argv[1] == 'u_P1':
        u_P1()
Beispiel #5
0
    plt.savefig('u_example_P1_welms.pdf')
    plt.show()


if __name__ == '__main__':
    import sys
    if len(sys.argv) == 1:
        print('Usage: %s phi | u_sines | u_P1 | pattern [num_elements] [d] [uniform | random]' % sys.argv[0])
        sys.exit(1)
    if sys.argv[1] == 'phi':
        draw_basis_functions()
    elif sys.argv[1] == 'pattern':
        num_elements = int(sys.argv[2])
        d = int(sys.argv[3])
        uniform = sys.argv[4]
        nodes, elements = mesh_uniform(num_elements, d, [0, 1])
        num_nodes = len(nodes)

        # Shuffle nodes in random order if necessary
        if uniform == 'random':
            global_node_numbering = list(range(0, num_nodes))
            import random
            random.shuffle(global_node_numbering)
            for e in range(len(elements)):
                for r in range(len(elements[e])):
                    elements[e][r] = global_node_numbering[elements[e][r]]
        draw_sparsity_pattern(elements, num_nodes)
    elif sys.argv[1] == 'u_sines':
        u_sines()
    elif sys.argv[1] == 'u_P1':
        u_P1()