Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions anastruct/fem/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,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,
],
[
Expand All @@ -477,5 +477,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
20 changes: 10 additions & 10 deletions anastruct/fem/system_components/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ 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
# 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(
Expand Down
68 changes: 68 additions & 0 deletions tests/test_geometric_stiffness.py
Original file line number Diff line number Diff line change
@@ -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]])
91 changes: 91 additions & 0 deletions tests/test_geometrical_non_linear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""``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
from anastruct.fem.system_components.solver import det_linear_buckling

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


@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)