Skip to content

Commit 950767d

Browse files
committed
use std::optional for settings
1 parent 7e034f5 commit 950767d

6 files changed

Lines changed: 64 additions & 68 deletions

File tree

src/cpp/firestarr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ int main(const int argc, const char* const argv[])
160160
// HACK: ISI for yesterday really doesn't matter so just use any wind
161161
// HACK: it's basically wrong to assign this precip to yesterday's object,
162162
// but don't want to add another argument right now
163-
const FwiWeather yesterday{settings.get_yesterday_weather()};
163+
const FwiWeather yesterday{settings.get_weather()};
164164
fs::fix_tm(&start_date);
165165
fs::logging::note(
166166
"Simulation start time after fix_tm() again is %d-%02d-%02d %02d:%02d",
@@ -180,7 +180,7 @@ int main(const int argc, const char* const argv[])
180180
start_point,
181181
start,
182182
settings.perimeter,
183-
settings.initial_size
183+
settings.initial_size.value_or(0)
184184
);
185185
Log::closeLogFile();
186186
}

src/cpp/fs/ArgumentParser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ void register_setter(T& variable, string v, string help, bool required, std::fun
6565
register_argument(v, help, required, [&variable, fct] { variable = fct(); });
6666
}
6767
template <class T>
68+
void register_setter(
69+
std::optional<T>& variable,
70+
string v,
71+
string help,
72+
bool required,
73+
std::function<T()> fct
74+
)
75+
{
76+
register_argument(v, help, required, [&variable, fct] { variable = fct(); });
77+
}
78+
template <class T>
6879
void register_setter(
6980
atomic<T>& variable,
7081
string v,
@@ -82,6 +93,11 @@ void register_index(T& index, string v, string help, bool required)
8293
{
8394
register_argument(v, help, required, [&] { index = parse_index<T>(); });
8495
}
96+
template <class T>
97+
void register_index(std::optional<T>& index, string v, string help, bool required)
98+
{
99+
register_argument(v, help, required, [&] { index = parse_index<T>(); });
100+
}
85101
string ArgumentParser::get_args()
86102
{
87103
std::string args{arguments_.at(0)};

src/cpp/fs/Settings.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "FuelLookup.h"
77
#include "Log.h"
88
#include "Trim.h"
9+
#include "unstable.h"
910
#include "Util.h"
1011
namespace fs::settings
1112
{
@@ -92,6 +93,7 @@ constexpr string to_string(const Mode mode)
9293
case Mode::Surface:
9394
return "SURFACE";
9495
}
96+
return logging::fatal<string>("Mode %s not handled", mode);
9597
};
9698
string ModeOptions()
9799
{
@@ -415,15 +417,15 @@ void Settings::saveTo(const string& output_directory) const noexcept
415417
}
416418
if (Mode::Test == mode)
417419
{
418-
put("FUEL", "initial fire size", initial_size);
420+
put("FUEL", "fbp fuel type", fuel_name);
419421
put("TEST_ALL", "test every combination of settings for tests", test_all);
420422
put("HOURS", "number of hours to run tests for", hours);
421423
}
422424
if (Mode::Simulation != mode)
423425
{
424-
put("FFMC", "fine fuel moisture code", ffmc.value);
425-
put("DMC", "duff moisture code", dmc.value);
426-
put("DC", "drought code", dc.value);
426+
put("FFMC", "fine fuel moisture code", ffmc);
427+
put("DMC", "duff moisture code", dmc);
428+
put("DC", "drought code", dc);
427429
put("WD", "wind direction (degrees)", wind_direction);
428430
put("WS", "wind speed (km/h)", wind_speed);
429431
}
@@ -435,32 +437,21 @@ void Settings::saveTo(const string& output_directory) const noexcept
435437
// FIX: don't have output for set/getRoot()
436438
// put("", "", );
437439
}
438-
FwiWeather Settings::get_test_weather() const
440+
FwiWeather Settings::get_weather() const
439441
{
440442
return {
441443
Weather{
442444
Temperature::Zero(),
443445
RelativeHumidity::Zero(),
444-
Wind{Speed{wind_speed}, Direction{Degrees{wind_direction}}},
445-
Precipitation::Zero()
446-
},
447-
ffmc,
448-
dmc,
449-
dc
450-
};
451-
}
452-
FwiWeather Settings::get_yesterday_weather() const
453-
{
454-
return {
455-
Weather{
456-
Temperature::Zero(),
457-
RelativeHumidity::Zero(),
458-
Wind{Speed{wind_speed}, Direction{Degrees{wind_direction}}},
446+
Wind{
447+
Speed{wind_speed.value_or(Speed::Invalid().value)},
448+
Direction{Degrees{wind_direction.value_or(Direction::Invalid().value)}}
449+
},
459450
{apcp_prev}
460451
},
461-
ffmc,
462-
dmc,
463-
dc
452+
ffmc.value_or(Ffmc::Invalid()),
453+
dmc.value_or(Dmc::Invalid()),
454+
dc.value_or(Dc::Invalid())
464455
};
465456
}
466457
}

