diff --git a/python/cabledyn/deck_file.py b/python/cabledyn/deck_file.py index c9e957f1..1df28e8a 100644 --- a/python/cabledyn/deck_file.py +++ b/python/cabledyn/deck_file.py @@ -1018,8 +1018,15 @@ def _positional_numbers_are_finite(tokens: list[str]) -> bool: return False -def _positional_range_error(tokens: list[str]) -> str | None: - """Return a native-finalizer range error for a parsed positional option.""" +def _positional_range_error(tokens: list[str], *, effective: bool = True) -> str | None: + """Return a native range error for a parsed positional option. + + The native reader checks some rules on every row as it reads it (positive wave + height and period, distinct profile depths, every spectral and wavetrain row) and + the magnitude bounds of a regular or JONSWAP ``waves`` row and of a ``current`` row + only on the row in effect once OPTIONS is read: the last one. ``effective=False`` + returns the per-row rules alone, for a row a later row may override. + """ mode = tokens[0].lower() if mode == "profile": # The native two-point parser accepts either input direction and @@ -1028,7 +1035,7 @@ def _positional_range_error(tokens: list[str]) -> str | None: if abs(_native_float(tokens[5]) - _native_float(tokens[1])) <= 100.0 * math.ulp(1.0): return "current profile depths must be strictly increasing" values = [_native_float(token) for token in tokens[1:9]] - if ( + if effective and ( abs(values[0]) > _NATIVE_MAX_COORDINATE or abs(values[4]) > _NATIVE_MAX_COORDINATE or any(abs(value) > _NATIVE_MAX_FLUID_SPEED for value in values[1:4] + values[5:8]) @@ -1038,7 +1045,9 @@ def _positional_range_error(tokens: list[str]) -> str | None: "in magnitude" ) elif mode == "uniform": - if any(abs(_native_float(token)) > _NATIVE_MAX_FLUID_SPEED for token in tokens[1:4]): + if effective and any( + abs(_native_float(token)) > _NATIVE_MAX_FLUID_SPEED for token in tokens[1:4] + ): return "OPTION current velocity components must be at most 1e3 m/s" elif ( mode in {"airy", "jonswap", "stream", "dean"} @@ -1049,6 +1058,8 @@ def _positional_range_error(tokens: list[str]) -> str | None: gamma = _native_float(tokens[3]) if mode == "jonswap" else 1.0 if height <= 0.0 or period <= 0.0 or gamma < 1.0: return "waves require positive height/period and JONSWAP gamma >= 1" + if not effective: + return None low, high = _NATIVE_WAVE_PERIOD_RANGE if height > _NATIVE_MAX_WAVE_HEIGHT or not low <= period <= high: return "waves need height <= 1e3 m and period in [0.1, 1e5] s" @@ -3255,7 +3266,8 @@ def _option_row(self, index: int) -> OptionRecord | None: raise DeckFormatError( f"{where}: positional option values must be finite plain numbers" ) - if error := _positional_range_error(tokens): + # the magnitude bounds apply to the row in effect only (``_validate``) + if error := _positional_range_error(tokens, effective=False): raise DeckFormatError(f"{where}: {error}") _raise_option_defect(where, tokens) return OptionRecord(index, tokens[-1], tuple(tokens[:-1]), description) @@ -3380,6 +3392,15 @@ def _validate(self) -> None: options = self.options channels = self.outputs option_by_key = {_option_key(option.keyword): option for option in options} + # native finalize_checks: the magnitude bounds of the waves and current rows in + # effect (the last of each); a row a later row overrides takes its row rules only + for overridden_kind in ("waves", "current"): + record = option_by_key.get(overridden_kind) + if record is None or record.keyword_first: + continue + tokens = [*record.values, record.keyword] + if _is_positional_option(tokens) and (error := _positional_range_error(tokens)): + raise DeckFormatError(f"{self._label}:{record.index + 1}: {error}") has_depth = "water_depth" in option_by_key has_bathymetry = "bathymetry" in option_by_key if has_depth and has_bathymetry: diff --git a/python/tests/test_deck_native_parity.py b/python/tests/test_deck_native_parity.py index df4b1368..7d1ee333 100644 --- a/python/tests/test_deck_native_parity.py +++ b/python/tests/test_deck_native_parity.py @@ -584,12 +584,41 @@ def _carries_waves(name: str) -> bool: if _carries_waves(name) for variant in ("spelled", "upper") ] +_PROFILE_ROW = "profile -30 0.2 0 0 0 0.5 0 0 current" +# (rows, message): the last waves or current row is the one in effect; the native reader +# checks the magnitude bounds on that row alone and the row rules on every row +_OVERRIDDEN_ROW_CASES = [ + (("airy 2000 8 0 waves", "none waves"), None), + (("airy 2 0.01 0 waves", "airy 2 8 0 waves"), None), + (("stream 2 2e5 0 waves", "pm 3 10 0 waves"), None), + (("jonswap 3 10 40 0 waves", "airy 2 8 0 waves"), None), + (("jonswap 3 10 2000 0 waves", "airy 2 8 0 waves"), None), + (("jonswap 3 10 2000 0 waves", "stream 2 8 0 waves"), None), + (("jonswap 3000 10 3.3 0 waves", "none waves"), None), + (("uniform 2000 0 0 current", "none current"), None), + (("uniform 2000 0 0 current", _PROFILE_ROW), None), + (("profile -2e6 0.2 0 0 0 0.5 0 0 current", "uniform 0.1 0 0 current"), None), + (("profile -30 2000 0 0 0 0.5 0 0 current", _PROFILE_ROW), None), + (("airy 2 8 0 waves", "airy 2000 8 0 waves"), r"waves need height <= 1e3 m"), + (("none waves", "jonswap 3 10 40 0 waves"), r"JONSWAP gamma must be in \[1, 32\.6\)"), + (("airy 2 8 0 waves", "jonswap 3 10 2000 0 waves"), r"JONSWAP gamma must be in \[1, 32\.6\)"), + ((_PROFILE_ROW, "uniform 2000 0 0 current"), r"at most 1e3 m/s"), + (("uniform 0.1 0 0 current", "profile -2e6 0.2 0 0 0 0.5 0 0 current"), r"at most 1e6 m"), + # the row rules hold on an overridden row too + (("airy 0 8 0 waves", "none waves"), r"positive height/period"), + (("jonswap 3 10 0.5 0 waves", "airy 2 8 0 waves"), r"JONSWAP gamma >= 1"), + (("profile -30 0.2 0 0 -30 0.5 0 0 current", "none current"), r"strictly increasing"), + (("pm 3000 10 0 waves", "none waves"), r"a wave train needs height <= 1e3 m"), +] + + _ALL_FILE_CASES = [ *[("held", rows, files) for rows, files in _WAVE_RECORD_CASES], *_ANCILLARY_CASES, *_SPELLED_CASES, *_STATIC_CASES, *_SHIPPED_CASES, + *[("held", rows, {}) for rows, _ in _OVERRIDDEN_ROW_CASES], ] @@ -827,6 +856,21 @@ def test_jonswap_gamma_limit_applies_on_every_path(rows): _rejects(_options(_HELD, *rows), r"JONSWAP gamma must be in \[1, 32\.6\)") +@pytest.mark.parametrize(("rows", "message"), _OVERRIDDEN_ROW_CASES) +def test_magnitude_bounds_apply_to_the_waves_and_current_rows_in_effect(rows, message): + text = _options(_HELD, *rows) + if message is None: + DeckFile.from_text(text) + else: + _rejects(text, message) + + +def test_an_edited_waves_row_takes_the_magnitude_bounds(): + deck = DeckFile.from_text(_options(_HELD, "airy 2000 8 0 waves", "airy 2 8 0 waves")) + with pytest.raises(ValueError, match="waves need height <= 1e3 m"): + deck.set_option("waves", ("airy", 2000.0, 8.0, 0.0)) + + def test_spread_jonswap_row_is_validated_as_a_wave_train(): DeckFile.from_text(_options(_HELD, "jonswap 2 8 3.3 2e6 waves")) _rejects( diff --git a/src/CableDyn_DeckDriver.f90 b/src/CableDyn_DeckDriver.f90 index 6aa49a46..33dea36d 100644 --- a/src/CableDyn_DeckDriver.f90 +++ b/src/CableDyn_DeckDriver.f90 @@ -17093,6 +17093,8 @@ SUBROUTINE apply_option(line, opts, ErrStat, ErrMsg) opts%current_profile_velocity(:, 1) = [x6, x7, x8] opts%current_profile_velocity(:, 2) = [x2, x3, x4] END IF + ! last row wins: an earlier uniform row leaves no velocity behind + opts%current_velocity = CD_ZERO opts%has_current = .TRUE. opts%has_current_profile = .TRUE. RETURN @@ -17148,6 +17150,8 @@ SUBROUTINE apply_option(line, opts, ErrStat, ErrMsg) opts%wave_height = x1 opts%wave_period = x2 opts%wave_direction = x3 + ! last row wins: a regular wave has no gamma, so an earlier JONSWAP row leaves none + opts%wave_gamma = CD_ONE opts%has_wave = .TRUE. opts%wave_kind = WAVE_AIRY IF (to_lower(TRIM(mode)) /= 'airy') opts%wave_kind = WAVE_STREAM diff --git a/tests/test_deck_driver.f90 b/tests/test_deck_driver.f90 index 78f0d38a..96adb521 100644 --- a/tests/test_deck_driver.f90 +++ b/tests/test_deck_driver.f90 @@ -2197,6 +2197,42 @@ SUBROUTINE check_parser_hardening() 'LINE 1 combines EI=0 and finite-EI sections') END IF END BLOCK + + ! The last waves or current row sets the sea: a row it overrides leaves nothing behind + ! that the bounds of the effective row are checked against (a JONSWAP gamma under an + ! Airy row, a uniform velocity under a profile row), while the effective row is checked. + BLOCK + CHARACTER(1) :: nl + CHARACTER(*), PARAMETER :: CLOCK = '0.1 dtM'//NEW_LINE('a')//'0.2 TMax' + CHARACTER(64), PARAMETER :: OVERRIDDEN(4) = [CHARACTER(64) :: & + 'jonswap 3.0 10.0 2000.0 0.0 waves', & + 'jonswap 3.0 10.0 2000.0 0.0 waves', & + 'uniform 2000.0 0.0 0.0 current', & + 'airy 2000.0 8.0 0.0 waves'] + CHARACTER(64), PARAMETER :: EFFECTIVE(4) = [CHARACTER(64) :: & + 'airy 2.0 8.0 0.0 waves', 'stream 2.0 8.0 0.0 waves', & + 'profile -30 0.2 0 0 0 0.5 0 0 current', 'none waves'] + INTEGER :: k + nl = NEW_LINE('a') + DO k = 1, SIZE(OVERRIDDEN) + CALL write_text_file('deck_hard_overridden.dat', hardening_deck(ANCHOR, SECTION, CLOCK//nl// & + TRIM(OVERRIDDEN(k))//nl//TRIM(EFFECTIVE(k)), & + 'FairTen1')) + CALL CD_Run_Deck_Driver('deck_hard_overridden.dat', 'deck_hard_overridden', conv_l, es_l, em_l) + CALL require(es_l == CD_DECKDRV_OK .AND. conv_l, '"'//TRIM(OVERRIDDEN(k))//'" overridden by "'// & + TRIM(EFFECTIVE(k))//'" leaves no stale bound: '//TRIM(em_l)) + END DO + CALL expect_badinput('deck_hard_effective_gamma.dat', hardening_deck(ANCHOR, SECTION, CLOCK//nl// & + 'airy 2.0 8.0 0.0 waves'//nl// & + 'jonswap 3.0 10.0 2000.0 0.0 waves', & + 'FairTen1'), & + 'an out-of-range JONSWAP gamma on the effective row', 'gamma <= 1e3') + CALL expect_badinput('deck_hard_effective_current.dat', hardening_deck(ANCHOR, SECTION, CLOCK//nl// & + 'profile -30 0.2 0 0 0 0.5 0 0 current'// & + nl//'uniform 2000.0 0.0 0.0 current', & + 'FairTen1'), & + 'an out-of-range uniform current on the effective row', 'at most 1e3 m/s') + END BLOCK END SUBROUTINE check_parser_hardening FUNCTION rigid6_failure_deck(failure_rows, extra_point) RESULT(body)