API Reference

Core Modules

Materials

Materials module defining material properties for structural elements.

class fem2d.materials.ElasticMaterial(E, rho=0.0)[source]

Bases: object

Represents an elastic material with stiffness and density properties.

E

Young’s Modulus of the material.

Type:

float

rho

Mass density (mass per unit volume) of the material. Defaults to 0.0.

Type:

float, optional

Sections

Sections module defining cross-sectional properties of structural elements.

class fem2d.sections.Section(area, moi)[source]

Bases: object

Represents a cross-section of a structural element with area and moment of inertia.

A

Cross-sectional area.

Type:

float

I

Moment of inertia (second moment of area).

Type:

float

classmethod from_circle(diameter)[source]

Create a circular section. :param diameter: Diameter of the circle in consistent units. :type diameter: float

Returns:

A Section object with area = π*(d/2)^2 and moment of inertia = π*(d/2)^4 / 4.

Return type:

Section

classmethod from_rectangle(width, depth)[source]

Create a rectangular section. :param width: Width (horizontal dimension) in consistent units. :type width: float :param depth: Depth (vertical dimension) in consistent units. :type depth: float

Returns:

A Section object with area = width*depth and moment of inertia = width * depth**3 / 12.

Return type:

Section

Nodes

Nodes module representing points in the structural model.

class fem2d.nodes.Node(nid, x, y)[source]

Bases: object

Represents a node in a 2D finite element model.

id

Unique identifier of the node.

Type:

int or str

x

x-coordinate of the node.

Type:

float

y

y-coordinate of the node.

Type:

float

support

Fixity condition of the node’s degrees of freedom [ux, uy, rz].

Type:

list of bool

load

Nodal force and moment values [Fx, Fy, Mz].

Type:

list of float

dofs

Global degrees of freedom indices assigned to this node.

Type:

list of int or None

mass

Translational lumped mass of the node.

Type:

float

inertia

Rotational lumped inertia of the node.

Type:

float

set_load(fx=0.0, fy=0.0, mz=0.0)[source]

Apply forces and moment directly to the node.

Parameters:
  • fx (float, optional) – Concentrated load in the global x-direction. Defaults to 0.0.

  • fy (float, optional) – Concentrated load in the global y-direction. Defaults to 0.0.

  • mz (float, optional) – Concentrated moment about the global z-axis. Defaults to 0.0.

set_mass(mass=0.0, inertia=0.0)[source]

Add lumped mass (translational) and optionally rotational inertia.

Parameters:
  • mass (float, optional) – Translational mass lumped at the node. Defaults to 0.0.

  • inertia (float, optional) – Rotational inertia lumped at the node. Defaults to 0.0.

set_support(ux_fixed=False, uy_fixed=False, rz_fixed=False)[source]

Set support fixity conditions for the degrees of freedom.

Parameters:
  • ux_fixed (bool, optional) – Whether translation along the x-axis is fixed. Defaults to False.

  • uy_fixed (bool, optional) – Whether translation along the y-axis is fixed. Defaults to False.

  • rz_fixed (bool, optional) – Whether rotation about the z-axis is fixed. Defaults to False.

Loads

Loads module defining point, distributed, and varying loads for nodes and elements.

class fem2d.loads.DistributedLoad(element, wx=0.0, wy=0.0)[source]

Bases: ElementLoad

Represents a uniformly distributed load acting on an element. The load is defined in the element’s local coordinate system:

wx: force per unit length along local x (axial) wy: force per unit length along local y (transverse)

class fem2d.loads.ElementLoad(element)[source]

Bases: object

Base class for loads acting along the length of an element.

element

The element to which the load is applied.

Type:

ElementBase

class fem2d.loads.ElementPointLoad(element, px=0.0, py=0.0, mz=0.0, x=0.0)[source]

Bases: ElementLoad

Represents a point load acting on an element at a specific distance from its start node. The load is defined in the element’s local coordinate system:

px: force along local x (axial) py: force along local y (transverse) mz: moment around local z x: distance from the start node of the element.

