Skip to content

Geometry

Tagged CAD: the geometry interface, tagged build123d shapes, cross-sections, the frame checks, tagged STEP import, the winding packer and build123d helpers.

femforge.geometry

Tagged CAD infrastructure: the GeometryArea interface, tagged build123d shapes, cross-sections, STEP import and the winding packer.

Modules:

  • authored –

    The authored bodies of a sectioned run, tessellated in the solve frame and written as geometry.authored.vtp.

  • build123d_geometry –

    A geometry authored as a build123d tagged assembly.

  • config –

    The parametric-geometry config area: a geometry the driver materializes into a STEP before meshing.

  • conformance –

    Checks that an authored model fits a sectioning frame: a solid of revolution about its axis, or a prism through its thickness.

  • section –

    Tagged cross-sections of tagged 3D solids: the quarter-wedge cutaway, and the axisymmetric and planar sections the 2D solves mesh.

  • step –

    Read an authored STEP into tagged parts.

  • tags –

    Tagged build123d shapes whose tags ride their STEP names into the mesh.

  • utils –

    build123d helpers: boundary boxes and cylinders, rectangles, assembly-tree pruning and collinearity checks.

  • winding –

    Round-wire winding packer: the orthocyclic layer stack a wire settles into in a rectangular window, every wire position, and the derived electrical quantities.

Classes:

  • Build123dGeometry –

    A GeometryArea whose STEP is the export of model, the tagged assembly in the authored frame.

  • GeometryArea –

    A parametric geometry the driver builds into a STEP in the authored frame; config.frame converts it for the solve.

Build123dGeometry pydantic-model

Bases: GeometryArea

A GeometryArea whose STEP is the export of model, the tagged assembly in the authored frame.

Config:

  • arbitrary_types_allowed: True
  • use_attribute_docstrings: True
  • extra: allow

model abstractmethod

model() -> TaggedCompound

The tagged assembly in the authored frame.

Source code in src/femforge/geometry/build123d_geometry.py
@abstractmethod
def model(self) -> TaggedCompound:
    """The tagged assembly in the authored frame."""

build_step

build_step(path: Path) -> Path

Export model as a STEP at path and return it.

Source code in src/femforge/geometry/build123d_geometry.py
def build_step(self, path: Path) -> Path:
    """Export [`model`][] as a STEP at ``path`` and return it."""
    self.model().export_step(str(path))
    return path

GeometryArea pydantic-model

Bases: ConfigArea

A parametric geometry the driver builds into a STEP in the authored frame; config.frame converts it for the solve.

build_step

build_step(path: Path) -> Path

Write the geometry as a STEP at path, in the authored frame, and return it.

Source code in src/femforge/geometry/config.py
def build_step(self, path: Path) -> Path:
    """Write the geometry as a STEP at ``path``, in the authored frame, and return it."""
    raise NotImplementedError

femforge.geometry.section

Tagged cross-sections of tagged 3D solids: the quarter-wedge cutaway, and the axisymmetric and planar sections the 2D solves mesh.

Functions:

  • cross_section_3D –

    Cut away the material on the positive side of every plane, copying each solid's tags and color; raises when the planes share no volume or the wedge consumes a solid whole.

  • solve_location –

    The placement that carries authored shapes into the solve frame of plane, the plane landing on Z = 0.

  • axisymmetric_cross_section –

    Section every tagged solid on the X >= 0 half of plane into the solve frame (X = radial >= 0, Y = along the axis, Z = 0), and every marker face into a named edge; a missed solid raises.

  • planar_cross_section –

    Section every tagged solid on plane into the solve frame (X = plane.x_dir, Y = plane.y_dir, Z = 0), and every marker face into a named edge; a missed solid raises.

  • export_section –

    Write section to the STEP step and its named edges to the edge file beside it, which the mesher reads back.

cross_section_3D

cross_section_3D(compound: TaggedCompound, planes: Iterable[Plane] = (YZ, XY), margin: float = 1.0) -> TaggedCompound

Cut away the material on the positive side of every plane, copying each solid's tags and color; raises when the planes share no volume or the wedge consumes a solid whole.

Parameters:

  • compound (TaggedCompound) –

    The tagged assembly to cut.

  • planes (Iterable[Plane], default: (YZ, XY) ) –

    The cutting planes; the default cuts away the X >= 0, Z >= 0 quarter wedge.

  • margin (float, default: 1.0 ) –

    How far the cutting boxes reach past the parts.

Source code in src/femforge/geometry/section.py
def cross_section_3D(
    compound: TaggedCompound,
    planes: Iterable[b3d.Plane] = (b3d.Plane.YZ, b3d.Plane.XY),
    margin: float = 1.0,
) -> TaggedCompound:
    """Cut away the material on the positive side of every plane, copying each solid's tags and color; raises when the planes share no volume or the wedge consumes a solid whole.

    Args:
        compound: The tagged assembly to cut.
        planes: The cutting planes; the default cuts away the X >= 0, Z >= 0 quarter wedge.
        margin: How far the cutting boxes reach past the parts.
    """

    def half_space_box(plane: b3d.Plane, center: b3d.Vector, radius: float, margin: float) -> b3d.Part:
        """A box covering the ball of `radius` around `center` on the positive side of `plane`, its wall exactly on the plane."""
        offset: b3d.Vector = center - plane.origin
        reach: float = max(offset.dot(plane.z_dir) + radius, margin)
        box = b3d.Box(2 * radius, 2 * radius, reach, align=(b3d.Align.CENTER, b3d.Align.CENTER, b3d.Align.MIN))
        return plane.location * b3d.Pos(offset.dot(plane.x_dir), offset.dot(plane.y_dir)) * box

    parts: list[TaggedPart] = [leaf for leaf in compound.placed_leaves() if isinstance(leaf, TaggedPart)]
    if not parts:
        raise ValueError("cross_section_3D needs at least one TaggedPart solid to section.")

    plane_list: list[b3d.Plane] = list(planes)
    if not plane_list:
        raise ValueError("cross_section_3D needs at least one plane to cut with.")

    # Bounds come from the parts, not compound.bounding_box() — a Compound's box can silently omit children.
    boxes: list[b3d.BoundBox] = [part.bounding_box() for part in parts]
    minimum = b3d.Vector(min(box.min.X for box in boxes), min(box.min.Y for box in boxes), min(box.min.Z for box in boxes))
    maximum = b3d.Vector(max(box.max.X for box in boxes), max(box.max.Y for box in boxes), max(box.max.Z for box in boxes))
    center: b3d.Vector = (minimum + maximum) / 2
    radius: float = (maximum - minimum).length / 2 + margin

    half_spaces: list[b3d.Part] = [half_space_box(plane, center, radius, margin) for plane in plane_list]
    wedge: b3d.Part = half_spaces[0]
    for half_space in half_spaces[1:]:
        intersect = cast("Callable[[b3d.Part], b3d.Part | None]", wedge.intersect)
        overlap: b3d.Part | None = intersect(half_space)
        if overlap is None or not overlap.solids():
            raise ValueError("the planes' positive half-spaces share no volume — there is no wedge to cut away.")

        wedge = b3d.Part(overlap.solids())

    sections: list[TaggedPart] = []
    for part in parts:
        cut = cast("Callable[[b3d.Part], b3d.Part]", part.cut)
        section: b3d.Part = cut(wedge)
        if not section.solids():
            raise ValueError(f"the sectioning wedge consumed the solid tagged {part.tags_str()!r} whole.")

        # A list, not section.wrapped: wrapping a bare TopoDS_Solid where a TopoDS_Compound belongs reads volume 0.
        tagged = TaggedPart([section], tags=part.current_tags)
        tagged.color = part.color
        sections.append(tagged)

    result: TaggedCompound = TaggedCompound(children=sections)
    result.label = f"3D Cross Section of {compound.label}"
    return result

solve_location

solve_location(plane: SolvePlane) -> Location

The placement that carries authored shapes into the solve frame of plane, the plane landing on Z = 0.

Source code in src/femforge/geometry/section.py
def solve_location(plane: SolvePlane) -> b3d.Location:
    """The placement that carries authored shapes into the solve frame of ``plane``, the plane landing on Z = 0."""
    x_dir: b3d.Vector = b3d.Vector(*plane.x_dir.to_tuple())
    y_dir: b3d.Vector = b3d.Vector(*plane.y_dir.to_tuple())
    solve_frame = b3d.Plane(origin=b3d.Vector(*plane.origin.to_tuple()), x_dir=x_dir, z_dir=x_dir.cross(y_dir))

    return solve_frame.location.inverse()

axisymmetric_cross_section

axisymmetric_cross_section(compound: TaggedCompound, plane: SolvePlane, margin: float = 1.0) -> TaggedCompound

Section every tagged solid on the X >= 0 half of plane into the solve frame (X = radial >= 0, Y = along the axis, Z = 0), and every marker face into a named edge; a missed solid raises.

Parameters:

