lib – library for basicfem solver

lib.output_utility

Utility functions for finite element analysis result output.

lib.output_utility.plot_contour(fem_result, value_type, direction, title_string, scale_factor, output_dir_path, plot_on_deformed=True)[source]

Generate the contour plot of given values for 2D elements.

The contour can be plotted on the deformed grid or undeformed grid. Only a single color is assigned to an element.

Note that currenly the nodal labeling must starts from 1 and increment continuously for the given strucuture, which is generally satisfied by the msh file provided by the Gmsh software.

Parameters:
  • fem_result (BaseSolver) – A Solver object contains the analysis result.
  • value_type (str) – A string that designated the type of value to be plotted. Currently accepts either ‘stress’ or ‘strain’.
  • direction (str) – A string designate the direction of the stress or strain tensor. Currently accepts ‘xx’, ‘yy’, or ‘xy’
  • title_string (str) – A string to be show as the title of the plot
  • scale_factor (float) – The deformation scale factor assigned in the param.
  • output_dir_path (str) – The path to the output directory.
  • plot_on_deformed (bool) – Flag that determines if the contour is plotted on the deformed structures. Default is True.
lib.output_utility.plot_deformation_shape_2D(nodal_data, element_data, displacements, scale_factor, output_dir_path)[source]

Generate deformation plot for general 2d elements.

The undeformed shape will also be superimposed on the figure.

nodal_data : numpy.array
the nodal_data from the SolverInput object
element_data : numpy.array
the element_data from the SolverInput object
displacements : numpy.array
the displacements from the BaseSolver object
scale_factor : float
The deformation scale factor assigned in the param.
output_dir_path : str
The path to the output directory.
lib.output_utility.plot_deformed_shape_1D(fem_result, scale_factor, output_dir_path, has_original_shape=True)[source]

Plot the deformed shape of the 1D element structures (truss and frames).

Parameters:
  • fem_result (BaseSolver) – A Solver object contains the analysis result.
  • scale_factor (float) – The deformation scale factor assigned in the param.
  • output_dir_path (str) – The path to the output directory.
  • has_original_shape (bool) – If True, the undeformed shape will also be plotted on the same figure.
lib.output_utility.save_data(array, filename, dir_name=None)[source]

A wrapper function for numpy.savetxt to store an array in a txt file.

matrix : numpy.ndarray
The numpy array to be saved into txt file.
filename : str
The filename for the txt file.
dir_name : str
The name of the directory to save the txt file. Only name is needed. Example: frame_output frame_output/frame_data

lib.solver_input module

solver_input module contains classes that represents the input data

class lib.solver_input.SolverInput(source_name)[source]

Bases: object

SolverInput is the object representing the input data of the structure to be solved.

This is a abstract class that cannot be instantiated, served as a base class for all the specific input data object.

Parameters:source_name (str) – The name of the input directory or the input xlsx file containing struture’s data.
node_per_element

Number of node per element.

Type:int
dof

Degree of freedoms of nodes.

Type:int
num_of_nodes

Total number of nodes of the structure.

Type:int
num_of_elements

Total number of elements of the structure.

Type:int
nodal_data

An array containing the nodal labels and nodal coordinates. The structure is as follows:

1st column : node labels

2nd column : x coordinate of the node

3rd column : y coordinate of the node

4th column : z coordinate of the node (if applicable)

Type:numpy.ndarray
element_data

An array containing the cooresponding nodes and properties of the elements. The structure is as follows:

1st column : element labels

2nd column : first nodes

3rd column : second nodes

4th column : property number (see properties array)

Type:numpy.ndarray
bound_con

an array containing the nodal fixed boundary conditions of the problem. The 1st column is the corresponding nodal labels, the remaining columns corresponds to conditions in x, y, and z coordinates. 1 represents fixed, 0 represents fixed. Nodes not included in this array are treated as free nodes.

Type:numpy.ndarray
bound_con_nonhomo

An array containing the non-homogeneous conditions. Similar structure as bound_con, except that columns corresponding x, y, and z coodinates are the magnitude of displacements.

