Finite element method in 2D

FEM 2D

fem_2d.main(mesh_file, nelx, nely, lx, ly, force_matrix, restri_matrix=None, E=210000000000.0, v=0.3, rho=7860, alpha=0, beta=0, eta=0, factor=1000, freq=180, node_plot=None, freq_rsp=[], save=False, timing=False)
Parameters
  • nelx (int) – Number of elements on the x-axis.

  • nely (int) – Number of elements on the y-axis.

  • lx (float) – x-axis length.

  • ly (float) – y-axis length.

  • force_matrix (numpy.array) –

    It’s a list of lists. The list can be:

    • [x_coordinate, y_coordinate, force_applied_x, force_applied_y, force_value]

    • [value_coordinate, column_to_compare, force_applied_x, force_applied_y, force_value, error_margin]

  • restri_matrix (numpy.array, optional) –

    It’s a list of lists. Defaults to None.

    • [x_coordinate, y_coordinate, constrain_disp_x, constrain_disp_y]

    • [value_coordinate, column_to_compare, constrain_disp_x, constrain_disp_y, error_margin]

  • E (float, optional) – Elastic modulus. Defaults to 210e9.

  • v (float, optional) – Poisson’s ratio. Defaults to 0.3.

  • rho (float, optional) – Density. Defaults to 7860.

  • alpha (float, optional) – Damping coefficient proportional to mass. Defaults to 0.

  • beta (float, optional) – Damping coefficient proportional to stiffness. Defaults to 0.

  • eta (float, optional) – Damping coefficient. Defaults to 0.

  • factor (float, optional) – Factor to deform the mesh. Defaults to 1000.

  • freq (int, optional) – Optimized frequency. Defaults to 180.

  • node_plot (list, optional) –

    The node coordinate to plot the frequency response graph. Defaults to None.

    • The elements are respectively node, x direction, y direction.

    • If None the first element of the force matrix and the z direction is used.

  • freq_rsp (list, optional) –

    If len is 3, a frequency response graph of the original and optimized structure is generated. Defaults to [].

    • First value is the minimum frequency of the graph.

    • Second value is the maximum frequency of the graph.

    • Third value is the step between each calculation of the objective function.

  • save (bool, optional) – if True save the optimization and frequency response graphs as PNG. Defaults to False.

  • timing (bool, optional) – If True shows the process optimization time. Defaults to False.

General functions

functions_2d.apply_U(disp_vector, coord, factor)

Apply displacement to coordinates.

Parameters
  • disp_vector (numpy.array) – Displacement.

  • coord (numpy.array) – mesh coordinates.

  • factor (float) – Factor to deform the mesh.

Returns

Displaced mesh coordinates.

functions_2d.change_U_shape(disp_vector)

Transform displacement vector in matrix.

Parameters

disp_vector (numpy.array) – Displacement.

Returns

Displacement of each axis.

functions_2d.duplicate_force(force_matrix)

Doubled force value for x direction = y direction = 1.

Parameters

force_matrix (numpy.array) – The columns are respectively node, x direction, y direction, force value.

Returns

Load.

functions_2d.freqresponse(coord, connect, ind_rows, ind_cols, nelx, nely, E, v, rho, alpha, beta, eta, freq_rsp, node_plot, load_vector, free_ind)

Get the displacement values for a specific node.

Parameters
  • coord (numpy.array) – Coordinates of the element.

  • connect (numpy.array) – Element connectivity.

  • ind_rows (numpy.array) – Node indexes to make the assembly.

  • ind_cols (numpy.array) – Node indexes to make the assembly.

  • nelx (int) – Number of elements on the X-axis.

  • nely (int) – Number of elements on the Y-axis.

  • E (float) – Elastic modulus.

  • v (float) – Poisson’s ratio.

  • rho (float) – Density.

  • alpha (float) – Damping coefficient proportional to mass.

  • beta (float) – Damping coefficient proportional to stiffness.

  • eta (float) – Damping coefficient.

  • freq_rsp (list) – Frequency range. First value is the minimum frequency. Second value is the maximum frequency. Third value is the step between each calculation of the objective function.

  • node_plot (int) – Node to salve the displacement.

  • load_vector (numpy.array) – Load.

Returns

Displacement array.

functions_2d.generate_ind_rows_cols(connect)

Node indexes to make the assembly

Parameters

connect (numpy.array) – Element connectivity.

Returns

Node indexes to make the assembly

functions_2d.generate_xy_coord(lx, ly, nelx, nely)

Create the mesh coordinate range

