From 453e07057f4d316f946ff4dc97257f3996ec4204 Mon Sep 17 00:00:00 2001 From: "Mitchell.Tesch" Date: Wed, 4 Nov 2026 09:38:16 +1000 Subject: [PATCH 1/2] Make internal hinge detection independent of element order Since 102cb38, check_internal_hinges() runs on every add_element(). It released the remaining elements of a node as soon as the elements added so far met the hinge criteria, and never undid this. A joint where only one of three elements is released became a hinge if the released element was added before the third one (#464). check_internal_hinges() now records the releases it adds and undoes them before re-evaluating a node. Internal hinges are matched by node id, as the post-processing replaces the node objects, and add_element() copies the spring dict, as it may be shared by several elements. Fixes #464 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- anastruct/fem/system.py | 14 +++++-- anastruct/fem/system_components/util.py | 54 ++++++++++++++++--------- tests/test_e2e.py | 50 +++++++++++++++++++++++ 3 files changed, 95 insertions(+), 23 deletions(-) diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index ae30419f..6ef22f69 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -128,6 +128,12 @@ def __init__( self.supports_hinged: List[Node] = [] self.supports_rotational: List[Node] = [] self.internal_hinges: List[Node] = [] + # node ids passed to add_internal_hinge(), and the releases that check_internal_hinges() + # added per node as (element id, element end, original spring) + self._explicit_internal_hinges: Set[int] = set() + self._hinge_releases: Dict[ + int, List[Tuple[int, Literal[1, 2], Optional[float]]] + ] = {} self.supports_roll: List[Node] = [] self.supports_spring_x: List[Tuple[Node, bool]] = [] self.supports_spring_y: List[Tuple[Node, bool]] = [] @@ -388,8 +394,8 @@ def add_element( if mp is None: mp = {} - if spring is None: - spring = {} + # Copy, as hinge releases are added to the springs and a dict may be shared by elements + spring = {} if spring is None else dict(spring) element_type = kwargs.get("element_type", "general") @@ -1062,7 +1068,9 @@ def add_internal_hinge(self, node_id: Union[int, Sequence[int]]) -> None: id_ = _negative_index_to_id(id_, self.node_map.keys()) # add the support to the support list for the plotter - self.internal_hinges.append(self.node_map[id_]) + if not any(node.id == id_ for node in self.internal_hinges): + self.internal_hinges.append(self.node_map[id_]) + self._explicit_internal_hinges.add(id_) def add_support_roll( self, diff --git a/anastruct/fem/system_components/util.py b/anastruct/fem/system_components/util.py index 3c35765c..fc30d10f 100644 --- a/anastruct/fem/system_components/util.py +++ b/anastruct/fem/system_components/util.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Optional, Sequence, Tuple, Union +from typing import TYPE_CHECKING, Literal, Optional, Sequence, Tuple, Union import numpy as np @@ -14,6 +14,9 @@ def check_internal_hinges(system: "SystemElements", node_id: int) -> None: """Identify internal hinges, set their hinge status + The releases added by a previous call are undone first, so the result only depends on + the elements currently connected to the node, not on the order in which they were added. + Args: system (SystemElements): System in which the node is located node_id (int): Id of the node in the system @@ -21,6 +24,15 @@ def check_internal_hinges(system: "SystemElements", node_id: int) -> None: node = system.node_map[node_id] + for el_id, released_end, original in system._hinge_releases.pop(node_id, []): + if el_id in system.element_map: + springs = system.element_map[el_id].springs + assert springs is not None + if original is None: + springs.pop(released_end) + else: + springs[released_end] = original + hinges = [] for el_id in node.elements: el = system.element_map[el_id] @@ -34,26 +46,27 @@ def check_internal_hinges(system: "SystemElements", node_id: int) -> None: # If at least one element is connected # and no more than one element is rigidly connected - if (1 < len(hinges) <= 1 + sum(hinges)) or (len(hinges) == 1 and sum(hinges) == 1): - system.internal_hinges.append(node) + is_hinge = node_id in system._explicit_internal_hinges or ( + (1 < len(hinges) <= 1 + sum(hinges)) or (len(hinges) == 1 and sum(hinges) == 1) + ) - if node in system.internal_hinges: - node.hinge = True + # Match by id, as the post-processing replaces the node objects + system.internal_hinges[:] = [n for n in system.internal_hinges if n.id != node_id] + node.hinge = is_hinge + + if is_hinge: + system.internal_hinges.append(node) # If the elements aren't already all hinged, then set them to be - if len(hinges) - sum(hinges) >= 1: - for el_id in node.elements: - el = system.element_map[el_id] - assert el.springs is not None - if node_id == el.node_id1 and not ( - 1 in el.springs and el.springs[1] == 0 - ): - el.springs.update({1: 0}) - if node_id == el.node_id2 and not ( - 2 in el.springs and el.springs[2] == 0 - ): - el.springs.update({2: 0}) - system.element_map[el_id].springs = el.springs + releases = [] + for el_id in node.elements: + el = system.element_map[el_id] + assert el.springs is not None + end: Literal[1, 2] = 1 if node_id == el.node_id1 else 2 + if el.springs.get(end) != 0: + releases.append((el_id, end, el.springs.get(end))) + el.springs[end] = 0 + system._hinge_releases[node_id] = releases def append_node_id( @@ -105,8 +118,9 @@ def remove_node_id( system.supports_hinged.remove(node) if node in system.supports_rotational: system.supports_rotational.remove(node) - if node in system.internal_hinges: - system.internal_hinges.remove(node) + system.internal_hinges[:] = [n for n in system.internal_hinges if n.id != node_id] + system._explicit_internal_hinges.discard(node_id) + system._hinge_releases.pop(node_id, None) if node in system.supports_roll: ind = system.supports_roll.index(node) system.supports_roll.remove(node) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 4c5b3b24..ba5dfdf1 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -577,6 +577,56 @@ def it_results_in_symmetric_element_demands(): system.get_element_results(2)["Mmax"] ) + def describe_internal_hinge_independent_of_element_order(): + # Test bug fix for a joint with only one of three elements released being treated as + # an internal hinge, depending on the order in which the elements are added (#464) + + release = {1: 0} + elements = { + "left": ([[0, 4], [4, 4]], None), + "right": ([[4, 4], [8, 4]], release), + "column": ([[4, 4], [4, 0]], None), + } + + def _build(order): + system = SystemElements() + ids = { + name: system.add_element(elements[name][0], spring=elements[name][1]) + for name in order + } + for location in ([0, 4], [8, 4], [4, 0]): + system.add_support_fixed(system.find_node_id(location)) + system.q_load(q=-10, element_id=[ids["left"], ids["right"]]) + system.solve() + return system, ids + + systems = [ + _build(order) + for order in ( + ["left", "column", "right"], + ["left", "right", "column"], + ["right", "left", "column"], + ) + ] + + def it_does_not_create_an_internal_hinge(): + for system, ids in systems: + assert system.internal_hinges == [] + assert system.element_map[ids["left"]].springs == {} + assert system.element_map[ids["column"]].springs == {} + assert release == {1: 0} + + def it_results_in_the_same_moments_for_any_element_order(): + moments = [] + for system, ids in systems: + node_id = system.find_node_id([4, 4]) + m_left = system.element_map[ids["left"]].node_map[node_id].Tz + m_column = system.element_map[ids["column"]].node_map[node_id].Tz + assert m_left == approx(-m_column) + moments.append(m_left) + assert moments[0] != approx(0) + assert moments == approx([moments[0]] * len(moments)) + def context_q_and_q_perp_loads(): # Test addition of simultaneous q and q_perp loads From 39da7f8cf31f70a5fbb7d2c42d90a63ffea37043 Mon Sep 17 00:00:00 2001 From: "Mitchell.Tesch" Date: Mon, 23 Nov 2026 11:36:59 +1000 Subject: [PATCH 2/2] Only treat a node as an internal hinge when every element is released check_internal_hinges() treated a node as an internal hinge when at most one element was rigidly connected to it, and released that element too by writing 0 into its springs. As it runs on every add_element(), the result depended on the order in which the elements were added (#464). A node is now an internal hinge only if every element connected to it is released there, or it was passed to add_internal_hinge(). Its rotation is then restrained, and element springs are never modified. add_internal_hinge() nodes are kept by discretize(), and solve() raises for a moment load on an internal hinge whose rotation no support restrains, instead of silently losing it. --- anastruct/fem/system.py | 19 +++--- anastruct/fem/system_components/util.py | 72 +++++++++++---------- tests/test_e2e.py | 83 +++++++++++++++++++++++-- 3 files changed, 128 insertions(+), 46 deletions(-) diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index 6ef22f69..3bc9dec0 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -128,12 +128,8 @@ def __init__( self.supports_hinged: List[Node] = [] self.supports_rotational: List[Node] = [] self.internal_hinges: List[Node] = [] - # node ids passed to add_internal_hinge(), and the releases that check_internal_hinges() - # added per node as (element id, element end, original spring) + # node ids passed to add_internal_hinge() self._explicit_internal_hinges: Set[int] = set() - self._hinge_releases: Dict[ - int, List[Tuple[int, Literal[1, 2], Optional[float]]] - ] = {} self.supports_roll: List[Node] = [] self.supports_spring_x: List[Tuple[Node, bool]] = [] self.supports_spring_y: List[Tuple[Node, bool]] = [] @@ -394,8 +390,8 @@ def add_element( if mp is None: mp = {} - # Copy, as hinge releases are added to the springs and a dict may be shared by elements - spring = {} if spring is None else dict(spring) + if spring is None: + spring = {} element_type = kwargs.get("element_type", "general") @@ -499,6 +495,8 @@ def remove_element(self, element_id: int) -> None: # Check if node is now orphaned, and remove it and its loads if so if len(self.node_map[node_id].elements) == 0: system_components.util.remove_node_id(self, node_id) + else: + system_components.util.check_internal_hinges(self, node_id) # Remove element_id self.element_map.pop(element_id) @@ -897,6 +895,7 @@ def solve( Raises: FEMException: The eigenvalues of the stiffness matrix are non zero, which indicates an unstable structure. Check your support conditions + FEMException: A moment load is applied to an internal hinge that no support restrains. Returns: np.ndarray: Displacements vector. @@ -904,6 +903,7 @@ def solve( for node_id in self.node_map: system_components.util.check_internal_hinges(self, node_id) + system_components.util.check_internal_hinge_moment_loads(self) if self.system_displacement_vector is None: system_components.assembly.process_supports(self) @@ -1055,7 +1055,8 @@ def add_support_rotational(self, node_id: Union[int, Sequence[int]]) -> None: def add_internal_hinge(self, node_id: Union[int, Sequence[int]]) -> None: """Model a internal hinge at a given node. - This may alternatively be done by setting a spring restraint to zero (`{1: 0}` or `{2: 0}`). + This may alternatively be done by setting a spring restraint to zero (`{1: 0}` or `{2: 0}`) + at this node on every element connected to it. The effect is the same, though this function may be easier to use. Args: @@ -2268,6 +2269,8 @@ def discretize(self, n: int = 10) -> None: ss.add_support_rotational((node.id - 1) * n + 1) for args in self.supports_spring_args: ss.add_support_spring((args[0] - 1) * n + 1, *args[1:]) + for node_id in self._explicit_internal_hinges: + ss.add_internal_hinge((node_id - 1) * n + 1) # loads for node_id, forces in self.loads_point.items(): diff --git a/anastruct/fem/system_components/util.py b/anastruct/fem/system_components/util.py index fc30d10f..e50ac573 100644 --- a/anastruct/fem/system_components/util.py +++ b/anastruct/fem/system_components/util.py @@ -14,8 +14,13 @@ def check_internal_hinges(system: "SystemElements", node_id: int) -> None: """Identify internal hinges, set their hinge status - The releases added by a previous call are undone first, so the result only depends on - the elements currently connected to the node, not on the order in which they were added. + A node is an internal hinge if it was passed to `add_internal_hinge()`, or if every + element connected to it is released (k=0) at that node. Nothing then restrains the + rotation of the node, so it is restrained instead (see `process_supports()`), which + keeps the stiffness matrix non-singular without changing any element forces. + + The springs of the elements are not modified, so the result only depends on the + elements currently connected to the node, not on the order in which they were added. Args: system (SystemElements): System in which the node is located @@ -24,49 +29,51 @@ def check_internal_hinges(system: "SystemElements", node_id: int) -> None: node = system.node_map[node_id] - for el_id, released_end, original in system._hinge_releases.pop(node_id, []): - if el_id in system.element_map: - springs = system.element_map[el_id].springs - assert springs is not None - if original is None: - springs.pop(released_end) - else: - springs[released_end] = original - - hinges = [] + released = [] for el_id in node.elements: el = system.element_map[el_id] assert el.springs is not None - if node_id == el.node_id1 and 1 in el.springs and el.springs[1] == 0: - hinges.append(1) - elif node_id == el.node_id2 and 2 in el.springs and el.springs[2] == 0: - hinges.append(1) - else: - hinges.append(0) + end: Literal[1, 2] = 1 if node_id == el.node_id1 else 2 + released.append(el.springs.get(end) == 0) - # If at least one element is connected - # and no more than one element is rigidly connected is_hinge = node_id in system._explicit_internal_hinges or ( - (1 < len(hinges) <= 1 + sum(hinges)) or (len(hinges) == 1 and sum(hinges) == 1) + len(released) > 0 and all(released) ) # Match by id, as the post-processing replaces the node objects system.internal_hinges[:] = [n for n in system.internal_hinges if n.id != node_id] node.hinge = is_hinge - if is_hinge: system.internal_hinges.append(node) - # If the elements aren't already all hinged, then set them to be - releases = [] - for el_id in node.elements: - el = system.element_map[el_id] - assert el.springs is not None - end: Literal[1, 2] = 1 if node_id == el.node_id1 else 2 - if el.springs.get(end) != 0: - releases.append((el_id, end, el.springs.get(end))) - el.springs[end] = 0 - system._hinge_releases[node_id] = releases + +def check_internal_hinge_moment_loads(system: "SystemElements") -> None: + """Check that no moment load is applied to an unsupported internal hinge + + The rotation of an internal hinge is only restrained to keep the stiffness matrix + non-singular. No element resists a moment load on it, so unless a support restrains + the rotation, the load would otherwise be silently lost in that restraint. + + Args: + system (SystemElements): System to check + + Raises: + FEMException: Raised when a moment load is applied to an unsupported internal hinge + """ + supported = {n.id for n in system.supports_fixed + system.supports_rotational} + supported |= {n.id for n, _ in system.supports_spring_z} + supported |= { + n.id + for n, rotate in zip(system.supports_roll, system.supports_roll_rotate) + if not rotate + } + for node in system.internal_hinges: + if system.loads_moment.get(node.id, 0) != 0 and node.id not in supported: + raise FEMException( + "StabilityError", + f"A moment load is applied to node {node.id}, but every element is hinged " + "at this node, so nothing resists it.", + ) def append_node_id( @@ -120,7 +127,6 @@ def remove_node_id( system.supports_rotational.remove(node) system.internal_hinges[:] = [n for n in system.internal_hinges if n.id != node_id] system._explicit_internal_hinges.discard(node_id) - system._hinge_releases.pop(node_id, None) if node in system.supports_roll: ind = system.supports_roll.index(node) system.supports_roll.remove(node) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index d7016935..a6823470 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -2,6 +2,7 @@ from pytest import approx, raises from anastruct import LoadCase, LoadCombination, SystemElements +from anastruct.basic import FEMException from .fixtures.e2e_fixtures import * from .utils import pspec_context @@ -109,7 +110,7 @@ def context_example_4(): def it_results_in_correct_solution(): sol = np.fromstring( """0.00000000e+00 0.00000000e+00 1.46957616e-25 2.00000000e-09 - -7.34788079e-25 0.00000000e+00 0.00000000e+00 0.00000000e+00 + -7.34788079e-25 -2.08333293e-03 0.00000000e+00 0.00000000e+00 4.16666667e-03""", float, sep=" ", @@ -577,14 +578,13 @@ def it_results_in_symmetric_element_demands(): system.get_element_results(2)["Mmax"] ) - def describe_internal_hinge_independent_of_element_order(): + def context_internal_hinge_independent_of_element_order(): # Test bug fix for a joint with only one of three elements released being treated as # an internal hinge, depending on the order in which the elements are added (#464) - release = {1: 0} elements = { "left": ([[0, 4], [4, 4]], None), - "right": ([[4, 4], [8, 4]], release), + "right": ([[4, 4], [8, 4]], {1: 0}), "column": ([[4, 4], [4, 0]], None), } @@ -614,7 +614,15 @@ def it_does_not_create_an_internal_hinge(): assert system.internal_hinges == [] assert system.element_map[ids["left"]].springs == {} assert system.element_map[ids["column"]].springs == {} - assert release == {1: 0} + + def it_does_not_create_an_internal_hinge_when_splitting_an_element_first(): + system = SystemElements() + left = system.add_element(elements["left"][0]) + system.add_element(elements["right"][0], spring=elements["right"][1]) + system.insert_node(left, location=[2, 4]) + column = system.add_element(elements["column"][0]) + assert system.internal_hinges == [] + assert system.element_map[column].springs == {} def it_results_in_the_same_moments_for_any_element_order(): moments = [] @@ -627,6 +635,71 @@ def it_results_in_the_same_moments_for_any_element_order(): assert moments[0] != approx(0) assert moments == approx([moments[0]] * len(moments)) + def context_moment_load_on_a_joint_with_one_rigid_element(): + # Test that a joint where all but one element is released isn't an internal hinge, + # so a moment load on it is carried by the rigid element + + system = SystemElements() + system.add_element([[0, 0], [5, 0]]) + system.add_element([[5, 0], [10, 0]], spring={1: 0}) + system.add_support_fixed(1) + system.add_support_roll(3, direction="x") + system.moment_load(2, Tz=10) + system.solve() + + def it_does_not_create_an_internal_hinge(): + assert system.internal_hinges == [] + assert system.element_map[1].springs == {} + + def it_carries_the_moment_load_to_the_support(): + assert system.get_node_results_system(1)["Tz"] == approx(10) + + def context_moment_load_on_an_internal_hinge(): + # Test that a moment load on a node where every element is released raises, instead of + # being lost in the restraint of the node's rotation + + system = SystemElements() + system.add_element([[0, 0], [5, 0]], spring={2: 0}) + system.add_element([[5, 0], [10, 0]], spring={1: 0}) + system.add_support_fixed([1, 3]) + system.moment_load(2, Tz=10) + + def it_raises_a_stability_error(): + with raises(FEMException, match="StabilityError"): + system.solve() + + def context_add_internal_hinge(): + # Test that add_internal_hinge() releases every element at the node without + # modifying their springs, also when the system is discretized + + def _build(discretize): + system = SystemElements() + system.add_element([[0, 0], [5, 0]]) + system.add_element([[5, 0], [10, 0]]) + system.add_support_fixed([1, 3]) + system.add_internal_hinge(2) + system.point_load(2, Fy=-10) + if discretize: + system.discretize(4) + system.solve() + return system + + system = _build(discretize=False) + discretized = _build(discretize=True) + + def it_does_not_modify_the_springs(): + for s in (system, discretized): + assert all(el.springs == {} for el in s.element_map.values()) + + def it_keeps_the_internal_hinge_when_discretized(): + assert [node.id for node in system.internal_hinges] == [2] + assert [node.id for node in discretized.internal_hinges] == [5] + + def it_results_in_hinged_moments(): + # two cantilevers that each carry half of the point load + assert system.get_node_results_system(1)["Tz"] == approx(-25) + assert discretized.get_node_results_system(1)["Tz"] == approx(-25) + def context_q_and_q_perp_loads(): # Test addition of simultaneous q and q_perp loads