diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d160eb7..d2006685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -139,6 +139,17 @@ All notable changes to CableDyn are recorded in this file. The format follows at 0.109 1/m instead of 0.075 1/m, and the anchor tension stays at its static 1.23 kN, where the coarse mesh showed stick-slip peaks up to 4.1 kN. The validation record and the tutorials are updated. +- Faster tension-only (`EI = 0`) line steps. In every Newton evaluation the element kernels of + the line loads (axial damping, Morison drag, Froude-Krylov, buoyancy recovery, added mass) no + longer re-check, element by element, data the line model validates when it is set; the + Froude-Krylov and added-mass kernels check their section data once instead of three times; + the velocity Jacobians of the Froude-Krylov and buoyancy loads, which are zero, are no longer + added into the band; a step reuses the consistent-mass band of the previous one while the + lengths and masses are unchanged; and the Newton solve no longer rescans a matrix already + checked finite. With the release build, `examples/als_volturnus_line_break_time.dat` runs + 13 % faster and `examples/spar_pinned_rods.dat` 13 %. Every input error is reported as + before. With gfortran the results are bit-identical. With the Intel compiler of the release + they can differ in the last printed digit, and by more where a deck amplifies round-off. ### Fixed @@ -258,6 +269,17 @@ All notable changes to CableDyn are recorded in this file. The format follows on the rounded surface. When the anchor walk of a finite-EI line over a non-planar bathymetry reaches a state that is rejected, the failure no longer states that the line is too long for its span to hang as a catenary, which is proven over a planar seabed only. +- Junction end tangent of a tension-only line step that ends right after a fresh tangent + evaluation. In the monolithic body and point coupling, a line step accepted within the + near-tolerance allowance at its Newton iteration limit (or converged on a freshly evaluated + tangent) still marked its last factorization as current, although the new tangent had + overwritten it. The condensed end tangent, and the warm start of a re-solve of the same step, + were then back-solved with the unfactored matrix: in a unit test the end tangent was wrong by + five orders of magnitude, which can slow or misdirect the outer junction iteration. The step + now clears the factorization flags whenever it overwrites the band without factoring it, so + the end query factors the current tangent (one more tangent assembly and factorization for + such a step of a junction solve). The outputs of the example decks and of the other tests + are unchanged. ## [0.1.1] - 2026-10-09 diff --git a/src/CableDyn_Damping.f90 b/src/CableDyn_Damping.f90 index 3f00918d..03e4bdfb 100644 --- a/src/CableDyn_Damping.f90 +++ b/src/CableDyn_Damping.f90 @@ -252,16 +252,27 @@ SUBROUTINE CD_Cable_Axial_Damping_Load(q, v, elem_conn, l0, ba, force, jac_q, ja END DO END SUBROUTINE CD_Cable_Axial_Damping_Load - SUBROUTINE CD_Axial_Damping_Element_Force(qa, qb, va, vb, l0, ba, force, ErrStat, ErrMsg, l0_dot) + SUBROUTINE CD_Axial_Damping_Element_Force(qa, qb, va, vb, l0, ba, force, ErrStat, ErrMsg, l0_dot, & + inputs_validated) !! Two-node axial damping force kernel for model hot paths. + !! `inputs_validated` is an internal hot-path contract (as in CD_Component_Wave_Kinematics), + !! shared with CD_Axial_Damping_Element_Load: a caller that already guarantees a finite + !! state, a finite positive l0 and a finite resolved ba >= 0 skips the per-element input + !! scan. The default remains full validation for public/direct calls. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, ba REAL(wp), INTENT(OUT) :: force(6) REAL(wp), INTENT(IN), OPTIONAL :: l0_dot !! unstretched-length rate (see the cable-path routines) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated REAL(wp) :: chord(3), tang(3), rel(3), length, ldot, td_vec(3) - CALL validate_element_inputs(qa, qb, va, vb, l0, ba, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_DAMP_OK + ErrMsg = '' + ELSE + CALL validate_element_inputs(qa, qb, va, vb, l0, ba, ErrStat, ErrMsg) + END IF force = CD_ZERO IF (ErrStat /= CD_DAMP_OK) RETURN IF (PRESENT(l0_dot)) THEN @@ -285,18 +296,25 @@ SUBROUTINE CD_Axial_Damping_Element_Force(qa, qb, va, vb, l0, ba, force, ErrStat force(4:6) = -td_vec END SUBROUTINE CD_Axial_Damping_Element_Force - SUBROUTINE CD_Axial_Damping_Element_Load(qa, qb, va, vb, l0, ba, force, jac_q, jac_v, ErrStat, ErrMsg, l0_dot) + SUBROUTINE CD_Axial_Damping_Element_Load(qa, qb, va, vb, l0, ba, force, jac_q, jac_v, ErrStat, ErrMsg, l0_dot, & + inputs_validated) !! Two-node axial damping force and residual-Jacobian kernel. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, ba REAL(wp), INTENT(OUT) :: force(6), jac_q(6, 6), jac_v(6, 6) REAL(wp), INTENT(IN), OPTIONAL :: l0_dot !! unstretched-length rate: the damped rate is ldot - (length/l0)*l0_dot INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Axial_Damping_Element_Force INTEGER :: i REAL(wp) :: chord(3), tang(3), rel(3), length, ldot, k, td_vec(3) REAL(wp) :: eye(3, 3), ktt(3, 3), m1(3, 3), m2(3, 3), dmat(3, 3) - CALL validate_element_inputs(qa, qb, va, vb, l0, ba, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_DAMP_OK + ErrMsg = '' + ELSE + CALL validate_element_inputs(qa, qb, va, vb, l0, ba, ErrStat, ErrMsg) + END IF force = CD_ZERO jac_q = CD_ZERO jac_v = CD_ZERO @@ -360,6 +378,13 @@ PURE FUNCTION outer(a, b) RESULT(m) END DO END FUNCTION outer + PURE LOGICAL FUNCTION trusted(inputs_validated) RESULT(trust) + !! The value of an optional inputs_validated flag (.FALSE. when absent). + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated + trust = .FALSE. + IF (PRESENT(inputs_validated)) trust = inputs_validated + END FUNCTION trusted + SUBROUTINE add_block(mat, node_i, node_j, blk) !! Accumulate a 3x3 block into the (node_i, node_j) DOF positions of a global matrix. REAL(wp), INTENT(INOUT) :: mat(:, :) diff --git a/src/CableDyn_Dynamic.f90 b/src/CableDyn_Dynamic.f90 index 90e03ce4..5509fd86 100644 --- a/src/CableDyn_Dynamic.f90 +++ b/src/CableDyn_Dynamic.f90 @@ -145,6 +145,15 @@ MODULE CableDyn_Dynamic REAL(wp), ALLOCATABLE :: nodes_eval(:, :) INTEGER :: topo_n_nodes = 0, bw_kl = 0, bw_ku = 0 LOGICAL :: topo_valid = .FALSE., bw_valid = .FALSE. + ! M_band holds the element consistent mass band assembled for the remembered connectivity + ! and free list (bw_free) from the lengths mb_l0 and masses per length mb_rho; a step whose + ! inputs match reuses it instead of assembling the same band again. Every other writer of + ! M_band clears mb_valid. + LOGICAL :: mb_valid = .FALSE. + REAL(wp), ALLOCATABLE :: mb_l0(:), mb_rho(:) + ! TEST-ONLY diagnostic: the number of steps that reused the remembered mass band (saturating). + ! Only the unit tests read it (to assert that the cache hit); no result depends on it. + INTEGER :: mb_hits = 0 INTEGER :: ad_kl = 0 INTEGER, ALLOCATABLE :: ad_finv(:), ad_ipiv(:), ad_fixed(:) REAL(wp), ALLOCATABLE :: ad_full(:, :), ad_add(:, :), ad_ab(:, :), ad_x(:, :), ad_result(:, :) @@ -164,7 +173,8 @@ MODULE CableDyn_Dynamic LOGICAL :: end_valid = .FALSE. ! the factored effective band in eff_band/ipiv belongs to the current step (a junction solve ! of this step factored it): a warm re-solve of the same step starts with a chord iteration - ! on it instead of assembling and factoring a new tangent + ! on it instead of assembling and factoring a new tangent. Cleared whenever eff_band is + ! overwritten by a tangent that is not factored. LOGICAL :: step_factor = .FALSE. ! force scale of the junction solves (largest element tension of the last one): the ! 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) work%bw_ku = 0 work%topo_valid = .FALSE. work%bw_valid = .FALSE. + work%mb_valid = .FALSE. + work%mb_hits = 0 + IF (ALLOCATED(work%mb_l0)) DEALLOCATE (work%mb_l0) + IF (ALLOCATED(work%mb_rho)) DEALLOCATE (work%mb_rho) IF (ALLOCATED(work%ad_finv)) DEALLOCATE (work%ad_finv) IF (ALLOCATED(work%ad_ipiv)) DEALLOCATE (work%ad_ipiv) 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 RETURN END IF END IF + ! the band is factored in place below: not the step's reusable mass band + work%mb_valid = .FALSE. IF (use_element_mass_band) THEN CALL assemble_mass_band_free(elem_conn, l0, rho_a, work%dof_marker(1:n_dof), kl, ku, M_band, es, em) ELSE @@ -615,7 +631,7 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on REAL(wp), POINTER :: a_eval(:), a_alpha(:), q_alpha(:), v_eval(:), v_alpha(:), fint_eval(:) ! q_alpha viewed as (3, n_nodes) nodal coordinates, without a copy REAL(wp), POINTER :: q_alpha_nodes(:, :) - LOGICAL :: topo_ok, bw_hit + LOGICAL :: topo_ok, bw_hit, mb_hit INTEGER :: il, jl REAL(wp), POINTER :: Kt_eval(:, :), tension_eval(:), load_eval(:), jac_q_eval(:, :), jac_v_eval(:, :) 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 direct_banded = .NOT. has_load_proc .AND. .NOT. has_added_mass_proc use_element_mass_band = direct_banded .AND. .NOT. PRESENT(structural_mass) CALL validate_topology_once(work, elem_conn, n_dof/3, topo_ok) + bw_hit = .FALSE. IF (direct_banded) THEN bw_hit = topo_ok .AND. work%bw_valid 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 kl = work%bw_kl ku = work%bw_ku ELSE + work%mb_valid = .FALSE. CALL CD_Cable_Free_Bandwidth(elem_conn, n_dof/3, free, kl, ku, es, em, topology_validated=topo_ok) IF (es /= 0) THEN 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 RETURN END IF END IF + es = 0 IF (use_element_mass_band) THEN - CALL assemble_mass_band_free(elem_conn, l0, rho_a, work%dof_marker(1:n_dof), kl, ku, M_band, es, em) + ! The element mass band depends only on the connectivity, the free list, l0 and + ! rho_a: a step with the inputs of the remembered band (bw_hit: same connectivity and + ! free list) reuses it, which is the band the assembly would build again. + mb_hit = .FALSE. + IF (bw_hit) mb_hit = mass_band_matches(work, l0, rho_a) + IF (.NOT. mb_hit) THEN + work%mb_valid = .FALSE. + CALL assemble_mass_band_free(elem_conn, l0, rho_a, work%dof_marker(1:n_dof), kl, ku, M_band, es, em) + IF (es == 0 .AND. work%bw_valid) CALL remember_mass_band(work, l0, rho_a) + ELSE IF (work%mb_hits < HUGE(work%mb_hits)) THEN + work%mb_hits = work%mb_hits + 1 + END IF ELSE + work%mb_valid = .FALSE. CALL pack_dense_free_band(M, free, kl, ku, CD_ONE, M_band, es, em) END IF 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 IF (tangent_fresh) THEN IF (direct_banded) THEN CALL eval_dynamic(qk, R, es, em, effbandout=eff_band) + ! factored_now and work%step_factor state that eff_band/ipiv hold the LU of the last + ! factorization. eff_band now holds the unfactored tangent at qk, and they are set + ! again only once it is factored below: an exit before that (convergence, or the + ! near-tolerance allowance at the iteration limit) leaves the junction end query and + ! a warm re-solve to factor it instead of back-solving with an unfactored band. + factored_now = .FALSE. + work%step_factor = .FALSE. ELSE CALL eval_dynamic(qk, R, es, em, effout=eff) END IF @@ -1017,7 +1055,10 @@ SUBROUTINE CD_Cable_Gen_Alpha_Step(q, v, a, elem_conn, l0, ea, rho_a, tension_on eff_band_unfactored(il, jl) = eff_band(il, jl) END DO END DO - CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es, em) + ! eval_dynamic has just checked this band finite when a banded load or added-mass + ! callback contributes to it; the factorization then skips repeating that scan. + CALL CD_Factor_Banded(eff_band, kl, ku, ipiv, es, em, & + matrix_validated=has_load_band_proc .OR. has_added_mass_band_proc) factored_now = es == 0 work%step_factor = factored_now .AND. work%end_request 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 op_norm = MAX(CD_ONE, dense_row_abs_sum_max(eff, free)) END IF IF (direct_banded) THEN - CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es, em) + ! A tangent factored in this iteration is the finite LU CD_Factor_Banded returned; a + ! reused one keeps the full check. + CALL CD_Solve_Factored_Banded(eff_band, kl, ku, ipiv, dq, es, em, factor_validated=tangent_fresh) ELSE eff_free = eff(free, free) 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 CALL ensure_real_vector(work%load_eval, n_dof, ErrStat, ErrMsg) IF (ErrStat /= CD_DYN_OK) RETURN IF (need_band) THEN + ! a reallocated mass band no longer holds the remembered one + IF (ALLOCATED(work%M_band)) THEN + IF (SIZE(work%M_band, 1) < ldab .OR. SIZE(work%M_band, 2) < n_free) work%mb_valid = .FALSE. + ELSE + work%mb_valid = .FALSE. + END IF CALL ensure_real_matrix(work%M_band, ldab, n_free, ErrStat, ErrMsg) IF (ErrStat /= CD_DYN_OK) RETURN 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) work%bw_valid = .TRUE. END SUBROUTINE remember_bandwidth + LOGICAL FUNCTION mass_band_matches(work, l0, rho_a) RESULT(match) + !! work%M_band holds the element mass band of these l0 and rho_a (for the remembered + !! connectivity and free list). Equality is tested as <= and >= so that a NaN never matches. + TYPE(CD_CableGenAlphaWorkspace), INTENT(IN) :: work + REAL(wp), INTENT(IN) :: l0(:), rho_a(:) + INTEGER :: e + + match = work%mb_valid + IF (match) match = SIZE(work%mb_l0) == SIZE(l0) .AND. SIZE(work%mb_rho) == SIZE(rho_a) + IF (.NOT. match) RETURN + DO e = 1, SIZE(l0) + IF (.NOT. (work%mb_l0(e) <= l0(e) .AND. work%mb_l0(e) >= l0(e))) THEN + match = .FALSE. + RETURN + END IF + END DO + DO e = 1, SIZE(rho_a) + IF (.NOT. (work%mb_rho(e) <= rho_a(e) .AND. work%mb_rho(e) >= rho_a(e))) THEN + match = .FALSE. + RETURN + END IF + END DO + END FUNCTION mass_band_matches + + SUBROUTINE remember_mass_band(work, l0, rho_a) + !! Record the inputs of the element mass band just assembled into work%M_band. + TYPE(CD_CableGenAlphaWorkspace), INTENT(INOUT) :: work + REAL(wp), INTENT(IN) :: l0(:), rho_a(:) + INTEGER :: istat + + work%mb_valid = .FALSE. + IF (ALLOCATED(work%mb_l0)) THEN + IF (SIZE(work%mb_l0) /= SIZE(l0)) DEALLOCATE (work%mb_l0) + END IF + IF (ALLOCATED(work%mb_rho)) THEN + IF (SIZE(work%mb_rho) /= SIZE(rho_a)) DEALLOCATE (work%mb_rho) + END IF + IF (.NOT. ALLOCATED(work%mb_l0)) THEN + ALLOCATE (work%mb_l0(SIZE(l0)), STAT=istat) + IF (istat /= 0) RETURN + END IF + IF (.NOT. ALLOCATED(work%mb_rho)) THEN + ALLOCATE (work%mb_rho(SIZE(rho_a)), STAT=istat) + IF (istat /= 0) RETURN + END IF + work%mb_l0 = l0 + work%mb_rho = rho_a + work%mb_valid = .TRUE. + END SUBROUTINE remember_mass_band + SUBROUTINE partition_free_dofs_workspace(fixed_dofs, n_dof, free, n_free, fixed, marker, ErrStat, ErrMsg) INTEGER, INTENT(IN) :: fixed_dofs(:), n_dof INTEGER, INTENT(INOUT) :: free(:), fixed(:) @@ -1907,14 +2006,15 @@ SUBROUTINE partition_free_dofs_workspace(fixed_dofs, n_dof, free, n_free, fixed, END SUBROUTINE partition_free_dofs_workspace SUBROUTINE ensure_int_vector(x, n, ErrStat, ErrMsg) + !! The ensure_* helpers run for every workspace array on every step: they write ErrMsg only + !! on failure, and their callers (ensure_dof_workspace, ensure_step_workspace) clear it. INTEGER, ALLOCATABLE, INTENT(INOUT) :: x(:) INTEGER, INTENT(IN) :: n INTEGER, INTENT(OUT) :: ErrStat - CHARACTER(*), INTENT(OUT) :: ErrMsg + CHARACTER(*), INTENT(INOUT) :: ErrMsg INTEGER :: istat ErrStat = CD_DYN_OK - ErrMsg = '' IF (n < 0) THEN CALL fail(ErrStat, ErrMsg, 'internal workspace size must be non-negative') RETURN @@ -1931,11 +2031,10 @@ SUBROUTINE ensure_real_vector(x, n, ErrStat, ErrMsg) REAL(wp), ALLOCATABLE, INTENT(INOUT) :: x(:) INTEGER, INTENT(IN) :: n INTEGER, INTENT(OUT) :: ErrStat - CHARACTER(*), INTENT(OUT) :: ErrMsg + CHARACTER(*), INTENT(INOUT) :: ErrMsg INTEGER :: istat ErrStat = CD_DYN_OK - ErrMsg = '' IF (n < 0) THEN CALL fail(ErrStat, ErrMsg, 'internal workspace size must be non-negative') RETURN @@ -1952,11 +2051,10 @@ SUBROUTINE ensure_real_matrix(x, n1, n2, ErrStat, ErrMsg) REAL(wp), ALLOCATABLE, INTENT(INOUT) :: x(:, :) INTEGER, INTENT(IN) :: n1, n2 INTEGER, INTENT(OUT) :: ErrStat - CHARACTER(*), INTENT(OUT) :: ErrMsg + CHARACTER(*), INTENT(INOUT) :: ErrMsg INTEGER :: istat ErrStat = CD_DYN_OK - ErrMsg = '' IF (n1 < 0 .OR. n2 < 0) THEN CALL fail(ErrStat, ErrMsg, 'internal workspace shape must be non-negative') RETURN diff --git a/src/CableDyn_Hydro.f90 b/src/CableDyn_Hydro.f90 index cb8e7b08..2e033c65 100644 --- a/src/CableDyn_Hydro.f90 +++ b/src/CableDyn_Hydro.f90 @@ -267,24 +267,35 @@ SUBROUTINE CD_Cable_Morison_Drag_Force(q, v, elem_conn, l0, fluid_velocity, wate END SUBROUTINE CD_Cable_Morison_Drag_Force SUBROUTINE CD_Morison_Drag_Element_Load(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & - rho, diameter, cdn, cdt, force, jac_q, jac_v, ErrStat, ErrMsg) + rho, diameter, cdn, cdt, force, jac_q, jac_v, ErrStat, ErrMsg, & + inputs_validated) !! Two-node Morison drag element kernel for performance-critical model assembly. !! !! This routine is formula-identical to the one-element path of !! CD_Cable_Morison_Drag_Load, but avoids cable-wide shape validation and !! reshape/scatter work inside Newton iterations. + !! `inputs_validated` is an internal hot-path contract (as in CD_Component_Wave_Kinematics), + !! shared by the element kernels below: a caller that already guarantees finite state, + !! fields and waterlines, a positive l0 and valid section scalars skips the per-element + !! input scan. The default remains full validation for public/direct calls. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, fluid_a(3), fluid_b(3), wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, cdn, cdt REAL(wp), INTENT(OUT) :: force(6), jac_q(6, 6), jac_v(6, 6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated INTEGER :: k REAL(wp) :: za, zb, frac, dfrac_da, dfrac_db, chord(3), length, tangent(3) REAL(wp) :: wa, wb, dwa_da, dwa_db, dwb_da, dwb_db, rel(3), f_per_len(3), Jrel(3, 3) REAL(wp) :: ga(3), gb(3), scale, dscale, drel(3), df(3), proj(3, 3) - CALL validate_drag_element_inputs(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & - rho, diameter, cdn, cdt, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_drag_element_inputs(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & + rho, diameter, cdn, cdt, ErrStat, ErrMsg) + END IF force = CD_ZERO jac_q = CD_ZERO jac_v = CD_ZERO @@ -374,18 +385,24 @@ SUBROUTINE CD_Morison_Drag_Element_Load(qa, qb, va, vb, l0, fluid_a, fluid_b, wl END SUBROUTINE CD_Morison_Drag_Element_Load SUBROUTINE CD_Morison_Drag_Element_Force(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & - rho, diameter, cdn, cdt, force, ErrStat, ErrMsg) + rho, diameter, cdn, cdt, force, ErrStat, ErrMsg, inputs_validated) !! Two-node Morison drag force kernel for residual-only trial evaluations. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, fluid_a(3), fluid_b(3), wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, cdn, cdt REAL(wp), INTENT(OUT) :: force(6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), wa, wb, lower, higher REAL(wp) :: fluid_e(3), rel(3), f_per_len(3) - CALL validate_drag_element_inputs(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & - rho, diameter, cdn, cdt, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_drag_element_inputs(qa, qb, va, vb, l0, fluid_a, fluid_b, wl_a, wl_b, & + rho, diameter, cdn, cdt, ErrStat, ErrMsg) + END IF force = CD_ZERO IF (ErrStat /= CD_HYDRO_OK) RETURN @@ -550,21 +567,29 @@ SUBROUTINE CD_Cable_Froude_Krylov_Force(q, v, elem_conn, l0, fluid_acceleration, END SUBROUTINE CD_Cable_Froude_Krylov_Force SUBROUTINE CD_Froude_Krylov_Element_Load(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & - rho, diameter, can, cat, force, jac_q, jac_v, ErrStat, ErrMsg) + rho, diameter, can, cat, force, jac_q, jac_v, ErrStat, ErrMsg, & + inputs_validated) !! Two-node Froude-Krylov/fluid-inertia load and residual Jacobian. + !! The load is velocity-independent: jac_v is identically zero. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, accel_a(3), accel_b(3), wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, can, cat REAL(wp), INTENT(OUT) :: force(6), jac_q(6, 6), jac_v(6, 6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load INTEGER :: k, lo0, hi0, col_a, col_b REAL(wp) :: za, zb, frac, df_da, df_db, chord(3), length, tangent(3), proj(3, 3), dt(3), zero_dt(3) REAL(wp) :: f_lo(3), f_hi(3), df_lo(3), df_hi(3), df_node_lo(3), df_node_hi(3) REAL(wp) :: c_lolo, c_lohi, c_hihi, dc_lolo, dc_lohi, dc_hihi, df_frac, eye(3, 3) REAL(wp) :: acc_lo(3), acc_hi(3) - CALL validate_fk_element_inputs(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & - rho, diameter, can, cat, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_fk_element_inputs(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & + rho, diameter, can, cat, ErrStat, ErrMsg) + END IF force = CD_ZERO jac_q = CD_ZERO jac_v = CD_ZERO @@ -629,19 +654,28 @@ SUBROUTINE CD_Froude_Krylov_Element_Load(qa, qb, va, vb, l0, accel_a, accel_b, w END SUBROUTINE CD_Froude_Krylov_Element_Load SUBROUTINE CD_Froude_Krylov_Element_Force(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & - rho, diameter, can, cat, force, ErrStat, ErrMsg) - !! Two-node Froude-Krylov/fluid-inertia force. + rho, diameter, can, cat, force, ErrStat, ErrMsg, inputs_validated) + !! Two-node Froude-Krylov/fluid-inertia force. The per-length force is + !! CD_Froude_Krylov_Per_Length evaluated with its shared statements: the section scalars + !! and accelerations are validated once (or by the caller) and only the tangent checks of + !! unit_tangent can still fail, so the unit tangent is formed once for both end nodes. REAL(wp), INTENT(IN) :: qa(3), qb(3), va(3), vb(3), l0, accel_a(3), accel_b(3), wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, can, cat REAL(wp), INTENT(OUT) :: force(6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load INTEGER :: lo0, hi0 REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), acc_lo(3), acc_hi(3), f_lo(3), f_hi(3) - REAL(wp) :: c_lolo, c_lohi, c_hihi, f + REAL(wp) :: c_lolo, c_lohi, c_hihi, f, unit(3), normal(3), tangential(3) - CALL validate_fk_element_inputs(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & - rho, diameter, can, cat, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_fk_element_inputs(qa, qb, va, vb, l0, accel_a, accel_b, wl_a, wl_b, & + rho, diameter, can, cat, ErrStat, ErrMsg) + END IF force = CD_ZERO IF (ErrStat /= CD_HYDRO_OK) RETURN za = qa(3) - wl_a @@ -666,10 +700,12 @@ SUBROUTINE CD_Froude_Krylov_Element_Force(qa, qb, va, vb, l0, accel_a, accel_b, acc_lo = accel_b acc_hi = accel_a END IF - CALL CD_Froude_Krylov_Per_Length(acc_lo, tangent, rho, diameter, can, cat, f_lo, ErrStat, ErrMsg) - IF (ErrStat /= CD_HYDRO_OK) RETURN - CALL CD_Froude_Krylov_Per_Length(acc_hi, tangent, rho, diameter, can, cat, f_hi, ErrStat, ErrMsg) + CALL unit_tangent(tangent, unit, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN + CALL split_about_unit(acc_lo, unit, normal, tangential) + CALL fk_force_from_split(normal, tangential, rho, diameter, can, cat, f_lo) + CALL split_about_unit(acc_hi, unit, normal, tangential) + CALL fk_force_from_split(normal, tangential, rho, diameter, can, cat, f_hi) f = frac c_lolo = l0*(f - f*f + f**3/3.0_wp) c_lohi = l0*(0.5_wp*f*f - f**3/3.0_wp) @@ -757,15 +793,22 @@ SUBROUTINE CD_Cable_Buoyancy_Recovery_Force(q, v, elem_conn, l0, waterline_z, & END SUBROUTINE CD_Cable_Buoyancy_Recovery_Force SUBROUTINE CD_Buoyancy_Recovery_Element_Load(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, & - force, jac_q, jac_v, ErrStat, ErrMsg) + force, jac_q, jac_v, ErrStat, ErrMsg, inputs_validated) !! Two-node wetting-dependent buoyancy recovery force and residual Jacobian. + !! The load is velocity-independent: jac_v is identically zero. REAL(wp), INTENT(IN) :: qa(3), qb(3), l0, wl_a, wl_b, rho, diameter, gravity REAL(wp), INTENT(OUT) :: force(6), jac_q(6, 6), jac_v(6, 6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load REAL(wp) :: za, zb, frac, df_da, df_db, half_g, recovery, buoyancy, drec_da, drec_db - CALL validate_buoyancy_element_inputs(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_buoyancy_element_inputs(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, ErrStat, ErrMsg) + END IF force = CD_ZERO jac_q = CD_ZERO jac_v = CD_ZERO @@ -787,15 +830,21 @@ SUBROUTINE CD_Buoyancy_Recovery_Element_Load(qa, qb, l0, wl_a, wl_b, rho, diamet END SUBROUTINE CD_Buoyancy_Recovery_Element_Load SUBROUTINE CD_Buoyancy_Recovery_Element_Force(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, & - force, ErrStat, ErrMsg) + force, ErrStat, ErrMsg, inputs_validated) !! Two-node wetting-dependent buoyancy recovery force. REAL(wp), INTENT(IN) :: qa(3), qb(3), l0, wl_a, wl_b, rho, diameter, gravity REAL(wp), INTENT(OUT) :: force(6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load REAL(wp) :: za, zb, frac, half_g, recovery, buoyancy - CALL validate_buoyancy_element_inputs(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_buoyancy_element_inputs(qa, qb, l0, wl_a, wl_b, rho, diameter, gravity, ErrStat, ErrMsg) + END IF force = CD_ZERO IF (ErrStat /= CD_HYDRO_OK) RETURN za = qa(3) - wl_a @@ -1026,20 +1075,26 @@ SUBROUTINE CD_Cable_Added_Mass_Force(q, accel, elem_conn, l0, waterline_z, rho, END SUBROUTINE CD_Cable_Added_Mass_Force SUBROUTINE CD_Added_Mass_Element(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, rho, diameter, can, cat, & - M_add, dMa_a_dq, ErrStat, ErrMsg) + M_add, dMa_a_dq, ErrStat, ErrMsg, inputs_validated) !! Two-node added-mass matrix and directional derivative kernel. REAL(wp), INTENT(IN) :: qa(3), qb(3), accel_a(3), accel_b(3), l0, wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, can, cat REAL(wp), INTENT(OUT) :: M_add(6, 6), dMa_a_dq(6, 6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load INTEGER :: lo0, hi0 REAL(wp) :: za, zb, frac, df_da, df_db, chord(3), length, tangent(3), proj(3, 3), zero_dt(3) REAL(wp) :: Me(3, 3), dMe(3, 3), acc_lo(3), acc_hi(3) REAL(wp) :: c_lolo, c_lohi, c_hihi, dc_lolo, dc_lohi, dc_hihi, eye(3, 3) - CALL validate_added_mass_element_inputs(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, & - rho, diameter, can, cat, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_added_mass_element_inputs(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, & + rho, diameter, can, cat, ErrStat, ErrMsg) + END IF M_add = CD_ZERO dMa_a_dq = CD_ZERO IF (ErrStat /= CD_HYDRO_OK) RETURN @@ -1115,34 +1170,40 @@ SUBROUTINE CD_Added_Mass_Element(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, rho, END BLOCK END SUBROUTINE CD_Added_Mass_Element - SUBROUTINE CD_Added_Mass_Element_Matrix(qa, qb, l0, wl_a, wl_b, rho, diameter, can, cat, M_add, ErrStat, ErrMsg) + SUBROUTINE CD_Added_Mass_Element_Matrix(qa, qb, l0, wl_a, wl_b, rho, diameter, can, cat, M_add, ErrStat, ErrMsg, & + inputs_validated) !! Two-node configuration-dependent Morison added-mass matrix without !! d(M*a)/dq work. Used by initial-acceleration solves where the supplied - !! acceleration is exactly zero but the mass matrix is still needed. + !! acceleration is exactly zero but the mass matrix is still needed. The per-length + !! matrix is CD_Morison_Added_Mass_Per_Length evaluated with its shared statements once + !! the section scalars are validated (only the tangent checks of unit_tangent remain). REAL(wp), INTENT(IN) :: qa(3), qb(3), l0, wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, can, cat REAL(wp), INTENT(OUT) :: M_add(6, 6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load INTEGER :: lo0, hi0 - REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), Me(3, 3) + REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), Me(3, 3), unit(3) REAL(wp) :: c_lolo, c_lohi, c_hihi M_add = CD_ZERO ErrStat = CD_HYDRO_OK ErrMsg = '' - IF (.NOT. (CD_All_Finite(qa) .AND. CD_All_Finite(qb) .AND. & - CD_Is_Finite(l0) .AND. l0 > CD_ZERO .AND. CD_Is_Finite(wl_a) .AND. & - CD_Is_Finite(wl_b))) THEN - CALL fail(ErrStat, ErrMsg, 'added-mass element matrix geometry must be finite') - RETURN - END IF - CALL validate_hydro_scalars(rho, diameter, ErrStat, ErrMsg) - IF (ErrStat /= CD_HYDRO_OK) RETURN - IF (.NOT. (CD_Is_Finite(can) .AND. CD_Is_Finite(cat) .AND. can >= CD_ZERO .AND. cat >= CD_ZERO)) THEN - CALL fail(ErrStat, ErrMsg, 'added-mass coefficients must be finite and non-negative') - RETURN + IF (.NOT. trusted(inputs_validated)) THEN + IF (.NOT. (CD_All_Finite(qa) .AND. CD_All_Finite(qb) .AND. & + CD_Is_Finite(l0) .AND. l0 > CD_ZERO .AND. CD_Is_Finite(wl_a) .AND. & + CD_Is_Finite(wl_b))) THEN + CALL fail(ErrStat, ErrMsg, 'added-mass element matrix geometry must be finite') + RETURN + END IF + CALL validate_hydro_scalars(rho, diameter, ErrStat, ErrMsg) + IF (ErrStat /= CD_HYDRO_OK) RETURN + IF (.NOT. (CD_Is_Finite(can) .AND. CD_Is_Finite(cat) .AND. can >= CD_ZERO .AND. cat >= CD_ZERO)) THEN + CALL fail(ErrStat, ErrMsg, 'added-mass coefficients must be finite and non-negative') + RETURN + END IF END IF za = qa(3) - wl_a @@ -1163,8 +1224,9 @@ SUBROUTINE CD_Added_Mass_Element_Matrix(qa, qb, l0, wl_a, wl_b, rho, diameter, c lo0 = 4 hi0 = 1 END IF - CALL CD_Morison_Added_Mass_Per_Length(tangent, rho, diameter, can, cat, Me, ErrStat, ErrMsg) + CALL unit_tangent(tangent, unit, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN + CALL added_mass_about_unit(unit, rho, diameter, can, cat, Me) CALL wet_consistent_coefficients(l0, frac, c_lolo, c_lohi, c_hihi) M_add(lo0:lo0 + 2, lo0:lo0 + 2) = M_add(lo0:lo0 + 2, lo0:lo0 + 2) + c_lolo*Me M_add(lo0:lo0 + 2, hi0:hi0 + 2) = M_add(lo0:lo0 + 2, hi0:hi0 + 2) + c_lohi*Me @@ -1173,19 +1235,26 @@ SUBROUTINE CD_Added_Mass_Element_Matrix(qa, qb, l0, wl_a, wl_b, rho, diameter, c END SUBROUTINE CD_Added_Mass_Element_Matrix SUBROUTINE CD_Added_Mass_Element_Force(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, rho, diameter, can, cat, & - force, ErrStat, ErrMsg) - !! Two-node added-mass product M_add(q)*accel for residual-only evaluations. + force, ErrStat, ErrMsg, inputs_validated) + !! Two-node added-mass product M_add(q)*accel for residual-only evaluations. The + !! per-length matrix is formed as in CD_Added_Mass_Element_Matrix. REAL(wp), INTENT(IN) :: qa(3), qb(3), accel_a(3), accel_b(3), l0, wl_a, wl_b REAL(wp), INTENT(IN) :: rho, diameter, can, cat REAL(wp), INTENT(OUT) :: force(6) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated !! see CD_Morison_Drag_Element_Load INTEGER :: lo0, hi0 - REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), Me(3, 3) + REAL(wp) :: za, zb, frac, chord(3), length, tangent(3), Me(3, 3), unit(3) REAL(wp) :: c_lolo, c_lohi, c_hihi, acc_lo(3), acc_hi(3) - CALL validate_added_mass_element_inputs(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, & - rho, diameter, can, cat, ErrStat, ErrMsg) + IF (trusted(inputs_validated)) THEN + ErrStat = CD_HYDRO_OK + ErrMsg = '' + ELSE + CALL validate_added_mass_element_inputs(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, & + rho, diameter, can, cat, ErrStat, ErrMsg) + END IF force = CD_ZERO IF (ErrStat /= CD_HYDRO_OK) RETURN @@ -1211,8 +1280,9 @@ SUBROUTINE CD_Added_Mass_Element_Force(qa, qb, accel_a, accel_b, l0, wl_a, wl_b, acc_lo = accel_b acc_hi = accel_a END IF - CALL CD_Morison_Added_Mass_Per_Length(tangent, rho, diameter, can, cat, Me, ErrStat, ErrMsg) + CALL unit_tangent(tangent, unit, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN + CALL added_mass_about_unit(unit, rho, diameter, can, cat, Me) CALL wet_consistent_coefficients(l0, frac, c_lolo, c_lohi, c_hihi) force(lo0:lo0 + 2) = c_lolo*MATMUL(Me, acc_lo) + c_lohi*MATMUL(Me, acc_hi) force(hi0:hi0 + 2) = c_lohi*MATMUL(Me, acc_lo) + c_hihi*MATMUL(Me, acc_hi) @@ -1225,7 +1295,7 @@ SUBROUTINE CD_Split_Normal_Tangential(vector, tangent, normal, tangential, ErrSt REAL(wp), INTENT(OUT) :: normal(3), tangential(3) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg - REAL(wp) :: unit(3), dot_vt + REAL(wp) :: unit(3) normal = CD_ZERO tangential = CD_ZERO @@ -1237,9 +1307,7 @@ SUBROUTINE CD_Split_Normal_Tangential(vector, tangent, normal, tangential, ErrSt CALL fail(ErrStat, ErrMsg, 'vector must be finite') RETURN END IF - dot_vt = DOT_PRODUCT(vector, unit) - tangential = dot_vt*unit - normal = vector - tangential + CALL split_about_unit(vector, unit, normal, tangential) END SUBROUTINE CD_Split_Normal_Tangential SUBROUTINE CD_Morison_Drag_Per_Length(rel_velocity, tangent, rho, diameter, cdn, cdt, & @@ -1366,7 +1434,7 @@ SUBROUTINE CD_Morison_Added_Mass_Per_Length(tangent, rho, diameter, can, cat, M_ REAL(wp), INTENT(OUT) :: M_a(3, 3) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg - REAL(wp) :: unit(3), q_outer(3, 3), eye(3, 3), area + REAL(wp) :: unit(3) M_a = CD_ZERO ErrStat = CD_HYDRO_OK @@ -1379,10 +1447,7 @@ SUBROUTINE CD_Morison_Added_Mass_Per_Length(tangent, rho, diameter, can, cat, M_ END IF CALL unit_tangent(tangent, unit, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN - eye = identity3() - q_outer = outer3(unit, unit) - area = 0.25_wp*PI*diameter*diameter - M_a = rho*area*(can*(eye - q_outer) + cat*q_outer) + CALL added_mass_about_unit(unit, rho, diameter, can, cat, M_a) END SUBROUTINE CD_Morison_Added_Mass_Per_Length SUBROUTINE CD_Froude_Krylov_Per_Length(fluid_acceleration, tangent, rho, diameter, can, cat, & @@ -1393,7 +1458,7 @@ SUBROUTINE CD_Froude_Krylov_Per_Length(fluid_acceleration, tangent, rho, diamete REAL(wp), INTENT(OUT) :: force(3) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg - REAL(wp) :: normal(3), tangential(3), area + REAL(wp) :: normal(3), tangential(3) force = CD_ZERO ErrStat = CD_HYDRO_OK @@ -1406,8 +1471,7 @@ SUBROUTINE CD_Froude_Krylov_Per_Length(fluid_acceleration, tangent, rho, diamete END IF CALL CD_Split_Normal_Tangential(fluid_acceleration, tangent, normal, tangential, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN - area = 0.25_wp*PI*diameter*diameter - force = rho*area*((CD_ONE + can)*normal + (CD_ONE + cat)*tangential) + CALL fk_force_from_split(normal, tangential, rho, diameter, can, cat, force) END SUBROUTINE CD_Froude_Krylov_Per_Length SUBROUTINE CD_Solve_Dispersion_Wavenumber(omega, depth, gravity, k, ErrStat, ErrMsg) @@ -2363,13 +2427,15 @@ END SUBROUTINE validate_waterline_force_shapes SUBROUTINE assemble_added_mass_matrix_only(q, elem_conn, l0, waterline_z, rho, diameter, can, cat, & M_add, ErrStat, ErrMsg) + !! The callers validate the section scalars first, so the per-length matrix is formed with + !! the statements of CD_Morison_Added_Mass_Per_Length after its tangent checks alone. REAL(wp), INTENT(IN) :: q(:), l0(:), waterline_z(:), rho, diameter, can, cat INTEGER, INTENT(IN) :: elem_conn(:, :) REAL(wp), INTENT(OUT) :: M_add(:, :) INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg INTEGER :: e, a, b, lo, hi, n_nodes - REAL(wp) :: frac, za, zb, chord(3), length, tangent(3), Me(3, 3) + REAL(wp) :: frac, za, zb, chord(3), length, tangent(3), Me(3, 3), unit(3) REAL(wp) :: c_lolo, c_lohi, c_hihi, f M_add = CD_ZERO @@ -2402,8 +2468,9 @@ SUBROUTINE assemble_added_mass_matrix_only(q, elem_conn, l0, waterline_z, rho, d lo = b hi = a END IF - CALL CD_Morison_Added_Mass_Per_Length(tangent, rho, diameter, can, cat, Me, ErrStat, ErrMsg) + CALL unit_tangent(tangent, unit, ErrStat, ErrMsg) IF (ErrStat /= CD_HYDRO_OK) RETURN + CALL added_mass_about_unit(unit, rho, diameter, can, cat, Me) f = frac c_lolo = l0(e)*(f - f*f + f**3/3.0_wp) c_lohi = l0(e)*(0.5_wp*f*f - f**3/3.0_wp) @@ -2646,14 +2713,14 @@ SUBROUTINE profile_ratios(k, depth, z_eval, cosh_ratio, sinh_ratio) END SUBROUTINE profile_ratios SUBROUTINE unit_tangent(tangent, unit, ErrStat, ErrMsg) + !! Writes ErrMsg only on failure: every caller has cleared it. REAL(wp), INTENT(IN) :: tangent(3) REAL(wp), INTENT(OUT) :: unit(3) INTEGER, INTENT(OUT) :: ErrStat - CHARACTER(*), INTENT(OUT) :: ErrMsg + CHARACTER(*), INTENT(INOUT) :: ErrMsg REAL(wp) :: norm unit = CD_ZERO ErrStat = CD_HYDRO_OK - ErrMsg = '' IF (.NOT. CD_All_Finite(tangent)) THEN CALL fail(ErrStat, ErrMsg, 'tangent must be finite') RETURN @@ -2666,6 +2733,43 @@ SUBROUTINE unit_tangent(tangent, unit, ErrStat, ErrMsg) unit = tangent/norm END SUBROUTINE unit_tangent + PURE SUBROUTINE split_about_unit(vector, unit, normal, tangential) + !! Normal and tangential parts of vector about a unit tangent from unit_tangent. + REAL(wp), INTENT(IN) :: vector(3), unit(3) + REAL(wp), INTENT(OUT) :: normal(3), tangential(3) + REAL(wp) :: dot_vt + dot_vt = DOT_PRODUCT(vector, unit) + tangential = dot_vt*unit + normal = vector - tangential + END SUBROUTINE split_about_unit + + PURE SUBROUTINE fk_force_from_split(normal, tangential, rho, diameter, can, cat, force) + !! Froude-Krylov plus fluid-inertia force per unit length from the split fluid acceleration. + REAL(wp), INTENT(IN) :: normal(3), tangential(3), rho, diameter, can, cat + REAL(wp), INTENT(OUT) :: force(3) + REAL(wp) :: area + area = 0.25_wp*PI*diameter*diameter + force = rho*area*((CD_ONE + can)*normal + (CD_ONE + cat)*tangential) + END SUBROUTINE fk_force_from_split + + PURE SUBROUTINE added_mass_about_unit(unit, rho, diameter, can, cat, M_a) + !! Morison added-mass matrix per unit length about a unit tangent from unit_tangent. + REAL(wp), INTENT(IN) :: unit(3), rho, diameter, can, cat + REAL(wp), INTENT(OUT) :: M_a(3, 3) + REAL(wp) :: q_outer(3, 3), eye(3, 3), area + eye = identity3() + q_outer = outer3(unit, unit) + area = 0.25_wp*PI*diameter*diameter + M_a = rho*area*(can*(eye - q_outer) + cat*q_outer) + END SUBROUTINE added_mass_about_unit + + PURE LOGICAL FUNCTION trusted(inputs_validated) RESULT(trust) + !! The value of an optional inputs_validated flag (.FALSE. when absent). + LOGICAL, INTENT(IN), OPTIONAL :: inputs_validated + trust = .FALSE. + IF (PRESENT(inputs_validated)) trust = inputs_validated + END FUNCTION trusted + SUBROUTINE validate_hydro_scalars(rho, diameter, ErrStat, ErrMsg) REAL(wp), INTENT(IN) :: rho, diameter INTEGER, INTENT(OUT) :: ErrStat diff --git a/src/CableDyn_Model.f90 b/src/CableDyn_Model.f90 index 7bd66c1f..be545525 100644 --- a/src/CableDyn_Model.f90 +++ b/src/CableDyn_Model.f90 @@ -3467,7 +3467,7 @@ SUBROUTINE CD_Update_Model_SegmentLength(model, elem, l0_new, l0_dot, ErrStat, E END IF END IF ! Commit. The consistent mass follows l0 implicitly (it is applied element by - ! element from the committed lengths, never stored). + ! element from the committed lengths, and the step rebuilds its mass band when they change). l0_entry = model%l0(elem) l0_dot_entry = model%l0_dot(elem) f_ext_entry = model%f_ext @@ -5884,12 +5884,18 @@ SUBROUTINE model_contributor_load_banded(model, q, v, free, kl, ku, force, jac_q END DO END IF + ! q and v are finite (checked above) and every element input the kernels validate -- l0, + ! the damping coefficients, the nodal fluid fields and waterlines, and the section scalars -- + ! is validated wherever the model sets it (CD_Init_Model, CD_Update_Model_Hydro_Fields, + ! CD_Update_Model_SegmentLength): the kernels skip their per-element input scan. Every + ! writer of these fields must validate them (the snapshot restore and the model copies + ! assign only previously validated states). DO e = 1, model%n_elem CALL extract_element_state(model, e, q, v, idx, q2, v2) IF (model%has_damping) THEN CALL CD_Axial_Damping_Element_Load(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & model%ba_dyn(e), f2, jq2, jv2, ErrStat, ErrMsg, & - l0_dot=model%l0_dot(e)) + l0_dot=model%l0_dot(e), inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) CALL scatter_element_matrix_band(jac_q_band, model%free_map_work, kl, ku, idx, jq2, ErrStat, ErrMsg) @@ -5930,7 +5936,8 @@ SUBROUTINE model_contributor_load_banded(model, q, v, free, kl, ku, force, jac_q CALL CD_Morison_Drag_Element_Load(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & fluid2(:, 1), fluid2(:, 2), wl2(1), wl2(2), model%drag_rho, & model%drag_diameter_elem(e), model%drag_cdn_elem(e), & - model%drag_cdt_elem(e), f2, jq2, jv2, ErrStat, ErrMsg) + model%drag_cdt_elem(e), f2, jq2, jv2, ErrStat, ErrMsg, & + inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) CALL scatter_element_matrix_band(jac_q_band, model%free_map_work, kl, ku, idx, jq2, ErrStat, ErrMsg) @@ -5944,25 +5951,28 @@ SUBROUTINE model_contributor_load_banded(model, q, v, free, kl, ku, force, jac_q CALL CD_Froude_Krylov_Element_Load(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & accel2(:, 1), accel2(:, 2), wl2(1), wl2(2), model%fk_rho, & model%fk_diameter_elem(e), model%fk_can_elem(e), & - model%fk_cat_elem(e), f2, jq2, jv2, ErrStat, ErrMsg) + model%fk_cat_elem(e), f2, jq2, jv2, ErrStat, ErrMsg, & + inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) CALL scatter_element_matrix_band(jac_q_band, model%free_map_work, kl, ku, idx, jq2, ErrStat, ErrMsg) IF (ErrStat /= CD_MODEL_OK) RETURN - CALL scatter_element_matrix_band(jac_v_band, model%free_map_work, kl, ku, idx, jv2, ErrStat, ErrMsg) - IF (ErrStat /= CD_MODEL_OK) RETURN + ! jv2 is identically zero (a velocity-independent load), and adding zeros changes no + ! entry of a band that starts at +0 and only accumulates: its scatter is skipped. The + ! index and bandwidth checks of this element ran in the jac_q scatter above (the step + ! passes both bands with the same shape). END IF IF (model%has_buoyancy_recovery) THEN CALL extract_nodal_scalar(model%buoyancy_waterline_z, model, e, wl2) CALL CD_Buoyancy_Recovery_Element_Load(q2(1:3), q2(4:6), model%l0(e), wl2(1), wl2(2), & model%buoyancy_rho, model%buoyancy_diameter_elem(e), & - model%buoyancy_gravity, f2, jq2, jv2, ErrStat, ErrMsg) + model%buoyancy_gravity, f2, jq2, jv2, ErrStat, ErrMsg, & + inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) CALL scatter_element_matrix_band(jac_q_band, model%free_map_work, kl, ku, idx, jq2, ErrStat, ErrMsg) IF (ErrStat /= CD_MODEL_OK) RETURN - CALL scatter_element_matrix_band(jac_v_band, model%free_map_work, kl, ku, idx, jv2, ErrStat, ErrMsg) - IF (ErrStat /= CD_MODEL_OK) RETURN + ! jv2 is identically zero: no scatter (as for Froude-Krylov above) END IF END DO @@ -6083,6 +6093,7 @@ SUBROUTINE model_contributor_force(model, q, v, force, ErrStat, ErrMsg, end_node END DO END IF + ! validated element inputs: see model_contributor_load_banded DO e = 1, model%n_elem IF (ends) THEN IF (ALL(model%elem_conn(:, e) /= 1) .AND. ALL(model%elem_conn(:, e) /= nn)) CYCLE @@ -6092,7 +6103,7 @@ SUBROUTINE model_contributor_force(model, q, v, force, ErrStat, ErrMsg, end_node IF (model%has_damping) THEN CALL CD_Axial_Damping_Element_Force(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & model%ba_dyn(e), f2, ErrStat, ErrMsg, & - l0_dot=model%l0_dot(e)) + l0_dot=model%l0_dot(e), inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) END IF @@ -6121,7 +6132,7 @@ SUBROUTINE model_contributor_force(model, q, v, force, ErrStat, ErrMsg, end_node CALL CD_Morison_Drag_Element_Force(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & fluid2(:, 1), fluid2(:, 2), wl2(1), wl2(2), model%drag_rho, & model%drag_diameter_elem(e), model%drag_cdn_elem(e), & - model%drag_cdt_elem(e), f2, ErrStat, ErrMsg) + model%drag_cdt_elem(e), f2, ErrStat, ErrMsg, inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) END IF @@ -6131,7 +6142,7 @@ SUBROUTINE model_contributor_force(model, q, v, force, ErrStat, ErrMsg, end_node CALL CD_Froude_Krylov_Element_Force(q2(1:3), q2(4:6), v2(1:3), v2(4:6), model%l0(e), & accel2(:, 1), accel2(:, 2), wl2(1), wl2(2), model%fk_rho, & model%fk_diameter_elem(e), model%fk_can_elem(e), & - model%fk_cat_elem(e), f2, ErrStat, ErrMsg) + model%fk_cat_elem(e), f2, ErrStat, ErrMsg, inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) END IF @@ -6139,7 +6150,8 @@ SUBROUTINE model_contributor_force(model, q, v, force, ErrStat, ErrMsg, end_node CALL extract_nodal_scalar(model%buoyancy_waterline_z, model, e, wl2) CALL CD_Buoyancy_Recovery_Element_Force(q2(1:3), q2(4:6), model%l0(e), wl2(1), wl2(2), & model%buoyancy_rho, model%buoyancy_diameter_elem(e), & - model%buoyancy_gravity, f2, ErrStat, ErrMsg) + model%buoyancy_gravity, f2, ErrStat, ErrMsg, & + inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN CALL scatter_element_vector(force, idx, f2) END IF @@ -6756,6 +6768,7 @@ SUBROUTINE model_added_mass_banded(model, q, accel, free, kl, ku, M_add_a, M_add END DO END IF + ! validated element inputs (q and accel checked above): see model_contributor_load_banded DO e = 1, model%n_elem CALL extract_element_state(model, e, q, accel, idx, q2, a2) CALL extract_nodal_scalar(model%added_mass_waterline_z, model, e, wl2) @@ -6764,14 +6777,14 @@ SUBROUTINE model_added_mass_banded(model, q, accel, free, kl, ku, M_add_a, M_add CALL CD_Added_Mass_Element_Matrix(q2(1:3), q2(4:6), model%l0(e), wl2(1), wl2(2), & model%added_mass_rho, model%added_mass_diameter_elem(e), & model%added_mass_can_elem(e), model%added_mass_cat_elem(e), & - m2, ErrStat, ErrMsg) + m2, ErrStat, ErrMsg, inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN ma2 = CD_ZERO ELSE CALL CD_Added_Mass_Element(q2(1:3), q2(4:6), a2(1:3), a2(4:6), model%l0(e), wl2(1), wl2(2), & model%added_mass_rho, model%added_mass_diameter_elem(e), & model%added_mass_can_elem(e), model%added_mass_cat_elem(e), & - m2, dm2, ErrStat, ErrMsg) + m2, dm2, ErrStat, ErrMsg, inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN ma2 = MATMUL(m2, a2) END IF @@ -6779,7 +6792,7 @@ SUBROUTINE model_added_mass_banded(model, q, accel, free, kl, ku, M_add_a, M_add CALL CD_Added_Mass_Element_Force(q2(1:3), q2(4:6), a2(1:3), a2(4:6), model%l0(e), wl2(1), wl2(2), & model%added_mass_rho, model%added_mass_diameter_elem(e), & model%added_mass_can_elem(e), model%added_mass_cat_elem(e), & - ma2, ErrStat, ErrMsg) + ma2, ErrStat, ErrMsg, inputs_validated=.TRUE.) IF (ErrStat /= 0) RETURN END IF CALL scatter_element_vector(M_add_a, idx, ma2) diff --git a/src/CableDyn_System.f90 b/src/CableDyn_System.f90 index 65c2c140..aceed8f4 100644 --- a/src/CableDyn_System.f90 +++ b/src/CableDyn_System.f90 @@ -3158,8 +3158,8 @@ SUBROUTINE CD_System_Restore(system, ErrStat, ErrMsg) IF (ALLOCATED(system%lines(i)%f_ext_dist_base)) & system%lines(i)%f_ext_dist_base = system%snap_fbase(lo:hi) IF (MAXVAL(ABS(system%lines(i)%l0 - system%snap_l0(le:he))) > CD_ZERO) THEN - ! The consistent mass follows l0 implicitly (never stored); only the - ! length-dependent seabed coefficients need a rebuild. + ! The consistent mass follows l0 implicitly (the step rebuilds its mass band when the + ! lengths change); only the length-dependent seabed coefficients need a rebuild. system%lines(i)%l0 = system%snap_l0(le:he) IF (system%lines(i)%has_seabed .AND. system%lines(i)%has_seabed_recompute) THEN CALL CD_Nodal_Seabed_Stiffness(system%lines(i)%seabed_kbot, & diff --git a/tests/test_cable_dynamic.f90 b/tests/test_cable_dynamic.f90 index 12478a2c..b38de153 100644 --- a/tests/test_cable_dynamic.f90 +++ b/tests/test_cable_dynamic.f90 @@ -39,6 +39,7 @@ PROGRAM test_cable_dynamic CALL case_dynamic_load_callback_matches_fext() CALL case_banded_load_callback_matches_dense() CALL case_workspace_matches_legacy_step() + CALL case_workspace_mass_band_follows_inputs() CALL case_modified_newton_reuses_tangent() CALL case_line_search_fallback_handles_bad_tangent() CALL case_added_mass_callback_changes_initial_acceleration() @@ -46,6 +47,7 @@ PROGRAM test_cable_dynamic CALL case_preassembled_mass_rejects_offband_entries() CALL case_banded_added_mass_honors_structural_mass() CALL case_banded_dense_callback_mix_rejected() + CALL case_junction_tangent_after_allowance_exit() CALL case_fixed_dofs_held() CALL case_fail_closed() CALL case_failure_returns_input_state() @@ -369,6 +371,87 @@ SUBROUTINE case_workspace_matches_legacy_step() END DO END SUBROUTINE case_workspace_matches_legacy_step + SUBROUTINE case_workspace_mass_band_follows_inputs() + !! A reusable workspace keeps the element mass band of its last step and reuses it while + !! the connectivity, free list, l0 and rho_a are unchanged. A change of any of them, or an + !! initial-acceleration solve (which factors the band in place), rebuilds it: every step + !! equals the same step through a fresh workspace bit for bit. + INTEGER :: conn(2, 2), es + REAL(wp) :: l0(2), ea(2), rho_a(2), q0(9), f_ext(9), q(9), v(9), a(9), l0b(2), rhob(2) + TYPE(CD_CableGenAlphaWorkspace) :: work + CHARACTER(120) :: em + + CALL three_node_chain(conn, l0, ea, rho_a, q0) + f_ext = 0.0_wp + f_ext(6) = -15.0_wp + q = q0 + q(6) = -0.04_wp + v = 0.0_wp + v(6) = -0.05_wp + a = 0.0_wp + a(5) = 0.2_wp + l0b = [0.9_wp, 0.93_wp] + rhob = [5.0_wp, 6.5_wp] + + ! each step states whether it must reuse the remembered band (the workspace's test-only + ! hit counter), so a cache that never hits fails here + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3, 7, 8, 9], .FALSE., & + 'mass-band:first') + CALL require(work%mb_valid, 'mass-band:remembered') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3, 7, 8, 9], .TRUE., & + 'mass-band:reused') + CALL mass_band_step_pair(work, q, v, a, conn, l0b, ea, rho_a, f_ext, [1, 2, 3, 7, 8, 9], .FALSE., & + 'mass-band:new-l0') + CALL mass_band_step_pair(work, q, v, a, conn, l0b, ea, rhob, f_ext, [1, 2, 3, 7, 8, 9], .FALSE., & + 'mass-band:new-rho') + CALL mass_band_step_pair(work, q, v, a, conn, l0b, ea, rhob, f_ext, [1, 2, 3, 7, 8, 9], .TRUE., & + 'mass-band:new-rho-reused') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3, 7, 8, 9], .FALSE., & + 'mass-band:back') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3], .FALSE., & + 'mass-band:new-free-list') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3], .TRUE., & + 'mass-band:new-free-list-reused') + CALL CD_Cable_Initial_Acceleration(q, v, conn, l0, ea, rho_a, .FALSE., f_ext, [1, 2, 3], & + a, es, em, workspace=work) + CALL expect_es(es, CD_DYN_OK, 'mass-band:initial-acceleration') + CALL require(.NOT. work%mb_valid, 'mass-band:initial-acceleration-clears') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3], .FALSE., & + 'mass-band:after-initial-acceleration') + CALL mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, [1, 2, 3], .TRUE., & + 'mass-band:after-initial-acceleration-reused') + END SUBROUTINE case_workspace_mass_band_follows_inputs + + SUBROUTINE mass_band_step_pair(work, q, v, a, conn, l0, ea, rho_a, f_ext, fixed, expect_hit, label) + !! One step through the shared workspace and the same step through a fresh one; the shared + !! step reuses the remembered mass band exactly when expect_hit (test-only counter mb_hits). + TYPE(CD_CableGenAlphaWorkspace), INTENT(INOUT) :: work + REAL(wp), INTENT(IN) :: q(9), v(9), a(9), l0(2), ea(2), rho_a(2), f_ext(9) + INTEGER, INTENT(IN) :: conn(2, 2), fixed(:) + LOGICAL, INTENT(IN) :: expect_hit + CHARACTER(*), INTENT(IN) :: label + INTEGER, PARAMETER :: I8 = SELECTED_INT_KIND(18) + TYPE(GenAlphaConfig) :: cfg + TYPE(CD_CableGenAlphaWorkspace) :: fresh + REAL(wp) :: q_w(9), v_w(9), a_w(9), q_f(9), v_f(9), a_f(9) + INTEGER :: es_w, es_f, it_w, it_f, hits + LOGICAL :: conv_w, conv_f, stall_w, stall_f + CHARACTER(120) :: em_w, em_f + + hits = work%mb_hits + CALL CD_Cable_Gen_Alpha_Step(q, v, a, conn, l0, ea, rho_a, .FALSE., f_ext, fixed, 0.01_wp, cfg, & + q_w, v_w, a_w, conv_w, stall_w, it_w, es_w, em_w, workspace=work) + CALL require(work%mb_hits == hits + MERGE(1, 0, expect_hit), label//':cache-hit') + CALL CD_Cable_Gen_Alpha_Step(q, v, a, conn, l0, ea, rho_a, .FALSE., f_ext, fixed, 0.01_wp, cfg, & + q_f, v_f, a_f, conv_f, stall_f, it_f, es_f, em_f, workspace=fresh) + CALL require(fresh%mb_hits == 0, label//':fresh-no-hit') + CALL expect_es(es_w, CD_DYN_OK, label//':status') + CALL require(conv_w .AND. conv_f .AND. it_w == it_f .AND. es_w == es_f, label//':flags') + CALL require(ALL(TRANSFER(q_w, [0_I8]) == TRANSFER(q_f, [0_I8])) .AND. & + ALL(TRANSFER(v_w, [0_I8]) == TRANSFER(v_f, [0_I8])) .AND. & + ALL(TRANSFER(a_w, [0_I8]) == TRANSFER(a_f, [0_I8])), label//':bitwise') + END SUBROUTINE mass_band_step_pair + SUBROUTINE case_modified_newton_reuses_tangent() !! The EI=0 generalized-alpha config advertises modified Newton. It must reuse !! the tangent within a step without changing the accepted state. @@ -820,6 +903,106 @@ SUBROUTINE case_banded_dense_callback_mix_rejected() CALL expect_es(es, CD_DYN_BADINPUT, 'mix:init-load+amband-rejected') END SUBROUTINE case_banded_dense_callback_mix_rejected + SUBROUTINE case_junction_tangent_after_allowance_exit() + !! A step accepted by the near-tolerance allowance at the iteration limit exits right after + !! a fresh tangent evaluation, which overwrites the factored effective band with the + !! unfactored tangent. The junction end query must not back-solve with it as if it were + !! the last LU factorization: its condensed end tangent and end map must agree with those + !! the same converged iterate gets through an ordinary exit (whose factor is a valid LU). + INTEGER, PARAMETER :: I8 = SELECTED_INT_KIND(18) + INTEGER :: conn(2, 3), n_iter, k + REAL(wp) :: l0(3), ea(3), rho_a(3), f_ext(12), q(12), v(12) + REAL(wp) :: q_b(12), q_r(12), rel_tol, kb(6, 6), kr(6, 6), xb(6, 6), xr(6, 6), err_k, err_x + LOGICAL :: converged, found, valid + + ! two free nodes between held ends, so the effective tangent couples the free nodes + conn = RESHAPE([1, 2, 2, 3, 3, 4], [2, 3]) + l0 = 0.9_wp + ea = 1000.0_wp + rho_a = 5.0_wp + q = [0.0_wp, 0.0_wp, 0.0_wp, 1.0_wp, 0.0_wp, -0.1_wp, 2.0_wp, 0.0_wp, -0.1_wp, 3.0_wp, 0.0_wp, 0.0_wp] + f_ext = 0.0_wp + f_ext(6) = -15.0_wp + v = 0.0_wp + v(6) = -0.005_wp + callback_kq = 20.0_wp + callback_cv = 3.0_wp + ! a tolerance for which the single allowed Newton iteration ends between rel_tol and the + ! allowance 2 rel_tol: the step converges at the limit after a second, fresh tangent + found = .FALSE. + DO k = 0, 40 + rel_tol = 7.0e-9_wp*10.0_wp**(-0.25_wp*REAL(k, wp)) + CALL junction_query_step(conn, l0, ea, rho_a, f_ext, q, v, rel_tol, q_b, converged, n_iter, kb, xb, valid) + IF (converged .AND. n_iter == 1 .AND. callback_tangent_count >= 2) THEN + found = .TRUE. + EXIT + END IF + END DO + CALL require(found, 'allowance-exit:path-taken') + IF (.NOT. found) RETURN + CALL require(valid, 'allowance-exit:end-query-valid') + ! the same iterate through an ordinary exit: the allowance as the tolerance + CALL junction_query_step(conn, l0, ea, rho_a, f_ext, q, v, 2.0_wp*rel_tol, q_r, converged, n_iter, kr, xr, valid) + CALL require(converged .AND. valid .AND. callback_tangent_count == 1, 'allowance-exit:reference-ordinary-exit') + CALL require(ALL(TRANSFER(q_b, [0_I8]) == TRANSFER(q_r, [0_I8])), 'allowance-exit:same-iterate') + err_k = nan_max_abs(kb - kr)/nan_max_abs(kr) + err_x = nan_max_abs(xb - xr)/nan_max_abs(xr) + IF (.NOT. (err_k <= 1.0e-6_wp .AND. err_x <= 1.0e-6_wp)) & + WRITE (*, '(A,ES10.3,A,ES10.3)') 'allowance-exit: relative end-tangent error ', err_k, & + ', end-map error ', err_x + CALL require(err_k <= 1.0e-6_wp, 'allowance-exit:end-tangent-matches-ordinary-exit') + CALL require(err_x <= 1.0e-6_wp, 'allowance-exit:end-map-matches-ordinary-exit') + END SUBROUTINE case_junction_tangent_after_allowance_exit + + SUBROUTINE junction_query_step(conn, l0, ea, rho_a, f_ext, q, v, rel_tol, q_new, converged, n_iter, & + end_k, end_x, valid) + !! One step of the four-node line (ends held) with the junction end query armed, one Newton + !! iteration allowed and a counted banded load, through a fresh workspace. + INTEGER, INTENT(IN) :: conn(2, 3) + REAL(wp), INTENT(IN) :: l0(3), ea(3), rho_a(3), f_ext(12), q(12), v(12), rel_tol + REAL(wp), INTENT(OUT) :: q_new(12), end_k(6, 6), end_x(6, 6) + LOGICAL, INTENT(OUT) :: converged, valid + INTEGER, INTENT(OUT) :: n_iter + TYPE(CD_CableGenAlphaWorkspace) :: work + TYPE(GenAlphaConfig) :: cfg + REAL(wp) :: a(12), v_new(12), a_new(12) + INTEGER :: es + LOGICAL :: stalled + CHARACTER(120) :: em + + ! a consistent start: the predictor is then close to the step solution + CALL CD_Cable_Initial_Acceleration(q, v, conn, l0, ea, rho_a, .FALSE., f_ext, [1, 2, 3, 10, 11, 12], a, es, em, & + load_force_proc=linear_force_only) + CALL expect_es(es, CD_DYN_OK, 'allowance-exit:initial-acceleration') + cfg%rel_tol = rel_tol + cfg%abs_tol = TINY(1.0_wp) + cfg%max_iter = 1 + work%end_request = .TRUE. + callback_tangent_count = 0 + CALL CD_Cable_Gen_Alpha_Step(q, v, a, conn, l0, ea, rho_a, .FALSE., f_ext, [1, 2, 3, 10, 11, 12], 1.0e-3_wp, & + cfg, q_new, v_new, a_new, converged, stalled, n_iter, es, em, & + load_force_proc=linear_force_only, load_band_proc=counting_banded_load, & + workspace=work) + CALL expect_es(es, CD_DYN_OK, 'allowance-exit:step') + end_k = 0.0_wp + end_x = 0.0_wp + valid = work%end_valid + IF (valid) THEN + end_k = work%end_k + end_x = work%end_x + END IF + END SUBROUTINE junction_query_step + + SUBROUTINE counting_banded_load(q_in, v_in, free, kl, ku, force, jac_q_band, jac_v_band, ErrStat, ErrMsg) + !! banded_linear_load, counting the tangent evaluations. + REAL(wp), INTENT(IN) :: q_in(:), v_in(:) + INTEGER, INTENT(IN) :: free(:), kl, ku + REAL(wp), INTENT(OUT) :: force(:), jac_q_band(:, :), jac_v_band(:, :) + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + callback_tangent_count = callback_tangent_count + 1 + CALL banded_linear_load(q_in, v_in, free, kl, ku, force, jac_q_band, jac_v_band, ErrStat, ErrMsg) + END SUBROUTINE counting_banded_load SUBROUTINE case_fixed_dofs_held() !! Fixed DOFs are returned bit-exactly at their q0 value with zero velocity and !! acceleration, even while the free interior node accelerates under gravity. diff --git a/tests/test_damping.f90 b/tests/test_damping.f90 index 3893a850..2de3cb0f 100644 --- a/tests/test_damping.f90 +++ b/tests/test_damping.f90 @@ -101,6 +101,33 @@ SUBROUTINE case_element_kernel_matches_cable_path() force_only, es, em) CALL require(es == CD_DAMP_OK, 'element:force-ok') CALL require(nan_max_abs(force_only - force_ref) < 1.0e-9_wp, 'element:force-only-match') + + ! inputs_validated (the model hot path) skips only the input scan: the outputs, status and + ! message equal the default call's bit for bit, for a regular and a collapsed element + BLOCK + INTEGER, PARAMETER :: I8 = SELECTED_INT_KIND(18) + REAL(wp) :: f2(6), jq2(6, 6), jv2(6, 6) + INTEGER :: es2, icase + CHARACTER(160) :: em2 + DO icase = 1, 2 + IF (icase == 2) q(4:6) = q(1:3) + CALL CD_Axial_Damping_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0(1), ba(1), & + force_elem, jq_elem, jv_elem, es, em, l0_dot=0.3_wp) + CALL CD_Axial_Damping_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0(1), ba(1), & + f2, jq2, jv2, es2, em2, l0_dot=0.3_wp, inputs_validated=.TRUE.) + CALL require(ALL(TRANSFER(force_elem, [0_I8]) == TRANSFER(f2, [0_I8])) .AND. & + ALL(TRANSFER(jq_elem, [0_I8]) == TRANSFER(jq2, [0_I8])) .AND. & + ALL(TRANSFER(jv_elem, [0_I8]) == TRANSFER(jv2, [0_I8])) .AND. & + es == es2 .AND. em == em2, 'element:validated-load-bitwise') + CALL CD_Axial_Damping_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0(1), ba(1), & + force_only, es, em, l0_dot=0.3_wp) + CALL CD_Axial_Damping_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0(1), ba(1), & + f2, es2, em2, l0_dot=0.3_wp, inputs_validated=.TRUE.) + CALL require(ALL(TRANSFER(force_only, [0_I8]) == TRANSFER(f2, [0_I8])) .AND. & + es == es2 .AND. em == em2, 'element:validated-force-bitwise') + END DO + CALL require(es2 == CD_DAMP_BADINPUT, 'element:validated-collapsed-fails') + END BLOCK END SUBROUTINE case_element_kernel_matches_cable_path SUBROUTINE case_signed_tension() diff --git a/tests/test_hydro.f90 b/tests/test_hydro.f90 index 8cb6ad1b..1c5a4fd1 100644 --- a/tests/test_hydro.f90 +++ b/tests/test_hydro.f90 @@ -42,6 +42,7 @@ PROGRAM test_hydro CALL test_added_mass_element_matches_cable_path() CALL test_added_mass_element_matrix_matches_cable_path() CALL test_cable_added_mass_derivative_matches_fd() + CALL test_element_kernels_validated_path() CALL test_added_mass_and_fk() CALL test_current_profile() CALL test_seastate_steady_current() @@ -758,6 +759,133 @@ SUBROUTINE reference_profile_ratios(k, depth, z_eval, cosh_ratio, sinh_ratio) sinh_ratio = (e_kz - e_neg)/denom END SUBROUTINE reference_profile_ratios + SUBROUTINE test_element_kernels_validated_path() + !! inputs_validated (the model hot path) skips only the per-element input scan: on valid + !! inputs every output, status and message of each element kernel is bit-for-bit the + !! default call's, including the failures that remain possible -- a collapsed element and + !! finite end nodes whose chord length overflows (the tangent check of the Froude-Krylov + !! and added-mass per-length forms). + CHARACTER(*), PARAMETER :: NO_DIRECTION = 'CableDyn_Hydro: tangent must be a non-zero direction' + INTEGER :: icase, es1, es2 + REAL(wp) :: q(6), v(6), accel(3, 2), fluid(3, 2), acc6(6), wl(2), l0 + REAL(wp) :: f1(6), f2(6), jq1(6, 6), jq2(6, 6), jv1(6, 6), jv2(6, 6) + CHARACTER(160) :: em1, em2 + CHARACTER(40) :: label + + fluid(:, 1) = [0.8_wp, 0.1_wp, -0.2_wp] + fluid(:, 2) = [0.3_wp, 0.6_wp, 0.1_wp] + accel(:, 1) = [0.5_wp, -0.2_wp, 0.4_wp] + accel(:, 2) = [-0.1_wp, 0.6_wp, 0.2_wp] + acc6 = [0.3_wp, -0.1_wp, 0.2_wp, -0.4_wp, 0.5_wp, -0.2_wp] + v = [0.14_wp, -0.23_wp, 0.04_wp, -0.07_wp, 0.19_wp, -0.11_wp] + wl = [0.05_wp, -0.02_wp] + l0 = 1.7_wp + ! case 4 overflows the chord length on purpose: must not halt a trapping build + CALL IEEE_GET_HALTING_MODE(IEEE_USUAL, fp_halt) + CALL IEEE_SET_HALTING_MODE(IEEE_USUAL, .FALSE.) + DO icase = 1, 4 + SELECT CASE (icase) + CASE (1) ! surface-piercing + q = [-0.2_wp, 0.1_wp, -0.8_wp, 1.4_wp, -0.3_wp, 0.2_wp] + CASE (2) ! fully wet + q = [0.1_wp, -0.2_wp, -2.6_wp, 1.2_wp, 0.4_wp, -1.25_wp] + CASE (3) ! collapsed + q = [0.3_wp, 0.3_wp, -1.0_wp, 0.3_wp, 0.3_wp, -1.0_wp] + CASE DEFAULT ! finite ends, overflowing chord length + q = [-1.0e300_wp, 0.0_wp, -1.0_wp, 1.0e300_wp, 0.0_wp, -1.0_wp] + END SELECT + WRITE (label, '(A,I0,A)') 'validated-path case ', icase, ': ' + + CALL CD_Morison_Drag_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0, fluid(:, 1), fluid(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.05_wp, 0.32_wp, f1, jq1, jv1, es1, em1) + CALL CD_Morison_Drag_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0, fluid(:, 1), fluid(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.05_wp, 0.32_wp, f2, jq2, jv2, es2, em2, & + inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. same_bits(jq1, jq2) .AND. same_bits(jv1, jv2) .AND. & + es1 == es2 .AND. em1 == em2, TRIM(label)//'drag load') + CALL CD_Morison_Drag_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0, fluid(:, 1), fluid(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.05_wp, 0.32_wp, f1, es1, em1) + CALL CD_Morison_Drag_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0, fluid(:, 1), fluid(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.05_wp, 0.32_wp, f2, es2, em2, & + inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. es1 == es2 .AND. em1 == em2, TRIM(label)//'drag force') + + CALL CD_Froude_Krylov_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0, accel(:, 1), accel(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f1, jq1, jv1, es1, em1) + CALL CD_Froude_Krylov_Element_Load(q(1:3), q(4:6), v(1:3), v(4:6), l0, accel(:, 1), accel(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f2, jq2, jv2, es2, em2, & + inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. same_bits(jq1, jq2) .AND. same_bits(jv1, jv2) .AND. & + es1 == es2 .AND. em1 == em2, TRIM(label)//'FK load') + CALL require(nan_max_abs(jv2) <= CD_ZERO, TRIM(label)//'FK load has no velocity Jacobian') + CALL CD_Froude_Krylov_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0, accel(:, 1), accel(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f1, es1, em1) + CALL CD_Froude_Krylov_Element_Force(q(1:3), q(4:6), v(1:3), v(4:6), l0, accel(:, 1), accel(:, 2), & + wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f2, es2, em2, & + inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. es1 == es2 .AND. em1 == em2, TRIM(label)//'FK force') + IF (icase == 4) CALL require(es2 == CD_HYDRO_BADINPUT .AND. em2 == NO_DIRECTION, & + TRIM(label)//'FK force tangent check') + + CALL CD_Buoyancy_Recovery_Element_Load(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 9.80665_wp, & + f1, jq1, jv1, es1, em1) + CALL CD_Buoyancy_Recovery_Element_Load(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 9.80665_wp, & + f2, jq2, jv2, es2, em2, inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. same_bits(jq1, jq2) .AND. same_bits(jv1, jv2) .AND. & + es1 == es2 .AND. em1 == em2, TRIM(label)//'buoyancy load') + CALL require(nan_max_abs(jv2) <= CD_ZERO, TRIM(label)//'buoyancy load has no velocity Jacobian') + CALL CD_Buoyancy_Recovery_Element_Force(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 9.80665_wp, & + f1, es1, em1) + CALL CD_Buoyancy_Recovery_Element_Force(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 9.80665_wp, & + f2, es2, em2, inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. es1 == es2 .AND. em1 == em2, TRIM(label)//'buoyancy force') + + CALL CD_Added_Mass_Element(q(1:3), q(4:6), acc6(1:3), acc6(4:6), l0, wl(1), wl(2), & + 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, jq1, jv1, es1, em1) + CALL CD_Added_Mass_Element(q(1:3), q(4:6), acc6(1:3), acc6(4:6), l0, wl(1), wl(2), & + 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, jq2, jv2, es2, em2, inputs_validated=.TRUE.) + CALL require(same_bits(jq1, jq2) .AND. same_bits(jv1, jv2) .AND. es1 == es2 .AND. em1 == em2, & + TRIM(label)//'added mass') + CALL CD_Added_Mass_Element_Matrix(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, & + jq1, es1, em1) + CALL CD_Added_Mass_Element_Matrix(q(1:3), q(4:6), l0, wl(1), wl(2), 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, & + jq2, es2, em2, inputs_validated=.TRUE.) + CALL require(same_bits(jq1, jq2) .AND. es1 == es2 .AND. em1 == em2, TRIM(label)//'added-mass matrix') + IF (icase == 4) CALL require(es2 == CD_HYDRO_BADINPUT .AND. em2 == NO_DIRECTION, & + TRIM(label)//'added-mass matrix tangent check') + CALL CD_Added_Mass_Element_Force(q(1:3), q(4:6), acc6(1:3), acc6(4:6), l0, wl(1), wl(2), & + 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f1, es1, em1) + CALL CD_Added_Mass_Element_Force(q(1:3), q(4:6), acc6(1:3), acc6(4:6), l0, wl(1), wl(2), & + 1025.0_wp, 0.24_wp, 1.1_wp, 0.2_wp, f2, es2, em2, inputs_validated=.TRUE.) + CALL require(same_bits(f1, f2) .AND. es1 == es2 .AND. em1 == em2, TRIM(label)//'added-mass force') + IF (icase == 4) CALL require(es2 == CD_HYDRO_BADINPUT .AND. em2 == NO_DIRECTION, & + TRIM(label)//'added-mass force tangent check') + END DO + CALL IEEE_SET_FLAG(IEEE_USUAL, .FALSE.) + CALL IEEE_SET_HALTING_MODE(IEEE_USUAL, fp_halt) + END SUBROUTINE test_element_kernels_validated_path + + PURE LOGICAL FUNCTION same_bits(a, b) RESULT(same) + !! a and b hold the same binary64 values bit for bit (signed zeros and NaNs included). + REAL(wp), INTENT(IN) :: a(..), b(..) + INTEGER, PARAMETER :: I8 = SELECTED_INT_KIND(18) + same = .FALSE. + SELECT RANK (a) + RANK (1) + SELECT RANK (b) + RANK (1) + same = SIZE(a) == SIZE(b) + IF (same) same = ALL(TRANSFER(a, [0_I8]) == TRANSFER(b, [0_I8])) + END SELECT + RANK (2) + SELECT RANK (b) + RANK (2) + same = SIZE(a) == SIZE(b) + IF (same) same = ALL(TRANSFER(a, [0_I8]) == TRANSFER(b, [0_I8])) + END SELECT + END SELECT + END FUNCTION same_bits + SUBROUTINE test_fail_closed() REAL(wp) :: force(3), jac_rel(3, 3), jac_tan(3, 3) INTEGER :: es