Source code in src/femforge/geometry/section.py
def axisymmetric_cross_section(compound: TaggedCompound, plane: SolvePlane, margin: float = 1.0) -> TaggedCompound:
    """Section every tagged solid on the X >= 0 half of ``plane`` into the solve frame (X = radial >= 0, Y = along the axis, Z = 0), and every marker face into a named edge; a missed solid raises.

    Args:
        compound: The tagged assembly to section.
        plane: The solve plane, from [`Axisymmetric.plane`][femforge.frames.Axisymmetric.plane].
        margin: How far the half-plane reaches past the parts.
    """
    sections: list[TaggedFace | TaggedEdge] = _section(
        compound,
        plane,
        half=True,
        margin=margin,
        kind="axisymmetric",
    )

    for leaf in sections:
        box: b3d.BoundBox = leaf.bounding_box()

        if box.min.X < -_TOLERANCE:
            raise ValueError(f"the section tagged {leaf.tags_str()!r} reaches X = {box.min.X:g} < 0, which Elmer's axisymmetric solve rejects silently.")

    result: TaggedCompound = TaggedCompound(children=sections)
    result.label = f"Cross Section of {compound.label}"

    return result

planar_cross_section

planar_cross_section(compound: TaggedCompound, plane: SolvePlane, margin: float = 1.0) -> TaggedCompound

Section every tagged solid on plane into the solve frame (X = plane.x_dir, Y = plane.y_dir, Z = 0), and every marker face into a named edge; a missed solid raises.

Parameters:

Source code in src/femforge/geometry/section.py
def planar_cross_section(compound: TaggedCompound, plane: SolvePlane, margin: float = 1.0) -> TaggedCompound:
    """Section every tagged solid on ``plane`` into the solve frame (X = ``plane.x_dir``, Y = ``plane.y_dir``, Z = 0), and every marker face into a named edge; a missed solid raises.

    Args:
        compound: The tagged assembly to section.
        plane: The solve plane, from [`Cartesian2D.plane`][femforge.frames.Cartesian2D.plane].
        margin: How far the plane reaches past the parts.
    """
    sections: list[TaggedFace | TaggedEdge] = _section(
        compound,
        plane,
        half=False,
        margin=margin,
        kind="planar",
    )
    result: TaggedCompound = TaggedCompound(children=sections)
    result.label = f"Planar Section of {compound.label}"

    return result

export_section

export_section(section: TaggedCompound, step: Path) -> None

Write section to the STEP step and its named edges to the edge file beside it, which the mesher reads back.

Source code in src/femforge/geometry/section.py
def export_section(section: TaggedCompound, step: Path) -> None:
    """Write ``section`` to the STEP ``step`` and its named edges to the edge file beside it, which the mesher reads back."""
    from femforge.meshing.section_edges import CurveSpan, SectionEdge, SectionEdges

    def span(edge: TaggedEdge) -> CurveSpan:
        start: b3d.Vector = edge.start_point()
        end: b3d.Vector = edge.end_point()

        return CurveSpan(start=Vec3(start.X, start.Y, start.Z), end=Vec3(end.X, end.Y, end.Z), length=edge.length)

    section.export_step(str(step))
    edges: list[TaggedEdge] = [leaf for leaf in section.placed_leaves() if isinstance(leaf, TaggedEdge)]
    SectionEdges(edges=[SectionEdge(name=edge.tags_str(), span=span(edge)) for edge in edges]).write(step)

femforge.geometry.authored

The authored bodies of a sectioned run, tessellated in the solve frame and written as geometry.authored.vtp.

Classes:

  • AuthoredMesh –

    The triangles of the authored bodies, in millimetres in the solve frame.

Functions:

  • tessellate_bodies –

    Tessellate every authored body of compound placed in the solve frame of plane.

  • write_vtp –

    Write mesh as a zlib-compressed VTK PolyData file with its body index as a cell array, and return path.

  • write_authored_geometry –

    Write the authored bodies of compound in the solve frame of plane to path, coarsening past the triangle limit; None when no tessellation fits it.

Attributes:

  • AUTHORED_DEFLECTION –

    [-]: The tessellation tolerance per millimetre of the bounding-box diagonal of the model; build123d applies it relative to the size of each edge.

  • AUTHORED_ANGLE –

    [rad]: The largest angle between neighbouring facet normals of the tessellation.

  • AUTHORED_TRIANGLE_LIMIT –

    The most triangles the file may hold; a finer tessellation is retried coarser.

  • AUTHORED_RETRIES –

    How many times a tessellation over the limit is retried at twice the deflection before the file is skipped.

  • AUTHORED_BODY_ARRAY –

    The cell array that carries the index of the authored body each triangle belongs to.

AUTHORED_DEFLECTION module-attribute

AUTHORED_DEFLECTION = 0.001

[-]: The tessellation tolerance per millimetre of the bounding-box diagonal of the model; build123d applies it relative to the size of each edge.

AUTHORED_ANGLE module-attribute

AUTHORED_ANGLE = 0.3

[rad]: The largest angle between neighbouring facet normals of the tessellation.

AUTHORED_TRIANGLE_LIMIT module-attribute

AUTHORED_TRIANGLE_LIMIT = 200000

The most triangles the file may hold; a finer tessellation is retried coarser.

AUTHORED_RETRIES module-attribute

AUTHORED_RETRIES = 3

How many times a tessellation over the limit is retried at twice the deflection before the file is skipped.

AUTHORED_BODY_ARRAY module-attribute

AUTHORED_BODY_ARRAY = 'AuthoredBody'

The cell array that carries the index of the authored body each triangle belongs to.

AuthoredMesh dataclass

AuthoredMesh(points: NDArray[float64], triangles: NDArray[int64], bodies: NDArray[int64])

The triangles of the authored bodies, in millimetres in the solve frame.

Attributes:

points instance-attribute

points: NDArray[float64]

[mm]: The (n, 3) vertices.

triangles instance-attribute

triangles: NDArray[int64]

The (m, 3) vertex indices of each triangle.

bodies instance-attribute

bodies: NDArray[int64]

The index of the authored body each triangle belongs to.

tessellate_bodies

tessellate_bodies(compound: TaggedCompound, plane: SolvePlane, scale: float = 1.0) -> AuthoredMesh

Tessellate every authored body of compound placed in the solve frame of plane.

Parameters:

  • compound (TaggedCompound) –

    The authored model, in millimetres.

  • plane (SolvePlane) –

    The solve plane whose frame the bodies are placed in.

  • scale (float, default: 1.0 ) –

    The multiple of AUTHORED_DEFLECTION to tessellate at.

Source code in src/femforge/geometry/authored.py
def tessellate_bodies(compound: TaggedCompound, plane: SolvePlane, scale: float = 1.0) -> AuthoredMesh:
    """Tessellate every authored body of ``compound`` placed in the solve frame of ``plane``.

    Args:
        compound: The authored model, in millimetres.
        plane: The solve plane whose frame the bodies are placed in.
        scale: The multiple of ``AUTHORED_DEFLECTION`` to tessellate at.
    """
    from femforge.geometry.section import solve_location

    parts: list[TaggedPart] = [leaf for leaf in compound.placed_leaves(solve_location(plane)) if isinstance(leaf, TaggedPart)]
    box: b3d.BoundBox = compound.bounding_box()
    deflection: float = scale * AUTHORED_DEFLECTION * box.diagonal
    points: list[NDArray[np.float64]] = [np.zeros((0, 3))]
    triangles: list[NDArray[np.int64]] = [np.zeros((0, 3), dtype=np.int64)]
    bodies: list[NDArray[np.int64]] = [np.zeros(0, dtype=np.int64)]
    offset: int = 0

    for index, part in enumerate(parts):
        _clean(part)
        vertices, faces = part.tessellate(deflection, AUTHORED_ANGLE)
        corners: NDArray[np.float64] = np.array([(vertex.X, vertex.Y, vertex.Z) for vertex in vertices], dtype=np.float64).reshape(-1, 3)
        indices: NDArray[np.int64] = np.array(faces, dtype=np.int64).reshape(-1, 3)
        points.append(corners)
        triangles.append(indices + offset)
        bodies.append(np.full(len(indices), index, dtype=np.int64))
        offset += len(corners)

    return AuthoredMesh(points=np.concatenate(points), triangles=np.concatenate(triangles), bodies=np.concatenate(bodies))

write_vtp

write_vtp(mesh: AuthoredMesh, path: Path) -> Path

Write mesh as a zlib-compressed VTK PolyData file with its body index as a cell array, and return path.

Source code in src/femforge/geometry/authored.py
def write_vtp(mesh: AuthoredMesh, path: Path) -> Path:
    """Write ``mesh`` as a zlib-compressed VTK PolyData file with its body index as a cell array, and return ``path``."""
    count: int = len(mesh.triangles)
    connectivity: str = _encoded(mesh.triangles.astype(np.int32).ravel())
    offsets: str = _encoded((np.arange(1, count + 1) * 3).astype(np.int32))
    bodies: str = _encoded(mesh.bodies.astype(np.int32))
    points: str = _encoded(mesh.points.astype(np.float32).ravel())
    xml: str = (
        '<?xml version="1.0"?>\n'
        '<VTKFile type="PolyData" version="1.0" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">\n'
        f'<PolyData><Piece NumberOfPoints="{len(mesh.points)}" NumberOfPolys="{count}">\n'
        f'<CellData><DataArray type="Int32" Name="{AUTHORED_BODY_ARRAY}" format="binary">{bodies}</DataArray></CellData>\n'
        f'<Points><DataArray type="Float32" NumberOfComponents="3" format="binary">{points}</DataArray></Points>\n'
        f'<Polys><DataArray type="Int32" Name="connectivity" format="binary">{connectivity}</DataArray>\n'
        f'<DataArray type="Int32" Name="offsets" format="binary">{offsets}</DataArray></Polys>\n'
        "</Piece></PolyData></VTKFile>\n"
    )
    _ = path.write_bytes(xml.encode("ascii"))

    return path

