Skip to content

Commit f9bc2de

Browse files
committed
fix wave input parsing
1 parent 1c39fc0 commit f9bc2de

1 file changed

Lines changed: 48 additions & 7 deletions

File tree

src/hydro_yaml_parser.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ YAMLHydroData ReadHydroYAML(const std::string& hydro_file_path) {
178178
bool period_form_linspace = false;
179179
bool period_form_range = false;
180180

181+
// Wave convenience fields
182+
bool waves_amplitude_set = false;
183+
double waves_amplitude = 0.0;
184+
bool period_set_by_synonym = false; // scalar period given via t/p/tp
185+
181186
auto parse_inline_brace_kv = [](const std::string& v) -> std::vector<std::pair<std::string, std::string>> {
182187
// Expect something like: { start: 6.0, stop: 9.0, num: 4 }
183188
std::vector<std::pair<std::string, std::string>> out;
@@ -390,11 +395,18 @@ YAMLHydroData ReadHydroYAML(const std::string& hydro_file_path) {
390395
}
391396
} else if (in_waves) {
392397
// Parse wave properties
393-
if (!in_period_block && key == "type") {
398+
// normalize key for shorthand handling
399+
std::string key_lower = key;
400+
std::transform(key_lower.begin(), key_lower.end(), key_lower.begin(), ::tolower);
401+
402+
if (!in_period_block && key_lower == "type") {
394403
data.waves.type = value;
395-
} else if (!in_period_block && key == "height") {
404+
} else if (!in_period_block && (key_lower == "height" || key_lower == "h")) {
396405
data.waves.height = ParseDouble(value, 0.0);
397-
} else if (!in_period_block && key == "period") {
406+
} else if (!in_period_block && (key_lower == "amplitude" || key_lower == "a")) {
407+
waves_amplitude = ParseDouble(value, 0.0);
408+
waves_amplitude_set = true;
409+
} else if (!in_period_block && (key_lower == "period" || key_lower == "t" || key_lower == "tp" || key_lower == "p")) {
398410
period_block_seen = true;
399411
period_form_values = period_form_linspace = period_form_range = false;
400412
data.waves.period_values.clear();
@@ -403,6 +415,7 @@ YAMLHydroData ReadHydroYAML(const std::string& hydro_file_path) {
403415
if (!looks_structured) {
404416
data.waves.period = ParseDouble(value, 0.0);
405417
data.waves.period_values.push_back(data.waves.period);
418+
period_set_by_synonym = (key_lower != "period");
406419
} else {
407420
// Inline forms on same line
408421
// values inline list inside braces
@@ -509,13 +522,13 @@ YAMLHydroData ReadHydroYAML(const std::string& hydro_file_path) {
509522
throw std::runtime_error("waves.period: range produced no values");
510523
}
511524
data.waves.period = data.waves.period_values.front();
512-
} else if (!in_period_block && key == "direction") {
525+
} else if (!in_period_block && key_lower == "direction") {
513526
data.waves.direction = ParseDouble(value, 0.0);
514-
} else if (!in_period_block && key == "phase") {
527+
} else if (!in_period_block && key_lower == "phase") {
515528
data.waves.phase = ParseDouble(value, 0.0);
516-
} else if (!in_period_block && key == "spectrum") {
529+
} else if (!in_period_block && key_lower == "spectrum") {
517530
data.waves.spectrum = value;
518-
} else if (!in_period_block && key == "seed") {
531+
} else if (!in_period_block && key_lower == "seed") {
519532
try { data.waves.seed = std::stoi(value); } catch (...) { data.waves.seed = -1; }
520533
}
521534
}
@@ -556,6 +569,34 @@ YAMLHydroData ReadHydroYAML(const std::string& hydro_file_path) {
556569
}
557570
}
558571

572+
// Apply amplitude to height if provided
573+
if (waves_amplitude_set) {
574+
const double derived_height = 2.0 * waves_amplitude;
575+
if (data.waves.height > 0.0) {
576+
const double eps = 1e-9;
577+
if (std::abs(data.waves.height - derived_height) > eps) {
578+
throw std::runtime_error("waves: both height and amplitude provided but inconsistent (expected height = 2*amplitude)");
579+
}
580+
} else {
581+
data.waves.height = derived_height;
582+
}
583+
}
584+
585+
// Validation and helpful errors for regular waves
586+
{
587+
std::string type_lower = data.waves.type;
588+
std::transform(type_lower.begin(), type_lower.end(), type_lower.begin(), ::tolower);
589+
if (type_lower == "regular") {
590+
if (data.waves.height <= 0.0) {
591+
throw std::runtime_error("waves: regular requires wave height (use 'height' or 'h', or 'amplitude'/'a')");
592+
}
593+
bool has_period = (data.waves.period > 0.0) || !data.waves.period_values.empty();
594+
if (!has_period) {
595+
throw std::runtime_error("waves: regular requires wave period (use 'period' or shorthand 't', 'tp', or 'p')");
596+
}
597+
}
598+
}
599+
559600
// Validate that we found the required sections
560601
if (!in_hydrodynamics) {
561602
throw std::runtime_error("No 'hydrodynamics:' section found in hydro file: " + hydro_file_path);

0 commit comments

Comments
 (0)