From 3b1a5c9b247b621dc5aaa285f5c898af82a6982d Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sun, 8 Nov 2026 19:18:22 +0900 Subject: [PATCH 1/2] perf(model): form the end-coupled loads of an EI=0 line from its end rows The coupled loads of an EI=0 line are the end rows of the residual M a + f_int - f_ext + M_add a - load. CD_Calc_Model_CoupledLoads formed the whole residual: the full structural mass product, the added-mass force and every load contributor of every element, to read the rows of the coupled DOFs. Every result stays bit-identical. - A line whose coupled DOFs all belong to its first or last node (a line between points), with finite accelerations, now forms the remaining terms on the two end rows only (coupled_loads_at_ends). It uses the elements that touch an end node and the end nodes' own terms (model_contributor_force end_nodes_only, as CD_Get_Model_EndForces does). Each row takes its terms in the element order of the whole-line assembly, so the loads are bit-identical. The whole-line internal force is kept, with its geometry and finiteness checks. A line with an interior coupled DOF, or a non-finite acceleration, takes the whole-line path as before, so it fails as before. - CD_Step_System and CD_Calc_System_CoupledLoads run their per-line loops without entering an OpenMP region when the team has one thread. The inactive region still set up a team on every call. The loop bodies are unchanged. - extract_element_state and extract_element_positions copy the two nodes' contiguous triples instead of gathering them through a vector subscript. All 54 examples give byte-identical output files on one thread (progress timing aside), and so do the line-break, mixed-body and two-train decks on the default thread count. The Rigid6 line-break decks als_volturnus_line_break_time and _tension run 7-8 % faster. Signed-off-by: Jae Hoon Seo --- CHANGELOG.md | 6 ++ src/CableDyn_Model.f90 | 131 +++++++++++++++++++++++++++++++++++++++- src/CableDyn_System.f90 | 98 ++++++++++++++++++++---------- 3 files changed, 200 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3549554..05ea66ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,12 @@ All notable changes to CableDyn are recorded in this file. The format follows to the touchdown point located from the two nodes' clearances: the error falls at the second order (2.1, 0.7, 0.2 N) and the outputs vary continuously. +### Changed + +- The coupled loads of an EI=0 line attached at its end nodes are formed from those nodes' rows + only, and a system stepped on one thread runs its per-line loops without entering an OpenMP + region. Results are bit-identical; a moored Rigid6 body on three chains runs about 7 % faster. + ## [0.4.1] - 2026-11-08 ### Added diff --git a/src/CableDyn_Model.f90 b/src/CableDyn_Model.f90 index 683b18b2..d6c14ca4 100644 --- a/src/CableDyn_Model.f90 +++ b/src/CableDyn_Model.f90 @@ -4428,6 +4428,14 @@ SUBROUTINE CD_Calc_Model_CoupledLoads(model, coupled_loads, ErrStat, ErrMsg) ErrMsg = 'CableDyn_Model: internal-force assembly failed during coupled-load calculation: '//TRIM(em) RETURN END IF + ! A line coupled at its end nodes only (a line between points): the remaining terms on the + ! end rows alone (coupled_loads_at_ends), bit-identical to the whole-line residual below. + ! The whole-line terms validate the accelerations element by element (the added-mass + ! kernels): with any non-finite one they are formed whole, to fail as they do. + IF (ALL(model%fixed_dofs <= 3 .OR. model%fixed_dofs > model%n_dof - 3) .AND. CD_All_Finite(model%a)) THEN + CALL coupled_loads_at_ends(model, fint, coupled_loads, ErrStat, ErrMsg) + RETURN + END IF CALL model_structural_mass_product(model, model%a, residual) residual = residual + fint - model%f_ext IF (model%has_added_mass) THEN @@ -4453,6 +4461,119 @@ SUBROUTINE CD_Calc_Model_CoupledLoads(model, coupled_loads, ErrStat, ErrMsg) END ASSOCIATE END SUBROUTINE CD_Calc_Model_CoupledLoads + SUBROUTINE coupled_loads_at_ends(model, fint, coupled_loads, ErrStat, ErrMsg) + !! The rest of CD_Calc_Model_CoupledLoads, given the whole-line internal force fint, for a + !! line whose coupled DOFs all belong to its first or last node and whose accelerations are + !! finite. Only the two end rows of M a + f_int - f_ext + M_add a - load are formed, from + !! the elements that touch an end node and the end nodes' own terms (model_contributor_force + !! end_nodes_only). Each row takes its terms in the element order of the whole-line + !! assembly and its operations in the same order, so the loads are bit-identical to it, as + !! in CD_Get_Model_EndForces; the elements that touch no end node, which add nothing to + !! these rows, are not evaluated. + TYPE(CD_ModelType), INTENT(IN) :: model + REAL(wp), INTENT(IN) :: fint(:) + REAL(wp), INTENT(OUT) :: coupled_loads(:) + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + + INTEGER :: n, nn, e, a, b, ia, ib, sa, sb, k, d, es, idx(6), lk + REAL(wp) :: inertia(3, 2), added(3, 2), residual(3, 2), load(9) + REAL(wp) :: coeff, q2(6), a2(6), wl2(2), ma2(6) + CHARACTER(1024) :: em + + ErrStat = CD_MODEL_OK + ErrMsg = '' + coupled_loads = CD_ZERO + lk = MIN(LEN(em), KERNEL_MSG_LEN) + n = model%n_dof + nn = n/3 + ! M a (model_structural_mass_product) on the end rows, from zero in element order + inertia = CD_ZERO + DO e = 1, model%n_elem + a = model%elem_conn(1, e) + b = model%elem_conn(2, e) + sa = end_slot(a) + sb = end_slot(b) + IF (sa == 0 .AND. sb == 0) CYCLE + ia = 3*a - 2 + ib = 3*b - 2 + coeff = model%rho_a(e)*model%l0(e)/6.0_wp + IF (sa /= 0) inertia(:, sa) = inertia(:, sa) + coeff*(2.0_wp*model%a(ia:ia + 2) + model%a(ib:ib + 2)) + IF (sb /= 0) inertia(:, sb) = inertia(:, sb) + coeff*(model%a(ia:ia + 2) + 2.0_wp*model%a(ib:ib + 2)) + END DO + DO k = 1, 2 + residual(:, k) = inertia(:, k) + fint(end_base(k) + 1:end_base(k) + 3) - & + model%f_ext(end_base(k) + 1:end_base(k) + 3) + END DO + ! M_add a (model_added_mass_force), from zero in element order + IF (model%has_added_mass) THEN + added = CD_ZERO + DO e = 1, model%n_elem + sa = end_slot(model%elem_conn(1, e)) + sb = end_slot(model%elem_conn(2, e)) + IF (sa == 0 .AND. sb == 0) CYCLE + CALL extract_element_positions(model, e, model%q, idx, q2) + a2 = model%a(idx) + CALL extract_nodal_scalar(model%added_mass_waterline_z, model, e, wl2) + em = '' + 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, es, em(1:lk)) + IF (es /= 0) THEN + ErrStat = CD_MODEL_SOLVEFAIL + ErrMsg = 'CableDyn_Model: added-mass assembly failed during coupled-load calculation: '//TRIM(em) + RETURN + END IF + ! the far node's share stays finite-checked as in the whole-line force + IF (.NOT. CD_All_Finite(ma2)) THEN + CALL fail(es, em, 'added-mass force returned non-finite values') + ErrStat = CD_MODEL_SOLVEFAIL + ErrMsg = 'CableDyn_Model: added-mass assembly failed during coupled-load calculation: '//TRIM(em) + RETURN + END IF + IF (sa /= 0) added(:, sa) = added(:, sa) + ma2(1:3) + IF (sb /= 0) added(:, sb) = added(:, sb) + ma2(4:6) + END DO + residual = residual + added + END IF + CALL model_contributor_force(model, model%q, model%v, load, es, em, end_nodes_only=.TRUE.) + IF (es /= 0) THEN + ErrStat = CD_MODEL_SOLVEFAIL + ErrMsg = 'CableDyn_Model: load assembly failed during coupled-load calculation: '//TRIM(em) + RETURN + END IF + residual(:, 1) = residual(:, 1) - load(1:3) + residual(:, 2) = residual(:, 2) - load(4:6) + DO k = 1, SIZE(model%fixed_dofs) + d = model%fixed_dofs(k) + IF (d <= 3) THEN + coupled_loads(k) = -residual(d, 1) + ELSE + coupled_loads(k) = -residual(d - end_base(2), 2) + END IF + END DO + + CONTAINS + + PURE INTEGER FUNCTION end_slot(node) RESULT(s) + !! 1 for the first node, 2 for the last, 0 for an interior node. + INTEGER, INTENT(IN) :: node + s = 0 + IF (node == 1) THEN + s = 1 + ELSE IF (node == nn) THEN + s = 2 + END IF + END FUNCTION end_slot + + PURE INTEGER FUNCTION end_base(s) RESULT(base) + !! DOF offset of end slot s. + INTEGER, INTENT(IN) :: s + base = MERGE(0, n - 3, s == 1) + END FUNCTION end_base + END SUBROUTINE coupled_loads_at_ends + SUBROUTINE CD_Calc_Model_CoupledKinematicDerivatives(model, dload_dq, dload_dv, ErrStat, ErrMsg) !! Analytic reduced coupled-load derivatives with respect to prescribed !! coupled positions and velocities. Free DOFs are statically condensed with @@ -9106,8 +9227,11 @@ PURE SUBROUTINE extract_element_state(model, e, q, v, idx, q2, v2) a = model%elem_conn(1, e) b = model%elem_conn(2, e) idx = [3*a - 2, 3*a - 1, 3*a, 3*b - 2, 3*b - 1, 3*b] - q2 = q(idx) - v2 = v(idx) + ! the two nodes' contiguous triples (the copies q(idx), v(idx) without a vector subscript) + q2(1:3) = q(3*a - 2:3*a) + q2(4:6) = q(3*b - 2:3*b) + v2(1:3) = v(3*a - 2:3*a) + v2(4:6) = v(3*b - 2:3*b) END SUBROUTINE extract_element_state PURE SUBROUTINE extract_element_positions(model, e, q, idx, q2) @@ -9122,7 +9246,8 @@ PURE SUBROUTINE extract_element_positions(model, e, q, idx, q2) a = model%elem_conn(1, e) b = model%elem_conn(2, e) idx = [3*a - 2, 3*a - 1, 3*a, 3*b - 2, 3*b - 1, 3*b] - q2 = q(idx) + q2(1:3) = q(3*a - 2:3*a) + q2(4:6) = q(3*b - 2:3*b) END SUBROUTINE extract_element_positions PURE SUBROUTINE extract_nodal_vector(field, model, e, field2) diff --git a/src/CableDyn_System.f90 b/src/CableDyn_System.f90 index 6ca7fc81..272563b8 100644 --- a/src/CableDyn_System.f90 +++ b/src/CableDyn_System.f90 @@ -1493,27 +1493,24 @@ RECURSIVE SUBROUTINE CD_Step_System(system, dt, q_coupled, v_coupled, a_coupled, ! Line models own disjoint state and Newton workspaces. Workers stage results; ! the following serial pass selects the first error and reduces flags in deck order. n_team = line_team_size(system) - !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(i) NUM_THREADS(n_team) IF(n_team > 1) - CALL CD_Fatal_Thread_Init() ! this thread can report its own stack overflow - ! DYNAMIC: a line that is sub-stepped (CD_Step_Model) costs several of its plain steps, so the - ! lines take unequal times; each line's result does not depend on the thread that steps it - !$OMP DO SCHEDULE(DYNAMIC) - DO i = 1, system%n_lines - ! the halves of a stall recovery (0 < depth < MAX_SUBDIV) take their steps as given, as the - ! rungs of CD_Step_Model_Recovering do: no rotation- and snap-resolved sub-steps of - ! sub-steps. The plain full-dt rerun at the depth floor (depth = MAX_SUBDIV) steps every line - ! as the nominal step (depth 0) does, so its result is that of the step without the - ! fallback; the junction step (JUNCTION_NO_SUBDIV = MAX_SUBDIV) likewise, its armed lines - ! being taken as given by CD_Step_Model itself - CALL CD_Step_Model(system%lines(i), dt, system%line_converged(i), system%line_stalled(i), & - system%line_iter(i), system%line_stat(i), system%line_msg(i), & - prescribed_q=system%lines(i)%q_prescribed_work, & - prescribed_v=system%lines(i)%v_prescribed_work, & - prescribed_a=system%lines(i)%a_prescribed_work, & - as_given=depth > 0 .AND. depth < MAX_SUBDIV) - END DO - !$OMP END DO - !$OMP END PARALLEL + IF (n_team > 1) THEN + !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(i) NUM_THREADS(n_team) + CALL CD_Fatal_Thread_Init() ! this thread can report its own stack overflow + ! DYNAMIC: a line that is sub-stepped (CD_Step_Model) costs several of its plain steps, so the + ! lines take unequal times; each line's result does not depend on the thread that steps it + !$OMP DO SCHEDULE(DYNAMIC) + DO i = 1, system%n_lines + CALL step_line(i) + END DO + !$OMP END DO + !$OMP END PARALLEL + ELSE + ! one thread: the same loop without entering a parallel region + CALL CD_Fatal_Thread_Init() + DO i = 1, system%n_lines + CALL step_line(i) + END DO + END IF DO i = 1, system%n_lines IF (system%line_stat(i) /= CD_MODEL_OK) THEN CALL restore_failed_step(system, system%line_stat(i), 'line step failed: '//TRIM(system%line_msg(i)), & @@ -1676,6 +1673,25 @@ RECURSIVE SUBROUTINE CD_Step_System(system, dt, q_coupled, v_coupled, a_coupled, END IF ErrStat = CD_SYSTEM_OK ErrMsg = '' + + CONTAINS + + SUBROUTINE step_line(il) + !! Step line il with its staged prescribed motion. The halves of a stall recovery + !! (0 < depth < MAX_SUBDIV) take their steps as given, as the rungs of + !! CD_Step_Model_Recovering do: no rotation- and snap-resolved sub-steps of sub-steps. The + !! plain full-dt rerun at the depth floor (depth = MAX_SUBDIV) steps every line as the + !! nominal step (depth 0) does, so its result is that of the step without the fallback; the + !! junction step (JUNCTION_NO_SUBDIV = MAX_SUBDIV) likewise, its armed lines being taken as + !! given by CD_Step_Model itself. + INTEGER, INTENT(IN) :: il + CALL CD_Step_Model(system%lines(il), dt, system%line_converged(il), system%line_stalled(il), & + system%line_iter(il), system%line_stat(il), system%line_msg(il), & + prescribed_q=system%lines(il)%q_prescribed_work, & + prescribed_v=system%lines(il)%v_prescribed_work, & + prescribed_a=system%lines(il)%a_prescribed_work, & + as_given=depth > 0 .AND. depth < MAX_SUBDIV) + END SUBROUTINE step_line END SUBROUTINE CD_Step_System SUBROUTINE CD_Step_System_Junction(system, dt, q_coupled, v_coupled, a_coupled, active, warm, loads, kmat, & @@ -2432,17 +2448,23 @@ SUBROUTINE CD_Calc_System_CoupledLoads(system, coupled_loads, ErrStat, ErrMsg) RETURN END IF n_team = line_team_size(system) - !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(i, lo, hi) NUM_THREADS(n_team) IF(n_team > 1) - CALL CD_Fatal_Thread_Init() ! this thread can report its own stack overflow - !$OMP DO SCHEDULE(STATIC) - DO i = 1, system%n_lines - lo = system%line_off(i) - hi = system%line_off(i + 1) - 1 - CALL CD_Calc_Model_CoupledLoads(system%lines(i), system%line_load_work(lo:hi), & - system%line_stat(i), system%line_msg(i)) - END DO - !$OMP END DO - !$OMP END PARALLEL + IF (n_team > 1) THEN + !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(i) NUM_THREADS(n_team) + CALL CD_Fatal_Thread_Init() ! this thread can report its own stack overflow + !$OMP DO SCHEDULE(STATIC) + DO i = 1, system%n_lines + CALL line_coupled_loads(i) + END DO + !$OMP END DO + !$OMP END PARALLEL + ELSE + ! one thread: the same loop without entering a parallel region (whose team setup costs + ! more than a short line's loads) + CALL CD_Fatal_Thread_Init() + DO i = 1, system%n_lines + CALL line_coupled_loads(i) + END DO + END IF DO i = 1, system%n_lines IF (system%line_stat(i) /= CD_MODEL_OK) THEN ErrStat = CD_SYSTEM_SOLVEFAIL @@ -2453,6 +2475,18 @@ SUBROUTINE CD_Calc_System_CoupledLoads(system, coupled_loads, ErrStat, ErrMsg) hi = system%line_off(i + 1) - 1 CALL accumulate_mapped_loads(system, lo, hi, system%line_load_work(lo:hi), coupled_loads) END DO + + CONTAINS + + SUBROUTINE line_coupled_loads(il) + !! The coupled loads of line il into its slice of the per-line workspace. + INTEGER, INTENT(IN) :: il + INTEGER :: l_lo, l_hi + l_lo = system%line_off(il) + l_hi = system%line_off(il + 1) - 1 + CALL CD_Calc_Model_CoupledLoads(system%lines(il), system%line_load_work(l_lo:l_hi), & + system%line_stat(il), system%line_msg(il)) + END SUBROUTINE line_coupled_loads END SUBROUTINE CD_Calc_System_CoupledLoads INTEGER FUNCTION line_team_size(system) RESULT(n_team) From 7a5cf3cb2b4a5a907255161f50df6e2f17f7d30d Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sun, 8 Nov 2026 19:34:40 +0900 Subject: [PATCH 2/2] chore(changelog): the entry moves to the batch changelog change The [Unreleased] entries of the open changes are added together in one change, so that they do not conflict with each other. Signed-off-by: Jae Hoon Seo --- CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05ea66ef..e3549554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,12 +19,6 @@ All notable changes to CableDyn are recorded in this file. The format follows to the touchdown point located from the two nodes' clearances: the error falls at the second order (2.1, 0.7, 0.2 N) and the outputs vary continuously. -### Changed - -- The coupled loads of an EI=0 line attached at its end nodes are formed from those nodes' rows - only, and a system stepped on one thread runs its per-line loops without entering an OpenMP - region. Results are bit-identical; a moored Rigid6 body on three chains runs about 7 % faster. - ## [0.4.1] - 2026-11-08 ### Added