diff --git a/CHANGELOG.md b/CHANGELOG.md index a102a6f1..ba47391b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -432,6 +432,25 @@ All notable changes to CableDyn are recorded in this file. The format follows the statics matrix overflowed the 1 MiB stack of an MSVC-linked program. Every ifx build now compiles with `/heap-arrays:1024` (`-heap-arrays 1024` off Windows), as the static release and the OpenFAST Visual Studio project did; the static release is unchanged. +- A finite-EI cable on the coupled aggregate (a mixed `EI = 0` + finite-EI standalone deck, + OpenFAST `CompMooring = 5`, FAST.Farm and the C API) stepped to a fixed relative Newton + tolerance of 5e-3 (200 iterations) whatever the deck's `dynamic_solver` row said, and its static + start to 1e-6; the same cable alone in a deck stepped to the deck's tolerance (default 1e-8, 30 + iterations). Every route now takes the deck's `rel` and `max_iter` and the static tolerance + `min(1e-6, rel)`. A lazy cable beside a chain, both fairleads moved 3.5 m from rest: at + `rhoInf` 1, `dtM` 0.05 s the mixed route's fairlead tension peaked at 4.3e5 N (anchor 5.5e5 N) + against 1.9e4 N (2.1e3 N) for the cable alone; at `dtM` 0.1 s it fell to 8307 N against + 10799 N and the fairlead bend moment rose to 1612 against 3.9 N m. The routes, and the C API, + OpenFAST and FAST.Farm builds driven through the same motion, now agree to 5e-8 of the peak + tension. The loose steps also made the coupled cable non-dissipative: held at both ends in still + water, the residual each step left grew at `rhoInf` 0.4 into a sustained oscillation (+-2.1 N on + 1.5e4 N in 10 s on that cable; +-14 N at 8 Hz on 1.2e4 N on the IEA-15 MW mixed cable, also in + OpenFAST `CompMooring = 5`), and at `rhoInf` 1 a seeded march diverged; the held cable + now stays at its static tensions to 6e-5 N and a seeded one decays as on the single-line route. + On the default `rhoInf` 0.4 under motion the difference was 4.7e-4 of the anchor tension, and + the IEA-15 MW mixed example in a 60 s platform motion changes by 1.6e-6 of its cable tension. + Cost: a coupled cable step takes about 2.0 tangent factorisations instead of 1.1, and the + aggregate step of that example (chains included) 38 % more CPU time (the D03 deck: 19 %). - A rod numbered as a body no longer joins the body's attachment points. A `RodA/B` end point keeps its rod's id where a `Body` point keeps its body's, and the body's point list took every point with its id: a rod end POINT row listed before the body's points became the body's diff --git a/CMakeLists.txt b/CMakeLists.txt index de683382..7e462550 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1302,6 +1302,15 @@ target_link_libraries(test_c_api PRIVATE cabledyn_core) add_test(NAME c_api COMMAND test_c_api) set_tests_properties(c_api PROPERTIES LABELS "fortran;unit;dynamic;capi") +# One Newton tolerance for a finite-EI cable on every route: the mixed deck's cable (coupled +# aggregate) against the same cable alone (standalone cubic-Hermite route) at rhoInf 1 / dtM 0.05 +# and rhoInf 0.4 / dtM 0.1, and the C API, OpenFAST and FAST.Farm builds of the aggregate driven +# by a host through the same motion, against the driver. +add_executable(test_coupled_cable_tolerance tests/test_coupled_cable_tolerance.f90) +target_link_libraries(test_coupled_cable_tolerance PRIVATE cabledyn_core) +add_test(NAME coupled_cable_tolerance COMMAND test_coupled_cable_tolerance $) +set_tests_properties(coupled_cable_tolerance PROPERTIES LABELS "fortran;driver;integration;dynamic;capi;openfast") + # Compile and run through the installed-style C header against the SHARED ABI # product. This catches missing Windows exports, C/Fortran prototype drift, ABI # version drift, and loader failures that the Fortran-native lifecycle gate cannot. @@ -1513,9 +1522,12 @@ set_tests_properties(memory_growth_aggregate PROPERTIES LABELS "fortran;openfast # Zero heap allocations in the per-step aggregate advance after warm-up, counted through # GNU ld --wrap malloc/calloc/realloc wrappers (GNU toolchains only). if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_ID STREQUAL "GNU") - add_executable(test_step_allocations tests/test_step_allocations.f90 tests/allocation_counter.c) + add_executable(test_step_allocations tests/test_step_allocations.f90 tests/allocation_counter.c + tests/allocation_phase_counter.c) target_link_libraries(test_step_allocations PRIVATE cabledyn_core) - target_link_options(test_step_allocations PRIVATE "-Wl,--wrap=malloc,--wrap=calloc,--wrap=realloc") + # the phase window of a whole driver run (tests/allocation_phase_counter.c) wraps CD_Profile_Enter + target_link_options(test_step_allocations PRIVATE + "-Wl,--wrap=malloc,--wrap=calloc,--wrap=realloc,--wrap=__cabledyn_profile_MOD_cd_profile_enter") add_test(NAME step_allocations COMMAND test_step_allocations ${CMAKE_CURRENT_SOURCE_DIR}/examples/iea15mw_umaine_mixed_cabledyn.dat 200.0 200) @@ -1546,6 +1558,10 @@ if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_ID STREQUAL "GN # And with the nodal twist of a line free in torsion at one end. add_test(NAME step_allocations_torsion_one_end COMMAND test_step_allocations free) set_tests_properties(step_allocations_torsion_one_end PROPERTIES LABELS "fortran;dynamic;memory") + # The standalone driver's march of a finite-EI line on the cubic-Hermite route moved by a vessel, + # run in process: the step phase of every step after warm-up. + add_test(NAME step_allocations_driver_vessel COMMAND test_step_allocations driver_vessel) + set_tests_properties(step_allocations_driver_vessel PROPERTIES LABELS "fortran;driver;dynamic;memory") endif() add_executable(test_snap_load tests/test_snap_load.f90) diff --git a/doc/driver_format.md b/doc/driver_format.md index 1512b826..41d49e02 100644 --- a/doc/driver_format.md +++ b/doc/driver_format.md @@ -1276,7 +1276,7 @@ syntax form. The most frequently edited keywords are: | `ei0_substep_max` | integer from 2 to 1024; most sub-steps `auto` takes for one step | `16` | | `axial_quadrature_order` | Gauss order 1–6 for the finite-EI axial energy; a value different from the bending order gives selective integration | `4` | | `bending_quadrature_order` | Gauss order 1–6 for the finite-EI curvature energy | `4` | -| `dynamic_solver rel abs max_iter backtracks [rhoInf]` | dynamic Newton controls; keyword-first syntax, unlike scalar rows. `EI = 0` solves use all four; standalone finite-EI decks use `rel` and `max_iter` (coupled cables: see the OPTIONS reference). Choose them for the mesh and load case; the 952-element Gulf of Maine example uses `1e-4 1e-14 100 12` | `1e-8 1e-14 30 12` | +| `dynamic_solver rel abs max_iter backtracks [rhoInf]` | dynamic Newton controls; keyword-first syntax, unlike scalar rows. `EI = 0` solves use all four; finite-EI cables use `rel` and `max_iter` on every route (standalone, mixed, OpenFAST, FAST.Farm, C API). Choose them for the mesh and load case; the 952-element Gulf of Maine example uses `1e-4 1e-14 100 12` | `1e-8 1e-14 30 12` | | `current` | `none current` / `uniform vx vy vz current` / `profile z1 vx1 vy1 vz1 z2 vx2 vy2 vz2 current`; included in the static initial condition. Standalone only | none | | `waves` | `none waves` / `airy H T dir waves` / `stream H T dir waves` (`dean`; regular nonlinear stream-function wave) / `jonswap Hs Tp gamma dir waves` / `pm Hs Tp dir waves` (`issc`, `bretschneider`) / `torsethaugen Hs Tp dir waves` / `ochihubble Hs1 Tp1 lambda1 Hs2 Tp2 lambda2 dir waves`. Standalone only | none | | `wavetrain` | one train of a multi-train sea, rows add up (at most 16): `airy H T dir wavetrain`, `jonswap Hs Tp gamma dir s wavetrain`, `pm Hs Tp dir s wavetrain`, `torsethaugen Hs Tp dir s wavetrain`, `ochihubble Hs1 Tp1 lambda1 Hs2 Tp2 lambda2 dir s wavetrain`; `s` is the cos-2s spreading exponent (`0` long-crested). Excludes a `waves` row | none | diff --git a/doc/options.rst b/doc/options.rst index 2564dbd5..cc59b65b 100644 --- a/doc/options.rst +++ b/doc/options.rst @@ -338,13 +338,12 @@ rejected as unknown. The dynamic Newton controls are set with ``dynamic_solver`` - ``1e-8 1e-14 30 12`` - ``rel`` and ``abs`` finite and > 0; ``max_iter`` an integer ≥ 1; ``backtracks`` an integer ≥ 0; the optional sixth token sets ``rhoInf`` (same range). ``EI = 0`` dynamic solves - use all four controls. A standalone finite-EI (Hermite) deck uses only ``rel`` and - ``max_iter`` for each step (``abs`` and ``backtracks`` are ignored on this route) and - converges its static equilibrium to at least ``min(1e-6, rel)``. Finite-EI cables in - OpenFAST and in mixed ``EI = 0`` + finite-EI standalone decks do not read ``rel``, - ``abs``, ``max_iter`` or ``backtracks``: they use a fixed step tolerance (relative - ``5e-3``, 200 Newton iterations) and a ``1e-6`` static tolerance; only the optional - ``rhoInf`` token applies to them. Use controls qualified for + use all four controls. A finite-EI (Hermite) cable uses only ``rel`` and ``max_iter`` + for each step (``abs`` and ``backtracks`` are ignored on this route) and converges its + static equilibrium to at least ``min(1e-6, rel)``, on every route: alone in a standalone + deck, in a mixed ``EI = 0`` + finite-EI standalone deck, in OpenFAST + (``CompMooring = 5``), in FAST.Farm and behind the C API, so a cable driven by the same + motion steps to the same state on each. Use controls qualified for the mesh and load case; the maintained 952-element Gulf of Maine cable uses ``1e-4 1e-14 100 12`` - both (see rules) diff --git a/src/CableDyn_DeckDriver.f90 b/src/CableDyn_DeckDriver.f90 index b1731ce3..79e3d75a 100644 --- a/src/CableDyn_DeckDriver.f90 +++ b/src/CableDyn_DeckDriver.f90 @@ -1601,6 +1601,11 @@ RECURSIVE SUBROUTINE build_one_hermite_cable(ln, sections, types, points, opts, INTEGER, INTENT(OUT) :: coupled_node INTEGER, INTENT(OUT) :: ErrStat CHARACTER(*), INTENT(OUT) :: ErrMsg + ! TRUE: a standalone build (its notes printed, a Fixed End A admitted). FALSE: a static helper + ! build inside this one (still-water, light-line, coarse pass), whose cable is never stepped: + ! its static tolerance stays at COUPLED_STATIC_TOL. Absent: a coupled build (aggregate, OpenFAST, + ! FAST.Farm, C API, mixed deck). Every build that is stepped takes the deck's dynamic_solver + ! rel and max_iter, so a cable steps to the same Newton tolerance on every route. LOGICAL, INTENT(IN), OPTIONAL :: standalone_controls TYPE(CD_BathymetryType), INTENT(IN), OPTIONAL :: bathymetry REAL(wp), INTENT(IN), OPTIONAL :: fairlead_orientation(3, 3) @@ -1651,8 +1656,8 @@ RECURSIVE SUBROUTINE build_one_hermite_cable(ln, sections, types, points, opts, REAL(wp) :: step_tol, static_tol, static_tol_target REAL(wp), ALLOCATABLE :: q_coupled_tol(:) INTEGER :: polish_iters - ! Static tolerance of the coupled route (step_tol 5e-3 there), on which the mesh- - ! sequenced hierarchy's intermediate readiness and recovery budgets are exercised. + ! Loosest static tolerance (that of a static helper build), on which the mesh-sequenced + ! hierarchy's intermediate readiness and recovery budgets are exercised. REAL(wp), PARAMETER :: COUPLED_STATIC_TOL = 1.0e-6_wp ! Newton-iteration budget of one mesh-sequenced fallback solve: a bounded failure path. INTEGER, PARAMETER :: FALLBACK_SEQUENCE_BUDGET = 2000 @@ -1724,8 +1729,8 @@ RECURSIVE SUBROUTINE build_one_hermite_cable(ln, sections, types, points, opts, ErrStat = CD_DECKDRV_OK ErrMsg = '' - step_max_iter = 200 - step_tol = 5.0e-3_wp + step_max_iter = opts%dynamic_max_iter + step_tol = opts%dynamic_rel_tol standalone_mode = .FALSE. weightless_bow = .FALSE. contact_enabled = .FALSE. @@ -1774,20 +1779,19 @@ RECURSIVE SUBROUTINE build_one_hermite_cable(ln, sections, types, points, opts, end_b_dcm = end_b_orientation END IF contact_z = -2000.0_wp - IF (PRESENT(standalone_controls)) THEN - IF (standalone_controls) THEN - standalone_mode = .TRUE. - step_max_iter = opts%dynamic_max_iter - step_tol = opts%dynamic_rel_tol - END IF - END IF - fixed_fairlead_ok = standalone_mode - IF (PRESENT(fixed_fairlead)) fixed_fairlead_ok = fixed_fairlead_ok .OR. fixed_fairlead ! Hand the dynamic integrator an equilibrium converged at least as tightly as ! its own Newton stopping criterion. A looser static tolerance can otherwise ! appear as an artificial load impulse on the first nominally stationary step, ! particularly after local refinement of a light, bending-stiff cable. static_tol = MIN(COUPLED_STATIC_TOL, step_tol) + IF (PRESENT(standalone_controls)) THEN + standalone_mode = standalone_controls + ! a static helper build is never stepped: the static state it hands back is the one of + ! the loosest static tolerance, as before (the parent build polishes its own) + IF (.NOT. standalone_controls) static_tol = COUPLED_STATIC_TOL + END IF + fixed_fairlead_ok = standalone_mode + IF (PRESENT(fixed_fairlead)) fixed_fairlead_ok = fixed_fairlead_ok .OR. fixed_fairlead contact_enabled = opts%has_wtrdpth .OR. PRESENT(bathymetry) IF (opts%has_wtrdpth) contact_z = -opts%wtrdpth IF (PRESENT(bathymetry)) THEN @@ -3093,11 +3097,12 @@ RECURSIVE SUBROUTINE build_one_hermite_cable(ln, sections, types, points, opts, ' attachment(s) lumped at the nearest nodes (largest arc offset ', att_offset, ' m)' CALL report_note(note_text) END IF - ! Dynamic step controls: a relative Newton tolerance of 5e-3 (0.5% of the physical weight - ! force scale, on the coupling boundary) with a generous iteration budget. This is the - ! usual order for a coupling-boundary force residual; a stiffer 1e-4/1e-3 would demand a - ! static seed tighter than the static solve delivers and add - ! no coupling-force accuracy. The 1e-6 static seed sits far inside it at rest. + ! Dynamic step controls: the deck's dynamic_solver relative tolerance and iteration budget + ! (default 1e-8 and 30) on every route, standalone or coupled, so that a cable driven by the + ! same motion steps to the same state on each. The tolerance is not a coupling-force accuracy + ! alone: at rhoInf near 1 the march does not damp the residual a step leaves in the modes dtM + ! does not resolve, and a lazy wave stepped to 5e-3 under the same motion peaked at 4.3e5 N + ! against 1.9e4 N (the subdivision ladder takes the steps that do not converge in max_iter). CALL CD_HFMF_Init(cable, l0, ea, ei, rho_a, w, q_static, dynamic_fixed_dofs, contact_z, CD_ZERO, & opts%rho_inf, dt, coupled_node, es, em, max_iter=step_max_iter, tol=step_tol, & frame_cs=frame_cs, axial_quadrature_order=opts%axial_quadrature_order, & @@ -31030,12 +31035,14 @@ SUBROUTINE write_hermite_dynamic_out(out_root, channels, lines, points, opts, ca IF (motion%has_rot) THEN ! vessel motion: the fairlead end connection turns with the vessel (the DCM is ! global-to-vessel, the transpose of the vessel rotation); a moving End B rides on the - ! same vessel + ! same vessel. The DCM is formed in a local array: TRANSPOSE as an actual argument is + ! a strided view the compiler packs into a heap temporary at every step. + dcm_a = TRANSPOSE(motion%rot(:, :, step + 1)) CALL hermite_step_ends(cables(i), up, uv, ua, ubp, ubv, uba, es, em, & - u_orientation=TRANSPOSE(motion%rot(:, :, step + 1)), & + u_orientation=dcm_a, & u_angular_velocity=motion%omega(:, step + 1), & u_angular_acceleration=motion%alpha(:, step + 1), & - ub_orientation=TRANSPOSE(motion%rot(:, :, step + 1)), & + ub_orientation=dcm_a, & ub_angular_velocity=motion%omega(:, step + 1), & ub_angular_acceleration=motion%alpha(:, step + 1)) ELSE IF (motion%has_turb_rot) THEN @@ -31493,11 +31500,13 @@ SUBROUTINE apply_motion_to_hermite_cables(cables, lines, points, motion, time_in END IF IF (motion%has_rot) THEN ! vessel motion: an end connection at the fairlead (and at a moving End B) turns with the vessel + ! (the DCM in a local array, as in the march: no packed temporary) + dcm_a = TRANSPOSE(motion%rot(:, :, time_index)) CALL hermite_set_ends(cables(i), q, v, a, qb, vb, ab, es, em, & - u_orientation=TRANSPOSE(motion%rot(:, :, time_index)), & + u_orientation=dcm_a, & u_angular_velocity=motion%omega(:, time_index), & u_angular_acceleration=motion%alpha(:, time_index), & - ub_orientation=TRANSPOSE(motion%rot(:, :, time_index)), & + ub_orientation=dcm_a, & ub_angular_velocity=motion%omega(:, time_index), & ub_angular_acceleration=motion%alpha(:, time_index)) ELSE IF (motion%has_turb_rot) THEN diff --git a/tests/allocation_phase_counter.c b/tests/allocation_phase_counter.c new file mode 100644 index 00000000..bbe82e0a --- /dev/null +++ b/tests/allocation_phase_counter.c @@ -0,0 +1,46 @@ +/* File: tests/allocation_phase_counter.c + * SPDX-License-Identifier: Apache-2.0 + * Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University + * + * Phase window of the allocation counter (tests/allocation_counter.c) for a whole standalone + * driver run made in process. The test executable is linked with GNU ld + * --wrap=__cabledyn_profile_MOD_cd_profile_enter (the gfortran symbol of CD_Profile_Enter), which + * every time march calls at each phase change of every step, armed or not. Armed, the wrapper + * counts the allocations of the step phase (CD_PROF_SOLVE) after a number of warm-up steps and + * switches the counter off at the next phase change, so the march's fluid sampling, checks and + * outputs stay outside the counted window. */ + +void __real___cabledyn_profile_MOD_cd_profile_enter(const int *phase); +void cd_test_allocation_counting(int on); + +/* CD_PROF_SOLVE of CableDyn_Profile */ +enum { PHASE_SOLVE = 4 }; + +static int armed = 0; +static long long warm_steps = 0; +static long long solve_entries = 0; + +void __wrap___cabledyn_profile_MOD_cd_profile_enter(const int *phase) +{ + if (armed) { + if (*phase == PHASE_SOLVE) { + solve_entries++; + cd_test_allocation_counting(solve_entries > warm_steps); + } else { + cd_test_allocation_counting(0); + } + } + __real___cabledyn_profile_MOD_cd_profile_enter(phase); +} + +/* Arm (on != 0) with the number of step phases left uncounted, or disarm. */ +void cd_test_phase_counting(int on, long long warm) +{ + armed = on; + warm_steps = warm; + solve_entries = 0; + if (!on) cd_test_allocation_counting(0); +} + +/* Step phases entered since the arming. */ +long long cd_test_phase_solve_entries(void) { return solve_entries; } diff --git a/tests/test_coupled_cable_tolerance.f90 b/tests/test_coupled_cable_tolerance.f90 new file mode 100644 index 00000000..e8230ef1 --- /dev/null +++ b/tests/test_coupled_cable_tolerance.f90 @@ -0,0 +1,529 @@ +! File: tests/test_coupled_cable_tolerance.f90 +! SPDX-License-Identifier: Apache-2.0 +! Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University +!> One Newton tolerance for a finite-EI cable on every route. A cable in a mixed EI = 0 + finite-EI +!> deck, in OpenFAST (CompMooring = 5), in FAST.Farm and behind the C API runs on the coupled +!> aggregate; alone in a deck it runs on the standalone cubic-Hermite route. Both step it with the +!> deck's dynamic_solver relative tolerance and iteration budget (default 1e-8, 30). A lazy cable +!> (EI 1.99e4 N m^2, 80 elements, its run on a 100 m seabed) hangs from a Coupled fairlead that +!> moves along (3, 1, 1.5) m sin(2 pi t / 10 s), ramped in over 5 s (C2), beside a 450 m chain on +!> a second Coupled point moved the same way. The standalone driver (the executable given as the +!> first argument) runs the mixed deck and the cable alone, from the same motionFile. +!> 1. rhoInf 1.0, dtM 0.05 s and rhoInf 0.4, dtM 0.1 s: FairTen2 and AnchTen2 of the two routes +!> agree at every row to 1e-6 of their peak (measured: to the printed precision, 5e-8), and the +!> fairlead bend moment BendMom2N1 (a near-zero moment at the pinned end, 0.07 and 3.9 N m) to +!> 1e-3 of its peak (measured 1.2e-5 and 5e-8). The coupled cable stepped to the former fixed +!> coupled tolerance 5e-3 failed both: at rhoInf 1 the mixed route's FairTen2 peaked at 4.3e5 N +!> against 1.9e4 N (AnchTen2 5.5e5 against 2.1e3 N), at dtM 0.1 its FairTen2 fell to 8307 N +!> against 10799 N and its BendMom2N1 rose to 1612 against 3.9 N m. +!> 2. rhoInf 1.0, dtM 0.05 s, the cable alone driven by a host through the analytic motion: the +!> C API (CableDyn_InitDeck, CableDyn_Step, CableDyn_EvalChannel), the aggregate as OpenFAST +!> builds it (host environment, no deck ambient, host fluid fields) and as FAST.Farm builds it +!> (a TURBINES row, a Turbine1 fairlead) give the standalone driver's channels to the gate 1 +!> bounds from the static row on (measured 4.5e-8 and 2.1e-5); the OpenFAST and FAST.Farm cables +!> carry the deck's default tolerance and budget, and a deck's dynamic_solver row (2e-8, 25) +!> reaches them. +!> 3. The same mixed deck and the cable alone, both fairleads held in still water for 10 s at dtM +!> 0.025 s: at rhoInf 0, 0.4 and 1.0 the held equilibrium stays put on both routes (FairTen2 and +!> AnchTen2 within HELD_TOL = 1e-3 N of the static row; measured 6e-5 N, the printed digits) and +!> the routes agree to TEN_TOL. Stepped to 5e-3 the coupled cable was not dissipative: the +!> residual each step left grew at rhoInf 0.4 into an oscillation of +-2.1 N (FairTen2) and +!> +-1.1 N (AnchTen2) by 10 s, and the anchor tension moved by 0.029 N at rhoInf 0 and 0.015 N +!> at rhoInf 1. With the fairlead row 1 placed 0.1 mm off the static fairlead and then held +!> (a seed), the disturbance decays at rhoInf 0.4 (the last 2.5 s at most half the first) and +!> stays bounded at rhoInf 1 (at most the first 2.5 s), as on the single-line route, which the +!> mixed route matches to TEN_TOL; stepped to 5e-3 the coupled cable seeded by 1 cm grew at +!> rhoInf 1 from +-8.2e5 N to +-3.0e6 N while the single-line route decayed from 5.0e4 N to +!> 3.3e3 N. +PROGRAM test_coupled_cable_tolerance + USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_BOOL, C_CHAR, C_DOUBLE, C_INT, C_LOC, C_NULL_CHAR, C_PTR + USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY: IEEE_IS_FINITE + USE CableDyn_Precision, ONLY: wp + USE CableDyn_CAPI, ONLY: CableDyn_Create, CableDyn_Close, CableDyn_InitDeck, CableDyn_Step, & + CableDyn_EvalChannel, CableDyn_GetLastError, CableDyn_NCoupledDOF + USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, & + CD_AGG_NFluidNodes, CD_AGG_SetFluidFields, CD_AGG_Step_Moving, & + CD_AGG_EvalChannel, CD_AGG_End, CD_AGG_OK + IMPLICIT NONE + + REAL(wp), PARAMETER :: PI = 3.14159265358979323846_wp, PERIOD = 10.0_wp, RAMP = 5.0_wp, TMAX = 20.0_wp + REAL(wp), PARAMETER :: DIR(3) = [3.0_wp, 1.0_wp, 1.5_wp] + REAL(wp), PARAMETER :: CHAIN_FAIR(3) = [20.0_wp, 0.0_wp, -10.0_wp], CAB_FAIR(3) = [-20.0_wp, 0.0_wp, -10.0_wp] + ! gate bounds: tensions to TEN_TOL of their peak, the fairlead bend moment to BEND_TOL of its peak or + ! of BEND_FLOOR [N m] (a held fairlead's moment is round-off about zero); SEED_TOL replaces TEN_TOL + ! for the seeded undamped march of gate 3 (rhoInf 1), which keeps the step-to-step solver residual + REAL(wp), PARAMETER :: TEN_TOL = 1.0e-6_wp, BEND_TOL = 1.0e-3_wp, BEND_FLOOR = 1.0_wp, SEED_TOL = 1.0e-4_wp + ! gate 3: the held equilibrium's tensions to HELD_TOL [N] of the static row; the seed of the + ! fairlead row 1 [m]; the time step, duration and decay windows [s] + REAL(wp), PARAMETER :: HELD_TOL = 1.0e-3_wp, SEED = 1.0e-4_wp, HELD_DT = 0.025_wp, HELD_TMAX = 10.0_wp, & + DECAY_WINDOW = 2.5_wp + INTEGER, PARAMETER :: MAXROW = 500, MAXCOL = 8, NCH = 3 + CHARACTER(16), PARAMETER :: CHANNELS(NCH) = [CHARACTER(16) :: 'FairTen2', 'AnchTen2', 'BendMom2N1'] + ! host routes of gate 2 + INTEGER, PARAMETER :: HOST_CAPI = 1, HOST_OPENFAST = 2, HOST_FARM = 3 + CHARACTER(10), PARAMETER :: HOST_NAME(3) = [CHARACTER(10) :: 'C API', 'OpenFAST', 'FAST.Farm'] + CHARACTER(1024) :: driver + INTEGER :: n_fail = 0, host + + IF (COMMAND_ARGUMENT_COUNT() < 1) ERROR STOP 'usage: test_coupled_cable_tolerance ' + CALL GET_COMMAND_ARGUMENT(1, driver) + + CALL check_routes(1.0_wp, 0.05_wp, 'cct_r1') + CALL check_routes(0.4_wp, 0.1_wp, 'cct_r04') + DO host = HOST_CAPI, HOST_FARM + CALL check_host(host) + END DO + CALL check_held(0.0_wp, .FALSE., 'cct_h0') + CALL check_held(0.4_wp, .FALSE., 'cct_h04') + CALL check_held(1.0_wp, .FALSE., 'cct_h1') + CALL check_held(0.4_wp, .TRUE., 'cct_s04') + CALL check_held(1.0_wp, .TRUE., 'cct_s1') + IF (n_fail > 0) THEN + WRITE (*, '(A,I0,A)') 'FAIL: ', n_fail, ' coupled-cable tolerance gate(s) failed' + ERROR STOP 1 + END IF + WRITE (*, '(A)') 'PASS: the coupled and the standalone finite-EI cable step to the same tolerance' + +CONTAINS + + SUBROUTINE require(cond, msg) + LOGICAL, INTENT(IN) :: cond + CHARACTER(*), INTENT(IN) :: msg + IF (.NOT. cond) THEN + n_fail = n_fail + 1 + WRITE (*, '(A,A)') 'FAILED: ', msg + END IF + END SUBROUTINE require + + SUBROUTINE motion_state(t, g, dg, d2g) + !! g(t) = r(t) sin(2 pi t / PERIOD) with the C2 ramp r = 10 s^3 - 15 s^4 + 6 s^5, s = min(t / RAMP, 1), + !! and its exact first and second derivatives: from rest with zero acceleration at t = 0. + REAL(wp), INTENT(IN) :: t + REAL(wp), INTENT(OUT) :: g, dg, d2g + REAL(wp) :: s, r, dr, d2r, w, f, df, d2f + s = MIN(t/RAMP, 1.0_wp) + r = 10.0_wp*s**3 - 15.0_wp*s**4 + 6.0_wp*s**5 + dr = 0.0_wp + d2r = 0.0_wp + IF (t < RAMP) THEN + dr = (30.0_wp*s**2 - 60.0_wp*s**3 + 30.0_wp*s**4)/RAMP + d2r = (60.0_wp*s - 180.0_wp*s**2 + 120.0_wp*s**3)/RAMP**2 + END IF + w = 2.0_wp*PI/PERIOD + f = SIN(w*t) + df = w*COS(w*t) + d2f = -w*w*SIN(w*t) + g = r*f + dg = dr*f + r*df + d2g = d2r*f + 2.0_wp*dr*df + r*d2f + END SUBROUTINE motion_state + + SUBROUTINE write_motion(path, dt, with_chain) + !! The motionFile of the cable's fairlead (point 3) and, with_chain, the chain's (point 1). + CHARACTER(*), INTENT(IN) :: path + REAL(wp), INTENT(IN) :: dt + LOGICAL, INTENT(IN) :: with_chain + INTEGER :: u, k + REAL(wp) :: t, g, dg, d2g + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') '# time id x y z vx vy vz ax ay az' + DO k = 0, NINT(TMAX/dt) + 1 + t = REAL(k, wp)*dt + CALL motion_state(t, g, dg, d2g) + IF (with_chain) WRITE (u, '(F10.6,A,9ES24.15E3)') t, ' 1 ', CHAIN_FAIR + DIR*g, DIR*dg, DIR*d2g + WRITE (u, '(F10.6,A,9ES24.15E3)') t, ' 3 ', CAB_FAIR + DIR*g, DIR*dg, DIR*d2g + END DO + CLOSE (u) + END SUBROUTINE write_motion + + SUBROUTINE write_held_motion(path, with_chain) + !! The gate 3 seed: the cable's fairlead (point 3) and, with_chain, the chain's (point 1) at + !! SEED m along DIR off their deck positions from row 1 on, at rest. + CHARACTER(*), INTENT(IN) :: path + LOGICAL, INTENT(IN) :: with_chain + REAL(wp), PARAMETER :: ZERO(6) = 0.0_wp + INTEGER :: u, k + REAL(wp) :: t, off(3) + off = SEED*DIR/NORM2(DIR) + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') '# time id x y z vx vy vz ax ay az' + DO k = 0, NINT(HELD_TMAX/HELD_DT) + 1 + t = REAL(k, wp)*HELD_DT + IF (with_chain) WRITE (u, '(F10.6,A,9ES24.15E3)') t, ' 1 ', CHAIN_FAIR + off, ZERO + WRITE (u, '(F10.6,A,9ES24.15E3)') t, ' 3 ', CAB_FAIR + off, ZERO + END DO + CLOSE (u) + END SUBROUTINE write_held_motion + + SUBROUTINE write_deck(path, rho_inf, dt, mixed, motion, farm, solver, run_tmax) + !! The cable (line 2, points 3 -> 4) alone or, mixed, beside the chain (line 1, points 1 -> 2); + !! motion: the motionFile (blank: a host drives point 3, or the deck holds it); farm: point 3 is + !! Turbine1 of a TURBINES row at the origin; solver: a dynamic_solver row (blank: the defaults); + !! run_tmax: TMax (default TMAX). + CHARACTER(*), INTENT(IN) :: path, motion, solver + REAL(wp), INTENT(IN) :: rho_inf, dt + LOGICAL, INTENT(IN) :: mixed, farm + REAL(wp), INTENT(IN), OPTIONAL :: run_tmax + INTEGER :: u + REAL(wp) :: tm + tm = TMAX + IF (PRESENT(run_tmax)) tm = run_tmax + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'coupled-cable tolerance gate' + WRITE (u, '(A)') '--- LINE TYPES ---' + WRITE (u, '(A)') 'Name Diam Mass EA BA EI Cdn Cdt Can Cat' + WRITE (u, '(A)') '(-) (m) (kg/m) (N) (-) (Nm2) (-) (-) (-) (-)' + WRITE (u, '(A)') 'chain 0.1 48.8389796939 500000000 -0.5 0 1.2 0.4 1 0.5' + WRITE (u, '(A)') 'cab 0.16 36.7 469000000 -0.5 19900 1.2 0.1 1 0' + IF (farm) THEN + WRITE (u, '(A)') '--- TURBINES ---' + WRITE (u, '(A)') 'J X0 Y0 Z0' + WRITE (u, '(A)') '(-) (m) (m) (m)' + WRITE (u, '(A)') '1 0 0 0' + END IF + WRITE (u, '(A)') '--- POINTS ---' + WRITE (u, '(A)') 'ID Type X Y Z Mass Vol CdA Ca' + WRITE (u, '(A)') '(-) (-) (m) (m) (m) (kg) (m3) (m2) (-)' + IF (mixed) THEN + WRITE (u, '(A)') '1 Coupled 20 0 -10 0 0 0 0' + WRITE (u, '(A)') '2 Fixed 400 0 -100 0 0 0 0' + END IF + WRITE (u, '(A)') '3 '//MERGE('Turbine1', 'Coupled ', farm)//' -20 0 -10 0 0 0 0' + WRITE (u, '(A)') '4 Fixed -200 0 -100 0 0 0 0' + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + IF (mixed) WRITE (u, '(A)') '1 1 2 -' + WRITE (u, '(A)') '2 3 4 -' + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + IF (mixed) WRITE (u, '(A)') '1 chain 450 50' + WRITE (u, '(A)') '2 cab 260 80' + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '100 WtrDpth' + WRITE (u, '(A)') '1e5 kBot' + WRITE (u, '(A)') '1e4 cBot' + WRITE (u, '(F6.3,A)') dt, ' dtM' + WRITE (u, '(F6.2,A)') tm, ' TMax' + WRITE (u, '(F5.2,A)') rho_inf, ' rhoInf' + IF (LEN_TRIM(motion) > 0) WRITE (u, '(A)') TRIM(motion)//' motionFile' + IF (LEN_TRIM(solver) > 0) WRITE (u, '(A)') TRIM(solver) + WRITE (u, '(A)') '--- OUTPUTS ---' + WRITE (u, '(A)') 'FairTen2' + WRITE (u, '(A)') 'AnchTen2' + WRITE (u, '(A)') 'BendMom2N1' + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + END SUBROUTINE write_deck + + SUBROUTINE run_driver(deck, root, ok) + CHARACTER(*), INTENT(IN) :: deck, root + LOGICAL, INTENT(OUT) :: ok + INTEGER :: ex, cs + CALL EXECUTE_COMMAND_LINE('"'//TRIM(driver)//'" '//deck//' '//root//' > '//root//'.log 2>&1', & + WAIT=.TRUE., EXITSTAT=ex, CMDSTAT=cs) + ok = cs == 0 .AND. ex == 0 + END SUBROUTINE run_driver + + SUBROUTINE read_channels(path, vals, nrow) + !! The CHANNELS columns of a driver .out file, one row per output time. + CHARACTER(*), INTENT(IN) :: path + REAL(wp), INTENT(OUT) :: vals(MAXROW, NCH) + INTEGER, INTENT(OUT) :: nrow + CHARACTER(32) :: names(MAXCOL) + CHARACTER(4096) :: buf + REAL(wp) :: row(MAXCOL) + INTEGER :: u, ios, ncol, k, c, col(NCH) + vals = 0.0_wp + nrow = 0 + OPEN (NEWUNIT=u, FILE=path, STATUS='OLD', ACTION='READ', IOSTAT=ios) + IF (ios /= 0) RETURN + READ (u, '(A)', IOSTAT=ios) buf + READ (u, '(A)', IOSTAT=ios) buf + names = '' + READ (buf, *, IOSTAT=ios) names + ncol = 0 + DO k = 1, MAXCOL + IF (LEN_TRIM(names(k)) > 0) ncol = k + END DO + col = 0 + DO c = 1, NCH + DO k = 1, ncol + IF (TRIM(names(k)) == TRIM(CHANNELS(c))) col(c) = k + END DO + END DO + IF (ANY(col == 0)) THEN + CLOSE (u); RETURN + END IF + DO + IF (nrow == MAXROW) EXIT + READ (u, *, IOSTAT=ios) row(1:ncol) + IF (ios /= 0) EXIT + nrow = nrow + 1 + vals(nrow, :) = row(col) + END DO + CLOSE (u) + END SUBROUTINE read_channels + + SUBROUTINE compare(label, ref, got, n, ten_bound) + !! Each channel of got against ref over n rows: |got - ref| <= tol max|ref| (the bend moment: + !! tol max(max|ref|, BEND_FLOOR)); ten_bound replaces TEN_TOL. + CHARACTER(*), INTENT(IN) :: label + REAL(wp), INTENT(IN) :: ref(:, :), got(:, :) + INTEGER, INTENT(IN) :: n + REAL(wp), INTENT(IN), OPTIONAL :: ten_bound + REAL(wp) :: e, peak, tol + INTEGER :: c + DO c = 1, NCH + peak = MAXVAL(ABS(ref(1:n, c))) + IF (c == 3) peak = MAX(peak, BEND_FLOOR) + e = MAXVAL(ABS(got(1:n, c) - ref(1:n, c)))/MAX(peak, TINY(1.0_wp)) + IF (.NOT. IEEE_IS_FINITE(e)) e = HUGE(1.0_wp) + tol = TEN_TOL + IF (PRESENT(ten_bound)) tol = ten_bound + IF (c == 3) tol = BEND_TOL + WRITE (*, '(2X,A,1X,A,A,ES10.3,A,ES10.3,A,ES10.3,A,ES10.3,A,ES9.2,A,ES8.1,A)') label, TRIM(CHANNELS(c)), & + ': peak ', peak, ' (', MAXVAL(ABS(got(1:n, c))), '), range ', MINVAL(ref(1:n, c)), ' (', & + MINVAL(got(1:n, c)), '), max|diff|/peak ', e, ' (bound ', tol, ')' + CALL require(e <= tol, label//': '//TRIM(CHANNELS(c))//' agrees with the standalone route') + END DO + END SUBROUTINE compare + + SUBROUTINE check_routes(rho_inf, dt, root) + !! Gate 1: the mixed route against the standalone cubic-Hermite route, same cable and motion. + REAL(wp), INTENT(IN) :: rho_inf, dt + CHARACTER(*), INTENT(IN) :: root + REAL(wp) :: va(MAXROW, NCH), vb(MAXROW, NCH) + INTEGER :: ra, rb + LOGICAL :: ok_a, ok_b + CHARACTER(64) :: label + WRITE (label, '(A,F4.2,A,F5.3,A)') 'rhoInf ', rho_inf, ', dtM ', dt, ' s' + CALL write_motion(root//'_single.txt', dt, .FALSE.) + CALL write_motion(root//'_mixed.txt', dt, .TRUE.) + CALL write_deck(root//'_single.dat', rho_inf, dt, .FALSE., root//'_single.txt', .FALSE., '') + CALL write_deck(root//'_mixed.dat', rho_inf, dt, .TRUE., root//'_mixed.txt', .FALSE., '') + CALL run_driver(root//'_single.dat', root//'_single', ok_a) + CALL run_driver(root//'_mixed.dat', root//'_mixed', ok_b) + CALL require(ok_a .AND. ok_b, TRIM(label)//': both routes run') + IF (.NOT. (ok_a .AND. ok_b)) RETURN + CALL read_channels(root//'_single.out', va, ra) + CALL read_channels(root//'_mixed.out', vb, rb) + CALL require(ra == NINT(TMAX/dt) + 1 .AND. rb == ra, TRIM(label)//': every output row is written') + IF (ra /= rb .OR. ra < 2) RETURN + WRITE (*, '(A)') 'gate 1, '//TRIM(label)//': standalone (mixed route):' + CALL compare('mixed', va, vb, ra) + END SUBROUTINE check_routes + + REAL(wp) FUNCTION half_swing(v, n, t0, t1) RESULT(h) + !! Half the peak-to-peak of v over the output rows with t0 < t <= t1 (row k at (k - 1) HELD_DT). + REAL(wp), INTENT(IN) :: v(:), t0, t1 + INTEGER, INTENT(IN) :: n + REAL(wp) :: lo, hi, t + INTEGER :: k + lo = HUGE(1.0_wp) + hi = -HUGE(1.0_wp) + DO k = 1, n + t = REAL(k - 1, wp)*HELD_DT + IF (t <= t0 + 1.0e-9_wp .OR. t > t1 + 1.0e-9_wp) CYCLE + lo = MIN(lo, v(k)) + hi = MAX(hi, v(k)) + END DO + h = 0.5_wp*(hi - lo) + IF (.NOT. IEEE_IS_FINITE(h)) h = HUGE(1.0_wp) + END FUNCTION half_swing + + SUBROUTINE check_held(rho_inf, seeded, root) + !! Gate 3: both fairleads held in still water (seeded: row 1 SEED m off the static fairlead). + REAL(wp), INTENT(IN) :: rho_inf + LOGICAL, INTENT(IN) :: seeded + CHARACTER(*), INTENT(IN) :: root + REAL(wp) :: va(MAXROW, NCH), vb(MAXROW, NCH), dev, h0, h1, ratio, bound + INTEGER :: ra, rb, c, r + LOGICAL :: ok_a, ok_b + CHARACTER(80) :: label + WRITE (label, '(A,F4.2,A)') 'rhoInf ', rho_inf, MERGE(', seeded', ', held ', seeded) + IF (seeded) THEN + CALL write_held_motion(root//'_single.txt', .FALSE.) + CALL write_held_motion(root//'_mixed.txt', .TRUE.) + CALL write_deck(root//'_single.dat', rho_inf, HELD_DT, .FALSE., root//'_single.txt', .FALSE., '', HELD_TMAX) + CALL write_deck(root//'_mixed.dat', rho_inf, HELD_DT, .TRUE., root//'_mixed.txt', .FALSE., '', HELD_TMAX) + ELSE + CALL write_deck(root//'_single.dat', rho_inf, HELD_DT, .FALSE., '', .FALSE., '', HELD_TMAX) + CALL write_deck(root//'_mixed.dat', rho_inf, HELD_DT, .TRUE., '', .FALSE., '', HELD_TMAX) + END IF + CALL run_driver(root//'_single.dat', root//'_single', ok_a) + CALL run_driver(root//'_mixed.dat', root//'_mixed', ok_b) + CALL require(ok_a .AND. ok_b, TRIM(label)//': both routes run') + IF (.NOT. (ok_a .AND. ok_b)) RETURN + CALL read_channels(root//'_single.out', va, ra) + CALL read_channels(root//'_mixed.out', vb, rb) + CALL require(ra == NINT(HELD_TMAX/HELD_DT) + 1 .AND. rb == ra, TRIM(label)//': every output row is written') + IF (ra /= rb .OR. ra < 2) RETURN + WRITE (*, '(A)') 'gate 3, '//TRIM(label)//': standalone (mixed route):' + IF (seeded .AND. rho_inf > 0.95_wp) THEN + CALL compare('mixed', va, vb, ra, SEED_TOL) + ELSE + CALL compare('mixed', va, vb, ra) + END IF + DO c = 1, 2 + IF (.NOT. seeded) THEN + DO r = 1, 2 + IF (r == 1) dev = MAXVAL(ABS(va(1:ra, c) - va(1, c))) + IF (r == 2) dev = MAXVAL(ABS(vb(1:rb, c) - vb(1, c))) + IF (.NOT. IEEE_IS_FINITE(dev)) dev = HUGE(1.0_wp) + WRITE (*, '(2X,A,1X,A,A,ES10.3,A,ES8.1,A)') TRIM(MERGE('single', 'mixed ', r == 1)), TRIM(CHANNELS(c)), & + ': held equilibrium, max|T - T(0)| ', dev, ' N (bound ', HELD_TOL, ' N)' + CALL require(dev <= HELD_TOL, TRIM(label)//': '//TRIM(CHANNELS(c))//' holds the equilibrium on the '// & + TRIM(MERGE('single', 'mixed ', r == 1))//' route') + END DO + ELSE + h0 = half_swing(vb(:, c), rb, 0.0_wp, DECAY_WINDOW) + h1 = half_swing(vb(:, c), rb, HELD_TMAX - DECAY_WINDOW, HELD_TMAX) + ratio = h1/MAX(h0, TINY(1.0_wp)) + bound = MERGE(1.0_wp, 0.5_wp, rho_inf > 0.95_wp) + WRITE (*, '(2X,A,1X,A,A,ES10.3,A,ES10.3,A,F7.4,A,F4.2,A)') 'mixed', TRIM(CHANNELS(c)), ': half swing ', h0, & + ' N in the first 2.5 s, ', h1, ' N in the last, ratio ', ratio, ' (bound ', bound, ')' + CALL require(ratio <= bound, TRIM(label)//': the '//TRIM(CHANNELS(c))//' disturbance '// & + TRIM(MERGE('stays bounded', 'decays ', rho_inf > 0.95_wp))//' on the mixed route') + END IF + END DO + END SUBROUTINE check_held + + SUBROUTINE check_host(route) + !! Gate 2: a host drives the cable alone through the analytic motion at rhoInf 1, dtM 0.05. + INTEGER, INTENT(IN) :: route + REAL(wp), PARAMETER :: RI = 1.0_wp, DT = 0.05_wp + CHARACTER(*), PARAMETER :: ROOT = 'cct_r1' + TYPE(CD_AGG_ModuleType), ALLOCATABLE :: agg + CHARACTER(KIND=C_CHAR), TARGET :: c_path(64), c_tok(16), msg(512) + TYPE(C_PTR) :: handle + INTEGER(C_INT) :: ces, n_iter + LOGICAL(C_BOOL) :: cconv, cstall + REAL(C_DOUBLE), TARGET :: q(3), v(3), a(3) + REAL(C_DOUBLE) :: cval + REAL(wp) :: va(MAXROW, NCH), got(MAXROW, NCH), pos(3, 1), vel(3, 1), acc(3, 1), t, g, dg, d2g + REAL(wp), ALLOCATABLE :: fv(:, :), fa(:, :), wz(:) + INTEGER :: ra, k, c, i, es, nit, nfl + LOGICAL :: conv, stall + CHARACTER(2048) :: em + CHARACTER(64) :: deck + CHARACTER(16) :: tok + + CALL read_channels(ROOT//'_single.out', va, ra) + CALL require(ra == NINT(TMAX/DT) + 1, HOST_NAME(route)//': the standalone reference of gate 1 exists') + IF (ra /= NINT(TMAX/DT) + 1) RETURN + deck = 'cct_host_'//TRIM(MERGE('farm', 'host', route == HOST_FARM))//'.dat' + CALL write_deck(TRIM(deck), RI, DT, .FALSE., '', route == HOST_FARM, '') + got = 0.0_wp + IF (route == HOST_CAPI) THEN + c_path = C_NULL_CHAR + DO i = 1, LEN_TRIM(deck) + c_path(i) = deck(i:i) + END DO + CALL CableDyn_Create(handle, ces) + CALL CableDyn_InitDeck(handle, C_LOC(c_path), INT(LEN_TRIM(deck), C_INT), ces) + IF (ces /= 0_C_INT) THEN + CALL CableDyn_GetLastError(handle, C_LOC(msg), INT(SIZE(msg), C_INT)) + em = '' + DO i = 1, SIZE(msg) + IF (msg(i) == C_NULL_CHAR) EXIT + em(i:i) = msg(i) + END DO + END IF + CALL require(ces == 0_C_INT .AND. CableDyn_NCoupledDOF(handle, ces) == 3_C_INT, & + 'C API: the cable deck initialises with one coupled point: '//TRIM(em)) + IF (ces /= 0_C_INT) THEN + CALL CableDyn_Close(handle, ces); RETURN + END IF + ! row k + 1 at t = k dt; row 1 is the static state + DO k = 0, ra - 1 + t = REAL(k, wp)*DT + CALL motion_state(t, g, dg, d2g) + q = CAB_FAIR + DIR*g + v = DIR*dg + a = DIR*d2g + IF (k > 0) THEN + CALL CableDyn_Step(handle, REAL(DT, C_DOUBLE), C_LOC(q), C_LOC(v), C_LOC(a), 3_C_INT, cconv, cstall, & + n_iter, ces) + IF (ces /= 0_C_INT .OR. .NOT. LOGICAL(cconv)) EXIT + END IF + DO c = 1, NCH + c_tok = C_NULL_CHAR + tok = CHANNELS(c) + DO i = 1, LEN_TRIM(tok) + c_tok(i) = tok(i:i) + END DO + CALL CableDyn_EvalChannel(handle, C_LOC(c_tok), INT(SIZE(c_tok), C_INT), cval, ces) + got(k + 1, c) = REAL(cval, wp) + END DO + IF (ces /= 0_C_INT) EXIT + END DO + CALL require(ces == 0_C_INT .AND. k == ra, 'C API: every step converges and every channel evaluates') + CALL CableDyn_Close(handle, ces) + ELSE + ALLOCATE (agg) + IF (route == HOST_OPENFAST) THEN + CALL CD_AGG_Init_From_Deck(agg, TRIM(deck), DT, es, em, env_gravity=9.80665_wp, env_rho_water=1025.0_wp, & + env_wtrdpth=100.0_wp, external_fluid=.TRUE., forbid_deck_ambient=.TRUE.) + ELSE + CALL CD_AGG_Init_From_Deck(agg, TRIM(deck), DT, es, em, n_turbines=1, & + turbine_ref_pos=RESHAPE([0.0_wp, 0.0_wp, 0.0_wp], [3, 1]), & + forbid_deck_ambient=.TRUE.) + END IF + CALL require(es == CD_AGG_OK, HOST_NAME(route)//': the cable deck initialises: '//TRIM(em)) + IF (es /= CD_AGG_OK) RETURN + CALL require(CD_AGG_NMovingPoints(agg, es, em) == 1 .AND. agg%ncable == 1, & + HOST_NAME(route)//': one cable, one moving point') + IF (agg%ncable /= 1) RETURN + CALL require(agg%cables(1)%tol == 1.0e-8_wp .AND. agg%cables(1)%max_iter == 30, & + HOST_NAME(route)//': the cable steps to the deck default dynamic_solver 1e-8, 30') + nfl = CD_AGG_NFluidNodes(agg, es, em) + ALLOCATE (fv(3, nfl), fa(3, nfl), wz(nfl)) + fv = 0.0_wp + fa = 0.0_wp + wz = 0.0_wp + DO k = 0, ra - 1 + t = REAL(k, wp)*DT + CALL motion_state(t, g, dg, d2g) + pos(:, 1) = CAB_FAIR + DIR*g + vel(:, 1) = DIR*dg + acc(:, 1) = DIR*d2g + IF (k > 0) THEN + IF (route == HOST_OPENFAST) THEN + CALL CD_AGG_SetFluidFields(agg, fv, fa, wz, es, em) + IF (es /= CD_AGG_OK) EXIT + END IF + CALL CD_AGG_Step_Moving(agg, DT, pos, vel, acc, conv, stall, nit, es, em, t_committed=t) + IF (es /= CD_AGG_OK .OR. .NOT. conv) EXIT + END IF + DO c = 1, NCH + CALL CD_AGG_EvalChannel(agg, c, got(k + 1, c), es, em) + END DO + IF (es /= CD_AGG_OK) EXIT + END DO + CALL require(es == CD_AGG_OK .AND. k == ra, HOST_NAME(route)//': every step converges: '//TRIM(em)) + CALL CD_AGG_End(agg, es, em) + ! a deck's dynamic_solver row reaches the host route's cable + CALL write_deck(TRIM(deck), RI, DT, .FALSE., '', route == HOST_FARM, 'dynamic_solver 2.0e-8 1.0e-14 25 12') + IF (route == HOST_OPENFAST) THEN + CALL CD_AGG_Init_From_Deck(agg, TRIM(deck), DT, es, em, env_gravity=9.80665_wp, env_rho_water=1025.0_wp, & + env_wtrdpth=100.0_wp, external_fluid=.TRUE., forbid_deck_ambient=.TRUE.) + ELSE + CALL CD_AGG_Init_From_Deck(agg, TRIM(deck), DT, es, em, n_turbines=1, & + turbine_ref_pos=RESHAPE([0.0_wp, 0.0_wp, 0.0_wp], [3, 1]), & + forbid_deck_ambient=.TRUE.) + END IF + CALL require(es == CD_AGG_OK, HOST_NAME(route)//': the dynamic_solver deck initialises: '//TRIM(em)) + IF (es /= CD_AGG_OK) RETURN + CALL require(agg%cables(1)%tol == 2.0e-8_wp .AND. agg%cables(1)%max_iter == 25, & + HOST_NAME(route)//': the deck dynamic_solver row sets the cable tolerance and budget') + CALL CD_AGG_End(agg, es, em) + END IF + WRITE (*, '(A)') 'gate 2, '//TRIM(HOST_NAME(route))//', rhoInf 1.00, dtM 0.050 s: standalone (host route):' + CALL compare(TRIM(HOST_NAME(route)), va, got, ra) + END SUBROUTINE check_host + +END PROGRAM test_coupled_cable_tolerance diff --git a/tests/test_mixed_route_currents.f90 b/tests/test_mixed_route_currents.f90 index 8e60d849..2abe5024 100644 --- a/tests/test_mixed_route_currents.f90 +++ b/tests/test_mixed_route_currents.f90 @@ -11,14 +11,12 @@ !> cable along x, the chain along y), in the static start and the march: the mixed route !> against the single-line routes, each channel to a fraction of its range, beside the same !> comparison in still water (the route difference without a current). The static start agrees -!> within STATIC_TOL: the chain exactly, the cable to the stopping point of the coupled cable -!> build's static polish (its out-of-plane offset, the softest coordinate, by 4 um of 0.55 m; -!> in still water exactly). In the march the cable steps with the coupled Newton controls on -!> the mixed route (relative tolerance 5e-3, a fresh factor) and with the deck's on its own -!> (1e-8, a carried factor), which differ by about 1.2e-3 of the range in still water too -!> (DRIVER_CABLE_TOL); with the deck's controls given to the aggregate cable (the library -!> march of the driver's loop) the march keeps the static start's difference -!> (LIBRARY_CABLE_TOL). The chain agrees within CHAIN_TOL (exactly in a sheared current; its +!> within STATIC_TOL: the chain and the cable exactly (measured 0). In the march the cable steps +!> with the deck's Newton controls on both routes (1e-8 by default), in the driver and in the +!> library march of the driver's loop alike: it agrees within DRIVER_CABLE_TOL (measured 0) and +!> LIBRARY_CABLE_TOL (1.9e-8). Before the coupled cable took the deck's controls, it stepped to +!> 5e-3 on the mixed route and differed by 1.2e-3 of the range, in still water too. The chain +!> agrees within CHAIN_TOL (exactly in a sheared current; its !> row-1 transfer re-derives its interior accelerations on the mixed route, by 5e-5 in still !> water). The current is felt: both mid nodes leave the vertical plane of their chord, and !> the chain's fairlead tension history differs from the still-water run's. @@ -66,8 +64,8 @@ PROGRAM test_mixed_route_currents CHARACTER(16), PARAMETER :: CHAIN_OUTS(5) = [CHARACTER(16) :: 'FairTen1', 'AnchTen1', 'L1N11px', 'L1N11py', & 'L1N11pz'] ! differences of the mixed route from the single-line routes, of each channel's range (gate 1): the - ! static start; the cable stepped with the coupled Newton controls, and with the deck's; the chain - REAL(wp), PARAMETER :: STATIC_TOL = 1.0e-5_wp, DRIVER_CABLE_TOL = 2.0e-3_wp, LIBRARY_CABLE_TOL = 1.0e-5_wp + ! static start; the cable stepped by the driver, and by the library loop; the chain + REAL(wp), PARAMETER :: STATIC_TOL = 1.0e-6_wp, DRIVER_CABLE_TOL = 1.0e-6_wp, LIBRARY_CABLE_TOL = 1.0e-6_wp REAL(wp), PARAMETER :: CHAIN_TOL = 1.0e-4_wp ! round-off between two writings of one field (gates 3 and 4), of each channel's range, and of a ! uniform field interpolated on a grid (gate 4) @@ -363,7 +361,7 @@ SUBROUTINE compare_case(dir, label, still) e_cable_static, e_cable, ', chain ', e_chain_static, e_chain, ' (static start, march; of each channel''s range)' CALL require(e_cable_static <= STATIC_TOL .AND. e_chain_static <= STATIC_TOL, & label//': the static start agrees within the static tolerance') - CALL require(e_cable <= DRIVER_CABLE_TOL, label//': the cable agrees within the coupled Newton tolerance') + CALL require(e_cable <= DRIVER_CABLE_TOL, label//': the cable agrees within the deck''s Newton tolerance') CALL require(e_chain <= CHAIN_TOL, label//': the chain agrees within the row-1 transfer') ! the current is felt: both mid nodes leave the vertical plane of their chord IF (PRESENT(still)) THEN diff --git a/tests/test_mixed_route_torsion.f90 b/tests/test_mixed_route_torsion.f90 index ec685c8d..f33803a1 100644 --- a/tests/test_mixed_route_torsion.f90 +++ b/tests/test_mixed_route_torsion.f90 @@ -10,11 +10,11 @@ !> alone on the standalone cubic-Hermite route and beside a moving EI = 0 chain on the mixed !> route. Torq, Twist and the mid-line node agree to the printed precision of the .out !> (8 digits), the fairlead and anchor tension, fairlead inclination and End A bend moment -!> (the Rigid clamp turns with the vessel on both routes) to 5e-5 of their range: the -!> coupled cables step with the coupled Newton controls (relative tolerance 5e-3, a fresh -!> factor every step), the standalone line with the deck's (1e-8, a carried factor). With -!> the aggregate cables given the standalone controls the mixed route reproduces every -!> channel of the standalone route to the printed precision. The same chain moved by a +!> (the Rigid clamp turns with the vessel on both routes) to 5e-5 of their range: both step +!> to the deck's Newton tolerance (1e-8), the coupled cables with a fresh factor every step, +!> the standalone line with a carried factor. With the aggregate cables given the carried +!> factor too the mixed route reproduces every channel of the standalone route to the +!> printed precision. The same chain moved by a !> motionFile of the same point translations holds the frames: the End A bend moment and !> the twist then differ from the turning-vessel run (the rotation is not dropped). !> 2. A motionFile surge with a roll column (End A rolls 20 deg about its director, from diff --git a/tests/test_openfast_aggregate.f90 b/tests/test_openfast_aggregate.f90 index 4187e938..7ffb2192 100644 --- a/tests/test_openfast_aggregate.f90 +++ b/tests/test_openfast_aggregate.f90 @@ -1123,20 +1123,18 @@ SUBROUTINE case_cable_modified_newton() ! The DETERMINISTIC plumbing gate: the option must land on the cable model itself ! (a plumbed-but-dead flag fails here; the flag's solver behavior is gated at the ! element level by hermite_cable_dynamic::check_modified_newton). The counters are - ! reported, not gated: at this path's frozen production tolerance (5e-3 relative) - ! per-iteration contraction is weak, so reuse yields no assembly saving on this rig - ! -- the benefit lives at tighter tolerances (the committed 800 m bench at 1e-4). + ! reported, not gated: on this small rig reuse yields no reliable assembly saving + ! -- the benefit lives on longer, finer cables (the committed 800 m bench at 1e-4). CALL require(.NOT. aggf%cables(1)%line%modified_newton, 'mn-cable: plain deck leaves the flag off') CALL require(aggm%cables(1)%line%modified_newton, 'mn-cable: the OPTIONS row reaches the cable model') - ! Both variants commit each step at the SAME relative tolerance; the coupled-node - ! reaction lives on FIXED DOFs outside the converged free residual, so the honest - ! agreement bound is tolerance-consistent (a few x 5e-3 of the force scale), not - ! trajectory-tight. Gate at 10% of magnitude as the same-physics sanity check. + ! Both variants commit each step at the SAME relative tolerance (the deck's); the + ! coupled-node reaction lives on FIXED DOFs outside the converged free residual, so the + ! agreement bound is tolerance-consistent, not trajectory-tight. Gate at 10% of + ! magnitude as the same-physics sanity check. CALL require(NORM2(lf(:, 1) - lm(:, 1)) <= 0.10_wp*NORM2(lf(:, 1)), & 'mn-cable: coupled loads agree to the shared step tolerance') - ! The counters stay collected (they document that reuse saves nothing at this - ! path's frozen 5e-3 tolerance -- see the CHANGELOG scope note) but a passing - ! test prints nothing; gate them only for finiteness of the collection itself. + ! The counters stay collected but a passing test prints nothing; gate them only for + ! finiteness of the collection itself. CALL require(ntan_full > 0 .AND. ntan_mn > 0, 'mn-cable: profile counters collected') CALL CD_AGG_End(aggf, es, em) diff --git a/tests/test_step_allocations.f90 b/tests/test_step_allocations.f90 index f101c1eb..80369904 100644 --- a/tests/test_step_allocations.f90 +++ b/tests/test_step_allocations.f90 @@ -39,10 +39,15 @@ PROGRAM test_step_allocations !! With "mixed_current" the same march of a mixed deck takes the deck's own current !! (deck_current): a MoorDyn-C Currents 5 grid that varies in space and time, sampled at every !! node of the chain and of the finite-EI cable before each step, past its last sample too. + !! With "driver_vessel" the gate runs a whole standalone driver run in process + !! (CD_Run_Deck_Driver) of a finite-EI line on the cubic-Hermite route moved by a vesselMotion + !! record, and counts the step phase of every step after warm-up (tests/allocation_phase_counter.c: + !! the end kinematics, the vessel orientation and rates handed to the cable, its implicit step). !! !! Usage: test_step_allocations [ranges] !! test_step_allocations torsion | nodal | free | current_grid | motion_roll | motion_vessel | !! mixed_current + !! test_step_allocations driver_vessel USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT, C_LONG_LONG USE CableDyn_Precision, ONLY: wp USE CableDyn_EndConnection, ONLY: CD_ENDCONN_RIGID @@ -53,6 +58,7 @@ PROGRAM test_step_allocations USE CableDyn_HermiteCableDynamic, ONLY: CD_HermiteCable_Dyn_End_Force, CD_HermiteCable_Dyn_Energy, & CD_HermiteCable_Dyn_Torsion_State, CD_HermiteCable_Dyn_Node_Torque, & CD_HermiteCable_Dyn_Element_Twist + USE CableDyn_DeckDriver, ONLY: CD_Run_Deck_Driver USE CableDyn_Hydro, ONLY: CD_CurrentGridType, CD_Current_Grid_Resample, CD_Current_Grid_Velocity, & CD_Current_Grid_Gradient USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, & @@ -71,6 +77,15 @@ SUBROUTINE cd_test_allocation_counting(on) BIND(C, NAME='cd_test_allocation_coun IMPORT :: C_INT INTEGER(C_INT), VALUE :: on END SUBROUTINE cd_test_allocation_counting + SUBROUTINE cd_test_phase_counting(on, warm) BIND(C, NAME='cd_test_phase_counting') + IMPORT :: C_INT, C_LONG_LONG + INTEGER(C_INT), VALUE :: on + INTEGER(C_LONG_LONG), VALUE :: warm + END SUBROUTINE cd_test_phase_counting + FUNCTION cd_test_phase_solve_entries() BIND(C, NAME='cd_test_phase_solve_entries') RESULT(n) + IMPORT :: C_LONG_LONG + INTEGER(C_LONG_LONG) :: n + END FUNCTION cd_test_phase_solve_entries END INTERFACE INTEGER, PARAMETER :: WARMUP_STEPS = 40 REAL(wp), PARAMETER :: DT = 0.025_wp, SURGE_AMPLITUDE = 0.5_wp, SURGE_PERIOD = 20.0_wp @@ -95,9 +110,11 @@ END SUBROUTINE cd_test_allocation_counting CALL motion_gate(TRIM(arg) == 'motion_vessel') CASE ('mixed_current') CALL mixed_current_gate() + CASE ('driver_vessel') + CALL driver_vessel_gate() CASE DEFAULT ERROR STOP 'a single argument must be "torsion", "nodal", "free", "current_grid", "motion_roll", '// & - '"motion_vessel" or "mixed_current"' + '"motion_vessel", "mixed_current" or "driver_vessel"' END SELECT STOP END IF @@ -475,6 +492,85 @@ SUBROUTINE motion_gate(vessel) PRINT '(A)', 'PASS: no heap allocation in the prescribed-motion march of a mixed deck after warm-up' END SUBROUTINE motion_gate + SUBROUTINE driver_vessel_gate() + !! Zero heap allocation in the step phase of the standalone driver's march of a finite-EI line on + !! the cubic-Hermite route (a lazy cable from a Vessel fairlead to a Fixed anchor on the seabed), + !! moved by a vesselMotion record (surge 1 m and roll 3 deg from rest): every step hands the + !! cable the vessel's orientation, angular velocity and acceleration with its fairlead state. + INTEGER, PARAMETER :: N_WARM = 40, NT = 200 + REAL(wp), PARAMETER :: DTM = 0.05_wp, PI = 3.14159265358979323846_wp + CHARACTER(*), PARAMETER :: ROOT = 'step_alloc_driver_vessel' + REAL(wp) :: t, x, vx, ax, ro, dro, d2ro, om + INTEGER :: u, k + INTEGER(C_LONG_LONG) :: c0, n_alloc, n_steps + LOGICAL :: converged + om = 2.0_wp*PI/10.0_wp + OPEN (NEWUNIT=u, FILE=ROOT//'.txt', STATUS='REPLACE', ACTION='WRITE') + DO k = 0, NT + 1 + t = DTM*k + x = 0.5_wp*(1.0_wp - COS(om*t)) + vx = 0.5_wp*om*SIN(om*t) + ax = 0.5_wp*om*om*COS(om*t) + ro = 1.5_wp*(1.0_wp - COS(om*t)) + dro = 1.5_wp*om*SIN(om*t)*PI/180.0_wp + d2ro = 1.5_wp*om*om*COS(om*t)*PI/180.0_wp + ! time x y z roll pitch yaw vx vy vz wx wy wz ax ay az alx aly alz + WRITE (u, '(F8.3,ES25.16E3,A,ES25.16E3,A,ES25.16E3,A,ES25.16E3,A,ES25.16E3,A,ES25.16E3,A)') t, x, & + ' 0 -10 ', ro, ' 0 0 ', vx, ' 0 0 ', dro, ' 0 0 ', ax, ' 0 0 ', d2ro, ' 0 0' + END DO + CLOSE (u) + OPEN (NEWUNIT=u, FILE=ROOT//'.dat', STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'step allocation gate: finite-EI line on a vessel, standalone driver' + WRITE (u, '(A)') '--- LINE TYPES ---' + WRITE (u, '(A)') 'Name Diam Mass EA BA EI Cdn Cdt Can Cat' + WRITE (u, '(A)') '(-) (m) (kg/m) (N) (-) (Nm2) (-) (-) (-) (-)' + WRITE (u, '(A)') 'cab 0.16 36.7 469000000 -0.5 19900 1.2 0.1 1 0' + WRITE (u, '(A)') '--- POINTS ---' + WRITE (u, '(A)') 'ID Type X Y Z Mass Vol CdA Ca' + WRITE (u, '(A)') '(-) (-) (m) (m) (m) (kg) (m3) (m2) (-)' + WRITE (u, '(A)') '1 Vessel -20 0 -10 0 0 0 0' + WRITE (u, '(A)') '2 Fixed -200 0 -100 0 0 0 0' + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + WRITE (u, '(A)') '1 1 2 -' + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + WRITE (u, '(A)') '1 cab 260 80' + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '100 WtrDpth' + WRITE (u, '(F5.3,A)') DTM, ' dtM' + WRITE (u, '(F6.2,A)') DTM*NT, ' TMax' + WRITE (u, '(A)') ROOT//'.txt vesselMotion' + WRITE (u, '(A)') '0|0|-10 vesselRef' + WRITE (u, '(A)') '--- OUTPUTS ---' + WRITE (u, '(A)') 'FairTen1' + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + c0 = cd_test_allocation_count() + CALL cd_test_phase_counting(1_C_INT, INT(N_WARM, C_LONG_LONG)) + CALL CD_Run_Deck_Driver(ROOT//'.dat', ROOT, converged, es, em) + n_steps = cd_test_phase_solve_entries() + CALL cd_test_phase_counting(0_C_INT, 0_C_LONG_LONG) + n_alloc = cd_test_allocation_count() - c0 + CALL check('driver run') + IF (.NOT. converged) ERROR STOP 'the driver run did not converge' + PRINT '(A,I0,A,I0,A,I0,A)', 'standalone driver, finite-EI line on a vessel: ', n_steps, ' steps (', & + n_steps - N_WARM, ' counted), ', n_alloc, ' heap allocations in their step phase' + IF (n_steps /= NT) THEN + PRINT '(A)', 'FAIL: the step phase window did not see every step (CD_Profile_Enter not wrapped?)' + ERROR STOP 1 + END IF + IF (n_alloc /= 0) THEN + PRINT '(A)', 'FAIL: the step phase of the standalone Hermite march allocated after warm-up' + ERROR STOP 1 + END IF + PRINT '(A)', 'PASS: no heap allocation in the step phase of the standalone Hermite march after warm-up' + END SUBROUTINE driver_vessel_gate + SUBROUTINE mixed_current_gate() !! Zero heap allocation in the standalone driver's per-step march of a mixed deck in its own !! current: a sagging finite-EI line beside a chain, both fairleads surging (motionFile), in