Parameters
  • lx (float) – X-axis length.

  • ly (float) – Y-axis length.

  • nelx (int) – Number of elements on the X-axis.

  • nely (int) – Number of elements on the Y-axis.

Returns

Tuple with range x and y.

functions_2d.get_dofs(nodes_dir)

Get DOFs that meet the specified direction.

Parameters

nodes_dir (numpy.array) –

[nodes numbers, x direction, y direction].

  • x direction and y direction can be -1, 0 or 1.

Returns

DOFs of each node in array nodes.

functions_2d.get_load_vector(nelx, nely, force_matrix)

Creates the force vector.

Parameters
  • nelx (int) – Number of elements on the x-axis.

  • nely (int) – Number of elements on the y-axis.

  • force_matrix (numpy.array) – The columns are respectively node, x direction, y direction, force value.

Returns

Loading vector.

functions_2d.get_matrices(matrix, coord, force)

Gets the force matrix or the contraint matrix.

Parameters
  • matrix (list) – List passed by the user.

  • coord (numpy.array) – Coordinates of the element.

  • force (bool) – True if encountering force matrix.

Returns

force_matrix or restri_matrix.

functions_2d.get_matrix(nodes_coord, nodes_col, np_matrix, index_by_coord, index_by_col, ind1, ind2, total_cols)

Creates the node matrix.

Parameters
  • nodes_coord (list) – Nodes passed by coordinate.

  • nodes_col (list) – Nodes passed by column.

  • coord (numpy.array) – Coordinates of the element.

  • matrix (numpy.array) – List passed to an array.

  • index_by_coord (list) – indices of elements passed by coordinates.

  • index_by_col (list) – indices of elements passed by columns.

  • ind1 (int) – Indices of matrix with nodes.

  • ind2 (int) – Indices of matrix passed by user.

  • total_cols (int) – Number of columns desired for the matrix.

Returns

matrix with nodes.

functions_2d.get_nodes(coord, np_matrix, index_by_coord, index_by_col)

Gets nodes by a coordinate or a column.

Parameters
  • coord (numpy.array) – Coordinates of the element.

  • np_matrix (numpy.array) – List passed to an array.

  • index_by_coord (list) – indices of elements passed by coordinates.

  • index_by_col (list) – indices of elements passed by columns.

  • ind (int) – The column that has the margin of error.

Returns

Nodes.

functions_2d.get_nodes1d(coord, coord_user, eps, column)

Get node numbers that are equal to coord.

Parameters
  • coord (numpy.array) – mesh coordinates.

  • coord_user (numpy.array) – coordinates in one direction (x or y).

  • eps (float) – Acceptable margin of difference.

  • column (int) – Direction to compare (x or y). x = 1 and y = 2.

Returns

Nodes.

functions_2d.get_nodes_by_coord(coord, coord_user)

Get node number by coordinate.

Parameters
  • coord (numpy.array) – mesh coordinates.

  • coord_user (numpy.array) – user coordinates.

Returns

Nodes of the coordinates provided.

functions_2d.get_num_el(coord)

Get number of elements in mesh.

Parameters

coord (numpy.array) – Coordinates of the element.

Returns

Number of elements in x and y.

functions_2d.get_size_el(coord)

Get size of mesh.

Parameters

coord (numpy.array) – Coordinates of the element.

Returns

Size of mesh.

functions_2d.harmonic_solution(stif_matrix, mass_matrix, alpha, beta, eta, freq, ngl, free_ind, load_vector, timing=False)

Direct method and no damping

Parameters
  • alpha (float) – Damping coefficient proportional to mass.

  • beta (float) – Damping coefficient proportional to stiffness.

  • eta (float) – Damping coefficient.

  • freq (int) – Analyzed frequency.

  • timing (bool, optional) – If True shows the process optimization time. Defaults to False.

Returns

Displacement array.

functions_2d.matricesQ4(ee, coord, connect, E, v, rho)

Q4 stiffness and mass matrices.

Parameters
  • ee (int) – Element.

  • coord (numpy.array) – Coordinates of the element.

  • connect (numpy.array) – Element connectivity.

  • E (float) – Elastic modulus.

  • v (float) – Poisson’s ratio.

  • rho (float) – Density.

Returns

Tuple with stiffness and mass matrices.

functions_2d.regularmeshQ4(lx, ly, nelx, nely, timing=False)

Create a regular Q4 mesh.

Parameters
  • lx (float) – X-axis length.

  • ly (float) – Y-axis length.

  • nelx (int) – Number of elements on the X-axis.

  • nely (int) – Number of elements on the Y-axis.

  • timing (bool, optional) – If True shows the process optimization time. Defaults to False.

