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{
31 void operator()(IObserver*) const;
32};
37 : public logging::SelfLogger
38{
39public:
44 [[nodiscard]] static size_t completed() noexcept;
49 [[nodiscard]] static size_t count() noexcept;
54 [[nodiscard]] static size_t total_steps() noexcept;
59 [[nodiscard]] constexpr size_t weightedDsr() const noexcept
60 {
61 return weather_->weightedDsr();
62 }
63 virtual ~Scenario();
78 size_t id,
80 wx::FireWeather* weather_daily,
81 DurationSize start_time,
82 // const shared_ptr<IntensityMap>& initial_intensity,
83 const shared_ptr<topo::Perimeter>& perimeter,
84 const topo::StartPoint& start_point,
85 Day start_day,
86 Day last_date);
100 size_t id,
102 wx::FireWeather* weather_daily,
103 DurationSize start_time,
104 const shared_ptr<topo::Cell>& start_cell,
105 const topo::StartPoint& start_point,
106 Day start_day,
107 Day last_date);
112 Scenario(Scenario&& rhs) noexcept;
113 Scenario(const Scenario& rhs) = delete;
119 Scenario& operator=(Scenario&& rhs) noexcept;
120 Scenario& operator=(const Scenario& rhs) const = delete;
121 // HACK: use for surface right now
128 [[nodiscard]] Scenario* reset_with_new_start(const shared_ptr<topo::Cell>& start_cell,
129 util::SafeVector* final_sizes);
137 [[nodiscard]] Scenario* reset(mt19937* mt_extinction,
138 mt19937* mt_spread,
139 util::SafeVector* final_sizes);
144 void burn(const Event& event);
149 void cancel(bool show_warning) noexcept;
156 [[nodiscard]]
157#ifdef NDEBUG
158 constexpr
159#endif
161 cell(const Idx row, const Idx column) const
162 {
163 return model_->cell(row, column);
164 }
170 template <class P>
171 [[nodiscard]] constexpr topo::Cell cell(const Position<P>& position) const
172 {
173 return model_->cell(position);
174 }
179 [[nodiscard]] constexpr Idx rows() const
180 {
181 return model_->rows();
182 }
187 [[nodiscard]] constexpr Idx columns() const
188 {
189 return model_->columns();
190 }
195 [[nodiscard]] constexpr MathSize cellSize() const
196 {
197 return model_->cellSize();
198 }
203 [[nodiscard]] constexpr int64_t simulation() const
204 {
205 return simulation_;
206 }
211 [[nodiscard]] constexpr const topo::StartPoint& startPoint() const
212 {
213 return start_point_;
214 }
219 [[nodiscard]] constexpr DurationSize startTime() const
220 {
221 return start_time_;
222 }
227 [[nodiscard]] constexpr size_t id() const
228 {
229 return id_;
230 }
235 [[nodiscard]] constexpr const Model& model() const
236 {
237 return *model_;
238 }
244 [[nodiscard]] constexpr DurationSize dayStart(const size_t for_day) const
245 {
246 return start_point_.dayStart(for_day);
247 }
253 [[nodiscard]] constexpr DurationSize dayEnd(const size_t for_day) const
254 {
255 return start_point_.dayEnd(for_day);
256 }
262 [[nodiscard]] const wx::FwiWeather* weather(const DurationSize time) const
263 {
264 return weather_->at(time);
265 }
266 [[nodiscard]] const wx::FwiWeather* weather_daily(const DurationSize time) const
267 {
268 return weather_daily_->at(time);
269 }
275 [[nodiscard]] constexpr int nd(const DurationSize time) const
276 {
277 return model().nd(time);
278 }
284 [[nodiscard]] ThresholdSize extinctionThreshold(const DurationSize time) const
285 {
286 return extinction_thresholds_.at(util::time_index(time - start_day_));
287 }
293 [[nodiscard]] ThresholdSize spreadThresholdByRos(const DurationSize time) const
294 {
295 return spread_thresholds_by_ros_.at(util::time_index(time - start_day_));
296 }
302 [[nodiscard]] constexpr bool isAtNight(const DurationSize time) const
303 {
304 const auto day = static_cast<Day>(time);
305 const auto hour_part = 24 * (time - day);
306 return hour_part < dayStart(day) || hour_part > dayEnd(day);
307 }
313 [[nodiscard]] MathSize minimumFfmcForSpread(const DurationSize time) const noexcept
314 {
316 }
322 [[nodiscard]] bool isSurrounded(const Location& location) const;
323 template <class P>
324 [[nodiscard]] bool isSurrounded(const Position<P>& position) const
325 {
326 return isSurrounded(Location{position.hash()});
327 }
333 [[nodiscard]] topo::Cell cell(const InnerPos& p) const noexcept;
339 Scenario* run(map<DurationSize, ProbabilityMap*>* probabilities);
344 void scheduleFireSpread(const Event& event);
349 [[nodiscard]] MathSize currentFireSize() const;
355 [[nodiscard]] bool canBurn(const topo::Cell& location) const;
361 // [[nodiscard]] bool canBurn(HashSize hash) const;
367 [[nodiscard]] bool hasBurned(const Location& location) const;
368 template <class P>
369 [[nodiscard]] bool hasBurned(const Position<P>& position) const
370 {
371 return hasBurned(Location{position.hash()});
372 }
378 // [[nodiscard]] bool hasBurned(HashSize hash) const;
383 void addEvent(Event&& event);
388 // bool evaluateNextEvent();
389 void evaluateNextEvent();
393 void endSimulation() noexcept;
398 void addSaveByOffset(int offset);
404 template <class V>
405 void addSave(V time);
410 void saveObservers(const string& base_name) const;
415 void saveObservers(DurationSize time) const;
421 void saveIntensity(const string& dir, const string& base_name) const;
426 [[nodiscard]] bool ran() const noexcept;
434 [[nodiscard]] bool survives(const DurationSize time,
435 const topo::Cell& cell,
436 const DurationSize time_at_location) const
437 {
439 {
440 // always survive if deterministic
441 return true;
442 }
443 try
444 {
445 const auto fire_wx = weather_;
446 // // NOTE: Does using daily makes sense if we're looking at moisture?
447 // // HACK: use daily with diurnal curves to be consistent with pre-hourly wx version
448 // const auto fire_wx = weather_daily_;
449 const auto wx = fire_wx->at(time);
450 // use Mike's table
451 const auto mc = wx->mcDmcPct();
452 if (100 > mc
453 || (109 >= mc && 5 > time_at_location)
454 || (119 >= mc && 4 > time_at_location)
455 || (131 >= mc && 3 > time_at_location)
456 || (145 >= mc && 2 > time_at_location)
457 || (218 >= mc && 1 > time_at_location))
458 {
459 return true;
460 }
461 // we can look by fuel type because the entire landscape shares the weather
462 return extinctionThreshold(time) < fire_wx->survivalProbability(
463 time,
464 cell.fuelCode());
465 }
466 catch (const std::out_of_range& e)
467 {
468 // FIX: just ignore for now
469 // std::cerr << e.what() << '\n';
470 // logging::warning("Survival is checking for weather that doesn't exist at %f", time);
471 // no weather, so don't survive
472 return false;
473 }
474 }
479 [[nodiscard]] vector<DurationSize> savePoints() const;
484 void saveStats(DurationSize time) const;
489 void registerObserver(IObserver* observer);
494 void notify(const Event& event) const;
499 void evaluate(const Event& event);
503 void clear() noexcept;
504protected:
505 string add_log(const char* format) const noexcept override;
518 size_t id,
519 wx::FireWeather* weather,
520 wx::FireWeather* weather_daily,
521 DurationSize start_time,
522 // const shared_ptr<IntensityMap>& initial_intensity,
523 const shared_ptr<topo::Perimeter>& perimeter,
524 const shared_ptr<topo::Cell>& start_cell,
525 topo::StartPoint start_point,
526 Day start_day,
527 Day last_date);
535 vector<DurationSize> save_points_;
539 vector<ThresholdSize> extinction_thresholds_{};
543 vector<ThresholdSize> spread_thresholds_by_ros_{};
547 DurationSize current_time_;
555 BurnedData* unburnable_;
559 set<Event, EventCompare> scheduler_;
563 unique_ptr<IntensityMap> intensity_;
564 // /**
565 // * @brief Initial intensity map based off perimeter
566 // */
567 // shared_ptr<IntensityMap> initial_intensity_;
571 shared_ptr<topo::Perimeter> perimeter_;
575 map<topo::SpreadKey, SpreadInfo> spread_info_{};
579 map<topo::Cell, DurationSize> arrival_{};
583 MathSize max_ros_;
587 shared_ptr<topo::Cell> start_cell_;
603 map<DurationSize, ProbabilityMap*>* probabilities_;
615 size_t id_;
619 DurationSize start_time_;
623 DurationSize last_save_;
627 size_t current_time_index_ = numeric_limits<size_t>::max();
631 int64_t simulation_;
643 bool ran_;
647 bool cancelled_ = false;
648 shared_ptr<LogPoints> log_points_;
652 size_t step_;
657};
658}
Definition Log.h:247
Definition CellPoints.h:206
A specific Event scheduled in a specific Scenario.
Definition Event.h:19
Interface for observers that get notified when cells burn so they can track things.
Definition Observer.h:20
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:282
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:38
Model * model_
Model this Scenario is being run in.
Definition Scenario.h:599
constexpr DurationSize dayStart(const size_t for_day) const
Sunrise time for given day.
Definition Scenario.h:244
wx::FireWeather * weather_
Hourly weather to use for this Scenario.
Definition Scenario.h:591
size_t oob_spread_
How many times this scenario tried to spread out of bounds.
Definition Scenario.h:656
constexpr Idx rows() const
Number of rows.
Definition Scenario.h:179
map< topo::Cell, DurationSize > arrival_
Map of when Cell had first Point arrive in it.
Definition Scenario.h:579
size_t id_
Identifier.
Definition Scenario.h:615
MathSize max_ros_
Maximum rate of spread for current time.
Definition Scenario.h:583
static size_t total_steps() noexcept
Total number of spread events for all Scenarios.
Definition Scenario.cpp:94
const wx::FwiWeather * weather(const DurationSize time) const
FwiWeather for given time.
Definition Scenario.h:262
topo::StartPoint start_point_
Origin of fire.
Definition Scenario.h:611
ThresholdSize spreadThresholdByRos(const DurationSize time) const
Get spread threshold for given time.
Definition Scenario.h:293
CellPointsMap points_
Map of Cells to the PointSets within them.
Definition Scenario.h:551
MathSize minimumFfmcForSpread(const DurationSize time) const noexcept
Minimum Fine Fuel Moisture Code for spread to be possible.
Definition Scenario.h:313
void addSaveByOffset(int offset)
Add a save point for simulation data at the given offset.
Definition Scenario.cpp:1158
constexpr MathSize cellSize() const
Cell width and height (m)
Definition Scenario.h:195
shared_ptr< topo::Cell > start_cell_
Cell that the Scenario starts from if no Perimeter.
Definition Scenario.h:587
bool canBurn(const topo::Cell &location) const
Whether or not a Cell can burn.
Definition Scenario.cpp:1136
int64_t simulation_
Simulation number.
Definition Scenario.h:631
static size_t count() noexcept
Number of Scenarios that have been initialized.
Definition Scenario.cpp:90
bool ran() const noexcept
Whether or not this Scenario has run already.
Definition Scenario.cpp:483
bool ran_
Whether or not this Scenario has completed running.
Definition Scenario.h:643
list< unique_ptr< IObserver, IObserver_deleter > > observers_
Observers to be notified when cells burn.
Definition Scenario.h:531
constexpr int nd(const DurationSize time) const
Difference between date and the date of minimum foliar moisture content.
Definition Scenario.h:275
util::SafeVector * final_sizes_
Where to append the final size of this Scenario when run is complete.
Definition Scenario.h:607
topo::Cell cell(const Idx row, const Idx column) const
Get Cell for given row and column.
Definition Scenario.h:161
vector< DurationSize > savePoints() const
List of what times the simulation will save.
Definition Scenario.cpp:1164
static size_t completed() noexcept
Number of Scenarios that have completed running.
Definition Scenario.cpp:86
DurationSize start_time_
Start time (decimal days)
Definition Scenario.h:619
vector< ThresholdSize > extinction_thresholds_
Thresholds used to determine if extinction occurs.
Definition Scenario.h:539
constexpr topo::Cell cell(const Position< P > &position) const
Get Cell for given Location.
Definition Scenario.h:171
map< topo::SpreadKey, SpreadInfo > spread_info_
Calculated SpreadInfo for SpreadKey for current time.
Definition Scenario.h:575
map< DurationSize, ProbabilityMap * > * probabilities_
Map of ProbabilityMaps by time snapshot for them was taken.
Definition Scenario.h:603
BurnedData * unburnable_
Contains information on cells that are not burnable.
Definition Scenario.h:555
void evaluateNextEvent()
Evaluate next Event in the queue.
Definition Scenario.cpp:1179
void registerObserver(IObserver *observer)
Register an IObserver that will be notified when Cells burn.
Definition Scenario.cpp:451
void endSimulation() noexcept
End the simulation.
Definition Scenario.cpp:1152
shared_ptr< topo::Perimeter > perimeter_
Initial intensity map based off perimeter.
Definition Scenario.h:571
void addEvent(Event &&event)
Whether or not Location with given hash has burned already.
Definition Scenario.cpp:1174
wx::FireWeather * weather_daily_
Weather stream to use for spread and extinction probability.
Definition Scenario.h:595
constexpr const Model & model() const
Model this Scenario is running in.
Definition Scenario.h:235
MathSize currentFireSize() const
Current fire size (ha)
Definition Scenario.cpp:1132
Scenario & operator=(Scenario &&rhs) noexcept
Move assignment.
Definition Scenario.cpp:518
constexpr size_t weightedDsr() const noexcept
Weighted Danger Severity Rating.
Definition Scenario.h:59
void clear() noexcept
Clear the Event list and all other data.
Definition Scenario.cpp:63
constexpr size_t id() const
Identifier.
Definition Scenario.h:227
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:434
constexpr DurationSize dayEnd(const size_t for_day) const
Sunset time for given day.
Definition Scenario.h:253
void saveObservers(const string &base_name) const
Tell Observers to save their data with base file name.
Definition Scenario.cpp:462
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:164
bool isSurrounded(const Location &location) const
Whether or not the given Location is surrounded by cells that are burnt.
Definition Scenario.cpp:577
vector< ThresholdSize > spread_thresholds_by_ros_
Thresholds used to determine if spread occurs.
Definition Scenario.h:543
void notify(const Event &event) const
Notify IObservers that a Cell has burned.
Definition Scenario.cpp:455
DurationSize last_save_
Which save point is the last one.
Definition Scenario.h:623
Scenario * reset(mt19937 *mt_extinction, mt19937 *mt_spread, util::SafeVector *final_sizes)
Reset thresholds and set SafeVector to output results to.
Definition Scenario.cpp:250
Day last_date_
Last day of simulation.
Definition Scenario.h:639
void addSave(V time)
Add a save point for simulation data at given time.
Definition Scenario.cpp:1169
constexpr const topo::StartPoint & startPoint() const
StartPoint that provides sunrise/sunset times.
Definition Scenario.h:211
void saveStats(DurationSize time) const
Save state of Scenario at given time.
Definition Scenario.cpp:443
size_t current_time_index_
Time index for current time.
Definition Scenario.h:627
void saveIntensity(const string &dir, const string &base_name) const
Save burn intensity information.
Definition Scenario.cpp:479
size_t step_
How many times point spread event has happened.
Definition Scenario.h:652
unique_ptr< IntensityMap > intensity_
Map of what intensity each cell has burned at.
Definition Scenario.h:563
bool hasBurned(const Location &location) const
Whether or not Cell with the given hash can burn.
Definition Scenario.cpp:1144
constexpr Idx columns() const
Number of columns.
Definition Scenario.h:187
bool cancelled_
Whether this has been cancelled.
Definition Scenario.h:647
void scheduleFireSpread(const Event &event)
Schedule a fire spread Event.
Definition Scenario.cpp:923
void cancel(bool show_warning) noexcept
Definition Scenario.cpp:1191
Day start_day_
First day of simulation.
Definition Scenario.h:635
void burn(const Event &event)
Burn cell that Event takes place in.
Definition Scenario.cpp:550
DurationSize current_time_
Current time for this Scenario.
Definition Scenario.h:547
void evaluate(const Event &event)
Take whatever steps are necessary to process the given Event.
Definition Scenario.cpp:319
vector< DurationSize > save_points_
List of times to save simulation.
Definition Scenario.h:535
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:210
ThresholdSize extinctionThreshold(const DurationSize time) const
Get extinction threshold for given time.
Definition Scenario.h:284
set< Event, EventCompare > scheduler_
Event scheduler used for ordering events.
Definition Scenario.h:559
constexpr int64_t simulation() const
Simulation number.
Definition Scenario.h:203
constexpr DurationSize startTime() const
Simulation start time.
Definition Scenario.h:219
constexpr bool isAtNight(const DurationSize time) const
Whether or not time is after sunrise and before sunset.
Definition Scenario.h:302
static MathSize minimumFfmcAtNight() noexcept
Minimum Fine Fuel Moisture Code required for spread during the night.
Definition Settings.cpp:724
static bool deterministic() noexcept
Whether or not to run deterministically (100% chance of spread & survival)
Definition Settings.cpp:616
static MathSize minimumFfmc() noexcept
Minimum Fine Fuel Moisture Code required for spread during the day.
Definition Settings.cpp:720
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
Deleter for IObserver to get around incomplete class with unique_ptr.
Definition Scenario.h:30
Definition util.h:14