-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyaml_loader.jl
More file actions
1309 lines (1158 loc) · 50.4 KB
/
yaml_loader.jl
File metadata and controls
1309 lines (1158 loc) · 50.4 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
# Copyright (c) 2025 Bart van de Lint, Jelle Poland
# SPDX-License-Identifier: MPL-2.0
using YAML
using Logging
using LinearAlgebra
using VortexStepMethod, KiteUtils
# ------------------ helpers ------------------
"""
get_field_or_nothing(::Type{T}, row::NamedTuple,
field::Symbol) where T
Convert field to type T if present, otherwise return nothing.
# Examples
```julia
get_field_or_nothing(Int64, row, :idx) # -> Int64 or nothing
get_field_or_nothing(Tuple{Int64,Int64}, row, :pair)
# -> (Int64, Int64) or nothing
```
"""
function get_field_or_nothing(::Type{T}, row::NamedTuple,
field::Symbol) where T
if !haskey(row, field) || isnothing(row[field])
return nothing
end
return convert_to_type(T, row[field])
end
"""
convert_to_type(::Type{T}, value) where T
Convert value to type T. Handles special cases like Tuples.
"""
convert_to_type(::Type{T}, value) where T = T(value)
# Special handling for Tuple types
function convert_to_type(
::Type{Tuple{T,T}}, value) where T
vec = Vector{Int}(value)
return (T(vec[1]), T(vec[2]))
end
"""
resolve_references(row::NamedTuple, property_tables::Dict{String, Dict{String, NamedTuple}})
Resolve string references in a row by looking them up in property tables.
If a field value is a String, check if it exists as a key in any property table.
If found, merge those properties into the current row (current row takes precedence).
"""
function resolve_references(row::NamedTuple, property_tables::Dict{String, Dict{String, NamedTuple}})
resolved = Dict{Symbol, Any}(pairs(row))
for (field, value) in pairs(row)
# Check if this is a string reference (unquoted strings in YAML)
if value isa String
# Try to find this reference in any property table
for (table_name, lookup_dict) in property_tables
if haskey(lookup_dict, value)
# Found! Merge these properties (current row takes precedence)
ref_props = lookup_dict[value]
for (k, v) in pairs(ref_props)
# Skip 'name' — it identifies the referenced
# item, not a property to inherit.
k === :name && continue
if !haskey(resolved, k) || resolved[k] === nothing
resolved[k] = v
end
end
break
end
end
end
end
return NamedTuple(resolved)
end
"""
calculate_derived_properties!(props::Dict{Symbol, Any})
Calculate derived properties like unit_stiffness and unit_damping from material properties.
Modifies props in-place.
"""
function calculate_derived_properties!(props::Dict{Symbol, Any})
# Calculate unit_stiffness from material properties if missing or if it's a string (material name)
# Store EA; the spring k is computed later as EA / len in generate_system.jl.
if haskey(props, :youngs_modulus) && haskey(props, :diameter_mm) && haskey(props, :l0)
# Check if we need to calculate (missing, nothing, or is a string reference)
need_calculation = !haskey(props, :unit_stiffness) ||
props[:unit_stiffness] === nothing ||
props[:unit_stiffness] isa String
if need_calculation
d_m = Float64(props[:diameter_mm]) / 1000.0 # mm to m
A = π * (d_m / 2)^2
E = Float64(props[:youngs_modulus])
props[:unit_stiffness] = E * A
end
end
# Calculate unit_damping from damping coefficient if missing or is a string
if haskey(props, :damping_per_stiffness) && haskey(props, :unit_stiffness)
# Only calculate if unit_stiffness is now a number
if props[:unit_stiffness] isa Number
need_damping_calc = !haskey(props, :unit_damping) ||
props[:unit_damping] === nothing ||
props[:unit_damping] isa String
if need_damping_calc
props[:unit_damping] = Float64(props[:damping_per_stiffness]) * Float64(props[:unit_stiffness])
end
end
end
# Set default unit_damping if still missing
if !haskey(props, :unit_damping) || props[:unit_damping] === nothing || props[:unit_damping] isa String
props[:unit_damping] = 0.0
end
end
function parse_table(tbl)::Vector{NamedTuple}
haskey(tbl, "data") || throw(ArgumentError("table is missing `data`"))
rows = tbl["data"]
(isnothing(rows) || isempty(rows)) && return NamedTuple[]
# Check format: if first row is a Dict,
# use dict format; if Array, use header format
first_row = first(rows)
if first_row isa AbstractDict
# Dict format: each row is already a dict with named keys
# Convert each dict to a NamedTuple
out = NamedTuple[]
for row in rows
nt = NamedTuple{Tuple(Symbol.(keys(row)))}(
Tuple(values(row)))
push!(out, nt)
end
return out
else
# Array format: requires headers
haskey(tbl, "headers") ||
throw(ArgumentError(
"table with array rows requires `headers`"))
headers = String.(tbl["headers"])
out = NamedTuple[]
for (k, row) in enumerate(rows)
# skip empty or comment rows
if isempty(row) ||
(isa(row[1], String) && startswith(row[1], "#"))
continue
end
# allow missing trailing columns (fill with nothing)
if length(row) < length(headers)
row = vcat(row, fill(nothing,
length(headers) - length(row)))
end
if length(row) > length(headers)
@warn "Skipping row $k: has $(length(row)) " *
"values, expected $(length(headers))."
continue
end
nt = NamedTuple{Tuple(Symbol.(headers))}(Tuple(row))
push!(out, nt)
end
return out
end
end
"""
call_yaml_constructor(Constructor, row::NamedTuple,
args_spec, kwargs_spec; mappings=Dict())
Generic YAML-to-constructor caller. Extracts positional
args and kwargs from YAML row and calls constructor.
# Arguments
- `Constructor`: Constructor function to call
- `row::NamedTuple`: Parsed YAML row
- `args_spec::Vector{Symbol}`: Names for positional args
- `kwargs_spec::Vector{Symbol}`: Names for kwargs
# Keyword Arguments
- `mappings::Dict{Symbol, Function}`: Mapping functions
that take the row and return the arg value
# Example
```julia
row = (idx=1, x=0.0, y=0.0, z=0.0, type="STATIC")
point = call_yaml_constructor(Point, row,
[:idx, :pos_cad, :type], # positional args
[:extra_mass, :wing_idx]; # kwargs
mappings=Dict(
:pos_cad => r -> [Float64(r.x),
Float64(r.y), Float64(r.z)],
:type => r -> parse_dynamics_type(
String(r.type))
))
```
"""
function call_yaml_constructor(
Constructor,
row::NamedTuple,
args_spec::Vector{Symbol},
kwargs_spec::Vector;
mappings::Dict{Symbol, <:Function}=
Dict{Symbol, Function}())
# Extract positional arguments
args = []
for arg_name in args_spec
if haskey(mappings, arg_name)
push!(args, mappings[arg_name](row))
elseif haskey(row, arg_name)
push!(args, row[arg_name])
else
error("Missing required arg $arg_name")
end
end
# Extract keyword arguments (only if present)
kwargs = Dict{Symbol, Any}()
for kwarg_name in kwargs_spec
if haskey(mappings, kwarg_name)
kwargs[kwarg_name] = mappings[kwarg_name](row)
elseif haskey(row, kwarg_name)
val = row[kwarg_name]
# Skip nothing values (Julia nothing or YAML "nothing" string)
if !isnothing(val) && val != "nothing"
kwargs[kwarg_name] = val
end
end
end
return Constructor(args...; kwargs...)
end
"""
load_sys_struct_from_yaml(yaml_path::AbstractString; system_name="from_yaml", set=nothing)
Build a `SystemStructure` from a component-based structural YAML file.
**IMPORTANT**: All indices (points, segments, etc.) must be sequential
starting from 1 with no gaps.
# Expected top-level blocks
- `points`: table with headers `[id,x,y,z,type,mass,body_damping,world_damping]`
- `type`: STATIC, DYNAMIC, WING, or QUASI_STATIC
- `id` must be sequential: 1, 2, 3, ...
- `segments`: table with one of two formats:
- Direct format: `[id,point_i,point_j,type,l0,diameter_mm,unit_stiffness,unit_damping,compression_frac]`
- Named format: `[name,point_i,point_j]` (requires `segment_properties` block)
- `segment_properties`: (optional) table with headers `[name,type,l0,diameter_mm,unit_stiffness,unit_damping,compression_frac]`
- Used with named segment format for shared properties across symmetric segments
- `pulleys`: table with headers `[id,segment_i,segment_j,type]`
- `type`: DYNAMIC or QUASI_STATIC
- `groups`: (optional) table with headers `[id,point_idxs,gamma,type,reference_chord_frac]`
- `tethers`: (optional) table with headers `[id,segment_ids,ground_point_id]`
- `winches`: (optional) table with headers `[id,tether_ids]`
- `wings`: (optional, typically from VSM configuration)
- `transforms`: (optional, typically from settings)
"""
function load_sys_struct_from_yaml(yaml_path::AbstractString; system_name="from_yaml", set=nothing(), ignore_l0::Bool=false, wing_type::Union{Nothing,WingType}=nothing, aero_mode::Union{Nothing,AeroMode}=nothing, vsm_set=nothing)
data = YAML.load_file(yaml_path)
# Use provided settings or fall back to base settings
set === nothing && (set = load_settings("base"))
# Parse string types to enums
function parse_dynamics_type(s::String)
s_upper = uppercase(s)
s_upper == "STATIC" && return STATIC
s_upper == "DYNAMIC" && return DYNAMIC
s_upper == "WING" && return WING
s_upper == "QUASI_STATIC" && return QUASI_STATIC
error("Unknown DynamicsType: $s")
end
function parse_segment_type(s::String)
s_upper = uppercase(s)
s_upper == "POWER_LINE" && return POWER_LINE
s_upper == "STEERING_LINE" && return STEERING_LINE
s_upper == "BRIDLE" && return BRIDLE
error("Unknown SegmentType: $s")
end
# Note: Name resolution is now handled by SystemStructure.assign_indices_and_resolve!
# Helper to convert raw reference to proper type (Int or Symbol)
function to_ref(val)
# Handle Julia nothing
isnothing(val) && return nothing
if val isa Integer
return Int(val)
elseif val isa String
# Handle "nothing" string from YAML as Julia nothing
val == "nothing" && return nothing
return Symbol(val)
elseif val isa Symbol
return val
else
return Int(val)
end
end
# Load points - SystemStructure handles resolution
points = Point[]
if haskey(data, "points")
point_rows = parse_table(data["points"])
for (i, row) in enumerate(point_rows)
# Create Point using new constructor (name as first positional arg)
# Raw references are passed - SystemStructure will resolve them
point = call_yaml_constructor(Point, row,
[:name, :pos_cad, :type],
[:wing, :transform, :extra_mass,
:body_frame_damping, :world_frame_damping,
:area, :drag_coeff];
mappings=Dict(
:pos_cad => r -> KVec3(r.pos_cad...),
:type => r -> parse_dynamics_type(String(r.type)),
:name => r -> haskey(r, :name) && !isnothing(r.name) ? Symbol(r.name) : i,
# Pass raw references - constructor handles defaults
:wing => r -> haskey(r, :wing_idx) ? to_ref(r.wing_idx) : nothing,
:transform => r -> haskey(r, :transform_idx) ? to_ref(r.transform_idx) : nothing,
:body_frame_damping => r -> haskey(r, :body_frame_damping) ? r.body_frame_damping : nothing,
:world_frame_damping => r -> haskey(r, :world_frame_damping) ? r.world_frame_damping : nothing
))
point.pos_w .= point.pos_cad
point.vel_w .= 0.0
push!(points, point)
end
end
isempty(points) &&
error("No points found in YAML file $(yaml_path).")
# Build property tables for reference resolution
property_tables = Dict{String, Dict{String, NamedTuple}}()
# Load materials table
if haskey(data, "materials")
materials_dict = Dict{String, NamedTuple}()
material_rows = parse_table(data["materials"])
for row in material_rows
name = String(row.name)
materials_dict[name] = row
end
property_tables["materials"] = materials_dict
end
# Load elements table (old-style segment properties)
if haskey(data, "elements")
elements_dict = Dict{String, NamedTuple}()
element_rows = parse_table(data["elements"])
for row in element_rows
name = String(row.name)
elements_dict[name] = row
end
property_tables["elements"] = elements_dict
end
# Load segment_properties table (for backward compatibility)
if haskey(data, "segment_properties")
segment_props_dict = Dict{String, NamedTuple}()
prop_rows = parse_table(data["segment_properties"])
for row in prop_rows
name = String(row.name)
segment_props_dict[name] = row
end
property_tables["segment_properties"] = segment_props_dict
end
# Load segments - SystemStructure handles resolution
segments = Segment[]
if haskey(data, "segments")
segment_rows = parse_table(data["segments"])
for (i, row) in enumerate(segment_rows)
# Resolve material references and calculate derived properties
resolved_row = resolve_references(row, property_tables)
props = Dict{Symbol, Any}(pairs(resolved_row))
calculate_derived_properties!(props)
# Convert back to NamedTuple for constructor
resolved_row = NamedTuple(props)
# Create Segment using new constructor (name, set, point_i, point_j, type)
# Raw point references are passed - SystemStructure will resolve
segment = call_yaml_constructor(Segment, resolved_row,
[:name, :set, :point_i, :point_j, :type],
[:l0, :diameter_mm, :unit_stiffness,
:unit_damping, :compression_frac];
mappings=Dict(
:set => r -> set,
:point_i => r -> to_ref(r.point_i),
:point_j => r -> to_ref(r.point_j),
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end,
:type => r -> parse_segment_type(
String(r.type))
))
push!(segments, segment)
end
end
# Load pulleys - SystemStructure handles resolution
pulleys = Pulley[]
if haskey(data, "pulleys")
pulley_rows = parse_table(data["pulleys"])
for (i, row) in enumerate(pulley_rows)
# Create Pulley using new constructor (name, segment_i, segment_j, type)
pulley = call_yaml_constructor(Pulley, row,
[:name, :segment_i, :segment_j, :type],
[];
mappings=Dict(
:segment_i => r -> to_ref(r.segment_i),
:segment_j => r -> to_ref(r.segment_j),
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end,
:type => r -> parse_dynamics_type(String(r.type))
))
push!(pulleys, pulley)
end
end
# Load groups (optional, for deformable wings) - SystemStructure handles resolution
groups = Group[]
if haskey(data, "groups") &&
haskey(data["groups"], "data") &&
data["groups"]["data"] !== nothing &&
!isempty(data["groups"]["data"])
group_rows = parse_table(data["groups"])
for (i, row) in enumerate(group_rows)
# Create Group using new constructor (name, points, type, moment_frac)
group = call_yaml_constructor(Group, row,
[:name, :points, :type, :moment_frac],
[:damping];
mappings=Dict(
:points => r -> [to_ref(p) for p in r.point_idxs],
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end,
:type => r -> parse_dynamics_type(
String(r.type))
))
push!(groups, group)
end
end
# Load tethers (optional) - SystemStructure handles resolution
tethers = Tether[]
if haskey(data, "tethers") &&
haskey(data["tethers"], "data") &&
data["tethers"]["data"] !== nothing &&
!isempty(data["tethers"]["data"])
tether_rows = parse_table(data["tethers"])
for (i, row) in enumerate(tether_rows)
# Create Tether using new constructor (name, segments; winch_point)
# Pass raw values - constructor handles defaults
tether = call_yaml_constructor(Tether, row,
[:name, :segments],
[:winch_point];
mappings=Dict(
:segments => r -> begin
if !hasfield(typeof(r), :segment_idxs) || isnothing(r.segment_idxs)
[]
else
[to_ref(s) for s in r.segment_idxs]
end
end,
:winch_point => r -> begin
if !hasfield(typeof(r), :winch_point_idx) || isnothing(r.winch_point_idx)
nothing # Constructor handles default
else
to_ref(r.winch_point_idx)
end
end,
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end
))
push!(tethers, tether)
end
end
# Load winches (optional) - SystemStructure handles resolution
winches = Winch[]
if haskey(data, "winches") &&
haskey(data["winches"], "data") &&
data["winches"]["data"] !== nothing &&
!isempty(data["winches"]["data"])
winch_rows = parse_table(data["winches"])
for (i, row) in enumerate(winch_rows)
# Create Winch using new constructor (name, set, tethers)
winch = call_yaml_constructor(Winch, row,
[:name, :set, :tethers],
[:tether_len, :tether_vel, :brake,
:friction_epsilon];
mappings=Dict(
:set => r -> set,
:tethers => r -> [to_ref(t) for t in r.tether_idxs],
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end
))
push!(winches, winch)
end
end
# Parse wing type
function parse_wing_type(s::String)
s_upper = uppercase(s)
s_upper == "REFINE" && return REFINE
s_upper == "QUATERNION" && return QUATERNION
error("Unknown WingType: $s")
end
# Parse aero mode
function parse_aero_mode(s::String)
s_upper = uppercase(s)
s_upper == "AERO_NONE" && return AERO_NONE
s_upper == "AERO_DIRECT" && return AERO_DIRECT
s_upper == "AERO_LINEARIZED" && return AERO_LINEARIZED
error("Unknown AeroMode: $s")
end
# Parse reference points - returns raw references (SystemStructure will resolve)
# Supports both numeric indices and symbolic names
# Examples: [12, 13], [le_center, te_center], [12, [13, 14]], [[le_1, le_2], [te_1, te_2]]
function parse_ref_points(row, field)
!hasfield(typeof(row), field) && return nothing
val = getfield(row, field)
val === nothing && return nothing
# Parse [a, b] or [a, [b, c]] - keep as raw references
@assert length(val) == 2 "ref_points must have 2 elements"
function convert_ref(v)
if v isa Vector
return [to_ref(x) for x in v]
else
return to_ref(v)
end
end
p1 = convert_ref(val[1])
p2 = convert_ref(val[2])
return (p1, p2)
end
# Load wings (optional)
wings = VSMWing[]
if haskey(data, "wings") &&
haskey(data["wings"], "data") &&
data["wings"]["data"] !== nothing &&
!isempty(data["wings"]["data"])
wing_rows = parse_table(data["wings"])
# Validate vsm_set is provided when wings are defined
if isnothing(vsm_set)
error("Wings are defined in YAML but vsm_set was not provided to load_sys_struct_from_yaml. " *
"Please pass a VortexStepMethod.VSMSettings object via the vsm_set keyword argument.")
end
for (i, row) in enumerate(wing_rows)
# Use provided wing_type parameter or parse from YAML
wt = isnothing(wing_type) ? parse_wing_type(String(row.type)) : wing_type
# Build kwargs based on wing type - SystemStructure handles resolution
# Determine aero_mode: kwarg > YAML > default
am = if !isnothing(aero_mode)
aero_mode
elseif hasfield(typeof(row), :aero_mode) &&
!isnothing(row.aero_mode)
parse_aero_mode(String(row.aero_mode))
else
wt == QUATERNION ? AERO_LINEARIZED :
AERO_DIRECT
end
if wt == REFINE
# REFINE wings need z_ref_points, y_ref_points, origin
# Pass raw values - constructor handles defaults
wing = call_yaml_constructor(VSMWing, row,
[:name, :set, :groups, :vsm_set],
[:transform, :y_damping, :angular_damping,
:wing_type, :aero_mode,
:z_ref_points, :y_ref_points, :origin, :pos_cad,
:aero_scale_chord];
mappings=Dict(
:set => r -> set,
:groups => r -> [], # REFINE wings don't have groups
:vsm_set => r -> vsm_set,
:wing_type => r -> wt,
:aero_mode => r -> am,
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end,
:transform => r -> begin
if hasfield(typeof(r), :transform_idx) && !isnothing(r.transform_idx)
to_ref(r.transform_idx)
else
nothing # Constructor handles default
end
end,
:z_ref_points => r ->
parse_ref_points(r, :z_ref_points),
:y_ref_points => r ->
parse_ref_points(r, :y_ref_points),
:origin => r -> begin
if !hasfield(typeof(r), :origin_idx) || r.origin_idx === nothing
return nothing
end
to_ref(r.origin_idx)
end,
:aero_scale_chord => r ->
hasfield(typeof(r), :aero_scale_chord) && !isnothing(r.aero_scale_chord) ?
float(r.aero_scale_chord) : 0.0,
:pos_cad => r -> begin
# Note: pos_cad will be set from origin point position after resolution
# For now, return nothing - SystemStructure will handle this
nothing
end
))
else # QUATERNION
# Pass raw values - constructor handles defaults
wing = call_yaml_constructor(VSMWing, row,
[:name, :set, :groups, :vsm_set],
[:transform, :y_damping, :angular_damping,
:wing_type, :aero_mode, :aero_scale_chord,
:aero_z_offset, :pos_cad,
:z_ref_points, :y_ref_points, :origin];
mappings=Dict(
:set => r -> set,
:aero_mode => r -> am,
:groups => r -> hasfield(typeof(r), :groups) &&
!isnothing(r.groups) ?
[to_ref(g) for g in r.groups] : [],
:vsm_set => r -> vsm_set,
:wing_type => r -> wt,
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i
end
end,
:transform => r -> begin
if hasfield(typeof(r), :transform_idx) &&
!isnothing(r.transform_idx)
to_ref(r.transform_idx)
else
nothing
end
end,
:pos_cad => r -> begin
if !hasfield(typeof(r), :pos_cad) ||
r.pos_cad === nothing
return nothing
end
KVec3(r.pos_cad...)
end,
:aero_scale_chord => r ->
hasfield(typeof(r), :aero_scale_chord) &&
!isnothing(r.aero_scale_chord) ?
float(r.aero_scale_chord) : 0.0,
:z_ref_points => r ->
parse_ref_points(r, :z_ref_points),
:y_ref_points => r ->
parse_ref_points(r, :y_ref_points),
:origin => r -> begin
if !hasfield(typeof(r), :origin_idx) ||
r.origin_idx === nothing
return nothing
end
to_ref(r.origin_idx)
end
))
end
push!(wings, wing)
end
end
# Load transforms (optional) - SystemStructure handles resolution
transforms = Transform[]
if haskey(data, "transforms") &&
haskey(data["transforms"], "data") &&
data["transforms"]["data"] !== nothing &&
!isempty(data["transforms"]["data"])
transform_rows = parse_table(data["transforms"])
for (i, row) in enumerate(transform_rows)
# Create Transform using new constructor (name, elevation, azimuth, heading; kwargs)
transform = call_yaml_constructor(Transform, row,
[:name, :elevation, :azimuth, :heading],
[:base_point, :base_pos, :base_transform,
:wing, :rot_point,
:elevation_vel, :azimuth_vel, :turn_rate];
mappings=Dict(
:elevation => r -> deg2rad(r.elevation),
:azimuth => r -> deg2rad(r.azimuth),
:heading => r -> deg2rad(r.heading),
:elevation_vel => r -> hasfield(typeof(r), :elevation_vel) && !isnothing(r.elevation_vel) ?
deg2rad(r.elevation_vel) : 0.0,
:azimuth_vel => r -> hasfield(typeof(r), :azimuth_vel) && !isnothing(r.azimuth_vel) ?
deg2rad(r.azimuth_vel) : 0.0,
:turn_rate => r -> hasfield(typeof(r), :turn_rate) && !isnothing(r.turn_rate) ?
deg2rad(r.turn_rate) : 0.0,
:base_pos => r -> KVec3(r.base_pos...),
:base_point => r -> to_ref(r.base_point_idx),
:base_transform => r -> begin
if hasfield(typeof(r), :base_transform_idx) && !isnothing(r.base_transform_idx)
to_ref(r.base_transform_idx)
else
nothing
end
end,
:rot_point => r -> begin
if hasfield(typeof(r), :rot_point_idx) && !isnothing(r.rot_point_idx)
to_ref(r.rot_point_idx)
else
nothing
end
end,
:wing => r -> begin
if !hasfield(typeof(r), :wing_idx) || r.wing_idx === nothing
return nothing
end
to_ref(r.wing_idx)
end,
:name => r -> begin
if haskey(r, :name) && !isnothing(r.name)
Symbol(r.name)
else
i # Use index as name if no name provided
end
end
))
push!(transforms, transform)
elev_deg = rad2deg(transform.elevation)
azim_deg = rad2deg(transform.azimuth)
head_deg = rad2deg(transform.heading)
elev_vel_deg = rad2deg(transform.elevation_vel)
azim_vel_deg = rad2deg(transform.azimuth_vel)
turn_rate_deg = rad2deg(transform.turn_rate)
info_msg = " ✓ Transform $(i) created: " *
"elevation=$(elev_deg)°, azimuth=$(azim_deg)°, heading=$(head_deg)°"
if elev_vel_deg != 0.0 || azim_vel_deg != 0.0
info_msg *= ", elevation_vel=$(elev_vel_deg)°/s, azimuth_vel=$(azim_vel_deg)°/s"
end
if turn_rate_deg != 0.0
info_msg *= ", turn_rate=$(turn_rate_deg)°/s"
end
@info info_msg
end
end
# SystemStructure constructor now handles WING→STATIC
# conversion when no wings are defined
return SystemStructure(system_name, set; points, groups,
segments, pulleys, tethers, winches, wings, transforms, ignore_l0, vsm_set)
end
"""
update_yaml_from_sys_struct!(sys_struct::SystemStructure,
source_struc_yaml::AbstractString,
dest_struc_yaml::AbstractString,
source_aero_yaml::AbstractString,
dest_aero_yaml::AbstractString)
Update point positions in structural and aerodynamic YAML files from
the current state of a SystemStructure.
# Arguments
- `sys_struct`: SystemStructure with current point positions
- `source_struc_yaml`: Path to source structural geometry YAML file
- `dest_struc_yaml`: Path to destination structural YAML file
- `source_aero_yaml`: Path to source aero geometry YAML file
- `dest_aero_yaml`: Path to destination aero YAML file
Source and destination paths must be different for each pair.
# Example
```julia
sys = load_sys_struct_from_yaml("struc_geometry.yaml"; ...)
sam = SymbolicAWEModel(set, sys)
# ... run simulation ...
update_yaml_from_sys_struct!(sys,
"struc_geometry.yaml",
"struc_geometry_stable.yaml",
"aero_geometry.yaml",
"aero_geometry_stable.yaml")
```
"""
function update_yaml_from_sys_struct!(sys_struct::SystemStructure,
source_struc_yaml::AbstractString,
dest_struc_yaml::AbstractString,
source_aero_yaml::AbstractString,
dest_aero_yaml::AbstractString)
# Helper to format coordinate with rounding
function format_coord(val::Float64)
# Round to 4 decimals, set small values to 0
rounded = abs(val) < 1e-4 ? 0.0 : round(val, digits=4)
return rounded
end
# Validate paths are not the same
src_struc = abspath(source_struc_yaml)
dst_struc = abspath(dest_struc_yaml)
src_aero = abspath(source_aero_yaml)
dst_aero = abspath(dest_aero_yaml)
# Update pos_b for REFINE wing points based on current wing orientation
for wing in sys_struct.wings
if wing.wing_type == REFINE
R_w_to_b = wing.R_b_to_w' # transpose to get world-to-body
for point in sys_struct.points
if point.wing_idx == wing.idx
point.pos_b .= R_w_to_b * (point.pos_w - wing.pos_w)
end
end
end
end
# Build position dictionary from system structure (body-frame positions)
positions = Dict{Int, Vector{Float64}}()
for point in sys_struct.points
positions[point.idx] = copy(point.pos_b)
end
# Build segment l0 dictionary from system structure
segment_l0s = Dict{Int, Float64}()
for seg in sys_struct.segments
segment_l0s[seg.idx] = seg.l0
end
# Update structural geometry YAML
struc_full_path = isabspath(source_struc_yaml) ? source_struc_yaml :
joinpath(pwd(), source_struc_yaml)
if !isfile(struc_full_path)
error("Source structural YAML file not found: $struc_full_path")
end
dest_struc_full_path = isabspath(dest_struc_yaml) ? dest_struc_yaml :
joinpath(pwd(), dest_struc_yaml)
lines = readlines(struc_full_path)
n_points_updated = 0
n_segments_updated = 0
in_points_section = false
in_segments_section = false
for (i, line) in enumerate(lines)
# Track which section we're in
if occursin(r"^points:", line)
in_points_section = true
in_segments_section = false
elseif occursin(r"^segments:", line)
in_points_section = false
in_segments_section = true
elseif occursin(r"^\w+:", line) # New section starts
in_points_section = false
in_segments_section = false
end
# Update lines in the points section
if in_points_section
# Match: "- [idx, [x, y, z], ..." where coordinates are floats
m = match(r"^(\s*-\s*\[)(\d+)(,\s*\[)([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?,\s*[-+]?\d+\.?\d*(?:[eE][-+]?\d+)?,\s*[-+]?\d+\.?\d*(?:[eE][-+]?\d+)?)(\].*)", line)
if m !== nothing
point_idx = parse(Int, m.captures[2])
if haskey(positions, point_idx)
new_pos = positions[point_idx]
x = format_coord(new_pos[1])
y = format_coord(new_pos[2])
z = format_coord(new_pos[3])
new_coords = "$x, $y, $z"
lines[i] = m.captures[1] * m.captures[2] *
m.captures[3] * new_coords * m.captures[5]
n_points_updated += 1
end
end
end
# Update lines in the segments section
if in_segments_section
# Match: "- [idx, point_i, point_j, type, l0, ...]"
# Format: [idx, point_i, point_j, type, l0, diameter_mm, unit_stiffness, unit_damping, compression_frac]
# We want to update the l0 field (5th field, index 4)
m = match(r"^(\s*-\s*\[)(\d+)(,\s*\d+,\s*\d+,\s*\w+,\s*)([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?)(.*)", line)
if m !== nothing
seg_idx = parse(Int, m.captures[2])
if haskey(segment_l0s, seg_idx)
new_l0 = format_coord(segment_l0s[seg_idx])
# Reconstruct line with updated l0
lines[i] = m.captures[1] * m.captures[2] * m.captures[3] *
string(new_l0) * m.captures[5]
n_segments_updated += 1
end
end
end
end
@info "Updated structural positions and segments" n_points=n_points_updated n_segments=n_segments_updated
# Write updated structural YAML
@info "Writing updated structural YAML" source=struc_full_path dest=dest_struc_full_path
open(dest_struc_full_path, "w") do io
for line in lines
println(io, line)
end
end
# Update aerodynamic geometry YAML
aero_full_path = isabspath(source_aero_yaml) ? source_aero_yaml :
joinpath(pwd(), source_aero_yaml)
if !isfile(aero_full_path)
error("Source aero YAML file not found: $aero_full_path")
end
dest_aero_full_path = isabspath(dest_aero_yaml) ? dest_aero_yaml :
joinpath(pwd(), dest_aero_yaml)
aero_lines = readlines(aero_full_path)
n_aero_updated = 0
for (i, line) in enumerate(aero_lines)
# Match wing section data lines with point references in comments
# Format: "- [airfoil_id, LE_x, LE_y, LE_z, TE_x, TE_y, TE_z]"
# Look for comment indicating point mapping
comment_match = match(r"#.*points?\s+(\d+).*\(LE\).*and\s+(\d+).*\(TE\)", line)
if comment_match !== nothing
le_idx = parse(Int, comment_match.captures[1])
te_idx = parse(Int, comment_match.captures[2])
# Check next line for the actual data
if i < length(aero_lines)
data_line = aero_lines[i+1]
m = match(r"^(\s*-\s*\[)(\d+)(,\s*)([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?),\s*([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?),\s*([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?),\s*([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?),\s*([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?),\s*([-+]?\d+\.?\d*(?:[eE][-+]?\d+)?)(\])", data_line)
if m !== nothing && haskey(positions, le_idx) && haskey(positions, te_idx)
le_pos = positions[le_idx]
te_pos = positions[te_idx]
airfoil_id = m.captures[2]
new_data = "$(m.captures[1])$airfoil_id$(m.captures[3])" *
"$(format_coord(le_pos[1])), $(format_coord(le_pos[2])), $(format_coord(le_pos[3])), " *
"$(format_coord(te_pos[1])), $(format_coord(te_pos[2])), $(format_coord(te_pos[3]))$(m.captures[10])"
aero_lines[i+1] = new_data
n_aero_updated += 1
end
end
end
end