Type:numpy.ndarray
nodal_forces

An array containing the external nodal forces. Similar structure as bound_con, except that the columns corresponding x, y, and z coordinates are the magnitude of displacements.

Type:numpy.ndarray
properties

an array containing the properties of all elements. The structure is as follows:

1st column : property labels corresponds to array ‘element_data’

2nd column : material number corresponds to array ‘materials’

3rd column : cross-sectional area of the cooresponding elements.

4th column : z Moment of inertia of the corresponding elements.

Type:numpy.ndarray
materials

an array containing the properties of all elements. The structure is as follows:

1st column : material labels corresponds to array ‘properties’

2nd column : Young’s modulus

3rd column : Density of the material

Type:numpy.ndarray
get_element_property(property_type, element_num, is_label=False)[source]

returns the property of a single element.

get_element_property(property_type, element_num, is_label=False)[source]

Returns the property of a single elements.

Parameters:property_type (str) – A string that indicates the property to return.
element_num : int
an integer indicates the index or the label of the element.
is_label : bool
a boolean indicates if the variable ‘element_num’ is a label. If it is true, the function will search for the element property using the element’s label. If it is false, the function will search for the element property using the index of the array ‘element_data’. The default is false.
class lib.solver_input.TriangularElementInput(filename=None)[source]

Bases: lib.solver_input.SolverInput

get_element_property(property_type, element_num, is_label=False)[source]

See SolverInput for descriptions.

property_type options:

‘t’ : thickness

‘E’ : Young’s modulus

‘nu’ : poisson’s ratio

‘node_ind’ : indicies in the array ‘nodal_data’ corresponding to the element.

‘nodal_coord’ : nodal coordinates of the given element.

class lib.solver_input.TrussInput2D(filename)[source]

Bases: lib.solver_input.SolverInput

TrussInput object represents input data for truss structures.

See SolverInput class for more descriptions on parameters and attributes.

get_element_property(property_type, element_num, is_label=False)[source]

See SolverInput for descriptions.

property_type options:

‘A’ : area

‘E’ : Young’s modulus

‘I’ : moment of inertia

‘rho’ : material density

‘Yc’ : compressive yield strength

‘Yt’ : tensile yield strenth

‘L’ : element length

‘dcos’ : directional cosine values of the element

‘node_ind’ : indicies in the array ‘nodal_data’
corresponding to the element

lib.solvers

solvers module contains all the main FEM solver classes

class lib.solvers.BaseSolver(SolverInput)[source]

Bases: object

The base class for all the solvers.

The BaseSolver defines the global stiffness matrix construction, and the method for solving displacements, which is common for all the specific solvers. It also contains an abstract method to define the local stiffness matrix to be implement by other solvers.

Parameters:SolverInput (SolverInput) – SolverInput object that contains all input data.
sol_in

A local object reference to the SolverInput object.

Type:SolverInput
global_conn

The global connectivity list. The indices of the list represents the element index, and the value in the lists are the corresponding list that shows the indices in global stiffness matrix that corresponds to each of its nodes.

Type:list of lists
K_global

The stiffness matrix of the structure.

Type:numpy.array
displacements

The array of nodal displacements calculated by the solver.

Type:numpy.array
get_K_global()[source]

Calculate the global stiffness matrix.

get_Kel_global()[source]

Calculate the global stiffness matrix. (abstract method)

solve_displacement()[source]

Calculate the nodal displacement array.

get_element_conn(element_index)[source]

get the connectivity list of a given element. An index of the list represents an index of the local stiffness matrix, and the value of the lists represents the index of the global stiffness matrix. The connectivity list is used to assemble the global stiffness matrix from local stiffness matrices.

constraint_node_flags()[source]

return a 1D numpy of flags, where 1 in the element represents a fixed dof to the corresponded index that assigned by input boundary conditions.

initialize_disp_and_force(keepList)[source]

initialize displacements and force matrix to be solved.

fill_nodal_forces()[source]

helper function for initialize_disp_and_force to initializing force matrix.

constraint_node_flags()[source]

