@@ -145,6 +145,15 @@ MODULE CableDyn_Dynamic
145145 REAL (wp), ALLOCATABLE :: nodes_eval(:, :)
146146 INTEGER :: topo_n_nodes = 0 , bw_kl = 0 , bw_ku = 0
147147 LOGICAL :: topo_valid = .FALSE. , bw_valid = .FALSE.
148+ ! M_band holds the element consistent mass band assembled for the remembered connectivity
149+ ! and free list (bw_free) from the lengths mb_l0 and masses per length mb_rho; a step whose
150+ ! inputs match reuses it instead of assembling the same band again. Every other writer of
151+ ! M_band clears mb_valid.
152+ LOGICAL :: mb_valid = .FALSE.
153+ REAL (wp), ALLOCATABLE :: mb_l0(:), mb_rho(:)
154+ ! TEST-ONLY diagnostic: the number of steps that reused the remembered mass band (saturating).
155+ ! Only the unit tests read it (to assert that the cache hit); no result depends on it.
156+ INTEGER :: mb_hits = 0
148157 INTEGER :: ad_kl = 0
149158 INTEGER , ALLOCATABLE :: ad_finv(:), ad_ipiv(:), ad_fixed(:)
150159 REAL (wp), ALLOCATABLE :: ad_full(:, :), ad_add(:, :), ad_ab(:, :), ad_x(:, :), ad_result(:, :)
@@ -164,7 +173,8 @@ MODULE CableDyn_Dynamic
164173 LOGICAL :: end_valid = .FALSE.
165174 ! the factored effective band in eff_band/ipiv belongs to the current step (a junction solve
166175 ! of this step factored it): a warm re-solve of the same step starts with a chord iteration
167- ! on it instead of assembling and factoring a new tangent
176+ ! on it instead of assembling and factoring a new tangent. Cleared whenever eff_band is
177+ ! overwritten by a tangent that is not factored.
168178 LOGICAL :: step_factor = .FALSE.
169179 ! force scale of the junction solves (largest element tension of the last one): the
170180 ! residual gate of a warm-started junction solve is relative to it, not to its own
@@ -311,6 +321,10 @@ SUBROUTINE CD_Clear_GenAlpha_Workspace(work)
311321 work% bw_ku = 0
312322 work% topo_valid = .FALSE.
313323 work% bw_valid = .FALSE.
324+ work% mb_valid = .FALSE.
325+ work% mb_hits = 0
326+ IF (ALLOCATED (work% mb_l0)) DEALLOCATE (work% mb_l0)
327+ IF (ALLOCATED (work% mb_rho)) DEALLOCATE (work% mb_rho)
314328 IF (ALLOCATED (work% ad_finv)) DEALLOCATE (work% ad_finv)
315329 IF (ALLOCATED (work% ad_ipiv)) DEALLOCATE (work% ad_ipiv)
316330 IF (ALLOCATED (work% ad_fixed)) DEALLOCATE (work% ad_fixed)
@@ -536,6 +550,8 @@ SUBROUTINE CD_Cable_Initial_Acceleration(q, v, elem_conn, l0, ea, rho_a, tension
536550 RETURN
537551 END IF
538552 END IF
553+ ! the band is factored in place below: not the step's reusable mass band
554+ work% mb_valid = .FALSE.
539555 IF (use_element_mass_band) THEN
540556 CALL assemble_mass_band_free(elem_conn, l0, rho_a, work% dof_marker(1 :n_dof), kl, ku, M_band, es, em)
541557 ELSE
@@ -615,7 +631,7 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
615631 REAL (wp), POINTER :: a_eval(:), a_alpha(:), q_alpha(:), v_eval(:), v_alpha(:), fint_eval(:)
616632 ! q_alpha viewed as (3, n_nodes) nodal coordinates, without a copy
617633 REAL (wp), POINTER :: q_alpha_nodes(:, :)
618- LOGICAL :: topo_ok, bw_hit
634+ LOGICAL :: topo_ok, bw_hit, mb_hit
619635 INTEGER :: il, jl
620636 REAL (wp), POINTER :: Kt_eval(:, :), tension_eval(:), load_eval(:), jac_q_eval(:, :), jac_v_eval(:, :)
621637 REAL (wp), POINTER :: M_eff_eval(:, :), M_add_eval(:, :), dMa_a_dq_eval(:, :)
@@ -745,6 +761,7 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
745761 direct_banded = .NOT. has_load_proc .AND. .NOT. has_added_mass_proc
746762 use_element_mass_band = direct_banded .AND. .NOT. PRESENT (structural_mass)
747763 CALL validate_topology_once(work, elem_conn, n_dof/ 3 , topo_ok)
764+ bw_hit = .FALSE.
748765 IF (direct_banded) THEN
749766 bw_hit = topo_ok .AND. work% bw_valid
750767 IF (bw_hit) bw_hit = SIZE (work% bw_free) == n_free
@@ -753,6 +770,7 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
753770 kl = work% bw_kl
754771 ku = work% bw_ku
755772 ELSE
773+ work% mb_valid = .FALSE.
756774 CALL CD_Cable_Free_Bandwidth(elem_conn, n_dof/ 3 , free, kl, ku, es, em, topology_validated= topo_ok)
757775 IF (es /= 0 ) THEN
758776 CALL fail(ErrStat, ErrMsg, ' bandwidth analysis failed: ' // TRIM (em))
@@ -907,9 +925,22 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
907925 RETURN
908926 END IF
909927 END IF
928+ es = 0
910929 IF (use_element_mass_band) THEN
911- CALL assemble_mass_band_free(elem_conn, l0, rho_a, work% dof_marker(1 :n_dof), kl, ku, M_band, es, em)
930+ ! The element mass band depends only on the connectivity, the free list, l0 and
931+ ! rho_a: a step with the inputs of the remembered band (bw_hit: same connectivity and
932+ ! free list) reuses it, which is the band the assembly would build again.
933+ mb_hit = .FALSE.
934+ IF (bw_hit) mb_hit = mass_band_matches(work, l0, rho_a)
935+ IF (.NOT. mb_hit) THEN
936+ work% mb_valid = .FALSE.
937+ CALL assemble_mass_band_free(elem_conn, l0, rho_a, work% dof_marker(1 :n_dof), kl, ku, M_band, es, em)
938+ IF (es == 0 .AND. work% bw_valid) CALL remember_mass_band(work, l0, rho_a)
939+ ELSE IF (work% mb_hits < HUGE (work% mb_hits)) THEN
940+ work% mb_hits = work% mb_hits + 1
941+ END IF
912942 ELSE
943+ work% mb_valid = .FALSE.
913944 CALL pack_dense_free_band(M, free, kl, ku, CD_ONE, M_band, es, em)
914945 END IF
915946 IF (es /= 0 ) THEN
@@ -955,6 +986,13 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
955986 IF (tangent_fresh) THEN
956987 IF (direct_banded) THEN
957988 CALL eval_dynamic(qk, R, es, em, effbandout= eff_band)
989+ ! factored_now and work%step_factor state that eff_band/ipiv hold the LU of the last
990+ ! factorization. eff_band now holds the unfactored tangent at qk, and they are set
991+ ! again only once it is factored below: an exit before that (convergence, or the
992+ ! near-tolerance allowance at the iteration limit) leaves the junction end query and
993+ ! a warm re-solve to factor it instead of back-solving with an unfactored band.
994+ factored_now = .FALSE.
995+ work% step_factor = .FALSE.
958996 ELSE
959997 CALL eval_dynamic(qk, R, es, em, effout= eff)
960998 END IF
@@ -1017,7 +1055,10 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
10171055 eff_band_unfactored(il, jl) = eff_band(il, jl)
10181056 END DO
10191057 END DO
1020- CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es, em)
1058+ ! eval_dynamic has just checked this band finite when a banded load or added-mass
1059+ ! callback contributes to it; the factorization then skips repeating that scan.
1060+ CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es, em, &
1061+ matrix_validated= has_load_band_proc .OR. has_added_mass_band_proc)
10211062 factored_now = es == 0
10221063 work% step_factor = factored_now .AND. work% end_request
10231064 IF (es /= 0 ) THEN
@@ -1031,7 +1072,9 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on
10311072 op_norm = MAX (CD_ONE, dense_row_abs_sum_max(eff, free))
10321073 END IF
10331074 IF (direct_banded) THEN
1034- CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es, em)
1075+ ! A tangent factored in this iteration is the finite LU CD_Factor_Banded returned; a
1076+ ! reused one keeps the full check.
1077+ CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es, em, factor_validated= tangent_fresh)
10351078 ELSE
10361079 eff_free = eff(free, free)
10371080 CALL CD_Solve_Dense_As_Banded(eff_free, dq, es, em)
@@ -1758,6 +1801,12 @@ SUBROUTINE ensure_step_workspace(work, n_dof, n_elem, n_free, ldab, need_band, n
17581801 CALL ensure_real_vector(work% load_eval, n_dof, ErrStat, ErrMsg)
17591802 IF (ErrStat /= CD_DYN_OK) RETURN
17601803 IF (need_band) THEN
1804+ ! a reallocated mass band no longer holds the remembered one
1805+ IF (ALLOCATED (work% M_band)) THEN
1806+ IF (SIZE (work% M_band, 1 ) < ldab .OR. SIZE (work% M_band, 2 ) < n_free) work% mb_valid = .FALSE.
1807+ ELSE
1808+ work% mb_valid = .FALSE.
1809+ END IF
17611810 CALL ensure_real_matrix(work% M_band, ldab, n_free, ErrStat, ErrMsg)
17621811 IF (ErrStat /= CD_DYN_OK) RETURN
17631812 CALL ensure_real_vector(work% M_add_a_eval, n_dof, ErrStat, ErrMsg)
@@ -1863,6 +1912,56 @@ SUBROUTINE remember_bandwidth(work, free, kl, ku, topology_validated)
18631912 work% bw_valid = .TRUE.
18641913 END SUBROUTINE remember_bandwidth
18651914
1915+ LOGICAL FUNCTION mass_band_matches (work , l0 , rho_a ) RESULT(match)
1916+ ! ! work%M_band holds the element mass band of these l0 and rho_a (for the remembered
1917+ ! ! connectivity and free list). Equality is tested as <= and >= so that a NaN never matches.
1918+ TYPE (CD_CableGenAlphaWorkspace), INTENT (IN ) :: work
1919+ REAL (wp), INTENT (IN ) :: l0(:), rho_a(:)
1920+ INTEGER :: e
1921+
1922+ match = work% mb_valid
1923+ IF (match) match = SIZE (work% mb_l0) == SIZE (l0) .AND. SIZE (work% mb_rho) == SIZE (rho_a)
1924+ IF (.NOT. match) RETURN
1925+ DO e = 1 , SIZE (l0)
1926+ IF (.NOT. (work% mb_l0(e) <= l0(e) .AND. work% mb_l0(e) >= l0(e))) THEN
1927+ match = .FALSE.
1928+ RETURN
1929+ END IF
1930+ END DO
1931+ DO e = 1 , SIZE (rho_a)
1932+ IF (.NOT. (work% mb_rho(e) <= rho_a(e) .AND. work% mb_rho(e) >= rho_a(e))) THEN
1933+ match = .FALSE.
1934+ RETURN
1935+ END IF
1936+ END DO
1937+ END FUNCTION mass_band_matches
1938+
1939+ SUBROUTINE remember_mass_band (work , l0 , rho_a )
1940+ ! ! Record the inputs of the element mass band just assembled into work%M_band.
1941+ TYPE (CD_CableGenAlphaWorkspace), INTENT (INOUT ) :: work
1942+ REAL (wp), INTENT (IN ) :: l0(:), rho_a(:)
1943+ INTEGER :: istat
1944+
1945+ work% mb_valid = .FALSE.
1946+ IF (ALLOCATED (work% mb_l0)) THEN
1947+ IF (SIZE (work% mb_l0) /= SIZE (l0)) DEALLOCATE (work% mb_l0)
1948+ END IF
1949+ IF (ALLOCATED (work% mb_rho)) THEN
1950+ IF (SIZE (work% mb_rho) /= SIZE (rho_a)) DEALLOCATE (work% mb_rho)
1951+ END IF
1952+ IF (.NOT. ALLOCATED (work% mb_l0)) THEN
1953+ ALLOCATE (work% mb_l0(SIZE (l0)), STAT= istat)
1954+ IF (istat /= 0 ) RETURN
1955+ END IF
1956+ IF (.NOT. ALLOCATED (work% mb_rho)) THEN
1957+ ALLOCATE (work% mb_rho(SIZE (rho_a)), STAT= istat)
1958+ IF (istat /= 0 ) RETURN
1959+ END IF
1960+ work% mb_l0 = l0
1961+ work% mb_rho = rho_a
1962+ work% mb_valid = .TRUE.
1963+ END SUBROUTINE remember_mass_band
1964+
18661965 SUBROUTINE partition_free_dofs_workspace (fixed_dofs , n_dof , free , n_free , fixed , marker , ErrStat , ErrMsg )
18671966 INTEGER , INTENT (IN ) :: fixed_dofs(:), n_dof
18681967 INTEGER , INTENT (INOUT ) :: free(:), fixed(:)
@@ -1907,14 +2006,15 @@ SUBROUTINE partition_free_dofs_workspace(fixed_dofs, n_dof, free, n_free, fixed,
19072006 END SUBROUTINE partition_free_dofs_workspace
19082007
19092008 SUBROUTINE ensure_int_vector (x , n , ErrStat , ErrMsg )
2009+ ! ! The ensure_* helpers run for every workspace array on every step: they write ErrMsg only
2010+ ! ! on failure, and their callers (ensure_dof_workspace, ensure_step_workspace) clear it.
19102011 INTEGER , ALLOCATABLE , INTENT (INOUT ) :: x(:)
19112012 INTEGER , INTENT (IN ) :: n
19122013 INTEGER , INTENT (OUT ) :: ErrStat
1913- CHARACTER (* ), INTENT (OUT ) :: ErrMsg
2014+ CHARACTER (* ), INTENT (INOUT ) :: ErrMsg
19142015 INTEGER :: istat
19152016
19162017 ErrStat = CD_DYN_OK
1917- ErrMsg = ' '
19182018 IF (n < 0 ) THEN
19192019 CALL fail(ErrStat, ErrMsg, ' internal workspace size must be non-negative' )
19202020 RETURN
@@ -1931,11 +2031,10 @@ SUBROUTINE ensure_real_vector(x, n, ErrStat, ErrMsg)
19312031 REAL (wp), ALLOCATABLE , INTENT (INOUT ) :: x(:)
19322032 INTEGER , INTENT (IN ) :: n
19332033 INTEGER , INTENT (OUT ) :: ErrStat
1934- CHARACTER (* ), INTENT (OUT ) :: ErrMsg
2034+ CHARACTER (* ), INTENT (INOUT ) :: ErrMsg
19352035 INTEGER :: istat
19362036
19372037 ErrStat = CD_DYN_OK
1938- ErrMsg = ' '
19392038 IF (n < 0 ) THEN
19402039 CALL fail(ErrStat, ErrMsg, ' internal workspace size must be non-negative' )
19412040 RETURN
@@ -1952,11 +2051,10 @@ SUBROUTINE ensure_real_matrix(x, n1, n2, ErrStat, ErrMsg)
19522051 REAL (wp), ALLOCATABLE , INTENT (INOUT ) :: x(:, :)
19532052 INTEGER , INTENT (IN ) :: n1, n2
19542053 INTEGER , INTENT (OUT ) :: ErrStat
1955- CHARACTER (* ), INTENT (OUT ) :: ErrMsg
2054+ CHARACTER (* ), INTENT (INOUT ) :: ErrMsg
19562055 INTEGER :: istat
19572056
19582057 ErrStat = CD_DYN_OK
1959- ErrMsg = ' '
19602058 IF (n1 < 0 .OR. n2 < 0 ) THEN
19612059 CALL fail(ErrStat, ErrMsg, ' internal workspace shape must be non-negative' )
19622060 RETURN
0 commit comments