write_authored_geometry

write_authored_geometry(compound: TaggedCompound, plane: SolvePlane, path: Path) -> Path | None

Write the authored bodies of compound in the solve frame of plane to path, coarsening past the triangle limit; None when no tessellation fits it.

Parameters:

  • compound (TaggedCompound) –

    The authored model, in millimetres.

  • plane (SolvePlane) –

    The solve plane whose frame the bodies are placed in.

  • path (Path) –

    The .vtp file to write.

Source code in src/femforge/geometry/authored.py
def write_authored_geometry(compound: TaggedCompound, plane: SolvePlane, path: Path) -> Path | None:
    """Write the authored bodies of ``compound`` in the solve frame of ``plane`` to ``path``, coarsening past the triangle limit; None when no tessellation fits it.

    Args:
        compound: The authored model, in millimetres.
        plane: The solve plane whose frame the bodies are placed in.
        path: The ``.vtp`` file to write.
    """
    scale: float = 1.0

    for _ in range(AUTHORED_RETRIES + 1):
        mesh: AuthoredMesh = tessellate_bodies(compound, plane, scale)

        if len(mesh.triangles) <= AUTHORED_TRIANGLE_LIMIT:
            return write_vtp(mesh, path)

        scale *= 2.0

    log.bind(path=str(path), triangles=len(mesh.triangles), limit=AUTHORED_TRIANGLE_LIMIT).warning("Authored geometry skipped; its tessellation stays over the triangle limit.")
    path.unlink(missing_ok=True)

    return None

femforge.geometry.conformance

Checks that an authored model fits a sectioning frame: a solid of revolution about its axis, or a prism through its thickness.

Functions:

  • revolution_mismatches –

    The TURNED mismatch of every body and named face of the compound, each name's leaves fused: its change when turned TURN_DEGREES about axis, a share of its volume or area.

  • prism_mismatches –

    The DEPTH and PRISM mismatches of every body, and the PRISM mismatch of every named face, of the authored compound sectioned on plane, each name's leaves fused.

Attributes:

  • MISMATCH_TOLERANCE (float) –

    [-]: The largest mismatch fraction that still fits the frame.

  • TURN_DEGREES (float) –

    [deg]: The turn about the symmetry axis; not a divisor of 360 and off the knots of a NURBS revolution, which a 90° turn lands on and stalls OCC.

  • END_INSET (float) –

    [-]: The share of the depth the end sections of a named face sit inside the model's ends.

MISMATCH_TOLERANCE module-attribute

MISMATCH_TOLERANCE: float = 0.0001

[-]: The largest mismatch fraction that still fits the frame.

TURN_DEGREES module-attribute

TURN_DEGREES: float = 37.0

[deg]: The turn about the symmetry axis; not a divisor of 360 and off the knots of a NURBS revolution, which a 90° turn lands on and stalls OCC.

END_INSET module-attribute

END_INSET: float = 0.001

[-]: The share of the depth the end sections of a named face sit inside the model's ends.

revolution_mismatches

revolution_mismatches(compound: TaggedCompound, axis: Axis) -> list[BodyMismatch]

The TURNED mismatch of every body and named face of the compound, each name's leaves fused: its change when turned TURN_DEGREES about axis, a share of its volume or area.

Source code in src/femforge/geometry/conformance.py
def revolution_mismatches(compound: TaggedCompound, axis: Axis) -> list[BodyMismatch]:
    """The ``TURNED`` mismatch of every body and named face of the ``compound``, each name's leaves fused: its change when turned ``TURN_DEGREES`` about ``axis``, a share of its volume or area."""
    started: float = time.perf_counter()
    turn = b3d.Axis(axis.origin.to_tuple(), axis.direction.to_tuple())
    leaves: list[TaggedPart | TaggedFace] = [leaf for leaf in compound.placed_leaves() if isinstance(leaf, (TaggedPart, TaggedFace))]

    def measure(tags: str, shape: b3d.Part | b3d.Compound) -> list[BodyMismatch]:
        measure_of: Callable[[AnyShape | None], float] = _volume if isinstance(shape, b3d.Part) else _area
        fraction: float = _symmetric(shape, shape.rotate(turn, TURN_DEGREES), measure_of) / measure_of(shape)

        return [BodyMismatch(body=tags, check=FrameCheck.TURNED, fraction=fraction)]

    return _measured(leaves, measure, FrameCheck.TURNED, started)

prism_mismatches

prism_mismatches(compound: TaggedCompound, plane: SolvePlane, thickness: Length) -> list[BodyMismatch]

The DEPTH and PRISM mismatches of every body, and the PRISM mismatch of every named face, of the authored compound sectioned on plane, each name's leaves fused.

A body is compared with its mid-depth section extruded through the model's depth, the bodies' joint extent along the normal; a named face must span that depth.

Parameters:

Source code in src/femforge/geometry/conformance.py
def prism_mismatches(compound: TaggedCompound, plane: SolvePlane, thickness: Length) -> list[BodyMismatch]:
    """The ``DEPTH`` and ``PRISM`` mismatches of every body, and the ``PRISM`` mismatch of every named face, of the authored ``compound`` sectioned on ``plane``, each name's leaves fused.

    A body is compared with its mid-depth section extruded through the model's depth, the bodies' joint extent along the normal; a named face must span that depth.

    Args:
        compound: The authored model.
        plane: The solve plane, from [`Cartesian2D.plane`][femforge.frames.Cartesian2D.plane].
        thickness: The depth each body must span along the normal.
    """
    started: float = time.perf_counter()
    leaves: list[TaggedPart | TaggedFace] = [leaf for leaf in compound.placed_leaves(solve_location(plane)) if isinstance(leaf, (TaggedPart, TaggedFace))]
    solid_boxes: list[b3d.BoundBox] = [leaf.bounding_box() for leaf in leaves if isinstance(leaf, TaggedPart)]

    if not solid_boxes:
        return []

    boxes: list[b3d.BoundBox] = [leaf.bounding_box() for leaf in leaves]
    z0: float = min(box.min.Z for box in solid_boxes)
    z1: float = max(box.max.Z for box in solid_boxes)
    depth: float = z1 - z0
    mid: float = (z0 + z1) / 2
    left: float = min(box.min.X for box in boxes) - _SHEET_MARGIN
    bottom: float = min(box.min.Y for box in boxes) - _SHEET_MARGIN
    width: float = max(box.max.X for box in boxes) + _SHEET_MARGIN - left
    height: float = max(box.max.Y for box in boxes) + _SHEET_MARGIN - bottom
    target: float = thickness.m_as("mm")

    def sheet(z: float) -> b3d.Sketch:
        return cast("b3d.Sketch", b3d.Pos(left, bottom, z) * b3d.Rectangle(width, height, align=(b3d.Align.MIN, b3d.Align.MIN)))

    def measure_body(tags: str, body: b3d.Part) -> list[BodyMismatch]:
        box: b3d.BoundBox = body.bounding_box()
        extent: float = box.max.Z - box.min.Z
        section = body & sheet(mid)
        faces: list[b3d.Face] = [] if section is None else list(section.faces())
        depth_mismatch = BodyMismatch(body=tags, check=FrameCheck.DEPTH, fraction=abs(extent - target) / target)

        if not faces:
            return [depth_mismatch, BodyMismatch(body=tags, check=FrameCheck.PRISM, fraction=1.0)]

        extruded_solids: list[b3d.Solid] = [b3d.Solid.extrude(face.moved(b3d.Location((0, 0, z0 - mid))), b3d.Vector(0, 0, depth)) for face in faces]
        fraction: float = _symmetric(body, b3d.Part(extruded_solids), _volume) / _volume(body)

        return [depth_mismatch, BodyMismatch(body=tags, check=FrameCheck.PRISM, fraction=fraction)]

    def measure_face(tags: str, surface: b3d.Compound) -> list[BodyMismatch]:
        reference: float = _length(surface & sheet(mid))

        if reference == 0.0:
            return [BodyMismatch(body=tags, check=FrameCheck.PRISM, fraction=1.0)]

        box: b3d.BoundBox = surface.bounding_box()
        span: float = max(abs(box.min.Z - z0), abs(box.max.Z - z1)) / depth
        ends: list[float] = [abs(_length(surface & sheet(z)) - reference) / reference for z in (z0 + END_INSET * depth, z1 - END_INSET * depth)]

        return [BodyMismatch(body=tags, check=FrameCheck.PRISM, fraction=max(span, *ends))]

    def measure(tags: str, shape: b3d.Part | b3d.Compound) -> list[BodyMismatch]:
        return measure_body(tags, shape) if isinstance(shape, b3d.Part) else measure_face(tags, shape)

    return _measured(leaves, measure, FrameCheck.PRISM, started)

femforge.geometry.step

Read an authored STEP into tagged parts.

Functions:

import_tagged_step

import_tagged_step(path: Path) -> TaggedCompound

The STEP's named solids as world-placed TaggedPart children and its named sheet faces as TaggedFace children, tagged from their name paths.

