Ejemplo n.º 1
0
def ex_resolution_raw(max_max_area=2, min_max_area=0.01):
    elem_dict = dict(D=STEEL_D)
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    bottom_traction = -5e7
    x1, y1, simplices1 = mesh.generate_and_import_mesh(contour,
                                                       max_area=max_max_area)
    mask1 = x1 == np.amin(x1)
    vals1 = 0

    # We apply a downwards nodal force on the right side
    mask2 = x1 == np.amax(x1)
    A_n = calc_hat_area(y1[mask2])
    vals2 = np.zeros((A_n.size, 2))
    vals2[:, 1] = A_n * bottom_traction

    x_displacement, y_displacement = fem.FEM(x1,
                                             y1,
                                             simplices1,
                                             create_element_matrix,
                                             func_source,
                                             mask1,
                                             vals1,
                                             mask2,
                                             vals2,
                                             elem_dict=elem_dict)
    x1_new = x1 + x_displacement
    y1_new = y1 + y_displacement

    np.save('raw_exps1', [x1, y1, x1_new, y1_new, simplices1])
Ejemplo n.º 2
0
def create_mesh(min_angle=0, max_area=0.1):
    contours = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    x, y, simplices = mesh.generate_and_import_mesh(contours,
                                                    min_angle=min_angle,
                                                    max_area=max_area,
                                                    save_files=False,
                                                    print_triangle=False)
    return x, y, simplices
Ejemplo n.º 3
0
def ex_squares():
    poissons = [0.1, 0.3, 0.5]
    max_areas = [0.5, 0.1, 0.01]
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    save_file = 'squares'
    poissons2, max_areas2 = np.meshgrid(poissons, max_areas)

    num = 100
    min_load = 0
    max_load = -5e7
    areas = np.zeros((num, *poissons2.shape))
    loads = np.linspace(min_load, max_load, num=num)
    bar = Bar('simulating', max=areas.size)
    for i in range(poissons2.shape[0]):
        for j in range(poissons2.shape[1]):
            x, y, simplices = mesh.generate_and_import_mesh(
                contour, max_area=max_areas2[i, j])
            elem_dict = dict(D=D(nu=poissons2[i, j]))
            mask1 = x == np.amin(x)
            vals1 = 0
            # We apply a downwards nodal force on the right side
            mask2 = x == np.amax(x)
            A_n = calc_hat_area(y[mask2])
            vals2 = np.zeros((A_n.size, 2))
            vals2[:, 1] = A_n

            K = fem.assemble_global_matrix(x,
                                           y,
                                           simplices,
                                           create_element_matrix,
                                           elem_dict=elem_dict)
            f = fem.create_f_vector(x, y, simplices, func_source)
            K, f = fem.add_point_boundary(K, f, mask1, vals1)
            # bar = Bar('Simulating', max=num)
            for n, load in enumerate(loads):
                vals = vals2 * load
                f = fem.create_f_vector(x, y, simplices, func_source)
                f = fem.add_to_source(f, mask2, vals)
                u = np.linalg.solve(K, f)
                x_disp, y_disp = fem.unpack_u(u)
                x_new = x + x_disp
                y_new = y + y_disp
                triangles = mesh.all_triangles(simplices, x_new, y_new)
                area = np.sum(fem.calc_areas(triangles))
                areas[n, i, j] = area
                np.save(save_file, areas)
                bar.next()
    bar.finish()
Ejemplo n.º 4
0
def ex_resolution(max_max_area=2, min_max_area=0.01, num=50):
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    end_area = np.zeros(num)
    end_positions = np.zeros((num, 2))
    bottom_traction = -5e7
    max_areas = np.logspace(np.log2(max_max_area),
                            np.log2(min_max_area),
                            num=num,
                            base=2)
    savefile = 'areas'
    savefile2 = 'positions'
    bar = Bar('Simulate', max=num)
    for i, area in enumerate(max_areas):
        x, y, simplices = mesh.generate_and_import_mesh(contour, max_area=area)
        bottom_right_index = (x == np.amax(x)) * (y == np.amin(y))
        mask1 = x == np.amin(x)
        vals1 = 0

        # Get the element matric keyword arguments
        elem_dict = dict(D=STEEL_D)

        # We apply a downwards nodal force on the right side
        mask2 = x == np.amax(x)
        A_n = calc_hat_area(y[mask2])
        vals2 = np.zeros((A_n.size, 2))
        vals2[:, 1] = A_n * bottom_traction

        x_displacement, y_displacement = fem.FEM(x,
                                                 y,
                                                 simplices,
                                                 create_element_matrix,
                                                 func_source,
                                                 mask1,
                                                 vals1,
                                                 mask2,
                                                 vals2,
                                                 elem_dict=elem_dict)
        x_new = x + x_displacement
        y_new = y + y_displacement
        triangles_after = mesh.all_triangles(simplices, x_new, y_new)
        end_area[i] = np.sum(fem.calc_areas(triangles_after))
        end_positions[i, :] = [
            x_new[bottom_right_index], y_new[bottom_right_index]
        ]
        np.save(savefile, end_area)
        np.save(savefile2, end_positions)
        bar.next()
    bar.finish()
