Skip to content

Commit 6e39656

Browse files
authored
Merge pull request #110 from OpenSourceAWE/rm-initial-requirement
2 parents 0c118b9 + 8d2ff98 commit 6e39656

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

src/settings.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
1414
Flat struct, defining the settings of the Simulator and the Viewer.
1515
16+
Use [`load_settings`](@ref) to reload the global module singleton, or
17+
[`Settings(project)`](@ref) to create a fresh, independent instance.
18+
1619
$(TYPEDFIELDS)
1720
"""
1821
@with_kw mutable struct Settings @deftype Float64
@@ -355,13 +358,20 @@ StructTypes.StructType(::Type{Settings}) = StructTypes.Mutable()
355358
PROJECT::String = "system.yaml"
356359

357360
"""
358-
Settings(project)
361+
Settings(project; relax=false)
362+
363+
Create a fresh [`Settings`](@ref) instance, loading from the given
364+
project file. Unlike [`load_settings`](@ref), this does **not** modify
365+
the global module settings; it returns an independent struct.
359366
360-
Constructor for the [`Settings`](@ref) struct, loading settings from the given project file.
367+
## Parameters
368+
- `project`: The name of the project file to load.
369+
- `relax`: If true, missing sections in the settings file are
370+
skipped instead of raising an error.
361371
"""
362-
function Settings(project)
372+
function Settings(project; relax=false)
363373
set = Settings()
364-
return se(set, project)
374+
return se(set, project; relax)
365375
end
366376
const SETTINGS = Settings()
367377
const _SE_DICTS = IdDict{Settings, Dict{String, Any}}()
@@ -396,13 +406,18 @@ end
396406
"""
397407
load_settings(project=PROJECT; relax=false)
398408
399-
Load the project with the given file name.
409+
Reload the global module [`Settings`](@ref) from the given project
410+
file. Returns the updated global settings singleton. To obtain an
411+
independent settings instance instead, use the
412+
[`Settings(project)`](@ref) constructor.
400413
401414
The project must include the path and the suffix .yaml .
402415
403416
## Parameters
404-
- `project`: The name of the project file to load, defaults to the project that was loaded before.
405-
- `relax`: If true, no section needs to be present in the settings.yaml file.
417+
- `project`: The name of the project file to load, defaults to
418+
the project that was loaded before.
419+
- `relax`: If true, missing sections in the settings file are
420+
skipped instead of raising an error.
406421
"""
407422
function load_settings(project=PROJECT; relax=false)
408423
SETTINGS.segments=0
@@ -536,7 +551,7 @@ function se(settings::Settings, project=PROJECT; relax=false)
536551
dict = YAML.load_file(joinpath(DATA_PATH[1], sim_settings_path))
537552
_SE_DICTS[settings] = dict
538553
# update the settings struct from the dictionary
539-
required_sections = ["system", "initial", "solver", "kite", "tether", "environment"]
554+
required_sections = ["system", "solver", "kite", "tether", "environment"]
540555
if relax
541556
for section in required_sections
542557
if section in keys(dict)
@@ -546,7 +561,7 @@ function se(settings::Settings, project=PROJECT; relax=false)
546561
else
547562
update_settings(dict, required_sections, settings)
548563
end
549-
for section in ["steering", "depower", "kps4", "kps5", "bridle", "winch", "kcu"]
564+
for section in ["initial", "steering", "depower", "kps4", "kps5", "bridle", "winch", "kcu"]
550565
if section in keys(dict)
551566
update_settings(dict, [section], settings)
552567
end

test/test-settings.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,46 @@ using KiteUtils, Test
5959
@test se_dict()["environment"]["z0"] == se().z0
6060
set3 = update_settings()
6161
@test set3 == se()
62+
63+
# Settings(project) returns a fresh instance, not the global one
64+
fresh = Settings("system.yaml")
65+
@test fresh.sim_settings == "settings.yaml"
66+
@test fresh !== se()
67+
68+
# Settings(project) supports the relax kwarg
69+
fresh_relax = Settings("system_cabauw.yaml"; relax=true)
70+
@test fresh_relax.v_wind == 9.51
71+
@test fresh_relax !== se()
72+
73+
# "initial" section is optional (no error when missing)
74+
settings_yaml = joinpath(get_data_path(), "settings.yaml")
75+
lines = readlines(settings_yaml)
76+
# Create a settings file without the "initial" section
77+
no_initial = joinpath(tempdir(), "settings_no_initial.yaml")
78+
sys_no_initial = joinpath(tempdir(), "system_no_initial.yaml")
79+
in_initial = false
80+
open(no_initial, "w") do io
81+
for line in lines
82+
if match(r"^initial:", line) !== nothing
83+
in_initial = true
84+
continue
85+
end
86+
if in_initial && (startswith(line, " ") || isempty(line))
87+
continue
88+
end
89+
in_initial = false
90+
println(io, line)
91+
end
92+
end
93+
open(sys_no_initial, "w") do io
94+
println(io, "system:")
95+
println(io, " sim_settings: \"settings_no_initial.yaml\"")
96+
end
97+
old_path = get_data_path()
98+
set_data_path(tempdir())
99+
set_no_init = Settings("system_no_initial.yaml")
100+
@test set_no_init.sim_settings == "settings_no_initial.yaml"
101+
# initial fields keep their defaults (elevations defaults to [70])
102+
@test set_no_init.elevation == 70.0
103+
set_data_path(old_path)
62104
end

0 commit comments

Comments
 (0)