class fem2d.loads.PointLoad(node, fx=0.0, fy=0.0, mz=0.0)[source]

Bases: object

Represents a concentrated force/moment load acting on a node.

node

The Node object to which the load is applied.

Type:

Node

fx

Force along the global x-axis.

Type:

float

fy

Force along the global y-axis.

Type:

float

mz

Moment about the global z-axis.

Type:

float

class fem2d.loads.TriangularLoad(element, wx1=0.0, wx2=0.0, wy1=0.0, wy2=0.0)[source]

Bases: ElementLoad

Represents a trapezoidally distributed load (UVL) on an element. This can be used for triangular loads by setting one of w1 or w2 to zero. The load is defined in the element’s local coordinate system. w1 and w2 are load intensities at the start and end of the element. Can be axial (wx1, wx2) or transverse (wy1, wy2).

Structure

Structure module defining the main model container and global assembly/solver processes.

class fem2d.structure.Structure[source]

Bases: object

Represents a 2D finite element model container.

Manages nodes, elements, and loads, and handles degrees of freedom numbering, global stiffness/mass matrix assembly, boundary conditions, and solution procedures.

nodes

Dictionary of nodes mapped by their IDs.

Type:

dict of {int/str: Node}

elements

Dictionary of elements mapped by their IDs.

Type:

dict of {int/str: ElementBase}

loads

List of load objects applied to the structure.

Type:

list

K

Global stiffness matrix.

Type:

numpy.ndarray or None

F

Global force vector.

Type:

numpy.ndarray or None

M

Global mass matrix.

Type:

numpy.ndarray or None

disp

Global displacement vector.

Type:

numpy.ndarray or None

reactions

Global reaction force vector at fixed degrees of freedom.

Type:

numpy.ndarray or None

neq

Total number of degrees of freedom.

Type:

int

fixed_dofs

List of fixed degrees of freedom indices.

Type:

list of int

free_dofs

List of free degrees of freedom indices.

Type:

list of int

add_element(element)[source]

Add an element to the structure.

Parameters:

element (ElementBase) – The Element object to add.

add_load(load)[source]

Add a load to the structure.

Parameters:

load (PointLoad or ElementLoad) – The load object to add.

add_node(node)[source]

Add a Node to the structure.

Parameters:

node (Node) – The Node object to add.

apply_boundary_conditions()[source]

Determine free and fixed DOFs, partitioning matrices accordingly. Also automatically flags unstable DOFs before partitioning.

assemble_loads()[source]

Assemble the global force vector from nodal forces and element equivalent loads.

assemble_mass_matrix()[source]

Assemble the global mass matrix from element and nodal mass contributions.

assemble_stiffness()[source]

Assemble the global stiffness matrix from all element stiffness contributions.

get_reduced_matrices()[source]

Return (K_ff, M_ff) reduced to free degrees of freedom.

Assumes assemble_stiffness() and assemble_mass_matrix() have been called, and apply_boundary_conditions() has been called.

Returns:

  • K_ff (numpy.ndarray) – Reduced global stiffness matrix for free degrees of freedom.

  • M_ff (numpy.ndarray) – Reduced global mass matrix for free degrees of freedom.

Raises:

ValueError – If boundary conditions have not been applied yet.

number_dofs()[source]

Assign DOF indices to each node (3 DOFs per node: [ux, uy, rz]).

solve()[source]

Perform linear static analysis. Numbers DOFs, assembles matrices, partitions, solves for free displacements, and computes support reaction forces.

solve_nonlinear(tolerance=1e-08, max_iter=30)[source]

Perform geometrically nonlinear analysis using Newton-Raphson.

Assumes all elements implement the nonlinear interface (update_state, get_tangent_stiffness, get_internal_forces).

Parameters:
  • tolerance (float, optional) – Convergence tolerance. Defaults to 1e-8.

  • max_iter (int, optional) – Maximum iterations. Defaults to 30.

Solver

Solver module implementing non-linear iterative solvers (Newton-Raphson).