Returns

Tuple with the coordinate matrix, connectivity, and the indexes of each node.

functions_2d.remove_dofs(nelx, nely, del_dofs)

Delete specific DOFs from all DOFs.

Parameters
  • nelx (int) – Number of elements on the x-axis.

  • nely (int) – Number of elements on the y-axis.

  • del_dofs (numpy.array) – Array with DOFs to be removed.

Returns

Array without the DOFs removed.

functions_2d.shapeQ4(ssx, ttx)

Linear Shape Functions and Derivatives.

Parameters
  • ssx (float) – Local coordinate of the element on the X-axis.

  • ttx (float) – Local coordinate of the element on the Y-axis.

Returns

Tuple with the shape function and its derivative.

functions_2d.solution2D(coord, connect, ind_rows, ind_cols, nelx, nely, E, v, rho, timing=False)

Assembly matrices.

Parameters
  • coord (numpy.array) – Coordinates of the element.

  • connect (numpy.array) – Element connectivity.

  • ind_rows (numpy.array) – Node indexes to make the assembly.

  • ind_cols (numpy.array) – Node indexes to make the assembly.

  • nelx (int) – Number of elements on the X-axis.

  • nely (int) – Number of elements on the Y-axis.

  • E (float) – Elastic modulus.

  • v (float) – Poisson’s ratio.

  • rho (float) – Density.

  • timing (bool, optional) – If True shows the process optimization time. Defaults to False.

Returns

Stiffness and mass matrices.

functions_2d.turn_into_np(dicti_matrix, force)

Transforms dictionaries into numpy array.

Parameters
  • dicti_matrix (list) – List of dictionaries passed by the user.

  • force (bool) – True if encountering force matrix.

Returns

numpy array.

Plot

plots_2d.build_collection(coord, connect, disp_vector=None, timing=False)

Build quad mesh.

Parameters
  • coord (numpy.array) – Coordinates of the element.

  • connect (numpy.array) – Element connectivity.

  • disp_vector (numpy.array, optional) – Displacement.

  • timing (bool, optional) – If True shows the time to build the mesh. Defaults to False.

Returns

Matplotlib collection object.

plots_2d.get_size(lx, ly)

Gets columns with maximum and minimum length.

Parameters
  • lx (float) – X-axis length.

  • ly (float) – Y-axis length.

Returns

Column indexes with maximum and minimum length, respectively.

plots_2d.plot_collection(lx, ly, coord, pc, load_matrix=None, restri_matrix=None)

Plot mesh, force arrows and constrain nodes.

Parameters
  • lx (float) – X-axis length.

  • ly (float) – Y-axis length.

  • coord (numpy.array) – Coordinates of the element.

  • pc (matplotlib.collections) – Matplotlib collection object.

  • load_matrix (numpy.array, optional) – The columns are respectively node, x direction, y direction, force value.

  • restri_matrix (numpy.array, optional) –

Returns

A figure object and a single Axes object from matplotlib.pyplot.

plots_2d.plot_force(ax, coord, load_matrix, min_size, size)

Add load vector to the plot of deformed mesh.

Parameters
  • ax (matplotlib.axes.Axes) – Matplotlib axes object.

  • coord (numpy.array) – mesh coordinates.

  • load_matrix (numpy.array) – The columns are respectively node, x direction, y direction, force value.

  • min_size (float) – Value to define the width of load arrow.

  • size (float) – Load arrow length.

Returns

Matplotlib axes object.

plots_2d.plot_freqresponse(node, freq_range, disp_vector)

Plot the frequency response.

Parameters
  • node (list) – Node that was calculated the frequency response.

  • freq_range (list) –

    Range of analyzed frequencies.

    • First value is the minimum frequency.

    • Second value is the maximum frequency.

    • Third value is the step between each calculation of the function.

  • disp_vector (numpy.array) – Displacement.

Returns

A figure object and a single Axes object from matplotlib.pyplot.

plots_2d.plot_restr_nodes(ax, coord, restri_matrix)

Add contrain nodes to the plot of deformed mesh.

Parameters
  • ax (matplotlib.axes.Axes) – Matplotlib axes object.

  • coord (numpy.array) – mesh coordinates.

  • restri_matrix (numpy.array) –

Returns

Matplotlib axes object.

plots_2d.save_fig(fig, path)

Saves figure.

Parameters
  • fig – Object with the graph.

  • path – Directory to save the graph.

plots_2d.show_nodes(coord)

Plot nodes of the mesh.

Parameters

coord (numpy.array) – Coordinates of the element.