Source code in src/femforge/geometry/step.py
def import_tagged_step(path: Path) -> TaggedCompound:
    """The STEP's named solids as world-placed [`TaggedPart`][] children and its named sheet faces as [`TaggedFace`][] children, tagged from their name paths."""
    import build123d as b3d

    from femforge.geometry.tags import TaggedCompound, TaggedFace, TaggedPart

    bare: list[str] = []

    def tagged_leaves(node: b3d.Compound | b3d.Solid, inherited: TagDict) -> list[TaggedPart | TaggedFace]:
        tags: TagDict = {**inherited, **parse_tags(node.label)}
        children = cast("tuple[b3d.Compound | b3d.Solid, ...]", node.children)
        if children:
            return [leaf for child in children for leaf in tagged_leaves(child, tags)]

        if not tags:
            raise ValueError(f"{path}: a body has no key=value name to tag it by, so the mesh could not group it.")

        placed: b3d.Compound | b3d.Solid = node.located(node.global_location)
        if placed.solids():
            part = TaggedPart([*placed.solids()], tags=tags)
            part.color = node.color
            return [part]

        if not placed.faces():
            bare.append(node.label)
            return []

        faces: list[TaggedPart | TaggedFace] = []
        for face in placed.faces():
            tagged = TaggedFace(face, tags=tags)
            tagged.color = node.color
            faces.append(tagged)

        return faces

    read_step = cast("Callable[[Path], b3d.Compound]", b3d.import_step)
    imported: b3d.Compound = read_step(path)
    leaves: list[TaggedPart | TaggedFace] = tagged_leaves(imported, {})
    if not any(isinstance(leaf, TaggedPart) for leaf in leaves):
        raise ValueError(f"{path}: no body has a solid (an already-2D STEP); author the 3D model and let the frame section it.")

    if bare:
        raise ValueError(f"{path}: the entry {bare[0]!r} has neither a solid nor a face to mesh.")

    return TaggedCompound(children=leaves)

femforge.geometry.tags

Tagged build123d shapes whose tags ride their STEP names into the mesh.

Classes:

  • TaggedCompound –

    A build123d compound whose tags pass down to its tagged leaves on export.

  • TaggedFace –

    A build123d face whose tags ride its STEP name into the mesh: a body of a 2D model, or a named boundary group of a 3D one.

  • TaggedSketch –

    A build123d sketch that carries tags.

  • TaggedPart –

    A build123d part whose tags ride its STEP name into the mesh.

  • TaggedEdge –

    A build123d edge whose tags name a boundary group of a 2D model, such as the section of a named face.

Functions:

  • named –

    tags with name set as its TagKey.NAME tag, or tags unchanged when name is None.

TaggedCompound

TaggedCompound(*, children: Sequence[AnyShape] = (), tags: TagDict | None = None)

Bases: Compound, HasTags

A build123d compound whose tags pass down to its tagged leaves on export.

Methods:

  • rebuild_labels –

    Relabel every tagged part below this compound with its tags.

  • placed_leaves –

    Fresh copies of the tagged leaves at most one dimension below the highest, world-placed, moved by location, and carrying their ancestors' tags (deeper wins).

  • fingerprint –

    Order-independent SHA-256 of the placed leaves: kind, tags, color, size and quantized mass properties, faces and vertices.

  • flatten_and_tag –

    A new compound of the world-placed tagged leaves (see placed_leaves), labelled with their tags; self is left untouched.

  • export_step –

    Export the compound, flattened into its tagged leaves unless flatten is False, to the STEP file filename.

Attributes:

Source code in src/femforge/geometry/tags.py
def __init__(self, *, children: Sequence[AnyShape] = (), tags: TagDict | None = None) -> None:
    """Build the assembly over ``children`` and tag it with ``tags``, the ``key=value`` pairs every tagged leaf below inherits on export."""
    b3d.Compound.__init__(self, children=children)  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs
    HasTags.__init__(self, tags)
    self.label = self.tags_str()

children_at_global_location property

children_at_global_location: list[Any3D]

Return a list of children located at their global location.

rebuild_labels

rebuild_labels() -> None

Relabel every tagged part below this compound with its tags.

Source code in src/femforge/geometry/tags.py
def rebuild_labels(self) -> None:
    """Relabel every tagged part below this compound with its tags."""
    for child in cast("tuple[object, ...]", self.descendants):
        if isinstance(child, TaggedPart):
            child.label = child.tags_str()

placed_leaves

placed_leaves(location: Location | None = None) -> list[TaggedPart | TaggedFace | TaggedEdge]

Fresh copies of the tagged leaves at most one dimension below the highest, world-placed, moved by location, and carrying their ancestors' tags (deeper wins).

Solids keep their marker faces and faces their marker edges; edges under solids are construction geometry and dropped.

Source code in src/femforge/geometry/tags.py
def placed_leaves(self, location: b3d.Location | None = None) -> list["TaggedPart | TaggedFace | TaggedEdge"]:
    """Fresh copies of the tagged leaves at most one dimension below the highest, world-placed, moved by ``location``, and carrying their ancestors' tags (deeper wins).

    Solids keep their marker faces and faces their marker edges; edges under solids are construction geometry and dropped.
    """
    moved_by: b3d.Location = location if location is not None else b3d.Location()
    found: list[TaggedPart | TaggedFace | TaggedEdge] = [leaf for leaf in cast("tuple[object, ...]", self.descendants) if isinstance(leaf, (TaggedPart, TaggedFace, TaggedEdge))]
    kept_from: int = max((leaf.dim for leaf in found), default=0) - 1

    leaves: list[TaggedPart | TaggedFace | TaggedEdge] = []
    for leaf in found:
        if leaf.dim < kept_from:
            continue

        # A compound's own location never reaches its children's shapes, and leaf.moved() deep-copies the whole
        # parent tree, so the shapes are rebuilt with every ancestor's location applied.
        parent = cast("b3d.Compound | None", leaf.parent)
        ancestors: b3d.Location = parent.global_location if parent is not None else b3d.Location()
        placement: b3d.Location = moved_by * ancestors
        tags: TagDict = {key: value for node in leaf.path if isinstance(node, HasTags) for key, value in node.tags.items()}

        if isinstance(leaf, TaggedPart):
            placed: list[TaggedPart | TaggedFace | TaggedEdge] = [TaggedPart([solid.moved(placement) for solid in leaf.solids()], tags=tags)]
        elif isinstance(leaf, TaggedFace):
            placed = [TaggedFace(face.moved(placement), tags=tags) for face in leaf.faces()]
        else:
            placed = [TaggedEdge(edge.moved(placement), tags=tags) for edge in leaf.edges()]

        for copy in placed:
            copy.color = leaf.color

        leaves.extend(placed)

    return leaves

fingerprint

fingerprint() -> str

Order-independent SHA-256 of the placed leaves: kind, tags, color, size and quantized mass properties, faces and vertices.

Two imports of one STEP agree; an authored compound and its import agree as closely as OCCT integrates them alike.

Source code in src/femforge/geometry/tags.py
def fingerprint(self) -> str:
    """Order-independent SHA-256 of the placed leaves: kind, tags, color, size and quantized mass properties, faces and vertices.

    Two imports of one STEP agree; an authored compound and its import agree as closely as OCCT integrates them alike.
    """

    def rounded(value: float, scale: float) -> float:
        # + 0.0 folds -0.0 into 0.0, which json.dumps tells apart.
        return round(value / scale, 9) + 0.0

    def point(vector: b3d.Vector, scale: float) -> list[float]:
        return [rounded(coordinate, scale) for coordinate in vector]

    def describe(leaf: TaggedPart | TaggedFace | TaggedEdge) -> list[object]:
        box: b3d.BoundBox = leaf.bounding_box(optimal=True)
        scale: float = box.diagonal
        center: b3d.Vector = leaf.center(b3d.CenterOf.MASS)
        # STEP export drops transparency, so alpha is left out.
        rgba = None if leaf.color is None else cast("tuple[float, float, float, float]", tuple(leaf.color))
        color: list[float] | None = None if rgba is None else [rounded(channel, 1.0) for channel in rgba[:3]]
        volume: float | None = rounded(leaf.volume, scale**3) if isinstance(leaf, TaggedPart) else None
        area: float | None = None if isinstance(leaf, TaggedEdge) else rounded(leaf.area, scale**2)
        length: float | None = rounded(leaf.length, scale) if isinstance(leaf, TaggedEdge) else None
        tags: list[list[str]] = sorted([str(key), value] for key, value in leaf.tags.items())
        faces: list[list[float]] = sorted([rounded(face.area, scale**2), *point(face.center(b3d.CenterOf.MASS), scale)] for face in leaf.faces())
        vertices: list[list[float]] = sorted(point(vertex.center(), scale) for vertex in leaf.vertices())

        return [
            type(leaf).__name__,
            leaf.tags.get(TagKey.NAME),
            tags,
            color,
            float(f"{scale:.9g}"),
            volume,
            area,
            length,
            point(center, scale),
            point(box.min, scale),
            point(box.max, scale),
            faces,
            vertices,
        ]

    described: list[list[object]] = [describe(leaf) for leaf in self.placed_leaves()]
    return sha256_json(sorted(described, key=json.dumps))

flatten_and_tag

flatten_and_tag() -> Compound

A new compound of the world-placed tagged leaves (see placed_leaves), labelled with their tags; self is left untouched.

