8000 Factorize K11 once in analyze_linear and reuse it for every combination by ssdeassis · Pull Request #364 · JWock82/Pynite · GitHub
[go: up one dir, main page]

Skip to content

Factorize K11 once in analyze_linear and reuse it for every combination - #364

Open
ssdeassis wants to merge 1 commit into
JWock82:mainfrom
ssdeassis:perf/factorize-once
Open

ssdeassis wants to merge 1 commit into
JWock82:mainfrom
ssdeassis:perf/factorize-once

Conversation

@ssdeassis
Copy link
Copy Markdown
Contributor

Part of #360 (item 2). Follows #361.

analyze_linear() assembles the stiffness matrix only once, but each load combination still called spsolve, which factorizes K11 again. With the sparse solver, K11 is now LU-factorized once and the factorization is applied to the right-hand side of each combination (a forward and a back substitution).

Changes

  • Analysis._factorize_K11(K11) (new): factorizes K11 with scipy.sparse.linalg.splu and returns the factorization's solve. If splu cannot factorize the matrix (it raises RuntimeError when K11 is exactly singular), it returns None.
  • Analysis._solve_unknown_disp(...): new optional solver argument. When given (sparse case only), it is used instead of spsolve. The residual/finiteness stability check after the solve is unchanged.
  • FEModel3D.analyze_linear(): calls _factorize_K11 once before the load-combination loop and passes the result to _solve_unknown_disp. With None (exactly singular matrix, dense solver, or no unknown displacements) the previous code path runs, so unstable models are reported as before.
  • analyze(), analyze_PDelta() and the dense path are not changed.

Tests

  • Testing/test_factorize_once.py (new): a quad wall with three load combinations gives the same displacements as the previous per-combination spsolve path (rel. 1e-9) and as the dense solver; K11 is factorized only once for the three combinations; a mechanism (internal hinge without support) still raises.
  • Full test suite: 213 passed. The 2 failures (test_beam_rotation, test_circular_hopper) and the 4 collection errors (vtk) are missing optional plotting packages in my environment and fail the same way without this change. ruff check passes.

Benchmark (cylindrical wall from #360: R = 2.10 m, t = 0.20 m, H = 6 m, 10 cm mesh, 7,920 nodes, 7,788 quads, analyze_linear(sparse=True))

3 load combinations
per-combination spsolve (current main) 92.4 s
one factorization, reused 74.1 s

Each factorization of this K11 takes about 9 s, so every additional combination saves about that much. The largest displacement difference between the two runs is 4.1e-17 m on a maximum of 4.6e-5 m (relative 9e-13, round-off).

AI disclosure
The change, the test and the benchmark were prepared with the help of Claude (Anthropic). I have reviewed every changed line and checked the results against the previous code path and the dense solver.

analyze_linear assembles the stiffness matrix once, but each load
combination still called spsolve, which factorizes K11 again. With the
sparse solver, K11 is now LU-factorized once (scipy.sparse.linalg.splu,
in the new Analysis._factorize_K11) and the factorization is applied to
the right-hand side of each combination.

_solve_unknown_disp takes the optional solver and keeps the residual
stability check unchanged. If splu cannot factorize K11 (exactly
singular), _factorize_K11 returns None and the previous per-combination
spsolve path runs, so unstable models are reported as before. The dense
path is unchanged.

Adds Testing/test_factorize_once.py: same results as the per-combination
spsolve path and as the dense solver, one factorization for three
combinations, and an unstable model that still raises.

Part of JWock82#360 (item 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BZww7wTphWeFLC22L7YkLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0