Skip to content

ElementMask

ElementMask

ElementMask(mesh: MeshIndex, ids: ndarray)

Bases: _BaseMask

A mask of element IDs with chainable attribute and spatial filters.

All methods return a new ElementMask instance, ensuring immutability at the API level.

Example
import numpy as np
from femora.components.mask.mask_base import MeshIndex, ElementMask

mesh_index = MeshIndex(
    node_ids=np.array([0, 1, 2, 3]),
    node_coords=np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [2.0, 2.0, 2.0]]),
    node_ndf=np.array([3, 3, 3, 3]),
    node_core_map=[[0], [0], [0], [0]],
    element_ids=np.array([10, 11, 12]),
    element_connectivity=[np.array([0, 1]), np.array([1, 2]), np.array([2, 3])],
    element_centroids=np.array([[0.5, 0.0, 0.0], [0.5, 0.5, 0.0], [1.0, 1.5, 1.0]]),
    element_types=np.array(["BEAM", "BEAM", "TRUSS"]),
    material_tags=np.array([1, 2, 1]),
    section_tags=np.array([1, 1, 2]),
    region_tags=np.array([0, 1, 0]),
    core_ids=np.array([0, 0, 1]),
    element_id_to_index={10: 0, 11: 1, 12: 2}
)
all_elements = ElementMask(mesh_index, mesh_index.element_ids)
filtered_elements = all_elements.by_material(1)
print(filtered_elements.to_list())
# Output: [10, 12]

Methods:

by_ids

by_ids(ids: Sequence[int]) -> 'ElementMask'

Intersects the current element mask with specific element IDs.

Parameters:

Name Type Description Default
ids Sequence[int]

A sequence of element IDs to retain.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing the intersection of the current mask's IDs and the provided ids.

Example
# assuming `mask` is an ElementMask holding [10, 11, 12]
filtered_mask = mask.by_ids([11, 13])
print(filtered_mask.to_list())
# Output: [11]

by_type

by_type(name: str) -> 'ElementMask'

Filters elements by their type name (case-insensitive).

Parameters:

Name Type Description Default
name str

The element type name to match.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements whose type name matches the provided name.

Example
# Filter elements by type string (case-insensitive)
filtered_elements = all_elements.by_type("beam")
print(filtered_elements.to_list())
# Output: [10, 11]

by_material

by_material(tag: int) -> 'ElementMask'

Filters elements by their associated material tag.

Parameters:

Name Type Description Default
tag int

The integer material tag value to match.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements that have the specified material tag.

Example
# Filter elements by material tag
filtered_elements = all_elements.by_material(1)
print(filtered_elements.to_list())
# Output: [10, 12]

by_section

by_section(tag: int) -> 'ElementMask'

Filters elements by their associated section tag.

Parameters:

Name Type Description Default
tag int

The integer section tag value to match.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements that have the specified section tag.

Example
# Filter elements by section tag
filtered_elements = all_elements.by_section(2)
print(filtered_elements.to_list())
# Output: [12]

by_region

by_region(tag: int) -> 'ElementMask'

Filters elements by their associated region tag.

Parameters:

Name Type Description Default
tag int

The integer region tag value to match.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements that are associated with the specified region tag.

Example
# Filter elements by region tag
filtered_elements = all_elements.by_region(1)
print(filtered_elements.to_list())
# Output: [11]

by_core

by_core(core: int) -> 'ElementMask'

Filters elements by their partition or core ID.

Parameters:

Name Type Description Default
core int

The integer core/partition identifier to match.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements that belong to the specified core.

Example
# Filter elements by partition/core ID
filtered_elements = all_elements.by_core(0)
print(filtered_elements.to_list())
# Output: [10, 11]

by_bbox

by_bbox(xmin: float, xmax: float, ymin: float, ymax: float, zmin: float, zmax: float, *, use_centroid: bool = True) -> 'ElementMask'

Filters elements inside an axis-aligned bounding box.

Parameters:

Name Type Description Default
xmin float

The minimum x-coordinate of the bounding box.

required
xmax float

The maximum x-coordinate of the bounding box.

required
ymin float

The minimum y-coordinate of the bounding box.

required
ymax float

The maximum y-coordinate of the bounding box.

required
zmin float

The minimum z-coordinate of the bounding box.

required
zmax float

The maximum z-coordinate of the bounding box.

required
use_centroid bool

If True (default), elements are filtered based on whether their centroid lies within the bounding box. If False, an element is included if any of its nodes lies within the bounding box.

True

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing elements that are located within the specified bounding box.

Example
# Filter elements by centroid or nodal coordinates in bounding box
filtered_by_centroid = all_elements.by_bbox(0.5, 1.0, 0.5, 1.0, -0.1, 0.1, use_centroid=True)
print(filtered_by_centroid.to_list())
# Output: [11]

by_predicate

by_predicate(fn: Callable[[int, ndarray, str, int, int, int], bool]) -> 'ElementMask'

Filters elements using a custom predicate function.

The predicate function will be called for each element, and only elements for which the function returns True will be included in the new mask.

Parameters:

Name Type Description Default
fn Callable[[int, ndarray, str, int, int, int], bool]

A callable that accepts six arguments: - elem_id (int): The ID of the current element. - centroid (np.ndarray): The 3D centroid coordinates of the element. - type_name (str): The type name of the element (e.g., "BEAM", "TRIANGLE"). - material_tag (int): The material tag associated with the element. - section_tag (int): The section tag associated with the element. - region_tag (int): The region tag associated with the element. The function should return True to keep the element, False otherwise.

required

Returns:

Name Type Description
ElementMask 'ElementMask'

A new mask containing only elements that pass the predicate.

Example
# Custom predicate filtering
def filter_elements(eid, centroid, etype, mtag, stag, rtag):
    return mtag == 1 and centroid[0] > 0.2

filtered_elements = all_elements.by_predicate(filter_elements)
print(filtered_elements.to_list())
# Output: [10]

to_nodes

to_nodes() -> 'NodeMask'

Converts to a node mask containing all nodes in the mesh.

This method currently returns all nodes found in the underlying

mesh, regardless of which elements are selected in the current ElementMask. This behavior is due to an internal implementation detail where it iterates over all mesh elements' connectivity.

Returns:

Name Type Description
NodeMask 'NodeMask'

A new NodeMask containing all nodes incident to any element in the mesh.

Example
# Convert ElementMask to NodeMask containing elements' nodes
incident_nodes = selected_elements.to_nodes()
print(incident_nodes.to_list())
# Output: [0, 1, 2, 3]

to_tags

to_tags(start_tag: int | None = None) -> List[int]

Converts selected element IDs to OpenSees element tags.

This method offsets the existing explicit element IDs (which are already considered tags within Femora) by a start_tag value, primarily for compatibility with external tools like OpenSees that might use a different tagging scheme or require a specific offset.

Parameters:

Name Type Description Default
start_tag int | None

Optional. The value to add to each selected element ID. If None, it attempts to retrieve the starting element tag from a Model instance; otherwise, it defaults to 1.

None

Returns:

Type Description
List[int]

List[int]: A list of OpenSees-compatible element tags for the selected elements. These are the original element IDs plus the start_tag offset.

Example
# Convert ElementMask to integer tag list
print(selected_elements.to_tags(start_tag=500))
# Output: [510, 512]