Source code in src/femforge/geometry/tags.py
def flatten_and_tag(self) -> b3d.Compound:
    """A new compound of the world-placed tagged leaves (see [`placed_leaves`][]), labelled with their tags; ``self`` is left untouched."""
    return b3d.Compound(children=self.placed_leaves(), label=f"Flattened_{self.label}")

export_step

export_step(filename: str, flatten: bool = True) -> None

Export the compound, flattened into its tagged leaves unless flatten is False, to the STEP file filename.

Source code in src/femforge/geometry/tags.py
def export_step(self, filename: str, flatten: bool = True) -> None:
    """Export the compound, flattened into its tagged leaves unless ``flatten`` is False, to the STEP file ``filename``."""
    compound_to_export: b3d.Compound = self.flatten_and_tag() if flatten else self
    _ = b3d.export_step(compound_to_export, filename)  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs

TaggedFace

TaggedFace(face: Face, *, tags: TagDict | None = None, name: str | None = None)

Bases: Face, HasTags

A build123d face whose tags ride its STEP name into the mesh: a body of a 2D model, or a named boundary group of a 3D one.

Parameters:

  • face (Face) –

    The face to wrap, placed where it is.

  • tags (TagDict | None, default: None ) –

    The key=value pairs domain materials and EntityTag selectors match, e.g. {TagKey.MATERIAL: N52().tag}.

  • name (str | None, default: None ) –

    The body or boundary-group name, kept as written in the Name tag and uppercased in the mesh, Elmer, entities.json and the viewer; None composes it from the tags.

Attributes:

  • dim (int) –

    The topological dimension of the leaf.

Source code in src/femforge/geometry/tags.py
def __init__(self, face: b3d.Face, *, tags: TagDict | None = None, name: str | None = None) -> None:
    """Wrap ``face`` and tag it.

    Args:
        face: The face to wrap, placed where it is.
        tags: The ``key=value`` pairs domain materials and ``EntityTag`` selectors match, e.g. ``{TagKey.MATERIAL: N52().tag}``.
        name: The body or boundary-group name, kept as written in the ``Name`` tag and uppercased in the mesh, Elmer, ``entities.json`` and the viewer; None composes it from the tags.
    """
    b3d.Face.__init__(self, cast("object", face.wrapped))  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs
    HasTags.__init__(self, named(tags, name))
    self.label = self.tags_str()

dim class-attribute

dim: int = 2

The topological dimension of the leaf.

TaggedSketch

TaggedSketch(shapes: Iterable[AnyShape] | None = None, *, tags: TagDict | None = None)

Bases: Sketch, HasTags

A build123d sketch that carries tags.

Source code in src/femforge/geometry/tags.py
def __init__(self, shapes: Iterable[AnyShape] | None = None, *, tags: TagDict | None = None) -> None:
    """Build the sketch from ``shapes``, such as faces, and tag it with ``tags``."""
    b3d.Sketch.__init__(self, shapes)  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs
    HasTags.__init__(self, tags)
    self.label = self.tags_str()

TaggedPart

TaggedPart(shapes: Iterable[AnyShape], *, tags: TagDict | None = None, name: str | None = None)

Bases: Part, HasTags

A build123d part whose tags ride its STEP name into the mesh.

Parameters:

  • shapes (Iterable[AnyShape]) –

    The solids the part holds, such as [box] or a build123d part.

  • tags (TagDict | None, default: None ) –

    The key=value pairs domain materials and EntityTag selectors match, e.g. {TagKey.MATERIAL: N52().tag}.

  • name (str | None, default: None ) –

    The body name, kept as written in the Name tag and uppercased in the mesh, Elmer, entities.json and the viewer; None composes it from the tags.

Attributes:

  • dim (int) –

    The topological dimension of the leaf.

Source code in src/femforge/geometry/tags.py
def __init__(self, shapes: Iterable[AnyShape], *, tags: TagDict | None = None, name: str | None = None) -> None:
    """Build the part from ``shapes`` and tag it.

    Args:
        shapes: The solids the part holds, such as ``[box]`` or a build123d part.
        tags: The ``key=value`` pairs domain materials and ``EntityTag`` selectors match, e.g. ``{TagKey.MATERIAL: N52().tag}``.
        name: The body name, kept as written in the ``Name`` tag and uppercased in the mesh, Elmer, ``entities.json`` and the viewer; None composes it from the tags.
    """
    b3d.Part.__init__(self, shapes)  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs
    HasTags.__init__(self, named(tags, name))
    self.label = self.tags_str()

dim class-attribute

dim: int = 3

The topological dimension of the leaf.

TaggedEdge

TaggedEdge(edge: Edge, *, tags: TagDict | None = None, name: str | None = None)

Bases: Edge, HasTags

A build123d edge whose tags name a boundary group of a 2D model, such as the section of a named face.

Parameters:

  • edge (Edge) –

    The edge to wrap, placed where it is.

  • tags (TagDict | None, default: None ) –

    The key=value pairs EntityTag selectors match.

  • name (str | None, default: None ) –

    The boundary-group name, kept as written in the Name tag and uppercased in the mesh, Elmer and entities.json; None composes it from the tags.

Attributes:

  • dim (int) –

    The topological dimension of the leaf.

Source code in src/femforge/geometry/tags.py
def __init__(self, edge: b3d.Edge, *, tags: TagDict | None = None, name: str | None = None) -> None:
    """Wrap ``edge`` and tag it.

    Args:
        edge: The edge to wrap, placed where it is.
        tags: The ``key=value`` pairs ``EntityTag`` selectors match.
        name: The boundary-group name, kept as written in the ``Name`` tag and uppercased in the mesh, Elmer and ``entities.json``; None composes it from the tags.
    """
    b3d.Edge.__init__(self, cast("object", edge.wrapped))  # pyright: ignore[reportUnknownMemberType]  # OCP ships no stubs
    HasTags.__init__(self, named(tags, name))
    self.label = self.tags_str()

dim class-attribute

dim: int = 1

The topological dimension of the leaf.

named

named(tags: TagDict | None, name: str | None) -> TagDict | None

tags with name set as its TagKey.NAME tag, or tags unchanged when name is None.

Source code in src/femforge/geometry/tags.py
def named(tags: TagDict | None, name: str | None) -> TagDict | None:
    """``tags`` with ``name`` set as its [`TagKey.NAME`][] tag, or ``tags`` unchanged when ``name`` is None."""
    return tags if name is None else {**(tags or {}), TagKey.NAME: name}

femforge.geometry.winding

Round-wire winding packer: the orthocyclic layer stack a wire settles into in a rectangular window, every wire position, and the derived electrical quantities.

Classes:

  • OuterDiameterFit –

    How the specified window OD bounds the layer stack.

  • LengthFit –

    How the wires occupy the specified window length.

  • Wire –

    Round magnet wire.

  • CoilWindow –

    One coil's revolved winding window: the annular slot between the mandrel and the coil OD.

  • WireTurn –

    One turn's centerline circle.

  • WoundCoil –

    One packed coil, computed by wind; every quantity derives from the wire and window alone.

OuterDiameterFit

Bases: StrEnum

How the specified window OD bounds the layer stack.

Attributes:

  • UNDER –

    Keep only layers that stay inside the specified OD: actual coil OD <= specified.

  • OVER –

    Stop after the first layer that reaches the specified OD: actual coil OD >= specified.

UNDER class-attribute instance-attribute

UNDER = 'under'

Keep only layers that stay inside the specified OD: actual coil OD <= specified.

OVER class-attribute instance-attribute

OVER = 'over'

Stop after the first layer that reaches the specified OD: actual coil OD >= specified.

LengthFit

Bases: StrEnum

How the wires occupy the specified window length.

Attributes:

  • TANGENT –

    The largest integer count of mutually tangent wires, centered in the window; an end gap likely remains.

  • SPLAYED –

    The base layer spans the window wall to wall: the same wire count, splayed to an axial pitch above the wire OD.

TANGENT class-attribute instance-attribute

TANGENT = 'tangent'

The largest integer count of mutually tangent wires, centered in the window; an end gap likely remains.

SPLAYED class-attribute instance-attribute

SPLAYED = 'splayed'

The base layer spans the window wall to wall: the same wire count, splayed to an axial pitch above the wire OD.

Wire dataclass

Wire(copper_diameter: float, insulation_diameter: float)

Round magnet wire.

Attributes:

copper_diameter instance-attribute

copper_diameter: float

d_cu [m]: Bare copper diameter.

insulation_diameter instance-attribute

insulation_diameter: float

d_ins [m]: Overall diameter including the enamel.

copper_area property

copper_area: float

a_cu [m²]: Bare copper cross-section.

insulation_area property

insulation_area: float

a_ins [m²]: Insulated-wire cross-section.

CoilWindow dataclass

CoilWindow(inner_radius: float, outer_radius: float, length: float)

One coil's revolved winding window: the annular slot between the mandrel and the coil OD.

Attributes:

  • inner_radius (float) –

    r_in [m]: Radius of the window's inner wall, where the first layer seats.

  • outer_radius (float) –

    r_out [m]: Radius of the window's outer wall (the CAD coil OD); the last layer may stand proud of it under OVER fit.

  • length (float) –

    w [m]: Axial length of the window: the coil width, which the geometry sets equal to the magnet length.

  • area (float) –

    A_w [m²]: The r-z cross-section of the window: radial depth × axial length.

  • mean_radius (float) –

    r_mean [m]: Mid-radius of the window.

  • volume (float) –

    V_w [m³]: The revolved window volume, 2π·r_mean·A_w.

