diff --git a/CHANGELOG.md b/CHANGELOG.md index a65c5ca6..cb6f2dfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,10 @@ All notable changes to CableDyn are recorded in this file. The format follows the committed kinematics before transferring the current ones; it discarded them. With a 248-element power cable in the IEA-15 VolturnUS-S mooring this cuts `CalcOutput` from 41 s to 25 s and the 600 s run from 227 s to 212 s. Results do not change. +- The reaction of a finite-EI cable at a node (the fairlead load the OpenFAST module returns) + is assembled from the one or two elements that reach the node instead of the whole line, + bit-identical to the full assembly (lines with torsion keep the full assembly). In the same + run this cuts `CalcOutput` to 9 s and the run to 194 s. Results do not change. ## [0.2.0] - 2026-10-20 diff --git a/CMakeLists.txt b/CMakeLists.txt index ac225bca..88408f78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1084,6 +1084,17 @@ set_tests_properties(openfast_touchdown PROPERTIES # Other BLAS implementations ignore this environment variable. ENVIRONMENT "OPENBLAS_CORETYPE=Sandybridge") +# The reaction of a finite-EI cable assembles only the rows of its node: bit-identical to the +# full assembly at every node, with every load section, on hand-built cables and along a +# coupled trajectory of the same deck, and without touching the model's scratch. +add_executable(test_hermite_node_reaction tests/test_hermite_node_reaction.f90) +target_link_libraries(test_hermite_node_reaction PRIVATE cabledyn_core) +add_test(NAME hermite_node_reaction + COMMAND test_hermite_node_reaction ${CMAKE_SOURCE_DIR}/examples/iea15mw_umaine_mixed_cabledyn.dat) +set_tests_properties(hermite_node_reaction PROPERTIES + LABELS "fortran;unit;dynamic;openfast;contact" + ENVIRONMENT "OPENBLAS_CORETYPE=Sandybridge") + # The same deck initialises without a static compression/mesh note: the touchdown # contact kink is element-local, the smoothed element-mean axial force is tensile. add_executable(test_flagship_compression_note tests/test_flagship_compression_note.f90) diff --git a/src/CableDyn_HermiteCableDynamic.f90 b/src/CableDyn_HermiteCableDynamic.f90 index 4b7cf7b4..d239da1f 100644 --- a/src/CableDyn_HermiteCableDynamic.f90 +++ b/src/CableDyn_HermiteCableDynamic.f90 @@ -1110,7 +1110,8 @@ SUBROUTINE CD_HermiteCable_Attachment_Drag(rel_velocity, tangent, rho, cda, cdax END IF END SUBROUTINE CD_HermiteCable_Attachment_Drag - SUBROUTINE assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent, R, Kb, Kvb, ErrStat, ErrMsg) + SUBROUTINE assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent, R, Kb, Kvb, n_lo, n_hi, & + ErrStat, ErrMsg) !! Attachment loads into R = f_int - f_ext, with the velocity tangent dR/dv and the !! drag's dependence on the nodal line tangent in dR/dq. The held fluid field is !! constant over the step. @@ -1118,6 +1119,7 @@ SUBROUTINE assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent REAL(wp), INTENT(IN) :: q_cfg(:), v_cfg(:) LOGICAL, INTENT(IN) :: want_residual, want_tangent REAL(wp), INTENT(INOUT) :: R(:), Kb(:, :), Kvb(:, :) + INTEGER, INTENT(IN) :: n_lo, n_hi ! only the attachments at nodes n_lo..n_hi INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg INTEGER :: k, base, a, b @@ -1125,6 +1127,7 @@ SUBROUTINE assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent REAL(wp) :: rk(3), jrel(3, 3), jtan(3, 3) ErrStat = CD_HCDYN_OK; ErrMsg = '' DO k = 1, SIZE(model%att_node) + IF (model%att_node(k) < n_lo .OR. model%att_node(k) > n_hi) CYCLE base = 6*(model%att_node(k) - 1) CALL attachment_residual(model, k, q_cfg, v_cfg, want_tangent, rk, has_jac, jrel, jtan, ErrStat, ErrMsg) IF (ErrStat /= CD_HCDYN_OK) RETURN @@ -5118,7 +5121,7 @@ SUBROUTINE CD_HermiteCable_Dyn_Curvature_Exact(model, curv_out, ErrStat, ErrMsg) END DO END SUBROUTINE CD_HermiteCable_Dyn_Curvature_Exact - SUBROUTINE CD_HermiteCable_Dyn_Reaction(model, node, freac, ErrStat, ErrMsg) + SUBROUTINE CD_HermiteCable_Dyn_Reaction(model, node, freac, ErrStat, ErrMsg, full_assembly) !! Constraint reaction at a fixed/prescribed node: the force the LINE exerts ON its !! support (fairlead/anchor) at the current committed state, in global coordinates. !! @@ -5139,15 +5142,30 @@ SUBROUTINE CD_HermiteCable_Dyn_Reaction(model, node, freac, ErrStat, ErrMsg) REAL(wp), INTENT(OUT) :: freac(3) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + ! .TRUE.: evaluate from the full assembly, the reference the node's rows reproduce bit for bit + LOGICAL, INTENT(IN), OPTIONAL :: full_assembly INTEGER :: es, gi + LOGICAL :: full CHARACTER(1024) :: em2 ErrStat = CD_HCDYN_OK; ErrMsg = ''; freac = CD_ZERO + full = .FALSE. + IF (PRESENT(full_assembly)) full = full_assembly IF (.NOT. model%initialized) THEN ErrStat = CD_HCDYN_BADINPUT; ErrMsg = 'CD_HermiteCable_Dyn_Reaction: model not initialised'; RETURN END IF IF (node < 1 .OR. node > model%nn) THEN ErrStat = CD_HCDYN_BADINPUT; ErrMsg = 'CD_HermiteCable_Dyn_Reaction: node out of range'; RETURN END IF + ! Only the node's rows are needed: assemble them from the elements that reach the node + ! (bit-identical to the full assembly below, which torsion still needs). + IF (.NOT. (model%torsion%active .OR. full)) THEN + CALL node_reaction(model, node, freac, es, em2) + IF (es /= CD_HCDYN_OK) THEN + freac = CD_ZERO + ErrStat = es; ErrMsg = 'CD_HermiteCable_Dyn_Reaction: '//TRIM(em2) + END IF + RETURN + END IF ! Residual-only assembly at the committed state: no tangent buffer is touched, so the ! committed K/Kv (which callers may still hold between steps) are preserved. CALL assemble_residual(model, model%q, model%v, model%t, .TRUE., .FALSE., es, em2) @@ -5168,6 +5186,65 @@ SUBROUTINE CD_HermiteCable_Dyn_Reaction(model, node, freac, ErrStat, ErrMsg) freac = -(model%ws_inert(gi + 1:gi + 3) + model%ws_R(gi + 1:gi + 3)) END SUBROUTINE CD_HermiteCable_Dyn_Reaction + SUBROUTINE node_reaction(model, node, freac, ErrStat, ErrMsg) + !! CD_HermiteCable_Dyn_Reaction from the node's rows only: the residual of the one or two + !! elements that reach the node and of its nodal terms (assemble_residual node_only), and + !! the node's rows of the total mass times the acceleration, with the added mass of those + !! elements. Every row term is formed and summed in the order of the full assembly and of + !! band_matvec, so the reaction is bit-identical, at the cost of one or two elements + !! instead of the line, and the model's scratch is not touched. Not for a line with + !! torsion, which couples every element. + TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model + INTEGER, INTENT(IN) :: node + REAL(wp), INTENT(OUT) :: freac(3) + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + REAL(wp) :: Rn(SIZE(model%ws_R)), Me(12, 12), mam(3, -KL_D:KU_D), inert(3), m + INTEGER :: e, a, b, k, gi, gj, j, n, es + CHARACTER(1024) :: em + + ErrStat = CD_HCDYN_OK; ErrMsg = ''; freac = CD_ZERO + CALL assemble_residual(model, model%q, model%v, model%t, .TRUE., .FALSE., es, em, node_only=node, R_out=Rn) + IF (es /= CD_HCDYN_OK) THEN + ErrStat = es; ErrMsg = TRIM(em); RETURN + END IF + gi = 6*(node - 1) + n = SIZE(model%a) + ! the node's rows of the added mass, mam(k, j - (gi + k)) = Mamb(gi + k, j), summed element + ! by element from zero as assemble_added_mass scatters them + mam = CD_ZERO + IF (model%has_am) THEN + DO e = MAX(1, node - 1), MIN(model%ne, node) + CALL added_mass_element(model, model%q, e, Me, es, em) + IF (es /= CD_HCDYN_OK) THEN + ErrStat = es; ErrMsg = TRIM(em); RETURN + END IF + DO a = 1, 12 + k = elem_gdof(e, a) - gi + IF (k < 1 .OR. k > 3) CYCLE + DO b = 1, 12 + gj = elem_gdof(e, b) + mam(k, gj - (gi + k)) = mam(k, gj - (gi + k)) + Me(a, b) + END DO + END DO + END DO + IF (.NOT. CD_All_Finite(mam)) THEN + ErrStat = CD_HCDYN_NOCONVERGE; ErrMsg = 'assemble_added_mass: non-finite global added-mass matrix'; RETURN + END IF + END IF + ! (Mgb + Mamb) a on the node's rows, column by column as band_matvec accumulates them + inert = CD_ZERO + DO j = MAX(1, gi + 1 - KL_D), MIN(n, gi + 3 + KU_D) + DO k = 1, 3 + IF (j < gi + k - KL_D .OR. j > gi + k + KU_D) CYCLE + m = model%Mgb(KL_D + KU_D + 1 + gi + k - j, j) + IF (model%has_am) m = m + mam(k, j - (gi + k)) + inert(k) = inert(k) + m*model%a(j) + END DO + END DO + freac = -(inert + Rn(gi + 1:gi + 3)) + END SUBROUTINE node_reaction + SUBROUTINE CD_HermiteCable_Dyn_End_Force(model, node, force, ErrStat, ErrMsg) !! Force the line exerts on the point holding its end node (node = 1 or nn) at the !! committed state (q, v): the end element's internal force (elastic plus Kelvin-Voigt @@ -6456,7 +6533,7 @@ END SUBROUTINE CD_HermiteCable_Dyn_End ! ------------------------------------------------------------------------------------------ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_tangent, ErrStat, ErrMsg, & - endconn_direction_eval) + endconn_direction_eval, node_only, R_out) !! Residual R = f_int(q) - f_ext(q, v, t), banded position tangent Kb = dR/dq, and banded !! velocity tangent Kvb = dR/dv at configuration q_cfg with nodal velocity v_cfg at time !! t_eval (LAPACK band layout, see the module note), written into the MODEL'S OWN @@ -6481,8 +6558,16 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg REAL(wp), INTENT(IN), OPTIONAL :: endconn_direction_eval(3, 2) + ! node_only: assemble only the residual rows of that node, into R_out (sized like the + ! residual) instead of the model's residual: the elements that reach the node and its own + ! nodal terms, each added in the order of the full assembly, so those rows are bit-identical + ! and the model's scratch is untouched (node_reaction). Residual only, without torsion + ! (which couples every element). The other rows of R_out are partial. + INTEGER, INTENT(IN), OPTIONAL :: node_only + REAL(wp), INTENT(INOUT), TARGET, OPTIONAL :: R_out(:) REAL(wp), POINTER :: R(:), Kb(:, :), Kvb(:, :) INTEGER :: ne, e, a, gi, es, omp_threads + INTEGER :: e_lo, e_hi, n_lo, n_hi ! the elements and nodes assembled (all unless node_only) INTEGER(INT64) :: asm_t ! clock at the last section mark (asm_mark; profiled runs only) LOGICAL :: tangent_only REAL(wp) :: qe(12), ve(12), Ee, fe(12), Ke(12, 12), pen @@ -6510,6 +6595,25 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta R => model%ws_R Kb => model%ws_Kb Kvb => model%ws_Kvb + e_lo = 1; e_hi = ne; n_lo = 1; n_hi = model%nn + IF (PRESENT(node_only)) THEN + IF (want_tangent .OR. model%torsion%active .OR. .NOT. PRESENT(R_out)) THEN + ErrStat = CD_HCDYN_BADINPUT + ErrMsg = 'a single-node assembly is residual-only, without torsion, into a caller residual' + RETURN + END IF + IF (node_only < 1 .OR. node_only > model%nn) THEN + ErrStat = CD_HCDYN_BADINPUT; ErrMsg = 'single-node assembly: node out of range'; RETURN + END IF + e_lo = MAX(1, node_only - 1); e_hi = MIN(ne, node_only) + n_lo = node_only; n_hi = node_only + END IF + IF (PRESENT(R_out)) THEN + IF (SIZE(R_out) /= SIZE(model%ws_R)) THEN + ErrStat = CD_HCDYN_BADINPUT; ErrMsg = 'assemble_residual: R_out must be sized like the residual'; RETURN + END IF + R => R_out + END IF endconn_d0_eval = model%endconn_d0 IF (PRESENT(endconn_direction_eval)) endconn_d0_eval = endconn_direction_eval asm_t = 0_INT64 @@ -6545,7 +6649,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta END DO CALL asm_mark(HC_ASM_SCATTER, asm_t) ELSE - DO e = 1, ne + DO e = e_lo, e_hi qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3) qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) qe(7:9) = q_cfg(6*e + 1:6*e + 3) @@ -6576,7 +6680,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta IF (want_residual) THEN ! consistent cubic-Hermite self-weight on z DOFs (R = fint - fext => -fext adds +w) - DO e = 1, ne + DO e = e_lo, e_hi R(6*(e - 1) + 3) = R(6*(e - 1) + 3) + model%w(e)*0.5_wp*model%l0(e) R(6*(e - 1) + 6) = R(6*(e - 1) + 6) + model%w(e)*model%l0(e)*model%l0(e)/12.0_wp R(6*e + 3) = R(6*e + 3) + model%w(e)*0.5_wp*model%l0(e) @@ -6594,7 +6698,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta END IF CALL asm_mark(HC_ASM_WEIGHT, asm_t) IF (model%has_attach) THEN - CALL assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent, R, Kb, Kvb, es, em2) + CALL assemble_attachments(model, q_cfg, v_cfg, want_residual, want_tangent, R, Kb, Kvb, n_lo, n_hi, es, em2) IF (es /= CD_HCDYN_OK) THEN ErrStat = CD_HCDYN_NOCONVERGE; ErrMsg = TRIM(em2); RETURN END IF @@ -6608,7 +6712,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! drag configuration the added-mass configuration defines the surface and section, so ! the buoyancy is restored whenever either hydrodynamic configuration is present. IF (model%has_drag .OR. model%has_am) THEN - DO e = 1, ne + DO e = e_lo, e_hi IF (model%has_drag) THEN dry_wl = model%waterline_z dry_b = model%rho_w*model%gravity*0.25_wp*PI_L*model%diam(e)**2 @@ -6640,12 +6744,12 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! law as the deck compatibility path, including structured-bed slope terms. ! Retain the scalar flat penalty for existing direct API callers. IF (model%has_contact) THEN - CALL assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, es, em2) + CALL assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, R, n_lo, n_hi, es, em2) IF (es /= CD_HCDYN_OK) THEN ErrStat = es; ErrMsg = 'contact: '//TRIM(em2); RETURN END IF ELSE IF (model%kn > CD_ZERO) THEN - DO a = 1, model%nn + DO a = n_lo, n_hi gi = 6*(a - 1) + 3 pen = model%seabed_z - q_cfg(gi) IF (pen > CD_ZERO) THEN @@ -6699,7 +6803,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! Axial Kelvin-Voigt damping is an internal constitutive force. It uses the same ! reference-arc Hermite interpolation and axial quadrature as the elastic resultant. IF (model%has_axial_damping .AND. .NOT. tangent_only) THEN - DO e = 1, ne + DO e = e_lo, e_hi qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) ve(1:3) = v_cfg(6*(e - 1) + 1:6*(e - 1) + 3); ve(4:6) = v_cfg(6*(e - 1) + 4:6*(e - 1) + 6) @@ -6745,7 +6849,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! Jacobians djq = d(fdrag)/dq, djv = d(fdrag)/dv. R = f_int - f_ext, so R -= fdrag, K -= djq, ! Kv -= djv over all 12 element DOFs. IF (model%has_drag .AND. .NOT. tangent_only) THEN - DO e = 1, ne + DO e = e_lo, e_hi qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) ve(1:3) = v_cfg(6*(e - 1) + 1:6*(e - 1) + 3); ve(4:6) = v_cfg(6*(e - 1) + 4:6*(e - 1) + 6) @@ -6829,19 +6933,23 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! spring is a potential, so it enters the residual with the same sign as the internal ! force, and f . d == 0 keeps it off the stretch component by construction. IF (model%has_endconn) THEN - CALL add_endconn_band(R, Kb, q_cfg, 1, model%endconn_k(1), endconn_d0_eval(:, 1), & - want_residual, want_tangent, es, em2) - IF (es /= CD_ENDCONN_OK) THEN - ErrStat = CD_HCDYN_NOCONVERGE - ErrMsg = 'end connection at node 1: '//TRIM(em2) - RETURN + IF (n_lo == 1) THEN + CALL add_endconn_band(R, Kb, q_cfg, 1, model%endconn_k(1), endconn_d0_eval(:, 1), & + want_residual, want_tangent, es, em2) + IF (es /= CD_ENDCONN_OK) THEN + ErrStat = CD_HCDYN_NOCONVERGE + ErrMsg = 'end connection at node 1: '//TRIM(em2) + RETURN + END IF END IF - CALL add_endconn_band(R, Kb, q_cfg, ne + 1, model%endconn_k(2), endconn_d0_eval(:, 2), & - want_residual, want_tangent, es, em2) - IF (es /= CD_ENDCONN_OK) THEN - ErrStat = CD_HCDYN_NOCONVERGE - ErrMsg = 'end connection at final node: '//TRIM(em2) - RETURN + IF (n_hi == ne + 1) THEN + CALL add_endconn_band(R, Kb, q_cfg, ne + 1, model%endconn_k(2), endconn_d0_eval(:, 2), & + want_residual, want_tangent, es, em2) + IF (es /= CD_ENDCONN_OK) THEN + ErrStat = CD_HCDYN_NOCONVERGE + ErrMsg = 'end connection at final node: '//TRIM(em2) + RETURN + END IF END IF END IF @@ -6850,7 +6958,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! configuration's coefficients. An external load: R -= ffk, K -= fjq (geometric tangent chain; ! the wave-field spatial gradient is neglected in K -- see the element note). IF (model%has_wave .AND. model%has_am .AND. .NOT. tangent_only) THEN - DO e = 1, ne + DO e = e_lo, e_hi qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) IF (model%wv_ncomp > 0) THEN @@ -6904,7 +7012,7 @@ SUBROUTINE assemble_residual(model, q_cfg, v_cfg, t_eval, want_residual, want_ta ! (per-element two-node average, frozen over the step) with the added-mass ! configuration's coefficients -- the held-mode twin of the wave FK block above. IF (model%has_held_field .AND. model%has_am .AND. .NOT. tangent_only) THEN - DO e = 1, ne + DO e = e_lo, e_hi qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) IF (want_tangent) THEN @@ -7043,12 +7151,15 @@ SUBROUTINE add_endconn_band(R, Kb, q_cfg, node, k_rot, d0, want_residual, want_t END IF END SUBROUTINE add_endconn_band - SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, ErrStat, ErrMsg) - !! Assemble nodal contact directly into the model-owned residual bands. The - !! Jacobians are d(f_int-f_ext)/dq and d(f_int-f_ext)/dv. + SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, R, n_lo, n_hi, ErrStat, ErrMsg) + !! Assemble the nodal contact of nodes n_lo..n_hi into the residual R (assemble_residual's; + !! the model's own residual is touched only through R) and the model-owned tangent bands. + !! The Jacobians are d(f_int-f_ext)/dq and d(f_int-f_ext)/dv. TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model REAL(wp), INTENT(IN) :: q_cfg(:), v_cfg(:) LOGICAL, INTENT(IN) :: want_residual, want_tangent + REAL(wp), INTENT(INOUT) :: R(:) + INTEGER, INTENT(IN) :: n_lo, n_hi INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg @@ -7070,7 +7181,7 @@ SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, Er .NOT. ALLOCATED(model%contact_cn)) THEN ErrStat = CD_HCDYN_BADINPUT; ErrMsg = 'contact configuration is incomplete'; RETURN END IF - DO i = 1, model%nn + DO i = n_lo, n_hi ix = 6*i - 5; iy = ix + 1; iz = ix + 2 gx = CD_ZERO; gy = CD_ZERO IF (model%has_contact_bathymetry) THEN @@ -7116,7 +7227,7 @@ SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, Er dnormal_dq = -dnormal_dgap*nvec dnormal_dv = dnormal_dvz*nvec - IF (want_residual) model%ws_R(ix:iz) = model%ws_R(ix:iz) - normal*nvec + IF (want_residual) R(ix:iz) = R(ix:iz) - normal*nvec IF (want_tangent) THEN DO jc = 1, 3 DO jr = 1, 3 @@ -7126,7 +7237,7 @@ SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, Er END DO END IF IF (creased) THEN - IF (want_residual) model%ws_R(ix:iz) = model%ws_R(ix:iz) - fcrease + IF (want_residual) R(ix:iz) = R(ix:iz) - fcrease IF (want_tangent) THEN DO jc = 1, 3 DO jr = 1, 3 @@ -7152,7 +7263,7 @@ SUBROUTINE assemble_contact(model, q_cfg, v_cfg, want_residual, want_tangent, Er model%contact_mu*normal, ffr, dfr_dd, dfr_dc) mu_c = model%contact_mu END IF - IF (want_residual) model%ws_R(ix:iy) = model%ws_R(ix:iy) + ffr + IF (want_residual) R(ix:iy) = R(ix:iy) + ffr IF (want_tangent) THEN DO jr = 1, 2 CALL scatter_contact_band(model%ws_Kb, ix + jr - 1, ix, dfr_dd(jr, 1) + & @@ -8142,22 +8253,13 @@ SUBROUTINE assemble_added_mass(model, q_cfg, ErrStat, ErrMsg) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg INTEGER :: e, es - REAL(wp) :: qe(12), Me(12, 12), wl_e + REAL(wp) :: Me(12, 12) CHARACTER(1024) :: em2 ErrStat = CD_HCDYN_OK; ErrMsg = '' model%ws_Mamb = CD_ZERO IF (.NOT. model%has_am) RETURN DO e = 1, model%ne - qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) - qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) - ! Held-field mode: the inertia matrix must wet against the SAME free surface the - ! drag and Froude-Krylov use (the host-sampled elevation, node-averaged per - ! element) -- against the init-time still-water am_wl a surface-piercing element - ! would carry the wrong added mass exactly when the host drives waves. - wl_e = model%am_wl - IF (model%has_held_field) wl_e = 0.5_wp*(model%hf_wl(e) + model%hf_wl(e + 1)) - CALL CD_HermiteCable_AddedMass_Element(qe, model%l0(e), wl_e, model%am_rho, & - model%am_diam(e), model%am_can(e), model%am_cat(e), Me, es, em2) + CALL added_mass_element(model, q_cfg, e, Me, es, em2) IF (es /= CD_HCDYN_OK) THEN ErrStat = es; ErrMsg = TRIM(em2); RETURN END IF @@ -8172,6 +8274,28 @@ SUBROUTINE assemble_added_mass(model, q_cfg, ErrStat, ErrMsg) END IF END SUBROUTINE assemble_added_mass + SUBROUTINE added_mass_element(model, q_cfg, e, Me, ErrStat, ErrMsg) + !! Consistent added-mass matrix of element e at configuration q_cfg (assemble_added_mass, + !! node_reaction). + TYPE(CD_HermiteCableDynType), INTENT(IN) :: model + REAL(wp), INTENT(IN) :: q_cfg(:) + INTEGER, INTENT(IN) :: e + REAL(wp), INTENT(OUT) :: Me(12, 12) + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + REAL(wp) :: qe(12), wl_e + qe(1:3) = q_cfg(6*(e - 1) + 1:6*(e - 1) + 3); qe(4:6) = q_cfg(6*(e - 1) + 4:6*(e - 1) + 6) + qe(7:9) = q_cfg(6*e + 1:6*e + 3); qe(10:12) = q_cfg(6*e + 4:6*e + 6) + ! Held-field mode: the inertia matrix must wet against the SAME free surface the + ! drag and Froude-Krylov use (the host-sampled elevation, node-averaged per + ! element) -- against the init-time still-water am_wl a surface-piercing element + ! would carry the wrong added mass exactly when the host drives waves. + wl_e = model%am_wl + IF (model%has_held_field) wl_e = 0.5_wp*(model%hf_wl(e) + model%hf_wl(e + 1)) + CALL CD_HermiteCable_AddedMass_Element(qe, model%l0(e), wl_e, model%am_rho, & + model%am_diam(e), model%am_can(e), model%am_cat(e), Me, ErrStat, ErrMsg) + END SUBROUTINE added_mass_element + SUBROUTINE build_rigid_bases(model, directions, bases, ErrStat, ErrMsg) TYPE(CD_HermiteCableDynType), INTENT(IN) :: model REAL(wp), INTENT(IN) :: directions(3, 2) diff --git a/tests/test_hermite_node_reaction.f90 b/tests/test_hermite_node_reaction.f90 new file mode 100644 index 00000000..9f4f49e8 --- /dev/null +++ b/tests/test_hermite_node_reaction.f90 @@ -0,0 +1,264 @@ +! File: tests/test_hermite_node_reaction.f90 +! SPDX-License-Identifier: Apache-2.0 +! Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University +PROGRAM test_hermite_node_reaction + !! CD_HermiteCable_Dyn_Reaction assembles only the rows of its node (the one or two elements + !! that reach it and the node's own terms). It must equal the reaction of the full assembly + !! bit for bit at every node and leave the model's residual and mass scratch untouched: + !! A. hand-built cables with every load section a node can receive (structure, weight, + !! dry buoyancy, contact with friction on a controlled seabed, axial damping, drag in a + !! current, added mass and Froude-Krylov loads; Airy waves, or a held fluid field with + !! attachments; finite, rigid or no end connections), at scattered states; + !! B. the maintained IEA-15 MW mixed mooring example through the OpenFAST aggregate, along a + !! coupled surge and heave trajectory in a current, and after a fairlead transfer without + !! a step (the state the OpenFAST CalcOutput evaluates). + USE CableDyn_Precision, ONLY: wp + USE CableDyn_HermiteCableDynamic, ONLY: CD_HermiteCableDynType, CD_HermiteCable_Dyn_Init, & + CD_HermiteCable_Dyn_Reaction, CD_HermiteCable_Dyn_Set_Contact, & + CD_HermiteCable_Dyn_Set_Control_Seabed, & + CD_HermiteCable_Dyn_Set_Attachments, CD_HermiteCable_Dyn_Set_Axial_Damping, & + CD_HermiteCable_Dyn_Set_Drag, CD_HermiteCable_Dyn_Set_AddedMass, & + CD_HermiteCable_Dyn_Set_Waves, CD_HermiteCable_Dyn_Set_Held_Fluid, & + CD_HermiteCable_Dyn_Set_EndConnection, CD_HermiteCable_Dyn_End, & + CD_HCDYN_OK + USE CableDyn_EndConnection, ONLY: CD_ENDCONN_FINITE, CD_ENDCONN_RIGID + USE CableDyn_Line, ONLY: CD_Nodal_Seabed_Stiffness, CD_LINE_OK + USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, & + CD_AGG_NFluidNodes, CD_AGG_GetMovingPointMesh, CD_AGG_SetFluidFields, & + CD_AGG_Step_Moving, CD_AGG_UpdateStates_Moving, CD_AGG_End, CD_AGG_OK + USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: INT64 + IMPLICIT NONE + + INTEGER :: nfail, ncompared + INTEGER(INT64) :: seed + + nfail = 0 + ncompared = 0 + seed = 20261021_INT64 + CALL case_hand_built(waves=.TRUE., mode=CD_ENDCONN_FINITE) + CALL case_hand_built(waves=.FALSE., mode=CD_ENDCONN_FINITE) + CALL case_hand_built(waves=.FALSE., mode=CD_ENDCONN_RIGID) + CALL case_hand_built(waves=.TRUE., mode=-1) + IF (COMMAND_ARGUMENT_COUNT() >= 1) CALL case_mixed_example() + IF (nfail > 0) THEN + WRITE (*, '(A,I0,A)') 'FAIL: ', nfail, ' assertion(s) failed' + ERROR STOP 1 + END IF + WRITE (*, '(A,I0,A)') 'PASS: node reactions equal the full assembly bit for bit (', ncompared, ' nodes)' + +CONTAINS + + SUBROUTINE require(cond, msg) + LOGICAL, INTENT(IN) :: cond + CHARACTER(*), INTENT(IN) :: msg + IF (.NOT. cond) THEN + WRITE (*, '(A)') 'FAIL: '//TRIM(msg) + nfail = nfail + 1 + END IF + END SUBROUTINE require + + REAL(wp) FUNCTION uniform() RESULT(u) + !! Deterministic uniform deviate in [-1, 1) (xorshift64, no integer overflow), the same on + !! every platform. + seed = IEOR(seed, ISHFT(seed, 13)) + seed = IEOR(seed, ISHFT(seed, -7)) + seed = IEOR(seed, ISHFT(seed, 17)) + u = REAL(ISHFT(seed, -11), wp)/REAL(ISHFT(1_INT64, 53), wp) + u = 2.0_wp*u - 1.0_wp + END FUNCTION uniform + + LOGICAL FUNCTION same_bits(a, b) RESULT(same) + REAL(wp), INTENT(IN) :: a(:), b(:) + same = SIZE(a) == SIZE(b) + IF (same) same = ALL(TRANSFER(a, 0_INT64, SIZE(a)) == TRANSFER(b, 0_INT64, SIZE(b))) + END FUNCTION same_bits + + SUBROUTINE compare_nodes(model, label, nodes) + !! At each node: the node reaction equals the full-assembly reaction bit for bit, and it + !! leaves the residual and total-mass scratch of the full one untouched. + TYPE(CD_HermiteCableDynType), INTENT(INOUT) :: model + CHARACTER(*), INTENT(IN) :: label + INTEGER, INTENT(IN) :: nodes(:) + REAL(wp) :: f_full(3), f_node(3) + REAL(wp), ALLOCATABLE :: r_saved(:), m_saved(:) + INTEGER :: k, es1, es2 + CHARACTER(512) :: em + CHARACTER(16) :: tag + DO k = 1, SIZE(nodes) + WRITE (tag, '(I0)') nodes(k) + CALL CD_HermiteCable_Dyn_Reaction(model, nodes(k), f_full, es1, em, full_assembly=.TRUE.) + CALL require(es1 == CD_HCDYN_OK, label//' node '//TRIM(tag)//': full reaction: '//TRIM(em)) + r_saved = model%ws_R + m_saved = RESHAPE(model%ws_Msumb, [SIZE(model%ws_Msumb)]) + CALL CD_HermiteCable_Dyn_Reaction(model, nodes(k), f_node, es2, em) + CALL require(es2 == CD_HCDYN_OK, label//' node '//TRIM(tag)//': node reaction: '//TRIM(em)) + CALL require(same_bits(f_node, f_full), label//' node '//TRIM(tag)//': node reaction equals the full one') + CALL require(same_bits(model%ws_R, r_saved) .AND. & + same_bits(RESHAPE(model%ws_Msumb, [SIZE(model%ws_Msumb)]), m_saved), & + label//' node '//TRIM(tag)//': node reaction leaves the scratch untouched') + IF (.NOT. same_bits(f_node, f_full)) WRITE (*, '(2A,3ES24.16/A,3ES24.16)') ' node ', TRIM(tag), f_node, & + ' full ', f_full + ncompared = ncompared + 1 + END DO + END SUBROUTINE compare_nodes + + SUBROUTINE case_hand_built(waves, mode) + !! A 20-element cable from a fixed anchor on a controlled seabed rising to a fixed top, with + !! every node section, compared at all nodes over scattered positions (some below the bed), + !! velocities and accelerations. Attachments take the held field, so they come with it; + !! mode < 0 leaves the ends without a connection. + LOGICAL, INTENT(IN) :: waves + INTEGER, INTENT(IN) :: mode + INTEGER, PARAMETER :: NE = 20, NN = NE + 1, NDOF = 6*NN + REAL(wp), PARAMETER :: L = 2.0_wp, ZBED = -20.0_wp, KBOT = 1.0e5_wp, CBOT = 1.0e4_wp + TYPE(CD_HermiteCableDynType) :: m + REAL(wp) :: l0(NE), diam(NE), q(NDOF), d0(3, 2), k_rot(2), fv(3, NN), fa(3, NN), wl(NN), s, z + REAL(wp), ALLOCATABLE :: kn(:), cn(:) + INTEGER :: i, k, trial, es + CHARACTER(512) :: em + CHARACTER(32) :: label + + WRITE (label, '(A,L1,A,I0)') 'A waves=', waves, ' mode=', mode + l0 = L + diam = 0.15_wp + q = 0.0_wp + DO i = 1, NN + ! laid on the bed for the first half, then rising at 45 degrees + s = REAL(i - 1, wp)*L + IF (i <= NN/2) THEN + q(6*(i - 1) + 1) = s + q(6*(i - 1) + 3) = ZBED + q(6*(i - 1) + 4) = 1.0_wp + ELSE + q(6*(i - 1) + 1) = REAL(NN/2 - 1, wp)*L + (s - REAL(NN/2 - 1, wp)*L)/SQRT(2.0_wp) + q(6*(i - 1) + 3) = ZBED + (s - REAL(NN/2 - 1, wp)*L)/SQRT(2.0_wp) + q(6*(i - 1) + 4) = 1.0_wp/SQRT(2.0_wp) + q(6*(i - 1) + 6) = 1.0_wp/SQRT(2.0_wp) + END IF + END DO + CALL CD_HermiteCable_Dyn_Init(m, l0, [(1.0e6_wp, k=1, NE)], [(100.0_wp, k=1, NE)], [(5.0_wp, k=1, NE)], & + [(30.0_wp, k=1, NE)], q, [1, 2, 3, NDOF - 5, NDOF - 4, NDOF - 3], ZBED, 0.0_wp, & + 0.5_wp, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': init: '//TRIM(em)) + IF (es /= CD_HCDYN_OK) RETURN + CALL CD_HermiteCable_Dyn_Set_Axial_Damping(m, [(2.0e4_wp, k=1, NE)], es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': damping: '//TRIM(em)) + CALL CD_Nodal_Seabed_Stiffness(KBOT, diam, l0, kn, es, em) + CALL require(es == CD_LINE_OK, TRIM(label)//': seabed stiffness: '//TRIM(em)) + cn = kn*(CBOT/KBOT) + CALL CD_HermiteCable_Dyn_Set_Contact(m, kn, cn, 0.3_wp, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': contact: '//TRIM(em)) + CALL CD_HermiteCable_Dyn_Set_Control_Seabed(m, KBOT, diam, CBOT/KBOT, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': control seabed: '//TRIM(em)) + CALL CD_HermiteCable_Dyn_Set_Drag(m, 1025.0_wp, diam, [(1.2_wp, k=1, NE)], [(0.008_wp, k=1, NE)], 0.0_wp, & + [0.6_wp, 0.2_wp, 0.0_wp], es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': drag: '//TRIM(em)) + CALL CD_HermiteCable_Dyn_Set_AddedMass(m, 1025.0_wp, diam, [(1.0_wp, k=1, NE)], [(0.1_wp, k=1, NE)], 0.0_wp, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': added mass: '//TRIM(em)) + IF (waves) THEN + CALL CD_HermiteCable_Dyn_Set_Waves(m, 3.0_wp, 8.0_wp, 30.0_wp, -ZBED, 9.80665_wp, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': waves: '//TRIM(em)) + ELSE + DO i = 1, NN + fv(:, i) = [0.5_wp, 0.1_wp*REAL(i, wp)/REAL(NN, wp), 0.05_wp] + fa(:, i) = [0.2_wp, -0.1_wp, 0.3_wp] + wl(i) = 0.4_wp*SIN(REAL(i, wp)) + END DO + CALL CD_HermiteCable_Dyn_Set_Held_Fluid(m, fv, fa, wl, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': held fluid: '//TRIM(em)) + ! attachments next to each end and in the span, so both end rows receive one + CALL CD_HermiteCable_Dyn_Set_Attachments(m, [2, 7, NE], [50.0_wp, 80.0_wp, 20.0_wp], & + [0.01_wp, 0.03_wp, 0.02_wp], [0.1_wp, 0.2_wp, 0.0_wp], & + [0.05_wp, 0.1_wp, 0.0_wp], [1.0_wp, 1.0_wp, 0.5_wp], & + 1025.0_wp, 9.80665_wp, es, em) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': attachments: '//TRIM(em)) + END IF + IF (mode >= 0) THEN + d0(:, 1) = [1.0_wp, 0.0_wp, 0.0_wp] + d0(:, 2) = [1.0_wp, 0.0_wp, 1.0_wp]/SQRT(2.0_wp) + IF (mode == CD_ENDCONN_RIGID) THEN + k_rot = 0.0_wp + ELSE + k_rot = [5.0e3_wp, 8.0e3_wp] + END IF + CALL CD_HermiteCable_Dyn_Set_EndConnection(m, k_rot, d0, es, em, connection_mode=[mode, mode]) + CALL require(es == CD_HCDYN_OK, TRIM(label)//': end connections: '//TRIM(em)) + END IF + IF (nfail > 0) RETURN + + CALL compare_nodes(m, TRIM(label)//' rest', [(i, i=1, NN)]) + DO trial = 1, 6 + ! scattered state: positions within +-5 cm (bed nodes partly penetrating), velocities and + ! accelerations of order 1, at a later time + DO i = 1, NDOF + m%q(i) = q(i) + 0.05_wp*uniform() + m%v(i) = uniform() + m%a(i) = 2.0_wp*uniform() + END DO + DO i = 1, NN + z = q(6*(i - 1) + 3) + IF (z <= ZBED) m%q(6*(i - 1) + 3) = ZBED - 0.02_wp*ABS(uniform()) + END DO + m%t = 0.37_wp*REAL(trial, wp) + CALL compare_nodes(m, TRIM(label)//' scattered', [(i, i=1, NN)]) + END DO + CALL CD_HermiteCable_Dyn_End(m) + END SUBROUTINE case_hand_built + + SUBROUTINE case_mixed_example() + !! The mixed mooring example (EI = 0 chains and a grounded finite-EI power cable with contact, + !! friction, drag and added mass, host fluid held per step) along 4 s of coupled surge and + !! heave in a current, at every node of the cable every 0.5 s. + REAL(wp), PARAMETER :: DT = 0.025_wp, DURATION = 4.0_wp, PERIOD = 4.0_wp + TYPE(CD_AGG_ModuleType) :: agg + CHARACTER(1024) :: deck + CHARACTER(512) :: em + REAL(wp), ALLOCATABLE :: p0(:, :), p(:, :), v(:, :), a(:, :), load(:, :), fv(:, :), fa(:, :), wl(:) + REAL(wp) :: t, omega, pi + INTEGER :: es, ncp, nf, s, ni, nn, i, ccol + LOGICAL :: converged, stalled + + CALL GET_COMMAND_ARGUMENT(1, deck) + CALL CD_AGG_Init_From_Deck(agg, TRIM(deck), DT, es, em, external_fluid=.TRUE.) + CALL require(es == CD_AGG_OK, 'B: init: '//TRIM(em)) + IF (es /= CD_AGG_OK) RETURN + CALL require(ALLOCATED(agg%cables), 'B: the example builds a finite-EI cable') + IF (.NOT. ALLOCATED(agg%cables)) RETURN + nn = agg%cables(1)%line%nn + ncp = CD_AGG_NMovingPoints(agg, es, em) + nf = CD_AGG_NFluidNodes(agg, es, em) + ALLOCATE (p0(3, ncp), p(3, ncp), v(3, ncp), a(3, ncp), load(3, ncp), fv(3, nf), fa(3, nf), wl(nf)) + CALL CD_AGG_GetMovingPointMesh(agg, p0, v, a, load, es, em) + CALL require(es == CD_AGG_OK, 'B: initial mesh: '//TRIM(em)) + CALL compare_nodes(agg%cables(1)%line, 'B static', [(i, i=1, nn)]) + fv = 0.0_wp + fv(1, :) = 0.5_wp + fa = 0.0_wp + wl = 0.0_wp + pi = ACOS(-1.0_wp) + omega = 2.0_wp*pi/PERIOD + DO s = 1, NINT(DURATION/DT) + t = REAL(s, wp)*DT + p = p0 + p(1, :) = p0(1, :) + 1.5_wp*SIN(omega*t)*MIN(t/PERIOD, 1.0_wp) + p(3, :) = p0(3, :) + 0.8_wp*(1.0_wp - COS(omega*t))*MIN(t/PERIOD, 1.0_wp) + v = 0.0_wp + a = 0.0_wp + CALL CD_AGG_SetFluidFields(agg, fv, fa, wl, es, em) + IF (es == CD_AGG_OK) CALL CD_AGG_Step_Moving(agg, DT, p, v, a, converged, stalled, ni, es, em, t_committed=t) + CALL require(es == CD_AGG_OK .AND. converged, 'B: step: '//TRIM(em)) + IF (es /= CD_AGG_OK .OR. .NOT. converged) EXIT + IF (MOD(s, 20) == 0) CALL compare_nodes(agg%cables(1)%line, 'B march', [(i, i=1, nn)]) + END DO + ! a fairlead transfer without a step: the end element carries the current kinematics against + ! the committed interior, as in the OpenFAST CalcOutput + ccol = agg%ncp_sys + 1 + p(:, ccol) = p(:, ccol) + [0.3_wp, -0.2_wp, 0.25_wp] + v(:, ccol) = [0.4_wp, 0.1_wp, -0.3_wp] + CALL CD_AGG_UpdateStates_Moving(agg, p, v, a, es, em) + CALL require(es == CD_AGG_OK, 'B: transfer: '//TRIM(em)) + CALL compare_nodes(agg%cables(1)%line, 'B transfer', [1, 2, 3, nn - 2, nn - 1, nn]) + CALL CD_AGG_End(agg, es, em) + END SUBROUTINE case_mixed_example + +END PROGRAM test_hermite_node_reaction