|
| 1 | +# SPDX-FileCopyrightText: 2025 Bart van de Lint |
| 2 | +# SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +# test_yaml_update.jl - Tests for update_sys_struct_from_yaml! |
| 5 | +# |
| 6 | +# Verifies: |
| 7 | +# 1. Unchanged YAML round-trip: pos_cad and l0 unchanged |
| 8 | +# 2. Modified pos_cad: only the changed point is updated |
| 9 | +# 3. Modified segment l0: segment updated correctly |
| 10 | +# 4. l0=nothing in YAML: auto-calc from pos_cad |
| 11 | + |
| 12 | +using Test |
| 13 | +using SymbolicAWEModels |
| 14 | +using SymbolicAWEModels: KVec3 |
| 15 | +using LinearAlgebra: norm |
| 16 | +using KiteUtils |
| 17 | + |
| 18 | +const YAML_UPDATE_BASE = """ |
| 19 | +points: |
| 20 | + headers: [name, pos_cad, type, wing_idx, transform_idx, |
| 21 | + extra_mass, body_frame_damping, |
| 22 | + world_frame_damping, area, drag_coeff] |
| 23 | + data: |
| 24 | + - [pt_a, [1.0, 2.0, 3.0], DYNAMIC, nothing, nothing, |
| 25 | + 1.0, 0.0, 0.0, 0.0, 0.0] |
| 26 | + - [pt_b, [4.0, 5.0, 6.0], DYNAMIC, nothing, nothing, |
| 27 | + 1.0, 0.0, 0.0, 0.0, 0.0] |
| 28 | +
|
| 29 | +segments: |
| 30 | + headers: [name, point_i, point_j, type, l0, |
| 31 | + diameter_mm, unit_stiffness, unit_damping, |
| 32 | + compression_frac] |
| 33 | + data: |
| 34 | + - [seg_ab, pt_a, pt_b, POWER_LINE, 10.0, |
| 35 | + 1.0, 5000.0, 10.0, 1.0] |
| 36 | +""" |
| 37 | + |
| 38 | +const SETTINGS_YAML_UPDATE = """ |
| 39 | +system: |
| 40 | + log_file: "data/2plate" |
| 41 | + g_earth: 9.81 |
| 42 | +initial: |
| 43 | + l_tethers: [0.0] |
| 44 | + v_reel_outs: [0.0] |
| 45 | +solver: |
| 46 | + solver: "FBDF" |
| 47 | + abs_tol: 0.01 |
| 48 | + rel_tol: 0.01 |
| 49 | + relaxation: 0.6 |
| 50 | +kite: |
| 51 | + model: "" |
| 52 | + foil_file: "ram_air_kite/ram_air_kite_foil.dat" |
| 53 | + physical_model: "2plate" |
| 54 | + struc_geometry_path: "struc_geometry.yaml" |
| 55 | + aero_geometry_path: "aero_geometry.yaml" |
| 56 | + mass: 0.0 |
| 57 | + quasi_static: false |
| 58 | +tether: |
| 59 | + cd_tether: 0.958 |
| 60 | + unit_damping: 0.0 |
| 61 | + unit_stiffness: 0.0 |
| 62 | + rho_tether: 724.0 |
| 63 | + e_tether: 5.5e10 |
| 64 | +winch: |
| 65 | + winch_model: "TorqueControlledMachine" |
| 66 | + drum_radius: 0.110 |
| 67 | + gear_ratio: 1.0 |
| 68 | + inertia_total: 0.024 |
| 69 | + f_coulomb: 122.0 |
| 70 | + c_vf: 30.6 |
| 71 | +environment: |
| 72 | + rho_0: 1.225 |
| 73 | + v_wind: 0.0 |
| 74 | + upwind_dir: -90.0 |
| 75 | + profile_law: 0 |
| 76 | +""" |
| 77 | + |
| 78 | +@testset "update_sys_struct_from_yaml!" begin |
| 79 | + tmpdir = mktempdir() |
| 80 | + yaml_path = joinpath(tmpdir, "geometry.yaml") |
| 81 | + write(yaml_path, YAML_UPDATE_BASE) |
| 82 | + |
| 83 | + settings_path = joinpath(tmpdir, "settings.yaml") |
| 84 | + write(settings_path, SETTINGS_YAML_UPDATE) |
| 85 | + system_path = joinpath(tmpdir, "system.yaml") |
| 86 | + write(system_path, """ |
| 87 | +system: |
| 88 | + sim_settings: settings.yaml |
| 89 | +""") |
| 90 | + |
| 91 | + set_data_path(tmpdir) |
| 92 | + set = Settings("system.yaml") |
| 93 | + |
| 94 | + sys = load_sys_struct_from_yaml(yaml_path; |
| 95 | + system_name="yaml_update_test", set=set) |
| 96 | + |
| 97 | + # -------------------------------------------------------- |
| 98 | + # Test 1: Unchanged YAML round-trip |
| 99 | + # -------------------------------------------------------- |
| 100 | + @testset "Unchanged round-trip" begin |
| 101 | + orig_a = copy(sys.points[:pt_a].pos_cad) |
| 102 | + orig_b = copy(sys.points[:pt_b].pos_cad) |
| 103 | + orig_l0 = sys.segments[:seg_ab].l0 |
| 104 | + |
| 105 | + update_sys_struct_from_yaml!(sys, yaml_path) |
| 106 | + |
| 107 | + @test sys.points[:pt_a].pos_cad == orig_a |
| 108 | + @test sys.points[:pt_b].pos_cad == orig_b |
| 109 | + @test sys.segments[:seg_ab].l0 == orig_l0 |
| 110 | + end |
| 111 | + |
| 112 | + # -------------------------------------------------------- |
| 113 | + # Test 2: Modified point position |
| 114 | + # -------------------------------------------------------- |
| 115 | + @testset "Modified point position" begin |
| 116 | + modified_yaml = replace( |
| 117 | + YAML_UPDATE_BASE, |
| 118 | + "[1.0, 2.0, 3.0]" => "[10.0, 20.0, 30.0]") |
| 119 | + mod_path = joinpath(tmpdir, "modified_pos.yaml") |
| 120 | + write(mod_path, modified_yaml) |
| 121 | + |
| 122 | + orig_b = copy(sys.points[:pt_b].pos_cad) |
| 123 | + |
| 124 | + update_sys_struct_from_yaml!(sys, mod_path) |
| 125 | + |
| 126 | + @test sys.points[:pt_a].pos_cad == |
| 127 | + KVec3(10.0, 20.0, 30.0) |
| 128 | + # pt_b should be unchanged |
| 129 | + @test sys.points[:pt_b].pos_cad == orig_b |
| 130 | + end |
| 131 | + |
| 132 | + # -------------------------------------------------------- |
| 133 | + # Test 3: Modified segment l0 |
| 134 | + # -------------------------------------------------------- |
| 135 | + @testset "Modified segment l0" begin |
| 136 | + modified_yaml = replace( |
| 137 | + YAML_UPDATE_BASE, |
| 138 | + "10.0,\n 1.0, 5000.0" => |
| 139 | + "42.5,\n 1.0, 5000.0") |
| 140 | + mod_path = joinpath(tmpdir, "modified_l0.yaml") |
| 141 | + write(mod_path, modified_yaml) |
| 142 | + |
| 143 | + update_sys_struct_from_yaml!(sys, mod_path) |
| 144 | + |
| 145 | + @test sys.segments[:seg_ab].l0 == 42.5 |
| 146 | + end |
| 147 | + |
| 148 | + # -------------------------------------------------------- |
| 149 | + # Test 4: l0=nothing auto-calculates from pos_cad |
| 150 | + # -------------------------------------------------------- |
| 151 | + @testset "l0=nothing auto-calc from pos_cad" begin |
| 152 | + # First reset to base YAML so pos_cad is known |
| 153 | + write(yaml_path, YAML_UPDATE_BASE) |
| 154 | + update_sys_struct_from_yaml!(sys, yaml_path) |
| 155 | + |
| 156 | + expected_l0 = norm( |
| 157 | + KVec3(1.0, 2.0, 3.0) - KVec3(4.0, 5.0, 6.0)) |
| 158 | + |
| 159 | + nothing_yaml = replace( |
| 160 | + YAML_UPDATE_BASE, |
| 161 | + "10.0,\n 1.0, 5000.0" => |
| 162 | + "nothing,\n 1.0, 5000.0") |
| 163 | + nothing_path = joinpath(tmpdir, "nothing_l0.yaml") |
| 164 | + write(nothing_path, nothing_yaml) |
| 165 | + |
| 166 | + update_sys_struct_from_yaml!(sys, nothing_path) |
| 167 | + |
| 168 | + @test sys.segments[:seg_ab].l0 ≈ expected_l0 |
| 169 | + end |
| 170 | + |
| 171 | + rm(tmpdir; recursive=true) |
| 172 | +end |
0 commit comments