class fem2d.solver.NewtonRaphsonSolver(structure, tolerance=1e-08, max_iter=30)[source]

Bases: object

Newton-Raphson solver for geometrically non-linear structural analysis.

structure

The Structure object to be solved.

Type:

Structure

tol

Convergence tolerance for unbalanced force norm relative to external force.

Type:

float

max_iter

Maximum number of iterations allowed for convergence.

Type:

int

solve(P_ext)[source]

Solve for non-linear displacements given external force vector.

Parameters:

P_ext (numpy.ndarray) – External force vector for all degrees of freedom.

Returns:

Converged displacement vector for all degrees of freedom.

Return type:

numpy.ndarray

Results

Elements

Elements package containing definitions for different finite element types.

class fem2d.elements.BeamElement(eid, node_i, node_j, material, area, inertia, extra_mass=0.0)[source]

Bases: ElementBase

Elastic 2D Euler-Bernoulli beam element including axial, shear, and bending stiffness.

material

Material definition for the element.

Type:

ElasticMaterial

area

Cross-sectional area.

Type:

float

inertia

Moment of inertia of the cross-section.

Type:

float

extra_mass

Additional distributed mass per unit length (e.g. non-structural mass).

Type:

float

deformed_shape_points(global_disp, n_points=20, scale=1.0)[source]

Return a list of (x, y) points in global coordinates representing the deformed shape of the beam, scaled by scale.

Parameters:
  • global_disp (numpy.ndarray) – Full global displacement vector of the structure.

  • n_points (int, optional) – Number of points to generate along the element. Defaults to 20.

  • scale (float, optional) – Displacement magnification factor. Defaults to 1.0.

Returns:

List of (x, y) coordinates representing the deformed shape.

Return type:

list of tuple of float

equivalent_nodal_loads()[source]

Return local equivalent nodal loads due to distributed element loads.

Returns:

Equivalent nodal load vector (6x1).

Return type:

numpy.ndarray

get_local_forces()[source]

Compute and return the element internal forces in local coordinates.

Returns:

6-component vector [Fx_i, Fy_i, Mz_i, Fx_j, Fy_j, Mz_j] in local coordinates.

Return type:

numpy.ndarray

local_stiffness()[source]

Return the 6x6 element stiffness matrix in local coordinates.

Returns:

Stiffness matrix in local coordinate system.

Return type:

numpy.ndarray

mass_matrix()[source]

Return the 6x6 global mass matrix (including rotational inertia and extra mass).

Returns:

6x6 global mass matrix.

Return type:

numpy.ndarray

class fem2d.elements.SpringElement(eid, node_i, node_j, stiffness)[source]

Bases: TrussElement

Represents a 1D elastic spring element with specified axial stiffness.

k

Axial stiffness coefficient of the spring.

Type:

float

local_stiffness()[source]

Return the 4x4 spring stiffness matrix in local coordinates.

Returns:

4x4 stiffness matrix.

Return type:

numpy.ndarray

class fem2d.elements.TrussElement(eid, node_i, node_j, material, area, extra_mass=0.0)[source]

Bases: ElementBase

Elastic 2D truss (pin-jointed bar) element with only axial stiffness.

material

Material definition for the element.

Type:

ElasticMaterial or None

area

Cross-sectional area.

Type:

float or None

extra_mass

Additional distributed mass per unit length.

Type:

float

deformed_shape_points(global_disp, n_points=2, scale=1.0)[source]

Return end point coordinates representing the deformed shape of the truss.

Parameters:
  • global_disp (numpy.ndarray) – Full global displacement vector.

  • n_points (int, optional) – Number of points along the element. Defaults to 2.

  • scale (float, optional) – Displacement magnification factor. Defaults to 1.0.

Returns:

End point coordinates (start and end).

Return type:

list of tuple of float

get_local_forces()[source]

Compute and return the local member end forces.

Returns:

6-component vector [Fx_i, Fy_i, Mz_i, Fx_j, Fy_j, Mz_j] in local coordinates.

Return type:

numpy.ndarray

global_stiffness()[source]

Assemble and return the 6x6 global stiffness matrix. Expands the 4x4 matrix by inserting zeros for rotational DOFs.

Returns:

6x6 global stiffness matrix.

Return type:

numpy.ndarray

local_stiffness()[source]

Return the 4x4 element stiffness matrix in local coordinates.

Returns:

4x4 stiffness matrix.

Return type:

numpy.ndarray

mass_matrix()[source]

Return the 6x6 global mass matrix. Lumps the element and extra mass equally to translational DOFs at ends.

Returns:

6x6 global mass matrix.

Return type:

numpy.ndarray

transformation_matrix(dof_per_node=2)[source]

Return the transformation matrix from local to global coordinates.

Parameters:

dof_per_node (int, optional) – Number of degrees of freedom per node. Defaults to 2.

Returns:

4x4 transformation matrix.

Return type:

numpy.ndarray

Base Element Class

ElementBase module defining the base class for all structural finite elements.

class fem2d.elements.element.ElementBase(eid, node_i, node_j)[source]

Bases: object

Base class for structural elements in a 2D finite element model.

id

Unique identifier of the element.

Type:

int or str

node_i

Start node of the element.

Type:

Node

node_j

End node of the element.

Type:

Node

structure

The parent structure containing this element.

Type:

Structure or None

length

Length of the element.

Type:

float

cos

Cosine of the element orientation angle with respect to global x-axis.

Type:

float

sin

Sine of the element orientation angle with respect to global x-axis.

Type:

float

equivalent_nodal_loads()[source]

Return equivalent nodal loads due to element loads in global coordinates.

Returns:

Global equivalent load vector (size 6).

Return type:

numpy.ndarray

global_stiffness()[source]

Assemble global stiffness matrix (expanded to global system size).

Returns:

Global stiffness matrix (typically 6x6).

Return type:

numpy.ndarray

internal_forces(displacements)[source]

Given nodal displacements (global, 6‑vector), return local end forces.

Parameters:

displacements (numpy.ndarray) – Global displacement vector for the element nodes.

Returns:

Local end forces vector.

Return type:

numpy.ndarray

Raises:

NotImplementedError – Must be implemented by subclasses.

local_stiffness()[source]

Return element stiffness matrix in local coordinates.

Returns:

Local stiffness matrix.

Return type:

numpy.ndarray

Raises:

NotImplementedError – Must be implemented by subclasses.

transformation_matrix(dof_per_node=3)[source]

Return the transformation matrix from local to global coordinates.

Parameters:

dof_per_node (int, optional) – Number of degrees of freedom per node. Defaults to 3.

Returns:

Transformation matrix of size (2*dof_per_node, 2*dof_per_node).

Return type:

numpy.ndarray

Beam Elements

Beam element module defining 2D elastic Euler-Bernoulli beam elements with mass options.

class fem2d.elements.beam.BeamElement(eid, node_i, node_j, material, area, inertia, extra_mass=0.0)[source]

Bases: ElementBase

Elastic 2D Euler-Bernoulli beam element including axial, shear, and bending stiffness.

material

Material definition for the element.

Type:

ElasticMaterial

area

Cross-sectional area.

Type:

float

inertia

Moment of inertia of the cross-section.

Type:

float

extra_mass

Additional distributed mass per unit length (e.g. non-structural mass).

Type:

float

deformed_shape_points(global_disp, n_points=20, scale=1.0)[source]

Return a list of (x, y) points in global coordinates representing the deformed shape of the beam, scaled by scale.

Parameters:
  • global_disp (numpy.ndarray) – Full global displacement vector of the structure.

  • n_points (int, optional) – Number of points to generate along the element. Defaults to 20.

  • scale (float, optional) – Displacement magnification factor. Defaults to 1.0.

Returns:

List of (x, y) coordinates representing the deformed shape.

Return type:

list of tuple of float

equivalent_nodal_loads()[source]

Return local equivalent nodal loads due to distributed element loads.

Returns:

Equivalent nodal load vector (6x1).

Return type:

numpy.ndarray

get_local_forces()[source]

Compute and return the element internal forces in local coordinates.

Returns:

6-component vector [Fx_i, Fy_i, Mz_i, Fx_j, Fy_j, Mz_j] in local coordinates.

Return type:

numpy.ndarray

local_stiffness()[source]

Return the 6x6 element stiffness matrix in local coordinates.

Returns:

Stiffness matrix in local coordinate system.

Return type:

numpy.ndarray

mass_matrix()[source]

Return the 6x6 global mass matrix (including rotational inertia and extra mass).

Returns:

6x6 global mass matrix.

Return type:

numpy.ndarray

Non-Linear Beam Elements

Non-linear beam element module implementing a geometrically non-linear 2D beam element.

class fem2d.elements.beamNL.BeamElementNL(eid, node_i, node_j, material, area, inertia)[source]

Bases: object

Geometrically nonlinear 2D beam element using corotational formulation (Euler-Bernoulli).

Follows the interface required by NewtonRaphsonSolver.

id

Unique identifier of the element.

Type:

int or str

node_i

Start node of the element.

Type:

Node

node_j

End node of the element.

Type:

Node

material

Material definition for the element.

Type:

ElasticMaterial

area

Cross-sectional area.

Type:

float

inertia

Moment of inertia of the cross-section.

Type:

float

L0

Initial (undeformed) length.

Type:

float or None

alpha0

Initial (undeformed) chord angle in radians.

Type:

float or None

structure

The parent structure containing this element.

Type:

Structure or None

f_local

Current internal force vector in local coordinates.

Type:

numpy.ndarray or None

F_global

Current internal force vector in global coordinates.

Type:

numpy.ndarray or None

k_local

Current tangent stiffness matrix in local coordinates.

Type:

numpy.ndarray or None

K_global

Current tangent stiffness matrix in global coordinates.

Type:

numpy.ndarray or None

get_internal_forces()[source]

Return global internal forces vector of the element.

Returns:

6x1 internal forces vector in global coordinates.

Return type:

numpy.ndarray

get_local_forces()[source]

Return local end forces (6 components) for post‑processing.

Returns:

6x1 force vector in local coordinates.

Return type:

numpy.ndarray

get_tangent_stiffness()[source]

Return global tangent stiffness matrix of the element.

Returns:

6x6 tangent stiffness matrix in global coordinates.

Return type:

numpy.ndarray

update_state(global_disp)[source]

Compute current internal forces and tangent stiffness in global coordinates given the global displacement vector of the whole structure.

Parameters:

global_disp (numpy.ndarray) – Full global displacement vector of the structure.

Beam with Hinges

Beam with hinges module defining beam elements with internal moment releases.

class fem2d.elements.beam_hinges.BeamWithHingesElement(eid, node_i, node_j, material, area, inertia, hinge_i=False, hinge_j=False)[source]

Bases: BeamElement

Beam element that allows specifying internal moment releases (hinges) at ends i and/or j.

hinge_i

Whether end i has a moment release (hinge).

Type:

bool

hinge_j

Whether end j has a moment release (hinge).

Type:

bool

local_stiffness()[source]

Compute local stiffness matrix taking moment releases into account.

Returns:

6x6 local stiffness matrix.

Return type:

numpy.ndarray

Raises:

NotImplementedError – If only a single hinge is specified (not yet fully implemented).

Truss Elements

Truss element module defining 2D elastic truss (bar) elements with axial stiffness.

class fem2d.elements.truss.TrussElement(eid, node_i, node_j, material, area, extra_mass=0.0)[source]

Bases: ElementBase

Elastic 2D truss (pin-jointed bar) element with only axial stiffness.

material

Material definition for the element.

Type:

ElasticMaterial or None

area

Cross-sectional area.

Type:

float or None

extra_mass

Additional distributed mass per unit length.

Type:

float

deformed_shape_points(global_disp, n_points=2, scale=1.0)[source]