Ejemplo n.º 5
0
def ex_with_external2():
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    bottom_traction = -5e8
    x, y, simplices = mesh.generate_and_import_mesh(contour, max_area=0.1)

    # Clamp the left side
    mask1 = x == np.amin(x)
    vals1 = 0

    # Get the element matric keyword arguments
    elem_dict = dict(D=STEEL_D)

    # We apply a downwards nodal force on the right side
    mask2 = x == np.amax(x)
    A_n = calc_hat_area(y[mask2])
    vals2 = np.zeros((A_n.size, 2))
    vals2[:, 1] = A_n * bottom_traction

    x_displacement, y_displacement = fem.FEM(x,
                                             y,
                                             simplices,
                                             create_element_matrix,
                                             func_source,
                                             mask1,
                                             vals1,
                                             mask2,
                                             vals2,
                                             elem_dict=elem_dict)
    x_new = x + x_displacement
    y_new = y + y_displacement
    fig, ax = plt.subplots(figsize=(4, 3))
    ax.triplot(x, y, simplices, color='r')
    ax.triplot(x_new, y_new, simplices, color='b')
    ax.set_title(f'Linear deformation of steel. $t=5\cdot 10^8$ N/m')
    ax.set_aspect('equal')
    ax.set_xlabel('x, [m]')
    ax.set_ylabel('y, [m]')
    fig.tight_layout()
    # triangles_before = mesh.all_triangles(simplices, x, y)
    # triangles_after = mesh.all_triangles(simplices, x_new, y_new)
    # area_before = np.sum(fem.calc_areas(triangles_before))
    # area_after = np.sum(fem.calc_areas(triangles_after))
    # print(area_before)
    # print(area_after)
    # np.save('raw_exps2', [x, y, x_new, y_new, simplices])
    fig.savefig('handin/raw.pdf')
Ejemplo n.º 6
0
def ex_load(min_load=0, max_load=-5e7, num=100, max_area=0.01):
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    x, y, simplices = mesh.generate_and_import_mesh(contour, max_area=max_area)

    start_area = 12
    savefile = 'loads'
    areas = np.zeros(num)
    loads = np.linspace(min_load, max_load, num=num)

    mask1 = x == np.amin(x)
    vals1 = 0

    # Get the element matric keyword arguments
    elem_dict = dict(D=STEEL_D)

    # We apply a downwards nodal force on the right side
    mask2 = x == np.amax(x)
    A_n = calc_hat_area(y[mask2])
    vals2 = np.zeros((A_n.size, 2))
    vals2[:, 1] = A_n

    K = fem.assemble_global_matrix(x,
                                   y,
                                   simplices,
                                   create_element_matrix,
                                   elem_dict=elem_dict)
    f = fem.create_f_vector(x, y, simplices, func_source)
    K, f = fem.add_point_boundary(K, f, mask1, vals1)
    # bar = Bar('Simulating', max=num)
    for i, load in enumerate(loads):
        vals = vals2 * load
        f = fem.create_f_vector(x, y, simplices, func_source)
        f = fem.add_to_source(f, mask2, vals)
        u = np.linalg.solve(K, f)
        x_disp, y_disp = fem.unpack_u(u)
        x_new = x + x_disp
        y_new = y + y_disp
        triangles = mesh.all_triangles(simplices, x_new, y_new)
        area = np.sum(fem.calc_areas(triangles))
        areas[i] = area
        np.save(savefile, [loads, areas])
Ejemplo n.º 7
0
def create_ball(r=1, y0=0, N=20, max_area=0.1, min_angle=30, plot=False):
    """
    Creates a ball mesh with radius r and offset y, using a regular N-sided
    polygon.
    
    """
    name = 'ball'
    theta = np.linspace(0, 2 * np.pi, N, endpoint=False)
    x = r * np.cos(theta)
    y = y0 + r * np.sin(theta)
    points = np.array((x, y)).T
    x, y, T = mesh.generate_and_import_mesh([points],
                                            min_angle=min_angle,
                                            max_area=max_area)
    fvm.write_to_mat(x, y)
    fvm.call_matlab(name)
    if plot:
        x, y, T, cvs = fvm.load_cvs_mat(f'{name}.mat')
        fig, ax = plt.subplots()
        ax.triplot(x, y, T)
        fig.tight_layout()
        plt.show()
Ejemplo n.º 8
0
def ex_show_loads():
    contour = [np.array([[0, 0], [6, 0], [6, 2], [0, 2]])]
    x, y, simplices = mesh.generate_and_import_mesh(contour, max_area=0.01)

    load = -5e7

    mask1 = x == np.amin(x)
    vals1 = 0

    # Get the element matric keyword arguments
    elem_dict = dict(D=STEEL_D)

    # We apply a downwards nodal force on the right side
    mask2 = x == np.amax(x)
    A_n = calc_hat_area(y[mask2])
    vals2 = np.zeros((A_n.size, 2))
    vals2[:, 1] = A_n

    K = fem.assemble_global_matrix(x,
                                   y,
                                   simplices,
                                   create_element_matrix,
                                   elem_dict=elem_dict)
    f = fem.create_f_vector(x, y, simplices, func_source)
    K, f = fem.add_point_boundary(K, f, mask1, vals1)
    vals = vals2 * load
    f = fem.add_to_source(f, mask2, vals)
    u = np.linalg.solve(K, f)
    x_disp, y_disp = fem.unpack_u(u)
    x_new = x + x_disp
    y_new = y + y_disp
    fig, ax = plt.subplots()
    ax.triplot(x, y, simplices)
    ax.triplot(x_new, y_new, simplices)
    triangles = mesh.all_triangles(simplices, x_new, y_new)
    area = np.sum(fem.calc_areas(triangles))
    print(area)
    plt.show()