diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c95d454..3d552ce6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,20 @@ All notable changes to CableDyn are recorded in this file. The format follows outer iterations per step. The junction converges to the same tolerance, so results differ only within it, and by no more than the tolerance already allowed against a tightly converged run: tensions by about 2e-6 relative and the body position by under 1 µm. +- The OpenFAST linearisation check (`validation/scripts/openfast_lin_ab.py`) now compares + CableDyn's quasi-static mooring stiffness entry by entry with stock MoorDyn's zero-frequency + reduction `D - C A^-1 B`, and fails above 0.5 %. It writes the `.lin` files with the + full-precision `OutFmt` `ES24.15E3`, lets MoorDyn settle its initial condition, and + linearises at t = 0. Its default case is the `examples/openfast` IEA-15 UMaine twin. On + that case the per-fairlead stiffness agrees to 0.07–0.10 % (0.29 % for the transverse + term), and the platform 6 × 6 diagonals agree to 0.10 % or better. The earlier record had + called MoorDyn's reduction unusable; with the 4-digit default `OutFmt` the same case + reproduces that asymmetric reduction, so the check now refuses a reference written below + full precision. VALIDATION.md and the OpenFAST page of the manual are updated. +- The FAST.Farm shared-mooring comparison with stock MoorDyn now covers 600 s of the r-test + `MD_Shared` farm (`validation/scripts/fastfarm_shared_ab.py`): tension means agree to 0.02 %, + extremes to 0.25 %, and platform motions to millimetres. The migration guide now says that on + a line with no `Fixed` end `FairTen` and `AnchTen` name the opposite ends from MoorDyn's. ### Fixed diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index bad3ba8e..7e3c14f9 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -169,6 +169,12 @@ cmake -S -B -DCABLEDYN_ROOT= powershell -File integration/openfast/openfast_build_gate.ps1 -BuildDir -CaseFst ``` +With the MinGW gfortran of the conda environment, add +`-DCMAKE_EXE_LINKER_FLAGS="-Wl,--stack,16777216"` to the configure line. OpenFAST's +CMake raises the 2 MB default stack to 16 MB only for Cygwin and Visual Studio builds, +and a MinGW `FAST.Farm.exe` (`-DBUILD_FASTFARM=ON`) with the default stack stops with a +stack overflow while ElastoDyn initialises. + `openfast_build_gate.ps1` builds `openfast.exe`, runs a `CompMooring = 5` case, checks that the CableDyn banner appears in the log, and checks that the output is finite. diff --git a/RELEASE.md b/RELEASE.md index ad8f6270..1d3a1e8a 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -68,9 +68,14 @@ only the `cabledyn` package and its metadata. `openfast.exe` with CableDyn, checks that a coupled run prints the CableDyn banner, and checks that the output is finite. 2. **Linearisation check**: with the built executable, run - `python validation/scripts/openfast_lin_ab.py --exe --case-dir `. - The CableDyn mooring block must be symmetric to ≤ 1e-3 with negative restoring - diagonals and bounded sway coupling. + `python validation/scripts/openfast_lin_ab.py --exe --case-dir `, + where `` holds the `examples/openfast` IEA-15 UMaine twin decks and + the r-test `MD_Shared` turbine-1 ElastoDyn, HydroDyn, SeaState and HydroData + inputs (listed in the script docstring). The CableDyn mooring block must be + symmetric to ≤ 1e-3 with negative restoring diagonals and bounded sway + coupling. Its per-fairlead blocks and platform 6×6 stiffness must match stock + MoorDyn's quasi-static reduction to 0.5%. The script writes the `.lin` files + at full precision. 3. Confirm that [VALIDATION.md](VALIDATION.md) matches the results at the release commit. diff --git a/VALIDATION.md b/VALIDATION.md index c93346d7..04b223b7 100644 --- a/VALIDATION.md +++ b/VALIDATION.md @@ -330,6 +330,24 @@ scripts, results, and source provenance are in bit-identical (`correction_rewind`); a 40 s `NumCrctn = 1` OpenFAST run matches stock MoorDyn to **≤0.16%** of range and is bit-identical to its own `NumCrctn = 0` run. + - *Linearisation* + ([`validation/scripts/openfast_lin_ab.py`](https://github.com/SMI-Lab-Inha/CableDyn/blob/main/validation/scripts/openfast_lin_ab.py)): + the twin decks linearise once at t = 0. Each run has platform DOFs only, still water, and HydroDyn + `ExctnMod = RdtnMod = 0`. MoorDyn's relaxation runs for `TmaxIC = 400 s`, and both `.lin` files are + written with `OutFmt = ES24.15E3`. CableDyn's quasi-static `dYdu` (no states) is compared with + MoorDyn's zero-frequency reduction `D − C A⁻¹ B` over 882 states. The per-fairlead stiffness agrees + to **0.07–0.10%** (line 1: 49 741 / 49 706 N/m in-plane horizontal) and the transverse `H/X` term + to 0.29%. The platform 6×6 diagonals agree to **≤0.10%** (surge 77 211 / 77 156 N/m, + roll 2.6473e8 / 2.6466e8 N·m/rad), and every entry to ≤1.0e-3 of √(KᵢᵢKⱼⱼ). Standalone CableDyn + static solves reproduce the block by finite differences to 1.5e-5, and its transverse term equals + the analytic `H/X`. Both 50-segment models sit 7–12% above the continuous elastic catenary + because of the touchdown discretisation; refining to 800 segments converges to it within 0.1%. + MoorDyn's reduction needs the full-precision `OutFmt`. With the usual `ES10.3E2` (4 digits), + `D ≈ 1e8 N/m` cancels to a ~1e4 N/m result and the block is 57% asymmetric with sign-flipped + diagonals; the script refuses such a reference. In the full-system ElastoDyn linearisation, roll + and pitch are 7.29e6 N·m/rad below the module-level 6×6 for both mooring modules. A run with no + mooring and only a constant −6.084e6 N vertical load at the platform reference point (HydroDyn + `AddF0`) reproduces this term, so it comes from ElastoDyn/glue code and not from the mooring module. The mooring-only A/B compares an all-chain system that both codes model in the same way; the finite-EI cable under coupled motion is compared with OrcaFlex in L4-1c. @@ -707,8 +725,8 @@ stop with a named error. | Correction iterations (`NumCrctn > 0`) | Pass verified | bit-identical to own `NumCrctn = 0` run | | SeaState wave/current kinematics | Pass verified | wave A/B ≤0.16% of range | | Checkpoint-restart (incl. Free/Connect points) | Pass verified | restarted window = continuous run to 0.0 | -| Linearisation | Pass scoped | quasi-static `dYdu`; invariants tested; not compared entry by entry with MoorDyn's `-lin` output, which is sensitive to the seabed-contact operating point in this case | -| FAST.Farm shared moorings + clumps + channel record | Pass verified | 60 s farm A/B against stock MoorDyn; tension differences range from 0.04% to 3% across the compared statistics | +| Linearisation | Pass verified (quasi-static stiffness) | quasi-static `dYdu` compared entry by entry with stock MoorDyn's zero-frequency reduction `D − C A⁻¹ B` (full-precision `.lin` output): fairlead stiffness 0.07–0.10%, transverse `H/X` term 0.29%, platform 6×6 diagonals ≤0.10%; standalone static FD 1.5e-5; transverse term equals `H/X`. Velocity/acceleration columns are zero by design ([`openfast_lin_ab.py`](https://github.com/SMI-Lab-Inha/CableDyn/blob/main/validation/scripts/openfast_lin_ab.py)) | +| FAST.Farm shared moorings + clumps + channel record | Pass verified | 600 s A/B of the r-test `MD_Shared` farm against stock MoorDyn (same deck, analysis from 100 s; [`fastfarm_shared_ab.py`](https://github.com/SMI-Lab-Inha/CableDyn/blob/main/validation/scripts/fastfarm_shared_ab.py)): tension means ≤0.02%, minima and maxima ≤0.25%, platform motions within millimetres and 0.001°; shared-line tension std 2.2–2.7% lower, 1.0% at half the mooring step (CableDyn's implicit step at the 0.05 s coupling step against MoorDyn's 1e-4 s explicit step). On the shared lines, which have no Fixed end, `FairTen`/`AnchTen` name the opposite ends from MoorDyn's (see the migration guide). MoorDyn's initial settling does not converge for the anchor legs 6 and 7 (2.4 m excess length at t = 0), a start-up transient over by 100 s that most likely set the 3% end of the earlier 60 s record | | Slack-line tension convention | Pass verified | tension-only dynamics; BA on slack lines matches `MoorDyn_Line.f90` | | Farm + restart / Farm + linearisation | Not applicable | FAST.Farm has no restart and does not call module linearisation; the same applies to stock MoorDyn | | Line failures (`FAILURE` table) | Pass verified | MoorDyn semantics on both paths (reserve-point pre-allocation, time or tension trigger, detach onto a massless Free point); standalone (point systems and free Rigid6 bodies), coupled, and checkpoint-replay tests | diff --git a/doc/migrating.rst b/doc/migrating.rst index c2954251..e987db21 100644 --- a/doc/migrating.rst +++ b/doc/migrating.rst @@ -34,7 +34,11 @@ elements — exactly equivalent to a CableDyn ``LINES`` row ``1 4 1 -`` plus one ``1 chain 850.0 50``. MoorDyn writes lines anchor-first; when ``AttachA`` is a ``Fixed`` anchor and ``AttachB`` a ``Vessel``/``Coupled``/``Body`` fairlead, CableDyn swaps the ends so that **End A is the fairlead**, as everywhere in CableDyn. Endpoint channels therefore keep their -physical meaning: ``FairTen`` is at the fairlead, ``AnchTen`` at the anchor. +physical meaning: ``FairTen`` is at the fairlead, ``AnchTen`` at the anchor. A line with no +``Fixed`` end (a shared line between two turbines or through a clump) keeps the order written: +CableDyn's ``FairTen`` is at ``AttachA`` and ``AnchTen`` at ``AttachB``, while MoorDyn writes +``FairTen`` at ``AttachB`` (its last node) and ``AnchTen`` at ``AttachA``. Compare such lines +by end, not by channel name. ``examples/openfast/MoorDyn_UMaine.dat`` is such a stock deck. For a standalone run it needs only what OpenFAST (maintained by NLR, the National Laboratory of the Rockies, formerly NREL) @@ -112,7 +116,8 @@ Keyword map decks in OpenFAST (the standalone driver rejects it). A deck with a finite-EI cable rejects both * - ``FairTen``, ``AnchTen``, point/node channels - - same names; tensions in N (:doc:`outputs`) + - same names; tensions in N (:doc:`outputs`). On a line with no ``Fixed`` end the two + end channels name the opposite ends from MoorDyn's (see above) What differs on purpose ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/openfast.rst b/doc/openfast.rst index 3791536a..1e701366 100644 --- a/doc/openfast.rst +++ b/doc/openfast.rst @@ -256,7 +256,10 @@ without an auxiliary process: ``MooringFile`` and removed after use, so side files resolve as on restart. Set ``dtM = DT`` and linearise at the initial operating point (``CalcSteady = False``, ``LinTimes = 0``, platform at its still-water equilibrium, rotor speed and pitch at their - steady values). Contact switching makes derivatives near touchdown non-smooth. A deck with + steady values). Contact switching makes derivatives near touchdown non-smooth. OpenFAST writes + the ``.lin`` matrices in the ``.fst`` ``OutFmt``; to compare them with MoorDyn's + ``D - C A^-1 B``, use a full-precision format such as ``"ES24.15E3"``, because the usual + 4-digit format cannot resolve that reduction. A deck with Rigid6 bodies or rods is rejected at initialisation: the quasi-static reduction would hold them at their deck pose instead of re-solving their equilibrium. * **FAST.Farm** selects CableDyn with ``Mod_SharedMooring = 5`` for shared lines and diff --git a/validation/scripts/fastfarm_shared_ab.py b/validation/scripts/fastfarm_shared_ab.py new file mode 100644 index 00000000..5843e155 --- /dev/null +++ b/validation/scripts/fastfarm_shared_ab.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +"""FAST.Farm shared-mooring A/B: CableDyn (Mod_SharedMooring = 5) vs stock MoorDyn (= 3). + +Twin runs of OpenFAST's r-test case ``glue-codes/fast-farm/MD_Shared`` (two IEA-15 MW UMaine +turbines, three shared lines through two clumps, four anchor legs, steady 8 m/s wind, polar +wake, JONSWAP waves), identical except for the shared-mooring module. CableDyn reads the stock +MoorDyn deck ``SharedMooring2.dat`` unchanged. + +``prepare`` copies the r-test folder twice and edits, identically in both copies except for +``Mod_SharedMooring``: ``TMax``, ``WrMooringVis False``, turbine ``WrVTK 0``, and the +anchor-leg anchor tensions ``AnchTen5``-``AnchTen7`` (the r-test deck writes ``AnchTen1``- +``AnchTen4`` only) and the clump position channels ``Point2p*``/``Point3p*`` added to the +deck's OUTPUTS. Run ``FAST.Farm.exe +FAST.Farm.fstf`` in each copy (one OpenMP thread), then ``compare``. + +Channel pairing. MoorDyn writes ``FairTen`` at the line's last node (deck ``AttachB``) and +``AnchTen`` at its first (``AttachA``) for every line. CableDyn writes ``FairTen`` at its +End A and ``AnchTen`` at its End B, and swaps a stock line's ends only when ``AttachA`` is a +Fixed anchor. On the anchor legs 4-7 the names therefore agree; on the shared lines 1-3, which +have no Fixed end, the same physical end carries the opposite name, and ``compare`` pairs them +by end (both pairings are printed for the record). + +Usage: + python -I validation/scripts/fastfarm_shared_ab.py prepare --rtest \\ + --out [--tmax 600] + python -I validation/scripts/fastfarm_shared_ab.py compare --md \\ + --cd [--t-start 100] [--csv ] + +Outputs compared (window t >= t-start): shared-line and anchor-leg tensions, clump positions, +and both turbines' platform motions; mean, std, min, max as (CD - MD)/|MD| and the RMS of the +difference over the MoorDyn std. +""" + +from __future__ import annotations + +import argparse +import re +import shutil +import sys +from pathlib import Path + +import numpy as np + +TURBINE_FILES = ("IEA-15-240-RWT-UMaineSemi_T1.fst", "IEA-15-240-RWT-UMaineSemi_T2.fst") + + +def die(msg: str) -> None: + print(f"FAIL: {msg}") + sys.exit(1) + + +def set_value(text: str, key: str, value: str, what: str) -> str: + """Replace the value field (first word) of the line whose key is `key`.""" + pat = re.compile(r"^(\s*)(\S+)(\s+" + re.escape(key) + r"\b)", re.M) + new, n = pat.subn(lambda m: m.group(1) + value + m.group(3), text, count=1) + if n != 1: + die(f"{what}: key {key} not found") + return new + + +def prepare(rtest: Path, out: Path, tmax: float) -> None: + if not (rtest / "FAST.Farm.fstf").is_file(): + die(f"{rtest} is not the MD_Shared r-test folder (no FAST.Farm.fstf)") + for tag, mod in (("md", 3), ("cd", 5)): + run = out / tag + if run.exists(): + die(f"{run} exists; choose an empty --out") + shutil.copytree(rtest, run, ignore=shutil.ignore_patterns("*.out", "*.outb", "*.vtp")) + fstf = run / "FAST.Farm.fstf" + t = fstf.read_text() + t = set_value(t, "TMax", f"{tmax:.1f}", fstf.name) + t = set_value(t, "Mod_SharedMooring", str(mod), fstf.name) + t = set_value(t, "WrMooringVis", "False", fstf.name) + fstf.write_text(t) + for name in TURBINE_FILES: + f = run / name + f.write_text(set_value(f.read_text(), "WrVTK", "0", name)) + deck = run / "SharedMooring2.dat" + t = deck.read_text() + marker = "FairTen7\n" + if marker not in t: + die("SharedMooring2.dat: OUTPUTS marker FairTen7 not found") + extra = "".join(f"AnchTen{line}\n" for line in (5, 6, 7)) + extra += "".join(f"Point{p}p{c}\n" for p in (2, 3) for c in "xyz") + deck.write_text(t.replace(marker, marker + extra, 1)) + print(f"prepared {run} (Mod_SharedMooring = {mod}, TMax = {tmax:g} s)") + + +def read_table(path: Path) -> dict[str, np.ndarray]: + """A FAST-style or CableDyn table: a 'Time' header row, a units row, then numbers.""" + if not path.is_file(): + die(f"missing output {path}") + lines = path.read_text().splitlines() + try: + i = next(k for k, line in enumerate(lines) if line.split() and line.split()[0] == "Time") + except StopIteration: + die(f"{path}: no 'Time' header") + names = lines[i].split() + data = np.array([line.split() for line in lines[i + 2 :] if line.strip()], dtype=float) + if data.ndim != 2 or data.shape[1] != len(names) or not np.all(np.isfinite(data)): + die(f"{path}: malformed or non-finite table") + return {n.upper(): data[:, j] for j, n in enumerate(names)} + + +def run_tmax(run: Path) -> float: + """TMax of a prepared run folder, from its FAST.Farm.fstf.""" + fstf = run / "FAST.Farm.fstf" + if not fstf.is_file(): + die(f"missing {fstf}") + m = re.search(r"^\s*(\S+)\s+TMax\b", fstf.read_text(), re.M) + if not m: + die(f"{fstf}: no TMax") + return float(m.group(1)) + + +def require_complete(table: dict, tmax: float, what: str) -> None: + """A run that stopped early would leave a shorter shared window: refuse it by name. A + complete table ends within one output interval of TMax (FAST.Farm's MoorDyn output omits + the last row).""" + t = table["TIME"] + if t.size < 2: + die(f"{what} has fewer than two rows") + t_end = float(t[-1]) + dt_out = float(np.median(np.diff(t))) + if t_end < tmax - 1.5 * dt_out: + die(f"{what} ends at t = {t_end:g} s, before TMax = {tmax:g} s (incomplete run)") + + +def align(a: dict, b: dict, t0: float, cadence_ms: int = 50) -> tuple[np.ndarray, np.ndarray]: + """Indices of the samples both tables share on a 1 ms grid, every cadence_ms, from t0.""" + ta = np.round(a["TIME"] * 1000).astype(np.int64) + tb = np.round(b["TIME"] * 1000).astype(np.int64) + common, ia, ib = np.intersect1d(ta, tb, return_indices=True) + keep = (common >= round(t0 * 1000)) & (common % cadence_ms == 0) + if not np.any(keep): + die("the two runs share no samples in the window") + return ia[keep], ib[keep] + + +def row(label: str, xm: np.ndarray, xc: np.ndarray) -> dict: + sm = np.array([xm.mean(), xm.std(), xm.min(), xm.max()]) + sc = np.array([xc.mean(), xc.std(), xc.min(), xc.max()]) + with np.errstate(divide="ignore", invalid="ignore"): + rel = np.where(np.abs(sm) > 0, (sc - sm) / np.abs(sm), np.nan) + rms = float(np.sqrt(np.mean((xc - xm) ** 2))) + return {"channel": label, "md": sm, "cd": sc, "rel": rel, + "rms_over_std": rms / sm[1] if sm[1] > 0 else np.nan} + + +def compare(md_dir: Path, cd_dir: Path, t0: float, csv: Path | None) -> None: + tmax = run_tmax(md_dir) + if abs(run_tmax(cd_dir) - tmax) > 1.0e-9 * max(1.0, tmax): + die("the two runs have different TMax") + md = read_table(md_dir / "FAST.Farm.FarmMD.MD.out") + cd = read_table(cd_dir / "FAST.Farm.FarmCD.out") + require_complete(md, tmax, "MoorDyn mooring output") + require_complete(cd, tmax, "CableDyn mooring output") + ia, ib = align(md, cd, t0) + pairs = [] + ends = {1: ("T1 fairlead", "clump 2"), 2: ("clump 2", "clump 3"), 3: ("clump 3", "T2 fairlead")} + for line, (a, b) in ends.items(): + pairs.append((f"L{line} end A, {a} [MD AnchTen{line} / CD FairTen{line}]", + f"ANCHTEN{line}", f"FAIRTEN{line}")) + pairs.append((f"L{line} end B, {b} [MD FairTen{line} / CD AnchTen{line}]", + f"FAIRTEN{line}", f"ANCHTEN{line}")) + for line in (4, 5, 6, 7): + pairs.append((f"L{line} anchor [AnchTen{line}]", f"ANCHTEN{line}", f"ANCHTEN{line}")) + pairs.append((f"L{line} fairlead [FairTen{line}]", f"FAIRTEN{line}", f"FAIRTEN{line}")) + for p in (2, 3): + for c in "XYZ": + pairs.append((f"Point{p}p{c.lower()} (clump)", f"POINT{p}P{c}", f"POINT{p}P{c}")) + rows = [] + for label, km, kc in pairs: + if km not in md or kc not in cd: + die(f"channel {km} / {kc} missing (run 'prepare' first)") + rows.append(row(label, md[km][ia], cd[kc][ib])) + same = [row(f"same name {k}{line}", md[f"{k}{line}"][ia], cd[f"{k}{line}"][ib]) + for line in (1, 2, 3) for k in ("FAIRTEN", "ANCHTEN")] + for tw in ("T1", "T2"): + a = read_table(md_dir / f"FAST.Farm.{tw}.out") + b = read_table(cd_dir / f"FAST.Farm.{tw}.out") + require_complete(a, tmax, f"MoorDyn run {tw} output") + require_complete(b, tmax, f"CableDyn run {tw} output") + ja, jb = align(a, b, t0) + for ch in ("PtfmSurge", "PtfmSway", "PtfmHeave", "PtfmRoll", "PtfmPitch", "PtfmYaw"): + rows.append(row(f"{tw} {ch}", a[ch.upper()][ja], b[ch.upper()][jb])) + print(f"window t >= {t0:g} s; % = (CD - MD)/|MD|; rms/std = RMS(CD - MD)/std(MD)") + print(f"{'channel':54s} {'MD mean':>11s} {'mean%':>8s} {'std%':>8s} {'min%':>8s} {'max%':>8s} {'rms/std':>8s}") + lines = ["channel,MD_mean,CD_mean,MD_std,CD_std,MD_min,CD_min,MD_max,CD_max," + "rel_mean,rel_std,rel_min,rel_max,rms_over_std"] + for r in rows + same: + m, c, rel = r["md"], r["cd"], 100 * r["rel"] + print(f"{r['channel']:54s} {m[0]:11.5g} {rel[0]:8.3f} {rel[1]:8.2f} {rel[2]:8.3f} " + f"{rel[3]:8.3f} {r['rms_over_std']:8.3f}") + vals = [m[0], c[0], m[1], c[1], m[2], c[2], m[3], c[3], *r["rel"], r["rms_over_std"]] + lines.append(",".join([f'"{r["channel"]}"'] + [f"{v:.8g}" for v in vals])) + if csv: + csv.write_text("\n".join(lines) + "\n") + print(f"wrote {csv}") + + +def main() -> None: + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + sub = ap.add_subparsers(dest="cmd", required=True) + p = sub.add_parser("prepare", help="copy and edit the r-test case for both modules") + p.add_argument("--rtest", type=Path, required=True) + p.add_argument("--out", type=Path, required=True) + p.add_argument("--tmax", type=float, default=600.0) + c = sub.add_parser("compare", help="compare the MoorDyn and CableDyn runs") + c.add_argument("--md", type=Path, required=True) + c.add_argument("--cd", type=Path, required=True) + c.add_argument("--t-start", type=float, default=100.0) + c.add_argument("--csv", type=Path) + args = ap.parse_args() + if args.cmd == "prepare": + prepare(args.rtest, args.out, args.tmax) + else: + compare(args.md, args.cd, args.t_start, args.csv) + + +if __name__ == "__main__": + main() diff --git a/validation/scripts/openfast_lin_ab.py b/validation/scripts/openfast_lin_ab.py index 32cb0ebf..c8c72970 100644 --- a/validation/scripts/openfast_lin_ab.py +++ b/validation/scripts/openfast_lin_ab.py @@ -5,32 +5,68 @@ Twin still-water IEA-15MW UMaine runs, identical except for the mooring module, each linearized once at the same time with module-level output (LinOutMod). -WHAT IS GATED -- CableDyn's quasi-static dYdu stiffness block (force rows vs -translation-displacement columns, matched by ROW/COLUMN LABELS) must satisfy -the invariants of a true mooring tangent stiffness at equilibrium: - * symmetry (a conservative static tangent); - * negative displacement diagonals (restoring); - * spread-symmetry: a collective surge produces (near-)zero net sway force - on this y-symmetric three-line spread. -Measured on this model the block is symmetric to machine noise and its -collective surge stiffness is stable across operating points (~75 kN/m). - -WHAT IS REPORTED, NOT GATED -- the same block from stock MoorDyn's -zero-frequency reduction K_md = D - C A^-1 B. Measured on this catenary + -seabed model, MoorDyn's own FD linearization is contact-noisy: its block is -asymmetric (~14 %) and violates the spread's y-symmetry by orders of -magnitude, so it is NOT a usable reference here (the numbers are printed as -evidence). The rigorous cross-code check is the finite-displacement settled -A/B (displaced-start static solves in both codes, `openfast_settled_ab.py`). +The script derives the linearization decks from a time-domain twin pair: + * .fst: one linearization at --t-lin (default 0, the still-water initial + operating point the CableDyn documentation prescribes), LinInputs = + LinOutputs = 2, LinOutMod, and a FULL-PRECISION OutFmt (--outfmt, default + ES24.15E3). OpenFAST writes the .lin matrices with OutFmt; the usual + ES10.3E2 keeps 4 significant digits, which cannot resolve MoorDyn's + D - C A^-1 B (D ~ 1e8 N/m, the result ~ 1e4 N/m) -- the cancellation + turns the reduction into noise; + * HydroDyn: ExctnMod = RdtnMod = NewmanApp = 0 (HydroDyn refuses to + linearize DFT excitation, convolution radiation and Newman drift); + * SeaState: WaveMod = 0 (still water; SeaState linearizes only still water); + * MoorDyn deck: TmaxIC = --md-tmaxic (default 400 s) and threshIC = 1e-7, + so the dynamic-relaxation initial state is settled, and no + `SeaState WaterKin` row (no effect in still water). + +WHAT IS GATED + 1. CableDyn invariants of a tangent mooring stiffness at equilibrium, on its + quasi-static dYdu (force rows vs translation-displacement columns, matched + by ROW/COLUMN LABELS): symmetry, negative (restoring) displacement + diagonals, and no net sway force under a collective surge of this + y-symmetric spread. + 2. The reference itself: MoorDyn's zero-frequency reduction + K_md = D - C A^-1 B must be symmetric (<= 1e-3) and its .lin matrices + must carry >= 10 significant digits; otherwise the comparison is refused + with a named error (the usual cause is a low-precision OutFmt). + 3. CableDyn against MoorDyn, entry by entry: + * per-fairlead 3x3 stiffness blocks: entries >= 10 % of the block's + largest within --gate-pct (default 0.5 %), every entry >= 1 % of it + within 2 x --gate-pct; cross-fairlead entries ~0 in both; + * the 6x6 platform stiffness about the global origin (rigid platform + motion of the fairleads plus the geometric term of each code's own + operating-point fairlead loads): diagonals within --gate-pct, every + entry within --gate-pct of sqrt(K_ii K_jj). + Measured on this model (OutFmt ES24.15E3, TmaxIC 400 s, t = 0): the + dominant fairlead entries agree to 0.07-0.10 %, the small transverse + (H/X) entry to 0.29 %, the 6x6 diagonals to <= 0.10 % and every 6x6 + entry to 1.0e-3 of sqrt(K_ii K_jj) or better. + +The 6x6 is the module-level stiffness. A full-system ElastoDyn linearization +additionally contains a term of ElastoDyn/glue origin proportional to the +static vertical platform load (identical for both modules; see VALIDATION.md). + +Case directory: the twin pair examples/openfast/IEA-15-UMaine_CompMooring5_CableDyn.fst +and IEA-15-UMaine_CompMooring3_MoorDyn.fst (the --ref-cd/--ref-md defaults) +with their mooring decks CableDyn_UMaine.dat and MoorDyn_UMaine.dat, plus the +files they reference that are not in the repository: the ElastoDyn (with its +tower and blade files), HydroDyn and SeaState inputs and the WAMIT HydroData +folder of OpenFAST r-test glue-codes/fast-farm/MD_Shared (turbine 1, with the +pose reset to zero as described in examples/openfast/README.md). The script +writes its Main_lin_* decks and *_lin input copies into that directory. Fail-closed: missing .lin files, unparseable sections, empty matched blocks (e.g. a module whose coupled-mesh inputs did not enter the linearization set), -and non-finite entries all abort with a named error. +mismatched fairlead geometry between the two decks, and non-finite entries all +abort with a named error. Usage: python validation/scripts/openfast_lin_ab.py --exe --case-dir - [--ref-cd Main_settle_cd5.fst] [--ref-md Main_settle_md3.fst] - [--t-lin 60.0] [--gate-pct 5.0] [--skip-run] + [--ref-cd IEA-15-UMaine_CompMooring5_CableDyn.fst] + [--ref-md IEA-15-UMaine_CompMooring3_MoorDyn.fst] + [--t-lin 0.0] [--gate-pct 0.5] [--outfmt ES24.15E3] [--md-tmaxic 400] + [--skip-run] """ from __future__ import annotations @@ -44,6 +80,7 @@ import numpy as np LIN_BLOCK = re.compile(r"^-+\s*LINEARIZATION\s*-+", re.IGNORECASE) +MIN_DIGITS = 10 # significant digits the .lin matrices must carry for the MoorDyn reduction def die(msg: str) -> None: @@ -51,14 +88,17 @@ def die(msg: str) -> None: sys.exit(1) -def swap_keys(src: Path, dst: Path, swaps: dict[str, str], required: bool, what: str) -> set[str]: +def swap_keys(src: Path, dst: Path, swaps: dict[str, str], required: bool, what: str, + drop: re.Pattern | None = None) -> set[str]: """Copy a value-first OpenFAST input file, replacing the value field of the named keys. The key is the LAST word before ' - ' (LinTimes carries a - comma-separated value list).""" + comma-separated value list). Lines matching `drop` are left out.""" lines = src.read_text().splitlines(keepends=True) out: list[str] = [] seen: set[str] = set() for ln in lines: + if drop is not None and drop.match(ln): + continue m = re.match(r"^\s*[^-\s].*?(\w+)\s+-\s", ln) key = m.group(1) if m else None if key in swaps: @@ -74,12 +114,37 @@ def swap_keys(src: Path, dst: Path, swaps: dict[str, str], required: bool, what: return seen -def prepare_lin_deck(src: Path, dst: Path, t_lin: float) -> None: +def file_entry(text: str, key: str, where: str) -> str: + m = re.search(rf'"([^"]+)"\s+{key}\b', text) + if not m: + die(f"{where}: {key} entry not found") + return m.group(1) + + +def derive(dst_fst: Path, text: str, key: str, suffix: str, swaps: dict[str, str], + need: set[str], what: str, drop: re.Pattern | None = None) -> str: + """Write a modified copy of the input file the .fst names under `key` and + point the .fst text at it.""" + name = file_entry(text, key, dst_fst.name) + src = (dst_fst.parent / name).resolve() + if not src.is_file(): + die(f"{dst_fst.name}: {key} file not found: {src}") + dst = dst_fst.parent / (src.stem + suffix + src.suffix) + seen = swap_keys(src, dst, swaps, required=False, what=what, drop=drop) + if need and not need & seen: + die(f"{src.name}: none of {sorted(need)} found -- cannot make the {what} deck") + return text.replace(f'"{name}"', f'"{dst.name}"') + + +def prepare_lin_deck(src: Path, dst: Path, t_lin: float, outfmt: str, md_tmaxic: float) -> None: """Copy an .fst deck, enabling one linearization snapshot at t_lin, and derive - a linearization-compatible HydroDyn twin (no DFT excitation, no convolution - radiation, no Newman 2nd-order -- HydroDyn refuses to linearize those).""" + linearization-compatible HydroDyn/SeaState twins (and, for MoorDyn, a settled + initial-condition deck).""" + if not src.is_file(): + die(f"reference deck not found: {src}") swap_keys(src, dst, { - "TMax": f"{t_lin:.1f}", + "TMax": f"{max(t_lin, 0.1):.6f}", + "OutFmt": f'"{outfmt}"', "Linearize": "True", "CalcSteady": "False", "NLinTimes": "1", @@ -94,20 +159,17 @@ def prepare_lin_deck(src: Path, dst: Path, t_lin: float) -> None: }, required=True, what="linearization") text = dst.read_text() - m = re.search(r'"([^"]+)"\s+HydroFile', text) - if not m: - die(f"{dst.name}: HydroFile entry not found") - hydro_src = (dst.parent / m.group(1)).resolve() - hydro_dst = dst.parent / (hydro_src.stem + "_lin" + hydro_src.suffix) - seen = swap_keys(hydro_src, hydro_dst, { - "ExctnMod": "0", - "RdtnMod": "0", - "NewmanApp": "0", - }, required=False, what="HydroDyn linearization") - if not {"ExctnMod", "RdtnMod"} & seen: - die(f"{hydro_src.name}: neither ExctnMod nor RdtnMod found -- cannot make " - f"the HydroDyn deck linearization-compatible") - dst.write_text(text.replace(m.group(1), hydro_dst.name)) + text = derive(dst, text, "HydroFile", "_lin", + {"ExctnMod": "0", "RdtnMod": "0", "NewmanApp": "0"}, + {"ExctnMod", "RdtnMod"}, "HydroDyn linearization") + if re.search(r'^\s*1\s+CompSeaSt\b', text, re.M): + text = derive(dst, text, "SeaStFile", "_lin", {"WaveMod": "0"}, {"WaveMod"}, + "SeaState still-water") + if re.search(r'^\s*3\s+CompMooring\b', text, re.M): + text = derive(dst, text, "MooringFile", "_lin", + {"TmaxIC": f"{md_tmaxic:.1f}", "threshIC": "1.0e-7"}, {"TmaxIC"}, + "MoorDyn settled-IC", drop=re.compile(r"^\s*SeaState\s+WaterKin\b", re.I)) + dst.write_text(text) def run_case(exe: Path, fst: Path) -> None: @@ -123,8 +185,30 @@ def run_case(exe: Path, fst: Path) -> None: die(f"openfast failed on {fst.name} (rc={proc.returncode}):\n{tail}") +def coupled_points(fst: Path) -> np.ndarray: + """Coordinates of the Coupled/Vessel points of the mooring deck named by the .fst, + in deck order (the order of the coupled-mesh nodes in both modules).""" + text = fst.read_text() + deck = (fst.parent / file_entry(text, "MooringFile", fst.name)).resolve() + if not deck.is_file(): + die(f"{fst.name}: MooringFile not found: {deck}") + pts: list[list[float]] = [] + in_points = False + for ln in deck.read_text().splitlines(): + if ln.lstrip().startswith("-"): + in_points = "POINTS" in ln.upper() or "CONNECTION" in ln.upper() + continue + tok = ln.split() + if in_points and len(tok) >= 5 and tok[0].isdigit() and \ + tok[1].lower() in ("coupled", "vessel", "cpld", "ves"): + pts.append([float(v) for v in tok[2:5]]) + if not pts: + die(f"{deck.name}: no Coupled/Vessel points found in the POINTS section") + return np.array(pts) + + def parse_module_lin(path: Path) -> dict: - """Parse an OpenFAST module .lin file: sizes, labels, and A/B/C/D matrices.""" + """Parse an OpenFAST module .lin file: sizes, labels, operating points, A/B/C/D.""" if not path.is_file(): die(f"missing linearization file: {path}") text = path.read_text().splitlines() @@ -166,6 +250,7 @@ def read_labels(header: str, count: int) -> list[str]: u_labels = read_labels("Order of inputs", n_u) if n_u else [] y_labels = read_labels("Order of outputs", n_y) if n_y else [] + digits = [99] def read_matrix(name: str, rows: int, cols: int) -> np.ndarray: if rows == 0 or cols == 0: @@ -187,6 +272,9 @@ def read_matrix(name: str, rows: int, cols: int) -> np.ndarray: flat.extend(float(v) for v in toks) except ValueError: break + if len(flat) == len(toks): # first data row: mantissa digits + mant = toks[0].lstrip("+-").upper().split("E")[0].replace(".", "") + digits[0] = min(digits[0], len(mant)) if len(flat) != rows * cols: die(f"{path.name}: {name} yielded {len(flat)} values, expected {rows * cols}") mat = np.array(flat).reshape(rows, cols) @@ -196,15 +284,13 @@ def read_matrix(name: str, rows: int, cols: int) -> np.ndarray: die(f"{path.name}: matrix {name} ({rows}x{cols}) not found") raise AssertionError - return { - "n_x": n_x, - "u_labels": u_labels, - "y_labels": y_labels, + mats = { "A": read_matrix("A", n_x, n_x), "B": read_matrix("B", n_x, n_u), "C": read_matrix("C", n_y, n_x), "D": read_matrix("D", n_y, n_u), } + return {"n_x": n_x, "u_labels": u_labels, "y_labels": y_labels, "digits": digits[0], **mats} def match_indices(labels: list[str], must: list[str]) -> list[int]: @@ -212,56 +298,88 @@ def match_indices(labels: list[str], must: list[str]) -> list[int]: return idx +def op_values(labels: list[str], idx: list[int]) -> np.ndarray: + """Operating-point column (second token) of the selected label rows.""" + return np.array([float(labels[i].split()[1].rstrip(",")) for i in idx]) + + def static_impedance(mod: dict) -> np.ndarray: """K = D - C A^+ B (reduces to D when the module carries no states). - A^+ is the pseudo-inverse: a lumped-mass mooring on a FRICTIONLESS seabed has - neutral horizontal modes on its grounded nodes (zero restoring force), so A is - singular and a naive solve fills the block with junk (measured: asymmetric, - sign-flipped diagonals). The minimum-norm solution projects the neutral modes - out -- they stay put under a static boundary displacement, which is exactly the - quasi-static physics.""" + A^+ is the pseudo-inverse when A is numerically singular: a lumped-mass + mooring on a FRICTIONLESS seabed can have neutral horizontal modes on its + grounded nodes (zero restoring force). The minimum-norm solution projects the + neutral modes out -- they stay put under a static boundary displacement, which + is exactly the quasi-static physics. On the settled UMaine deck with full + precision A is regular (the seabed penalty and line tension restrain every + node) and the plain solve is used.""" if mod["n_x"] == 0: return mod["D"] A = mod["A"] sv = np.linalg.svd(A, compute_uv=False) cond = sv[0] / max(sv[-1], 1e-300) + print(f"MD state matrix: {A.shape[0]} states, cond ~ {cond:.2e}") if cond > 1e10: - print(f"note: state matrix cond ~ {cond:.2e} (neutral modes); using the " - f"pseudo-inverse zero-frequency reduction") + print("note: neutral modes; using the pseudo-inverse zero-frequency reduction") X = np.linalg.lstsq(A, mod["B"], rcond=1e-8)[0] else: X = np.linalg.solve(A, mod["B"]) return mod["D"] - mod["C"] @ X -def block(mod: dict, K: np.ndarray, row_must: list[str], col_must: list[str], what: str) -> np.ndarray: +def block(mod: dict, K: np.ndarray, row_must: list[str], col_must: list[str], what: str) -> tuple: rows = match_indices(mod["y_labels"], row_must) cols = match_indices(mod["u_labels"], col_must) if not rows or not cols: die(f"{what}: no labels matched rows={row_must} ({len(rows)}) cols={col_must} ({len(cols)}) " f"-- the module's coupled mesh did not enter the linearization set") - return K[np.ix_(rows, cols)] + return K[np.ix_(rows, cols)], rows + + +def skew(v: np.ndarray) -> np.ndarray: + return np.array([[0.0, -v[2], v[1]], [v[2], 0.0, -v[0]], [-v[1], v[0], 0.0]]) + + +def platform_stiffness(K: np.ndarray, r: np.ndarray, F0: np.ndarray) -> np.ndarray: + """6x6 stiffness -d[F; M]/d[u; theta] about the global origin for a rigid motion + of the fairleads (d_i = u + theta x r_i), from the nodal stiffness K = -dF/dx and + the operating-point fairlead loads F0 (load on the platform): the moment + linearisation adds the geometric term (theta x r_i) x F_i.""" + n = r.shape[0] + T = np.zeros((3 * n, 6)) + for i in range(n): + T[3 * i:3 * i + 3, 0:3] = np.eye(3) + T[3 * i:3 * i + 3, 3:6] = -skew(r[i]) + dF = -K @ T + L = np.zeros((6, 6)) + for i in range(n): + L[0:3] += dF[3 * i:3 * i + 3] + L[3:6] += skew(r[i]) @ dF[3 * i:3 * i + 3] + L[3:6, 3:6] += skew(F0[i]) @ skew(r[i]) + return -L def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--exe", type=Path, required=True) ap.add_argument("--case-dir", type=Path, required=True) - ap.add_argument("--ref-cd", default="Main_settle_cd5.fst") - ap.add_argument("--ref-md", default="Main_settle_md3.fst") - ap.add_argument("--t-lin", type=float, default=60.0) - # accepted for compatibility with the other A/B scripts; the pass criteria here are the - # invariants above, not a percentage - ap.add_argument("--gate-pct", type=float, default=5.0) + ap.add_argument("--ref-cd", default="IEA-15-UMaine_CompMooring5_CableDyn.fst") + ap.add_argument("--ref-md", default="IEA-15-UMaine_CompMooring3_MoorDyn.fst") + ap.add_argument("--t-lin", type=float, default=0.0) + ap.add_argument("--gate-pct", type=float, default=0.5, + help="CableDyn vs MoorDyn tolerance in %% (see the module docstring)") + ap.add_argument("--outfmt", default="ES24.15E3", + help="OutFmt written into both decks; the .lin matrices use it") + ap.add_argument("--md-tmaxic", type=float, default=400.0, + help="MoorDyn dynamic-relaxation time TmaxIC (s)") ap.add_argument("--skip-run", action="store_true") args = ap.parse_args() case = args.case_dir cd_fst = case / "Main_lin_cd5.fst" md_fst = case / "Main_lin_md3.fst" - prepare_lin_deck(case / args.ref_cd, cd_fst, args.t_lin) - prepare_lin_deck(case / args.ref_md, md_fst, args.t_lin) + prepare_lin_deck(case / args.ref_cd, cd_fst, args.t_lin, args.outfmt, args.md_tmaxic) + prepare_lin_deck(case / args.ref_md, md_fst, args.t_lin, args.outfmt, args.md_tmaxic) if not args.skip_run: for fst in (cd_fst, md_fst): @@ -276,12 +394,19 @@ def main() -> None: die("CableDyn module reported continuous states under linearization " "(the dYdu-only reduction must register Nx = 0)") + r_cd = coupled_points(cd_fst) + r_md = coupled_points(md_fst) + if r_cd.shape != r_md.shape or np.abs(r_cd - r_md).max() > 1e-6: + die(f"the two mooring decks have different Coupled/Vessel fairleads:\nCD {r_cd}\nMD {r_md}") + K_cd = static_impedance(cd) K_md = static_impedance(md) - # The gated block: CD's quasi-static stiffness invariants. - kb_cd = block(cd, K_cd, ["force"], ["translation displacement"], "CD stiffness") + # 1. CableDyn's quasi-static stiffness invariants. + kb_cd, rows_cd = block(cd, K_cd, ["force"], ["translation displacement"], "CD stiffness") n = kb_cd.shape[0] + if n != 3 * r_cd.shape[0] or kb_cd.shape[1] != n: + die(f"CD stiffness block {kb_cd.shape} does not match {r_cd.shape[0]} coupled fairleads") sym = np.linalg.norm(kb_cd - kb_cd.T) / max(np.linalg.norm(kb_cd), 1e-30) print(f"CD stiffness block {kb_cd.shape}: symmetry |K-K^T|/|K| = {sym:.2e} (gate 1e-3)") diag = np.diag(kb_cd) @@ -292,21 +417,75 @@ def main() -> None: fy = float(kb_cd[1::3, :] @ e @ np.ones(n // 3)) print(f"CD collective surge stiffness: dFx = {fx:.5g} N/m, net sway leak |dFy/dFx| = " f"{abs(fy) / max(abs(fx), 1e-30):.2e} (gate 1e-2 on this y-symmetric spread)") - ok = sym <= 1e-3 and bool(np.all(diag < 0.0)) and abs(fy) <= 1e-2 * abs(fx) - - # Reported, not gated: stock MoorDyn's zero-frequency reduction on the same - # block. Measured contact-noisy on catenary + seabed decks (asymmetric, - # y-symmetry-violating) -- printed as evidence, not used as a reference. - kb_md = block(md, K_md, ["force"], ["translation displacement"], "MD stiffness") + failures: list[str] = [] + if not (sym <= 1e-3 and bool(np.all(diag < 0.0)) and abs(fy) <= 1e-2 * abs(fx)): + failures.append("CD stiffness block violates the quasi-static invariants") + + # 2. The reference: MoorDyn's zero-frequency reduction on the same block. + kb_md, rows_md = block(md, K_md, ["force"], ["translation displacement"], "MD stiffness") + if kb_md.shape != kb_cd.shape: + die(f"MD stiffness block {kb_md.shape} differs from CD {kb_cd.shape}") sym_md = np.linalg.norm(kb_md - kb_md.T) / max(np.linalg.norm(kb_md), 1e-30) - fx_md = float(kb_md[0::3, :] @ e @ np.ones(n // 3)) - fy_md = float(kb_md[1::3, :] @ e @ np.ones(n // 3)) - print(f"MD (informational): collective surge dFx = {fx_md:.5g} N/m, asymmetry {sym_md:.2f}, " - f"sway leak |dFy/dFx| = {abs(fy_md) / max(abs(fx_md), 1e-30):.2f} -- a symmetric spread " - f"must have ~zero leak; MoorDyn's -lin FD is contact-noisy here and is not the reference") - - if not ok: - die("CD stiffness block violates the quasi-static invariants (see above)") + print(f"MD reference: .lin digits {md['digits']} (gate >= {MIN_DIGITS}), " + f"symmetry |K-K^T|/|K| = {sym_md:.2e} (gate 1e-3)") + if md["digits"] < MIN_DIGITS or sym_md > 1e-3: + failures.append(f"MoorDyn reference unusable (digits {md['digits']}, asymmetry {sym_md:.2e}): " + f"write the .lin files with a full-precision OutFmt (e.g. ES24.15E3) and " + f"settle the MoorDyn initial condition (TmaxIC)") + + # 3. CableDyn vs MoorDyn, entry by entry. + F0_cd = op_values(cd["y_labels"], rows_cd).reshape(-1, 3) + F0_md = op_values(md["y_labels"], rows_md).reshape(-1, 3) + dF0 = np.abs(F0_cd - F0_md).max() / np.abs(F0_md).max() + print(f"operating-point fairlead loads: max |CD-MD| / max|F| = {dF0:.2e} (gate 1e-2)") + if dF0 > 1e-2: + failures.append("the two operating points differ (fairlead loads)") + K = -kb_cd # stiffness = -dF/dx + Kr = -kb_md + tol = args.gate_pct / 100.0 + worst_dom, worst_all, worst_x = 0.0, 0.0, 0.0 + print("per-fairlead 3x3 blocks (K = -dF/dx, N/m), CD / MD / rel. diff:") + for p in range(n // 3): + s = slice(3 * p, 3 * p + 3) + b, br = K[s, s], Kr[s, s] + big = np.abs(br).max() + for i in range(3): + row = [] + for j in range(3): + rel = (b[i, j] - br[i, j]) / br[i, j] if abs(br[i, j]) > 1e-2 * big else 0.0 + if abs(br[i, j]) >= 0.1 * big: + worst_dom = max(worst_dom, abs(rel)) + if abs(br[i, j]) >= 1e-2 * big: + worst_all = max(worst_all, abs(rel)) + row.append(f"{b[i, j]:11.5g} / {br[i, j]:11.5g} / {rel:+.2e}") + print(f" fairlead {p + 1} {'xyz'[i]}: " + " | ".join(row)) + for q in range(n // 3): + if q != p: + t = slice(3 * q, 3 * q + 3) + worst_x = max(worst_x, float(np.abs(K[s, t]).max() + np.abs(Kr[s, t]).max()) / big) + print(f"fairlead blocks: dominant entries max rel. diff {worst_dom:.2e} (gate {tol:.1e}); " + f"all entries >= 1% of block max {worst_all:.2e} (gate {2 * tol:.1e}); " + f"cross-fairlead / block max {worst_x:.1e} (gate 1e-6)") + if worst_dom > tol or worst_all > 2 * tol or worst_x > 1e-6: + failures.append("per-fairlead stiffness differs from MoorDyn beyond the gate") + + K6 = platform_stiffness(K, r_cd, F0_cd) + K6r = platform_stiffness(Kr, r_md, F0_md) + dofs = ["Surge", "Sway", "Heave", "Roll", "Pitch", "Yaw"] + print("platform 6x6 stiffness about the origin, diagonal (CD / MD / rel. diff):") + rel_d = np.diag(K6) / np.diag(K6r) - 1.0 + for i in range(6): + print(f" {dofs[i]:5s} {K6[i, i]:13.6e} / {K6r[i, i]:13.6e} / {rel_d[i]:+.2e}") + norm = np.abs(K6 - K6r) / np.sqrt(np.abs(np.outer(np.diag(K6r), np.diag(K6r)))) + i, j = np.unravel_index(int(norm.argmax()), norm.shape) + print(f" surge-pitch {K6[0, 4]:.6e} / {K6r[0, 4]:.6e}, sway-roll {K6[1, 3]:.6e} / {K6r[1, 3]:.6e}") + print(f"6x6: max |diag rel. diff| {np.abs(rel_d).max():.2e} (gate {tol:.1e}); " + f"max |dK_ij|/sqrt(K_ii K_jj) {norm.max():.2e} at ({dofs[i]},{dofs[j]}) (gate {tol:.1e})") + if np.abs(rel_d).max() > tol or norm.max() > tol: + failures.append("platform 6x6 stiffness differs from MoorDyn beyond the gate") + + if failures: + die("; ".join(failures)) print("PASS")