From 761d20144d0a201b4789c177f23d86c2b5140355 Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sat, 7 Nov 2026 20:58:07 +0900 Subject: [PATCH 1/4] feat(capi): return the section curvature of a whole line CurvX and CurvY were reachable in-process only as one channel per node, each of which evaluates the whole line. CD_C_LINE_SECTION_CURVATURE and Line.section_curvature() return both at every node of a finite-EI line in one evaluation, and match the channels bit for bit. Signed-off-by: Jae Hoon Seo --- CHANGELOG.md | 2 ++ doc/capi.rst | 5 +++-- doc/python.rst | 1 + python/cabledyn/objects.py | 12 ++++++++++- python/tests/test_host_fluid.py | 21 ++++++++++++++++++ src/CableDyn_CAPI.f90 | 38 ++++++++++++++++++++++++++++++--- src/CableDyn_CAPI.h | 2 ++ 7 files changed, 75 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfadb54d..02095703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ All notable changes to CableDyn are recorded in this file. The format follows `n_fluid_nodes`, `fluid_node_positions` and `update_fluid_fields`. The field is sampled and held as in the standalone march and in OpenFAST; in current and waves a stepped cable matches the standalone driver to the digits of its output. +- `Line.section_curvature()` and the C API quantity `CD_C_LINE_SECTION_CURVATURE` return `CurvX` + and `CurvY` at every node of a finite-EI line in one evaluation. ### Fixed diff --git a/doc/capi.rst b/doc/capi.rst index 5b647bb3..51bc79d0 100644 --- a/doc/capi.rst +++ b/doc/capi.rst @@ -1879,8 +1879,9 @@ handle numbers its lines 1..n). one value per node; ``_SEGMENT_TENSION`` (9), one value per segment; ``_DECK_NODE`` (10), one value per node: the deck node ``J`` of the node (the ```` of the node channels), 0 for a node a mesh refinement added, ``1 … n_nodes`` on a line - meshed as the deck. ``n_out`` must equal the value count. A library without code 10 - rejects it with ``CD_C_BAD_INPUT``. + meshed as the deck; ``_SECTION_CURVATURE`` (11), two values per node, ``CurvX`` and + ``CurvY`` interleaved, on a finite-EI line. ``n_out`` must equal the value count. A library + without code 10 or 11 rejects it with ``CD_C_BAD_INPUT``. * - ``CableDyn_GetPointState`` - position, velocity and the resultant force of the attached lines (``Point