return a 1D numpy of flags, where 1 in the element represents a fixed dof to the corresponded index.

Returns:a list of 1 or 0. 1 means the corresponding indices of the global stiffness matrix (which means the degree of freedom) is fixed.
Return type:list
fill_nodal_forces()[source]

fill-in the force vector from F matrix.

get_K_global()[source]

Calculate the global stiffness matrix.

Returns:The global stiffness matrix of the structure.
Return type:numpy.ndarray
get_Kel_global(element_index)[source]

Returns the element stiffness matrix in global coordinates for given element index.

get_element_conn(element_index)[source]

Get a list which its indices represent the indices of element stiffness matrix, whereas the elements of the list points to the indices of the global stiffness matrix. The list is called connectivity matrix.

Returns:A list that maps the local stiffnesss matrix indices to global stiffness matrix indices
Return type:list
initialize_disp_and_force(keepList)[source]

initialize the displacement and force vector for solving displacement. Non-homogeneous boundary conditions are also considered.

solveForce(dMatrix)[source]

Solved for the reaction forces of nodes.

solve_displacement()[source]

Solve and return the nodal displacements using global stiffness matrix and force vector.

Returns:The array containing every nodal displacement components.
Return type:numpy.ndarray
class lib.solvers.FrameSolver2D(SolverInput)[source]

Bases: lib.solvers.TrussSolver2D

Solver class for 2D frame structures.

Kel_local_axial(element_index)[source]

Get the Kel_local (element stiffness matrix in local coordinates) of the axial components.

Kel_local_bending(element_index)[source]

Get the Kel_local (element stiffness matrix in local coordinates) of the bending components.

get_Kel_local(element_index)[source]

combining the bending components and axial components of the stiffness matrix.

get_T(element_index)[source]

Get the transformation matrix T of given element.

get_beam_deflection(x, L, element_index, beam_nodal_disp)[source]

Get the beam deflection of interpolation points by using the shape functions.

get_interpolate_points(element_index, scale_factor, sub_element_num)[source]

Generates a 2D array of nodal coordinates after deformation and the effect of the deformation scale factor.

get_interpolated_axial_disp(x, L, lement_index, axial_nodal_disp)[source]

Get the axial displacements of interpolation points by using the shape functions.

interpolate_shape(mag_factor, sub_element_num=100)[source]

Generates a 3D array, with each element corresponds to a 2D array that represents the nodal coordinates after the deformation and the effect of scale factor in x and y direction for every interpolation points. The array is used to plot the deformation shape.

class lib.solvers.TriangularElementSolver(SolverInput)[source]

Bases: lib.solvers.BaseSolver

Constant Strain Triangular (CST) elements for 2D structures.

get_Jacobian_det(element_index)[source]

Get the determinant of the Jacobian matrix

get_Kel_global(element_index)[source]

Get element stiffness matrix.

get_element_strain(element_index, nodal_displacement)[source]

Get the strain of the given element index.

get_element_stress(element_index, element_strain)[source]

Get the stress of the given element index.

get_stress_and_strain()[source]

Get the stress and strain matrix containing the values for each elements.

strain_displacement_matrix(element_index)[source]

Get the strain-displacement matrix (the constitutive matrix)

stress_strain_matrix(element_index, is_plane_strain=False)[source]

Get the stress-strain matrix (the constitutive matrix)

class lib.solvers.TrussSolver2D(SolverInput)[source]

Bases: lib.solvers.BaseSolver

Solver class for 2D truss structures.

element_stiffness(element_index)[source]

return the stiffness of a single truss element.

get_Kel_global(element_index)[source]

Implementation of get_Kel_global method for BaseSolver class

get_Kel_local(element_index)[source]

returns the element stiffness matrix in local coordinates for 2D truss structural elements.

get_T(element_index)[source]

Get the transformation matrix T of given element.

get_stress()[source]

Implementation of get_stress method for BaseSolver class

interpolate_shape(mag_factor, sub_element_num=100)[source]

Note: Since truss uses linear shape functions, no interpolation is used. The method returns the deformed nodal positon of elements to make output utility usage consistent.

Module contents