src/cpp/fs/Settings.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,34 +177,33 @@ class Settings
177177
LazyFuelLookup fuel_lookup{};
178178
// Days to output probability contours for (1 is start date, 2 is day after, etc.)
179179
OutputDateOffsets output_date_offsets{};
180-
FwiWeather get_yesterday_weather() const;
180+
FwiWeather get_weather() const;
181181
// FIX: need to get rain since noon yesterday to start of this hourly weather
182182
Precipitation apcp_prev{Precipitation::Zero()};
183183

184184
public:
185185
// normal mode only variables
186186
// initial fire size (ha)
187-
size_t initial_size{0};
187+
std::optional<size_t> initial_size{};
188188

189189
public:
190190
// test mode only variables
191191
// name of fuel to use for test
192-
string fuel_name{};
192+
std::optional<string> fuel_name{};
193193
// test every combination of settings for tests
194-
bool test_all{false};
194+
std::optional<bool> test_all{};
195195
// number of hours to run tests for
196-
MathSize hours{INVALID_TIME};
197-
FwiWeather get_test_weather() const;
196+
std::optional<MathSize> hours{};
198197

199198
public:
200199
// test/surface mode variables
201-
Ffmc ffmc{Ffmc::Invalid()};
202-
Dmc dmc{Dmc::Invalid()};
203-
Dc dc{Dc::Invalid()};
204-
MathSize wind_direction{Direction::Invalid().value};
205-
MathSize wind_speed{Speed::Invalid().value};
206-
SlopeSize slope = static_cast<SlopeSize>(INVALID_SLOPE);
207-
AspectSize aspect = static_cast<AspectSize>(INVALID_ASPECT);
200+
std::optional<Ffmc> ffmc{};
201+
std::optional<Dmc> dmc{};
202+
std::optional<Dc> dc{};
203+
std::optional<MathSize> wind_direction{};
204+
std::optional<MathSize> wind_speed{};
205+
std::optional<SlopeSize> slope{};
206+
std::optional<AspectSize> aspect{};
208207