Return end point coordinates representing the deformed shape of the truss.

Parameters:
  • global_disp (numpy.ndarray) – Full global displacement vector.

  • n_points (int, optional) – Number of points along the element. Defaults to 2.

  • scale (float, optional) – Displacement magnification factor. Defaults to 1.0.

Returns:

End point coordinates (start and end).

Return type:

list of tuple of float

get_local_forces()[source]

Compute and return the local member end forces.

Returns:

6-component vector [Fx_i, Fy_i, Mz_i, Fx_j, Fy_j, Mz_j] in local coordinates.

Return type:

numpy.ndarray

global_stiffness()[source]

Assemble and return the 6x6 global stiffness matrix. Expands the 4x4 matrix by inserting zeros for rotational DOFs.

Returns:

6x6 global stiffness matrix.

Return type:

numpy.ndarray

local_stiffness()[source]

Return the 4x4 element stiffness matrix in local coordinates.

Returns:

4x4 stiffness matrix.

Return type:

numpy.ndarray

mass_matrix()[source]

Return the 6x6 global mass matrix. Lumps the element and extra mass equally to translational DOFs at ends.

Returns:

6x6 global mass matrix.

Return type:

numpy.ndarray

transformation_matrix(dof_per_node=2)[source]

Return the transformation matrix from local to global coordinates.

Parameters:

dof_per_node (int, optional) – Number of degrees of freedom per node. Defaults to 2.

Returns:

4x4 transformation matrix.

Return type:

numpy.ndarray

Non-Linear Truss Elements

Non-linear truss element module implementing a geometrically non-linear 2D truss element.

class fem2d.elements.trussNL.TrussElementNL(eid, node_i, node_j, material, area)[source]

Bases: ElementBase

Geometrically nonlinear truss element using corotational formulation. Nodes have 2 translational DOFs: [ux, uy], with rotation index mapped to zero.

material

Material definition for the element.

Type:

ElasticMaterial

area

Cross-sectional area.

Type:

float

L0

Initial (undeformed) length.

Type:

float

Q

Axial force (compression positive, tension negative).

Type:

float

cx

Current direction cosine along the global x-axis.

Type:

float

cy

Current direction cosine along the global y-axis.

Type:

float

L

Current deformed length.

Type:

float

ke

Elastic stiffness matrix in global coordinates.

Type:

numpy.ndarray or None

kg

Geometric stiffness matrix in global coordinates.

Type:

numpy.ndarray or None

k_t

Tangent stiffness matrix in global coordinates.

Type:

numpy.ndarray or None

F_global

Internal force vector in global coordinates.

Type:

numpy.ndarray or None

get_internal_forces()[source]

Return the global internal force vector of the element.

Returns:

6x1 global internal force vector.

Return type:

numpy.ndarray

get_local_forces()[source]

Return local end forces as a 6‑component array: [Fx_i, Fy_i, Mz_i, Fx_j, Fy_j, Mz_j] Axial force Q is positive in compression.

Returns:

6x1 force vector in local coordinates.

Return type:

numpy.ndarray

get_tangent_stiffness()[source]

Return the global tangent stiffness matrix of the element.

Returns:

6x6 tangent stiffness matrix in global coordinates.

Return type:

numpy.ndarray

update_state(global_disp)[source]

Given global displacement vector of the whole structure, extract this element’s end displacements and update internal state (internal force, tangent stiffness).

Parameters:

global_disp (numpy.ndarray) – Full global displacement vector of the structure.

Spring Elements

Spring element module defining 2D spring elements with axial stiffness.

class fem2d.elements.spring.SpringElement(eid, node_i, node_j, stiffness)[source]

Bases: TrussElement

Represents a 1D elastic spring element with specified axial stiffness.

k

Axial stiffness coefficient of the spring.

Type:

float

local_stiffness()[source]

Return the 4x4 spring stiffness matrix in local coordinates.

Returns:

4x4 stiffness matrix.

Return type:

numpy.ndarray

Utilities

Simple Frame Helper

Drawing Helpers