From d277ed41ef5fb18023aa120000ee56938bd7dc39 Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Fri, 20 Nov 2026 12:29:53 +0900 Subject: [PATCH 1/4] perf: cheaper nodal-twist reference and holonomy Hessians The nodal twist evaluates every element's reference twist and holonomy, with their 12 x 12 Hessians on each tangent assembly. On a power cable with the nodal twist in a coupled OpenFAST run, these kernels are a large share of the cable step. A good part of that time went on run-time library calls and on Hessian entries nobody reads: - skew, outer, outer9 and identity3 used RESHAPE and SPREAD, which gfortran turns into calls into its run-time library (spread, reshape_r8) on every Gauss point and solid angle. They now set their entries one by one, with the same products. - The solid angle of a reference triangle formed its full 9 x 9 Hessian, while CD_HermiteTwist_Reference reads only the blocks between the triangle's free vertices (the node tangents; the committed tangents and end directors are fixed). With the new optional argument free, solid_angle forms those blocks alone, each entry with the same expression as before. - element_holonomy evaluated the shape derivatives twice per Gauss point (once directly, once inside element_ab). It also formed the two blocks of its 9 x 9 intermediate Hessian that are never read (rows of the unit tangent against the r' and r'' columns). - The finiteness checks of the reference vertices and of the Hessian are one rank-3 and one rank-2 test instead of 6 and 12 calls per element. Every entry is formed by the same floating-point operations in the same order, so the results are bit-identical. Signed-off-by: Jae Hoon Seo --- src/CableDyn_HermiteTorsion.f90 | 116 +++++++++++++++++++++++++------- 1 file changed, 93 insertions(+), 23 deletions(-) diff --git a/src/CableDyn_HermiteTorsion.f90 b/src/CableDyn_HermiteTorsion.f90 index 752b5586..8929f748 100644 --- a/src/CableDyn_HermiteTorsion.f90 +++ b/src/CableDyn_HermiteTorsion.f90 @@ -1396,7 +1396,7 @@ PURE SUBROUTINE CD_HermiteTwist_Reference(qe, L, tri, tslot, tsign, value, grad, LOGICAL, INTENT(IN), OPTIONAL :: holonomy INTEGER, INTENT(IN), OPTIONAL :: quadrature_order INTEGER :: order, k, j, i, ntri, si, sj - LOGICAL :: want_hess, with_h, need(2), finite + LOGICAL :: want_hess, with_h, need(2), finite, free(3) REAL(wp) :: node_t(3, 2), proj(3, 3, 2), x(3, 3), om, g9(9), h9(9, 9), gt(3, 2), htt(3, 3, 2, 2) REAL(wp) :: he, ge(12), hk(12, 12), dmy, blk(3, 3), blk2(3, 3) CHARACTER(200) :: em @@ -1433,13 +1433,8 @@ PURE SUBROUTINE CD_HermiteTwist_Reference(qe, L, tri, tslot, tsign, value, grad, END IF need(1) = ANY(tslot == 1) .OR. with_h need(2) = ANY(tslot == 2) .OR. with_h - ! (column by column: a reshaped copy of the assumed-shape tri would be a heap temporary) - finite = CD_All_Finite(qe) .AND. CD_Is_Finite(L) .AND. CD_All_Finite(tsign) - DO k = 1, ntri - DO j = 1, 3 - finite = finite .AND. CD_All_Finite(tri(:, j, k)) - END DO - END DO + ! (the rank-3 test reads tri in place: a reshaped copy would be a heap temporary) + finite = CD_All_Finite(qe) .AND. CD_Is_Finite(L) .AND. CD_All_Finite(tsign) .AND. CD_All_Finite(tri) IF (.NOT. finite) THEN ErrStat = CD_HTORS_NONFINITE ErrMsg = 'CD_HermiteTwist_Reference: non-finite input' @@ -1488,7 +1483,11 @@ PURE SUBROUTINE CD_HermiteTwist_Reference(qe, L, tri, tslot, tsign, value, grad, RETURN END IF END DO - CALL solid_angle(x(:, 1), x(:, 2), x(:, 3), want_hess, om, g9, h9) + ! (a local mask: the expression tslot(:, k) /= 0 as the argument is a heap temporary) + DO j = 1, 3 + free(j) = tslot(j, k) /= 0 + END DO + CALL solid_angle(x(:, 1), x(:, 2), x(:, 3), want_hess, om, g9, h9, free=free) value = value + tsign(k)*om DO j = 1, 3 si = tslot(j, k) @@ -1544,11 +1543,7 @@ PURE SUBROUTINE CD_HermiteTwist_Reference(qe, L, tri, tslot, tsign, value, grad, RETURN END IF IF (want_hess) THEN - finite = .TRUE. - DO j = 1, 12 - finite = finite .AND. CD_All_Finite(hess(:, j)) - END DO - IF (.NOT. finite) THEN + IF (.NOT. CD_All_Finite(hess)) THEN ErrStat = CD_HTORS_NONFINITE ErrMsg = 'CD_HermiteTwist_Reference: non-finite twist Hessian' value = CD_ZERO @@ -1727,8 +1722,14 @@ PURE SUBROUTINE element_holonomy(qe, L, order, want_hess, h, grad, hess, margin, margin = HUGE(CD_ONE) CALL CD_HermiteCable_Gauss_Rule(order, gp, gw) DO ig = 1, order + ! (element_ab, on the shape derivatives at hand) CALL shape_derivatives(L, gp(ig), dh, ddh) - CALL element_ab(qe, L, gp(ig), a, b) + a = CD_ZERO + b = CD_ZERO + DO i = 1, 4 + a = a + dh(i)*qe(3*i - 2:3*i) + b = b + ddh(i)*qe(3*i - 2:3*i) + END DO s2 = DOT_PRODUCT(a, a) sp = SQRT(s2) IF (sp <= GEOM_TOL) THEN @@ -1771,7 +1772,12 @@ PURE SUBROUTINE element_holonomy(qe, L, order, want_hess, h, grad, hess, margin, hd(1:3, 1:3) = 2.0_wp*eye + ta*(eye/sp - outer(a, a)/(sp*s2)) + (outer(a, t) + outer(t, a))/sp hd(1:3, 7:9) = outer(a, a)/sp + sp*eye hd(7:9, 1:3) = TRANSPOSE(hd(1:3, 7:9)) - hf = (hn - f*hd - outer9(gf, gd) - outer9(gd, gf))/den + ! (the blocks read below: rows 7:9 only against columns 7:9) + DO j = 1, 9 + DO i = 1, MERGE(9, 6, j > 6) + hf(i, j) = (hn(i, j) - f*hd(i, j) - gf(i)*gd(j) - gd(i)*gf(j))/den + END DO + END DO DO j = 1, 4 DO i = 1, 4 blk = dh(i)*dh(j)*hf(1:3, 1:3) + dh(i)*ddh(j)*hf(1:3, 4:6) + ddh(i)*dh(j)*hf(4:6, 1:3) & @@ -1871,14 +1877,18 @@ PURE FUNCTION smallest_rotation(a, b, v) RESULT(r) r = c*v + cross(w, v) + w*DOT_PRODUCT(w, v)/(CD_ONE + c) END FUNCTION smallest_rotation - PURE SUBROUTINE solid_angle(x, y, z, want_hess, om, g, h) + PURE SUBROUTINE solid_angle(x, y, z, want_hess, om, g, h, free) !! Omega = 2 atan2(N, D), N = x.(y x z), D = 1 + x.y + y.z + z.x (signed solid angle of the !! geodesic triangle x, y, z), with gradient g and Hessian h in the raw vectors (x, y, z). - !! Under the fold guard every triangle used here has D >= 1.5. + !! Under the fold guard every triangle used here has D >= 1.5. With free, only the Hessian + !! blocks between two free vertices (free(j): vertex j is a variable of the caller) are + !! formed, each entry as the full Hessian forms it; the others are zero. REAL(wp), INTENT(IN) :: x(3), y(3), z(3) LOGICAL, INTENT(IN) :: want_hess REAL(wp), INTENT(OUT) :: om, g(9), h(9, 9) - REAL(wp) :: nv, dv, r2, gn(9), gd(9), eye(3, 3) + LOGICAL, INTENT(IN), OPTIONAL :: free(3) + REAL(wp) :: nv, dv, r2, gn(9), gd(9), eye(3, 3), h0(3, 3), c1, c2, s(3, 3) + INTEGER :: bi, bj, i, j, ii, jj nv = DOT_PRODUCT(x, cross(y, z)) dv = CD_ONE + DOT_PRODUCT(x, y) + DOT_PRODUCT(y, z) + DOT_PRODUCT(z, x) @@ -1894,6 +1904,42 @@ PURE SUBROUTINE solid_angle(x, y, z, want_hess, om, g, h) h = CD_ZERO IF (.NOT. want_hess) RETURN eye = identity3() + IF (PRESENT(free)) THEN + c1 = nv*nv - dv*dv + c2 = 2.0_wp*dv*nv + DO bj = 1, 3 + IF (.NOT. free(bj)) CYCLE + DO bi = 1, 3 + IF (.NOT. free(bi)) CYCLE + ! the block (bi, bj) of d2N and d2D before the scaling below + IF (bi == bj) THEN + h0 = CD_ZERO + ELSE + IF (bi + bj == 3) THEN + s = skew(z) + ELSE IF (bi + bj == 5) THEN + s = skew(x) + ELSE + s = skew(y) + END IF + IF (bj == MOD(bi, 3) + 1) THEN + h0 = -dv*s - nv*eye + ELSE + h0 = dv*s - nv*eye + END IF + END IF + DO j = 1, 3 + jj = 3*(bj - 1) + j + DO i = 1, 3 + ii = 3*(bi - 1) + i + h(ii, jj) = 2.0_wp*(h0(i, j)/r2 + (c1*(gn(ii)*gd(jj) + gd(ii)*gn(jj)) & + - c2*(gn(ii)*gn(jj) - gd(ii)*gd(jj)))/(r2*r2)) + END DO + END DO + END DO + END DO + RETURN + END IF ! d2N: (x,y) = -S(z), (y,z) = -S(x), (z,x) = -S(y); d2D: identity off-diagonal blocks h(1:3, 4:6) = -dv*skew(z) - nv*eye h(4:6, 1:3) = dv*skew(z) - nv*eye @@ -1959,28 +2005,52 @@ PURE FUNCTION cross(a, b) RESULT(c) c = [a(2)*b(3) - a(3)*b(2), a(3)*b(1) - a(1)*b(3), a(1)*b(2) - a(2)*b(1)] END FUNCTION cross + ! (element by element: the RESHAPE and SPREAD forms of these are run-time library calls on the + ! torsion kernels' hot path; the entries are the same products) + PURE FUNCTION skew(v) RESULT(s) !! S(v) w = v x w. REAL(wp), INTENT(IN) :: v(3) REAL(wp) :: s(3, 3) - s = RESHAPE([CD_ZERO, v(3), -v(2), -v(3), CD_ZERO, v(1), v(2), -v(1), CD_ZERO], [3, 3]) + s(1, 1) = CD_ZERO + s(2, 1) = v(3) + s(3, 1) = -v(2) + s(1, 2) = -v(3) + s(2, 2) = CD_ZERO + s(3, 2) = v(1) + s(1, 3) = v(2) + s(2, 3) = -v(1) + s(3, 3) = CD_ZERO END FUNCTION skew PURE FUNCTION outer(a, b) RESULT(m) REAL(wp), INTENT(IN) :: a(3), b(3) REAL(wp) :: m(3, 3) - m = SPREAD(a, 2, 3)*SPREAD(b, 1, 3) + INTEGER :: i, j + DO j = 1, 3 + DO i = 1, 3 + m(i, j) = a(i)*b(j) + END DO + END DO END FUNCTION outer PURE FUNCTION outer9(a, b) RESULT(m) REAL(wp), INTENT(IN) :: a(9), b(9) REAL(wp) :: m(9, 9) - m = SPREAD(a, 2, 9)*SPREAD(b, 1, 9) + INTEGER :: i, j + DO j = 1, 9 + DO i = 1, 9 + m(i, j) = a(i)*b(j) + END DO + END DO END FUNCTION outer9 PURE FUNCTION identity3() RESULT(m) REAL(wp) :: m(3, 3) - m = RESHAPE([CD_ONE, CD_ZERO, CD_ZERO, CD_ZERO, CD_ONE, CD_ZERO, CD_ZERO, CD_ZERO, CD_ONE], [3, 3]) + m = CD_ZERO + m(1, 1) = CD_ONE + m(2, 2) = CD_ONE + m(3, 3) = CD_ONE END FUNCTION identity3 END MODULE CableDyn_HermiteTorsion From 569ccef4ac45e49f16b6e80828f99aca781eb66b Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Fri, 20 Nov 2026 12:29:53 +0900 Subject: [PATCH 2/4] perf: a frame-only twist transfer re-bases only the elements that moved CD_HermiteCable_Dyn_Set_Torsion_Frames moves the committed torsion end frames without a step. The OpenFAST output probe calls it on every CalcOutput, and the checkpoint rebuild and the linearization call it as well. It re-based the twist reference of every element (tw_advance_reference), one twist_reference evaluation per element including the element holonomy. Yet the host writes only the end nodes, so the reference twist of every other element stays as it was. tw_advance_reference now forms the unit node tangents first. An interior element (not at a rigid end) whose two tangents equal the reference tangents, bit for bit, keeps its reference twist without the evaluation. Its transport solid angles are then exactly the reference terms they are differenced against, so twist_reference returns tw_beta(e) + tw_wrap(0) + 0; that sum is formed directly. The directors are carried as before. A frame-only transfer therefore evaluates only the elements at a rigid end, or at a node whose tangent the host turned. The commit of a step evaluates every element whose tangents moved. A zero tangent handle keeps a zero tangent in the scratch, so its elements are still evaluated and report it. The results are bit-identical. Signed-off-by: Jae Hoon Seo --- CHANGELOG.md | 10 +++++++ src/CableDyn_HermiteCableDynamic.f90 | 43 ++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c78ca02d..617e8ce5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -257,6 +257,16 @@ All notable changes to CableDyn are recorded in this file. The format follows evaluation. Results are unchanged to the bit; the Delta Flume case 9 deck of `validation/experiments/delta_flume` (64 elements, 80 000 steps) runs in 58.6 s instead of 73.2 s. +- The nodal twist of a finite-EI cable (torsion with polar inertia `Irn`) is cheaper to + evaluate. Its reference-twist and holonomy Hessians no longer call the run-time library for + small outer products and skew matrices, and they form only the solid-angle and intermediate + blocks that are read. A frame-only twist transfer (the OpenFAST output probe on every + `CalcOutput`, the checkpoint rebuild, linearization) re-bases only the elements at a rigid end + or at a turned node; the others keep their reference twist, which the evaluation would return + unchanged. Outputs, `.CD.out` and restarts are unchanged to the bit. On the IEA-15MW + VolturnUS-S with three chains and a lazy-wave power cable restrained in torsion (one thread), + simulation CPU per simulated hour fell from 36.5 to 32.5 min (241 elements, `DT = 0.02 s`) + and from 35.0 to 30.6 min (291 elements, `DT = 0.025 s`). ### Fixed diff --git a/src/CableDyn_HermiteCableDynamic.f90 b/src/CableDyn_HermiteCableDynamic.f90 index ed77b53b..1d1b92b7 100644 --- a/src/CableDyn_HermiteCableDynamic.f90 +++ b/src/CableDyn_HermiteCableDynamic.f90 @@ -11685,18 +11685,50 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) !! reference twists and end-frame references (the step's own formulas, so the committed !! state reproduces the converged iterate bit for bit), the unit tangents and the directors !! carried over by the smallest rotation (a rigid end's: its end frame's normal carried). + !! + !! An interior element (not at a rigid end) whose two unit node tangents are those of the + !! reference, bit for bit, keeps its reference twist: its transport solid angles are those + !! of the reference terms they are differenced against, so twist_reference returns + !! tw_beta(e) + tw_wrap(0) + 0, which is formed here without the evaluation. A frame-only + !! transfer between steps (CD_HermiteCable_Dyn_Set_Torsion_Frames at the committed shape, + !! where a host writes only the end nodes) so evaluates only the elements at a rigid end or + !! at a node whose tangent the host turned; the commit of a step evaluates every element + !! whose node tangents moved. (The skipped elements passed every check of twist_reference at + !! the same tangents and, the host writing only end nodes, the same shape.) TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model REAL(wp), INTENT(IN) :: q_new(:), ends_new(3, 4) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg INTEGER :: e, i, iend - REAL(wp) :: rho, beta, g(12), h(12, 12), psi, gl(3), hl(3, 3), u(3), t(3) + REAL(wp) :: rho, beta, g(12), h(12, 12), psi, gl(3), hl(3, 3), u(3), t(3), rn + LOGICAL :: kept_prev, kept_i ErrStat = CD_HCDYN_OK ErrMsg = '' + ! the unit tangents at q_new first: an element whose two tangents are the reference's keeps + ! its reference twist + ! (a zero handle keeps a zero tangent here, so its elements are evaluated and report it) + DO i = 1, model%nn + rn = NORM2(q_new(6*i - 2:6*i)) + model%tw_stn(:, i) = CD_ZERO + IF (rn > CD_ZERO) model%tw_stn(:, i) = q_new(6*i - 2:6*i)/rn + END DO + kept_prev = .FALSE. DO e = 1, model%ne - CALL twist_reference(model, e, q_new, ends_new, .FALSE., rho, beta, g, h, ErrStat, ErrMsg) - IF (ErrStat /= CD_HCDYN_OK) RETURN - model%tw_sbeta(e) = beta + kept_i = bits_equal(model%tw_stn(1, e + 1), model%tw_tn(1, e + 1)) .AND. & + bits_equal(model%tw_stn(2, e + 1), model%tw_tn(2, e + 1)) .AND. & + bits_equal(model%tw_stn(3, e + 1), model%tw_tn(3, e + 1)) + IF (e == 1) kept_prev = bits_equal(model%tw_stn(1, 1), model%tw_tn(1, 1)) .AND. & + bits_equal(model%tw_stn(2, 1), model%tw_tn(2, 1)) .AND. & + bits_equal(model%tw_stn(3, 1), model%tw_tn(3, 1)) + IF (kept_prev .AND. kept_i .AND. .NOT. ((e == 1 .AND. model%tw_rigid(1)) .OR. & + (e == model%ne .AND. model%tw_rigid(2)))) THEN + model%tw_sbeta(e) = model%tw_beta(e) + tw_wrap(CD_ZERO) + CD_ZERO + ELSE + CALL twist_reference(model, e, q_new, ends_new, .FALSE., rho, beta, g, h, ErrStat, ErrMsg) + IF (ErrStat /= CD_HCDYN_OK) RETURN + model%tw_sbeta(e) = beta + END IF + kept_prev = kept_i END DO model%tw_spsi = model%tw_psi DO iend = 1, 2 @@ -11706,8 +11738,7 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) model%tw_spsi(iend) = psi END DO DO i = 1, model%nn - t = q_new(6*i - 2:6*i)/NORM2(q_new(6*i - 2:6*i)) - model%tw_stn(:, i) = t + t = model%tw_stn(:, i) IF (i == 1 .AND. model%tw_rigid(1)) THEN u = CD_HermiteTwist_Carry(ends_new(:, 1), t, ends_new(:, 2)) ELSE IF (i == model%nn .AND. model%tw_rigid(2)) THEN From 0742970893844151d232b46ca91c056a11761b7c Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Fri, 20 Nov 2026 13:20:22 +0900 Subject: [PATCH 3/4] fix: keep an element's reference twist only at the committed shape The frame-only shortcut of tw_advance_reference kept an element's reference twist when its two unit node tangents were the reference's. That was enough for the value: the reference twist depends on the element only through those tangents. It was not enough for the checks, though. twist_reference also checks the element's shape (the holonomy's fold and handle checks). Suppose a host translates an elastically restrained end past its neighbouring node without turning the end tangent. The end element then folds while both of its tangents are unchanged, and the q_check pass of CD_HermiteCable_Dyn_Set_Torsion_Frames accepted it. So CD_HFMF_SetCoupledKinematics wrote the boundary before the following assembly found the fold, which broke its all-or-nothing update. The committed reference now records the configuration it was formed at (tw_rq, set by tw_commit_reference from the configuration tw_advance_reference re-based at). An element keeps its reference twist only when its 12 DOFs are those, bit for bit, as well as its tangents. It then passed every check of twist_reference at that very shape. A reference that comes from a mirror, a snapshot or tw_init carries no configuration (tw_rq_ok = .FALSE.), so every element is evaluated until the next commit. The results stay bit-identical. Signed-off-by: Jae Hoon Seo --- src/CableDyn_HermiteCableDynamic.f90 | 59 ++++++++++++++++++++-------- tests/test_nodal_twist_dynamic.f90 | 54 ++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 18 deletions(-) diff --git a/src/CableDyn_HermiteCableDynamic.f90 b/src/CableDyn_HermiteCableDynamic.f90 index 1d1b92b7..ac51f3cf 100644 --- a/src/CableDyn_HermiteCableDynamic.f90 +++ b/src/CableDyn_HermiteCableDynamic.f90 @@ -640,6 +640,12 @@ MODULE CableDyn_HermiteCableDynamic ! the step's residual norm. REAL(wp) :: tw_tsum = CD_ZERO, tw_spsi(2) = CD_ZERO, tw_tscale = CD_ONE REAL(wp), ALLOCATABLE :: tw_su(:, :), tw_stn(:, :), tw_sbeta(:) + ! tw_sq: the configuration tw_advance_reference re-based at; tw_rq: that of the committed + ! reference (tw_commit_reference), valid while tw_rq_ok (a reference that comes from a + ! mirror, a snapshot or tw_init has none). An element whose DOFs are tw_rq's, bit for bit, + ! passed every check of twist_reference there and keeps its reference twist. + REAL(wp), ALLOCATABLE :: tw_sq(:), tw_rq(:) + LOGICAL :: tw_rq_ok = .FALSE. ! tw_som(:, k): the angular velocity of end frame k over the running step, log(R_{n+1} ! R_n^T)/dt (R = [d n d x n]); tw_om: that of the committed step. A turning frame twists ! the element at a rigid end through rho, and the torsional damping takes that rate. @@ -10900,6 +10906,9 @@ PURE SUBROUTINE tw_unpack(model, buf) model%tw_psi = buf(9*nn + 2*ne + 1:9*nn + 2*ne + 2) model%tw_om = RESHAPE(buf(9*nn + 2*ne + 3:9*nn + 2*ne + 8), [3, 2]) model%tw_oal = RESHAPE(buf(9*nn + 2*ne + 9:9*nn + 2*ne + 14), [3, 2]) + ! (a reloaded reference carries no configuration of its own: tw_advance_reference evaluates + ! every element until the next commit) + model%tw_rq_ok = .FALSE. END SUBROUTINE tw_unpack ELEMENTAL REAL(wp) FUNCTION tw_wrap(x) RESULT(y) @@ -11519,7 +11528,7 @@ SUBROUTINE tw_init(model, mt, ErrStat, ErrMsg) model%tw_we(nn)) ALLOCATE (model%tw_eg(12, ne), model%tw_ehv(12, ne), model%tw_ek(ne), model%tw_ed(ne), model%tw_me(ne)) ALLOCATE (model%tw_K7(3*KB7 + 1, 7*nn), model%tw_x7(7*nn), model%tw_ipiv(7*nn)) - ALLOCATE (model%tw_su(3, nn), model%tw_stn(3, nn), model%tw_sbeta(ne)) + ALLOCATE (model%tw_su(3, nn), model%tw_stn(3, nn), model%tw_sbeta(ne), model%tw_sq(6*nn), model%tw_rq(6*nn)) ALLOCATE (model%ws_recovery_tw(tw_pack_size(model))) END IF model%tw_pn = CD_ZERO; model%tw_wn = CD_ZERO; model%tw_an = CD_ZERO; model%tw_pk = CD_ZERO @@ -11615,6 +11624,7 @@ SUBROUTINE tw_init(model, mt, ErrStat, ErrMsg) model%tw_w = CD_ZERO model%tw_al = CD_ZERO model%tw_mt = mt + model%tw_rq_ok = .FALSE. model%tw_active = .TRUE. CONTAINS PURE FUNCTION unit_perp(x, ax) RESULT(y) @@ -11675,6 +11685,8 @@ SUBROUTINE tw_release(model) IF (ALLOCATED(model%tw_eg)) DEALLOCATE (model%tw_eg, model%tw_ehv, model%tw_ek, model%tw_ed, model%tw_me) IF (ALLOCATED(model%tw_K7)) DEALLOCATE (model%tw_K7, model%tw_x7, model%tw_ipiv) IF (ALLOCATED(model%tw_su)) DEALLOCATE (model%tw_su, model%tw_stn, model%tw_sbeta) + IF (ALLOCATED(model%tw_sq)) DEALLOCATE (model%tw_sq, model%tw_rq) + model%tw_rq_ok = .FALSE. IF (ALLOCATED(model%ws_recovery_tw)) DEALLOCATE (model%ws_recovery_tw) IF (ALLOCATED(model%snap_tw)) DEALLOCATE (model%snap_tw) IF (ALLOCATED(model%snap_tw_fcR)) DEALLOCATE (model%snap_tw_fcR) @@ -11686,15 +11698,15 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) !! state reproduces the converged iterate bit for bit), the unit tangents and the directors !! carried over by the smallest rotation (a rigid end's: its end frame's normal carried). !! - !! An interior element (not at a rigid end) whose two unit node tangents are those of the - !! reference, bit for bit, keeps its reference twist: its transport solid angles are those - !! of the reference terms they are differenced against, so twist_reference returns - !! tw_beta(e) + tw_wrap(0) + 0, which is formed here without the evaluation. A frame-only - !! transfer between steps (CD_HermiteCable_Dyn_Set_Torsion_Frames at the committed shape, - !! where a host writes only the end nodes) so evaluates only the elements at a rigid end or - !! at a node whose tangent the host turned; the commit of a step evaluates every element - !! whose node tangents moved. (The skipped elements passed every check of twist_reference at - !! the same tangents and, the host writing only end nodes, the same shape.) + !! An interior element (not at a rigid end) whose 12 DOFs are those the committed reference + !! was formed at (tw_rq), bit for bit, keeps its reference twist: its unit node tangents are + !! the reference's, so its transport solid angles are those of the reference terms they are + !! differenced against and twist_reference returns tw_beta(e) + tw_wrap(0) + 0, which is + !! formed here without the evaluation; and it passed every check of twist_reference (the + !! element's fold and handle checks) at that same shape. A frame-only transfer between steps + !! (CD_HermiteCable_Dyn_Set_Torsion_Frames at the committed shape, where a host writes only + !! the end nodes) so evaluates only the elements at a rigid end or at a node the host moved; + !! the commit of a step evaluates every element that moved. TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model REAL(wp), INTENT(IN) :: q_new(:), ends_new(3, 4) INTEGER, INTENT(OUT) :: ErrStat @@ -11712,14 +11724,11 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) model%tw_stn(:, i) = CD_ZERO IF (rn > CD_ZERO) model%tw_stn(:, i) = q_new(6*i - 2:6*i)/rn END DO + model%tw_sq = q_new kept_prev = .FALSE. DO e = 1, model%ne - kept_i = bits_equal(model%tw_stn(1, e + 1), model%tw_tn(1, e + 1)) .AND. & - bits_equal(model%tw_stn(2, e + 1), model%tw_tn(2, e + 1)) .AND. & - bits_equal(model%tw_stn(3, e + 1), model%tw_tn(3, e + 1)) - IF (e == 1) kept_prev = bits_equal(model%tw_stn(1, 1), model%tw_tn(1, 1)) .AND. & - bits_equal(model%tw_stn(2, 1), model%tw_tn(2, 1)) .AND. & - bits_equal(model%tw_stn(3, 1), model%tw_tn(3, 1)) + kept_i = node_kept(e + 1) + IF (e == 1) kept_prev = node_kept(1) IF (kept_prev .AND. kept_i .AND. .NOT. ((e == 1 .AND. model%tw_rigid(1)) .OR. & (e == model%ne .AND. model%tw_rigid(2)))) THEN model%tw_sbeta(e) = model%tw_beta(e) + tw_wrap(CD_ZERO) + CD_ZERO @@ -11749,6 +11758,22 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) u = u - DOT_PRODUCT(u, t)*t model%tw_su(:, i) = u/NORM2(u) END DO + CONTAINS + LOGICAL FUNCTION node_kept(k) RESULT(kept) + !! Node k's six DOFs are those of the committed reference configuration and its unit + !! tangent the reference tangent, bit for bit. + INTEGER, INTENT(IN) :: k + INTEGER :: jj + kept = model%tw_rq_ok + DO jj = 6*k - 5, 6*k + IF (.NOT. kept) RETURN + kept = bits_equal(q_new(jj), model%tw_rq(jj)) + END DO + DO jj = 1, 3 + IF (.NOT. kept) RETURN + kept = bits_equal(model%tw_stn(jj, k), model%tw_tn(jj, k)) + END DO + END FUNCTION node_kept END SUBROUTINE tw_advance_reference SUBROUTINE tw_frame_rates(model, dt) @@ -11776,6 +11801,8 @@ SUBROUTINE tw_commit_reference(model) model%tw_beta = model%tw_sbeta model%tw_psi = model%tw_spsi model%tw_tn = model%tw_stn + model%tw_rq = model%tw_sq + model%tw_rq_ok = .TRUE. model%tw_u = model%tw_su END SUBROUTINE tw_commit_reference diff --git a/tests/test_nodal_twist_dynamic.f90 b/tests/test_nodal_twist_dynamic.f90 index 0a6bcf08..8d97d890 100644 --- a/tests/test_nodal_twist_dynamic.f90 +++ b/tests/test_nodal_twist_dynamic.f90 @@ -28,7 +28,11 @@ PROGRAM test_nodal_twist_dynamic !! carries at most 4e-6 of the peak kinetic energy with an extreme Irn = 1 kg m (6e-8 at !! 0.02 kg m), below the step's own energy band; !! R restart: a rolling, torsionally damped run restored from a snapshot, and one rebuilt - !! from the checkpoint mirror, continue bit-identically. + !! from the checkpoint mirror, continue bit-identically; + !! F a coupled write that folds the end element (the coupled end, elastic in torsion and + !! pinned in bending, translated past its neighbour with its tangent unchanged) is refused + !! before anything is written, after a march and after a frame-only transfer (whose + !! re-basing keeps the elements at the committed shape). USE CableDyn_Precision, ONLY: wp USE CableDyn_EndConnection, ONLY: CD_ENDCONN_RIGID USE CableDyn_HermiteTorsion, ONLY: CD_HermiteTorsionType, CD_HermiteTorsion_Line, CD_HermiteTorsion_Unwrap, & @@ -41,7 +45,8 @@ PROGRAM test_nodal_twist_dynamic USE CableDyn_OpenFAST_HermiteFMF, ONLY: CD_HFMF_ModuleType, CD_HFMF_Init, CD_HFMF_Set_EndConnection, & CD_HFMF_Set_Torsion, CD_HFMF_UpdateStates, CD_HFMF_CalcOutput, & CD_HFMF_Snapshot, CD_HFMF_Restore, CD_HFMF_MirrorSize, & - CD_HFMF_PackMirror, CD_HFMF_UnpackMirror, CD_HFMF_End, CD_HFMF_OK + CD_HFMF_PackMirror, CD_HFMF_UnpackMirror, CD_HFMF_End, CD_HFMF_OK, & + CD_HFMF_SetCoupledKinematics IMPLICIT NONE REAL(wp), PARAMETER :: PI = 3.14159265358979323846_wp @@ -61,6 +66,7 @@ PROGRAM test_nodal_twist_dynamic CALL check_frame_energy() CALL check_pinned_energy() CALL check_restart() + CALL check_fold_refused() IF (nfail > 0) THEN WRITE (*, '(A,I0,A)') 'FAIL: ', nfail, ' assertion(s) failed' @@ -266,6 +272,50 @@ SUBROUTINE check_quasi_static(label, pinned, compliance) CALL CD_HFMF_End(cn) END SUBROUTINE check_quasi_static + ! ------------------------------------------------------------------------------------------ + SUBROUTINE check_fold_refused() + !! F: march a bending-pinned line, elastic in torsion, then write the coupled end 0.8 m back + !! along the line, past its neighbour 0.5 m away, with the end tangent unchanged: the end + !! element folds, and the write is refused with the state untouched. The same after a + !! frame-only transfer of the march's last pose. + TYPE(CD_HFMF_ModuleType), ALLOCATABLE :: cab + REAL(wp) :: q(NDOF), x(3), v(3), a(3), dcm(3, 3), w(3), al(3), phi + REAL(wp), ALLOCATABLE :: q0(:), u0(:, :), b0(:) + INTEGER :: es, pass + LOGICAL :: ok + CHARACTER(300) :: em + ALLOCATE (cab) + CALL line_seed(0.0_wp, 0.0_wp, q) + CALL build_cable(cab, q, 1.0_wp, 0.01_wp, 1.0e-10_wp, 0.8_wp, .TRUE., 1.0e-9_wp, 0.05_wp) + CALL require(cab%line%tw_active, 'F: the nodal twist is active') + CALL march(cab, 0, 20, ok) + CALL require(ok, 'F: the march steps') + IF (.NOT. ok) RETURN + DO pass = 1, 2 + CALL drive(20, x, v, a, dcm, w, al, phi) + IF (pass == 2) THEN + ! a frame-only transfer of the committed pose first: it re-bases the reference + x = cab%line%q(NDOF - 5:NDOF - 3) + CALL CD_HFMF_SetCoupledKinematics(cab, x, v, a, es, em, u_orientation=dcm, & + u_angular_velocity=w, u_angular_acceleration=al) + CALL require(es == CD_HFMF_OK, 'F: a frame-only transfer of the committed pose: '//TRIM(em)) + END IF + q0 = cab%line%q + u0 = cab%line%tw_u + b0 = cab%line%tw_beta + ! (the coupled node's committed position, node NN; the cable frame is the global one) + x = cab%line%q(NDOF - 5:NDOF - 3) - [0.8_wp, 0.0_wp, 0.0_wp] + CALL CD_HFMF_SetCoupledKinematics(cab, x, v, a, es, em, u_orientation=dcm, u_angular_velocity=w, & + u_angular_acceleration=al) + WRITE (*, '(A,I0,A)') 'F fold through the coupled end (pass ', pass, '): '//TRIM(em) + CALL require(es /= CD_HFMF_OK, 'F: a write that folds the end element is refused') + CALL require(nan_max_abs(cab%line%q - q0) <= 0.0_wp .AND. nan_max_abs(cab%line%tw_beta - b0) <= 0.0_wp & + .AND. nan_max_abs(RESHAPE(cab%line%tw_u - u0, [3*NN])) <= 0.0_wp, & + 'F: the refused write leaves the line and its twist reference unchanged') + END DO + CALL CD_HFMF_End(cab) + END SUBROUTINE check_fold_refused + ! ------------------------------------------------------------------------------------------ SUBROUTINE check_waves() !! W: a roll A sin(Omega t) of the coupled end (amplitude ramped in over the first eight From ed43f45d09b879f8278f8de5dd19526a5821b9b4 Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Fri, 20 Nov 2026 15:29:50 +0900 Subject: [PATCH 4/4] fix: the twist shortcut keeps the last step's shape through reloads and drops it with the lengths The reference-twist shortcut of tw_advance_reference keeps an element's reference twist only at the configuration tw_rq, at which every element passed every check. Two problems with how tw_rq was kept: - It was re-recorded at every reference commit, a frame-only transfer's included, and dropped by every mirror reload (tw_unpack). In the OpenFAST shell the output probe moves the coupled end, commits the reference at that pose and is then reloaded from the mirror. So the shortcut was off for the probe of the following call, which cost the speed-up this change is for. tw_rq is now recorded only by the commit of a step (tw_commit_reference without frames_only). A frame-only transfer moves no element DOF but the end nodes', and keeps it. A reload keeps it as well: tw_rq is a configuration this model evaluated, and the shortcut still requires the element's unit node tangents to be those of the present reference, so a reloaded reference is used only where it matches. - A change of the unstretched lengths (active line control, rebuild_length_caches) did not drop it. The element shape depends on its reference length: a 0.5 m element with unit tangent handles folds at 2 m with the same DOFs, so its checks no longer hold. tw_rq_ok is now cleared there. Gate F of test_nodal_twist_dynamic gains the length case: a step records the configuration, a length change drops it, and the next step records it again. Results stay bit-identical. Signed-off-by: Jae Hoon Seo --- src/CableDyn_HermiteCableDynamic.f90 | 44 +++++++++++++++++----------- tests/test_nodal_twist_dynamic.f90 | 14 +++++++-- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/CableDyn_HermiteCableDynamic.f90 b/src/CableDyn_HermiteCableDynamic.f90 index ac51f3cf..efe473f6 100644 --- a/src/CableDyn_HermiteCableDynamic.f90 +++ b/src/CableDyn_HermiteCableDynamic.f90 @@ -640,10 +640,11 @@ MODULE CableDyn_HermiteCableDynamic ! the step's residual norm. REAL(wp) :: tw_tsum = CD_ZERO, tw_spsi(2) = CD_ZERO, tw_tscale = CD_ONE REAL(wp), ALLOCATABLE :: tw_su(:, :), tw_stn(:, :), tw_sbeta(:) - ! tw_sq: the configuration tw_advance_reference re-based at; tw_rq: that of the committed - ! reference (tw_commit_reference), valid while tw_rq_ok (a reference that comes from a - ! mirror, a snapshot or tw_init has none). An element whose DOFs are tw_rq's, bit for bit, - ! passed every check of twist_reference there and keeps its reference twist. + ! tw_sq: the configuration tw_advance_reference re-based at; tw_rq: the configuration of the + ! last committed step (tw_commit_reference after a step, not a frame-only transfer), at which + ! every element passed every check of twist_reference; valid while tw_rq_ok (none before the + ! first step, after tw_init or a change of the lengths). An element whose DOFs are tw_rq's + ! and whose unit node tangents are the reference's, bit for bit, keeps its reference twist. REAL(wp), ALLOCATABLE :: tw_sq(:), tw_rq(:) LOGICAL :: tw_rq_ok = .FALSE. ! tw_som(:, k): the angular velocity of end frame k over the running step, log(R_{n+1} @@ -3341,6 +3342,8 @@ SUBROUTINE rebuild_length_caches(model, l0_new, ErrStat, ErrMsg) cn_new = kn_new*model%seabed_cn_over_kn END IF model%l0 = l0_new + ! (the element checks of the nodal twist's recorded configuration hold for the old lengths) + model%tw_rq_ok = .FALSE. CALL MOVE_ALLOC(mgb_new, model%Mgb) CALL MOVE_ALLOC(trib_new, model%trib) IF (model%has_contact) THEN @@ -5160,7 +5163,7 @@ SUBROUTINE CD_HermiteCable_Dyn_Set_Torsion_Frames(model, ends, ErrStat, ErrMsg, IF (PRESENT(q_check)) RETURN ! a frame turn within one step of the end-frame constants (the wrapped change is unique) model%tors_drive_set = .FALSE. - CALL tw_commit_reference(model) + CALL tw_commit_reference(model, frames_only=.TRUE.) model%torsion%ends = ends model%tors_use_step = .FALSE. CALL assemble_twist(model, model%q, model%v, .TRUE., .FALSE., model%ws_R, model%ws_Kb, model%ws_Kvb, & @@ -10906,9 +10909,8 @@ PURE SUBROUTINE tw_unpack(model, buf) model%tw_psi = buf(9*nn + 2*ne + 1:9*nn + 2*ne + 2) model%tw_om = RESHAPE(buf(9*nn + 2*ne + 3:9*nn + 2*ne + 8), [3, 2]) model%tw_oal = RESHAPE(buf(9*nn + 2*ne + 9:9*nn + 2*ne + 14), [3, 2]) - ! (a reloaded reference carries no configuration of its own: tw_advance_reference evaluates - ! every element until the next commit) - model%tw_rq_ok = .FALSE. + ! (tw_rq stays: it is a configuration this model evaluated, and a reloaded reference is used + ! there only where its tangents are those of tw_rq, see tw_advance_reference) END SUBROUTINE tw_unpack ELEMENTAL REAL(wp) FUNCTION tw_wrap(x) RESULT(y) @@ -11698,12 +11700,12 @@ SUBROUTINE tw_advance_reference(model, q_new, ends_new, ErrStat, ErrMsg) !! state reproduces the converged iterate bit for bit), the unit tangents and the directors !! carried over by the smallest rotation (a rigid end's: its end frame's normal carried). !! - !! An interior element (not at a rigid end) whose 12 DOFs are those the committed reference - !! was formed at (tw_rq), bit for bit, keeps its reference twist: its unit node tangents are - !! the reference's, so its transport solid angles are those of the reference terms they are - !! differenced against and twist_reference returns tw_beta(e) + tw_wrap(0) + 0, which is + !! An interior element (not at a rigid end) whose 12 DOFs are those of the last committed step + !! (tw_rq) and whose unit node tangents are those of the reference, bit for bit, keeps its + !! reference twist: its transport solid angles are those of the reference terms they are + !! differenced against, so twist_reference returns tw_beta(e) + tw_wrap(0) + 0, which is !! formed here without the evaluation; and it passed every check of twist_reference (the - !! element's fold and handle checks) at that same shape. A frame-only transfer between steps + !! element's fold and handle checks) at that very shape. A frame-only transfer between steps !! (CD_HermiteCable_Dyn_Set_Torsion_Frames at the committed shape, where a host writes only !! the end nodes) so evaluates only the elements at a rigid end or at a node the host moved; !! the commit of a step evaluates every element that moved. @@ -11795,14 +11797,22 @@ SUBROUTINE tw_frame_rates(model, dt) END DO END SUBROUTINE tw_frame_rates - SUBROUTINE tw_commit_reference(model) - !! Make the reference state of tw_advance_reference the committed one. + SUBROUTINE tw_commit_reference(model, frames_only) + !! Make the reference state of tw_advance_reference the committed one. After a step it also + !! records the step's configuration as tw_rq; a frame-only transfer (frames_only) keeps the + !! tw_rq of the last step, as it moves no element DOF but the end nodes'. TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model + LOGICAL, INTENT(IN), OPTIONAL :: frames_only + LOGICAL :: record + record = .TRUE. + IF (PRESENT(frames_only)) record = .NOT. frames_only model%tw_beta = model%tw_sbeta model%tw_psi = model%tw_spsi model%tw_tn = model%tw_stn - model%tw_rq = model%tw_sq - model%tw_rq_ok = .TRUE. + IF (record) THEN + model%tw_rq = model%tw_sq + model%tw_rq_ok = .TRUE. + END IF model%tw_u = model%tw_su END SUBROUTINE tw_commit_reference diff --git a/tests/test_nodal_twist_dynamic.f90 b/tests/test_nodal_twist_dynamic.f90 index 8d97d890..c447791e 100644 --- a/tests/test_nodal_twist_dynamic.f90 +++ b/tests/test_nodal_twist_dynamic.f90 @@ -32,7 +32,8 @@ PROGRAM test_nodal_twist_dynamic !! F a coupled write that folds the end element (the coupled end, elastic in torsion and !! pinned in bending, translated past its neighbour with its tangent unchanged) is refused !! before anything is written, after a march and after a frame-only transfer (whose - !! re-basing keeps the elements at the committed shape). + !! re-basing keeps the elements at the committed shape); a length change drops the + !! configuration the shortcut of the re-basing keeps elements at, and the next step records it. USE CableDyn_Precision, ONLY: wp USE CableDyn_EndConnection, ONLY: CD_ENDCONN_RIGID USE CableDyn_HermiteTorsion, ONLY: CD_HermiteTorsionType, CD_HermiteTorsion_Line, CD_HermiteTorsion_Unwrap, & @@ -41,7 +42,8 @@ PROGRAM test_nodal_twist_dynamic CD_HermiteCable_Dyn_Step, CD_HermiteCable_Dyn_Energy, & CD_HermiteCable_Dyn_End, CD_HermiteCable_Dyn_Set_EndConnection, & CD_HermiteCable_Dyn_Set_Torsion, CD_HermiteCable_Dyn_Torsion_State, & - CD_HermiteCable_Dyn_Node_Torque, CD_HCDYN_OK + CD_HermiteCable_Dyn_Node_Torque, CD_HermiteCable_Dyn_Set_SegmentLength, & + CD_HCDYN_OK USE CableDyn_OpenFAST_HermiteFMF, ONLY: CD_HFMF_ModuleType, CD_HFMF_Init, CD_HFMF_Set_EndConnection, & CD_HFMF_Set_Torsion, CD_HFMF_UpdateStates, CD_HFMF_CalcOutput, & CD_HFMF_Snapshot, CD_HFMF_Restore, CD_HFMF_MirrorSize, & @@ -313,6 +315,14 @@ SUBROUTINE check_fold_refused() .AND. nan_max_abs(RESHAPE(cab%line%tw_u - u0, [3*NN])) <= 0.0_wp, & 'F: the refused write leaves the line and its twist reference unchanged') END DO + ! a length change: the element shapes are no longer those the checks passed at, so the + ! recorded configuration is dropped (every element is evaluated) until the next step + CALL require(cab%line%tw_rq_ok, 'F: a committed step records its configuration') + CALL CD_HermiteCable_Dyn_Set_SegmentLength(cab%line, [NE/2], [0.55_wp], [0.0_wp], es, em) + CALL require(es == CD_HCDYN_OK .AND. .NOT. cab%line%tw_rq_ok, & + 'F: a length change drops the recorded configuration: '//TRIM(em)) + CALL march(cab, 20, 21, ok) + CALL require(ok .AND. cab%line%tw_rq_ok, 'F: the next step records it again') CALL CD_HFMF_End(cab) END SUBROUTINE check_fold_refused