209208
private:
210209
/**

src/cpp/fs/StrictType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct StrictType
102102
value /= rhs.value;
103103
return *this;
104104
}
105+
operator string() noexcept { return string(value); }
105106
};
106107
template <class ConcreteType, units::UnitType Units, class ValueType, int InvalidValue>
107108
[[nodiscard]] constexpr ConcreteType operator*(

src/cpp/fs/Test.cpp

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,7 @@ void show_options(const char* name, const vector<string>& values)
302302
};
303303
int test(Settings& settings)
304304
{
305-
const FwiWeather wx_test{settings.get_test_weather()};
306305
const auto output_directory{settings.output_directory};
307-
const auto num_hours{settings.hours};
308-
const ptr<const FwiWeather> wx{&wx_test};
309-
const auto constant_fuel_name{settings.fuel_name};
310-
const auto constant_slope{settings.slope};
311-
const auto constant_aspect{settings.aspect};
312-
const auto test_all{settings.test_all};
313306
static const AspectSize ASPECT_INCREMENT = 90;
314307
static const SlopeSize SLOPE_INCREMENT = 60;
315308
static const int WS_INCREMENT = 5;
@@ -344,24 +337,20 @@ int test(Settings& settings)
344337
settings.save_points = false;
345338
// make sure all tests run regardless of how long it takes
346339
settings.maximum_time_seconds = numeric_limits<size_t>::max();
347-
const auto hours = INVALID_TIME == num_hours ? DEFAULT_HOURS : num_hours;
348-
const auto ffmc = (fs::Ffmc::Invalid() == wx->ffmc) ? DEFAULT_FFMC : wx->ffmc;
349-
const auto dmc = (fs::Dmc::Invalid() == wx->dmc) ? DEFAULT_DMC : wx->dmc;
350-
const auto dc = (fs::Dc::Invalid() == wx->dc) ? DEFAULT_DC : wx->dc;
351-
// HACK: need to compare value and not object
352-
const auto wind_direction = (fs::Direction::Invalid().value == wx->wind.direction.value)
353-
? DEFAULT_WIND_DIRECTION
354-
: wx->wind.direction;
355-
const auto wind_speed =
356-
(fs::Speed::Invalid().value == wx->wind.speed.value) ? DEFAULT_WIND_SPEED : wx->wind.speed;
357-
const auto wind = fs::Wind{wind_speed, wind_direction};
358-
const auto slope = (INVALID_SLOPE == constant_slope) ? DEFAULT_SLOPE : constant_slope;
359-
const auto aspect = (INVALID_ASPECT == constant_aspect) ? DEFAULT_ASPECT : constant_aspect;
360-
const auto fixed_fuel_name = simplify_fuel_name(constant_fuel_name);
340+
const auto hours{settings.hours.value_or(DEFAULT_HOURS)};
341+
const auto ffmc{settings.ffmc.value_or(DEFAULT_FFMC)};
342+
const auto dmc{settings.dmc.value_or(DEFAULT_DMC)};
343+
const auto dc{settings.dc.value_or(DEFAULT_DC)};
344+
const Direction wind_direction{settings.wind_direction.value_or(DEFAULT_WIND_DIRECTION.value)};
345+
const Speed wind_speed{settings.wind_speed.value_or(DEFAULT_WIND_SPEED.value)};
346+
const Wind wind{wind_speed, wind_direction};
347+
const auto slope{settings.slope.value_or(DEFAULT_SLOPE)};
348+
const auto aspect{settings.aspect.value_or(DEFAULT_ASPECT)};
349+
const auto fixed_fuel_name = simplify_fuel_name(settings.fuel_name.value_or(""));
361350
const auto fuel = (fixed_fuel_name.empty() ? DEFAULT_FUEL_NAME : fixed_fuel_name);
362351
try
363352
{
364-
if (test_all)
353+
if (settings.test_all.value_or(false))
365354
{
366355
size_t result = 0;
367356
// generate all options first so we can say how many there are at start
@@ -378,7 +367,7 @@ int test(Settings& settings)
378367
fuel_names.emplace_back(fuel);
379368
}
380369
auto slopes = vector<SlopeSize>();
381-
if (INVALID_SLOPE == constant_slope)
370+
if (!settings.slope.has_value())
382371
{
383372
for (SlopeSize slope = 0; slope <= 100; slope += SLOPE_INCREMENT)
384373
{
@@ -387,10 +376,10 @@ int test(Settings& settings)
387376
}
388377
else
389378
{
390-
slopes.emplace_back(constant_slope);
379+
slopes.emplace_back(slope);
391380
}
392381
auto aspects = vector<AspectSize>();
393-
if (INVALID_ASPECT == constant_aspect)
382+
if (!settings.aspect.has_value())
394383
{
395384
for (AspectSize aspect = 0; aspect < 360; aspect += ASPECT_INCREMENT)
396385
{
@@ -399,10 +388,10 @@ int test(Settings& settings)
399388
}
400389
else
401390
{
402-
aspects.emplace_back(constant_aspect);
391+
aspects.emplace_back(aspect);
403392
}
404393
auto wind_directions = vector<DirectionSize>();
405-
if (fs::Direction::Invalid() == wx->wind.direction)
394+
if (!settings.wind_direction.has_value())
406395
{
407396
for (auto wind_direction = 0; wind_direction < 360; wind_direction += WD_INCREMENT)
408397
{
@@ -411,10 +400,10 @@ int test(Settings& settings)
411400
}
412401
else
413402
{
414-
wind_directions.emplace_back(static_cast<int>(wx->wind.direction.asDegreesSize()));
403+
wind_directions.emplace_back(static_cast<int>(wind_direction.value));
415404
}
416405
auto wind_speeds = vector<int>();
417-
if (fs::Speed::Invalid() == wx->wind.speed)
406+
if (!settings.wind_speed.has_value())
418407
{
419408
for (auto wind_speed = 0; wind_speed <= MAX_WIND; wind_speed += WS_INCREMENT)
420409
{
@@ -423,7 +412,7 @@ int test(Settings& settings)
423412
}
424413
else
425414
{
426-
wind_speeds.emplace_back(static_cast<int>(wx->wind.speed.value));
415+
wind_speeds.emplace_back(static_cast<int>(wind_speed.value));
427416
}
428417
size_t values = 1;
429418
values *= fuel_names.size();

0 commit comments

Comments
 (0)