diff --git a/CHANGELOG.md b/CHANGELOG.md index d43bf15..0328269 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -529,6 +529,16 @@ All notable changes to CableXSec are recorded in this file. The format follows ### Fixed +- `read_cabledyn_sections` reads a section placed on a deck node from that node's channels + alone: `CurvX`/`CurvY` on the components route, and `Ten`, `Twist` and `Torq` on every route. + Before, every section needed the channels of both nodes of its element, so a station written + at one node (the usual OUTPUTS request) could not be read without the next node's channels; + where both are written, the values are unchanged. A section just off a node (a + rounded arc length a few micrometres away) still needs both, and the error now names the + node and its arc length. On the components route the twist and torque histories are kept + at every section whose nodes write them; before, one section without `Twist` channels (a + hang-off node, say) dropped the twist of all of them, and `checks["torsion"]` called the + read twist-free although CableDyn's axes carry the twist. - A constant curvature cycles nothing in the distributed helix model. A sample that repeats the last one under the Coulomb bound now keeps the state in both kernels: its outputs equal the last sample's bit for bit and it takes no substep. Before, the compiled kernel started such a diff --git a/doc/global_local_guide.md b/doc/global_local_guide.md index ea4f39f..f306ee3 100644 --- a/doc/global_local_guide.md +++ b/doc/global_local_guide.md @@ -333,7 +333,13 @@ rotating plane, separators, a missing column). The station reader takes one station's columns under the names above; a CableDyn `.out` file holds them as `TenN`, `CurvXN` and `CurvYN` for every requested node, so a station is passed to `series_from_curvature_components` from its three -columns. `read_cabledyn_sections` does not read `CurvX`/`CurvY`. +columns. `read_cabledyn_sections(..., curvature_source="components")` reads +them at the deck nodes around each section (Section 2); a section placed on a +deck node, at the arc length `CableDynLine.node_arc_lengths()` gives it, needs +that node's `CurvX`, `CurvY` and `Ten` channels only (and its `Twist`/`Torq`, +recorded where written). A station given by a rounded arc length a few +micrometres off its node needs the next node's channels too; the error names +the node and its arc length. ## 3. Sea-state batch @@ -535,10 +541,10 @@ law (`xsec_stiffness`, the backbone of `compute_mkappa`) returned to CableDyn as an element constitutive law with history (biaxial stick–slip state per element and Gauss point, consistent tangent for the implicit integrator, state transfer between steps and restarts), followed by the local recovery -of this guide on the consistently bent histories. Also open: hang-off roll -about the cable axis (from the vessel attitude), wave dynamic pressure, -temperature histories from a coupled thermal run, and conductor fatigue in -the batch. +of this guide on the consistently bent histories. Also open: wave dynamic +pressure, temperature histories from a coupled thermal run, and conductor +fatigue in the batch. (The hang-off roll about the cable axis is read since +the `hang_off_rotation` input and `curvature_source="components"`, Section 2.) Two findings shape stage 2: diff --git a/src/cablexsec/io/cabledyn.py b/src/cablexsec/io/cabledyn.py index 9f27bac..259ee0b 100644 --- a/src/cablexsec/io/cabledyn.py +++ b/src/cablexsec/io/cabledyn.py @@ -136,7 +136,12 @@ cannot carry them and is refused on such a line. ``curvature_source="components"`` reads CableDyn's ``CurvX``/``CurvY`` at the deck nodes bracketing a section instead (linear between them, as the -tension), exact on CableDyn's axes and needing no node positions or angles. A line +tension), exact on CableDyn's axes and needing no node positions or angles. +A section on a deck node (its arc length equal to the node's, as +:meth:`CableDynLine.node_arc_lengths` gives it) reads that node's channels +alone, with no channel at the neighbouring nodes; the twist and torque of +this route are recorded at every section whose nodes write them, since the +axes carry the twist already. A line restrained in torsion at both ends without these channels is rejected, and so is a dynamic run of a line restrained at one end whose torsion CableDyn solves (:attr:`CableDynLine.torsion_solved`): the polar inertia carries torsional @@ -2404,6 +2409,8 @@ class _Plan: window: list[int] # 0-based nodes of the curvature spline (its grid) probe: bool = False node: int = -1 # 0-based deck node of a curvature probe + # 0-based deck nodes of the node channels, with their linear weights + deck_nodes: tuple[tuple[int, float], ...] = () data: dict[str, list[FloatArray]] = field(default_factory=dict) @@ -2412,6 +2419,52 @@ def _element_of(arcs: FloatArray, arc: float) -> int: return min(max(index, 0), int(arcs.size) - 2) +def _deck_weights( + arcs: FloatArray, element: int, arc: float +) -> tuple[tuple[int, float], ...]: + """Deck nodes whose channels give a section's node values, with weights. + + A section on a deck node reads that node alone; any other section is + linear between the two nodes of its element. + """ + if arc == arcs[element]: + return ((element, 1.0),) + if arc == arcs[element + 1]: + return ((element + 1, 1.0),) + weight = float((arc - arcs[element]) / (arcs[element + 1] - arcs[element])) + return ((element, 1.0 - weight), (element + 1, weight)) + + +def _at_deck_nodes( + values: FloatArray, + slot: Mapping[tuple[str, int], int], + kind: str, + nodes: tuple[tuple[int, float], ...], +) -> FloatArray: + """A node channel at a section: the node's own column, or linear between two.""" + if len(nodes) == 1: + return np.array(values[:, slot[(kind, nodes[0][0])]]) + (lower, w_lower), (upper, w_upper) = nodes + return np.asarray( + w_lower * values[:, slot[(kind, lower)]] + + w_upper * values[:, slot[(kind, upper)]] + ) + + +def _nearest_node_hint(arcs: FloatArray, element: int, arc: float) -> str: + """A note for a section that lies just off a deck node.""" + length = float(arcs[element + 1] - arcs[element]) + for node in (element, element + 1): + gap = abs(arc - float(arcs[node])) + if 0.0 < gap <= 1e-3 * length: + return ( + f"; it lies {gap:.3g} m from deck node {node + 1} " + f"(s = {float(arcs[node])!r} m): a section at that arc length reads " + "that node's channels alone" + ) + return "" + + def _window(element: int, n_nodes: int, half: int) -> list[int]: lo = max(0, element - half + 1) hi = min(n_nodes - 1, element + half) @@ -3056,7 +3109,8 @@ def read_cabledyn_sections( ``"auto"`` uses the tangent channels when every section has them and positions otherwise; the choice is recorded. ``"components"`` takes CableDyn's ``CurvXN``/``CurvYN`` (finite-EI lines) on - CableDyn's section axes (module notes). + CableDyn's section axes (module notes); a section on a deck node + needs that node's channels only. tangent_placement : {"auto", "chord"} Where the ``Dec``/``Azi`` values sit along the line. ``"auto"`` (CableDyn 0.4.2 and later): at the node on a finite-EI line, at the @@ -3394,10 +3448,11 @@ def has_components(node: int) -> bool: ) plans: list[_Plan] = [] for name, arc_deck, arc_pos, elem_deck, elem_pos, node in specs: + deck_nodes = _deck_weights(deck_arcs, elem_deck, arc_deck) window = ( _window(elem_deck, n_deck, window_nodes) if source == "tangent" - else [elem_deck, elem_deck + 1] + else [j for j, _ in deck_nodes] if source == "components" else _window(elem_pos, n_pos, window_nodes) ) @@ -3414,6 +3469,11 @@ def has_components(node: int) -> bool: raise CableSchemaError( f"section {name!r}: curvature from {source} needs {what} " f"at nodes {missing}" + + ( + _nearest_node_hint(deck_arcs, elem_deck, arc_deck) + if source == "components" + else "" + ) ) if missing: continue # a probe without its window is not checked @@ -3426,6 +3486,7 @@ def has_components(node: int) -> bool: window, probe=node >= 0, node=node, + deck_nodes=deck_nodes, ) ) sections_plans = [plan for plan in plans if not plan.probe] @@ -3442,13 +3503,24 @@ def has_components(node: int) -> bool: else: checks["tangent_placement"] = "chord mid-arc (EI = 0 line)" - torsion_nodes = [(plan.elem_deck, plan.elem_deck + 1) for plan in sections_plans] - twist_available = all( - ("twist", line_id, n + 1) in channels for pair in torsion_nodes for n in pair - ) - torque_available = all( - ("torq", line_id, n + 1) in channels for pair in torsion_nodes for n in pair - ) + # Twist and torque channels at each section's nodes. The reconstructed + # frame needs the twist at every section; CableDyn's components carry it + # in their axes, so that route records them where they are written. + twist_at = { + plan.name: all( + ("twist", line_id, n + 1) in channels for n, _ in plan.deck_nodes + ) + for plan in sections_plans + } + torque_at = { + plan.name: all(("torq", line_id, n + 1) in channels for n, _ in plan.deck_nodes) + for plan in sections_plans + } + twist_available = all(twist_at.values()) + torque_available = all(torque_at.values()) + if source != "components": + twist_at = dict.fromkeys(twist_at, twist_available) + torque_at = dict.fromkeys(torque_at, torque_available) if line.torsion_restrained and not twist_available and source != "components": raise CableSchemaError( f"line {line_id} is restrained in torsion; the material frame needs " @@ -3494,7 +3566,18 @@ def has_components(node: int) -> bool: "restrained_ends": sorted(line.torsion_restrained_ends), "solved": line.torsion_solved, "not_solved_because": line.torsion_note, - "material_twist": "Twist channels" if twist_available else "none (twist-free)", + "material_twist": ( + "Twist channels" + if twist_available + else "in CableDyn's section axes (CurvX/CurvY); Twist channels at " + + ( + ", ".join(repr(name) for name, ok in twist_at.items() if ok) + if any(twist_at.values()) + else "no section" + ) + if source == "components" and line.torsion_solved + else "none (twist-free)" + ), } # How End A's material frame turns in time (module notes, hang-off). @@ -3562,11 +3645,12 @@ def has_components(node: int) -> bool: ) if not use_segments: for plan in sections_plans: - for node in (plan.elem_deck, plan.elem_deck + 1): + for node, _ in plan.deck_nodes: if ("ten", line_id, node + 1) not in channels: raise CableSchemaError( f"section {plan.name!r}: tension needs .Line{line_id}.t.out " f"or Ten{line_id}N{node + 1} in the main output" + + _nearest_node_hint(deck_arcs, plan.elem_deck, plan.arc) ) # Main-table column plan. main_cols: list[int] = [0] @@ -3590,18 +3674,15 @@ def want(kind: str, node: int) -> None: for node in pos_order: for kind in ("px", "py", "pz"): want(kind, node) - if not use_segments: - for plan in sections_plans: - want("ten", plan.elem_deck) - want("ten", plan.elem_deck + 1) - if twist_available: - for plan in sections_plans: - want("twist", plan.elem_deck) - want("twist", plan.elem_deck + 1) - if torque_available: - for plan in sections_plans: - want("torq", plan.elem_deck) - want("torq", plan.elem_deck + 1) + for plan in sections_plans: + for kind, wanted in ( + ("ten", not use_segments), + ("twist", twist_at[plan.name]), + ("torq", torque_at[plan.name]), + ): + if wanted: + for node, _ in plan.deck_nodes: + want(kind, node) probe_nodes = [plan.node for plan in plans if plan.probe] for node in probe_nodes: want("curv", node) @@ -3668,7 +3749,6 @@ def want(kind: str, node: int) -> None: _chunks(tensions_table, [0] + [1 + e for e in elements], chunk_rows) ) - deck_l0 = np.diff(deck_arcs) # Arc length of each Dec/Azi value: the node's own tangent of a finite-EI # line (CableDyn 0.4.2+) at the node; a chord (central, one-sided at the # ends) at its mid-arc, the point of a smooth arc whose tangent it is. On a @@ -3870,11 +3950,9 @@ def append(plan: _Plan, key: str, values: FloatArray) -> None: e1 = _transport(e1, chords[:, k - 1], chords[:, k]) arc_e1[k] = e1 for plan in plans: - # Linear weights between the bracketing deck nodes (node - # channels) and position-grid nodes (positions). - weight = (plan.arc - deck_arcs[plan.elem_deck]) / deck_l0[ - plan.elem_deck - ] + # Linear weights between the bracketing position-grid nodes; + # the node channels take plan.deck_nodes (the node alone for + # a section on a deck node). weight_pos = (plan.arc - pos_arcs[plan.elem_pos]) / pos_l0[ plan.elem_pos ] @@ -3883,28 +3961,23 @@ def append(plan: _Plan, key: str, values: FloatArray) -> None: # curvature) on its section axes (x, y): the rotation # curvature on the same axes is (-CurvY, CurvX). curv_x, curv_y = ( - (1 - weight) * values[:, slot[(kind, plan.elem_deck)]] - + weight * values[:, slot[(kind, plan.elem_deck + 1)]] + _at_deck_nodes(values, slot, kind, plan.deck_nodes) for kind in ("curvx", "curvy") ) append(plan, "norm", np.hypot(curv_x, curv_y)) - if twist_available: + if twist_at[plan.name]: append( plan, "twist", np.radians( - (1 - weight) - * values[:, slot[("twist", plan.elem_deck)]] - + weight - * values[:, slot[("twist", plan.elem_deck + 1)]] + _at_deck_nodes(values, slot, "twist", plan.deck_nodes) ), ) - if torque_available: + if torque_at[plan.name]: append( plan, "torque", - (1 - weight) * values[:, slot[("torq", plan.elem_deck)]] - + weight * values[:, slot[("torq", plan.elem_deck + 1)]], + _at_deck_nodes(values, slot, "torq", plan.deck_nodes), ) append(plan, "kx", -curv_y) append(plan, "ky", curv_x) @@ -3964,8 +4037,7 @@ def append(plan: _Plan, key: str, values: FloatArray) -> None: axis2 = np.cross(tangent, axis1) if twist_available: twist = np.radians( - (1 - weight) * values[:, slot[("twist", plan.elem_deck)]] - + weight * values[:, slot[("twist", plan.elem_deck + 1)]] + _at_deck_nodes(values, slot, "twist", plan.deck_nodes) ) cos, sin = np.cos(twist)[:, None], np.sin(twist)[:, None] axis1, axis2 = ( @@ -3977,8 +4049,7 @@ def append(plan: _Plan, key: str, values: FloatArray) -> None: append( plan, "torque", - (1 - weight) * values[:, slot[("torq", plan.elem_deck)]] - + weight * values[:, slot[("torq", plan.elem_deck + 1)]], + _at_deck_nodes(values, slot, "torq", plan.deck_nodes), ) append(plan, "kx", np.sum(kappa * axis1, axis=-1)) append(plan, "ky", np.sum(kappa * axis2, axis=-1)) @@ -4010,9 +4081,7 @@ def append(plan: _Plan, key: str, values: FloatArray) -> None: :, ten_slot[lower + 1] ] else: - ten = (1 - weight) * values[ - :, slot[("ten", plan.elem_deck)] - ] + weight * values[:, slot[("ten", plan.elem_deck + 1)]] + ten = _at_deck_nodes(values, slot, "ten", plan.deck_nodes) append(plan, "tension", ten) append(plans[0], "time", time) n_rows += nt @@ -4243,7 +4312,10 @@ def share(item: dict[str, Any]) -> float: "in time by minimal rotation)" ), "material twist from Twist channels" - if twist_available + if twist_at[plan.name] + else "material twist in CableDyn's section axes (no Twist " + "channels at the section)" + if source == "components" and line.torsion_solved else "no material twist (line not restrained in torsion)", "hydrostatic external pressure only (no wave dynamic pressure)" if pressure == "hydrostatic" diff --git a/tests/test_cabledyn_outputs.py b/tests/test_cabledyn_outputs.py index 5989020..66876f4 100644 --- a/tests/test_cabledyn_outputs.py +++ b/tests/test_cabledyn_outputs.py @@ -746,3 +746,85 @@ def test_components_route_needs_the_component_channels(tmp_path): read_cabledyn_sections( root, deck, {"s": 4.95}, curvature_source="components", pressure="none" ) + + +def _vu_without(tmp_path, dropped): + """``vu_torsion`` with the named ``.CD.out`` channels left out.""" + root, deck = _vu_copy(tmp_path) + path = tmp_path / "vu.CD.out" + rows = [row.split("\t") for row in path.read_text(encoding="utf-8").splitlines()] + keep = [i for i, name in enumerate(rows[0]) if name not in dropped] + path.write_text( + "\n".join("\t".join(row[i] for i in keep) for row in rows) + "\n", + encoding="utf-8", + ) + return root, deck + + +def test_components_section_on_a_deck_node_reads_that_node_alone(tmp_path): + """A section on a deck node takes that node's CurvX/CurvY, Ten, Twist and + Torq without the next node's channels, bit for bit; a section just off + the node still needs both and is told where the node lies.""" + channels = _main_channels_units(DATA / "vu_torsion.CD.out") + arcs = read_cabledyn_deck(DATA / "vu_torsion.dat").line(1).node_arc_lengths() + on_node = float(arcs[10]) # deck node 11 + full = read_cabledyn_sections( + DATA / "vu_torsion", + DATA / "vu_torsion.dat", + {"s": on_node}, + curvature_source="components", + pressure="none", + ).histories["s"] + root, deck = _vu_without( + tmp_path, + {f"{kind}1N12" for kind in ("Ten", "Curv", "CurvX", "CurvY", "Twist", "Torq")}, + ) + read = read_cabledyn_sections( + root, deck, {"s": on_node}, curvature_source="components", pressure="none" + ) + history = read.histories["s"] + np.testing.assert_array_equal( + history.rotation_curvature_x_1_m, -channels["CurvY1N11"] + ) + np.testing.assert_array_equal( + history.rotation_curvature_y_1_m, channels["CurvX1N11"] + ) + np.testing.assert_array_equal(history.effective_tension_N, channels["Ten1N11"]) + np.testing.assert_array_equal(history.twist_rad, np.radians(channels["Twist1N11"])) + np.testing.assert_array_equal(history.torque_Nm, channels["Torq1N11"]) + for name in ( + "rotation_curvature_x_1_m", + "effective_tension_N", + "twist_rad", + "torque_Nm", + ): + np.testing.assert_array_equal(getattr(history, name), getattr(full, name)) + with pytest.raises(CableSchemaError, match="from deck node 11"): + read_cabledyn_sections( + root, + deck, + {"s": on_node + 1e-6}, + curvature_source="components", + pressure="none", + ) + + +def test_components_route_records_the_twist_where_it_is_written(tmp_path): + """CableDyn's components carry the twist in their axes: a section whose + nodes write no Twist channel is still read, without a twist history, and + the other sections keep theirs.""" + arcs = read_cabledyn_deck(DATA / "vu_torsion.dat").line(1).node_arc_lengths() + root, deck = _vu_without(tmp_path, {"Twist1N30", "Torq1N30"}) + read = read_cabledyn_sections( + root, + deck, + {"upper": float(arcs[10]), "mid": float(arcs[29])}, + curvature_source="components", + pressure="none", + ) + assert read.histories["upper"].twist_rad is not None + assert read.histories["upper"].torque_Nm is not None + assert read.histories["mid"].twist_rad is None + assert read.histories["mid"].torque_Nm is None + assert "'upper'" in read.checks["torsion"]["material_twist"] + assert "'mid'" not in read.checks["torsion"]["material_twist"]