diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 5f289870..e6d44401 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -322,14 +322,16 @@ analytic Jacobians. about its static equilibrium, `K φ = ω² M φ`, over the free DOFs, with `K` the static tangent (element stiffness and linearised seabed contact) and `M` the consistent mass plus Morison added mass. `K` and `M` are assembled in symmetric -band storage; LAPACK `DSBGVX` gives the eigenvalues and inverse iteration on the -banded pencil gives each mode shape, so the cost grows with line length rather -than with its square. Dense `DSYGV` is kept as the test reference. Both line paths -are supported (`nModes`). A finite-EI line's modes (`CD_Modal_Hermite_Line`) take -its end connections and attachments from the dynamic model, and a line restrained -in torsion adds the twist angle of every node: its lumped polar inertia and the -second variation of the nodal-twist energy (`CD_HermiteCable_Dyn_Twist_Hessian`) -in a band of 7 DOFs per node. +band storage. A shift-invert block Lanczos iteration on the banded Cholesky factor +of `K` gives the eigenvalues, certified by a Sturm count of `K - σM` (LAPACK +`DSBGVX` takes short lines, an indefinite or singular `K`, and an uncertified run), +and inverse iteration on the banded pencil gives each mode shape, so the cost +grows with line length rather than with its square. Dense `DSYGV` is kept as the +test reference. Both line paths are supported (`nModes`). A finite-EI line's +modes (`CD_Modal_Hermite_Line`) take its end connections and attachments from the +dynamic model, and a line restrained in torsion adds the twist angle of every +node: its lumped polar inertia and the second variation of the nodal-twist energy +(`CD_HermiteCable_Dyn_Twist_Hessian`) in a band of 7 DOFs per node. ### Outputs diff --git a/doc/examples.rst b/doc/examples.rst index cb8abf50..8db6ddc2 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -360,7 +360,7 @@ touchdown and lazy-wave shape from the geometry alone; there is no initial-shape - the 10 lowest natural periods and mode shapes (``nModes``) of the 80 m cable on its 1024-element mesh — :doc:`tutorial_lazywave` - Hermite, modal - - ~2 s + - ~1 s * - ``torsion_lazy_wave_hangoff_twist.dat`` - the 80 m cable with an illustrative ``GJ`` of 50 kN·m², clamped and restrained in torsion at both ends, twisted two turns at the hang-off over 60 s by the ``motionFile`` roll column diff --git a/doc/options.rst b/doc/options.rst index ca74bbde..f3c8387a 100644 --- a/doc/options.rst +++ b/doc/options.rst @@ -622,8 +622,8 @@ Ambient fluid and prescribed motion lines stops with ``OPTION nModes is not supported for mixed EI=0/finite-EI decks; remove it or set it to 0``. A line restrained in torsion adds the twist angle of every node to its modes (``PolarShare`` and ``dTwist`` in :doc:`outputs`). The banded solver has - no line-length limit: a - 1024-element finite-EI cable takes a few seconds. See :doc:`theory` + no line-length limit: the 10 modes of a 1024-element finite-EI cable take a fraction of a + second. See :doc:`theory` - standalone - ``10 nModes`` * - ``WaveSeed`` / ``wave_seed`` diff --git a/doc/outputs.rst b/doc/outputs.rst index 0cb33a4d..9134507a 100644 --- a/doc/outputs.rst +++ b/doc/outputs.rst @@ -462,7 +462,9 @@ row: * ``# Mode shapes``: ``LineID Mode Node X Y Z dX dY dZ``, one row per mode and node. ``X Y Z`` is the static node position and ``dX dY dZ`` the nodal translation of the mode, scaled so that the largest nodal displacement of the mode is 1. Nodes are numbered End A first, - as in the other line files; the held end nodes have zero displacement. + as in the other line files; the held end nodes have zero displacement. The sign of a mode + makes the largest component of its mode vector positive; where two components are equally + large (the mirrored nodes of a symmetric line), the first of them in the solver's node order. When a line is restrained in torsion (both ends, see :doc:`driver_format`), its modes carry the twist angle of every node (:ref:`nodal twist `) and both tables gain a diff --git a/doc/theory.rst b/doc/theory.rst index 7b45a636..38636300 100644 --- a/doc/theory.rst +++ b/doc/theory.rst @@ -1979,13 +1979,31 @@ element, the Hessian of its strain energy. The linearised seabed normal contact stiffness on the contacting nodes. :math:`\mathbf M` is the consistent structural mass plus the Morison added mass at the static shape. Friction, damping, and drag are left out, as in any undamped modal analysis. :math:`\mathbf K` and :math:`\mathbf M` are banded (the nodes are -numbered along the line), and only the ``nModes`` lowest modes are computed: LAPACK ``dsbgvx`` -gives the eigenvalues, inverse iteration with the banded factors of -:math:`\mathbf K - \omega^2\mathbf M` gives each shape (M-orthogonal to the shapes before -it, so a repeated frequency gets independent shapes), and the returned :math:`\omega^2` is the -Rayleigh quotient of the shape, summed in quadruple precision. That quotient keeps the lowest -frequencies of a long, axially stiff line accurate, where the band reduction alone loses -digits. Memory grows linearly with the line length. The frequencies +numbered along the line), and only the ``nModes`` lowest modes are computed. Their eigenvalues +come from a block Lanczos iteration on the shift-inverted operator +:math:`\mathbf K^{-1}\mathbf M`, applied with the banded Cholesky factor of :math:`\mathbf K`, +in the :math:`\mathbf M` inner product with full reorthogonalisation. The block holds two +vectors, so both planes of a straight line, whose transverse frequencies coincide, are found. +A Ritz value is accepted when the residual of the shift-inverted eigenproblem is below +:math:`10^{-10}` of its eigenvalue. The count is then certified: the Cholesky factor shows that +no eigenvalue lies at or below zero, and the inertia of :math:`\mathbf K - \sigma\mathbf M` +(the negative pivots of its banded :math:`\mathbf L\mathbf D\mathbf L^{\mathsf T}` +factorisation, by Sylvester's law of inertia) at a shift :math:`\sigma` in a gap above the +wanted eigenvalues must equal the number of Ritz values below :math:`\sigma`, so no frequency +is missed or counted twice. The iteration's time grows linearly with the line length. Short +lines, many modes of a short line, an indefinite or singular :math:`\mathbf K` (a compressed +or slack line), and an iteration that does not converge or is not certified within its basis +bound take LAPACK ``dsbgvx`` instead (band reduction and bisection), whose time grows with the +square of the line length. Inverse iteration with the banded factors of +:math:`\mathbf K - \omega^2\mathbf M` then gives each shape (M-orthogonal to the shapes +before it, so a repeated frequency gets independent shapes), and the returned +:math:`\omega^2` is the Rayleigh quotient of the shape, summed in quadruple precision. That +quotient keeps the lowest frequencies of a long, axially stiff line accurate, where the band +reduction alone loses digits. A shape the Lanczos eigenvalues do not give is solved again on +the eigenvalues of the band reduction. Each shape is signed so that its largest component is +positive; where two components are equally large to round-off (the mirrored nodes of a +symmetric line), the first of them in the solver's node order is. Memory grows linearly with the line +length. The frequencies :math:`f = \omega/2\pi` and the nodal translations of each mode (scaled to a unit largest nodal displacement) are written to ``.modes.out``. For a taut string of tension :math:`T` the transverse frequencies are :math:`f_n = (n/2L)\sqrt{T/m}`; for a shallow sagging diff --git a/doc/tutorial_lazywave.rst b/doc/tutorial_lazywave.rst index 40790dbb..c324eef7 100644 --- a/doc/tutorial_lazywave.rst +++ b/doc/tutorial_lazywave.rst @@ -184,7 +184,7 @@ vertical), 19.9 s (out of plane, along the grounded run) and 16.9 s (in plane). 2048 elements the out-of-plane periods and the 20.4 s mode agree to four digits. The in-plane modes that move the touchdown (39.9 s, 16.9 s and 11.7 s) shift by up to 0.5 %, because the linearised seabed contact at the touchdown changes with the node spacing. The run takes about -2 s, most of it in the modal solve. +a second, a quarter of it in the modal solve. Exercises --------- diff --git a/src/CableDyn_Modal.f90 b/src/CableDyn_Modal.f90 index 3c15668b..ec0c34d9 100644 --- a/src/CableDyn_Modal.f90 +++ b/src/CableDyn_Modal.f90 @@ -13,14 +13,16 @@ MODULE CableDyn_Modal !! !! K and M are assembled as their symmetric parts in LAPACK symmetric band storage (the !! nodes of a line are numbered along it, so the bandwidth is set by one element) and - !! CD_Modal_Solve_Band computes only the requested lowest modes: the eigenvalues by - !! LAPACK DSBGVX (band reduction and bisection), each mode shape by inverse iteration - !! on the banded pencil, M-orthogonalised against the modes before it, and the returned - !! eigenvalue as the Rayleigh quotient of that shape. Memory and time - !! grow linearly with the line length for the shapes and quadratically, with a small - !! constant, for the band reduction. The dense routines (CD_Modal_Bar_Matrices, - !! CD_Modal_Hermite_Matrices, CD_Modal_Solve with LAPACK DSYGV) are kept as the reference - !! the banded solver is tested against, for small lines only. + !! CD_Modal_Solve_Band computes only the requested lowest modes: the eigenvalues by a + !! shift-invert block Lanczos iteration on the banded Cholesky factor of K, certified by + !! a Sturm count of K - sigma M (or, for a short line, many modes, an indefinite or + !! singular K, or a Lanczos run that is not certified, by LAPACK DSBGVX: band reduction + !! and bisection), each mode shape by inverse iteration on the banded pencil, + !! M-orthogonalised against the modes before it, and the returned eigenvalue as the + !! Rayleigh quotient of that shape. Memory and time grow linearly with the line length + !! (quadratically, with a small constant, for the band reduction). The dense routines + !! (CD_Modal_Bar_Matrices, CD_Modal_Hermite_Matrices, CD_Modal_Solve with LAPACK DSYGV) + !! are kept as the reference the banded solver is tested against, for small lines only. !! !! EI = 0 lines (two-node bars, three translations per node): an element of rest length !! l0 and current length l along the unit vector e, tension T = EA (l/l0 - 1) (zero when @@ -58,7 +60,25 @@ MODULE CableDyn_Modal PRIVATE INTEGER, PARAMETER, PUBLIC :: CD_MODAL_OK = 0, CD_MODAL_BADINPUT = 1, CD_MODAL_FAIL = 2 + ! Eigenvalue stage of CD_Modal_Solve_Band: chosen by problem size, the shift-invert Lanczos + ! iteration (falling back to the band reduction when it is not certified), or the band + ! reduction (DSBGVX) + INTEGER, PARAMETER, PUBLIC :: CD_MODAL_EIG_AUTO = 0, CD_MODAL_EIG_LANCZOS = 1, CD_MODAL_EIG_BANDRED = 2 INTEGER, PARAMETER :: I8 = SELECTED_INT_KIND(18) + ! Shift-invert block Lanczos: block size (two, for the exactly repeated frequencies of the + ! two transverse planes of a straight line), the residual tolerance of a wanted Ritz value + ! and of the Ritz values that bound the Sturm interval (relative to the Ritz value of the + ! shift-inverted operator, with a floor at round-off of the largest one), the relative gap + ! the Sturm shift sits in, and the breakdown threshold of a new Lanczos vector. Below + ! LZ_MIN_DOF free DOFs, or where the basis and its M-image would hold more than + ! LZ_MAX_WORDS values (256 MB), the band reduction is used. + INTEGER, PARAMETER :: LZ_BLOCK = 2, LZ_MIN_DOF = 200 + INTEGER(I8), PARAMETER :: LZ_MAX_WORDS = 32000000_I8 + REAL(wp), PARAMETER :: LZ_TOL = 1.0e-10_wp, LZ_TOL_MAX = 1.0e-7_wp, LZ_TOL_GUARD = 1.0e-6_wp + REAL(wp), PARAMETER :: LZ_FLOOR = 1.0e-13_wp, LZ_GAP = 1.0e-3_wp, LZ_BREAK = 1.0e-10_wp + ! Relative tolerance within which two components of a mode shape count as equally large in + ! its sign convention + REAL(wp), PARAMETER :: SIGN_TIE = 1.0e-6_wp ! Largest problem of the dense reference solver: three 4000 x 4000 matrices are 400 MB INTEGER, PARAMETER :: DENSE_MAX_DOF = 4000 ! Inverse iterations per mode: at least MIN_ITER, at most MAX_ITER @@ -715,19 +735,25 @@ SUBROUTINE CD_Modal_Hermite_Matrices(q, l0, ea, ei, rho_a, K, M, ErrStat, ErrMsg CALL band_to_dense(mb, kd, M) END SUBROUTINE CD_Modal_Hermite_Matrices - SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, ErrStat, ErrMsg) + RECURSIVE SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, ErrStat, ErrMsg, & + eig_method, eig_used) !! The n_modes lowest eigenpairs of K phi = omega^2 M phi on the free DOFs, K and M in !! symmetric upper band storage of half-bandwidth kd (CD_Modal_Bar_Band, - !! CD_Modal_Hermite_Band). The held DOFs are removed, which keeps the band. DSBGVX - !! returns the lowest n_modes eigenvalues without eigenvectors (its eigenvectors would - !! need an n x n work matrix); each shape is then found by inverse iteration with the - !! banded LU factors of K - omega^2 M, M-orthogonalised against the shapes before it - !! (so a repeated frequency, such as the two transverse planes of a taut string, gets - !! two orthogonal shapes), and accepted on the residual of its eigen-equation. omega2 - !! is the Rayleigh quotient of the shape (quadruple-precision sums), in ascending order. - !! shapes(:, j) is the full-length mode vector (zero on the held DOFs), M-orthonormal - !! on the free DOFs, signed so that its largest component is positive. A negative - !! omega2 (an unstable or compressed state) is returned as computed. + !! CD_Modal_Hermite_Band). The held DOFs are removed, which keeps the band. The lowest + !! n_modes eigenvalues come from a shift-invert block Lanczos iteration (lanczos_lowest) + !! when it is the cheaper solver and its count is certified, else from DSBGVX (without + !! eigenvectors: those would need an n x n work matrix); each shape is then found by + !! inverse iteration with the banded LU factors of K - omega^2 M, M-orthogonalised + !! against the shapes before it (so a repeated frequency, such as the two transverse + !! planes of a taut string, gets two orthogonal shapes), and accepted on the residual of + !! its eigen-equation. omega2 is the Rayleigh quotient of the shape (quadruple-precision + !! sums), in ascending order. shapes(:, j) is the full-length mode vector (zero on the + !! held DOFs), M-orthonormal on the free DOFs, signed so that its largest component is + !! positive (the first of those equal to it within SIGN_TIE: a symmetric line's mirrored + !! nodes). A negative omega2 (an unstable or compressed state) is returned as computed. + !! eig_method (CD_MODAL_EIG_AUTO by default) selects the eigenvalue stage; eig_used + !! returns the one that produced the eigenvalues (CD_MODAL_EIG_LANCZOS or + !! CD_MODAL_EIG_BANDRED; CD_MODAL_EIG_AUTO when none did). REAL(wp), INTENT(IN) :: K(:, :), M(:, :) INTEGER, INTENT(IN) :: kd LOGICAL, INTENT(IN) :: free(:) @@ -735,13 +761,15 @@ SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, REAL(wp), INTENT(OUT) :: omega2(:), shapes(:, :) INTEGER, INTENT(OUT) :: n_found, ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg - INTEGER :: n, nf, kc, i, j, ii, jj, info, m_found, mode, it, ntry, prev, pass, es + INTEGER, INTENT(IN), OPTIONAL :: eig_method + INTEGER, INTENT(OUT), OPTIONAL :: eig_used + INTEGER :: n, nf, kc, i, j, ii, jj, info, m_found, mode, it, ntry, prev, pass, es, method INTEGER, ALLOCATABLE :: map(:), iwork(:), ifail(:), ipiv(:) INTEGER(I8) :: seed REAL(wp), ALLOCATABLE :: ac(:, :), bc(:, :), a(:, :), b(:, :), w(:), work(:), lu(:, :), x(:), y(:), r(:) REAL(wp), ALLOCATABLE :: vec(:, :), bvec(:, :) - REAL(wp) :: qdum(1, 1), zdum(1, 1), anorm, bnorm, shift, scale, nrm, res, rq, noise - LOGICAL :: converged + REAL(wp) :: qdum(1, 1), zdum(1, 1), anorm, bnorm, shift, scale, nrm, res, rq, noise, xmax + LOGICAL :: converged, lz_ok CHARACTER(1024) :: em EXTERNAL :: dsbgvx @@ -750,6 +778,9 @@ SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, omega2 = CD_ZERO shapes = CD_ZERO n_found = 0 + IF (PRESENT(eig_used)) eig_used = CD_MODAL_EIG_AUTO + method = CD_MODAL_EIG_AUTO + IF (PRESENT(eig_method)) method = eig_method n = SIZE(free) IF (kd < 0 .OR. SIZE(K, 1) /= kd + 1 .OR. SIZE(M, 1) /= kd + 1 .OR. SIZE(K, 2) /= n .OR. SIZE(M, 2) /= n .OR. & SIZE(shapes, 1) /= n .OR. SIZE(shapes, 2) < n_modes .OR. SIZE(omega2) < n_modes .OR. n_modes < 1) THEN @@ -789,35 +820,46 @@ SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, END DO n_found = MIN(n_modes, nf) - ! The lowest n_found eigenvalues (DSBGVX overwrites its band copies). - ALLOCATE (a(kc + 1, nf), b(kc + 1, nf), w(nf), work(7*nf), iwork(5*nf), ifail(nf)) - a = ac - b = bc - CALL dsbgvx('N', 'I', 'U', nf, kc, kc, a, kc + 1, b, kc + 1, qdum, 1, CD_ZERO, CD_ZERO, 1, n_found, & - 2.0_wp*TINY(CD_ONE), m_found, w, zdum, 1, work, iwork, ifail, info) - IF (info /= 0 .OR. m_found /= n_found) THEN - ErrStat = CD_MODAL_FAIL - IF (info == CD_BLAS_UNAVAILABLE_INFO) THEN - CALL CD_Blas_Runtime_Check('CableDyn_Modal (DSBGVX)', es, em) - ErrMsg = 'CableDyn_Modal: '//TRIM(em) - ELSE IF (info > nf) THEN - ErrMsg = 'CableDyn_Modal: the mass matrix is not positive definite (DSBGVX)' - ELSE - BLOCK - ! Local buffer: a record longer than the caller's ErrMsg truncates - ! instead of aborting the internal write. - CHARACTER(1024) :: wmsg - INTEGER :: wios - wmsg = '' - WRITE (wmsg, '(A,I0,A,I0,A,I0)', IOSTAT=wios) 'CableDyn_Modal: DSBGVX failed, INFO = ', info, ', ', m_found, & - ' of the requested eigenvalues found: ', n_found - ErrMsg = wmsg - END BLOCK + ! The lowest n_found eigenvalues: the shift-invert Lanczos iteration where it is the + ! cheaper solver, else (or when its count is not certified) the band reduction. + ALLOCATE (w(nf)) + lz_ok = .FALSE. + IF (method == CD_MODAL_EIG_LANCZOS .OR. (method == CD_MODAL_EIG_AUTO .AND. lanczos_cheaper(nf, kc, n_found))) & + CALL lanczos_lowest(ac, bc, kc, n_found, w, lz_ok) + IF (lz_ok) THEN + IF (PRESENT(eig_used)) eig_used = CD_MODAL_EIG_LANCZOS + ELSE + ! DSBGVX overwrites its band copies. + ALLOCATE (a(kc + 1, nf), b(kc + 1, nf), work(7*nf), iwork(5*nf), ifail(nf)) + a = ac + b = bc + CALL dsbgvx('N', 'I', 'U', nf, kc, kc, a, kc + 1, b, kc + 1, qdum, 1, CD_ZERO, CD_ZERO, 1, n_found, & + 2.0_wp*TINY(CD_ONE), m_found, w, zdum, 1, work, iwork, ifail, info) + IF (info /= 0 .OR. m_found /= n_found) THEN + ErrStat = CD_MODAL_FAIL + IF (info == CD_BLAS_UNAVAILABLE_INFO) THEN + CALL CD_Blas_Runtime_Check('CableDyn_Modal (DSBGVX)', es, em) + ErrMsg = 'CableDyn_Modal: '//TRIM(em) + ELSE IF (info > nf) THEN + ErrMsg = 'CableDyn_Modal: the mass matrix is not positive definite (DSBGVX)' + ELSE + BLOCK + ! Local buffer: a record longer than the caller's ErrMsg truncates + ! instead of aborting the internal write. + CHARACTER(1024) :: wmsg + INTEGER :: wios + wmsg = '' + WRITE (wmsg, '(A,I0,A,I0,A,I0)', IOSTAT=wios) 'CableDyn_Modal: DSBGVX failed, INFO = ', info, ', ', & + m_found, ' of the requested eigenvalues found: ', n_found + ErrMsg = wmsg + END BLOCK + END IF + n_found = 0 + RETURN END IF - n_found = 0 - RETURN + DEALLOCATE (a, b, work, iwork, ifail) + IF (PRESENT(eig_used)) eig_used = CD_MODAL_EIG_BANDRED END IF - DEALLOCATE (a, b, work, iwork, ifail) ! The shapes by inverse iteration on the banded pencil. anorm = sym_band_norm(ac, kc) @@ -894,7 +936,12 @@ SUBROUTINE CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, RETURN END IF END DO - i = MAXLOC(ABS(x), 1) + ! the sign: the largest component positive; of components equal to it in magnitude to + ! round-off (the mirrored nodes of a symmetric line) the first one + xmax = MAXVAL(ABS(x)) + DO i = 1, nf - 1 + IF (ABS(x(i)) >= (CD_ONE - SIGN_TIE)*xmax) EXIT + END DO IF (x(i) < CD_ZERO) x = -x vec(:, mode) = x CALL sym_band_mv(bc, kc, x, bvec(:, mode)) @@ -934,6 +981,15 @@ END SUBROUTINE shifted_general_band SUBROUTINE mode_fail(msg) CHARACTER(*), INTENT(IN) :: msg + INTEGER :: used + IF (lz_ok) THEN + ! A shape the Lanczos eigenvalues did not give: the whole solve again on the + ! eigenvalues of the band reduction, which succeeds or fails as it does alone. + CALL CD_Modal_Solve_Band(K, M, kd, free, n_modes, omega2, shapes, n_found, ErrStat, ErrMsg, & + eig_method=CD_MODAL_EIG_BANDRED, eig_used=used) + IF (PRESENT(eig_used)) eig_used = used + RETURN + END IF ErrStat = CD_MODAL_FAIL BLOCK ! Local buffer: a record longer than the caller's ErrMsg truncates @@ -951,6 +1007,254 @@ END SUBROUTINE mode_fail END SUBROUTINE CD_Modal_Solve_Band + INTEGER FUNCTION lanczos_max_basis(nf, nwant) RESULT(mb) + !! The bound on the Lanczos basis for nwant of nf eigenvalues: the iteration usually + !! converges with about twice nwant vectors. + INTEGER, INTENT(IN) :: nf, nwant + mb = MIN(nf, 2*nwant + MAX(nwant, 40)) + END FUNCTION lanczos_max_basis + + LOGICAL FUNCTION lanczos_cheaper(nf, kc, nwant) RESULT(yes) + !! Whether the shift-invert Lanczos iteration is expected to cost less than the band + !! reduction for nwant of nf eigenvalues at half-bandwidth kc: the work of its + !! reorthogonalisation grows as nf m**2 with the basis size m (about 2 nwant + 20), + !! that of the reduction as nf**2 kc (the constant measured on lines of 300 to 24000 + !! free DOFs). + !! The memory of the basis is bounded (LZ_MAX_WORDS). + INTEGER, INTENT(IN) :: nf, kc, nwant + INTEGER(I8) :: m + m = INT(MIN(nf, 2*nwant + 20), I8) + yes = nf >= LZ_MIN_DOF .AND. m*m <= 4_I8*INT(nf, I8)*INT(MAX(kc, 1), I8) .AND. & + 2_I8*INT(lanczos_max_basis(nf, nwant), I8)*INT(nf, I8) <= LZ_MAX_WORDS + END FUNCTION lanczos_cheaper + + SUBROUTINE lanczos_lowest(ac, bc, kc, nwant, w, ok) + !! The nwant lowest eigenvalues w (ascending) of K x = lambda M x, K and M symmetric in + !! upper band storage of half-bandwidth kc, both positive definite: a block Lanczos + !! iteration (block LZ_BLOCK, a deterministic pseudo-random start, full + !! M-reorthogonalisation by two passes of classical Gram-Schmidt) on the shift-invert + !! operator S = K^-1 M (banded Cholesky factor of K), whose largest eigenvalues + !! theta = 1/lambda belong to the wanted modes. The Ritz values are those of the + !! projection V^T M S V, each accepted on its Lanczos residual, the norm of + !! S x - theta x. Shift-invert keeps the low end of the spectrum to relative accuracy + !! where a band reduction loses it to the round-off of the axial stiffness. The count is + !! certified: the Cholesky factor of K shows that no eigenvalue lies at or below zero, + !! and the inertia of K - sigma M (sturm_count) at a shift sigma in a gap above the + !! wanted values equals the number of Ritz values below sigma, so no mode is missed (a + !! frequency repeated more often than the block size, an eigenvector the start misses) + !! and none is counted twice. ok is false (w undefined) when K or M is not positive + !! definite, the basis reaches lanczos_max_basis unconverged or uncertified, or a LAPACK + !! call fails; the caller then uses the band reduction. + REAL(wp), INTENT(IN) :: ac(:, :), bc(:, :) + INTEGER, INTENT(IN) :: kc, nwant + REAL(wp), INTENT(OUT) :: w(:) + LOGICAL, INTENT(OUT) :: ok + INTEGER :: nf, mmax, nb, c0, pb, pn, nnew, i, j, r, k, info, lwork, next_check, pass + INTEGER(I8) :: seed + REAL(wp), ALLOCATABLE :: fk(:, :), v(:, :), mv(:, :), wb(:, :), mwb(:, :), h(:, :), t(:, :), theta(:) + REAL(wp), ALLOCATABLE :: work(:), c(:, :), cc(:), est(:), lam(:), y(:), my(:) + REAL(wp) :: wq(1), g(LZ_BLOCK, LZ_BLOCK), ref(LZ_BLOCK), thmax, sigma, avg + LOGICAL :: good, conv + EXTERNAL :: dpbtrf, dpbtrs, dsyev + + ok = .FALSE. + w = CD_ZERO + nf = SIZE(ac, 2) + IF (nwant < 1 .OR. nwant > nf .OR. nf < 2*LZ_BLOCK) RETURN + mmax = lanczos_max_basis(nf, nwant) + ! M, then K, positive definite (the factor of M is not used further) + ALLOCATE (fk(kc + 1, nf)) + fk = bc + CALL dpbtrf('U', nf, kc, fk, kc + 1, info) + IF (info /= 0) RETURN + fk = ac + CALL dpbtrf('U', nf, kc, fk, kc + 1, info) + IF (info /= 0) RETURN + ALLOCATE (v(nf, mmax), mv(nf, mmax), wb(nf, LZ_BLOCK), mwb(nf, LZ_BLOCK), h(mmax, mmax), t(mmax, mmax), & + theta(mmax), c(mmax, LZ_BLOCK), cc(mmax), est(mmax), lam(mmax), y(nf), my(nf)) + h = CD_ZERO + t = CD_ZERO + CALL dsyev('V', 'U', mmax, t, mmax, theta, wq, -1, info) + IF (info /= 0) RETURN + lwork = MAX(3*mmax, INT(wq(1))) + ALLOCATE (work(lwork)) + + nb = 0 + DO j = 1, LZ_BLOCK + CALL random_vector(j) + CALL append(1, SQRT(SUM(y*y)), good) + IF (.NOT. good) RETURN + END DO + c0 = 0 + next_check = MIN(nwant + 1, nf) + DO + ! the block c0 + 1..nb: wb = S v, projected out of the basis (twice), the projection + ! coefficients the new columns of H = V^T M S V + pb = nb - c0 + wb(:, 1:pb) = mv(:, c0 + 1:nb) + CALL dpbtrs('U', nf, kc, pb, fk, kc + 1, wb, nf, info) + IF (info /= 0) RETURN + IF (.NOT. CD_All_Finite(wb(:, 1:pb))) RETURN + DO j = 1, pb + ref(j) = SQRT(SUM(wb(:, j)**2)) + END DO + DO pass = 1, 2 + c(1:nb, 1:pb) = MATMUL(TRANSPOSE(mv(:, 1:nb)), wb(:, 1:pb)) + wb(:, 1:pb) = wb(:, 1:pb) - MATMUL(v(:, 1:nb), c(1:nb, 1:pb)) + h(1:nb, c0 + 1:nb) = h(1:nb, c0 + 1:nb) + c(1:nb, 1:pb) + END DO + h(c0 + 1:nb, 1:c0) = TRANSPOSE(h(1:c0, c0 + 1:nb)) + DO j = c0 + 1, nb + DO i = c0 + 1, j - 1 + avg = 0.5_wp*(h(i, j) + h(j, i)) + h(i, j) = avg + h(j, i) = avg + END DO + END DO + + IF (nb >= next_check .OR. nb == nf .OR. nb >= mmax) THEN + next_check = nb + MAX(LZ_BLOCK, nb/8) + ! Ritz values lam(r) = 1/theta, ascending, and the norms est(r) of their residuals + ! wb s (s the Ritz vector's components on the last block) + t(1:nb, 1:nb) = h(1:nb, 1:nb) + CALL dsyev('V', 'U', nb, t, mmax, theta, work, lwork, info) + IF (info /= 0) RETURN + DO j = 1, pb + CALL sym_band_mv(bc, kc, wb(:, j), mwb(:, j)) + END DO + DO j = 1, pb + DO i = 1, pb + g(i, j) = DOT_PRODUCT(wb(:, i), mwb(:, j)) + END DO + END DO + thmax = theta(nb) + DO r = 1, nb + k = nb + 1 - r + lam(r) = HUGE(CD_ONE) + IF (theta(k) > CD_ZERO) lam(r) = CD_ONE/theta(k) + est(r) = SQRT(MAX(DOT_PRODUCT(t(c0 + 1:nb, k), MATMUL(g(1:pb, 1:pb), t(c0 + 1:nb, k))), CD_ZERO)) + END DO + conv = .TRUE. + DO r = 1, nwant + conv = conv .AND. converged_ritz(r, LZ_TOL) .AND. est(r) <= LZ_TOL_MAX*theta(nb + 1 - r) + END DO + IF (conv) THEN + IF (nb == nf) THEN + ! a complete basis: its Ritz values are the eigenvalues + ok = .TRUE. + ELSE + ! the first gap above the wanted values with every Ritz value below it converged + DO k = nwant, nb - 1 + IF (k > nwant .AND. .NOT. converged_ritz(k, LZ_TOL_GUARD)) EXIT + IF (.NOT. theta(nb - k) > CD_ZERO) EXIT + IF (lam(k + 1) - lam(k) >= LZ_GAP*lam(k + 1)) THEN + sigma = 0.5_wp*(lam(k) + lam(k + 1)) + ok = sturm_count(ac, bc, kc, sigma) == k + EXIT + END IF + END DO + END IF + IF (ok) THEN + w(1:nwant) = lam(1:nwant) + RETURN + END IF + END IF + END IF + IF (nb >= mmax .OR. nb == nf) RETURN + + ! the next block: the projected wb, M-orthonormalised (a column that has vanished, an + ! invariant subspace, is replaced by a fresh pseudo-random vector) + pn = MIN(LZ_BLOCK, nf - nb, mmax - nb, pb) + nnew = nb + DO j = 1, pn + y = wb(:, j) + CALL append(nnew + 1, ref(j), good) + IF (.NOT. good) THEN + CALL random_vector(nb + LZ_BLOCK + 1) + CALL append(1, SQRT(SUM(y*y)), good) + IF (.NOT. good) RETURN + END IF + END DO + c0 = nnew + END DO + + CONTAINS + + LOGICAL FUNCTION converged_ritz(r, tol) RESULT(yes) + !! Ritz value r (ascending) converged to tol relative to its theta, or to round-off of + !! the largest theta. + INTEGER, INTENT(IN) :: r + REAL(wp), INTENT(IN) :: tol + yes = theta(nb + 1 - r) > CD_ZERO .AND. est(r) <= MAX(tol*theta(nb + 1 - r), LZ_FLOOR*thmax) + END FUNCTION converged_ritz + + SUBROUTINE random_vector(stream) + !! y: a deterministic pseudo-random vector of the given stream. + INTEGER, INTENT(IN) :: stream + INTEGER :: ir + seed = 24680_I8 + 7919_I8*INT(stream, I8) + DO ir = 1, nf + seed = MOD(69069_I8*seed + 1_I8, 2147483647_I8) + y(ir) = REAL(seed, wp)/2147483647.0_wp - 0.5_wp + END DO + END SUBROUTINE random_vector + + SUBROUTINE append(lo, yref, good) + !! y orthogonalised (two passes) against the basis vectors lo..nb, M-normalised and + !! appended; good is false when it has vanished against its reference norm yref. + INTEGER, INTENT(IN) :: lo + REAL(wp), INTENT(IN) :: yref + LOGICAL, INTENT(OUT) :: good + INTEGER :: ip + REAL(wp) :: nrm2 + good = .FALSE. + DO ip = 1, 2 + IF (nb < lo) EXIT + cc(1:nb - lo + 1) = MATMUL(TRANSPOSE(mv(:, lo:nb)), y) + y = y - MATMUL(v(:, lo:nb), cc(1:nb - lo + 1)) + END DO + CALL sym_band_mv(bc, kc, y, my) + nrm2 = DOT_PRODUCT(y, my) + IF (.NOT. (nrm2 > CD_ZERO .AND. SQRT(SUM(y*y)) > LZ_BREAK*yref .AND. nrm2 <= HUGE(nrm2))) RETURN + nb = nb + 1 + v(:, nb) = y/SQRT(nrm2) + mv(:, nb) = my/SQRT(nrm2) + good = .TRUE. + END SUBROUTINE append + + END SUBROUTINE lanczos_lowest + + INTEGER FUNCTION sturm_count(ac, bc, kc, sigma) RESULT(cnt) + !! The number of eigenvalues of K x = lambda M x below sigma, K and M symmetric in upper + !! band storage of half-bandwidth kc and M positive definite: by Sylvester's law of + !! inertia, the number of negative pivots of the LDL^T factorisation of K - sigma M + !! without pivoting (which keeps the band). -1 when a pivot is zero or not finite. + REAL(wp), INTENT(IN) :: ac(:, :), bc(:, :), sigma + INTEGER, INTENT(IN) :: kc + REAL(wp), ALLOCATABLE :: a(:, :) + REAL(wp) :: d, f + INTEGER :: nf, k, i, j + nf = SIZE(ac, 2) + ALLOCATE (a(kc + 1, nf)) + a = ac - sigma*bc + cnt = 0 + DO k = 1, nf + d = a(kc + 1, k) + IF (.NOT. (ABS(d) > CD_ZERO .AND. ABS(d) <= HUGE(d))) THEN + cnt = -1 + RETURN + END IF + IF (d < CD_ZERO) cnt = cnt + 1 + ! the trailing block of the band: a(i, j) -= a(k, i) a(k, j) / d, k < i <= j <= k + kc + DO j = k + 1, MIN(nf, k + kc) + f = a(kc + 1 + k - j, j)/d + DO i = k + 1, j + a(kc + 1 + i - j, j) = a(kc + 1 + i - j, j) - f*a(kc + 1 + k - i, i) + END DO + END DO + END DO + END FUNCTION sturm_count + SUBROUTINE CD_Modal_Solve(K, M, free, n_modes, omega2, shapes, n_found, ErrStat, ErrMsg) !! Dense reference solver: the n_modes lowest eigenpairs of K phi = omega^2 M phi on the !! free DOFs (DSYGV, symmetric parts of K and M), for lines of at most 4000 free DOFs. diff --git a/tests/test_lapack_guard_pages.c b/tests/test_lapack_guard_pages.c index b55d69c7..172df573 100644 --- a/tests/test_lapack_guard_pages.c +++ b/tests/test_lapack_guard_pages.c @@ -2,14 +2,16 @@ * SPDX-License-Identifier: Apache-2.0 * Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University * - * The modal eigensolves (DSYGV, the dense reference; DSBGVX, the banded production solver) - * called through CableDyn's LAPACK layer -- in Windows GNU builds the run-time forwarders to - * openblas.dll -- with every argument array placed against a no-access page, at its end and, - * in a second pass, at its start. Any read or write outside the arrays the modal code passes - * faults at once and names the call. Sizes cover the test_modal cases and more (n <= 96, half - * band widths 0..11), with the argument sizes CableDyn_Modal uses (WORK 7n and IWORK 5n for - * DSBGVX, the queried LWORK for DSYGV). CTest runs it with OPENBLAS_CORETYPE=Haswell, the - * kernel selected on the hosts where test_modal was seen to fault. + * The modal eigensolves (DSYGV, the dense reference; DSBGVX, the band reduction; DPBTRF and + * DPBTRS, the banded Cholesky factor and solves of the shift-invert Lanczos stage; DSYEV, its + * projected eigenproblem) called through CableDyn's LAPACK layer -- in Windows GNU builds the + * run-time forwarders to openblas.dll -- with every argument array placed against a no-access + * page, at its end and, in a second pass, at its start. Any read or write outside the arrays + * the modal code passes faults at once and names the call. Sizes cover the test_modal cases and + * more (n <= 96, half band widths 0..11), with the argument sizes CableDyn_Modal uses (WORK 7n + * and IWORK 5n for DSBGVX, two right-hand sides for DPBTRS, the queried LWORK for DSYGV and + * DSYEV). CTest runs it with OPENBLAS_CORETYPE=Haswell, the kernel selected on the hosts where + * test_modal was seen to fault. */ #if !defined(_WIN32) #define _DEFAULT_SOURCE @@ -28,6 +30,11 @@ void dsbgvx_(const char *jobz, const char *range, const char *uplo, const int *n const double *vu, const int *il, const int *iu, const double *abstol, int *m, double *w, double *z, const int *ldz, double *work, int *iwork, int *ifail, int *info, size_t jobz_len, size_t range_len, size_t uplo_len); +void dsyev_(const char *jobz, const char *uplo, const int *n, double *a, const int *lda, double *w, double *work, + const int *lwork, int *info, size_t jobz_len, size_t uplo_len); +void dpbtrf_(const char *uplo, const int *n, const int *kd, double *ab, const int *ldab, int *info, size_t uplo_len); +void dpbtrs_(const char *uplo, const int *n, const int *kd, const int *nrhs, const double *ab, const int *ldab, + double *b, const int *ldb, int *info, size_t uplo_len); static char where[96] = "start"; @@ -160,6 +167,25 @@ int main(void) fprintf(stderr, "FAIL: DSYGV n = %d returned INFO = %d\n", n, info); ++failures; } + for (j = 0; j < n; ++j) { + for (i = 0; i <= j; ++i) { + a[i + j * n] = a[j + i * n] = next_random(&state) - 0.5; + } + } + lwork = -1; + snprintf(where, sizeof where, "DSYEV workspace query, n = %d", n); + dsyev_("V", "U", &n, a, &n, w, &query, &lwork, &info, 1, 1); + lwork = (int)query; + if (lwork < 3 * n) { + lwork = 3 * n; + } + work = guarded(sizeof(double) * (size_t)lwork, front); + snprintf(where, sizeof where, "DSYEV, n = %d, %s guard", n, front ? "front" : "end"); + dsyev_("V", "U", &n, a, &n, w, work, &lwork, &info, 1, 1); + if (info != 0) { + fprintf(stderr, "FAIL: DSYEV n = %d returned INFO = %d\n", n, info); + ++failures; + } for (kd = 0; kd <= 11 && kd < n; ++kd) { int ld = kd + 1, il = 1, iu = n < 6 ? n : 6, m = 0, one = 1; double vl = 0.0, vu = 0.0, tol = 4.4501477170144028e-308, q = 0.0, z = 0.0; @@ -183,12 +209,40 @@ int main(void) fprintf(stderr, "FAIL: DSBGVX n = %d kd = %d returned INFO = %d, M = %d\n", n, kd, info, m); ++failures; } + { + int two = 2; + double *fk = guarded(sizeof(double) * (size_t)ld * (size_t)n, front); + double *rhs = guarded(sizeof(double) * 2 * (size_t)n, front); + for (j = 0; j < n; ++j) { + for (i = j - kd > 0 ? j - kd : 0; i <= j; ++i) { + fk[(kd + i - j) + j * ld] = i == j ? 2.0 * n + kd : next_random(&state) - 0.5; + } + rhs[j] = next_random(&state); + rhs[j + n] = next_random(&state); + } + snprintf(where, sizeof where, "DPBTRF, n = %d, kd = %d, %s guard", n, kd, + front ? "front" : "end"); + dpbtrf_("U", &n, &kd, fk, &ld, &info, 1); + if (info != 0) { + fprintf(stderr, "FAIL: DPBTRF n = %d kd = %d returned INFO = %d\n", n, kd, info); + ++failures; + continue; + } + snprintf(where, sizeof where, "DPBTRS, n = %d, kd = %d, %s guard", n, kd, + front ? "front" : "end"); + dpbtrs_("U", &n, &kd, &two, fk, &ld, rhs, &n, &info, 1); + if (info != 0) { + fprintf(stderr, "FAIL: DPBTRS n = %d kd = %d returned INFO = %d\n", n, kd, info); + ++failures; + } + } } } } if (failures != 0) { return 1; } - printf("PASS: DSYGV and DSBGVX stay inside the arrays the modal code passes (n <= 96)\n"); + printf("PASS: DSYGV, DSYEV, DSBGVX, DPBTRF and DPBTRS stay inside the arrays the modal code passes " + "(n <= 96)\n"); return 0; } diff --git a/tests/test_modal.f90 b/tests/test_modal.f90 index a6cdee8f..7fef0e71 100644 --- a/tests/test_modal.f90 +++ b/tests/test_modal.f90 @@ -41,6 +41,8 @@ END FUNCTION blas_config CALL case_hermite_beam() CALL case_hermite_long_beam() CALL case_slack_string() + CALL case_solver_agreement() + CALL case_repeated_frequency() CALL case_irvine() CALL case_deck_hermite_and_rules() IF (nfail > 0) THEN @@ -201,23 +203,35 @@ SUBROUTINE case_hermite_beam() END SUBROUTINE case_hermite_beam SUBROUTINE check_band_against_dense(label, k, m, kb, mb, kd, free, om2_dense) - !! The banded solver on the band form of the dense matrices k, m: the frequencies of the - !! dense reference, and shapes that are M-orthonormal, zero on the held DOFs and satisfy - !! K phi = omega^2 M phi. + !! The banded solver on the band form of the dense matrices k, m, with each eigenvalue + !! stage (the shift-invert Lanczos iteration and the band reduction): the frequencies of + !! the dense reference, and shapes that are M-orthonormal, zero on the held DOFs and + !! satisfy K phi = omega^2 M phi. CHARACTER(*), INTENT(IN) :: label REAL(wp), INTENT(IN) :: k(:, :), m(:, :), kb(:, :), mb(:, :), om2_dense(:) INTEGER, INTENT(IN) :: kd LOGICAL, INTENT(IN) :: free(:) + CALL check_band_method(label//'[lanczos]', k, m, kb, mb, kd, free, om2_dense, CD_MODAL_EIG_LANCZOS) + CALL check_band_method(label//'[band-reduction]', k, m, kb, mb, kd, free, om2_dense, CD_MODAL_EIG_BANDRED) + END SUBROUTINE check_band_against_dense + + SUBROUTINE check_band_method(label, k, m, kb, mb, kd, free, om2_dense, method) + !! check_band_against_dense with one eigenvalue stage, which must be the one used. + CHARACTER(*), INTENT(IN) :: label + REAL(wp), INTENT(IN) :: k(:, :), m(:, :), kb(:, :), mb(:, :), om2_dense(:) + INTEGER, INTENT(IN) :: kd, method + LOGICAL, INTENT(IN) :: free(:) REAL(wp), ALLOCATABLE :: om2(:), sh(:, :), ksh(:, :), msh(:, :), gram(:, :) REAL(wp) :: knorm, fdiff, resid, ortho - INTEGER :: nm, nf, es, j + INTEGER :: nm, nf, es, j, used LOGICAL :: held_zero CHARACTER(256) :: em nm = SIZE(om2_dense) ALLOCATE (om2(nm), sh(SIZE(free), nm)) - CALL CD_Modal_Solve_Band(kb, mb, kd, free, nm, om2, sh, nf, es, em) + CALL CD_Modal_Solve_Band(kb, mb, kd, free, nm, om2, sh, nf, es, em, eig_method=method, eig_used=used) CALL require(es == CD_MODAL_OK .AND. nf == nm, label//':band-solve: '//TRIM(em)) IF (es /= CD_MODAL_OK .OR. nf /= nm) RETURN + CALL require(used == method, label//':eigenvalue-stage-used') knorm = MAXVAL(SUM(ABS(k), 1)) fdiff = 0.0_wp DO j = 1, nm @@ -244,7 +258,7 @@ SUBROUTINE check_band_against_dense(label, k, m, kb, mb, kd, free, om2_dense) CALL require(resid < 1.0e-9_wp, label//':band-shape-residual') CALL require(ortho < 1.0e-8_wp, label//':band-shapes-M-orthonormal') CALL require(held_zero, label//':band-held-dofs-zero') - END SUBROUTINE check_band_against_dense + END SUBROUTINE check_band_method SUBROUTINE case_slack_string() !! A slack tension-only string has no stiffness (K = 0): every frequency is zero and the @@ -252,7 +266,7 @@ SUBROUTINE case_slack_string() INTEGER, PARAMETER :: NE = 20, NN = NE + 1, NM = 6 REAL(wp) :: q(3*NN), l0(NE), ea_(NE), rho_a(NE), om2(NM), gram(NM, NM) REAL(wp), ALLOCATABLE :: kb(:, :), mb(:, :), k(:, :), m(:, :), sh(:, :) - INTEGER :: conn(2, NE), i, es, nf, kd + INTEGER :: conn(2, NE), i, es, nf, kd, used LOGICAL :: free(3*NN) CHARACTER(256) :: em ALLOCATE (k(3*NN, 3*NN), m(3*NN, 3*NN), sh(3*NN, NM)) @@ -280,18 +294,25 @@ SUBROUTINE case_slack_string() gram(i, i) = gram(i, i) - 1.0_wp END DO CALL require(nan_max_abs(gram) < 1.0e-10_wp, 'slack:shapes-M-orthonormal') + ! K = 0 is not positive definite: the Lanczos stage hands over to the band reduction + CALL CD_Modal_Solve_Band(kb, mb, kd, free, NM, om2, sh, nf, es, em, eig_method=CD_MODAL_EIG_LANCZOS, & + eig_used=used) + CALL require(es == CD_MODAL_OK .AND. nf == NM .AND. used == CD_MODAL_EIG_BANDRED, & + 'slack:lanczos-hands-over: '//TRIM(em)) + CALL require(nan_max_abs(om2) < 1.0e-9_wp, 'slack:lanczos-hands-over-zero-frequencies') END SUBROUTINE case_slack_string SUBROUTINE case_hermite_long_beam() !! A pre-tensioned 1024-element beam-string (6150 DOFs, beyond the dense reference): the !! banded solver against the closed-form frequencies; each repeated frequency (the two - !! transverse planes) gets shapes in both planes. + !! transverse planes) gets shapes in both planes. A line this long takes the shift-invert + !! Lanczos eigenvalue stage. INTEGER, PARAMETER :: NE = 1024, NN = NE + 1, NM = 10 REAL(wp), PARAMETER :: L = 500.0_wp, EI = 1.0e6_wp, RHO = 100.0_wp, EPS = 1.0e-4_wp REAL(wp), ALLOCATABLE :: q(:), l0(:), ea(:), eiv(:), rho_a(:), kb(:, :), mb(:, :), sh(:, :) LOGICAL, ALLOCATABLE :: free(:) REAL(wp) :: om2(NM), t, mc, f_exp, f_got, sy, sz - INTEGER :: i, es, kd, nf, j, n + INTEGER :: i, es, kd, nf, j, n, used INTEGER(8) :: c0, c1, rate CHARACTER(256) :: em ALLOCATE (q(6*NN), l0(NE), ea(NE), eiv(NE), rho_a(NE), sh(6*NN, NM), free(6*NN)) @@ -308,10 +329,11 @@ SUBROUTINE case_hermite_long_beam() CALL SYSTEM_CLOCK(c0, rate) CALL CD_Modal_Hermite_Band(q, l0, ea, eiv, rho_a, kd, kb, mb, es, em) CALL require(es == CD_MODAL_OK, 'long-beam:band-matrices: '//TRIM(em)) - CALL CD_Modal_Solve_Band(kb, mb, kd, free, NM, om2, sh, nf, es, em) + CALL CD_Modal_Solve_Band(kb, mb, kd, free, NM, om2, sh, nf, es, em, eig_used=used) CALL SYSTEM_CLOCK(c1) CALL require(es == CD_MODAL_OK .AND. nf == NM, 'long-beam:band-solve: '//TRIM(em)) IF (es /= CD_MODAL_OK .OR. nf /= NM) RETURN + CALL require(used == CD_MODAL_EIG_LANCZOS, 'long-beam:lanczos-eigenvalues') WRITE (*, '(A,I0,A,F8.3,A)') 'long beam: ', COUNT(free), ' free DOFs, banded solve in ', & REAL(c1 - c0, wp)/REAL(rate, wp), ' s' t = 1.0e9_wp*EPS @@ -330,6 +352,160 @@ SUBROUTINE case_hermite_long_beam() END DO END SUBROUTINE case_hermite_long_beam + SUBROUTINE sagged_line(finite_ei, ne, kb, mb, kd, free) + !! A line sagged on a parabola (span 500 m, sag 60 m, stretch 1e-3, ends held) with the + !! Morison added mass: a finite-EI power cable or an EI = 0 chain, in band storage. + LOGICAL, INTENT(IN) :: finite_ei + INTEGER, INTENT(IN) :: ne + REAL(wp), ALLOCATABLE, INTENT(OUT) :: kb(:, :), mb(:, :) + INTEGER, INTENT(OUT) :: kd + LOGICAL, ALLOCATABLE, INTENT(OUT) :: free(:) + REAL(wp), PARAMETER :: SPAN = 500.0_wp, SAG = 60.0_wp, EPS = 1.0e-3_wp + REAL(wp), ALLOCATABLE :: q(:), l0(:), ea(:), ei(:), rho(:), dia(:), can(:), cat(:), wl(:), xs(:), zs(:) + REAL(wp) :: dzdx, tl + INTEGER :: i, nn, es + INTEGER, ALLOCATABLE :: conn(:, :) + CHARACTER(256) :: em + nn = ne + 1 + ALLOCATE (l0(ne), ea(ne), ei(ne), rho(ne), dia(ne), can(ne), cat(ne), xs(0:ne), zs(0:ne)) + DO i = 0, ne + xs(i) = SPAN*REAL(i, wp)/ne + zs(i) = -4.0_wp*SAG*(xs(i)/SPAN)*(1.0_wp - xs(i)/SPAN) + END DO + DO i = 1, ne + l0(i) = SQRT((xs(i) - xs(i - 1))**2 + (zs(i) - zs(i - 1))**2)/(1.0_wp + EPS) + END DO + dia = 0.16_wp + can = 1.0_wp + cat = 0.0_wp + IF (finite_ei) THEN + ALLOCATE (q(6*nn), free(6*nn)) + DO i = 0, ne + dzdx = -4.0_wp*SAG/SPAN*(1.0_wp - 2.0_wp*xs(i)/SPAN) + tl = SQRT(1.0_wp + dzdx**2) + q(6*i + 1:6*i + 6) = [xs(i), 0.0_wp, zs(i), (1.0_wp + EPS)/tl, 0.0_wp, (1.0_wp + EPS)*dzdx/tl] + END DO + ea = 4.69e8_wp + ei = 1.99e4_wp + rho = 36.7_wp + free = .TRUE. + free(1:3) = .FALSE. + free(6*nn - 5:6*nn - 3) = .FALSE. + CALL CD_Modal_Hermite_Band(q, l0, ea, ei, rho, kd, kb, mb, es, em, am_rho=1025.0_wp, am_waterline=0.0_wp, & + am_diam=dia, am_can=can, am_cat=cat) + ELSE + ALLOCATE (q(3*nn), free(3*nn), wl(nn), conn(2, ne)) + DO i = 0, ne + q(3*i + 1:3*i + 3) = [xs(i), 0.0_wp, zs(i)] + END DO + DO i = 1, ne + conn(:, i) = [i, i + 1] + END DO + ea = 8.0e8_wp + rho = 150.0_wp + wl = 0.0_wp + free = .TRUE. + free(1:3) = .FALSE. + free(3*nn - 2:3*nn) = .FALSE. + CALL CD_Modal_Bar_Band(q, conn, l0, ea, rho, .TRUE., kd, kb, mb, es, em, am_rho=1025.0_wp, am_waterline=wl, & + am_diam=dia, am_can=can, am_cat=cat) + END IF + CALL require(es == CD_MODAL_OK, 'sagged-line:band-matrices: '//TRIM(em)) + END SUBROUTINE sagged_line + + SUBROUTINE case_solver_agreement() + !! The two eigenvalue stages on lines of distinct frequencies: a sagged finite-EI line + !! with added mass (300 elements, 1800 free DOFs) and a sagged EI = 0 chain (400 + !! elements, 1197). The production route takes the shift-invert Lanczos stage and gives + !! the frequencies of the band reduction to 1e-10 and the same shapes, with the same + !! signs (the symmetric sag puts equally large components on mirrored nodes: the first + !! one is positive). + INTEGER, PARAMETER :: NM = 20 + REAL(wp), ALLOCATABLE :: kb(:, :), mb(:, :), sh_l(:, :), sh_b(:, :) + LOGICAL, ALLOCATABLE :: free(:) + REAL(wp) :: om2_l(NM), om2_b(NM), fdiff, sdiff, gap + INTEGER :: kd, nf_l, nf_b, es_l, es_b, used, j, icase + CHARACTER(256) :: em + CHARACTER(16) :: label + DO icase = 1, 2 + IF (icase == 1) THEN + label = 'agree-hermite' + CALL sagged_line(.TRUE., 300, kb, mb, kd, free) + ELSE + label = 'agree-chain' + CALL sagged_line(.FALSE., 400, kb, mb, kd, free) + END IF + IF (ALLOCATED(sh_l)) DEALLOCATE (sh_l, sh_b) + ALLOCATE (sh_l(SIZE(free), NM), sh_b(SIZE(free), NM)) + CALL CD_Modal_Solve_Band(kb, mb, kd, free, NM, om2_l, sh_l, nf_l, es_l, em, eig_used=used) + CALL require(es_l == CD_MODAL_OK .AND. nf_l == NM, TRIM(label)//':solve: '//TRIM(em)) + CALL require(used == CD_MODAL_EIG_LANCZOS, TRIM(label)//':lanczos-eigenvalues') + CALL CD_Modal_Solve_Band(kb, mb, kd, free, NM, om2_b, sh_b, nf_b, es_b, em, eig_method=CD_MODAL_EIG_BANDRED) + CALL require(es_b == CD_MODAL_OK .AND. nf_b == NM, TRIM(label)//':band-reduction-solve: '//TRIM(em)) + IF (es_l /= CD_MODAL_OK .OR. es_b /= CD_MODAL_OK .OR. nf_l /= NM .OR. nf_b /= NM) CYCLE + fdiff = 0.0_wp + sdiff = 0.0_wp + DO j = 1, NM + fdiff = MAX(fdiff, ABS(om2_l(j) - om2_b(j))/ABS(om2_b(j))) + gap = HUGE(1.0_wp) + IF (j > 1) gap = MIN(gap, (om2_b(j) - om2_b(j - 1))/om2_b(j)) + IF (j < NM) gap = MIN(gap, (om2_b(j + 1) - om2_b(j))/om2_b(j)) + IF (gap > 1.0e-4_wp) sdiff = MAX(sdiff, nan_max_abs(sh_l(:, j) - sh_b(:, j))/nan_max_abs(sh_b(:, j))) + END DO + WRITE (*, '(A,2ES11.3)') TRIM(label)//': Lanczos vs band reduction, max relative omega^2 and shape '// & + 'differences: ', fdiff, sdiff + CALL require(fdiff <= 1.0e-10_wp, TRIM(label)//':frequencies-equal') + CALL require(sdiff <= 1.0e-8_wp, TRIM(label)//':shapes-and-signs-equal') + END DO + END SUBROUTINE case_solver_agreement + + SUBROUTINE case_repeated_frequency() + !! A frequency repeated six times, three times the Lanczos block: NN nodes of six DOFs + !! joined along a chain by identical springs on each DOF (ends held), K = L (x) I6 with L + !! the chain Laplacian, M = I, eigenvalues 2 k (1 - cos(j pi/(NN + 1))), each sixfold; the + !! 8 lowest modes cut the second value's copies. The Sturm count of the Lanczos stage + !! rejects a run that misses a copy (without it, a single-vector block accepts one): each + !! stage returns every copy, with M-orthonormal shapes. The same lattice shifted to a + !! negative lowest eigenvalue (K - s M, indefinite): the Lanczos stage hands over to the + !! band reduction. + INTEGER, PARAMETER :: NN = 150, NC = 6, N = NC*NN, NM = 8, KD = NC + REAL(wp), PARAMETER :: KS = 1.0e4_wp + REAL(wp), ALLOCATABLE :: kb(:, :), mb(:, :), sh(:, :), gram(:, :) + REAL(wp) :: om2(NM), lam(NM), shift, err + LOGICAL :: free(N) + INTEGER :: i, j, nf, es, used, method, pass + CHARACTER(256) :: em + ALLOCATE (kb(KD + 1, N), mb(KD + 1, N), sh(N, NM)) + free = .TRUE. + DO pass = 1, 2 + shift = 0.0_wp + IF (pass == 2) shift = 3.0_wp*KS*(1.0_wp - COS(PI/REAL(NN + 1, wp))) + kb = 0.0_wp + mb = 0.0_wp + kb(KD + 1, :) = 2.0_wp*KS - shift + kb(1, KD + 1:N) = -KS + mb(KD + 1, :) = 1.0_wp + DO j = 1, NM + lam(j) = 2.0_wp*KS*(1.0_wp - COS(REAL((j + NC - 1)/NC, wp)*PI/REAL(NN + 1, wp))) - shift + END DO + DO method = CD_MODAL_EIG_AUTO, CD_MODAL_EIG_LANCZOS + CALL CD_Modal_Solve_Band(kb, mb, KD, free, NM, om2, sh, nf, es, em, eig_method=method, eig_used=used) + CALL require(es == CD_MODAL_OK .AND. nf == NM, 'repeated:solve: '//TRIM(em)) + IF (es /= CD_MODAL_OK .OR. nf /= NM) CYCLE + err = MAXVAL(ABS(om2 - lam))/MAXVAL(ABS(lam)) + gram = MATMUL(TRANSPOSE(sh), sh) + DO i = 1, NM + gram(i, i) = gram(i, i) - 1.0_wp + END DO + WRITE (*, '(A,I0,A,I0,A,I0,A,2ES11.3)') 'repeated frequency: pass ', pass, ', method ', method, ', used ', & + used, ': omega^2 error, M-orthonormality error ', err, nan_max_abs(gram) + CALL require(err < 1.0e-9_wp, 'repeated:every-copy-found') + CALL require(nan_max_abs(gram) < 1.0e-10_wp, 'repeated:shapes-M-orthonormal') + IF (pass == 2) CALL require(used == CD_MODAL_EIG_BANDRED, 'repeated:indefinite-hands-over') + END DO + END DO + END SUBROUTINE case_repeated_frequency + ! ------------------------------------------------------------------ deck helpers SUBROUTINE read_table(path, marker, ncol, data, nrow)