Repository navigation
Expand file tree
/
Copy pathtest_deck_file.py
More file actions
4664 lines (4015 loc) · 186 KB
/
Copy pathtest_deck_file.py
File metadata and controls
4664 lines (4015 loc) · 186 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
"""Gates for loss-aware deck editing and case generation."""
from __future__ import annotations
import json
import math
import os
import re
from pathlib import Path
import pytest
from cabledyn import DeckFile, DeckFormatError, DeckWriter, generate_deck_cases
from cabledyn.deck_cli import main as deck_main
ROOT = Path(__file__).resolve().parents[2]
def _minimal_chain_source() -> str:
"""Return the stable mutation fixture derived from the public deck.
The maintained example intentionally lists every default. Parser unit tests
that inject one option need a minimal OPTIONS block so native last-row-wins
semantics do not make an unrelated documented default override the fixture.
"""
text = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
remove = {
"cbot",
"rhoinf",
"modified_newton",
"adaptive_mesh",
"cable_load_feedback",
"dynamic_solver",
"frictionmu",
"current",
"waves",
"waterkin",
}
result: list[str] = []
in_options = False
for line in text.splitlines(keepends=True):
if "OPTIONS" in line and line.lstrip().startswith("-"):
in_options = True
elif "OUTPUTS" in line and line.lstrip().startswith("-"):
in_options = False
if in_options:
visible = line.split(" - ", 1)[0].split()
keyword = ""
if visible:
keyword = (
visible[0]
if visible[0].lower() == "dynamic_solver"
else (visible[1] if len(visible) > 1 else "")
)
if keyword.lower() in remove or line.lstrip().startswith("-- Complete option"):
continue
line = re.sub(r" \[default:[^\]]*\](?=\r?\n?$)", "", line)
result.append(line)
return "".join(result)
def _end_connection_source() -> str:
source = (ROOT / "examples/lozon_gomex80_power_cable.dat").read_text(encoding="utf-8")
section = (
"--------------------- END CONNECTIONS ---------------------------------------\n"
"LineID End Stiffness EzX EzY EzZ\n"
"(-) (-) (N-m/rad) (-) (-) (-)\n"
"1 EndA 2.0e4 -0.25 0.0 -0.97\n"
)
return source.replace(
"--------------------- OPTIONS -----------------------------------------------\n",
section + "--------------------- OPTIONS -----------------------------------------------\n",
1,
)
@pytest.mark.parametrize(
("relative", "caller_driven"),
[
("examples/chain_catenary_r3_100m.dat", False),
("examples/syrope_polyester_mooring.dat", False),
("examples/iea15mw_umaine_mixed_cabledyn.dat", True),
],
)
def test_production_decks_parse_and_round_trip_without_edits(relative, caller_driven):
path = ROOT / relative
deck = DeckFile.read(path, caller_driven=caller_driven)
assert deck.text() == path.read_text(encoding="utf-8")
assert deck.line_types and deck.points and deck.lines and deck.sections
def test_end_connections_round_trip_edit_and_cli_inventory(tmp_path, capsys):
path = tmp_path / "end-connection.dat"
path.write_text(_end_connection_source(), encoding="utf-8")
deck = DeckFile.read(path)
assert len(deck.end_connections) == 1
assert deck.end_connections[0].tokens == (
"1",
"EndA",
"2.0e4",
"-0.25",
"0.0",
"-0.97",
)
deck.apply_many(
{
"end_connection.1.A.stiffness": "Rigid",
"end_connection.1.EndA.ezx": -1.0,
"end_connection.1.end_a.ezy": 0.0,
"end_connection.1.a.ezz": 0.0,
}
)
target = deck.write(tmp_path / "edited-end-connection.dat")
edited = DeckFile.read(target)
assert edited.end_connections[0].tokens == ("1", "EndA", "Rigid", "-1", "0", "0")
assert deck_main(["show", str(target)]) == 0
assert "end_connections: 1" in capsys.readouterr().out
@pytest.mark.parametrize(
("row", "message"),
[
("1 C 2.0e4 -0.25 0.0 -0.97", "End must be A or B"),
("1 A -1 -0.25 0.0 -0.97", "stiffness must be finite and non-negative, Pinned, or Rigid"),
("1 A nan -0.25 0.0 -0.97", "stiffness must be finite"),
("1 A 2.0e4 0 0 0", "direction must be non-zero"),
("9 A 2.0e4 -0.25 0.0 -0.97", "references an undefined line"),
],
)
def test_end_connection_invalid_rows_fail_python_preflight(tmp_path, row, message):
source = _end_connection_source().replace(
"1 EndA 2.0e4 -0.25 0.0 -0.97",
row,
)
path = tmp_path / "invalid-end-connection.dat"
path.write_text(source, encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
def test_end_connection_duplicate_and_unsupported_topology_fail_preflight(tmp_path):
source = _end_connection_source().replace(
"1 EndA 2.0e4 -0.25 0.0 -0.97\n",
"1 EndA 2.0e4 -0.25 0.0 -0.97\n1 a Rigid -1 0 0\n",
)
duplicate = tmp_path / "duplicate-end-connection.dat"
duplicate.write_text(source, encoding="utf-8")
with pytest.raises(DeckFormatError, match="duplicate END CONNECTIONS"):
DeckFile.read(duplicate)
moving_source = _end_connection_source().replace(
"2 Fixed 125.0 0.0 -80.0",
"2 Connect 125.0 0.0 -80.0",
)
moving = tmp_path / "moving-end-connection.dat"
moving.write_text(moving_source, encoding="utf-8")
with pytest.raises(DeckFormatError, match="Fixed End B"):
DeckFile.read(moving)
def test_deck_writer_authors_normalized_end_connections(tmp_path):
writer = DeckWriter(title="finite-EI cable with a rigid hang-off")
writer.add_line_type(
"cable",
diam=0.2,
mass=250.0,
ea=8.0e8,
ei=2.0e5,
cdn=1.0,
cdt=0.1,
can=1.0,
cat=0.0,
)
writer.add_point(1, "Coupled", 0.0, 0.0, -10.0)
writer.add_point(2, "Fixed", 100.0, 0.0, -80.0)
writer.add_line(1, node_a=1, node_b=2)
writer.add_section(line_id=1, line_type="cable", length=130.0, num_segs=40)
writer.add_end_connection(1, "EndA", "Infinity", (0.0, 0.0, -2.0))
writer.set_option(0.05, "dtM")
writer.set_option(0.0, "TMax")
path = writer.write(tmp_path / "writer-end-connection.dat")
parsed = DeckFile.read(path)
assert parsed.end_connections[0].tokens == ("1", "A", "Rigid", "0", "0", "-1")
with pytest.raises(ValueError, match="already has a connection"):
writer.add_end_connection(1, "A", 1.0e4, (1.0, 0.0, 0.0))
with pytest.raises(ValueError, match="non-zero"):
DeckWriter().add_end_connection(1, "A", "Pinned", (0.0, 0.0, 0.0))
with pytest.raises(ValueError, match="exactly three"):
DeckWriter().add_end_connection(1, "A", "Pinned", None) # type: ignore[arg-type]
def test_editors_change_only_selected_rows_and_preserve_comments(tmp_path):
source = ROOT / "examples/chain_catenary_r3_100m.dat"
deck = DeckFile.read(source)
original_lines = deck.text().splitlines()
deck.set_option("WtrDpth", 120.0)
deck.set_point(2, z=-5.0)
deck.set_line_type("chainR3", ea=1.7e9)
deck.set_section(1, length=560.0, numsegs=56)
target = deck.write(tmp_path / "edited.dat")
edited = target.read_text(encoding="utf-8")
assert "120 WtrDpth - Water depth (m)" in edited
assert "2 Coupled 0.0 0.0 -5" in edited
assert float(DeckFile.read(target).line_types[0].tokens[3]) == 1.7e9
assert "1 chainR3 560 56" in edited
changed = sum(a != b for a, b in zip(original_lines, edited.splitlines(), strict=True))
assert changed == 4
assert "-- R3 studless chain" in edited
def test_crlf_round_trips_and_native_whitespace_tokens_fail_closed(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
crlf = source.replace("\n", "\r\n")
path = tmp_path / "windows.dat"
path.write_bytes(crlf.encode("utf-8"))
deck = DeckFile.read(path)
assert deck.text().encode("utf-8") == path.read_bytes()
with pytest.raises(ValueError, match="cannot contain quotes or line breaks"):
deck.set_line_type("chainR3", ea='SYROPE:data/my"settings.dat|1.0|2.0')
# The native static route has no Syrope model, so the edit is rejected and
# rolled back on this static deck.
with pytest.raises(DeckFormatError, match="Syrope line has no static output"):
deck.set_line_type(
"chainR3",
ea="SYROPE:data/my_settings.dat|1.0|2.0",
ba="1.0|1.0",
)
assert deck.text().encode("utf-8") == path.read_bytes()
deck.set_line_type("chainR3", ea="2.5e9|3.0e9")
target = deck.write(tmp_path / "edited.dat")
written = target.read_bytes()
assert written.count(b"\r\n") == crlf.count("\r\n")
assert b"2.5e9|3.0e9" in written
def test_syrope_tokens_and_positional_wave_option_are_preserved(tmp_path):
source = ROOT / "examples/syrope_polyester_mooring.dat"
deck = DeckFile.read(source)
assert deck.line_types[0].tokens[3].startswith("SYROPE:")
deck.set_option("dtM", 0.025)
target = deck.write(tmp_path / "syrope.dat")
assert "SYROPE:data/syrope/syrope_settings.dat" in target.read_text(encoding="utf-8")
deck.set_line_type("rope", cdt=0.2)
assert '"SYROPE:data/syrope/syrope_settings.dat|1.53e8|23.12"' in deck.text()
wave_source = ROOT / "examples/dynamic_chain_waves.dat"
waves = DeckFile.read(wave_source)
waves.set_option("waves", ("airy", 3.0, 9.0, 15.0))
assert waves.option("waves").values == ("airy", "3", "9", "15")
@pytest.mark.parametrize(
("old", "new", "message"),
[
("|1.53e8|23.12", "|0|23.12", "alpha and beta must be positive"),
("|1.53e8|23.12", "|1.53e8|-1", "alpha and beta must be positive"),
("5.0e10|1.0e5", "0|0", "non-negative with a positive sum"),
("5.0e10|1.0e5", "-1|1", "non-negative with a positive sum"),
],
)
def test_syrope_constitutive_ranges_match_native_parser(tmp_path, old, new, message):
source = (ROOT / "examples/syrope_polyester_mooring.dat").read_text(encoding="utf-8")
path = tmp_path / "invalid-syrope-constants.dat"
path.write_text(source.replace(old, new), encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
def test_native_section_aliases_and_stock_hydro_columns_are_not_mislabelled(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace("LINE TYPES", "LINE DICTIONARY", 1)
source = source.replace("OPTIONS", "SOLVER OPTIONS", 1)
source = source.replace("OUTPUTS", "OUTPUT", 1)
source = source.replace("Cd_n Cd_t Ca_n Ca_t", "Cd_n Ca_n CdAx CaAx")
path = tmp_path / "aliases.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("WtrDpth").values == ("100.0",)
deck.set_line_type("chainR3", cdax=0.25)
assert deck.line_types[0].tokens[8] == "0.25"
with pytest.raises(KeyError, match="unknown field"):
deck.set_line_type("chainR3", cdt=0.25)
def test_hydro_column_order_tracks_each_line_type_header(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"chainR3 0.2466 373.5 1.607e9 -1.0 0.0 1.37 0.64 1.0 0.0",
"chainR3 0.2466 373.5 1.607e9 -1.0 0.0 1.37 0.64 1.0 0.0\n"
"Name Diam Mass EA BA EI Cd Ca CdAx CaAx\n"
"wire 0.1 10 1e8 0 0 1.2 1.0 0.2 0.0",
)
path = tmp_path / "mixed-hydro-headers.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
deck.set_line_type("chainR3", cdt=0.7)
deck.set_line_type("wire", cdax=0.5)
assert float(deck.line_types[0].tokens[7]) == pytest.approx(0.7)
assert float(deck.line_types[1].tokens[7]) == pytest.approx(1.0)
assert float(deck.line_types[1].tokens[8]) == pytest.approx(0.5)
def test_descriptive_table_header_with_apostrophe_is_skipped_before_tokenization(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"TypeName Diam",
"CableDyn's descriptive coefficient header\nTypeName Diam",
1,
)
path = tmp_path / "apostrophe-header.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.line_types[0].tokens[0] == "chainR3"
deck.set_line_type("chainR3", cdt=0.75)
assert deck.line_types[0].tokens[7] == "0.75"
def test_finite_ei_hydro_edit_does_not_overwrite_structural_columns(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"TypeName Diam MassDenInAir EA BA/-zeta EI Cd_n Cd_t Ca_n Ca_t",
"Name Diam Mass EA BA EI GAs GJ Irt Irn Cdn Cdt Can Cat",
).replace(
"chainR3 0.2466 373.5 1.607e9 -1.0 0.0 1.37 0.64 1.0 0.0",
"chainR3 0.2466 373.5 1.607e9 -1.0 2e4 3e8 4e4 5.0 6.0 1.37 0.64 1.0 0.0",
)
path = tmp_path / "finite-ei.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path, caller_driven=True)
deck.set_line_type("chainR3", cdn=2.25)
tokens = deck.line_types[0].tokens
assert tokens[6:10] == ("3e8", "4e4", "5.0", "6.0")
assert tokens[10] == "2.25"
def test_hydro_column_sniffing_uses_last_visible_header_not_comments(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
cable_order = source.replace(
"TypeName Diam",
"# migration note: old order was Cd Ca CdAx CaAx\nTypeName Diam",
)
cable_path = tmp_path / "cable-order.dat"
cable_path.write_text(cable_order, encoding="utf-8")
cable = DeckFile.read(cable_path)
cable.set_line_type("chainR3", cdt=0.75)
assert cable.line_types[0].tokens[7] == "0.75"
stock_order = source.replace(
"TypeName Diam MassDenInAir EA BA/-zeta EI Cd_n Cd_t Ca_n Ca_t",
"! CableDyn Cdt note\nTypeName Diam Mass EA BA EI Cd Ca CdAx CaAx",
)
stock_path = tmp_path / "stock-order.dat"
stock_path.write_text(stock_order, encoding="utf-8")
stock = DeckFile.read(stock_path)
stock.set_line_type("chainR3", cdax=0.25)
assert stock.line_types[0].tokens[8] == "0.25"
def test_three_column_and_stock_seven_column_lines_match_native_contract(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
three = source.replace("1 2 1 -", "1 2 1")
three_path = tmp_path / "three.dat"
three_path.write_text(three, encoding="utf-8")
assert len(DeckFile.read(three_path).lines[0].tokens) == 3
start = source.index("--------------------- LINES")
stop = source.index("--------------------- OPTIONS")
stock_block = (
"--------------------- LINES --------------------------------------------\n"
"ID LineType AttachA AttachB UnstrLen NumSegs Outputs\n"
"(-) (-) (-) (-) (m) (-) (-)\n"
"1 chainR3 2 1 550.0 55 -\n"
)
stock = source[:start] + stock_block + source[stop:]
stock_path = tmp_path / "stock.dat"
stock_path.write_text(stock, encoding="utf-8")
parsed = DeckFile.read(stock_path)
assert not parsed.sections
assert len(parsed.lines[0].tokens) == 7
parsed.set_section(1, length=600.0, numsegs=60)
assert parsed.lines[0].tokens[4:6] == ("600", "60")
before = parsed.text()
with pytest.raises(DeckFormatError, match="LINE Outputs accepts only"):
parsed.set_section(1, outputs="x")
assert parsed.text() == before
invalid_explicit = tmp_path / "invalid-explicit-output.dat"
invalid_explicit.write_text(
source.replace("1 2 1 -", "1 2 1 x"),
encoding="utf-8",
)
with pytest.raises(DeckFormatError, match="LINE Outputs accepts only"):
DeckFile.read(invalid_explicit)
valid_flags = tmp_path / "valid-output-flags.dat"
valid_flags.write_text(
source.replace("1 2 1 -", "1 2 1 PtT"),
encoding="utf-8",
)
DeckFile.read(valid_flags)
def test_fortran_d_exponent_lengths_match_native_parser(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
explicit_path = tmp_path / "explicit-d.dat"
explicit_path.write_text(
source.replace("1 chainR3 550.0 55", "1 chainR3 5.5D+2 55"),
encoding="utf-8",
)
assert DeckFile.read(explicit_path).sections[0].tokens[2] == "5.5D+2"
start = source.index("--------------------- LINES")
stop = source.index("--------------------- OPTIONS")
stock_block = (
"--------------------- LINES --------------------------------------------\n"
"ID LineType AttachA AttachB UnstrLen NumSegs Outputs\n"
"(-) (-) (-) (-) (m) (-) (-)\n"
"1 chainR3 2 1 5.5d+2 55 -\n"
)
stock_path = tmp_path / "stock-d.dat"
stock_path.write_text(source[:start] + stock_block + source[stop:], encoding="utf-8")
assert DeckFile.read(stock_path).lines[0].tokens[4] == "5.5d+2"
def test_numsegs_native_ceiling_applies_to_explicit_and_stock_rows(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
explicit_path = tmp_path / "explicit-too-many.dat"
explicit_path.write_text(
source.replace("1 chainR3 550.0 55", "1 chainR3 550.0 1000001"),
encoding="utf-8",
)
with pytest.raises(DeckFormatError, match="between 1 and 1000000"):
DeckFile.read(explicit_path)
start = source.index("--------------------- LINES")
stop = source.index("--------------------- OPTIONS")
stock_block = (
"--------------------- LINES --------------------------------------------\n"
"ID LineType AttachA AttachB UnstrLen NumSegs Outputs\n"
"(-) (-) (-) (-) (m) (-) (-)\n"
"1 chainR3 2 1 550.0 1000001 -\n"
)
stock_path = tmp_path / "stock-too-many.dat"
stock_path.write_text(source[:start] + stock_block + source[stop:], encoding="utf-8")
with pytest.raises(DeckFormatError, match="between 1 and 1000000"):
DeckFile.read(stock_path)
deck = DeckFile.read(ROOT / "examples/chain_catenary_r3_100m.dat")
before = deck.text()
with pytest.raises(DeckFormatError, match="between 1 and 1000000"):
deck.set_section(1, numsegs=1_000_001)
assert deck.text() == before
def test_nonstandard_table_headers_and_singular_wave_alias_are_native_compatible(tmp_path):
source = (ROOT / "examples/dynamic_chain_waves.dat").read_text(encoding="utf-8")
source = source.replace("ID NodeA NodeB Outputs", "LineID NodeA NodeB Outputs")
source = source.replace("airy 2.0 8.0 0.0 waves", "airy 2.0 8.0 0.0 wave")
path = tmp_path / "native-aliases.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("wave").values == ("airy", "2.0", "8.0", "0.0")
deck.set_option("wave", ("airy", 3.0, 9.0, 15.0))
assert deck.option("wave").values == ("airy", "3", "9", "15")
@pytest.mark.parametrize("comment", ["# coefficient columns", "! coefficient columns"])
def test_annotated_section_headers_match_native_scanning(tmp_path, comment):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"--------------------- LINE TYPES --------------------------------------",
"--------------------- LINE TYPES -------------------------------------- " + comment,
)
path = tmp_path / "annotated-header.dat"
path.write_bytes(source.encode("utf-8"))
deck = DeckFile.read(path)
assert deck.line_types
assert deck.text() == source
def test_section_scanner_rejects_unknown_and_accepts_native_optional_alias(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
optional = source.replace(
"--------------------- OPTIONS ------------------------------------------",
"--------------------- BODY ---------------------------------------------\n"
"--------------------- OPTIONS ------------------------------------------",
)
optional_path = tmp_path / "optional-body.dat"
optional_path.write_text(optional, encoding="utf-8")
assert DeckFile.read(optional_path).lines
unknown_path = tmp_path / "unknown-section.dat"
unknown_path.write_text(
source.replace(
"OPTIONS ------------------------------------------",
"OPTION -------------------------------------------",
),
encoding="utf-8",
)
with pytest.raises(DeckFormatError, match="unknown deck section 'OPTION'"):
DeckFile.read(unknown_path)
def test_open_ended_native_section_header_is_accepted(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"--------------------- LINE TYPES --------------------------------------",
"--- LINE TYPES",
)
path = tmp_path / "open-header.dat"
path.write_bytes(source.encode("utf-8"))
deck = DeckFile.read(path)
assert deck.line_types
assert deck.text() == source
@pytest.mark.parametrize(
("old", "new", "message"),
[
("100.0 WtrDpth", "orphan", "malformed OPTIONS row"),
('"FairTen1"', '"FairTen1' + "x" * 60, "longer than 64 characters"),
('"FairTen1"', "FairTen1;AnchTen1", "unsupported OUTPUT channel"),
],
)
def test_optional_sections_are_parsed_during_validation(tmp_path, old, new, message):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
path = tmp_path / "malformed-optional.dat"
path.write_text(source.replace(old, new), encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
@pytest.mark.parametrize("value", ["data#1.dat", "data!1.dat", "--disabled", "foo---bar.dat"])
def test_rendered_tokens_reject_native_comment_and_header_markers(tmp_path, value):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace("100.0 WtrDpth", "data/wk.dat WaterKin")
path = tmp_path / "waterkin.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
before = deck.text()
with pytest.raises(ValueError, match="comment/header markers"):
deck.set_option("WaterKin", value)
assert deck.text() == before
def test_direct_body_attachment_is_rejected(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
path = tmp_path / "body-attachment.dat"
path.write_text(source.replace("1 2 1 -", "1 Body1 1 -"), encoding="utf-8")
with pytest.raises(DeckFormatError, match="must be a point id"):
DeckFile.read(path)
def test_inline_comments_and_stock_outputs_end_follow_native_syntax(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace("1 2 1 -", "1 2 1 - -- fairlead connection")
source = source.replace(
'"AnchIncl1"',
'"AnchIncl1" # final channel\nEND\nThis "unterminated footer is ignored',
)
path = tmp_path / "comments.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.lines[0].tokens == ("1", "2", "1", "-")
assert deck.outputs == ("FairTen1", "AnchTen1", "FairIncl1", "AnchIncl1")
native = DeckFile.read(ROOT / "examples/chain_catenary_r3_100m.dat")
assert native.outputs == ("FairTen1", "AnchTen1", "FairIncl1", "AnchIncl1")
def test_windows_paths_retain_backslashes_during_parse_and_generation(tmp_path):
source = _minimal_chain_source()
source = source.replace("100.0 WtrDpth", r"C:\data\water.dat WaterKin")
path = tmp_path / "windows-path.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("WaterKin").values == (r"C:\data\water.dat",)
deck.set_option("WaterKin", r"D:\case_data\water.dat")
assert deck.option("WaterKin").values == (r"D:\case_data\water.dat",)
def test_repeated_options_follow_native_last_row_wins(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"100.0 WtrDpth",
"80.0 WtrDpth\n100.0 WtrDpth",
)
path = tmp_path / "repeated-option.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("WtrDpth").values == ("100.0",)
deck.set_option("WtrDpth", 120.0)
assert [row.values for row in deck.options if row.keyword.lower() == "wtrdpth"] == [
("80.0",),
("120",),
]
def test_scalar_option_prose_does_not_become_a_positional_option(tmp_path):
source = _minimal_chain_source()
source = source.replace(
"100.0 WtrDpth - Water depth (m)",
"100.0 WtrDpth\n0 WaveKin no current\n0.05 dtM before waves\n1.0 TMax",
)
path = tmp_path / "scalar-prose.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("WaveKin").values == ("0",)
assert deck.option("WaveKin").trailing == ("no", "current")
assert deck.option("dtM").values == ("0.05",)
assert deck.option("dtM").trailing == ("before", "waves")
def test_unknown_scalar_option_keyword_fails_closed(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace("100.0 WtrDpth", "100.0 WtrDpht")
path = tmp_path / "unknown-option.dat"
path.write_text(source, encoding="utf-8")
with pytest.raises(DeckFormatError, match=r"unknown OPTION keyword 'WtrDpht'"):
DeckFile.read(path)
@pytest.mark.parametrize(
"value, keyword, message",
[
("bad", "WtrDpth", "must be numeric"),
("NaN", "rhoW", "must be finite"),
("maybe", "modified_newton", "native boolean"),
("maybe", "cable_load_feedback", "native boolean"),
("maybe", "tensile_safety", "False, warn, or True"),
("-1e-6", "tensile_strain_tolerance", "must be >= 0"),
("3", "recovery_max_substeps", r"integer in \[4, 65536\]"),
("4.5", "recovery_max_substeps", r"integer in \[4, 65536\]"),
("NaN", "recovery_max_substeps", r"integer in \[4, 65536\]"),
("65537", "recovery_max_substeps", r"integer in \[4, 65536\]"),
("2.5", "axial_quadrature_order", r"integer in \[1, 6\]"),
("7", "bending_quadrature_order", r"integer in \[1, 6\]"),
("dynamic", "ICmode", "delete the ICmode row"),
("static", "ICmode", "always solves the static equilibrium directly"),
("relaxed", "bodyIC", "must be static or deck"),
("implicit", "bodyScheme", "monolithic or staggered"),
("fast", "bodySubstep", "none or accuracy"),
("x", "dtWave", "must be numeric"),
("2", "Currents", "supports only mode 0 or 1"),
("1.5", "Currents", "supports only mode 0 or 1"),
("4", "WaterKin", "numeric mode 0, 3 or 7"),
("1", "WaterKin", "numeric mode 0"),
("1e0", "WaterKin", "numeric mode 0"),
("-0.1", "frictionMu", "must be >= 0"),
("-1", "rampTime", "must be >= 0"),
("NaN", "maxStrain", "must be finite"),
("-0.1", "max_strain", "must be >= 0"),
("0", "WaveSeed", r"integer in \[1, 2147483646\]"),
("seven", "WaveSeed", r"integer in \[1, 2147483646\]"),
("2147483647", "WaveSeed", r"integer in \[1, 2147483646\]"),
("1.5", "wave_seed", "must be an integer"),
("maybe", "alpha_force_blend", "native boolean"),
("relaxed", "cable_statics", "continuation or sequenced"),
("nodal", "OutputRecovery", "equilibrium or element"),
("on", "ei0_substeps", "auto or off"),
("0", "ei0_substep_angle", r"\(0, 90\] deg"),
("91", "ei0SubstepAngle", r"\(0, 90\] deg"),
("NaN", "ei0_substep_angle", r"\(0, 90\] deg"),
("1", "ei0_substep_max", r"integer in \[2, 1024\]"),
("2.5", "ei0_substep_max", r"integer in \[2, 1024\]"),
("1025", "ei0SubstepMax", r"integer in \[2, 1024\]"),
("2048.5", "ei0_substep_max", r"integer in \[2, 1024\]"),
],
)
def test_invalid_scalar_option_values_fail_closed(tmp_path, value, keyword, message):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace("100.0 WtrDpth", f"{value} {keyword}")
path = tmp_path / "invalid-option-value.dat"
path.write_text(source, encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
def test_invalid_scalar_option_edit_is_transactional():
deck = DeckFile.read(ROOT / "examples/chain_catenary_r3_100m.dat")
before = deck.text()
with pytest.raises(DeckFormatError, match="must be numeric"):
deck.set_option("WtrDpth", "bad")
assert deck.text() == before
def test_zero_e_exponent_waterkin_mode_is_accepted(tmp_path):
source = _minimal_chain_source()
source = source.replace("100.0 WtrDpth", "0e0 WaterKin")
path = tmp_path / "zero-waterkin-mode.dat"
path.write_text(source, encoding="utf-8")
assert DeckFile.read(path).option("WaterKin").values == ("0e0",)
@pytest.mark.parametrize(
"row",
[
"static bodyIC",
"deck bodyIC",
"staggered bodyScheme",
"monolithic bodyScheme",
"none bodySubstep",
"accuracy bodySubstep",
"0.25 dtWave",
"60 TMax",
"motion.dat motionFile",
"RK4 tScheme",
"2.0 dtIC",
"60 TMaxIC",
"4 CdScaleIC",
"0.01 ThreshIC",
"1 WriteLog",
"0.1 dtOut",
"0 Currents",
"1 Currents",
"True tensile_safety",
"False tensileSafety",
"warn tensile_safety",
"True cable_load_feedback",
"False cableLoadFeedback",
"5e-6 tensile_strain_tolerance",
"1024 recovery_max_substeps",
"4 axial_quadrature_order",
"3 axialQuadratureOrder",
"2 axial_quadrature",
"4 bending_quadrature_order",
"3 bendingQuadratureOrder",
"2 bending_quadrature",
"20 rampTime",
"20 ramp_time",
"20 tRamp",
"0.25 maxStrain",
"0 max_strain",
"7 WaveSeed",
"2147483646 wave_seed",
"False alpha_force_blend",
"True alphaForceBlend",
"continuation cable_statics",
"sequenced cableStatics",
"equilibrium OutputRecovery",
"element output_recovery",
"auto ei0_substeps",
"off ei0Substeps",
"0.5 ei0_substep_angle",
"90 ei0SubstepAngle",
"2 ei0_substep_max",
"1024 ei0SubstepMax",
"1.9999999999999998 ei0_substep_max",
],
)
def test_native_scalar_option_vocabulary_is_accepted(tmp_path, row):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
if row == "60 TMax":
row = "0.1 dtM\n" + row
elif row.endswith("motionFile") or row == "1 Currents":
# a prescribed motion and a Currents 1 profile both need a dynamic deck
row = "0.1 dtM\n1.0 TMax\n" + row
source = source.replace(
"100.0 WtrDpth - Water depth (m)",
"100.0 WtrDpth - Water depth (m)\n" + row,
)
path = tmp_path / "supported-option.dat"
path.write_text(source, encoding="utf-8")
value, keyword = row.splitlines()[-1].split()
assert DeckFile.read(path).option(keyword).values == (value,)
@pytest.mark.parametrize(
"filename",
[
"lozon_gomex80_power_cable.dat",
"lozon_gomaine200_power_cable.dat",
"lozon_humboldt800_power_cable.dat",
"lozon_gomex80_power_cable_motion.dat",
],
)
def test_maintained_lozon_power_cable_decks_parse_and_round_trip(filename):
path = ROOT / "examples" / filename
deck = DeckFile.read(path)
assert deck.text() == path.read_text(encoding="utf-8")
assert deck.line_types and deck.points and deck.lines and deck.sections
@pytest.mark.parametrize("value", ["0", "none", "NONE"])
def test_disabled_motion_file_does_not_enable_prescribed_motion(tmp_path, value):
source = _minimal_chain_source().replace(
"100.0 WtrDpth - Water depth (m)",
f"100.0 WtrDpth\n{value} motionFile",
)
path = tmp_path / "disabled-motion.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("motionFile").values == (value,)
def test_motion_file_disable_token_obeys_last_row_wins(tmp_path):
source = _minimal_chain_source().replace(
"100.0 WtrDpth - Water depth (m)",
"100.0 WtrDpth\nmissing-motion.dat motionFile\nnone motionFile",
)
path = tmp_path / "disabled-motion-override.dat"
path.write_text(source, encoding="utf-8")
assert DeckFile.read(path).option("motionFile").values == ("none",)
def _vessel_source(rows: str) -> str:
return _minimal_chain_source().replace(
"100.0 WtrDpth - Water depth (m)",
"100.0 WtrDpth\n0.1 dtM\n1.0 TMax\n" + rows,
)
def test_vessel_motion_options_parse_and_rebase(tmp_path):
source = _vessel_source("vessel.dat vesselMotion\n80.0|0.0|-10.0 vesselRef")
path = tmp_path / "vessel.dat"
path.write_text(source, encoding="utf-8")
deck = DeckFile.read(path)
assert deck.option("vesselMotion").values == ("vessel.dat",)
assert deck.option("vesselRef").values == ("80.0|0.0|-10.0",)
rao = _vessel_source("airy 2.0 10.0 0.0 waves\nrao.dat vesselRAO")
(tmp_path / "rao-deck.dat").write_text(rao, encoding="utf-8")
assert DeckFile.read(tmp_path / "rao-deck.dat").option("vesselRAO").values == ("rao.dat",)
@pytest.mark.parametrize(
("rows", "message"),
[
("vessel.dat vesselMotion\nmotion.dat motionFile", "alternative prescribed-motion"),
("motion.dat motionFile\nrao.dat vesselRAO", "alternative prescribed-motion"),
("rao.dat vesselRAO", "vesselRAO needs deck waves"),
("stream 2.0 10.0 0.0 waves\nrao.dat vesselRAO", "stream-function wave"),
("vessel.dat vesselMotion\n1|2 vesselRef", "vesselRef must be x|y|z"),
("vessel.dat vesselMotion\n1|2|nan vesselRef", "vesselRef must be finite"),
],
)
def test_vessel_motion_options_fail_closed_like_the_native_reader(tmp_path, rows, message):
path = tmp_path / "vessel-bad.dat"
path.write_text(_vessel_source(rows), encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
_ATTACHMENT_DECK = """lazy-wave cable with buoyancy modules
--- LINE TYPES ---
Name Diam Mass EA BA EI Cdn Cdt Can Cat
(-) (m) (kg/m) (N) (-) (Nm2) (-) (-) (-) (-)
bare 0.16 36.70 4.69e8 0.0 1.99e4 1.2 0.1 1.0 0.0
chain 0.1 20.0 1.0e8 0.0 0.0 1.2 0.1 1.0 0.0
--- POINTS ---
ID Type X Y Z
(-) (-) (m) (m) (m)
1 Fixed 0.0 0.0 -56.0
2 Coupled 90.0 0.0 -14.0
3 Fixed 200.0 0.0 -56.0
--- LINES ---
ID NodeA NodeB Outputs
(-) (-) (-) (-)
1 2 1 -
2 2 3 -
--- SECTIONS ---
LineID LineType Length NumSegs
(-) (-) (m) (-)
1 bare 145.0 80
2 chain 120.0 20
--- ATTACHMENTS ---
LineID ArcLength Mass Volume CdA Ca
(-) (m) (kg) (m^3) (m^2) (-)
{rows}
--- OPTIONS ---
0.05 dtM
0.0 TMax
{options}
--- OUTPUTS ---
FairTen1
END
"""
def test_attachments_section_parses_series_and_aliases(tmp_path):
path = tmp_path / "attachments.dat"
rows = "1 42.5:5:87.5 114.15 0.2297 0.78 1.0\n1 20.0 500 0 0.5 0"
text = _ATTACHMENT_DECK.format(rows=rows, options="")
path.write_bytes(text.encode("utf-8"))
deck = DeckFile.read(path)
assert deck.text() == text
alias = tmp_path / "clumps.dat"
alias.write_bytes(text.replace("--- ATTACHMENTS ---", "--- CLUMPS ---").encode("utf-8"))
DeckFile.read(alias)
@pytest.mark.parametrize(
("row", "options", "message"),
[
("1 60.0 0 0 0 1.0", "", "positive Mass, Volume, CdA or CdAx"),
("1 60.0 100 0.1 0.1 1.0 0.2 7", "", "LineID ArcLength Mass Volume CdA Ca"),
("1 60.0 100 0.1 0.1 1.0 -0.2", "", "finite and >= 0"),
("1 60:0:70 100 0.1 0.1 1.0", "", "pitch > 0"),
("1 70:5:60 100 0.1 0.1 1.0", "", "first <= last"),
("1 60.0 100 0.1 0.1", "", "LineID ArcLength Mass Volume CdA Ca"),
("9 60.0 100 0.1 0.1 1.0", "", "undefined LINE"),
("2 60.0 100 0.1 0.1 1.0", "", "not a finite-EI line"),
("1 60.0 -1 0.1 0.1 1.0", "", "finite and >= 0"),
],
)
def test_attachments_fail_closed_like_the_native_reader(tmp_path, row, options, message):
path = tmp_path / "attachments-bad.dat"
path.write_text(_ATTACHMENT_DECK.format(rows=row, options=options), encoding="utf-8")
with pytest.raises(DeckFormatError, match=message):
DeckFile.read(path)
def test_attachments_accept_an_axial_drag_area_and_a_deck_current(tmp_path):
text = _ATTACHMENT_DECK.format(
rows="1 42.5:5:87.5 114.15 0.2297 0.78 1.0 0.2042", options="uniform 0.5 0 0 current"
)
path = tmp_path / "attachments-current.dat"
# all finite-EI lines: the deck driver takes a deck current
path.write_text(text.replace("1.0e8 0.0 0.0 1.2", "1.0e8 0.0 1.0e3 1.2"), encoding="utf-8")
DeckFile.read(path)
# a mixed EI=0/finite-EI deck runs on the aggregate in still water
with pytest.raises(DeckFormatError, match="runs in still water"):
DeckFile.from_text(text)
def test_viv_lift_flag_is_upper_case_and_standalone():
# V writes the VIV lift file; as MoorDyn's line flags it is case-sensitive (v, MoorDyn's
# node velocities, is not written)
deck = DeckFile.from_text(_edit(_DYNAMIC, ("1 2 1 -\n", "1 2 1 pV\n")))
assert deck.lines[0].tokens[3] == "pV"
with pytest.raises(DeckFormatError, match="accepts only '-', 'p', 't', 'r', and 'V'"):
DeckFile.from_text(_edit(_DYNAMIC, ("1 2 1 -\n", "1 2 1 pv\n")))
# a mixed EI=0/finite-EI deck writes no per-line files
mixed = _ATTACHMENT_DECK.format(rows="1 60.0 100 0.1 0.1 1.0", options="")
with pytest.raises(DeckFormatError, match="flags p, t and V"):
DeckFile.from_text(mixed.replace("1 2 1 -\n", "1 2 1 V\n"))
def test_attachments_are_accepted_on_the_coupled_route(tmp_path):
path = tmp_path / "attachments-coupled.dat"
path.write_text(
_ATTACHMENT_DECK.format(rows="1 60.0 100 0.1 0.1 1.0", options=""), encoding="utf-8"
)
DeckFile.read(path, caller_driven=True)
def test_vessel_motion_disable_row_clears_only_its_own_source(tmp_path):
path = tmp_path / "vessel-toggle.dat"
path.write_text(
_vessel_source("motion.dat motionFile\nnone motionFile\nvessel.dat vesselMotion"),
encoding="utf-8",
)
assert DeckFile.read(path).option("vesselMotion").values == ("vessel.dat",)
path.write_text(
_vessel_source("vessel.dat vesselMotion\nnone motionFile\nmotion.dat motionFile"),
encoding="utf-8",
)
with pytest.raises(DeckFormatError, match="alternative prescribed-motion"):
DeckFile.read(path)
def test_new_scalar_options_round_trip_through_edits(tmp_path):
source = (ROOT / "examples/chain_catenary_r3_100m.dat").read_text(encoding="utf-8")
source = source.replace(
"100.0 WtrDpth - Water depth (m)",
"100.0 WtrDpth - Water depth (m)\n"
"static bodyIC\n0.5 maxStrain - strain limit\n3 WaveSeed\n10 rampTime\n"
"True alpha_force_blend\ncontinuation cable_statics",
)
path = tmp_path / "new-options.dat"
path.write_bytes(source.encode("utf-8"))
deck = DeckFile.read(path)
assert deck.text() == source
deck.set_option("bodyIC", "deck")
deck.set_option("max_strain", 0.25)
deck.set_option("WaveSeed", 11)
deck.set_option("tRamp", 0)
deck.set_option("alphaForceBlend", "False")
deck.set_option("cable_statics", "sequenced")
edited = tmp_path / "new-options-edited.dat"
deck.write(edited)
reread = DeckFile.read(edited)
assert reread.option("bodyic").values == ("deck",)
assert reread.option("maxStrain").values == ("0.25",)