From 622e69304eeb62da5372d21cbf90ce066b29da55 Mon Sep 17 00:00:00 2001 From: Ayberk Korkmaz Date: Sat, 31 Oct 2026 14:19:10 +0300 Subject: [PATCH 1/2] Invert the rotational flexibility matrix for element springs --- anastruct/fem/elements.py | 19 +++-- tests/test_constitutive_matrix.py | 112 ++++++++++++++++-------------- tests/test_e2e.py | 4 +- 3 files changed, 76 insertions(+), 59 deletions(-) diff --git a/anastruct/fem/elements.py b/anastruct/fem/elements.py index a97fece2..2b32762d 100644 --- a/anastruct/fem/elements.py +++ b/anastruct/fem/elements.py @@ -362,11 +362,20 @@ def constitutive_matrix( # flexibility matrix c + springs on both sides: # [[ c11 + 1/k1, c12 ] - # [ c21 , c21 + 1/k2 ]] - if 1 in spring and spring[1] != 0: # not hinge - matrix[1][1] = 1 / (1 / matrix[1][1] + 1 / spring[1]) - if 2 in spring and spring[2] != 0: # not hinge - matrix[2][2] = 1 / (1 / matrix[2][2] + 1 / spring[2]) + # [ c21 , c22 + 1/k2 ]] + # The springs act in series with the bending flexibility, so the + # rotational block is inverted as a whole. Inverting each term on its + # own leaves an indefinite matrix for soft springs. + added = np.zeros(2) + for node in (1, 2): + if node in spring and spring[node] != 0: # not hinge + added[node - 1] = 1 / spring[node] + if added.any(): + block = matrix[1:, 1:] + active = np.flatnonzero(np.diag(block) != 0) + sub = block[np.ix_(active, active)] + flexibility = np.linalg.inv(sub) + np.diag(added[active]) + block[np.ix_(active, active)] = np.linalg.inv(flexibility) return matrix diff --git a/tests/test_constitutive_matrix.py b/tests/test_constitutive_matrix.py index 4cbea3b4..6a43f7cc 100644 --- a/tests/test_constitutive_matrix.py +++ b/tests/test_constitutive_matrix.py @@ -2,69 +2,77 @@ import numpy as np +from anastruct import SystemElements from anastruct.fem.elements import constitutive_matrix +EI, L = 15000.0, 4.0 -class TestConstitutiveMatrixSprings(unittest.TestCase): - def test_symmetric_with_springs_on_both_nodes(self): - # A linear-elastic element's constitutive matrix must be symmetric, - # spring release or not. - matrix = constitutive_matrix( - EA=1e9, - EI=15000.0, - l=4.0, - spring={1: 1000.0, 2: 2000.0}, - node_1_hinge=False, - node_2_hinge=False, - ) - np.testing.assert_allclose(matrix, matrix.T) - def test_spring_reduces_only_its_own_node_diagonal_term(self): - # A rotational spring in series with the beam's own rotational - # stiffness should only soften that node's diagonal term (per the - # flexibility matrix in the function's own comment); the - # off-diagonal (moment/rotation coupling) term is unaffected. - EI, l = 15000.0, 4.0 - k1, k2 = 1000.0, 2000.0 +def series_spring_block(k1=None, k2=None): + """Rotational block with springs in series with the beam flexibility.""" + stiffness = np.array([[4 * EI / L, -2 * EI / L], [-2 * EI / L, 4 * EI / L]]) + flexibility = np.linalg.inv(stiffness) + for index, k in enumerate((k1, k2)): + if k is not None: + flexibility[index, index] += 1 / k + return np.linalg.inv(flexibility) + - matrix = constitutive_matrix( - EA=1e9, - EI=EI, - l=l, - spring={1: k1, 2: k2}, - node_1_hinge=False, - node_2_hinge=False, - ) +class TestConstitutiveMatrixSprings(unittest.TestCase): + def test_springs_add_in_series_with_bending_flexibility(self): + for spring, (k1, k2) in ( + ({1: 1000.0}, (1000.0, None)), + ({2: 2000.0}, (None, 2000.0)), + ({1: 1000.0, 2: 2000.0}, (1000.0, 2000.0)), + ): + matrix = constitutive_matrix(1e9, EI, L, spring, False, False) + np.testing.assert_allclose(matrix[1:, 1:], series_spring_block(k1, k2)) + self.assertEqual(matrix[0][0], 1e9 / L) - c11_expected = 1 / (1 / (4 * EI / l) + 1 / k1) - c22_expected = 1 / (1 / (4 * EI / l) + 1 / k2) + def test_spring_matrix_is_symmetric_positive_definite(self): + # Soft springs used to leave an indefinite matrix, which made + # SystemElements.solve() raise a stability error. + for spring in ({1: 100.0}, {2: 100.0}, {1: 100.0, 2: 50.0}): + block = constitutive_matrix(1e9, EI, L, spring, False, False)[1:, 1:] + np.testing.assert_allclose(block, block.T) + self.assertTrue(np.all(np.linalg.eigvalsh(block) > 0)) - np.testing.assert_allclose(matrix[1][1], c11_expected) - np.testing.assert_allclose(matrix[2][2], c22_expected) - np.testing.assert_allclose(matrix[1][2], -2 * EI / l) - np.testing.assert_allclose(matrix[2][1], -2 * EI / l) + def test_hinge_with_spring_on_other_node(self): + matrix = constitutive_matrix(1e9, EI, L, {2: 2000.0}, True, False) + self.assertEqual(matrix[1][1], 0) + self.assertEqual(matrix[1][2], 0) + np.testing.assert_allclose(matrix[2][2], 1 / (L / (4 * EI) + 1 / 2000.0)) - def test_spring_at_node_2_only(self): - # Regression test: a spring at node 2 alone used to leave - # matrix[2][2] untouched and corrupt the off-diagonal terms instead. - EI, l = 15000.0, 4.0 - k2 = 2000.0 - matrix = constitutive_matrix( - EA=1e9, - EI=EI, - l=l, - spring={2: k2}, - node_1_hinge=False, - node_2_hinge=False, - ) +class TestRotationalSpringStructures(unittest.TestCase): + P = 10.0 - c22_expected = 1 / (1 / (4 * EI / l) + 1 / k2) + def test_cantilever_with_base_spring_matches_closed_form(self): + for k in (2000.0, 20000.0, 2e5): + ss = SystemElements(EI=EI, EA=1e9) + ss.add_element([[0, 0], [L, 0]], spring={1: k}) + ss.add_support_fixed(1) + ss.point_load(2, Fy=-self.P) + ss.solve() + expected = self.P * L**3 / (3 * EI) + self.P * L * L / k + self.assertAlmostEqual( + abs(ss.get_node_displacements(2)["uy"]), expected, places=10 + ) - np.testing.assert_allclose(matrix[1][1], 4 * EI / l) - np.testing.assert_allclose(matrix[2][2], c22_expected) - np.testing.assert_allclose(matrix[1][2], -2 * EI / l) - np.testing.assert_allclose(matrix[2][1], -2 * EI / l) + def test_cantilever_with_spring_at_element_end_matches_closed_form(self): + for k in (2000.0, 20000.0, 2e5): + ss = SystemElements(EI=EI, EA=1e9) + ss.add_element([[0, 0], [L, 0]], spring={2: k}) + ss.add_element([[L, 0], [2 * L, 0]]) + ss.add_support_fixed(1) + ss.point_load(3, Fy=-self.P) + ss.solve() + # The moment P*L at the spring opens a rotation P*L/k, which the + # second span carries as a rigid rotation over length L. + expected = self.P * (2 * L) ** 3 / (3 * EI) + self.P * L / k * L + self.assertAlmostEqual( + abs(ss.get_node_displacements(3)["uy"]), expected, places=10 + ) if __name__ == "__main__": diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 4c5b3b24..176fd07d 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -154,8 +154,8 @@ def context_example_7_rotational_spring(): def it_results_in_correct_solution(SS_7): sol = np.fromstring( - """0. 0. 0. 0. 0.23558645 -0.09665875 - 0. 0. 0.06433688""", + """0. 0. 0. 0. 0.25070634 -0.10443469 + 0. 0. 0.07363764""", float, sep=" ", ) From b9b24eebc01fad69b1463037cc2a5c1f0c0dc2dd Mon Sep 17 00:00:00 2001 From: Ayberk Korkmaz Date: Wed, 4 Nov 2026 13:51:10 +0300 Subject: [PATCH 2/2] Condense a released end so the other end keeps 3EI/l --- anastruct/fem/elements.py | 13 ++++++++++--- tests/test_constitutive_matrix.py | 23 ++++++++++++++++++++++- tests/test_e2e.py | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/anastruct/fem/elements.py b/anastruct/fem/elements.py index 2b32762d..4f589649 100644 --- a/anastruct/fem/elements.py +++ b/anastruct/fem/elements.py @@ -345,10 +345,17 @@ def constitutive_matrix( [[EA / l, 0, 0], [0, 4 * EI / l, -2 * EI / l], [0, -2 * EI / l, 4 * EI / l]] ) - if node_1_hinge or (spring is not None and 1 in spring and spring[1] == 0): + hinge_1 = node_1_hinge or (spring is not None and 1 in spring and spring[1] == 0) + hinge_2 = node_2_hinge or (spring is not None and 2 in spring and spring[2] == 0) + # Releasing one end condenses out its rotation: the other end of a + # pinned-fixed member has rotational stiffness 3EI/l, not 4EI/l. + if hinge_1 and not hinge_2: + matrix[2][2] = 3 * EI / l + if hinge_2 and not hinge_1: + matrix[1][1] = 3 * EI / l + if hinge_1: matrix[1][1] = matrix[1][2] = matrix[2][1] = 0 - - if node_2_hinge or (spring is not None and 2 in spring and spring[2] == 0): + if hinge_2: matrix[1][2] = matrix[2][1] = matrix[2][2] = 0 if spring is not None: diff --git a/tests/test_constitutive_matrix.py b/tests/test_constitutive_matrix.py index 6a43f7cc..bb54593e 100644 --- a/tests/test_constitutive_matrix.py +++ b/tests/test_constitutive_matrix.py @@ -41,12 +41,33 @@ def test_hinge_with_spring_on_other_node(self): matrix = constitutive_matrix(1e9, EI, L, {2: 2000.0}, True, False) self.assertEqual(matrix[1][1], 0) self.assertEqual(matrix[1][2], 0) - np.testing.assert_allclose(matrix[2][2], 1 / (L / (4 * EI) + 1 / 2000.0)) + np.testing.assert_allclose(matrix[2][2], 1 / (L / (3 * EI) + 1 / 2000.0)) class TestRotationalSpringStructures(unittest.TestCase): P = 10.0 + def test_zero_spring_at_pinned_support_changes_nothing(self): + # A joint moment splits by member end stiffness: 4EI/l for the + # member with a fixed far end, 3EI/l for the one with a pinned far end. + # Releasing the end that already sits on a pin must not change that. + moments = [] + for spring in (None, {2: 0}): + ss = SystemElements(EI=1000, EA=1e9) + ss.add_element([[0, 0], [4, 0]]) + ss.add_element([[4, 0], [8, 0]], spring=spring) + ss.add_support_fixed(1) + ss.add_support_roll(2, direction="x") + ss.add_support_hinged(3) + ss.moment_load(2, Ty=10) + ss.solve() + moments.append( + (ss.element_map[1].node_map[2].Tz, ss.element_map[2].node_map[2].Tz) + ) + expected = (10 * 4 / 7, 10 * 3 / 7) + for pair in moments: + np.testing.assert_allclose(pair, expected) + def test_cantilever_with_base_spring_matches_closed_form(self): for k in (2000.0, 20000.0, 2e5): ss = SystemElements(EI=EI, EA=1e9) diff --git a/tests/test_e2e.py b/tests/test_e2e.py index 176fd07d..975541a1 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -110,7 +110,7 @@ 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 - 3.12500040e-03""", + 4.16666667e-03""", float, sep=" ", )