inner_radius instance-attribute

inner_radius: float

r_in [m]: Radius of the window's inner wall, where the first layer seats.

outer_radius instance-attribute

outer_radius: float

r_out [m]: Radius of the window's outer wall (the CAD coil OD); the last layer may stand proud of it under OVER fit.

length instance-attribute

length: float

w [m]: Axial length of the window: the coil width, which the geometry sets equal to the magnet length.

area property

area: float

A_w [m²]: The r-z cross-section of the window: radial depth × axial length.

mean_radius property

mean_radius: float

r_mean [m]: Mid-radius of the window.

volume property

volume: float

V_w [m³]: The revolved window volume, 2π·r_mean·A_w.

WireTurn dataclass

WireTurn(layer: int, index: int, radius: float, axial_position: float)

One turn's centerline circle.

Attributes:

  • layer (int) –

    Zero-based layer index; layer 0 seats on the window's inner radius.

  • index (int) –

    Zero-based position along the layer.

  • radius (float) –

    r [m]: Radius of the turn's centerline.

  • axial_position (float) –

    z [m]: Axial position of the turn's center, measured from the window mid-plane.

layer instance-attribute

layer: int

Zero-based layer index; layer 0 seats on the window's inner radius.

index instance-attribute

index: int

Zero-based position along the layer.

radius instance-attribute

radius: float

r [m]: Radius of the turn's centerline.

axial_position instance-attribute

axial_position: float

z [m]: Axial position of the turn's center, measured from the window mid-plane.

WoundCoil dataclass

WoundCoil(wire: Wire, window: CoilWindow, outer_diameter_fit: OuterDiameterFit, length_fit: LengthFit, axial_pitch: float, radial_pitch: float, turns_per_layer: tuple[int, ...])

One packed coil, computed by wind; every quantity derives from the wire and window alone.

Methods:

  • wind –

    Pack the wire into the window: nested layers alternate n and n-1 turns, upper wires tangent in the grooves of the layer below.

  • wire_coordinates –

    Every turn's centerline circle, layer-major; odd layers sit in the grooves, offset by half the axial pitch.

  • coil_resistance –

    R_coil [Ω]: Per-coil DC resistance at temperature (K), by the same α law the FEA's σ(T) emits.

  • window_current_density –

    J [A/m²]: Homogenized azimuthal current density (signed): the ampere-turns spread over the specified window area — the FEA coil body's drive.

  • heat_density –

    q [W/m³]: Per-coil Joule density, I²·R_coil(T) over the revolved window volume.

  • wire_current_density –

    J_wire [A/m²]: Current density in the copper itself.

Attributes:

  • wire (Wire) –

    The wound wire.

  • window (CoilWindow) –

    The window the wire fills.

  • outer_diameter_fit (OuterDiameterFit) –

    Whether the stack stops under or just over the specified window OD.

  • length_fit (LengthFit) –

    Whether the base layer is tangent-packed or splayed wall to wall.

  • axial_pitch (float) –

    p_z [m]: Center-to-center spacing of neighbouring turns in a layer: the wire OD when tangent, wider when splayed.

  • radial_pitch (float) –

    p_r [m]: Center-to-center radial rise from one layer to the next, the groove-nested √(d² − (p_z/2)²).

  • turns_per_layer (tuple[int, ...]) –

    Turn count of each layer from the inside out; nested layers alternate n and n − 1.

  • turns (int) –

    N [-]: Wire turns in the coil.

  • num_layers (int) –

    The number of layers in the stack.

  • layer_radii (tuple[float, ...]) –

    [m]: Each layer's wire-center radius, layer 0 seated on the window's inner radius.

  • actual_outer_diameter (float) –

    [m]: The built coil's outer diameter: the outermost layer plus one wire radius.

  • actual_length (float) –

    [m]: The base layer's axial extent; upper layers are inset by half a pitch and never wider.

  • end_margin (float) –

    [m]: The gap between the base layer and each window wall (zero when splayed).

  • splay_gap (float) –

    [m]: The clearance between neighbouring wires in a layer (zero when tangent).

  • wire_length (float) –

    ℓ_wire [m]: Total wire length: each turn's circumference with the helical advance of one axial pitch.

  • copper_mass (float) –

    m_wire [kg]: Copper mass of the wire: length × bare-copper section × the Copper material's density.

  • copper_fill_factor (float) –

    f_cu [-]: Copper cross-section over the specified window cross-section.

  • mechanical_fill_factor (float) –

    f_mech [-]: Insulated-wire cross-section over the specified window cross-section; hex packing bounds it at π/(2√3) ≈ 0.907.

wire instance-attribute

wire: Wire

The wound wire.

window instance-attribute

window: CoilWindow

The window the wire fills.

outer_diameter_fit instance-attribute

outer_diameter_fit: OuterDiameterFit

Whether the stack stops under or just over the specified window OD.

length_fit instance-attribute

length_fit: LengthFit

Whether the base layer is tangent-packed or splayed wall to wall.

axial_pitch instance-attribute

axial_pitch: float

p_z [m]: Center-to-center spacing of neighbouring turns in a layer: the wire OD when tangent, wider when splayed.

radial_pitch instance-attribute

radial_pitch: float

p_r [m]: Center-to-center radial rise from one layer to the next, the groove-nested √(d² − (p_z/2)²).

turns_per_layer instance-attribute

turns_per_layer: tuple[int, ...]

Turn count of each layer from the inside out; nested layers alternate n and n − 1.

turns property

turns: int

N [-]: Wire turns in the coil.

num_layers property

num_layers: int

The number of layers in the stack.

layer_radii property

layer_radii: tuple[float, ...]

[m]: Each layer's wire-center radius, layer 0 seated on the window's inner radius.

actual_outer_diameter property

actual_outer_diameter: float

[m]: The built coil's outer diameter: the outermost layer plus one wire radius.

actual_length property

actual_length: float

[m]: The base layer's axial extent; upper layers are inset by half a pitch and never wider.

end_margin property

end_margin: float

[m]: The gap between the base layer and each window wall (zero when splayed).

splay_gap property

splay_gap: float

[m]: The clearance between neighbouring wires in a layer (zero when tangent).

wire_length property

wire_length: float

ℓ_wire [m]: Total wire length: each turn's circumference with the helical advance of one axial pitch.

copper_mass property

copper_mass: float

m_wire [kg]: Copper mass of the wire: length × bare-copper section × the Copper material's density.

copper_fill_factor property

copper_fill_factor: float

f_cu [-]: Copper cross-section over the specified window cross-section.

mechanical_fill_factor property

mechanical_fill_factor: float

f_mech [-]: Insulated-wire cross-section over the specified window cross-section; hex packing bounds it at π/(2√3) ≈ 0.907.

wind classmethod

wind(wire: Wire, window: CoilWindow, outer_diameter_fit: OuterDiameterFit = UNDER, length_fit: LengthFit = TANGENT) -> WoundCoil

Pack the wire into the window: nested layers alternate n and n-1 turns, upper wires tangent in the grooves of the layer below.

Parameters:

  • wire (Wire) –

    The wire to wind.

  • window (CoilWindow) –

    The window to fill.

  • outer_diameter_fit (OuterDiameterFit, default: UNDER ) –

    Whether the stack stops under or just over the window OD.

  • length_fit (LengthFit, default: TANGENT ) –

    Whether the base layer is tangent-packed or splayed wall to wall.

Source code in src/femforge/geometry/winding.py
@classmethod
def wind(
    cls,
    wire: Wire,
    window: CoilWindow,
    outer_diameter_fit: OuterDiameterFit = OuterDiameterFit.UNDER,
    length_fit: LengthFit = LengthFit.TANGENT,
) -> WoundCoil:
    """Pack the wire into the window: nested layers alternate n and n-1 turns, upper wires tangent in the grooves of the layer below.

    Args:
        wire: The wire to wind.
        window: The window to fill.
        outer_diameter_fit: Whether the stack stops under or just over the window OD.
        length_fit: Whether the base layer is tangent-packed or splayed wall to wall.
    """
    diameter = wire.insulation_diameter
    base_turns = math.floor(window.length / diameter + _COUNT_TOL)
    if base_turns < 2:
        raise ValueError(f"window length {window.length} m fits {base_turns} wires of OD {diameter} m; the nested-layer model needs at least 2 in the base layer.")

    if length_fit is LengthFit.TANGENT:
        axial_pitch = diameter
    else:
        axial_pitch = (window.length - diameter) / (base_turns - 1)

    # A groove wire touches both neighbours (center distance d) at a radial rise of sqrt(d^2 - (p/2)^2);
    # p < 2d always holds because base_turns is the floor count, so the sqrt stays real and positive.
    radial_pitch = math.sqrt(diameter**2 - (axial_pitch / 2) ** 2)

    seat_radius = window.inner_radius + diameter / 2
    turns_per_layer: list[int] = []
    layer = 0
    while True:
        build_radius = seat_radius + layer * radial_pitch + diameter / 2

        if outer_diameter_fit is OuterDiameterFit.UNDER and build_radius > window.outer_radius + _DIAMETER_TOL:
            break

        turns_per_layer.append(base_turns if layer % 2 == 0 else base_turns - 1)
        layer += 1

        if outer_diameter_fit is OuterDiameterFit.OVER and build_radius >= window.outer_radius - _DIAMETER_TOL:
            break

    if not turns_per_layer:
        raise ValueError(f"window radial depth {window.outer_radius - window.inner_radius} m cannot hold one layer of OD {diameter} m wire under {outer_diameter_fit}.")

    return cls(
        wire=wire,
        window=window,
        outer_diameter_fit=outer_diameter_fit,
        length_fit=length_fit,
        axial_pitch=axial_pitch,
        radial_pitch=radial_pitch,
        turns_per_layer=tuple(turns_per_layer),
    )