F``), 3 doubles each diff --git a/doc/python.rst b/doc/python.rst index 75dc5427..9efa3ffa 100644 --- a/doc/python.rst +++ b/doc/python.rst @@ -847,6 +847,7 @@ state at the time of the call through the same evaluator as the deck ``OUTPUTS`` line = model.line(1) xyz = line.node_positions() # (n_nodes, 3), End A first seg = line.segment_tensions() # (n_segments,) + axes = line.section_curvature() # (n_nodes, 2): CurvX, CurvY of a finite-EI line kappa = line.curvature() # (n_nodes,); bend_moment() on finite-EI lines buf = np.empty_like(xyz) for _ in range(100): diff --git a/python/cabledyn/objects.py b/python/cabledyn/objects.py index c8078548..3051eedf 100644 --- a/python/cabledyn/objects.py +++ b/python/cabledyn/objects.py @@ -54,6 +54,7 @@ "declination", "azimuth", "segment_tension", + "section_curvature", ] _LINE_QUANTITIES: dict[str, tuple[int, int]] = { "position": (1, 3), @@ -65,6 +66,7 @@ "declination": (7, 1), "azimuth": (8, 1), "segment_tension": (9, 1), + "section_curvature": (11, 2), } #: C ABI code of the deck node of each mesh node (``CD_C_LINE_DECK_NODE``). @@ -219,7 +221,7 @@ def values(self, quantity: LineQuantity, out: FloatArray | None = None) -> Float ) code, width = _LINE_QUANTITIES[quantity] count = self.n_segments if quantity == "segment_tension" else self.n_nodes - shape = (count, 3) if width == 3 else (count,) + shape = (count, width) if width > 1 else (count,) buf = _buffer(out, shape) with self._model._lock: handle = self._handle() @@ -258,6 +260,14 @@ def curvature(self, out: FloatArray | None = None) -> FloatArray: """Curvature at each node ``(n_nodes,)`` [1/m] (``CurvN``).""" return self.values("curvature", out) + def section_curvature(self, out: FloatArray | None = None) -> FloatArray: + """Curvature on the section axes at each node ``(n_nodes, 2)`` [1/m]. + + Columns ``CurvXN`` and ``CurvYN``, on a finite-EI line; one evaluation + for the whole line, so it is much cheaper than a channel per node. + """ + return self.values("section_curvature", out) + def bend_moment(self, out: FloatArray | None = None) -> FloatArray: """Bend moment magnitude at each node ``(n_nodes,)`` [N-m]; zero when EI = 0.""" return self.values("bend_moment", out) diff --git a/python/tests/test_host_fluid.py b/python/tests/test_host_fluid.py index 1af63874..1991c761 100644 --- a/python/tests/test_host_fluid.py +++ b/python/tests/test_host_fluid.py @@ -404,3 +404,24 @@ def test_the_stepping_example_runs(): tensions = runpy.run_path(str(example))["main"](duration=2.0) assert len(tensions) == 40 assert all(math.isfinite(value) and value > 0.0 for value in tensions) + + +def test_section_curvature_of_a_line_matches_its_channels(tmp_path): + deck = _deck(tmp_path, "library", "", STIFFENER) + with CableDyn() as model: + model.init_deck_rotating(deck) + model.calc_output() + line = model.lines[0] + values = line.section_curvature() + assert values.shape == (line.n_nodes, 2) + tokens = [f"Curv{axis}1N{j}" for j in range(1, line.n_nodes + 1) for axis in "XY"] + expected = model.channels(tokens).reshape(-1, 2) + np.testing.assert_array_equal(values, expected) + assert np.abs(values).max() > 0.0 + np.testing.assert_allclose(np.hypot(*values.T), line.curvature(), rtol=1e-9, atol=1e-12) + + +def test_section_curvature_needs_a_finite_ei_line(tmp_path): + with CableDyn(_deck(tmp_path, "library", "", template=EI0_DECK)) as model: + with pytest.raises(CableDynError, match="finite-EI"): + model.lines[0].section_curvature() diff --git a/src/CableDyn_CAPI.f90 b/src/CableDyn_CAPI.f90 index 98a50426..f243e9db 100644 --- a/src/CableDyn_CAPI.f90 +++ b/src/CableDyn_CAPI.f90 @@ -31,7 +31,7 @@ MODULE CableDyn_CAPI CD_FAST_CalcOutputDerivatives, CD_FAST_GetCoupledMotion, CD_FAST_IsInitialized, & CD_FAST_NCoupledDOF, CD_FAST_NPoints, CD_FAST_NLines, CD_FAST_OK, CD_FAST_BADINPUT, & CD_FAST_NOT_INITIALIZED, CD_FAST_ALLOCFAIL, CD_FAST_UpdatePointFluidFields - USE CableDyn_OpenFAST_HermiteFMF, ONLY: CD_HFMF_ModuleType + USE CableDyn_OpenFAST_HermiteFMF, ONLY: CD_HFMF_ModuleType, CD_HFMF_Section_Curvature, CD_HFMF_OK USE CableDyn_Precision, ONLY: wp, CD_ZERO, CD_ONE USE CableDyn_System, ONLY: CD_SystemType, CD_System_Line_NDOF, CD_Get_System_PointBlocks, CD_SYSTEM_OK, & CD_POINT_COUPLED @@ -100,7 +100,8 @@ MODULE CableDyn_CAPI CD_C_LINE_TENSION = 3_C_INT, CD_C_LINE_ACCELERATION = 4_C_INT, & CD_C_LINE_CURVATURE = 5_C_INT, CD_C_LINE_BEND_MOMENT = 6_C_INT, & CD_C_LINE_DECLINATION = 7_C_INT, CD_C_LINE_AZIMUTH = 8_C_INT, & - CD_C_LINE_SEGMENT_TENSION = 9_C_INT, CD_C_LINE_DECK_NODE = 10_C_INT + CD_C_LINE_SEGMENT_TENSION = 9_C_INT, CD_C_LINE_DECK_NODE = 10_C_INT, & + CD_C_LINE_SECTION_CURVATURE = 11_C_INT !> Capacity of the per-handle diagnostic retained for CableDyn_GetLastError. INTEGER, PARAMETER :: CD_C_MSG_LEN = 4096 @@ -1740,7 +1741,8 @@ SUBROUTINE CableDyn_GetLineValues(handle, index, quantity, out_ptr, n_out, err_s !! quantity: CD_C_LINE_POSITION / VELOCITY / ACCELERATION (3 per node, xyz interleaved), !! TENSION / CURVATURE / BEND_MOMENT / DECLINATION / AZIMUTH (1 per node), or !! SEGMENT_TENSION (1 per segment), or DECK_NODE (1 per node: the deck node of each node of the - !! line's final mesh, the J of the node channels; 0 for a node a refinement added). Every quantity + !! line's final mesh, the J of the node channels; 0 for a node a refinement added), or + !! SECTION_CURVATURE (2 per node, CurvX and CurvY interleaved; finite-EI lines). Every quantity !! covers every node of the final mesh. n_out must equal the value count exactly. TYPE(C_PTR), VALUE :: handle, out_ptr INTEGER(C_INT), VALUE :: index, quantity, n_out @@ -1773,6 +1775,8 @@ SUBROUTINE CableDyn_GetLineValues(handle, index, quantity, out_ptr, n_out, err_s CASE (CD_C_LINE_TENSION, CD_C_LINE_CURVATURE, CD_C_LINE_BEND_MOMENT, CD_C_LINE_DECLINATION, & CD_C_LINE_AZIMUTH, CD_C_LINE_DECK_NODE) nvals = nnode + CASE (CD_C_LINE_SECTION_CURVATURE) + nvals = 2*nnode CASE (CD_C_LINE_SEGMENT_TENSION) nvals = nnode - 1 CASE DEFAULT @@ -1800,6 +1804,34 @@ SUBROUTINE CableDyn_GetLineValues(handle, index, quantity, out_ptr, n_out, err_s RETURN END IF + IF (quantity == CD_C_LINE_SECTION_CURVATURE) THEN + ! one evaluation for the whole line, in internal node order (node nn is End A) + IF (.NOT. h%use_agg) THEN + CALL set_status(h, CD_C_BAD_INPUT, 'CableDyn_GetLineValues: the section curvature is defined on '// & + 'finite-EI lines', err_stat) + RETURN + END IF + IF (.NOT. h%agg%line_is_cable(line_id)) THEN + CALL set_status(h, CD_C_BAD_INPUT, 'CableDyn_GetLineValues: the section curvature is defined on '// & + 'finite-EI lines', err_stat) + RETURN + END IF + BLOCK + REAL(wp) :: kx(nnode), ky(nnode) + CALL CD_HFMF_Section_Curvature(h%agg%cables(h%agg%line_obj_index(line_id)), kx, ky, es, em) + IF (es /= CD_HFMF_OK) THEN + CALL set_status(h, CD_C_SOLVE_FAIL, 'CableDyn_GetLineValues: '//TRIM(em), err_stat) + RETURN + END IF + DO node = 1, nnode + out_c(2*node - 1) = REAL(kx(nnode + 1 - node), C_DOUBLE) + out_c(2*node) = REAL(ky(nnode + 1 - node), C_DOUBLE) + END DO + END BLOCK + CALL store_status(h, CD_C_OK, '') + RETURN + END IF + IF (quantity == CD_C_LINE_SEGMENT_TENSION) THEN BLOCK REAL(wp) :: ten(nvals) diff --git a/src/CableDyn_CAPI.h b/src/CableDyn_CAPI.h index 65240c07..1bb4a7e3 100644 --- a/src/CableDyn_CAPI.h +++ b/src/CableDyn_CAPI.h @@ -151,6 +151,8 @@ int CableDyn_NLines(void *handle, int *err_stat); #define CD_C_LINE_DECK_NODE 10 /* 1 per mesh node: its deck node J, 0 if added by a * refinement (an earlier library rejects the * code with CD_C_BAD_INPUT) */ +#define CD_C_LINE_SECTION_CURVATURE 11 /* 2 per node, CurvX CurvY interleaved [1/m]; + * finite-EI lines (ABI minor 4) */ int CableDyn_GetAbiMinor(void); int CableDyn_NObjects(void *handle, int kind, int *err_stat); From bc828075a9284bf81d41d3c39357355f39cfe3bd Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sat, 7 Nov 2026 22:29:09 +0900 Subject: [PATCH 2/4] perf(capi): evaluate the node quantities of a line in one pass CableDyn_GetLineValues built a text token per node and value and parsed it back, and each EI = 0 or geometric token also rebuilt the whole line, so a read cost O(n) tokens and O(n^2) work. CD_Eval_Aggregate_Line_Values now evaluates position, velocity, acceleration, tension, curvature, bend moment, declination and azimuth for a whole line, on finite-EI cables and EI = 0 lines, on the aggregate and point routes. It shares the per-node rules with the channel evaluator (cable_recovered_value, system_node_tension, line_geom_from_r, cable_global_component, cable_global_azimuth), so every value is the channel's bit for bit. A failed or non-finite value still goes through its channel, which gives the same status and message as before. The line is looked up without building the inventory, and the one scratch array lives on the handle. test_line_values compares every quantity with the channels, value by value, on the point and aggregate routes and at every node of a refined mesh. test_c_api_threads now reads the lines of every handle at each step. Signed-off-by: Jae Hoon Seo --- CMakeLists.txt | 7 + doc/capi.rst | 5 + src/CableDyn_CAPI.f90 | 199 ++++++++--- src/CableDyn_DeckDriver.f90 | 325 +++++++++++++++--- tests/test_c_api_threads.c | 61 +++- tests/test_line_values.f90 | 651 ++++++++++++++++++++++++++++++++++++ 6 files changed, 1139 insertions(+), 109 deletions(-) create mode 100644 tests/test_line_values.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index d337db75..a94b01ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1087,6 +1087,13 @@ add_test(NAME aggregate_step_atomicity COMMAND test_aggregate_step_atomicity WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) set_tests_properties(aggregate_step_atomicity PROPERTIES LABELS "fortran;unit;dynamic;capi") +add_executable(test_line_values tests/test_line_values.f90) +target_link_libraries(test_line_values PRIVATE cabledyn_core) +add_test(NAME line_values_match_channels + COMMAND test_line_values ${CMAKE_CURRENT_SOURCE_DIR}/tests/data/lazy_wave_touchdown_planar_coarse.dat + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +set_tests_properties(line_values_match_channels PROPERTIES LABELS "fortran;unit;dynamic;capi") + add_executable(test_core_input_guards tests/test_core_input_guards.f90) target_link_libraries(test_core_input_guards PRIVATE cabledyn_core) add_test(NAME core_input_guards COMMAND test_core_input_guards WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) diff --git a/doc/capi.rst b/doc/capi.rst index 51bc79d0..b4fd1395 100644 --- a/doc/capi.rst +++ b/doc/capi.rst @@ -1903,6 +1903,11 @@ On a finite-EI line whose mesh the static solve refined, ``n_nodes`` and the (:ref:`outputs-refined-nodes`); ``CD_C_LINE_DECK_NODE`` locates it: the array row whose value is 5. +``CableDyn_GetLineValues`` evaluates a whole line in one call, with no text token per node, and +each value is bit for bit the node channel's. It is the cheap way to read many nodes at every +step. Reading the tension, curvature and bend moment of a line after a step shares one evaluation +of its elements. + A ``NULL`` output pointer skips that output, except for the ``out`` array of ``CableDyn_GetLineValues`` and the ``value`` of ``CableDyn_EvalChannel``; the latter is not checked and must point to valid storage. An index out of range, a wrongly sized buffer, an diff --git a/src/CableDyn_CAPI.f90 b/src/CableDyn_CAPI.f90 index f243e9db..6bd537dc 100644 --- a/src/CableDyn_CAPI.f90 +++ b/src/CableDyn_CAPI.f90 @@ -15,8 +15,8 @@ MODULE CableDyn_CAPI USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY: IEEE_IS_FINITE USE CableDyn_DeckDriver, ONLY: CD_Init_Deck_System, CD_DECKDRV_OK, CD_DECKDRV_BADINPUT, CD_DECKDRV_SOLVEFAIL, & CD_Deck_Query_dtM, CD_DECK_NAMELEN, CD_Eval_Aggregate_Channel, & - CD_Eval_Aggregate_Segment_Tensions, CD_Rigid6_Inventory, CD_Rod_Inventory, & - CD_Channel_Token_Parses, CD_Cable_Deck_Node + CD_Eval_Aggregate_Segment_Tensions, CD_Eval_Aggregate_Line_Values, & + CD_Rigid6_Inventory, CD_Rod_Inventory, CD_Channel_Token_Parses, CD_Cable_Deck_Node USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, & CD_AGG_UpdateStates_Moving, CD_AGG_Step_Moving, CD_AGG_CalcOutput, & CD_AGG_GetMovingPointMesh, CD_AGG_End, CD_AGG_IsInitialized, CD_AGG_OK, & @@ -161,6 +161,8 @@ END FUNCTION cabledyn_blas_threads_once REAL(wp), ALLOCATABLE :: fluid_velocity_work(:, :) REAL(wp), ALLOCATABLE :: fluid_acceleration_work(:, :) REAL(wp), ALLOCATABLE :: waterline_work(:) + ! CableDyn_GetLineValues: one value per node of the longest line read so far. + REAL(wp), ALLOCATABLE :: line_work(:) ! Decks with BODIES or RODS run on the coupled aggregate (use_agg): its moving points are the ! coupled DOFs, its step is the deck dtM (agg_dt), and agg_t is its committed time. LOGICAL :: use_agg = .FALSE. @@ -1743,35 +1745,25 @@ SUBROUTINE CableDyn_GetLineValues(handle, index, quantity, out_ptr, n_out, err_s !! SEGMENT_TENSION (1 per segment), or DECK_NODE (1 per node: the deck node of each node of the !! line's final mesh, the J of the node channels; 0 for a node a refinement added), or !! SECTION_CURVATURE (2 per node, CurvX and CurvY interleaved; finite-EI lines). Every quantity - !! covers every node of the final mesh. n_out must equal the value count exactly. + !! covers every node of the final mesh. n_out must equal the value count exactly. The node + !! channels are evaluated for the whole line at once (CD_Eval_Aggregate_Line_Values). TYPE(C_PTR), VALUE :: handle, out_ptr INTEGER(C_INT), VALUE :: index, quantity, n_out INTEGER(C_INT), INTENT(OUT) :: err_stat TYPE(CD_C_Handle), POINTER :: h REAL(C_DOUBLE), POINTER :: out_c(:) - INTEGER, ALLOCATABLE :: ids(:), nnodes(:), sub(:) - INTEGER :: nnode, nvals, line_id, node, c, es - REAL(wp) :: val - CHARACTER(CD_DECK_NAMELEN) :: token, stem + INTEGER :: nnode, nvals, line_id, node, es, n_done, bad CHARACTER(CD_C_MSG_LEN) :: em - CHARACTER(1) :: field - CHARACTER(1), PARAMETER :: xyz(3) = ['x', 'y', 'z'] IF (.NOT. query_ready(handle, h, 'CableDyn_GetLineValues', err_stat)) RETURN - CALL object_inventory(h, INT(CD_C_OBJ_LINE), ids, nnodes, sub, err_stat) - IF (err_stat /= CD_C_OK) RETURN - IF (index < 0_C_INT .OR. INT(index) >= SIZE(ids)) THEN + CALL line_at_index(h, INT(index), line_id, nnode) + IF (line_id < 1) THEN CALL set_status(h, CD_C_BAD_INPUT, 'CableDyn_GetLineValues: line index out of range', err_stat) RETURN END IF - line_id = ids(index + 1) - nnode = nnodes(index + 1) - field = 'p' SELECT CASE (quantity) CASE (CD_C_LINE_POSITION, CD_C_LINE_VELOCITY, CD_C_LINE_ACCELERATION) nvals = 3*nnode - IF (quantity == CD_C_LINE_VELOCITY) field = 'v' - IF (quantity == CD_C_LINE_ACCELERATION) field = 'a' CASE (CD_C_LINE_TENSION, CD_C_LINE_CURVATURE, CD_C_LINE_BEND_MOMENT, CD_C_LINE_DECLINATION, & CD_C_LINE_AZIMUTH, CD_C_LINE_DECK_NODE) nvals = nnode @@ -1853,31 +1845,35 @@ SUBROUTINE CableDyn_GetLineValues(handle, index, quantity, out_ptr, n_out, err_s RETURN END IF - DO node = 1, nnode - stem = TRIM(int_text(line_id))//'N'//TRIM(int_text(node)) - SELECT CASE (quantity) - CASE (CD_C_LINE_POSITION, CD_C_LINE_VELOCITY, CD_C_LINE_ACCELERATION) - DO c = 1, 3 - CALL eval_token(h, 'L'//TRIM(stem)//field//xyz(c), val, err_stat, mesh_node=.TRUE.) - IF (err_stat /= CD_C_OK) RETURN - out_c(3*(node - 1) + c) = REAL(val, C_DOUBLE) - END DO - CYCLE - CASE (CD_C_LINE_TENSION) - token = 'Ten'//TRIM(stem) - CASE (CD_C_LINE_CURVATURE) - token = 'Curv'//TRIM(stem) - CASE (CD_C_LINE_BEND_MOMENT) - token = 'BendMom'//TRIM(stem) - CASE (CD_C_LINE_DECLINATION) - token = 'L'//TRIM(stem)//'Dec' - CASE DEFAULT - token = 'L'//TRIM(stem)//'Azi' - END SELECT - CALL eval_token(h, token, val, err_stat, mesh_node=.TRUE.) - IF (err_stat /= CD_C_OK) RETURN - out_c(node) = REAL(val, C_DOUBLE) + ! the node channels, whose codes are the quantity codes + IF (.NOT. line_work_ready(h, nnode)) THEN + CALL set_status(h, CD_C_ALLOC_FAIL, 'CableDyn_GetLineValues: workspace allocation failed', err_stat) + RETURN + END IF + IF (h%use_agg) THEN + CALL CD_Eval_Aggregate_Line_Values(line_id, INT(quantity), h%agg%has_sys, h%agg%sys%fast%system, h%agg%cables, & + h%agg%line_is_cable, h%agg%line_obj_index, out_c, h%line_work, n_done, es, em) + ELSE + CALL CD_Eval_Aggregate_Line_Values(line_id, INT(quantity), .TRUE., h%fast%system, h%no_cables, h%pt_is_cable, & + h%pt_obj, out_c, h%line_work, n_done, es, em) + END IF + ! the first value that failed: its channel gives the status and the reason, as a token would + bad = 0 + DO node = 1, n_done + IF (.NOT. IEEE_IS_FINITE(out_c(node))) THEN + bad = node + EXIT + END IF END DO + IF (bad == 0 .AND. es /= CD_DECKDRV_OK) bad = n_done + 1 + IF (bad > 0) THEN + CALL line_value_failure(h, line_id, quantity, bad, err_stat) + IF (err_stat /= CD_C_OK) RETURN + CALL map_deck_status(es, err_stat) + IF (err_stat == CD_C_OK) err_stat = CD_C_SOLVE_FAIL + CALL store_status(h, err_stat, 'CableDyn_GetLineValues: '//TRIM(em)) + RETURN + END IF CALL store_status(h, CD_C_OK, '') END SUBROUTINE CableDyn_GetLineValues @@ -2409,6 +2405,113 @@ LOGICAL FUNCTION query_ready(handle, h, caller, err_stat) RESULT(ready) ready = .TRUE. END FUNCTION query_ready + SUBROUTINE line_at_index(h, index, line_id, nnode) + !! Deck id and node count of entry `index` (0-based) of the line inventory (ascending deck id, + !! object_inventory's order) without building it; line_id = 0 when there is no such entry. + TYPE(CD_C_Handle), POINTER, INTENT(INOUT) :: h + INTEGER, INTENT(IN) :: index + INTEGER, INTENT(OUT) :: line_id, nnode + + line_id = 0 + nnode = 0 + IF (h%use_agg) THEN + CALL find_line(h%agg%line_is_cable, h%agg%line_obj_index) + ELSE + CALL find_line(h%pt_is_cable, h%pt_obj) + END IF + + CONTAINS + + SUBROUTINE find_line(is_cable, obj_index) + LOGICAL, ALLOCATABLE, INTENT(IN) :: is_cable(:) + INTEGER, ALLOCATABLE, INTENT(IN) :: obj_index(:) + INTEGER :: k, n + IF (index < 0 .OR. .NOT. (ALLOCATED(obj_index) .AND. ALLOCATED(is_cable))) RETURN + n = -1 + DO k = 1, SIZE(obj_index) + IF (obj_index(k) < 1) CYCLE + n = n + 1 + IF (n /= index) CYCLE + line_id = k + nnode = line_node_count(h, is_cable(k), obj_index(k)) + RETURN + END DO + END SUBROUTINE find_line + END SUBROUTINE line_at_index + + INTEGER FUNCTION line_node_count(h, is_cable, obj) RESULT(nnode) + !! Node count of a line of the handle's model: cable or system line obj. + TYPE(CD_C_Handle), POINTER, INTENT(INOUT) :: h + LOGICAL, INTENT(IN) :: is_cable + INTEGER, INTENT(IN) :: obj + INTEGER :: es + CHARACTER(CD_C_MSG_LEN) :: em + + IF (is_cable) THEN + nnode = h%agg%cables(obj)%line%nn + ELSE IF (h%use_agg) THEN + nnode = CD_System_Line_NDOF(h%agg%sys%fast%system, obj, es, em)/3 + ELSE + nnode = CD_System_Line_NDOF(h%fast%system, obj, es, em)/3 + END IF + END FUNCTION line_node_count + + LOGICAL FUNCTION line_work_ready(h, nnode) RESULT(ready) + !! Make the handle's line workspace hold nnode values; it only ever grows. + TYPE(CD_C_Handle), POINTER, INTENT(INOUT) :: h + INTEGER, INTENT(IN) :: nnode + INTEGER :: ios + + ready = .TRUE. + IF (ALLOCATED(h%line_work)) THEN + IF (SIZE(h%line_work) >= nnode) RETURN + DEALLOCATE (h%line_work) + END IF + ALLOCATE (h%line_work(nnode), STAT=ios) + ready = ios == 0 + END FUNCTION line_work_ready + + SUBROUTINE line_value_failure(h, line_id, quantity, value_index, err_stat) + !! Evaluate value value_index (1-based, in the array of CableDyn_GetLineValues) through its + !! channel, which sets the status and the message of a failed or non-finite node value. + TYPE(CD_C_Handle), POINTER, INTENT(INOUT) :: h + INTEGER, INTENT(IN) :: line_id, value_index + INTEGER(C_INT), INTENT(IN) :: quantity + INTEGER(C_INT), INTENT(OUT) :: err_stat + INTEGER :: node, c + REAL(wp) :: val + CHARACTER(CD_DECK_NAMELEN) :: token, stem + CHARACTER(1), PARAMETER :: xyz(3) = ['x', 'y', 'z'] + + node = value_index + c = 1 + IF (quantity == CD_C_LINE_POSITION .OR. quantity == CD_C_LINE_VELOCITY .OR. & + quantity == CD_C_LINE_ACCELERATION) THEN + node = (value_index - 1)/3 + 1 + c = value_index - 3*(node - 1) + END IF + stem = TRIM(int_text(line_id))//'N'//TRIM(int_text(node)) + SELECT CASE (quantity) + CASE (CD_C_LINE_POSITION) + token = 'L'//TRIM(stem)//'p'//xyz(c) + CASE (CD_C_LINE_VELOCITY) + token = 'L'//TRIM(stem)//'v'//xyz(c) + CASE (CD_C_LINE_ACCELERATION) + token = 'L'//TRIM(stem)//'a'//xyz(c) + CASE (CD_C_LINE_TENSION) + token = 'Ten'//TRIM(stem) + CASE (CD_C_LINE_CURVATURE) + token = 'Curv'//TRIM(stem) + CASE (CD_C_LINE_BEND_MOMENT) + token = 'BendMom'//TRIM(stem) + CASE (CD_C_LINE_DECLINATION) + token = 'L'//TRIM(stem)//'Dec' + CASE DEFAULT + token = 'L'//TRIM(stem)//'Azi' + END SELECT + CALL eval_token(h, token, val, err_stat, mesh_node=.TRUE.) + END SUBROUTINE line_value_failure + SUBROUTINE object_inventory(h, kind, ids, nnodes, sub, err_stat) !! The handle's objects of one kind: deck ids, node counts and subtypes. Lines are listed in !! ascending deck id, points in system order, bodies and rods in runtime order. @@ -2465,8 +2568,7 @@ SUBROUTINE object_inventory(h, kind, ids, nnodes, sub, err_stat) SUBROUTINE collect_lines(is_cable, obj_index) LOGICAL, ALLOCATABLE, INTENT(IN) :: is_cable(:) INTEGER, ALLOCATABLE, INTENT(IN) :: obj_index(:) - INTEGER :: k, n, obj, es - CHARACTER(CD_C_MSG_LEN) :: em + INTEGER :: k, n, obj IF (.NOT. (ALLOCATED(obj_index) .AND. ALLOCATED(is_cable))) THEN ALLOCATE (ids(0), nnodes(0), sub(0)) RETURN @@ -2479,15 +2581,8 @@ SUBROUTINE collect_lines(is_cable, obj_index) IF (obj < 1) CYCLE n = n + 1 ids(n) = k - sub(n) = 0 - IF (is_cable(k)) THEN - nnodes(n) = h%agg%cables(obj)%line%nn - sub(n) = 1 - ELSE IF (h%use_agg) THEN - nnodes(n) = CD_System_Line_NDOF(h%agg%sys%fast%system, obj, es, em)/3 - ELSE - nnodes(n) = CD_System_Line_NDOF(h%fast%system, obj, es, em)/3 - END IF + sub(n) = MERGE(1, 0, is_cable(k)) + nnodes(n) = line_node_count(h, is_cable(k), obj) END DO END SUBROUTINE collect_lines diff --git a/src/CableDyn_DeckDriver.f90 b/src/CableDyn_DeckDriver.f90 index 1f6f7832..27214031 100644 --- a/src/CableDyn_DeckDriver.f90 +++ b/src/CableDyn_DeckDriver.f90 @@ -258,6 +258,7 @@ MODULE CableDyn_DeckDriver ! In-process object queries of the C API (CableDyn_CAPI): segment tensions in the .t.out ! convention, and the body/rod inventories the Body/Rod channels resolve on. PUBLIC :: CD_Eval_Aggregate_Segment_Tensions, CD_Rigid6_Inventory, CD_Rod_Inventory, CD_Channel_Token_Parses + PUBLIC :: CD_Eval_Aggregate_Line_Values ! The refusal of the LINES flag r on a route that writes no range file. CHARACTER(*), PARAMETER :: RANGE_FLAG_REFUSED = 'the LINES Outputs flag r (range graph) is written by the '// & 'standalone driver and a single-turbine OpenFAST run; a FAST.Farm shared '// & @@ -36102,6 +36103,19 @@ PURE REAL(wp) FUNCTION line_geom_value(kind, deck_node, nnode, q_flat, stride, s v = line_geom_from_r(kind, r, nnode, deck_node, signed) END FUNCTION line_geom_value + PURE SUBROUTINE line_geom_values(kind, nnode, stride, q, signed, values) + !! line_geom_value at every deck node of a line. q(:, n) is internal node n's state, its first + !! three entries the position; the deck-ordered positions are read in place, not copied. + INTEGER, INTENT(IN) :: kind, nnode, stride + REAL(wp), INTENT(IN) :: q(stride, nnode) + LOGICAL, INTENT(IN) :: signed + REAL(wp), INTENT(OUT) :: values(nnode) + INTEGER :: j + DO j = 1, nnode + values(j) = line_geom_from_r(kind, q(1:3, nnode:1:-1), nnode, j, signed) + END DO + END SUBROUTINE line_geom_values + PURE REAL(wp) FUNCTION line_geom_from_r(kind, r, nnode, deck_node, signed) RESULT(v) !! Geometric channel value (kind: 5 curvature, 7 declination, 8 azimuth) from the !! deck-ordered node positions r(3, nnode). Factored out of line_geom_value so a caller @@ -36109,7 +36123,7 @@ PURE REAL(wp) FUNCTION line_geom_from_r(kind, r, nnode, deck_node, signed) RESUL !! rather than rebuilding it inside each per-node call -- O(nnode) instead of O(nnode^2). !! signed: the line's azimuth range (DeckLine%azi_signed). INTEGER, INTENT(IN) :: kind, nnode, deck_node - REAL(wp), INTENT(IN) :: r(3, nnode) + REAL(wp), INTENT(IN) :: r(:, :) ! 3 x nnode, deck order LOGICAL, INTENT(IN), OPTIONAL :: signed v = CD_ZERO SELECT CASE (kind) @@ -37210,6 +37224,177 @@ SUBROUTINE CD_Eval_Aggregate_Segment_Tensions(line_id, has_sys, system, cables, END BLOCK END SUBROUTINE CD_Eval_Aggregate_Segment_Tensions + SUBROUTINE CD_Eval_Aggregate_Line_Values(line_id, kind, has_sys, system, cables, line_is_cable, line_obj_index, & + values, work, n_done, ErrStat, ErrMsg) + !! One line-node channel at every node of the final mesh of deck line line_id, End A first, + !! with no token per node. kind is the channel's code (parse_line_node_channel): 1, 2 and 4 + !! give three values per node, xyz interleaved, and 3, 5, 6, 7 and 8 one. Every value is bit + !! for bit the one CD_Eval_Aggregate_Channel returns for that node with mesh_node = .TRUE.: + !! the two routes share the per-node rules (cable_recovered_value, system_node_tension, + !! line_geom_from_r, cable_global_component, cable_global_azimuth). work holds at least one + !! value per element. On a failure n_done counts the values written before it, in order, so a + !! caller can name the first bad value's channel. + INTEGER, INTENT(IN) :: line_id, kind + LOGICAL, INTENT(IN) :: has_sys + TYPE(CD_SystemType), INTENT(IN) :: system + TYPE(CD_HFMF_ModuleType), ALLOCATABLE, INTENT(INOUT) :: cables(:) + LOGICAL, INTENT(IN) :: line_is_cable(:) + INTEGER, INTENT(IN) :: line_obj_index(:) + REAL(wp), INTENT(OUT) :: values(:) + REAL(wp), INTENT(INOUT) :: work(:) + INTEGER, INTENT(OUT) :: n_done, ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + INTEGER :: obj + + ErrStat = CD_DECKDRV_OK + ErrMsg = '' + n_done = 0 + IF (kind < 1 .OR. kind > 8) THEN + CALL fail(ErrStat, ErrMsg, 'line values: not a line-node channel code'); RETURN + END IF + IF (line_id < 1 .OR. line_id > SIZE(line_obj_index)) THEN + CALL fail(ErrStat, ErrMsg, 'line values: unknown line id'); RETURN + END IF + obj = line_obj_index(line_id) + IF (obj < 1) THEN + CALL fail(ErrStat, ErrMsg, 'line values: line id maps to no built line'); RETURN + END IF + IF (line_is_cable(line_id)) THEN + IF (.NOT. ALLOCATED(cables)) THEN + CALL fail(ErrStat, ErrMsg, 'line values: line maps to a cable but none are built'); RETURN + END IF + IF (obj > SIZE(cables)) THEN + CALL fail(ErrStat, ErrMsg, 'line values: cable index out of range'); RETURN + END IF + CALL cable_line_values(cables(obj), kind, values, n_done, ErrStat, ErrMsg) + ELSE + IF (.NOT. has_sys) THEN + CALL fail(ErrStat, ErrMsg, 'line values: line maps to the EI=0 system but no system is present'); RETURN + END IF + CALL system_line_values(system, obj, kind, values, work, n_done, ErrStat, ErrMsg) + END IF + END SUBROUTINE CD_Eval_Aggregate_Line_Values + + SUBROUTINE system_line_values(system, li, kind, values, work, n_done, ErrStat, ErrMsg) + !! CD_Eval_Aggregate_Line_Values on EI=0 system line li (eval_system_line_channel at every node). + TYPE(CD_SystemType), INTENT(IN) :: system + INTEGER, INTENT(IN) :: li, kind + REAL(wp), INTENT(OUT) :: values(:) + REAL(wp), INTENT(INOUT) :: work(:) + INTEGER, INTENT(OUT) :: n_done, ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + INTEGER :: ndof, nnode, node, es + CHARACTER(1024) :: em + + ErrStat = CD_DECKDRV_OK + ErrMsg = '' + n_done = 0 + ndof = CD_System_Line_NDOF(system, li, es, em) + IF (es /= CD_SYSTEM_OK) THEN + CALL fail_solve(ErrStat, ErrMsg, 'line values: '//TRIM(em)); RETURN + END IF + nnode = ndof/3 + IF (SIZE(values) /= MERGE(ndof, nnode, kind == 1 .OR. kind == 2 .OR. kind == 4) .OR. SIZE(work) < nnode - 1) THEN + CALL fail(ErrStat, ErrMsg, 'line values: output size does not match the line'); RETURN + END IF + SELECT CASE (kind) + CASE (1) ! position + CALL node_vectors_reversed(system%lines(li)%q, nnode, values) + CASE (2) ! velocity + CALL node_vectors_reversed(system%lines(li)%v, nnode, values) + CASE (4) ! acceleration + CALL node_vectors_reversed(system%lines(li)%a, nnode, values) + CASE (3) ! nodal tension + CALL CD_Get_System_Line_Tension(system, li, work(1:nnode - 1), es, em) + IF (es /= CD_SYSTEM_OK) THEN + CALL fail_solve(ErrStat, ErrMsg, 'line values: '//TRIM(em)); RETURN + END IF + DO node = 1, nnode + CALL system_node_tension(system%lines(li), work(1:nnode - 1), deck_to_internal_node(node, nnode - 1), nnode, & + values(node), es, em) + IF (es /= CD_DECKDRV_OK) THEN + CALL fail_solve(ErrStat, ErrMsg, 'line values: '//TRIM(em)); RETURN + END IF + n_done = node + END DO + CASE DEFAULT ! curvature (5) / bend moment (6, EI=0 => 0) / declination (7) / azimuth (8) + CALL line_geom_values(kind, nnode, 3, system%lines(li)%q, system%lines(li)%azimuth_signed, values) + END SELECT + n_done = SIZE(values) + END SUBROUTINE system_line_values + + PURE SUBROUTINE node_vectors_reversed(state, nnode, values) + !! The 3-vectors of an EI=0 line's nodes (internal order) in public order, xyz interleaved. + INTEGER, INTENT(IN) :: nnode + REAL(wp), INTENT(IN) :: state(3, nnode) + REAL(wp), INTENT(OUT) :: values(3, nnode) + values = state(:, nnode:1:-1) + END SUBROUTINE node_vectors_reversed + + SUBROUTINE cable_line_values(cable, kind, values, n_done, ErrStat, ErrMsg) + !! CD_Eval_Aggregate_Line_Values on a finite-EI cable (eval_cable_channel at every node of the + !! final mesh). The tension, curvature and bend moment of the nodes share one element + !! evaluation through the cache of CD_HermiteCable_Dyn_Node_Values. + TYPE(CD_HFMF_ModuleType), INTENT(INOUT) :: cable + INTEGER, INTENT(IN) :: kind + REAL(wp), INTENT(OUT) :: values(:) + INTEGER, INTENT(OUT) :: n_done, ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + INTEGER :: nnode, nelem, node + + ErrStat = CD_DECKDRV_OK + ErrMsg = '' + n_done = 0 + IF (.NOT. cable_state_ready(cable)) THEN + CALL fail(ErrStat, ErrMsg, 'line values: cable state not available'); RETURN + END IF + nelem = cable%line%ne + nnode = cable%line%nn + IF (SIZE(values) /= MERGE(3*nnode, nnode, kind == 1 .OR. kind == 2 .OR. kind == 4)) THEN + CALL fail(ErrStat, ErrMsg, 'line values: output size does not match the line'); RETURN + END IF + SELECT CASE (kind) + CASE (1) ! position + CALL cable_global_vectors(cable, cable%line%q, values) + CASE (2) ! velocity + CALL cable_global_vectors(cable, cable%line%v, values) + CASE (4) ! acceleration + CALL cable_global_vectors(cable, cable%line%a, values) + CASE (3, 5, 6) ! nodal tension, curvature, bend moment + DO node = 1, nnode + CALL cable_recovered_value(cable, kind, deck_to_internal_node(node, nelem), 'line values', values(node), & + ErrStat, ErrMsg) + IF (ErrStat /= CD_DECKDRV_OK) RETURN + n_done = node + END DO + CASE DEFAULT ! declination (7), azimuth (8) + CALL line_geom_values(kind, nnode, 6, cable%line%q, cable%azimuth_signed, values) + IF (kind == 8) THEN + DO node = 1, nnode + values(node) = cable_global_azimuth(cable, values(node)) + END DO + END IF + END SELECT + n_done = SIZE(values) + END SUBROUTINE cable_line_values + + PURE SUBROUTINE cable_global_vectors(cable, state, values) + !! A cable's nodal vector state (internal order, stride 6) in the deck's global frame and + !! public order, xyz interleaved. + TYPE(CD_HFMF_ModuleType), INTENT(IN) :: cable + REAL(wp), CONTIGUOUS, INTENT(IN) :: state(:) + REAL(wp), INTENT(OUT) :: values(:) + INTEGER :: nnode, node, c, base + + nnode = cable%line%nn + DO node = 1, nnode + base = 6*(nnode - node) + DO c = 1, 3 + values(3*(node - 1) + c) = cable_global_component(cable, state(base + 1:base + 3), c) + END DO + END DO + END SUBROUTINE cable_global_vectors + SUBROUTINE CD_Rigid6_Inventory(rt, body_ids, rod_ids, rod_nsegs) !! Deck ids of the Rigid6 bodies the runtime owns (runtime order), and of the rods fixed or !! pinned to them (body order, then attachment order) with their NumSegs: the objects the @@ -37323,6 +37508,22 @@ SUBROUTINE line_model_end_tension(model, inode, nnode, val, ErrStat, ErrMsg) END IF END SUBROUTINE line_model_end_tension + SUBROUTINE system_node_tension(model, tension, inode, nnode, val, ErrStat, ErrMsg) + !! Tension at node inode (internal order) of an EI=0 line from its element tensions: the mean + !! of the two adjacent elements, and at a line end the force on the end's point. + TYPE(CD_ModelType), INTENT(IN) :: model + REAL(wp), INTENT(IN) :: tension(:) + INTEGER, INTENT(IN) :: inode, nnode + REAL(wp), INTENT(OUT) :: val + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + + ErrStat = CD_DECKDRV_OK + ErrMsg = '' + val = nodal_tension_value(tension, inode) + IF (inode == 1 .OR. inode == nnode) CALL line_model_end_tension(model, inode, nnode, val, ErrStat, ErrMsg) + END SUBROUTINE system_node_tension + SUBROUTINE eval_system_line_channel(system, li, ch, val, ErrStat, ErrMsg) !! Evaluate one channel for EI=0 system line `li` (internal system line index). Mirrors the !! standalone driver's system_channel_value but takes the resolved line index directly and @@ -37400,12 +37601,9 @@ SUBROUTINE eval_system_line_channel(system, li, ch, val, ErrStat, ErrMsg) IF (es /= CD_SYSTEM_OK) THEN CALL sys_solvefail(ch, em, ErrStat, ErrMsg); RETURN END IF - val = nodal_tension_value(tension, inode) - IF (inode == 1 .OR. inode == nnode) THEN - CALL line_model_end_tension(system%lines(li), inode, nnode, val, es, em) - IF (es /= CD_DECKDRV_OK) THEN - CALL sys_solvefail(ch, em, ErrStat, ErrMsg); RETURN - END IF + CALL system_node_tension(system%lines(li), tension, inode, nnode, val, es, em) + IF (es /= CD_DECKDRV_OK) THEN + CALL sys_solvefail(ch, em, ErrStat, ErrMsg); RETURN END IF END BLOCK RETURN @@ -37469,7 +37667,7 @@ SUBROUTINE eval_cable_channel(cable, ch, val, ErrStat, ErrMsg, mesh_node) lo = to_lower(ch) nelem = cable%line%ne nnode = cable%line%nn - IF (nelem < 1 .OR. nnode /= nelem + 1 .OR. SIZE(cable%line%q) < 6*nnode) THEN + IF (.NOT. cable_state_ready(cable)) THEN CALL fail(ErrStat, ErrMsg, 'OUTPUT "'//TRIM(ch)//'": cable state not available'); RETURN END IF IF (starts_with(lo, 'fairten')) THEN @@ -37540,47 +37738,14 @@ SUBROUTINE eval_cable_channel(cable, ch, val, ErrStat, ErrMsg, mesh_node) END IF inode = deck_to_internal_node(node, nelem) SELECT CASE (kind) - CASE (3) ! nodal tension - BLOCK - IF (inode == 1 .OR. inode == nnode) THEN - ! a line end: the same support-reaction tension as FairTen/AnchTen - CALL cable_end_tension(cable, inode, val, ErrStat, ErrMsg) - ELSE IF (cable%line%output_element_recovery) THEN - ! OutputRecovery element: the length-weighted average of the two adjacent - ! element-mean axial forces EA (|r'| - 1) - val = (cable%line%l0(inode - 1)*cable_element_mean_tension(cable, inode - 1) + & - cable%line%l0(inode)*cable_element_mean_tension(cable, inode))/ & - (cable%line%l0(inode - 1) + cable%line%l0(inode)) - ELSE - ! effective tension from nodal equilibrium (CD_HermiteCable_Dyn_Node_Recovery) - CALL cable_node_values(cable, inode, val, sec_curv, sec_bend, ErrStat, ErrMsg) - IF (ErrStat /= CD_DECKDRV_OK) RETURN - END IF - END BLOCK + CASE (3, 5, 6) ! nodal tension, curvature, bend moment + CALL cable_recovered_value(cable, kind, inode, ch, val, ErrStat, ErrMsg) CASE (1) ! position (cable-local state rotated to the deck's global frame) val = cable_global_component(cable, cable%line%q(6*(inode - 1) + 1:6*(inode - 1) + 3), comp) CASE (2) ! velocity val = cable_global_component(cable, cable%line%v(6*(inode - 1) + 1:6*(inode - 1) + 3), comp) CASE (4) ! acceleration val = cable_global_component(cable, cable%line%a(6*(inode - 1) + 1:6*(inode - 1) + 3), comp) - CASE (5) ! curvature: recovered bend moment / EI, or the larger exact one-sided trace - IF (.NOT. cable%line%output_element_recovery) THEN - CALL cable_node_values(cable, inode, sec_ten, val, sec_bend, ErrStat, ErrMsg) - RETURN - END IF - CALL cable_nodal_curvature(cable, inode, val, es, em) - IF (es /= CD_HCABLE_OK) THEN - CALL fail_solve(ErrStat, ErrMsg, 'OUTPUT "'//TRIM(ch)//'": '//TRIM(em)); RETURN - END IF - CASE (6) ! bend moment: recovered from nodal equilibrium, or EI times the larger exact trace - IF (.NOT. cable%line%output_element_recovery) THEN - CALL cable_node_values(cable, inode, sec_ten, sec_curv, val, ErrStat, ErrMsg) - RETURN - END IF - CALL cable_nodal_bend_moment(cable, inode, val, es, em) - IF (es /= CD_HCABLE_OK) THEN - CALL fail_solve(ErrStat, ErrMsg, 'OUTPUT "'//TRIM(ch)//'": '//TRIM(em)); RETURN - END IF CASE (12) ! the bend stiffener's share of the section moment (zero outside a stiffener) IF (.NOT. cable%line%output_element_recovery) THEN CALL cable_node_values(cable, inode, sec_ten, sec_curv, sec_bend, ErrStat, ErrMsg, sleeve_moment=val) @@ -37646,16 +37811,80 @@ SUBROUTINE eval_cable_channel(cable, ch, val, ErrStat, ErrMsg, mesh_node) END IF END BLOCK CASE (8) ! azimuth: local-frame azimuth plus the cable's frame heading, wrapped - val = line_geom_value(kind, node, nnode, cable%line%q(1:6*nnode), 6, cable%azimuth_signed) - IF (.NOT. cable_frame_is_identity(cable)) THEN - val = wrap_azimuth_deg(val + ATAN2(cable%frame_s, cable%frame_c)*45.0_wp/QUARTER_PI, & - cable%azimuth_signed) - END IF + val = cable_global_azimuth(cable, line_geom_value(kind, node, nnode, cable%line%q(1:6*nnode), 6, & + cable%azimuth_signed)) CASE DEFAULT ! declination (7) -- rotation-invariant geometric scalar val = line_geom_value(kind, node, nnode, cable%line%q(1:6*nnode), 6) END SELECT END SUBROUTINE eval_cable_channel + PURE LOGICAL FUNCTION cable_state_ready(cable) RESULT(ready) + !! Whether a finite-EI cable holds the state its channels read. + TYPE(CD_HFMF_ModuleType), INTENT(IN) :: cable + ready = .FALSE. + IF (cable%line%ne < 1 .OR. cable%line%nn /= cable%line%ne + 1) RETURN + ready = SIZE(cable%line%q) >= 6*cable%line%nn + END FUNCTION cable_state_ready + + SUBROUTINE cable_recovered_value(cable, kind, inode, label, val, ErrStat, ErrMsg) + !! The tension (kind 3), curvature (5) or bend moment (6) of a finite-EI cable at internal node + !! inode, under the line's OutputRecovery. label names the value in a failure message. + TYPE(CD_HFMF_ModuleType), INTENT(INOUT) :: cable + INTEGER, INTENT(IN) :: kind, inode + CHARACTER(*), INTENT(IN) :: label + REAL(wp), INTENT(OUT) :: val + INTEGER, INTENT(OUT) :: ErrStat + CHARACTER(*), INTENT(OUT) :: ErrMsg + REAL(wp) :: sec_ten, sec_curv, sec_bend + INTEGER :: es + CHARACTER(1024) :: em + + ErrStat = CD_DECKDRV_OK + ErrMsg = '' + val = CD_ZERO + SELECT CASE (kind) + CASE (3) ! nodal tension + IF (inode == 1 .OR. inode == cable%line%nn) THEN + ! a line end: the same support-reaction tension as FairTen/AnchTen + CALL cable_end_tension(cable, inode, val, ErrStat, ErrMsg) + ELSE IF (cable%line%output_element_recovery) THEN + ! OutputRecovery element: the length-weighted average of the two adjacent + ! element-mean axial forces EA (|r'| - 1) + val = (cable%line%l0(inode - 1)*cable_element_mean_tension(cable, inode - 1) + & + cable%line%l0(inode)*cable_element_mean_tension(cable, inode))/ & + (cable%line%l0(inode - 1) + cable%line%l0(inode)) + ELSE + ! effective tension from nodal equilibrium (CD_HermiteCable_Dyn_Node_Recovery) + CALL cable_node_values(cable, inode, val, sec_curv, sec_bend, ErrStat, ErrMsg) + END IF + CASE (5) ! curvature: recovered bend moment / EI, or the larger exact one-sided trace + IF (.NOT. cable%line%output_element_recovery) THEN + CALL cable_node_values(cable, inode, sec_ten, val, sec_bend, ErrStat, ErrMsg) + RETURN + END IF + CALL cable_nodal_curvature(cable, inode, val, es, em) + IF (es /= CD_HCABLE_OK) CALL fail_solve(ErrStat, ErrMsg, 'OUTPUT "'//TRIM(label)//'": '//TRIM(em)) + CASE (6) ! bend moment: recovered from nodal equilibrium, or EI times the larger exact trace + IF (.NOT. cable%line%output_element_recovery) THEN + CALL cable_node_values(cable, inode, sec_ten, sec_curv, val, ErrStat, ErrMsg) + RETURN + END IF + CALL cable_nodal_bend_moment(cable, inode, val, es, em) + IF (es /= CD_HCABLE_OK) CALL fail_solve(ErrStat, ErrMsg, 'OUTPUT "'//TRIM(label)//'": '//TRIM(em)) + END SELECT + END SUBROUTINE cable_recovered_value + + PURE REAL(wp) FUNCTION cable_global_azimuth(cable, azimuth) RESULT(val) + !! A cable-local azimuth [deg] in the deck's global frame: plus the cable's frame heading, + !! wrapped. Identity heading reads it straight through, bit-for-bit. + TYPE(CD_HFMF_ModuleType), INTENT(IN) :: cable + REAL(wp), INTENT(IN) :: azimuth + val = azimuth + IF (.NOT. cable_frame_is_identity(cable)) THEN + val = wrap_azimuth_deg(val + ATAN2(cable%frame_s, cable%frame_c)*45.0_wp/QUARTER_PI, cable%azimuth_signed) + END IF + END FUNCTION cable_global_azimuth + PURE FUNCTION cable_frame_is_identity(cable) RESULT(is_id) !! Exact identity-heading test. Order comparisons preserve the bit-for-bit !! identity fast path without tripping -Wcompare-reals. diff --git a/tests/test_c_api_threads.c b/tests/test_c_api_threads.c index ecd3f935..9e1e6e6f 100644 --- a/tests/test_c_api_threads.c +++ b/tests/test_c_api_threads.c @@ -19,13 +19,14 @@ * 3. 8 threads initialise the same Syrope deck, which shares one OWC table file. * 4. A missing deck reports "file not found". * 5. 24 threads step distinct handles concurrently while other threads keep - * failing to open a missing deck; every trajectory matches a serial reference. + * failing to open a missing deck; every trajectory, and the line values each thread + * reads at every step, match a serial reference. * * test_c_api_threads --mixed [ ...] instead steps every deck on * several threads at once, all decks concurrently (each handle its own model: EI = 0 * lines, finite-EI cables, Syrope and viscoelastic segments, points, bodies and rods take - * their own code paths), n_steps steps each, and checks every trajectory against the - * deck's serial reference. A procedure whose locals or callbacks live in storage that + * their own code paths), n_steps steps each, and checks every trajectory and the line + * values read at every step against the deck's serial reference. A procedure whose locals or callbacks live in storage that * every thread shares (static locals, closure trampolines) fails it. * * CABLEDYN_THREAD_TEST_LOOPS= repeats checks 2-5 n times in one process (stress @@ -259,8 +260,37 @@ static double private_mb(void) #endif } -/* Initialises `path`, takes n_step steps, and records the final coupled loads. */ -static int step_deck(const char *path, int n_step, double *loads, int *ndof_out, char *msg, size_t msg_len) +/* Reads every node quantity of every line of the handle and adds up their magnitudes. */ +static double read_lines(void *h, int *err) +{ + static const int width[8] = {3, 3, 1, 3, 1, 1, 1, 1}; /* CD_C_LINE_POSITION .. _AZIMUTH */ + double sum = 0.0; + int n_lines = CableDyn_NObjects(h, CD_C_OBJ_LINE, err), line, quantity, i; + + for (line = 0; line < n_lines && *err == CD_C_OK; ++line) { + int id, nn, sub; + double *buf; + CableDyn_GetObjectInfo(h, CD_C_OBJ_LINE, line, &id, &nn, &sub, err); + if (*err != CD_C_OK) break; + buf = (double *)malloc(3 * (size_t)nn * sizeof *buf); + if (buf == NULL) { + *err = CD_C_ALLOC_FAIL; + break; + } + for (quantity = 1; quantity <= 8 && *err == CD_C_OK; ++quantity) { + int n = width[quantity - 1] * nn; + CableDyn_GetLineValues(h, line, quantity, buf, n, err); + for (i = 0; i < n && *err == CD_C_OK; ++i) sum += fabs(buf[i]); + } + free(buf); + } + return sum; +} + +/* Initialises `path`, takes n_step steps reading its lines at each, and records the final + * coupled loads and the sum of the line values read. */ +static int step_deck(const char *path, int n_step, double *loads, double *lines, int *ndof_out, char *msg, + size_t msg_len) { void *h = NULL; int err = -1, ndof, k, n_iter = 0; @@ -280,6 +310,7 @@ static int step_deck(const char *path, int n_step, double *loads, int *ndof_out, return 1; } CableDyn_GetCoupledMotion(h, q, v, a, ndof, &err); + *lines = 0.0; for (k = 0; k < n_step && err == CD_C_OK; ++k) { /* A slow surge of every coupled x DOF keeps the lines working. */ int i; @@ -290,6 +321,7 @@ static int step_deck(const char *path, int n_step, double *loads, int *ndof_out, CableDyn_Close(&h, &err); return 1; } + if (err == CD_C_OK) *lines += read_lines(h, &err); } if (err == CD_C_OK) CableDyn_CalcOutput(h, loads, ndof, &err); if (err != CD_C_OK) CableDyn_GetLastError(h, msg, (int)msg_len); @@ -337,13 +369,14 @@ struct step_job { int ndof; int failed; double loads[MAX_DOF]; + double lines; char msg[512]; }; static void step_worker(void *raw) { struct step_job *job = (struct step_job *)raw; - job->failed = step_deck(job->path, job->n_step, job->loads, &job->ndof, job->msg, sizeof job->msg); + job->failed = step_deck(job->path, job->n_step, job->loads, &job->lines, &job->ndof, job->msg, sizeof job->msg); } static void missing_worker(void *raw) @@ -482,11 +515,11 @@ static int check_concurrent_steps(const char *deck) enum { N_STEPPERS = 24, N_MISSING = 4 }; static struct step_job jobs[N_STEPPERS + N_MISSING]; struct task tasks[N_STEPPERS + N_MISSING]; - double reference[MAX_DOF]; + double reference[MAX_DOF], reference_lines = 0.0; char msg[512] = {0}; int i, j, ndof = 0, failed = 0; - if (step_deck(deck, N_STEP, reference, &ndof, msg, sizeof msg) != 0) { + if (step_deck(deck, N_STEP, reference, &reference_lines, &ndof, msg, sizeof msg) != 0) { fprintf(stderr, "FAIL serial reference run: %s\n", msg); return 1; } @@ -522,6 +555,10 @@ static int check_concurrent_steps(const char *deck) break; } } + if (j == ndof && fabs(jobs[i].lines - reference_lines) > 1.0e-9 * (1.0 + fabs(reference_lines))) { + fprintf(stderr, " thread %d: line values %.17g, reference %.17g\n", i, jobs[i].lines, reference_lines); + ++failed; + } } printf("concurrent steps: %d threads x %d steps on distinct handles, %d missing-deck threads: failed=%d\n", N_STEPPERS, N_STEP, N_MISSING, failed); @@ -539,6 +576,7 @@ static int check_mixed_steps(int n_decks, char **decks, int n_step) enum { MAX_DECKS = 8 }; static struct step_job jobs[MAX_THREADS]; static double reference[MAX_DECKS][MAX_DOF]; + double reference_lines[MAX_DECKS]; struct task tasks[MAX_THREADS]; int ndof[MAX_DECKS]; char msg[512] = {0}; @@ -549,7 +587,7 @@ static int check_mixed_steps(int n_decks, char **decks, int n_step) return 1; } for (d = 0; d < n_decks; ++d) { - if (step_deck(decks[d], n_step, reference[d], &ndof[d], msg, sizeof msg) != 0) { + if (step_deck(decks[d], n_step, reference[d], &reference_lines[d], &ndof[d], msg, sizeof msg) != 0) { fprintf(stderr, "FAIL serial reference run of %s: %s\n", decks[d], msg); return 1; } @@ -583,6 +621,11 @@ static int check_mixed_steps(int n_decks, char **decks, int n_step) break; } } + if (j == ndof[d] && fabs(jobs[i].lines - reference_lines[d]) > 1.0e-9 * (1.0 + fabs(reference_lines[d]))) { + fprintf(stderr, " thread %d (%s): line values %.17g, reference %.17g\n", i, decks[d], jobs[i].lines, + reference_lines[d]); + ++failed; + } } printf("mixed concurrent steps: %d decks x %d threads x %d steps: failed=%d\n", n_decks, per_deck, n_step, failed); diff --git a/tests/test_line_values.f90 b/tests/test_line_values.f90 new file mode 100644 index 00000000..f4d46444 --- /dev/null +++ b/tests/test_line_values.f90 @@ -0,0 +1,651 @@ +! SPDX-License-Identifier: Apache-2.0 +! Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University +PROGRAM test_line_values + !! CableDyn_GetLineValues evaluates a line at once; every value must be the bit pattern of the + !! output channel of its node, whatever the route. Each deck runs twice, at the static start and + !! after a few moving steps, so velocity and acceleration are not zero: + !! + !! * through the C API, every value at a deck node against CableDyn_EvalChannel (the row of deck + !! node J comes from CD_C_LINE_DECK_NODE), on the point and the aggregate routes; + !! * on the aggregate, every value at every node of the final mesh, including the nodes a + !! refinement added, against CD_Eval_Aggregate_Channel with mesh_node = .TRUE. + !! + !! The set: a cable on +x (no frame heading), with a heading, just below +x (the signed azimuth + !! range), on -x, OutputRecovery element, a bend stiffener under both recoveries, restrained in + !! torsion with a rolling host, beside an EI = 0 line, EI = 0 lines alone, a mesh the static solve + !! refines, and decks with bodies or rods. A state with a NaN then fails at the same value on + !! both paths. + !! + !! usage: test_line_values + USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_BOOL, C_CHAR, C_DOUBLE, C_INT, C_LOC, C_NULL_PTR, C_PTR + USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: INT64 + USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY: IEEE_IS_NAN, IEEE_QUIET_NAN, IEEE_VALUE + USE CableDyn_Precision, ONLY: wp + USE CableDyn_CAPI, ONLY: CableDyn_Create, CableDyn_Close, CableDyn_InitDeck, CableDyn_InitDeckRotating, & + CableDyn_Step, CableDyn_GetCoupledMotion, CableDyn_GetLastError, CableDyn_EvalChannel, & + CableDyn_GetLineValues, CableDyn_GetObjectInfo, CableDyn_NCoupledDOF, CableDyn_NObjects, & + CableDyn_UpdateRotations + USE CableDyn_DeckDriver, ONLY: CD_Deck_Query_dtM, CD_DECKDRV_OK, CD_Eval_Aggregate_Channel, & + CD_Eval_Aggregate_Line_Values + USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_End, CD_AGG_GetMovingPointMesh, & + CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, CD_AGG_OK, CD_AGG_Step_Moving + USE CableDyn_System, ONLY: CD_System_Line_NDOF + IMPLICIT NONE + + INTEGER, PARAMETER :: NSTEPS = 4, N_QUANTITY = 8 + INTEGER(C_INT), PARAMETER :: LINE_KIND = 1_C_INT, DECK_NODE = 10_C_INT + INTEGER, PARAMETER :: QUANTITY(N_QUANTITY) = [1, 2, 4, 3, 5, 6, 7, 8] + REAL(wp), PARAMETER :: SWAY = 0.05_wp, RATE = 4.0_wp, ROLL = 0.02_wp, PI = 4.0_wp*ATAN(1.0_wp) + INTEGER :: nfail + INTEGER(INT64) :: n_capi, n_mesh, n_added + CHARACTER(1024) :: arg + + nfail = 0 + n_capi = 0 + n_mesh = 0 + n_added = 0 + IF (COMMAND_ARGUMENT_COUNT() /= 1) ERROR STOP 'usage: test_line_values ' + + CALL write_cable_deck('lv_plain.dat', 0.0_wp) + CALL check_deck('lv_plain.dat') + CALL write_cable_deck('lv_heading.dat', 40.0_wp) + CALL check_deck('lv_heading.dat', corrupt=.TRUE.) + CALL write_cable_deck('lv_signed.dat', 340.0_wp) + CALL check_deck('lv_signed.dat') + CALL write_cable_deck('lv_reversed.dat', 180.0_wp) + CALL check_deck('lv_reversed.dat') + CALL write_cable_deck('lv_element.dat', 130.0_wp, element_recovery=.TRUE.) + CALL check_deck('lv_element.dat', corrupt=.TRUE.) + CALL write_cable_deck('lv_stiffener.dat', 250.0_wp, stiffener=.TRUE.) + CALL check_deck('lv_stiffener.dat') + CALL write_cable_deck('lv_stiffener_element.dat', 310.0_wp, stiffener=.TRUE., element_recovery=.TRUE.) + CALL check_deck('lv_stiffener_element.dat') + CALL write_cable_deck('lv_torsion.dat', 40.0_wp, torsion=.TRUE.) + CALL check_deck('lv_torsion.dat', rotating=.TRUE.) + CALL write_cable_deck('lv_torsion_element.dat', 220.0_wp, torsion=.TRUE., element_recovery=.TRUE.) + CALL check_deck('lv_torsion_element.dat', rotating=.TRUE.) + CALL write_cable_deck('lv_mixed.dat', 40.0_wp, chain=.TRUE.) + CALL check_deck('lv_mixed.dat') + CALL write_chain_deck('lv_chains.dat') + CALL check_deck('lv_chains.dat', corrupt=.TRUE.) + CALL GET_COMMAND_ARGUMENT(1, arg) + CALL write_refined_deck(TRIM(arg), 'lv_refined.dat') + CALL check_deck('lv_refined.dat', refined=.TRUE.) + CALL write_object_deck('lv_body.dat', body=.TRUE.) + CALL check_deck('lv_body.dat') + CALL write_object_deck('lv_rod.dat', body=.FALSE.) + CALL check_deck('lv_rod.dat') + + WRITE (*, '(A,I0,A,I0,A,I0,A)') 'compared ', n_capi, ' values with CableDyn_EvalChannel and ', n_mesh, & + ' with the mesh-node channels (', n_added, ' at added nodes)' + CALL require(n_capi > 10000 .AND. n_mesh > n_capi .AND. n_added > 0, 'the set reaches every route') + IF (nfail > 0) THEN + WRITE (*, '(A,I0,A)') 'FAIL: ', nfail, ' assertion(s) failed' + ERROR STOP 1 + END IF + WRITE (*, '(A)') 'PASS: the whole-line values are the channels bit for bit' + +CONTAINS + + SUBROUTINE require(cond, msg) + LOGICAL, INTENT(IN) :: cond + CHARACTER(*), INTENT(IN) :: msg + IF (cond) RETURN + nfail = nfail + 1 + WRITE (*, '(2A)') 'FAIL: ', msg + END SUBROUTINE require + + LOGICAL FUNCTION same_bits(a, b) RESULT(same) + !! The same bit pattern, or both NaN (a NaN's sign and payload carry no value). + REAL(wp), INTENT(IN) :: a, b + same = TRANSFER(a, 0_INT64) == TRANSFER(b, 0_INT64) + IF (.NOT. same) same = IEEE_IS_NAN(a) .AND. IEEE_IS_NAN(b) + END FUNCTION same_bits + + PURE FUNCTION text(i) RESULT(s) + INTEGER, INTENT(IN) :: i + CHARACTER(:), ALLOCATABLE :: s + CHARACTER(16) :: b + WRITE (b, '(I0)') i + s = TRIM(b) + END FUNCTION text + + PURE INTEGER FUNCTION width(kind) RESULT(n) + INTEGER, INTENT(IN) :: kind + n = MERGE(3, 1, kind == 1 .OR. kind == 2 .OR. kind == 4) + END FUNCTION width + + PURE FUNCTION token(kind, line_id, node, comp) RESULT(s) + !! The channel of a quantity at a node, component comp of a vector. + INTEGER, INTENT(IN) :: kind, line_id, node, comp + CHARACTER(:), ALLOCATABLE :: s, stem + CHARACTER(1), PARAMETER :: xyz(3) = ['x', 'y', 'z'] + stem = text(line_id)//'N'//text(node) + SELECT CASE (kind) + CASE (1) + s = 'L'//stem//'p'//xyz(comp) + CASE (2) + s = 'L'//stem//'v'//xyz(comp) + CASE (4) + s = 'L'//stem//'a'//xyz(comp) + CASE (3) + s = 'Ten'//stem + CASE (5) + s = 'Curv'//stem + CASE (6) + s = 'BendMom'//stem + CASE (7) + s = 'L'//stem//'Dec' + CASE DEFAULT + s = 'L'//stem//'Azi' + END SELECT + END FUNCTION token + + SUBROUTINE check_deck(path, rotating, refined, corrupt) + CHARACTER(*), INTENT(IN) :: path + LOGICAL, INTENT(IN), OPTIONAL :: rotating, refined, corrupt + LOGICAL :: rot + REAL(wp) :: dt + LOGICAL :: has_dt + INTEGER :: es + CHARACTER(512) :: em + + rot = .FALSE. + IF (PRESENT(rotating)) rot = rotating + CALL CD_Deck_Query_dtM(path, dt, has_dt, es, em) + CALL require(es == CD_DECKDRV_OK .AND. has_dt, path//': dtM: '//TRIM(em)) + IF (es /= CD_DECKDRV_OK .OR. .NOT. has_dt) RETURN + CALL check_capi(path, dt, rot, refined) + CALL check_aggregate(path, dt, rot, corrupt) + END SUBROUTINE check_deck + + SUBROUTINE check_capi(path, dt, rot, refined) + CHARACTER(*), INTENT(IN) :: path + REAL(wp), INTENT(IN) :: dt + LOGICAL, INTENT(IN) :: rot + LOGICAL, INTENT(IN), OPTIONAL :: refined + TYPE(C_PTR) :: h + INTEGER(C_INT) :: es, n, n_iter + LOGICAL(C_BOOL) :: conv, stall + CHARACTER(C_CHAR), TARGET :: c_path(1024), msg(512) + REAL(C_DOUBLE), ALLOCATABLE, TARGET :: q0(:), q(:), v(:), a(:), dcm(:), om(:), al(:) + INTEGER :: k, s, j + REAL(wp) :: t, phi + + c_path = ' ' + DO k = 1, LEN(path) + c_path(k) = path(k:k) + END DO + CALL CableDyn_Create(h, es) + IF (rot) THEN + CALL CableDyn_InitDeckRotating(h, C_LOC(c_path), INT(LEN(path), C_INT), C_NULL_PTR, 0_C_INT, es) + ELSE + CALL CableDyn_InitDeck(h, C_LOC(c_path), INT(LEN(path), C_INT), es) + END IF + CALL CableDyn_GetLastError(h, C_LOC(msg), INT(SIZE(msg), C_INT)) + CALL require(es == 0_C_INT, path//': init: '//last_error(msg)) + IF (es /= 0_C_INT) THEN + CALL CableDyn_Close(h, es) + RETURN + END IF + CALL compare_capi(h, path//' at rest', refined) + n = CableDyn_NCoupledDOF(h, es) + IF (n > 0_C_INT) THEN + ALLOCATE (q0(n), q(n), v(n), a(n), dcm(3*n), om(n), al(n)) + CALL CableDyn_GetCoupledMotion(h, C_LOC(q0), C_LOC(v), C_LOC(a), n, es) + DO s = 1, NSTEPS + t = dt*REAL(s, wp) + q = q0 + v = 0.0_C_DOUBLE + a = 0.0_C_DOUBLE + q(1:n:3) = q0(1:n:3) + SWAY*SIN(RATE*t) + v(1:n:3) = SWAY*RATE*COS(RATE*t) + a(1:n:3) = -SWAY*RATE**2*SIN(RATE*t) + IF (rot) THEN + phi = ROLL*REAL(s, wp)/REAL(NSTEPS, wp) + om = 0.0_C_DOUBLE + al = 0.0_C_DOUBLE + om(1:n:3) = ROLL/(REAL(NSTEPS, wp)*dt) + DO j = 0, INT(n)/3 - 1 + dcm(9*j + 1:9*j + 9) = RESHAPE(TRANSPOSE(roll_dcm(phi)), [9]) + END DO + CALL CableDyn_UpdateRotations(h, C_LOC(dcm), C_LOC(om), C_LOC(al), n/3_C_INT, es) + CALL require(es == 0_C_INT, path//': rotations') + END IF + CALL CableDyn_Step(h, REAL(dt, C_DOUBLE), C_LOC(q), C_LOC(v), C_LOC(a), n, conv, stall, n_iter, es) + CALL CableDyn_GetLastError(h, C_LOC(msg), INT(SIZE(msg), C_INT)) + CALL require(es == 0_C_INT, path//': step: '//last_error(msg)) + IF (es /= 0_C_INT) EXIT + END DO + CALL compare_capi(h, path//' stepped') + END IF + CALL CableDyn_Close(h, es) + END SUBROUTINE check_capi + + FUNCTION last_error(msg) RESULT(s) + CHARACTER(C_CHAR), INTENT(IN) :: msg(:) + CHARACTER(:), ALLOCATABLE :: s + INTEGER :: k + s = '' + DO k = 1, SIZE(msg) + IF (IACHAR(msg(k)) == 0) EXIT + s = s//msg(k) + END DO + END FUNCTION last_error + + PURE FUNCTION roll_dcm(phi) RESULT(r) + !! The global-to-body DCM of a roll phi about +x. + REAL(wp), INTENT(IN) :: phi + REAL(wp) :: r(3, 3) + r = RESHAPE([1.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, COS(phi), -SIN(phi), 0.0_wp, SIN(phi), COS(phi)], [3, 3]) + END FUNCTION roll_dcm + + SUBROUTINE compare_capi(h, label, refined) + !! Every value at a deck node of every line against its channel. + TYPE(C_PTR), INTENT(IN) :: h + CHARACTER(*), INTENT(IN) :: label + LOGICAL, INTENT(IN), OPTIONAL :: refined + INTEGER(C_INT) :: es, n_lines, il, id, nn, sub + REAL(C_DOUBLE), ALLOCATABLE, TARGET :: vals(:), deck(:) + REAL(C_DOUBLE) :: val + CHARACTER(C_CHAR), TARGET :: tok(64) + CHARACTER(:), ALLOCATABLE :: name + INTEGER :: iq, w, node, c, j, k, bad, n_added_here + + n_lines = CableDyn_NObjects(h, LINE_KIND, es) + CALL require(es == 0_C_INT .AND. n_lines > 0_C_INT, label//': lines') + n_added_here = 0 + DO il = 0_C_INT, n_lines - 1_C_INT + CALL CableDyn_GetObjectInfo(h, LINE_KIND, il, id, nn, sub, es) + CALL require(es == 0_C_INT, label//': line info') + IF (ALLOCATED(deck)) DEALLOCATE (deck) + ALLOCATE (deck(nn)) + CALL CableDyn_GetLineValues(h, il, DECK_NODE, C_LOC(deck), nn, es) + CALL require(es == 0_C_INT, label//': deck nodes') + n_added_here = n_added_here + COUNT(NINT(deck) == 0) + DO iq = 1, N_QUANTITY + w = width(QUANTITY(iq)) + IF (ALLOCATED(vals)) DEALLOCATE (vals) + ALLOCATE (vals(w*nn)) + CALL CableDyn_GetLineValues(h, il, INT(QUANTITY(iq), C_INT), C_LOC(vals), INT(w*nn, C_INT), es) + CALL require(es == 0_C_INT, label//': line values') + IF (es /= 0_C_INT) CYCLE + bad = 0 + DO node = 1, INT(nn) + j = NINT(deck(node)) + IF (j == 0) CYCLE + DO c = 1, w + name = token(QUANTITY(iq), INT(id), j, c) + DO k = 1, LEN(name) + tok(k) = name(k:k) + END DO + CALL CableDyn_EvalChannel(h, C_LOC(tok), INT(LEN(name), C_INT), val, es) + n_capi = n_capi + 1 + IF (es /= 0_C_INT) THEN + bad = bad + 1 + ELSE IF (.NOT. same_bits(vals(w*(node - 1) + c), val)) THEN + bad = bad + 1 + IF (bad == 1) WRITE (*, '(A,1X,A,2ES25.16)') label, name, vals(w*(node - 1) + c), val + END IF + END DO + END DO + CALL require(bad == 0, label//': quantity '//text(QUANTITY(iq))//' of line '//text(INT(id))//': '// & + text(bad)//' values differ from their channels') + END DO + END DO + IF (PRESENT(refined)) CALL require(n_added_here > 0, label//': the static solve refines the mesh') + ! the entry's own checks + CALL CableDyn_GetLineValues(h, n_lines, 1_C_INT, C_LOC(deck), 3_C_INT, es) + CALL require(es /= 0_C_INT, label//': an index past the lines is rejected') + CALL CableDyn_GetLineValues(h, -1_C_INT, 1_C_INT, C_LOC(deck), 3_C_INT, es) + CALL require(es /= 0_C_INT, label//': a negative index is rejected') + END SUBROUTINE compare_capi + + SUBROUTINE check_aggregate(path, dt, rot, corrupt) + CHARACTER(*), INTENT(IN) :: path + REAL(wp), INTENT(IN) :: dt + LOGICAL, INTENT(IN) :: rot + LOGICAL, INTENT(IN), OPTIONAL :: corrupt + TYPE(CD_AGG_ModuleType) :: agg + INTEGER :: es, n, s, nit, id, k + LOGICAL :: cv, st, bad_state + REAL(wp) :: t, phi + REAL(wp), ALLOCATABLE :: p0(:, :), p(:, :), pv(:, :), pa(:, :), pl(:, :), dcm(:, :, :), om(:, :), al(:, :) + CHARACTER(512) :: em + + IF (rot) THEN + CALL CD_AGG_Init_From_Deck(agg, path, dt, es, em, host_orientation=.TRUE.) + ELSE + CALL CD_AGG_Init_From_Deck(agg, path, dt, es, em) + END IF + CALL require(es == CD_AGG_OK, path//': aggregate: '//TRIM(em)) + IF (es /= CD_AGG_OK) RETURN + CALL compare_mesh(agg, path//' at rest') + n = CD_AGG_NMovingPoints(agg, es, em) + IF (n > 0) THEN + ALLOCATE (p0(3, n), p(3, n), pv(3, n), pa(3, n), pl(3, n), dcm(3, 3, n), om(3, n), al(3, n)) + CALL CD_AGG_GetMovingPointMesh(agg, p0, pv, pa, pl, es, em) + DO s = 1, NSTEPS + t = dt*REAL(s, wp) + p = p0 + pv = 0.0_wp + pa = 0.0_wp + p(1, :) = p0(1, :) + SWAY*SIN(RATE*t) + pv(1, :) = SWAY*RATE*COS(RATE*t) + pa(1, :) = -SWAY*RATE**2*SIN(RATE*t) + IF (rot) THEN + phi = ROLL*REAL(s, wp)/REAL(NSTEPS, wp) + om = 0.0_wp + al = 0.0_wp + om(1, :) = ROLL/(REAL(NSTEPS, wp)*dt) + DO k = 1, n + dcm(:, :, k) = roll_dcm(phi) + END DO + CALL CD_AGG_Step_Moving(agg, dt, p, pv, pa, cv, st, nit, es, em, t_committed=t, orientation=dcm, & + angular_velocity=om, angular_acceleration=al) + ELSE + CALL CD_AGG_Step_Moving(agg, dt, p, pv, pa, cv, st, nit, es, em, t_committed=t) + END IF + CALL require(es == CD_AGG_OK, path//': aggregate step: '//TRIM(em)) + IF (es /= CD_AGG_OK) EXIT + END DO + CALL compare_mesh(agg, path//' stepped') + END IF + bad_state = .FALSE. + IF (PRESENT(corrupt)) bad_state = corrupt + IF (bad_state) THEN + ! a NaN at mid-line: both paths give the same values up to the same first failure + DO id = 1, SIZE(agg%line_obj_index) + k = agg%line_obj_index(id) + IF (k < 1) CYCLE + IF (agg%line_is_cable(id)) THEN + agg%cables(k)%line%q(6*(agg%cables(k)%line%nn/2) + 1) = IEEE_VALUE(t, IEEE_QUIET_NAN) + ELSE + agg%sys%fast%system%lines(k)%q(3*(SIZE(agg%sys%fast%system%lines(k)%q)/6) + 1) = & + IEEE_VALUE(t, IEEE_QUIET_NAN) + END IF + END DO + CALL compare_mesh(agg, path//' with a NaN', failing=.TRUE.) + END IF + CALL CD_AGG_End(agg, es, em) + END SUBROUTINE check_aggregate + + SUBROUTINE compare_mesh(agg, label, failing) + !! Every value at every node of the final mesh against the channel of that mesh node. + TYPE(CD_AGG_ModuleType), INTENT(INOUT) :: agg + CHARACTER(*), INTENT(IN) :: label + LOGICAL, INTENT(IN), OPTIONAL :: failing + REAL(wp), ALLOCATABLE :: vals(:), work(:) + REAL(wp) :: val + INTEGER :: id, obj, nn, iq, w, n_done, es, es_ch, node, c, i, bad + CHARACTER(512) :: em, em_ch + CHARACTER(:), ALLOCATABLE :: name + LOGICAL :: allow_failure + + allow_failure = .FALSE. + IF (PRESENT(failing)) allow_failure = failing + DO id = 1, SIZE(agg%line_obj_index) + obj = agg%line_obj_index(id) + IF (obj < 1) CYCLE + IF (agg%line_is_cable(id)) THEN + nn = agg%cables(obj)%line%nn + IF (ALLOCATED(agg%cables(obj)%deck_node)) n_added = n_added + nn - SIZE(agg%cables(obj)%deck_node) + ELSE + nn = CD_System_Line_NDOF(agg%sys%fast%system, obj, es, em)/3 + END IF + IF (ALLOCATED(work)) DEALLOCATE (work) + ALLOCATE (work(nn)) + DO iq = 1, N_QUANTITY + w = width(QUANTITY(iq)) + IF (ALLOCATED(vals)) DEALLOCATE (vals) + ALLOCATE (vals(w*nn)) + CALL CD_Eval_Aggregate_Line_Values(id, QUANTITY(iq), agg%has_sys, agg%sys%fast%system, agg%cables, & + agg%line_is_cable, agg%line_obj_index, vals, work, n_done, es, em) + IF (.NOT. allow_failure) CALL require(es == CD_DECKDRV_OK .AND. n_done == w*nn, label//': '//TRIM(em)) + bad = 0 + DO i = 1, MIN(n_done + 1, w*nn) + node = (i - 1)/w + 1 + c = i - w*(node - 1) + name = token(QUANTITY(iq), id, node, c) + CALL CD_Eval_Aggregate_Channel(name, agg%has_sys, agg%sys%fast%system, agg%cables, agg%line_is_cable, & + agg%line_obj_index, val, es_ch, em_ch, mesh_node=.TRUE.) + IF (i > n_done) THEN + ! the first value the line evaluation did not reach: the channel fails there too + IF (es_ch == CD_DECKDRV_OK .OR. es == CD_DECKDRV_OK) bad = bad + 1 + ELSE + n_mesh = n_mesh + 1 + IF (es_ch /= CD_DECKDRV_OK) THEN + bad = bad + 1 + ELSE IF (.NOT. same_bits(vals(i), val)) THEN + bad = bad + 1 + IF (bad == 1) WRITE (*, '(A,1X,A,2ES25.16)') label, name, vals(i), val + END IF + END IF + END DO + CALL require(bad == 0, label//': quantity '//text(QUANTITY(iq))//' of line '//text(id)//': '//text(bad)// & + ' values differ from the mesh-node channels') + END DO + ! a bad code or size is refused, with nothing written + CALL CD_Eval_Aggregate_Line_Values(id, 9, agg%has_sys, agg%sys%fast%system, agg%cables, agg%line_is_cable, & + agg%line_obj_index, vals, work, n_done, es, em) + CALL require(es /= CD_DECKDRV_OK .AND. n_done == 0, label//': a code past the node channels is refused') + CALL CD_Eval_Aggregate_Line_Values(id, 3, agg%has_sys, agg%sys%fast%system, agg%cables, agg%line_is_cable, & + agg%line_obj_index, vals(1:nn - 1), work, n_done, es, em) + CALL require(es /= CD_DECKDRV_OK .AND. n_done == 0, label//': a wrongly sized output is refused') + END DO + END SUBROUTINE compare_mesh + + SUBROUTINE write_cable_deck(path, heading, element_recovery, stiffener, torsion, chain) + !! A 100 m finite-EI cable from a coupled point at z = -10 to an anchor 70 m away along + !! heading [deg], at z = -60. Line ids are not consecutive. torsion restrains a level cable at + !! z = -50 (both ends Rigid); chain adds an EI = 0 line from a second coupled point, heading + !! just below +x (the signed azimuth range). + CHARACTER(*), INTENT(IN) :: path + REAL(wp), INTENT(IN) :: heading + LOGICAL, INTENT(IN), OPTIONAL :: element_recovery, stiffener, torsion, chain + LOGICAL :: rec, stf, tor, chn + REAL(wp) :: cx, sy + INTEGER :: u + + rec = .FALSE. + IF (PRESENT(element_recovery)) rec = element_recovery + stf = .FALSE. + IF (PRESENT(stiffener)) stf = stiffener + tor = .FALSE. + IF (PRESENT(torsion)) tor = torsion + chn = .FALSE. + IF (PRESENT(chain)) chn = chain + cx = COS(heading*PI/180.0_wp) + sy = SIN(heading*PI/180.0_wp) + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'Line values against the channels' + WRITE (u, '(A)') '--- LINE TYPES ---' + WRITE (u, '(A)') 'Name Diam Mass EA BA EI GAs GJ Irt Irn Cdn Cdt Can Cat' + WRITE (u, '(A)') '(-) (m) (kg/m) (N) (-) (Nm2) (N) (Nm2) (kgm) (kgm) (-) (-) (-) (-)' + WRITE (u, '(A)') 'cable 0.16 36.7 4.69e8 -1.0 1.99e4 9.02e7 5.0e4 0.0587 0.1174 1.2 0.1 1.0 0.0' + WRITE (u, '(A)') 'chain 0.10 150.0 8.0e8 0.0 0.0 0 0 0 0 1.2 0.4 1.0 0.5' + WRITE (u, '(A)') '--- POINTS ---' + WRITE (u, '(A)') 'ID Type X Y Z' + WRITE (u, '(A)') '(-) (-) (m) (m) (m)' + IF (tor) THEN + WRITE (u, '(A)') '1 Coupled 0.0 0.0 -50.0' + WRITE (u, '(A,2(1X,ES23.15),A)') '2 Fixed', 99.5_wp*cx, 99.5_wp*sy, ' -50.0' + ELSE + WRITE (u, '(A)') '1 Coupled 0.0 0.0 -10.0' + WRITE (u, '(A,2(1X,ES23.15),A)') '2 Fixed', 70.0_wp*cx, 70.0_wp*sy, ' -60.0' + END IF + IF (chn) THEN + WRITE (u, '(A)') '3 Coupled -20.0 5.0 -10.0' + WRITE (u, '(A)') '4 Fixed 200.0 -30.0 -60.0' + END IF + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + WRITE (u, '(A)') '3 1 2 -' + IF (chn) WRITE (u, '(A)') '6 3 4 -' + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + WRITE (u, '(A)') '3 cable 100.0 40' + IF (chn) WRITE (u, '(A)') '6 chain 240.0 24' + IF (stf) THEN + WRITE (u, '(A)') '--- STIFFENERS ---' + WRITE (u, '(A)') 'LineID End Length RootOD TipOD InnerD E Density' + WRITE (u, '(A)') '(-) (-) (m) (m) (m) (m) (Pa) (kg/m^3)' + WRITE (u, '(A)') '3 A 0.5 0.45 0.45 0.17 5.0e7 1150' + WRITE (u, '(A)') '3 A 6.5 0.45 0.18 0.17 5.0e7 1150' + END IF + IF (tor) THEN + WRITE (u, '(A)') '--- END CONNECTIONS ---' + WRITE (u, '(A)') 'LineID End Stiffness EzX EzY EzZ TorsStiffness NxX NxY NxZ Pretwist' + WRITE (u, '(A)') '(-) (-) (N-m/rad) (-) (-) (-) (N-m/rad) (-) (-) (-) (deg)' + WRITE (u, '(A,2(1X,ES23.15),A,2(1X,ES23.15),A)') '3 A Rigid', cx, sy, ' 0 Rigid', -sy, cx, ' 0 0' + WRITE (u, '(A,2(1X,ES23.15),A,2(1X,ES23.15),A)') '3 B Rigid', cx, sy, ' 0 Rigid', -sy, cx, ' 0 0' + END IF + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '60.0 WtrDpth' + WRITE (u, '(A)') '1.0e5 kBot' + WRITE (u, '(A)') '1.0e4 cBot' + WRITE (u, '(A)') '0.02 dtM' + WRITE (u, '(A)') '0.0 TMax' + IF (rec) WRITE (u, '(A)') 'element OutputRecovery' + WRITE (u, '(A)') '--- OUTPUTS ---' + WRITE (u, '(A)') 'FairTen3' + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + END SUBROUTINE write_cable_deck + + SUBROUTINE write_chain_deck(path) + !! EI = 0 lines alone (the point route), on three headings. + CHARACTER(*), INTENT(IN) :: path + INTEGER :: u + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'Line values against the channels, EI = 0 lines' + 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.10 150.0 8.0e8 -1.0 0.0 1.2 0.4 1.0 0.5' + WRITE (u, '(A)') '--- POINTS ---' + WRITE (u, '(A)') 'ID Type X Y Z' + WRITE (u, '(A)') '(-) (-) (m) (m) (m)' + WRITE (u, '(A)') '1 Coupled 0.0 0.0 -10.0' + WRITE (u, '(A)') '2 Fixed 240.0 -40.0 -60.0' + WRITE (u, '(A)') '3 Fixed -240.0 -40.0 -60.0' + WRITE (u, '(A)') '4 Fixed -30.0 240.0 -60.0' + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + WRITE (u, '(A)') '2 1 2 -' + WRITE (u, '(A)') '5 1 3 -' + WRITE (u, '(A)') '9 1 4 -' + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + WRITE (u, '(A)') '2 chain 260.0 26' + WRITE (u, '(A)') '5 chain 260.0 20' + WRITE (u, '(A)') '9 chain 260.0 13' + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '60.0 WtrDpth' + WRITE (u, '(A)') '1.0e6 kBot' + WRITE (u, '(A)') '1.0e5 cBot' + WRITE (u, '(A)') '0.02 dtM' + WRITE (u, '(A)') '0.0 TMax' + WRITE (u, '(A)') '--- OUTPUTS ---' + WRITE (u, '(A)') 'FairTen2' + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + END SUBROUTINE write_chain_deck + + SUBROUTINE write_object_deck(path, body) + !! EI = 0 lines on a coupled Rigid6 body, or on a free rod, between an anchor and a coupled + !! point in still water (the aggregate route with a body or rod runtime). + CHARACTER(*), INTENT(IN) :: path + LOGICAL, INTENT(IN) :: body + INTEGER :: u + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'Line values against the channels, a body or a rod' + 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)') 'line 0.10 80.0 1.0e5 0.0 0.0 1.0 0.0 1.0 0.0' + IF (body) THEN + WRITE (u, '(A)') '--- BODIES ---' + WRITE (u, '(A)') 'ID Type X Y Z Rot1 Rot2 Rot3 Mass Vol C33 C44 C55 CdA Ca Ixx Iyy Izz' + WRITE (u, '(A)') '(-) (-) (m) (m) (m) (deg) (deg) (deg) (kg) (m3) (N/m) (Nm/rad) (Nm/rad) (m2) (-) '// & + '(kgm2) (kgm2) (kgm2)' + WRITE (u, '(A)') '1 Rigid6 0.0 0.0 -4.0 0.0 0.0 0.0 100.0 0.05 0.0 100.0 100.0 0.0 0.0 20.0 20.0 20.0' + ELSE + WRITE (u, '(A)') '--- ROD TYPES ---' + WRITE (u, '(A)') 'Name Diam Mass Cd Ca CdEnd CaEnd' + WRITE (u, '(A)') '(-) (m) (kg/m) (-) (-) (-) (-)' + WRITE (u, '(A)') 'rodmat 0.20 50.0 1.0 1.0 0.0 0.0' + WRITE (u, '(A)') '--- RODS ---' + WRITE (u, '(A)') 'ID RodType Type XA YA ZA XB YB ZB NumSegs Outputs' + WRITE (u, '(A)') '(-) (-) (-) (m) (m) (m) (m) (m) (m) (-) (-)' + WRITE (u, '(A)') '1 rodmat Free 0.0 0.0 -4.0 0.0 0.0 -2.0 2 -' + 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) (-)' + WRITE (u, '(A)') '1 Fixed 2.0 0.0 -5.0 0.0 0.0 0.0 0.0' + IF (body) THEN + WRITE (u, '(A)') '2 Body1 0.0 0.0 0.0 0.0 0.0 0.0 0.0' + WRITE (u, '(A)') '3 Body1 0.0 0.0 0.0 0.0 0.0 0.0 0.0' + ELSE + WRITE (u, '(A)') '2 Rod1A 0.0 0.0 -4.0 0.0 0.0 0.0 0.0' + WRITE (u, '(A)') '3 Rod1B 0.0 0.0 -2.0 0.0 0.0 0.0 0.0' + END IF + WRITE (u, '(A)') '4 Coupled 0.0 0.0 -1.0 0.0 0.0 0.0 0.0' + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + WRITE (u, '(A)') '1 2 1 -' + WRITE (u, '(A)') '2 3 4 -' + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + WRITE (u, '(A)') '1 line 3.5 3' + WRITE (u, '(A)') '2 line 3.2 3' + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '10.0 WtrDpth' + WRITE (u, '(A)') '1.0e5 kBot' + WRITE (u, '(A)') '0.005 dtM' + WRITE (u, '(A)') '0.0 TMax' + WRITE (u, '(A)') '--- OUTPUTS ---' + WRITE (u, '(A)') 'FairTen1' + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + END SUBROUTINE write_object_deck + + SUBROUTINE write_refined_deck(source, path) + !! The coarse lazy-wave deck with its top section cut from 20 to 4 elements: the deck mesh + !! cannot resolve the curvature of the equilibrium and the static solve refines it. + CHARACTER(*), INTENT(IN) :: source, path + CHARACTER(512) :: row + INTEGER :: u, w, ios + LOGICAL :: found + + found = .FALSE. + OPEN (NEWUNIT=u, FILE=source, STATUS='OLD', ACTION='READ') + OPEN (NEWUNIT=w, FILE=path, STATUS='REPLACE', ACTION='WRITE') + DO + READ (u, '(A)', IOSTAT=ios) row + IF (ios /= 0) EXIT + IF (TRIM(row) == '4 bare 80.0 20') THEN + row = '4 bare 80.0 4' + found = .TRUE. + END IF + WRITE (w, '(A)') TRIM(row) + END DO + CLOSE (u) + CLOSE (w) + CALL require(found, 'the coarse deck has its top section') + END SUBROUTINE write_refined_deck + +END PROGRAM test_line_values From e6a37f2798cc2ae0b4b7a6d4f46154441d6d64da Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sat, 7 Nov 2026 23:03:44 +0900 Subject: [PATCH 3/4] fix(model): keep the azimuth range of an EI = 0 line through a model copy CD_Copy_Model set the azimuth range on its working copy, but commit_model_copy did not move it to the destination. Every EI = 0 line held by a system is such a copy, so it fell back to [0, 360) whatever its deck chord: a line heading just below +X read 350.5 deg where the standalone EI = 0 route reads -9.5 deg, in the channels and line values of the C API and Python on the point and aggregate routes, on the standalone mixed and multibody routes, and in the aggregate's channels, static profile and initialisation summary, which OpenFAST reports. The move is one line. Only the LNAzi value of an EI = 0 line whose chord heads within 45 deg of +X changes, by -360 deg where it read above 180 deg; no force or state depends on it. test_model checks the copy. test_line_values and test_objects sweep headings on both sides of +X, on EI = 0 and mixed decks and on a body, against the documented range on every route. Signed-off-by: Jae Hoon Seo --- doc/outputs.rst | 3 +- python/cabledyn/objects.py | 5 +- python/tests/test_objects.py | 92 ++++++++++++++++++++++++ src/CableDyn_Model.f90 | 1 + tests/test_line_values.f90 | 133 ++++++++++++++++++++++++++++++++++- tests/test_model.f90 | 2 + 6 files changed, 232 insertions(+), 4 deletions(-) diff --git a/doc/outputs.rst b/doc/outputs.rst index 758d4358..b36e4288 100644 --- a/doc/outputs.rst +++ b/doc/outputs.rst @@ -526,7 +526,8 @@ chord from End A to End B, at the deck positions, heads within 45° of +X, and i every other line. The cut of a line's range then lies at least 45° from its heading: a line swaying about its heading reads continuously, where a line along +X would otherwise jump between 0 and 360 as it moves a fraction of a degree to either side. The range is fixed for -the run, so the motion of the line cannot switch it. A round-off component across the cut +the run, so the motion of the line cannot switch it, and the same on every route: the standalone +driver, the C API and Python, and OpenFAST. A round-off component across the cut reads on its included side (0, or 180 in the signed range), and one across +X reads exactly 0. A line a host builds through the C interface without a deck reports [0, 360). diff --git a/python/cabledyn/objects.py b/python/cabledyn/objects.py index 3051eedf..12a8c0b3 100644 --- a/python/cabledyn/objects.py +++ b/python/cabledyn/objects.py @@ -277,7 +277,10 @@ def declination(self, out: FloatArray | None = None) -> FloatArray: return self.values("declination", out) def azimuth(self, out: FloatArray | None = None) -> FloatArray: - """Azimuth at each node ``(n_nodes,)`` [deg] (``LNAzi``).""" + """Azimuth at each node ``(n_nodes,)`` [deg] (``LNAzi``), in the line's range. + + (-180, 180] on a line whose deck chord heads within 45 deg of +X, otherwise [0, 360). + """ return self.values("azimuth", out) def arc_length(self) -> FloatArray: diff --git a/python/tests/test_objects.py b/python/tests/test_objects.py index 1f728f14..b49950bf 100644 --- a/python/tests/test_objects.py +++ b/python/tests/test_objects.py @@ -10,6 +10,7 @@ from __future__ import annotations +import math import os import re from pathlib import Path @@ -270,6 +271,97 @@ def test_body_and_rod_decks_match_driver_outputs_at_the_initial_state(tmp_path): _check_row(model, result, 0.0) +def _heading_deck(cables: bool) -> tuple[str, dict[int, float]]: + """Lines on headings on both sides of +x, and the azimuth each must report at End A. + + Line ``2 k`` runs in its vertical plane from its own coupled point to its own anchor. Within + 45 deg of +x the range is (-180, 180], otherwise [0, 360). With ``cables`` every second line + is finite-EI, which puts the deck on the mixed route. + """ + headings = [-46.0, -44.0, -30.0, -10.0, -1.0, 1.0, 10.0, 30.0, 44.0, 46.0, 180.0, 270.0] + points, lines, sections, outputs, expected = [], [], [], [], {} + for k, heading in enumerate(headings, start=1): + cable = cables and k % 2 == 0 + reach = 70.0 if cable else 240.0 + cx, sy = math.cos(math.radians(heading)), math.sin(math.radians(heading)) + points.append(f"{2 * k - 1} Coupled {5.0 * cx:.15e} {5.0 * sy:.15e} -10.0") + points.append(f"{2 * k} Fixed {(5.0 + reach) * cx:.15e} {(5.0 + reach) * sy:.15e} -60.0") + lines.append(f"{2 * k} {2 * k - 1} {2 * k} -") + sections.append(f"{2 * k} {'cable 100.0 20' if cable else 'chain 260.0 13'}") + outputs.append(f"L{2 * k}N1Azi") + expected[2 * k] = heading if abs(heading) < 45.0 else heading % 360.0 + text = "\n".join( + [ + "Lines on both sides of +x", + "--- LINE TYPES ---", + "Name Diam Mass EA BA EI Cdn Cdt Can Cat", + "(-) (m) (kg/m) (N) (-) (Nm2) (-) (-) (-) (-)", + "cable 0.16 36.7 4.69e8 -1.0 1.99e4 1.2 0.1 1.0 0.0", + "chain 0.10 150.0 8.0e8 -1.0 0.0 1.2 0.4 1.0 0.5", + "--- POINTS ---", + "ID Type X Y Z", + "(-) (-) (m) (m) (m)", + *points, + "--- LINES ---", + "ID NodeA NodeB Outputs", + "(-) (-) (-) (-)", + *lines, + "--- SECTIONS ---", + "LineID LineType Length NumSegs", + "(-) (-) (m) (-)", + *sections, + "--- OPTIONS ---", + "9.80665 g", + "1025.0 rhoW", + "60.0 WtrDpth", + "1.0e6 kBot", + "1.0e5 cBot", + "0.02 dtM", + "0.04 TMax", + "--- OUTPUTS ---", + *outputs, + "--- need this line ---", + "", + ] + ) + return text, expected + + +@needs_driver +@pytest.mark.parametrize("cables", [False, True], ids=["ei0", "mixed"]) +def test_azimuth_range_is_the_same_on_every_route(tmp_path, cables): + text, expected = _heading_deck(cables) + deck = _write(tmp_path, "headings.dat", text) + result = _driver_run(deck, tmp_path) + main = result.read_main() + # a run without dtM and TMax stops at the static solution, which an EI = 0 deck writes out + static_text = text.replace("0.02 dtM\n", "").replace("0.04 TMax\n", "") + static_deck = _write(tmp_path, "headings_static.dat", static_text) + static = _driver_run(static_deck, tmp_path).read_static() + with CableDyn(deck) as model: + for line_id, azimuth in expected.items(): + token = f"L{line_id}N1Azi" + driver = float(main.values[0, list(main.channels).index(token)]) + assert driver == pytest.approx(azimuth, abs=1.0e-5), f"driver {token}" + assert model.channel(token) == pytest.approx(azimuth, abs=1.0e-6), f"library {token}" + line = model.line(line_id) + np.testing.assert_allclose(line.azimuth(), azimuth, rtol=0.0, atol=1.0e-6) + profile = static.line(line_id).column("Azimuth") + np.testing.assert_allclose(profile, azimuth, rtol=0.0, atol=1.0e-5) + + +@needs_driver +def test_azimuth_range_of_a_line_on_a_body(tmp_path): + text = _BODY_DECK.replace("4 Fixed 40.0 0.0 -100.0", "4 Fixed 40.0 -10.0 -100.0") + text = text.replace('"FairTen1"\n', '"FairTen1"\n"L1N1Azi"\n') + deck = _write(tmp_path, "body_heading.dat", text) + main = _driver_run(deck, tmp_path).read_main() + driver = float(main.values[0, list(main.channels).index("L1N1Azi")]) + with CableDyn(deck) as model: + assert model.channel("L1N1Azi") == pytest.approx(driver, abs=1.0e-5) + assert -45.0 < driver < 0.0 + + # --- library-only gates -------------------------------------------------------- diff --git a/src/CableDyn_Model.f90 b/src/CableDyn_Model.f90 index 39d4f3f7..683b18b2 100644 --- a/src/CableDyn_Model.f90 +++ b/src/CableDyn_Model.f90 @@ -6925,6 +6925,7 @@ SUBROUTINE commit_model_copy(from, to) to%initialized = from%initialized to%tension_only = from%tension_only + to%azimuth_signed = from%azimuth_signed to%n_dof = from%n_dof to%n_elem = from%n_elem to%has_seabed = from%has_seabed diff --git a/tests/test_line_values.f90 b/tests/test_line_values.f90 index f4d46444..4bd63252 100644 --- a/tests/test_line_values.f90 +++ b/tests/test_line_values.f90 @@ -16,6 +16,10 @@ PROGRAM test_line_values !! refines, and decks with bodies or rods. A state with a NaN then fails at the same value on !! both paths. !! + !! Lines on headings on both sides of +x, EI = 0 alone and beside cables, must report the azimuth + !! range of their deck chord (doc/outputs.rst) through the line values, the channels, the + !! aggregate's channels (the OpenFAST shell's) and its initialisation summary. + !! !! usage: test_line_values USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_BOOL, C_CHAR, C_DOUBLE, C_INT, C_LOC, C_NULL_PTR, C_PTR USE, INTRINSIC :: ISO_FORTRAN_ENV, ONLY: INT64 @@ -27,8 +31,9 @@ PROGRAM test_line_values CableDyn_UpdateRotations USE CableDyn_DeckDriver, ONLY: CD_Deck_Query_dtM, CD_DECKDRV_OK, CD_Eval_Aggregate_Channel, & CD_Eval_Aggregate_Line_Values - USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_End, CD_AGG_GetMovingPointMesh, & - CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, CD_AGG_OK, CD_AGG_Step_Moving + USE CableDyn_OpenFAST_Aggregate, ONLY: CD_AGG_ModuleType, CD_AGG_End, CD_AGG_EvalChannel, CD_AGG_GetInitLine, & + CD_AGG_GetMovingPointMesh, CD_AGG_Init_From_Deck, CD_AGG_NMovingPoints, & + CD_AGG_NumChannels, CD_AGG_OK, CD_AGG_Step_Moving USE CableDyn_System, ONLY: CD_System_Line_NDOF IMPLICIT NONE @@ -36,6 +41,9 @@ PROGRAM test_line_values INTEGER(C_INT), PARAMETER :: LINE_KIND = 1_C_INT, DECK_NODE = 10_C_INT INTEGER, PARAMETER :: QUANTITY(N_QUANTITY) = [1, 2, 4, 3, 5, 6, 7, 8] REAL(wp), PARAMETER :: SWAY = 0.05_wp, RATE = 4.0_wp, ROLL = 0.02_wp, PI = 4.0_wp*ATAN(1.0_wp) + ! Line headings [deg]: within 45 deg of +x on either side, just outside it, and elsewhere. + REAL(wp), PARAMETER :: HEADINGS(12) = [-46.0_wp, -44.0_wp, -30.0_wp, -10.0_wp, -1.0_wp, 1.0_wp, 10.0_wp, 30.0_wp, & + 44.0_wp, 46.0_wp, 180.0_wp, 270.0_wp] INTEGER :: nfail INTEGER(INT64) :: n_capi, n_mesh, n_added CHARACTER(1024) :: arg @@ -75,6 +83,12 @@ PROGRAM test_line_values CALL check_deck('lv_body.dat') CALL write_object_deck('lv_rod.dat', body=.FALSE.) CALL check_deck('lv_rod.dat') + CALL write_heading_deck('lv_range_chains.dat', cables=.FALSE.) + CALL check_deck('lv_range_chains.dat') + CALL check_azimuth_range('lv_range_chains.dat') + CALL write_heading_deck('lv_range_mixed.dat', cables=.TRUE.) + CALL check_deck('lv_range_mixed.dat') + CALL check_azimuth_range('lv_range_mixed.dat') WRITE (*, '(A,I0,A,I0,A,I0,A)') 'compared ', n_capi, ' values with CableDyn_EvalChannel and ', n_mesh, & ' with the mesh-node channels (', n_added, ' at added nodes)' @@ -435,6 +449,121 @@ SUBROUTINE compare_mesh(agg, label, failing) END DO END SUBROUTINE compare_mesh + PURE REAL(wp) FUNCTION range_azimuth(heading) RESULT(azimuth) + !! The azimuth [deg] of a line on heading in its range: (-180, 180] within 45 deg of +x, + !! otherwise [0, 360). + REAL(wp), INTENT(IN) :: heading + azimuth = heading + IF (ABS(heading) > 45.0_wp) azimuth = MODULO(heading, 360.0_wp) + END FUNCTION range_azimuth + + SUBROUTINE check_azimuth_range(path) + !! Line 2 k of the deck runs on HEADINGS(k), in its vertical plane, at the static start. + CHARACTER(*), INTENT(IN) :: path + REAL(wp), PARAMETER :: TOL = 1.0e-6_wp + TYPE(C_PTR) :: h + TYPE(CD_AGG_ModuleType) :: agg + INTEGER(C_INT) :: es, n_lines, il, id, nn, sub + CHARACTER(C_CHAR), TARGET :: c_path(1024) + REAL(C_DOUBLE), ALLOCATABLE, TARGET :: azi(:) + REAL(wp) :: val, tension, force(3), inclination, declination, azimuth + INTEGER :: k, n, line_id, ies + CHARACTER(512) :: em + + c_path = ' ' + DO k = 1, LEN(path) + c_path(k) = path(k:k) + END DO + CALL CableDyn_Create(h, es) + CALL CableDyn_InitDeck(h, C_LOC(c_path), INT(LEN(path), C_INT), es) + CALL require(es == 0_C_INT, path//': range: init') + n_lines = CableDyn_NObjects(h, LINE_KIND, es) + CALL require(n_lines == SIZE(HEADINGS), path//': range: line count') + DO il = 0_C_INT, n_lines - 1_C_INT + CALL CableDyn_GetObjectInfo(h, LINE_KIND, il, id, nn, sub, es) + IF (ALLOCATED(azi)) DEALLOCATE (azi) + ALLOCATE (azi(nn)) + CALL CableDyn_GetLineValues(h, il, 8_C_INT, C_LOC(azi), nn, es) + CALL require(es == 0_C_INT .AND. MAXVAL(ABS(azi - range_azimuth(HEADINGS(id/2)))) <= TOL, & + path//': range: the azimuths of line '//text(INT(id))//' read through the C API') + END DO + CALL CableDyn_Close(h, es) + + CALL CD_AGG_Init_From_Deck(agg, path, 0.02_wp, ies, em) + CALL require(ies == CD_AGG_OK, path//': range: aggregate: '//TRIM(em)) + IF (ies /= CD_AGG_OK) RETURN + n = CD_AGG_NumChannels(agg) + CALL require(n == SIZE(HEADINGS), path//': range: channel count') + DO k = 1, n + CALL CD_AGG_EvalChannel(agg, k, val, ies, em) + CALL require(ies == CD_AGG_OK .AND. ABS(val - range_azimuth(HEADINGS(k))) <= TOL, & + path//': range: aggregate channel of line '//text(2*k)) + CALL CD_AGG_GetInitLine(agg, k, line_id, tension, force, inclination, declination, azimuth, ies, em) + CALL require(ies == CD_AGG_OK .AND. ABS(azimuth - range_azimuth(HEADINGS(line_id/2))) <= TOL, & + path//': range: initialisation summary of line '//text(line_id)) + END DO + CALL CD_AGG_End(agg, ies, em) + END SUBROUTINE check_azimuth_range + + SUBROUTINE write_heading_deck(path, cables) + !! Line 2 k from its own coupled point to its own anchor on HEADINGS(k): an EI = 0 chain, or + !! with cables every second line a finite-EI cable. + CHARACTER(*), INTENT(IN) :: path + LOGICAL, INTENT(IN) :: cables + REAL(wp) :: cx, sy, reach + INTEGER :: u, k + LOGICAL :: cable + + OPEN (NEWUNIT=u, FILE=path, STATUS='REPLACE', ACTION='WRITE') + WRITE (u, '(A)') 'Line values against the channels, lines on both sides of +x' + 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)') 'cable 0.16 36.7 4.69e8 -1.0 1.99e4 1.2 0.1 1.0 0.0' + WRITE (u, '(A)') 'chain 0.10 150.0 8.0e8 -1.0 0.0 1.2 0.4 1.0 0.5' + WRITE (u, '(A)') '--- POINTS ---' + WRITE (u, '(A)') 'ID Type X Y Z' + WRITE (u, '(A)') '(-) (-) (m) (m) (m)' + DO k = 1, SIZE(HEADINGS) + cable = cables .AND. MOD(k, 2) == 0 + reach = MERGE(70.0_wp, 240.0_wp, cable) + cx = COS(HEADINGS(k)*PI/180.0_wp) + sy = SIN(HEADINGS(k)*PI/180.0_wp) + WRITE (u, '(I0,A,2(1X,ES23.15),A)') 2*k - 1, ' Coupled', 5.0_wp*cx, 5.0_wp*sy, ' -10.0' + WRITE (u, '(I0,A,2(1X,ES23.15),A)') 2*k, ' Fixed', (5.0_wp + reach)*cx, (5.0_wp + reach)*sy, ' -60.0' + END DO + WRITE (u, '(A)') '--- LINES ---' + WRITE (u, '(A)') 'ID NodeA NodeB Outputs' + WRITE (u, '(A)') '(-) (-) (-) (-)' + DO k = 1, SIZE(HEADINGS) + WRITE (u, '(I0,1X,I0,1X,I0,A)') 2*k, 2*k - 1, 2*k, ' -' + END DO + WRITE (u, '(A)') '--- SECTIONS ---' + WRITE (u, '(A)') 'LineID LineType Length NumSegs' + WRITE (u, '(A)') '(-) (-) (m) (-)' + DO k = 1, SIZE(HEADINGS) + IF (cables .AND. MOD(k, 2) == 0) THEN + WRITE (u, '(I0,A)') 2*k, ' cable 100.0 20' + ELSE + WRITE (u, '(I0,A)') 2*k, ' chain 260.0 13' + END IF + END DO + WRITE (u, '(A)') '--- OPTIONS ---' + WRITE (u, '(A)') '9.80665 g' + WRITE (u, '(A)') '1025.0 rhoW' + WRITE (u, '(A)') '60.0 WtrDpth' + WRITE (u, '(A)') '1.0e6 kBot' + WRITE (u, '(A)') '1.0e5 cBot' + WRITE (u, '(A)') '0.02 dtM' + WRITE (u, '(A)') '0.0 TMax' + WRITE (u, '(A)') '--- OUTPUTS ---' + DO k = 1, SIZE(HEADINGS) + WRITE (u, '(A,I0,A)') 'L', 2*k, 'N1Azi' + END DO + WRITE (u, '(A)') '--- END ---' + CLOSE (u) + END SUBROUTINE write_heading_deck + SUBROUTINE write_cable_deck(path, heading, element_recovery, stiffener, torsion, chain) !! A 100 m finite-EI cable from a coupled point at z = -10 to an anchor 70 m away along !! heading [deg], at z = -60. Line ids are not consecutive. torsion restrains a level cable at diff --git a/tests/test_model.f90 b/tests/test_model.f90 index 35b1a7b2..a16f32e5 100644 --- a/tests/test_model.f90 +++ b/tests/test_model.f90 @@ -718,9 +718,11 @@ SUBROUTINE case_copy_model_preserves_state() CALL require(es == CD_MODEL_OK .AND. CD_Model_Is_Initialized(src), 'copy:init-src: '//TRIM(em)) CALL CD_Calc_Model_CoupledLoads(src, load_src, es, em) CALL require(es == CD_MODEL_OK .AND. ALLOCATED(src%dynamic_workspace%R), 'copy:source-loads: '//TRIM(em)) + src%azimuth_signed = .TRUE. CALL CD_Copy_Model(src, dst, es, em) CALL require(es == CD_MODEL_OK .AND. CD_Model_Is_Initialized(dst), 'copy:model: '//TRIM(em)) + CALL require(dst%azimuth_signed, 'copy:azimuth-range') CALL require(.NOT. ALLOCATED(dst%dynamic_workspace%R), 'copy:workspace-not-shared') CALL CD_Get_Model_State(src, q_src, v_src, a_src, es, em) CALL require(es == CD_MODEL_OK, 'copy:get-src: '//TRIM(em)) From 655f21df019b24cb9b4fd0da591d2bde8ad9e366 Mon Sep 17 00:00:00 2001 From: Jae Hoon Seo Date: Sat, 7 Nov 2026 23:34:37 +0900 Subject: [PATCH 4/4] chore: keep the changelog entry for the release batch Signed-off-by: Jae Hoon Seo --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02095703..cfadb54d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,8 +18,6 @@ All notable changes to CableDyn are recorded in this file. The format follows `n_fluid_nodes`, `fluid_node_positions` and `update_fluid_fields`. The field is sampled and held as in the standalone march and in OpenFAST; in current and waves a stepped cable matches the standalone driver to the digits of its output. -- `Line.section_curvature()` and the C API quantity `CD_C_LINE_SECTION_CURVATURE` return `CurvX` - and `CurvY` at every node of a finite-EI line in one evaluation. ### Fixed