FireSTARR
Loading...
Searching...
No Matches
Scenario.h
1/* Copyright (c) Queen's Printer for Ontario, 2020. */
2/* Copyright (c) His Majesty the King in Right of Canada as represented by the Minister of Natural Resources, 2024-2025. */
3
4/* SPDX-License-Identifier: AGPL-3.0-or-later */
5
6#pragma once
7#include "stdafx.h"
8#include "EventCompare.h"
9#include "FireWeather.h"
10#include "IntensityMap.h"
11#include "Model.h"
12#include "Settings.h"
13#include "StartPoint.h"
14#include "InnerPos.h"
15#include "FireSpread.h"
16#include "CellPoints.h"
17
18namespace fs::sim
19{
20class LogPoints;
21class IObserver;
22class Event;
23using topo::Location;
24using topo::Position;
25using PointSet = vector<InnerPos>;
30 : public logging::SelfLogger
31{
32public:
37 [[nodiscard]] static size_t completed() noexcept;
42 [[nodiscard]] static size_t count() noexcept;
47 [[nodiscard]] static size_t total_steps() noexcept;
52 [[nodiscard]] constexpr size_t weightedDsr() const noexcept
53 {
54 return weather_->weightedDsr();
55 }
56 virtual ~Scenario();
71 size_t id,
73 wx::FireWeather* weather_daily,
74 DurationSize start_time,
75 // const shared_ptr<IntensityMap>& initial_intensity,
76 const shared_ptr<topo::Perimeter>& perimeter,
77 const topo::StartPoint& start_point,
78 Day start_day,
79 Day last_date);
93 size_t id,
95 wx::FireWeather* weather_daily,
96 DurationSize start_time,
97 const shared_ptr<topo::Cell>& start_cell,
98 const topo::StartPoint& start_point,
99 Day start_day,
100 Day last_date);
105 Scenario(Scenario&& rhs) noexcept;
106 Scenario(const Scenario& rhs) = delete;
112 Scenario& operator=(Scenario&& rhs) noexcept;
113 Scenario& operator=(const Scenario& rhs) const = delete;
114 // HACK: use for surface right now
121 [[nodiscard]] Scenario* reset_with_new_start(const shared_ptr<topo::Cell>& start_cell,
122 util::SafeVector* final_sizes);
130 [[nodiscard]] Scenario* reset(mt19937* mt_extinction,
131 mt19937* mt_spread,
132 util::SafeVector* final_sizes);
137 void burn(const Event& event);
142 void cancel(bool show_warning) noexcept;
149 [[nodiscard]]
150#ifdef NDEBUG
151 CONSTEXPR
152#endif
154 cell(const Idx row, const Idx column) const
155 {
156 return model_->cell(row, column);
157 }
163 template <class P>
164 [[nodiscard]] constexpr topo::Cell cell(const Position<P>& position) const
165 {
166 return model_->cell(position);
167 }
172 [[nodiscard]] constexpr Idx rows() const
173 {
174 return model_->rows();
175 }
180 [[nodiscard]] constexpr Idx columns() const
181 {
182 return model_->columns();
183 }
188 [[nodiscard]] constexpr MathSize cellSize() const
189 {
190 return model_->cellSize();
191 }
196 [[nodiscard]] constexpr int64_t simulation() const
197 {
198 return simulation_;
199 }
204 [[nodiscard]] constexpr const topo::StartPoint& startPoint() const
205 {
206 return start_point_;
207 }
212 [[nodiscard]] constexpr DurationSize startTime() const
213 {
214 return start_time_;
215 }
220 [[nodiscard]] constexpr size_t id() const
221 {
222 return id_;
223 }
228 [[nodiscard]] constexpr const Model& model() const
229 {
230 return *model_;
231 }
237 [[nodiscard]] constexpr DurationSize dayStart(const size_t for_day) const
238 {
239 return start_point_.dayStart(for_day);
240 }
246 [[nodiscard]] constexpr DurationSize dayEnd(const size_t for_day) const
247 {
248 return start_point_.dayEnd(for_day);
249 }
255 [[nodiscard]] const wx::FwiWeather* weather(const DurationSize time) const
256 {
257 return weather_->at(time);
258 }
259 [[nodiscard]] const wx::FwiWeather* weather_daily(const DurationSize time) const
260 {
261 return weather_daily_->at(time);
262 }
268 [[nodiscard]] constexpr int nd(const DurationSize time) const
269 {
270 return model().nd(time);
271 }
277 [[nodiscard]] ThresholdSize extinctionThreshold(const DurationSize time) const
278 {
279 return extinction_thresholds_.at(util::time_index(time - start_day_));
280 }
286 [[nodiscard]] ThresholdSize spreadThresholdByRos(const DurationSize time) const
287 {
288 return spread_thresholds_by_ros_.at(util::time_index(time - start_day_));
289 }
295 [[nodiscard]] constexpr bool isAtNight(const DurationSize time) const
296 {
297 const auto day = static_cast<Day>(time);
298 const auto hour_part = 24 * (time - day);
299 return hour_part < dayStart(day) || hour_part > dayEnd(day);
300 }
306 [[nodiscard]] MathSize minimumFfmcForSpread(const DurationSize time) const noexcept
307 {
309 }
315 [[nodiscard]] bool isSurrounded(const Location& location) const;
316 template <class P>
317 [[nodiscard]] bool isSurrounded(const Position<P>& position) const
318 {
319 return isSurrounded(Location{position.hash()});
320 }
326 [[nodiscard]] topo::Cell cell(const InnerPos& p) const noexcept;
332 Scenario* run(map<DurationSize, ProbabilityMap*>* probabilities);
337 void scheduleFireSpread(const Event& event);
342 [[nodiscard]] MathSize currentFireSize() const;
348 [[nodiscard]] bool canBurn(const topo::Cell& location) const;
354 // [[nodiscard]] bool canBurn(HashSize hash) const;
360 [[nodiscard]] bool hasBurned(const Location& location) const;
361 template <class P>
362 [[nodiscard]] bool hasBurned(const Position<P>& position) const
363 {
364 return hasBurned(Location{position.hash()});
365 }
371 // [[nodiscard]] bool hasBurned(HashSize hash) const;
376 void addEvent(Event&& event);
381 // bool evaluateNextEvent();
382 void evaluateNextEvent();
386 void endSimulation() noexcept;
391 void addSaveByOffset(int offset);
397 template <class V>
398 void addSave(V time);
403 [[nodiscard]] bool ran() const noexcept;
411 [[nodiscard]] bool survives(const DurationSize time,
412 const topo::Cell& cell,
413 const DurationSize time_at_location) const
414 {
415 try
416 {
417 const auto fire_wx = weather_;
418 // // NOTE: Does using daily makes sense if we're looking at moisture?
419 // // HACK: use daily with diurnal curves to be consistent with pre-hourly wx version
420 // const auto fire_wx = weather_daily_;
421 const auto wx = fire_wx->at(time);
422 // use Mike's table
423 const auto mc = wx->mcDmcPct();
424 if (100 > mc
425 || (109 >= mc && 5 > time_at_location)
426 || (119 >= mc && 4 > time_at_location)
427 || (131 >= mc && 3 > time_at_location)
428 || (145 >= mc && 2 > time_at_location)
429 || (218 >= mc && 1 > time_at_location))
430 {
431 return true;
432 }
433 // we can look by fuel type because the entire landscape shares the weather
434 return extinctionThreshold(time) < fire_wx->survivalProbability(
435 time,
436 cell.fuelCode());
437 }
438 catch (const std::out_of_range& e)
439 {
440 // FIX: just ignore for now
441 // std::cerr << e.what() << '\n';
442 // logging::warning("Survival is checking for weather that doesn't exist at %f", time);
443 // no weather, so don't survive
444 return false;
445 }
446 }
451 [[nodiscard]] vector<DurationSize> savePoints() const;
456 void saveStats(DurationSize time) const;
461 void evaluate(const Event& event);
465 void clear() noexcept;
466protected:
467 string add_log(const char* format) const noexcept override;
480 size_t id,
481 wx::FireWeather* weather,
482 wx::FireWeather* weather_daily,
483 DurationSize start_time,
484 // const shared_ptr<IntensityMap>& initial_intensity,
485 const shared_ptr<topo::Perimeter>& perimeter,
486 const shared_ptr<topo::Cell>& start_cell,
487 topo::StartPoint start_point,
488 Day start_day,
489 Day last_date);
493 vector<DurationSize> save_points_;
497 vector<ThresholdSize> extinction_thresholds_{};
501 vector<ThresholdSize> spread_thresholds_by_ros_{};
505 DurationSize current_time_;
513 BurnedData* unburnable_;
517 set<Event, EventCompare> scheduler_;
521 unique_ptr<IntensityMap> intensity_;
522 // /**
523 // * @brief Initial intensity map based off perimeter
524 // */
525 // shared_ptr<IntensityMap> initial_intensity_;
529 shared_ptr<topo::Perimeter> perimeter_;
533 map<topo::SpreadKey, SpreadInfo> spread_info_{};
537 map<topo::Cell, DurationSize> arrival_{};
541 MathSize max_ros_;
545 shared_ptr<topo::Cell> start_cell_;
561 map<DurationSize, ProbabilityMap*>* probabilities_;
573 size_t id_;
577 DurationSize start_time_;
581 DurationSize last_save_;
585 size_t current_time_index_ = numeric_limits<size_t>::max();
589 int64_t simulation_;
601 bool ran_;
605 bool cancelled_ = false;
606 shared_ptr<LogPoints> log_points_;
610 size_t step_;
615};
616}
Definition Log.h:247
Definition CellPoints.h:164
A specific Event scheduled in a specific Scenario.
Definition Event.h:19
Contains all the immutable information regarding a simulation that is common between Scenarios.
Definition Model.h:134
topo::Cell cell(const Idx row, const Idx column) const
Cell at the given row and column.
Definition Model.h:167
constexpr Idx columns() const
Number of columns in extent.
Definition Model.h:202
constexpr int nd(const DurationSize time) const
Difference between date and the date of minimum foliar moisture content.
Definition Model.h:290
constexpr Idx rows() const
Cell at the Location represented by the given hash.
Definition Model.h:194
constexpr MathSize cellSize() const
Cell width and height (m)
Definition Model.h:210
A single Scenario in an Iteration using a specific FireWeather stream.
Definition Scenario.h:31
Model * model_
Model this Scenario is being run in.
Definition Scenario.h:557
constexpr DurationSize dayStart(const size_t for_day) const
Sunrise time for given day.
Definition Scenario.h:237
wx::FireWeather * weather_
Hourly weather to use for this Scenario.
Definition Scenario.h:549
size_t oob_spread_
How many times this scenario tried to spread out of bounds.
Definition Scenario.h:614
constexpr Idx rows() const
Number of rows.
Definition Scenario.h:172
map< topo::Cell, DurationSize > arrival_
Map of when Cell had first Point arrive in it.
Definition Scenario.h:537
size_t id_
Identifier.
Definition Scenario.h:573
MathSize max_ros_
Maximum rate of spread for current time.
Definition Scenario.h:541
static size_t total_steps() noexcept
Total number of spread events for all Scenarios.
Definition Scenario.cpp:87
const wx::FwiWeather * weather(const DurationSize time) const
FwiWeather for given time.
Definition Scenario.h:255
topo::StartPoint start_point_
Origin of fire.
Definition Scenario.h:569
ThresholdSize spreadThresholdByRos(const DurationSize time) const
Get spread threshold for given time.
Definition Scenario.h:286
CellPointsMap points_
Map of Cells to the PointSets within them.
Definition Scenario.h:509
MathSize minimumFfmcForSpread(const DurationSize time) const noexcept
Minimum Fine Fuel Moisture Code for spread to be possible.
Definition Scenario.h:306
void addSaveByOffset(int offset)
Add a save point for simulation data at the given offset.
Definition Scenario.cpp:989
constexpr MathSize cellSize() const
Cell width and height (m)
Definition Scenario.h:188
shared_ptr< topo::Cell > start_cell_
Cell that the Scenario starts from if no Perimeter.
Definition Scenario.h:545
bool canBurn(const topo::Cell &location) const
Whether or not a Cell can burn.
Definition Scenario.cpp:967
int64_t simulation_
Simulation number.
Definition Scenario.h:589
static size_t count() noexcept
Number of Scenarios that have been initialized.
Definition Scenario.cpp:83
bool ran() const noexcept
Whether or not this Scenario has run already.
Definition Scenario.cpp:423
bool ran_
Whether or not this Scenario has completed running.
Definition Scenario.h:601
constexpr int nd(const DurationSize time) const
Difference between date and the date of minimum foliar moisture content.
Definition Scenario.h:268
util::SafeVector * final_sizes_
Where to append the final size of this Scenario when run is complete.
Definition Scenario.h:565
topo::Cell cell(const Idx row, const Idx column) const
Get Cell for given row and column.
Definition Scenario.h:154
vector< DurationSize > savePoints() const
List of what times the simulation will save.
Definition Scenario.cpp:995
static size_t completed() noexcept
Number of Scenarios that have completed running.
Definition Scenario.cpp:79
DurationSize start_time_
Start time (decimal days)
Definition Scenario.h:577
vector< ThresholdSize > extinction_thresholds_
Thresholds used to determine if extinction occurs.
Definition Scenario.h:497
constexpr topo::Cell cell(const Position< P > &position) const
Get Cell for given Location.
Definition Scenario.h:164
map< topo::SpreadKey, SpreadInfo > spread_info_
Calculated SpreadInfo for SpreadKey for current time.
Definition Scenario.h:533
map< DurationSize, ProbabilityMap * > * probabilities_
Map of ProbabilityMaps by time snapshot for them was taken.
Definition Scenario.h:561
BurnedData * unburnable_
Contains information on cells that are not burnable.
Definition Scenario.h:513
void evaluateNextEvent()
Evaluate next Event in the queue.
Definition Scenario.cpp:1010
void endSimulation() noexcept
End the simulation.
Definition Scenario.cpp:983
shared_ptr< topo::Perimeter > perimeter_
Initial intensity map based off perimeter.
Definition Scenario.h:529
void addEvent(Event &&event)
Whether or not Location with given hash has burned already.
Definition Scenario.cpp:1005
wx::FireWeather * weather_daily_
Weather stream to use for spread and extinction probability.
Definition Scenario.h:553
constexpr const Model & model() const
Model this Scenario is running in.
Definition Scenario.h:228
MathSize currentFireSize() const
Current fire size (ha)
Definition Scenario.cpp:963
Scenario & operator=(Scenario &&rhs) noexcept
Move assignment.
Definition Scenario.cpp:457
constexpr size_t weightedDsr() const noexcept
Weighted Danger Severity Rating.
Definition Scenario.h:52
void clear() noexcept
Clear the Event list and all other data.
Definition Scenario.cpp:59
constexpr size_t id() const
Identifier.
Definition Scenario.h:220
bool survives(const DurationSize time, const topo::Cell &cell, const DurationSize time_at_location) const
Whether or not the fire survives the conditions.
Definition Scenario.h:411
constexpr DurationSize dayEnd(const size_t for_day) const
Sunset time for given day.
Definition Scenario.h:246
Scenario(Model *model, size_t id, wx::FireWeather *weather, wx::FireWeather *weather_daily, DurationSize start_time, const shared_ptr< topo::Perimeter > &perimeter, const topo::StartPoint &start_point, Day start_day, Day last_date)
Constructor.
Definition Scenario.cpp:157
bool isSurrounded(const Location &location) const
Whether or not the given Location is surrounded by cells that are burnt.
Definition Scenario.cpp:514
vector< ThresholdSize > spread_thresholds_by_ros_
Thresholds used to determine if spread occurs.
Definition Scenario.h:501
DurationSize last_save_
Which save point is the last one.
Definition Scenario.h:581
Scenario * reset(mt19937 *mt_extinction, mt19937 *mt_spread, util::SafeVector *final_sizes)
Reset thresholds and set SafeVector to output results to.
Definition Scenario.cpp:239
Day last_date_
Last day of simulation.
Definition Scenario.h:597
void addSave(V time)
Add a save point for simulation data at given time.
Definition Scenario.cpp:1000
constexpr const topo::StartPoint & startPoint() const
StartPoint that provides sunrise/sunset times.
Definition Scenario.h:204
void saveStats(DurationSize time) const
Save state of Scenario at given time.
Definition Scenario.cpp:415
size_t current_time_index_
Time index for current time.
Definition Scenario.h:585
size_t step_
How many times point spread event has happened.
Definition Scenario.h:610
unique_ptr< IntensityMap > intensity_
Map of what intensity each cell has burned at.
Definition Scenario.h:521
bool hasBurned(const Location &location) const
Whether or not Cell with the given hash can burn.
Definition Scenario.cpp:975
constexpr Idx columns() const
Number of columns.
Definition Scenario.h:180
bool cancelled_
Whether this has been cancelled.
Definition Scenario.h:605
void scheduleFireSpread(const Event &event)
Schedule a fire spread Event.
Definition Scenario.cpp:760
void cancel(bool show_warning) noexcept
Definition Scenario.cpp:1022
Day start_day_
First day of simulation.
Definition Scenario.h:593
void burn(const Event &event)
Burn cell that Event takes place in.
Definition Scenario.cpp:488
DurationSize current_time_
Current time for this Scenario.
Definition Scenario.h:505
void evaluate(const Event &event)
Take whatever steps are necessary to process the given Event.
Definition Scenario.cpp:304
vector< DurationSize > save_points_
List of times to save simulation.
Definition Scenario.h:493
Scenario * reset_with_new_start(const shared_ptr< topo::Cell > &start_cell, util::SafeVector *final_sizes)
Assign start Cell, reset thresholds and set SafeVector to output results to.
Definition Scenario.cpp:203
ThresholdSize extinctionThreshold(const DurationSize time) const
Get extinction threshold for given time.
Definition Scenario.h:277
set< Event, EventCompare > scheduler_
Event scheduler used for ordering events.
Definition Scenario.h:517
constexpr int64_t simulation() const
Simulation number.
Definition Scenario.h:196
constexpr DurationSize startTime() const
Simulation start time.
Definition Scenario.h:212
constexpr bool isAtNight(const DurationSize time) const
Whether or not time is after sunrise and before sunset.
Definition Scenario.h:295
static MathSize minimumFfmcAtNight() noexcept
Minimum Fine Fuel Moisture Code required for spread during the night.
Definition Settings.cpp:611
static MathSize minimumFfmc() noexcept
Minimum Fine Fuel Moisture Code required for spread during the day.
Definition Settings.cpp:607
A Position with a Slope, Aspect, and Fuel.
Definition Cell.h:20
static constexpr FuelCodeSize fuelCode(const SpreadKey value) noexcept
Fuel.
Definition Cell.h:136
Definition Location.h:229
Perimeter for an existing fire to initialize a simulation with.
Definition Perimeter.h:24
A Position with a row and column.
Definition Location.h:57
constexpr HashSize hash() const noexcept
Hash derived from row and column.
Definition Location.h:80
A Point that has sunrise and sunset times for each day.
Definition StartPoint.h:17
constexpr DurationSize dayStart(const size_t day) const
Sunrise time.
Definition StartPoint.h:57
constexpr DurationSize dayEnd(const size_t day) const
Sunset time.
Definition StartPoint.h:66
A vector with added thread safety.
Definition SafeVector.h:15
A stream of weather that gets used by a Scenario every Iteration.
Definition FireWeather.h:29
constexpr size_t weightedDsr() const noexcept
Weighted Danger Severity Rating for the stream.
Definition FireWeather.h:91
const FwiWeather * at(const DurationSize time) const
Get FwiWeather for given time.
Definition FireWeather.h:53
A Weather value with calculated FWI indices.
Definition FWI.h:209
constexpr MathSize mcDmcPct() const
Moisture content (%) based on Dmc.
Definition FWI.h:398
Definition util.h:14