From 0348c641ee96529721778f2b8abef8645189c813 Mon Sep 17 00:00:00 2001 From: "Mitchell.Tesch" Date: Tue, 3 Nov 2026 14:54:02 +1000 Subject: [PATCH 1/3] Fix sign of the geometric stiffness matrix geometric_stiffness_matrix converted the reference (y-up) matrix to anastruct's y convention by flipping the y columns only. A change of DOF sign must be applied to rows and columns; flipping the columns alone negated the transverse terms, so in solve(geometrical_non_linear=True) axial compression stiffened elements and tension softened them. Also fixes row 4 (s1 * c2 -> s2 * c2), so node 2's forces are the negatives of node 1's. --- anastruct/fem/elements.py | 6 +-- tests/test_geometric_stiffness.py | 68 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/test_geometric_stiffness.py diff --git a/anastruct/fem/elements.py b/anastruct/fem/elements.py index a97fece..7a5e01e 100644 --- a/anastruct/fem/elements.py +++ b/anastruct/fem/elements.py @@ -440,7 +440,7 @@ def geometric_stiffness_matrix(l: float, N: float, a1: float, a2: float) -> np.n 6 / 5 * s1 * c1, l / 10 * s1, 6 / 5 * s2**2, - -6 / 5 * s1 * c2, + -6 / 5 * s2 * c2, l / 10 * s2, ], [ @@ -461,5 +461,5 @@ def geometric_stiffness_matrix(l: float, N: float, a1: float, a2: float) -> np.n ], ] ) - * np.array([1, -1, 1, 1, -1, 1]) - ) # conversion from coordinate system + * np.outer([1, -1, 1, 1, -1, 1], [1, -1, 1, 1, -1, 1]) + ) # conversion from coordinate system: flip y in rows and columns diff --git a/tests/test_geometric_stiffness.py b/tests/test_geometric_stiffness.py new file mode 100644 index 0000000..d13504f --- /dev/null +++ b/tests/test_geometric_stiffness.py @@ -0,0 +1,68 @@ +"""Geometric stiffness matrix Kg used by ``solve(geometrical_non_linear=True)``. + +The reference matrix (H. P. Gavin, CEE 421 notes, linked in elements.py) has +y up; anastruct's y is opposite, so the y DOFs must be flipped in rows *and* +columns. Flipping only the columns made compression stiffen an element. +""" + +import math + +import numpy as np +import pytest + +from anastruct.fem.elements import geometric_stiffness_matrix + +ANGLES = [0.0, 0.3, math.pi / 4, math.pi / 2, 2.0, math.pi] + + +@pytest.mark.parametrize("angle", ANGLES) +def test_geometric_stiffness_matrix_is_symmetric(angle): + kg = geometric_stiffness_matrix(2.5, -100.0, angle, angle) + np.testing.assert_allclose(kg, kg.T, atol=1e-12) + + +@pytest.mark.parametrize( + "a1, a2", [(0.0, 0.0), (0.2, 0.5), (1.0, 1.3), (math.pi / 2, 0.4)] +) +def test_geometric_stiffness_forces_are_in_equilibrium(a1, a2): + """The nodal forces Kg produces balance: node 2's x / y rows are the + negatives of node 1's (rows 3 and 4 equal minus rows 0 and 1).""" + kg = geometric_stiffness_matrix(2.5, -100.0, a1, a2) + np.testing.assert_allclose(kg[3], -kg[0], atol=1e-12) + np.testing.assert_allclose(kg[4], -kg[1], atol=1e-12) + + +@pytest.mark.parametrize("angle", ANGLES) +def test_compression_softens_and_tension_stiffens(angle): + """Axial compression (N < 0) reduces the stiffness of every element + DOF set it acts on; tension increases it.""" + compression = geometric_stiffness_matrix(2.5, -100.0, angle, angle) + tension = geometric_stiffness_matrix(2.5, 100.0, angle, angle) + assert np.all(np.linalg.eigvalsh(compression) <= 1e-9) + assert np.all(np.linalg.eigvalsh(tension) >= -1e-9) + assert np.linalg.eigvalsh(compression).min() < 0 + assert np.linalg.eigvalsh(tension).max() > 0 + + +def test_horizontal_element_matches_the_reference_matrix(): + """For a horizontal element Kg is the reference N/l matrix on the + transverse and rotational DOFs, in anastruct's y convention.""" + l, n = 2.0, -50.0 + kg = geometric_stiffness_matrix(l, n, 0.0, 0.0) + reference = ( + n + / l + * np.array( + [ + [6 / 5, l / 10, -6 / 5, l / 10], + [l / 10, 2 * l**2 / 15, -l / 10, -(l**2) / 30], + [-6 / 5, -l / 10, 6 / 5, -l / 10], + [l / 10, -(l**2) / 30, -l / 10, 2 * l**2 / 15], + ] + ) + ) + flip = np.diag([-1, 1, -1, 1]) # anastruct's y is opposite the reference's + expected = flip @ reference @ flip + dofs = [1, 2, 4, 5] + np.testing.assert_allclose(kg[np.ix_(dofs, dofs)], expected, atol=1e-12) + assert not np.any(kg[[0, 3], :]) and not np.any(kg[:, [0, 3]]) From 1c146cc31d1fbf763eb4147d678857ed28e87110 Mon Sep 17 00:00:00 2001 From: "Mitchell.Tesch" Date: Tue, 3 Nov 2026 14:54:58 +1000 Subject: [PATCH 2/3] Fix the buckling eigen-solve in geometrical non-linear analysis det_linear_buckling replaced scipy.linalg.eigvals(k0, kg) with np.linalg.inv(kg) * k0. That is an element-wise product, and kg is singular for beam elements (no geometric stiffness on the axial DOFs), so every horizontal member raised LinAlgError: Singular matrix and other members returned wrong factors. Solve (k0 + lambda kg) x = 0 through the eigenvalues mu of inv(k0) kg, with lambda = -1/mu, and return the smallest positive lambda, or inf when nothing is in compression. Still numpy only. --- anastruct/fem/system_components/solver.py | 16 ++--- tests/test_geometrical_non_linear.py | 81 +++++++++++++++++++++++ 2 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 tests/test_geometrical_non_linear.py diff --git a/anastruct/fem/system_components/solver.py b/anastruct/fem/system_components/solver.py index c740fa9..237a03a 100644 --- a/anastruct/fem/system_components/solver.py +++ b/anastruct/fem/system_components/solver.py @@ -109,15 +109,13 @@ def det_linear_buckling(system: "SystemElements") -> float: system.solve() kg = system.reduced_system_matrix - k0 - # solve (k -λkg)x = 0 - - # The following two lines are equivalent to the cleaner - # `scipy.linalg.eigvals(k0, kg)``, but doing this in numpy - # instead allows us to drop the very heavy scipy dependency - # that was needed for no other reason than this one line. - invkg_k0 = np.linalg.inv(kg) * k0 - eigenvalues = np.abs(np.linalg.eigvals(invkg_k0)) - return float(np.min(eigenvalues)) + + # (k0 + λ kg) x = 0 ⇔ k0⁻¹ kg x = μ x with λ = −1/μ. kg is singular + # (no axial terms), so it is not inverted. No μ < 0 means no compression. + mu = np.linalg.eigvals(np.linalg.solve(k0, kg)).real + if mu.min() > -1e-12: + return float("inf") + return float(-1 / mu.min()) def geometrically_non_linear( diff --git a/tests/test_geometrical_non_linear.py b/tests/test_geometrical_non_linear.py new file mode 100644 index 0000000..aef3fac --- /dev/null +++ b/tests/test_geometrical_non_linear.py @@ -0,0 +1,81 @@ +"""``solve(geometrical_non_linear=True)`` against closed-form beam-column +results: a simply supported member, uniform transverse load q, axial +force N, meshed into 20 elements. + +Exact mid-span moments (P = |N|, k = sqrt(P / EI), u = kL / 2): + +- compression: M = q EI / P · (sec u − 1) +- tension: M = q EI / P · (1 − sech u) + +Buckling factor: N_cr / P in compression (N_cr = π² EI / L²); no buckling +(infinite factor) when the member is in tension. +""" + +import math + +import numpy as np +import pytest + +from anastruct import SystemElements + +EI = 200e6 * 9.05e-6 # kNm² +EA = 200e6 * 2300e-6 # kN +L = 6.0 +Q = 5.0 # kN/m +N_CR = math.pi**2 * EI / L**2 +N_ELEMENTS = 20 + + +def _member(axial: float, *, vertical: bool = False) -> SystemElements: + """``axial`` > 0 is tension. Pin at the start, roller at the end, and + the load perpendicular to the member.""" + ss = SystemElements(EA=EA, EI=EI) + end = [0, L] if vertical else [L, 0] + ids = ss.add_multiple_elements([[0, 0], end], N_ELEMENTS) + ss.add_support_hinged(1) + ss.add_support_roll(ss.id_last_node, direction=1 if vertical else 2) + ss.q_load(q=Q if vertical else -Q, element_id=ids) + if vertical: + ss.point_load(ss.id_last_node, Fy=axial) + else: + ss.point_load(ss.id_last_node, Fx=axial) + return ss + + +def _max_moment(ss: SystemElements) -> float: + return max(np.max(np.abs(r["M"])) for r in ss.get_element_results(verbose=True)) + + +def _exact(ratio: float, compression: bool) -> float: + p = ratio * N_CR + u = L / 2 * math.sqrt(p / EI) + if compression: + return Q * EI / p * (1 / math.cos(u) - 1) + return Q * EI / p * (1 - 1 / math.cosh(u)) + + +@pytest.mark.parametrize("vertical", [False, True], ids=["horizontal", "vertical"]) +@pytest.mark.parametrize("ratio", [0.3, 0.6]) +def test_compression_amplifies_moment(ratio, vertical): + ss = _member(-ratio * N_CR, vertical=vertical) + ss.solve(geometrical_non_linear=True) + assert _max_moment(ss) == pytest.approx(_exact(ratio, True), rel=1e-3) + assert ss.buckling_factor == pytest.approx(1 / ratio, rel=1e-3) + + +@pytest.mark.parametrize("vertical", [False, True], ids=["horizontal", "vertical"]) +@pytest.mark.parametrize("ratio", [0.3, 0.6]) +def test_tension_reduces_moment_and_cannot_buckle(ratio, vertical): + ss = _member(ratio * N_CR, vertical=vertical) + ss.solve(geometrical_non_linear=True) + assert _max_moment(ss) == pytest.approx(_exact(ratio, False), rel=1e-3) + assert ss.buckling_factor == math.inf + + +def test_no_axial_force_matches_first_order(): + first = _member(0.0) + first.solve() + second = _member(0.0) + second.solve(geometrical_non_linear=True) + assert _max_moment(second) == pytest.approx(_max_moment(first), rel=1e-12) + assert second.buckling_factor == math.inf From 1df0ad3e479c6c0aa870957845ec0772068665b3 Mon Sep 17 00:00:00 2001 From: Mitchell tesch Date: Tue, 3 Nov 2026 21:37:34 +1000 Subject: [PATCH 3/3] Report buckling factors below 1 instead of rejecting the system det_linear_buckling assembled k0 + kg with a full solve(). Once the loads exceed the buckling load that matrix is no longer positive definite, so validate() rejected it with a StabilityError and no factor could be reported. Only the assembled matrix is needed, so build it with solve(naked=True). A member at 1.2 N_cr now reports a factor of 0.833. --- anastruct/fem/system_components/solver.py | 4 +++- tests/test_geometrical_non_linear.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/anastruct/fem/system_components/solver.py b/anastruct/fem/system_components/solver.py index 237a03a..ada1871 100644 --- a/anastruct/fem/system_components/solver.py +++ b/anastruct/fem/system_components/solver.py @@ -107,7 +107,9 @@ def det_linear_buckling(system: "SystemElements") -> float: el.compile_geometric_non_linear_stiffness_matrix() el.reset() - system.solve() + # Only the assembled k0 + kg is needed. Beyond the buckling load it is no + # longer positive definite, and a full solve would reject it as unstable. + system.solve(naked=True) kg = system.reduced_system_matrix - k0 # (k0 + λ kg) x = 0 ⇔ k0⁻¹ kg x = μ x with λ = −1/μ. kg is singular diff --git a/tests/test_geometrical_non_linear.py b/tests/test_geometrical_non_linear.py index aef3fac..e431e44 100644 --- a/tests/test_geometrical_non_linear.py +++ b/tests/test_geometrical_non_linear.py @@ -17,6 +17,7 @@ import pytest from anastruct import SystemElements +from anastruct.fem.system_components.solver import det_linear_buckling EI = 200e6 * 9.05e-6 # kNm² EA = 200e6 * 2300e-6 # kN @@ -79,3 +80,12 @@ def test_no_axial_force_matches_first_order(): second.solve(geometrical_non_linear=True) assert _max_moment(second) == pytest.approx(_max_moment(first), rel=1e-12) assert second.buckling_factor == math.inf + + +@pytest.mark.parametrize("vertical", [False, True], ids=["horizontal", "vertical"]) +def test_buckling_factor_below_one_is_reported(vertical): + """Beyond the buckling load, k0 + kg is no longer positive definite. + The buckling factor only needs that matrix, so it must still be found.""" + assert det_linear_buckling( + _member(-1.2 * N_CR, vertical=vertical) + ) == pytest.approx(1 / 1.2, rel=1e-3)