wire_coordinates

wire_coordinates() -> tuple[WireTurn, ...]

Every turn's centerline circle, layer-major; odd layers sit in the grooves, offset by half the axial pitch.

Source code in src/femforge/geometry/winding.py
def wire_coordinates(self) -> tuple[WireTurn, ...]:
    """Every turn's centerline circle, layer-major; odd layers sit in the grooves, offset by half the axial pitch."""
    base_turns = self.turns_per_layer[0]
    first_position = -(base_turns - 1) * self.axial_pitch / 2

    turns: list[WireTurn] = []
    for layer, (radius, count) in enumerate(zip(self.layer_radii, self.turns_per_layer)):
        start = first_position + (self.axial_pitch / 2 if layer % 2 else 0.0)
        for index in range(count):
            turns.append(WireTurn(layer=layer, index=index, radius=radius, axial_position=start + index * self.axial_pitch))
    return tuple(turns)

coil_resistance

coil_resistance(temperature: float = REFERENCE_TEMPERATURE) -> float

R_coil [Ω]: Per-coil DC resistance at temperature (K), by the same α law the FEA's σ(T) emits.

Source code in src/femforge/geometry/winding.py
def coil_resistance(self, temperature: float = REFERENCE_TEMPERATURE) -> float:
    """`R_coil` `[Ω]`: Per-coil DC resistance at ``temperature`` (K), by the same α law the FEA's σ(T) emits."""
    at_reference = self.wire_length / (CONDUCTIVITY_AT_REFERENCE * self.wire.copper_area)
    return at_reference * (1.0 + RESISTIVITY_TEMPERATURE_COEFFICIENT * (temperature - REFERENCE_TEMPERATURE))

window_current_density

window_current_density(current: float) -> float

J [A/m²]: Homogenized azimuthal current density (signed): the ampere-turns spread over the specified window area — the FEA coil body's drive.

Source code in src/femforge/geometry/winding.py
def window_current_density(self, current: float) -> float:
    """`J` `[A/m²]`: Homogenized azimuthal current density (signed): the ampere-turns spread over the specified window area — the FEA coil body's drive."""
    return self.turns * current / self.window.area

heat_density

heat_density(current: float, temperature: float = REFERENCE_TEMPERATURE) -> float

q [W/m³]: Per-coil Joule density, I²·R_coil(T) over the revolved window volume.

Source code in src/femforge/geometry/winding.py
def heat_density(self, current: float, temperature: float = REFERENCE_TEMPERATURE) -> float:
    """`q` `[W/m³]`: Per-coil Joule density, I²·R_coil(T) over the revolved window volume."""
    return current**2 * self.coil_resistance(temperature) / self.window.volume

wire_current_density

wire_current_density(current: float) -> float

J_wire [A/m²]: Current density in the copper itself.

Source code in src/femforge/geometry/winding.py
def wire_current_density(self, current: float) -> float:
    """`J_wire` `[A/m²]`: Current density in the copper itself."""
    return abs(current) / self.wire.copper_area

femforge.geometry.utils

build123d helpers: boundary boxes and cylinders, rectangles, assembly-tree pruning and collinearity checks.

Classes:

  • NestedRect –

    The frame-shaped region between an inner and an outer rectangle.

  • Rect –

    An axis-aligned 2D rectangle of build123d vectors, with edge and corner accessors.

Functions:

  • all_colliinear –

    Check if all points in the list are collinear within a given tolerance.

  • are_colliinear –

    Check if three points are collinear within a given tolerance.

  • boundary_box –

    A translucent box around shape's bounding box, grown by padding along each axis, half on each side.

  • boundary_cylinder –

    A cylinder about axis around shape's bounding box, its diameter and length grown by padding, half on each side.

  • collinear_ends –

    Assume points are collinear, find the furthest and closest point along that line

  • compound_topology –

    A rich tree of compound's descendants, by type and label.

  • create_debug_arrow –

    A debug arrow part on axis, for the viewer.

  • drop_collinear_points –

    points with interior points that continue the previous direction dropped (endpoints kept).

  • remove_where –

    Filter nodes based on criteria and automatically vacuum empty leftover compounds.

NestedRect

NestedRect(inside: Rect | None = None, outside: Rect | None = None, label: str = '')

The frame-shaped region between an inner and an outer rectangle.

Parameters:

  • inside (Rect | None, default: None ) –

    The inner rectangle; None starts an empty one.

  • outside (Rect | None, default: None ) –

    The outer rectangle; None starts an empty one.

  • label (str, default: '' ) –

    A name for the region and its faces; empty derives one from the object id.

Methods:

  • face –

    Return a face representing the area between the inside and outside rectangles.

  • extrude –

    Return a part representing the area between the inside and outside rectangles extruded to the given thickness.

Source code in src/femforge/geometry/utils.py
def __init__(self, inside: Rect | None = None, outside: Rect | None = None, label: str = ""):
    """Hold the two rectangles.

    Args:
        inside: The inner rectangle; None starts an empty one.
        outside: The outer rectangle; None starts an empty one.
        label: A name for the region and its faces; empty derives one from the object id.
    """

    self.label = label or f"NestedRect_{id(self)}"
    self.inside = inside or Rect(label=f"{self.label}.inside")
    self.outside = outside or Rect(label=f"{self.label}.outside")

face

face() -> Face

Return a face representing the area between the inside and outside rectangles.

Source code in src/femforge/geometry/utils.py
def face(self) -> b3d.Face:
    """Return a face representing the area between the inside and outside rectangles."""
    outer_wire = b3d.Rectangle(self.outside.width, self.outside.height).wire().translate(self.outside.center)
    inner_wire = b3d.Rectangle(self.inside.width, self.inside.height).wire().translate(self.inside.center)
    face = b3d.Face(outer_wire, [inner_wire], label=f"{self.label}_Face")
    return face

extrude

extrude(thickness: float) -> Part

Return a part representing the area between the inside and outside rectangles extruded to the given thickness.

Source code in src/femforge/geometry/utils.py
def extrude(self, thickness: float) -> b3d.Part:
    """Return a part representing the area between the inside and outside rectangles extruded to the given thickness."""
    face = self.face()
    part = b3d.extrude(face, amount=thickness)
    part.label = f"{self.label}_Part"
    return part

Rect

Rect(min: V | None = None, max: V | None = None, label: str = '')

An axis-aligned 2D rectangle of build123d vectors, with edge and corner accessors.

Parameters:

  • min (V | None, default: None ) –

    The lower-left corner; None is the origin.

  • max (V | None, default: None ) –

    The upper-right corner; None is the origin.

  • label (str, default: '' ) –

    A name shown in the repr.

Attributes:

  • left (float) –

    The x of the left edge; setting it moves only that edge.

  • right (float) –

    The x of the right edge; setting it moves only that edge.

  • bottom (float) –

    The y of the bottom edge; setting it moves only that edge.

  • top (float) –

    The y of the top edge; setting it moves only that edge.

  • width (float) –

    The extent along x.

  • height (float) –

    The extent along y.

  • size (V) –

    The width and height as a vector.

  • center (V) –

    The midpoint.

  • corners (list[V]) –

    The four corners, counter-clockwise from min.

  • points (list[V]) –

    The four corners, the same as corners.

Source code in src/femforge/geometry/utils.py
def __init__(self, min: V | None = None, max: V | None = None, label: str = ""):
    """Span ``min`` to ``max``.

    Args:
        min: The lower-left corner; None is the origin.
        max: The upper-right corner; None is the origin.
        label: A name shown in the repr.
    """
    self.min = min or V()
    self.max = max or V()
    self.label = label

left property writable

left: float

The x of the left edge; setting it moves only that edge.

right property writable

right: float

The x of the right edge; setting it moves only that edge.

bottom property writable

bottom: float

The y of the bottom edge; setting it moves only that edge.

top property writable

top: float

The y of the top edge; setting it moves only that edge.

width property

width: float

The extent along x.

height property

height: float

The extent along y.

size property

size: V

The width and height as a vector.

center property

center: V

The midpoint.

corners property

corners: list[V]

The four corners, counter-clockwise from min.

points property

points: list[V]

The four corners, the same as corners.

all_colliinear

all_colliinear(points: list[V], tolerance: float = 1e-09) -> bool

Check if all points in the list are collinear within a given tolerance.

Source code in src/femforge/geometry/utils.py
def all_colliinear(points: list[V], tolerance: float = 1e-9) -> bool:
    """Check if all points in the list are collinear within a given tolerance."""
    if len(points) < 3:
        return True  # Less than 3 points are always collinear

    p1 = points[0]
    p2 = points[1]

    for i in range(2, len(points)):
        p3 = points[i]
        if not are_colliinear(p1, p2, p3, tolerance):
            return False

    return True

are_colliinear

are_colliinear(p1: V, p2: V, p3: V, tolerance: float = 1e-09) -> bool

Check if three points are collinear within a given tolerance.

Parameters:

  • p1 (V) –

    The first point.

  • p2 (V) –

    The middle point.

  • p3 (V) –

    The last point.

  • tolerance (float, default: 1e-09 ) –

    The largest cross product of the two segments still counted as collinear.

Source code in src/femforge/geometry/utils.py
def are_colliinear(p1: V, p2: V, p3: V, tolerance: float = 1e-9) -> bool:
    """Check if three points are collinear within a given tolerance.

    Args:
        p1: The first point.
        p2: The middle point.
        p3: The last point.
        tolerance: The largest cross product of the two segments still counted as collinear.
    """
    v1 = p2 - p1
    v2 = p3 - p2
    cross_product = v1.cross(v2)
    return cross_product.length <= tolerance

boundary_box

boundary_box(shape: Compound, padding: Vec3 = Vec3(0, 0, 0)) -> Part

A translucent box around shape's bounding box, grown by padding along each axis, half on each side.

Source code in src/femforge/geometry/utils.py
def boundary_box(shape: b3d.Compound, padding: Vec3 = Vec3(0, 0, 0)) -> b3d.Part:
    """A translucent box around ``shape``'s bounding box, grown by ``padding`` along each axis, half on each side."""
    half_pad = b3d.Vector(padding.x, padding.y, padding.z) * 0.5

    bounds = shape.bounding_box()
    min_pt = bounds.min - half_pad
    max_pt = bounds.max + half_pad
    size = max_pt - min_pt

    with b3d.BuildPart(mode=b3d.Mode.PRIVATE) as box:
        with b3d.BuildSketch(b3d.Plane.XY.offset(min_pt.Z)):
            with b3d.Locations(min_pt):
                b3d.Rectangle(size.X, size.Y, align=(b3d.Align.MIN, b3d.Align.MIN))

        b3d.extrude(amount=size.Z, dir=b3d.Axis.Z.direction)

    assert box.part is not None  # BuildPart always yields a part after the context

    result = box.part
    result.color = b3d.Color(1, 1, 1, 0.2)
    result.label = "Boundary Box"
    return result

boundary_cylinder

boundary_cylinder(shape: Compound, axis: Axis = Z, padding: float = 0.0) -> Part

A cylinder about axis around shape's bounding box, its diameter and length grown by padding, half on each side.

Parameters:

  • shape (Compound) –

    The bodies the cylinder bounds.

  • axis (Axis, default: Z ) –

    The cylinder's axis, the symmetry axis of an axisymmetric model.

  • padding (float, default: 0.0 ) –

    How much the diameter and the length grow past the bounding box, in millimetres.

Source code in src/femforge/geometry/utils.py
def boundary_cylinder(shape: b3d.Compound, axis: Axis = Axis.Z, padding: float = 0.0) -> b3d.Part:
    """A cylinder about ``axis`` around ``shape``'s bounding box, its diameter and length grown by ``padding``, half on each side.

    Args:
        shape: The bodies the cylinder bounds.
        axis: The cylinder's axis, the symmetry axis of an axisymmetric model.
        padding: How much the diameter and the length grow past the bounding box, in millimetres.
    """
    bounds: b3d.BoundBox = shape.bounding_box()
    origin = b3d.Vector(*axis.origin.to_tuple())
    direction = b3d.Vector(*axis.direction.to_tuple())
    corners: list[b3d.Vector] = [b3d.Vector(x, y, z) - origin for x in (bounds.min.X, bounds.max.X) for y in (bounds.min.Y, bounds.max.Y) for z in (bounds.min.Z, bounds.max.Z)]
    heights: list[float] = [corner.dot(direction) for corner in corners]
    radius: float = max((corner - direction * height).length for corner, height in zip(corners, heights, strict=True))
    low: float = min(heights)
    high: float = max(heights)
    placement: b3d.Location = b3d.Plane(origin=origin + direction * ((low + high) / 2), z_dir=direction).location

    result = cast(b3d.Part, placement * b3d.Cylinder(radius + padding / 2, high - low + padding))
    result.color = b3d.Color(1, 1, 1, 0.2)
    result.label = "Boundary Cylinder"

    return result

collinear_ends

collinear_ends(points: list[V]) -> list[V]

Assume points are collinear, find the furthest and closest point along that line

Source code in src/femforge/geometry/utils.py
def collinear_ends(points: list[V]) -> list[V]:
    """Assume points are collinear, find the furthest and closest point along that line"""
    if len(points) < 2:
        return points

    # Use the first two points to define the line
    p1 = points[0]
    p2 = points[1]
    direction = (p2 - p1).normalized()

    # Project all points onto the line defined by p1 and direction
    projections = [(point, (point - p1).dot(direction)) for point in points]

    # Find the minimum and maximum projections
    min_proj = min(projections, key=lambda x: x[1])
    max_proj = max(projections, key=lambda x: x[1])

    return [min_proj[0], max_proj[0]]

compound_topology

compound_topology(compound: Compound)

A rich tree of compound's descendants, by type and label.

Source code in src/femforge/geometry/utils.py
def compound_topology(compound: b3d.Compound):
    """A rich tree of ``compound``'s descendants, by type and label."""
    from rich.tree import Tree

    tree = Tree(f"Compound: {compound.label or 'Unnamed'}")

    def walk(node: b3d.Compound, parent_tree: Tree):
        for child in cast("tuple[b3d.Compound, ...]", node.children):
            label = f"{type(child).__name__}: {child.label or 'Unnamed'}"
            child_tree = parent_tree.add(label)
            if isinstance(child, b3d.Compound):
                walk(child, child_tree)

    walk(compound, tree)
    return tree

create_debug_arrow

create_debug_arrow(axis: Axis, length: float = 10, radius: float = 0.2, color: Color = Color('cyan')) -> Part

A debug arrow part on axis, for the viewer.

Parameters:

  • axis (Axis) –

    The axis the arrow starts on and points along.

  • length (float, default: 10 ) –

    The overall length.

  • radius (float, default: 0.2 ) –

    The stem radius; the head is 2.5 times wider.

  • color (Color, default: Color('cyan') ) –

    The arrow's color.

Source code in src/femforge/geometry/utils.py
def create_debug_arrow(  # pragma: no cover - viewer-only debug geometry
    axis: b3d.Axis,
    length: float = 10,
    radius: float = 0.2,
    color: b3d.Color = b3d.Color("cyan"),
) -> b3d.Part:
    """A debug arrow part on ``axis``, for the viewer.

    Args:
        axis: The axis the arrow starts on and points along.
        length: The overall length.
        radius: The stem radius; the head is 2.5 times wider.
        color: The arrow's color.
    """
    arrow_compound = b3d.Compound()

    proportion_body = 0.85
    proportion_head = 1 - proportion_body

    with b3d.BuildPart() as arrow:
        # Arrow stem
        b3d.Cylinder(
            radius=radius,
            height=length * proportion_body,
            align=(b3d.Align.CENTER, b3d.Align.CENTER, b3d.Align.MIN),
        )

        # Arrow head
        with b3d.Locations((0, 0, length * proportion_body)):
            b3d.Cone(
                bottom_radius=radius * 2.5,
                top_radius=0,
                height=length * proportion_head,
                align=(b3d.Align.CENTER, b3d.Align.CENTER, b3d.Align.MIN),
            )

    arrow_compound += arrow.part
    arrow_compound.color = color
    arrow_compound.label = "Debug Arrow"
    # build123d's Compound.locate() is typed as a Compound | Wire union; this is always a solid Part.
    return cast(b3d.Part, arrow_compound.locate(axis.location))

drop_collinear_points

drop_collinear_points(points: list[V]) -> list[V]

points with interior points that continue the previous direction dropped (endpoints kept).

Source code in src/femforge/geometry/utils.py
def drop_collinear_points(points: list[V]) -> list[V]:
    """``points`` with interior points that continue the previous direction dropped (endpoints kept)."""
    kept: list[V] = points[:1]
    for index in range(1, len(points) - 1):
        if not are_colliinear(points[index - 1], points[index], points[index + 1]):
            kept.append(points[index])

    kept.append(points[-1])
    return kept

remove_where

remove_where(assembly: Compound, criteria_func: Callable[[Compound], bool]) -> Compound

Filter nodes based on criteria and automatically vacuum empty leftover compounds.

Source code in src/femforge/geometry/utils.py
def remove_where(assembly: b3d.Compound, criteria_func: Callable[[b3d.Compound], bool]) -> b3d.Compound:
    """Filter nodes based on criteria and automatically vacuum empty leftover compounds."""

    nodes_to_remove = set(node for node in _tree_nodes(assembly, PostOrderIter) if criteria_func(node))

    for node in _tree_nodes(assembly, PostOrderIter):
        children = cast("tuple[b3d.Compound, ...]", node.children)
        kept = [child for child in children if child not in nodes_to_remove]
        if children:
            node.children = kept

        if isinstance(node, b3d.Compound) and not kept and len(node.solids()) == 0 and node != assembly:
            nodes_to_remove.add(node)

    return assembly