pops-core  0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
config.hpp
Go to the documentation of this file.
1 /*
2  * Tests for the PoPS Config class.
3  *
4  * Copyright (C) 2020 by the authors.
5  *
6  * Authors: Vaclav Petras <wenzeslaus gmail com>
7  *
8  * This file is part of PoPS.
9 
10  * PoPS is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 2 of the License, or
13  * (at your option) any later version.
14 
15  * PoPS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19 
20  * You should have received a copy of the GNU General Public License
21  * along with PoPS. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef POPS_CONFIG_HPP
25 #define POPS_CONFIG_HPP
26 
27 #include "scheduling.hpp"
28 #include "utils.hpp"
29 
30 #include <vector>
31 
32 namespace pops {
33 
34 class Config
35 {
36 public:
37  // Seed
38  int random_seed{0};
39  // Size
40  int rows{0};
41  int cols{0};
42  double ew_res{0};
43  double ns_res{0};
45  // Reduced stochasticity
49  bool deterministic{false};
51  // Temperature
53  double lethal_temperature{-273.15}; // 0 K
55  bool weather{false};
56  double reproductive_rate{0};
57  // SI/SEI
58  std::string model_type;
60  // Kernels
61  std::string natural_kernel_type;
62  double natural_scale{0};
63  std::string natural_direction;
64  double natural_kappa{0};
67  std::string anthro_kernel_type;
68  double anthro_scale{0};
69  std::string anthro_direction;
70  double network_min_time{0};
71  double network_max_time{0};
72  double network_speed{0};
73  double anthro_kappa{0};
74  double shape{1.0};
75  // Treatments
76  bool use_treatments{false};
77  // Mortality
78  bool use_mortality{false};
79  std::string mortality_frequency;
81  double mortality_rate{0};
82  int mortality_time_lag{0}; // Time lag of mortality in mortality steps
83  // Quarantine
84  bool use_quarantine{false};
85  std::string quarantine_frequency;
87  // Movements
88  bool use_movements{false};
89  std::vector<unsigned> movement_schedule;
90  double dispersal_percentage{0.99};
91  std::string output_frequency;
93  bool use_spreadrates{true};
94  std::string spreadrate_frequency;
98  double leaving_percentage{0};
100 
102  {
103  scheduler_ = Scheduler(date_start_, date_end_, step_unit_, step_num_units_);
104  spread_schedule_ =
105  scheduler_.schedule_spread(Season(season_start_month_, season_end_month_));
106  output_schedule_ =
108  if (use_mortality)
109  mortality_schedule_ = schedule_from_string(
112  lethal_schedule_ =
114  if (use_spreadrates)
115  spread_rate_schedule_ = schedule_from_string(
117  if (use_quarantine)
118  quarantine_schedule_ = schedule_from_string(
120  schedules_created_ = true;
121  }
122 
123  const Scheduler& scheduler() const
124  {
125  if (!schedules_created_)
126  throw std::logic_error(
127  "Schedules were not created before calling scheduler()");
128  return scheduler_;
129  }
130 
131  const std::vector<bool>& spread_schedule() const
132  {
133  if (!schedules_created_)
134  throw std::logic_error(
135  "Schedules were not created before calling spread_schedule()");
136  return spread_schedule_;
137  }
138 
139  const std::vector<bool>& mortality_schedule() const
140  {
141  if (!schedules_created_)
142  throw std::logic_error(
143  "Schedules were not created before calling mortality_schedule()");
144  return mortality_schedule_;
145  }
146 
147  const std::vector<bool>& lethal_schedule() const
148  {
150  throw std::logic_error(
151  "lethal_schedule() not available when use_lethal_temperature is false");
152  if (!schedules_created_)
153  throw std::logic_error(
154  "Schedules were not created before calling lethal_schedule()");
155  return lethal_schedule_;
156  }
157 
158  const std::vector<bool>& spread_rate_schedule() const
159  {
160  if (!use_spreadrates)
161  throw std::logic_error(
162  "spread_rate_schedule() not available when use_spreadrates is false");
163  if (!schedules_created_)
164  throw std::logic_error(
165  "Schedules were not created before calling spread_rate_schedule()");
166  return spread_rate_schedule_;
167  }
168 
169  const std::vector<bool>& quarantine_schedule() const
170  {
171  if (!use_quarantine)
172  throw std::logic_error(
173  "quarantine_schedule() not available when use_quarantine is false");
174  if (!schedules_created_)
175  throw std::logic_error(
176  "Schedules were not created before calling quarantine_schedule()");
177  return quarantine_schedule_;
178  }
179 
180  const std::vector<bool>& output_schedule() const
181  {
182  if (!schedules_created_)
183  throw std::logic_error(
184  "Schedules were not created before calling output_schedule()");
185  return output_schedule_;
186  }
187 
189  {
190  if (!schedules_created_)
191  throw std::logic_error(
192  "Schedules were not created before calling num_mortality_steps()");
193  return get_number_of_scheduled_actions(mortality_schedule_);
194  }
195 
196  unsigned num_lethal()
197  {
199  throw std::logic_error(
200  "num_lethal() not available when use_lethal_temperature is false");
201  if (!schedules_created_)
202  throw std::logic_error(
203  "Schedules were not created before calling num_lethal()");
204  return get_number_of_scheduled_actions(lethal_schedule_);
205  }
206 
207  unsigned rate_num_steps()
208  {
209  if (!use_spreadrates)
210  throw std::logic_error(
211  "rate_num_steps() not available when use_spreadrates is false");
212  if (!schedules_created_)
213  throw std::logic_error(
214  "Schedules were not created before calling rate_num_steps()");
215  return get_number_of_scheduled_actions(spread_rate_schedule_);
216  }
217 
219  {
220  if (!use_quarantine)
221  throw std::logic_error(
222  "quarantine_num_steps() not available when use_quarantine is false");
223  if (!schedules_created_)
224  throw std::logic_error(
225  "Schedules were not created before calling quarantine_num_steps()");
226  return get_number_of_scheduled_actions(quarantine_schedule_);
227  }
228 
229  const Date& date_start() const
230  {
231  return date_start_;
232  }
233 
234  template<typename... Args>
235  void set_date_start(Args&&... args)
236  {
237  date_start_ = Date(std::forward<Args>(args)...);
238  }
239 
240  const Date& date_end() const
241  {
242  return date_end_;
243  }
244 
245  template<typename... Args>
246  void set_date_end(Args&&... args)
247  {
248  date_end_ = Date(std::forward<Args>(args)...);
249  }
250 
252  {
253  return step_unit_;
254  }
255 
257  {
258  step_unit_ = step_unit;
259  }
260 
261  void set_step_unit(const std::string& text)
262  {
263  step_unit_ = step_unit_enum_from_string(text);
264  }
265 
266  unsigned step_num_units() const
267  {
268  return step_num_units_;
269  }
270 
272  {
273  step_num_units_ = step_num_units;
274  }
275 
276  // TODO: move to Season?
277  void set_season_start_end_month(int start, int end)
278  {
279  season_start_month_ = start;
280  season_end_month_ = end;
281  }
282 
283  void set_season_start_end_month(const std::string& start, const std::string& end)
284  {
285  season_start_month_ = std::stoi(start);
286  season_end_month_ = std::stoi(end);
287  }
288 
289 private:
290  Date date_start_{"0-01-01"};
291  Date date_end_{"0-01-02"};
292 
293  int season_start_month_{1};
294  int season_end_month_{12};
295 
296  StepUnit step_unit_{StepUnit::Day};
297  unsigned step_num_units_{1};
298 
299  Scheduler scheduler_{date_start_, date_end_, step_unit_, step_num_units_};
300  bool schedules_created_{false};
301 
302  std::vector<bool> spread_schedule_;
303  std::vector<bool> output_schedule_;
304  std::vector<bool> mortality_schedule_;
305  std::vector<bool> lethal_schedule_;
306  std::vector<bool> spread_rate_schedule_;
307  std::vector<bool> quarantine_schedule_;
308 };
309 
310 } // namespace pops
311 
312 #endif // POPS_CONFIG_HPP
pops::Config::num_mortality_steps
unsigned num_mortality_steps()
Definition: config.hpp:188
pops::Date
Representation and manipulation of a date for the simulation.
Definition: date.hpp:32
pops::Config::weather
bool weather
Definition: config.hpp:55
pops::Config::rows
int rows
Definition: config.hpp:40
pops::Config::shape
double shape
Definition: config.hpp:74
pops::Config::set_date_end
void set_date_end(Args &&... args)
Definition: config.hpp:246
pops::Config::step_num_units
unsigned step_num_units() const
Definition: config.hpp:266
pops::Config::lethal_temperature_month
int lethal_temperature_month
Definition: config.hpp:54
utils.hpp
pops::Config::anthro_kappa
double anthro_kappa
Definition: config.hpp:73
pops::Config::set_date_start
void set_date_start(Args &&... args)
Definition: config.hpp:235
pops::Config::model_type
std::string model_type
Definition: config.hpp:58
pops::Config::mortality_frequency_n
unsigned mortality_frequency_n
Definition: config.hpp:80
pops::Config::quarantine_frequency
std::string quarantine_frequency
Definition: config.hpp:85
pops::Config::movement_stochasticity
bool movement_stochasticity
Definition: config.hpp:48
pops::Config::set_step_num_units
void set_step_num_units(unsigned step_num_units)
Definition: config.hpp:271
pops::Config::use_movements
bool use_movements
Definition: config.hpp:88
pops::Config::ew_res
double ew_res
Definition: config.hpp:42
pops::step_unit_enum_from_string
StepUnit step_unit_enum_from_string(const std::string &text)
Get step enum from string.
Definition: scheduling.hpp:79
pops::Config::leaving_scale_coefficient
double leaving_scale_coefficient
Definition: config.hpp:99
pops::Config::spread_schedule
const std::vector< bool > & spread_schedule() const
Definition: config.hpp:131
pops::Config::set_step_unit
void set_step_unit(StepUnit step_unit)
Definition: config.hpp:256
pops::schedule_from_string
std::vector< bool > schedule_from_string(const Scheduler &scheduler, const std::string &frequency, unsigned n=0)
Get output (export) schedule based on frequency string ("year", "month", "week", "day",...
Definition: scheduling.hpp:406
pops::Config::anthro_direction
std::string anthro_direction
Definition: config.hpp:69
pops::Config::movement_schedule
std::vector< unsigned > movement_schedule
Definition: config.hpp:89
pops::Config::set_season_start_end_month
void set_season_start_end_month(int start, int end)
Definition: config.hpp:277
pops::Config::natural_kappa
double natural_kappa
Definition: config.hpp:64
pops::Scheduler::schedule_spread
std::vector< bool > schedule_spread(const Season &season) const
Schedule spread.
Definition: scheduling.hpp:178
pops::Config::lethal_schedule
const std::vector< bool > & lethal_schedule() const
Definition: config.hpp:147
pops::Config::quarantine_schedule
const std::vector< bool > & quarantine_schedule() const
Definition: config.hpp:169
pops::Config::use_overpopulation_movements
bool use_overpopulation_movements
Definition: config.hpp:96
pops::get_number_of_scheduled_actions
unsigned get_number_of_scheduled_actions(const std::vector< bool > &action_schedule)
Returns how many actions are scheduled.
Definition: scheduling.hpp:390
pops::Config::set_step_unit
void set_step_unit(const std::string &text)
Definition: config.hpp:261
pops::Config::establishment_stochasticity
bool establishment_stochasticity
Definition: config.hpp:47
pops::Config::reproductive_rate
double reproductive_rate
Definition: config.hpp:56
pops::Config::mortality_frequency
std::string mortality_frequency
Definition: config.hpp:79
pops::Scheduler
Definition: scheduling.hpp:94
pops::Config::network_speed
double network_speed
Definition: config.hpp:72
pops::Season
Holds beginning and end of a season and decides what is in season.
Definition: date.hpp:473
pops::Config::mortality_schedule
const std::vector< bool > & mortality_schedule() const
Definition: config.hpp:139
pops::Config::establishment_probability
double establishment_probability
Definition: config.hpp:50
pops::Config::scheduler
const Scheduler & scheduler() const
Definition: config.hpp:123
pops::Config::natural_direction
std::string natural_direction
Definition: config.hpp:63
pops::Config::anthro_kernel_type
std::string anthro_kernel_type
Definition: config.hpp:67
pops::Config::use_treatments
bool use_treatments
Definition: config.hpp:76
pops::Config::latency_period_steps
int latency_period_steps
Definition: config.hpp:59
pops::Config::num_lethal
unsigned num_lethal()
Definition: config.hpp:196
pops::Config::set_season_start_end_month
void set_season_start_end_month(const std::string &start, const std::string &end)
Definition: config.hpp:283
pops::Config::bbox
BBox< double > bbox
Definition: config.hpp:44
pops::Config::output_frequency_n
unsigned output_frequency_n
Definition: config.hpp:92
pops::Config::random_seed
int random_seed
Definition: config.hpp:38
pops::Config::spreadrate_frequency_n
unsigned spreadrate_frequency_n
Definition: config.hpp:95
pops::Config::dispersal_percentage
double dispersal_percentage
Definition: config.hpp:90
pops::Config::spread_rate_schedule
const std::vector< bool > & spread_rate_schedule() const
Definition: config.hpp:158
pops::Config
Definition: config.hpp:34
pops
Definition: cauchy_kernel.hpp:25
pops::Config::output_frequency
std::string output_frequency
Definition: config.hpp:91
pops::Config::use_spreadrates
bool use_spreadrates
Definition: config.hpp:93
pops::Config::generate_stochasticity
bool generate_stochasticity
Definition: config.hpp:46
pops::Config::date_end
const Date & date_end() const
Definition: config.hpp:240
pops::Config::use_anthropogenic_kernel
bool use_anthropogenic_kernel
Definition: config.hpp:65
pops::Config::create_schedules
void create_schedules()
Definition: config.hpp:101
pops::Config::percent_natural_dispersal
double percent_natural_dispersal
Definition: config.hpp:66
pops::Config::use_quarantine
bool use_quarantine
Definition: config.hpp:84
pops::Config::output_schedule
const std::vector< bool > & output_schedule() const
Definition: config.hpp:180
pops::Config::deterministic
bool deterministic
Definition: config.hpp:49
scheduling.hpp
pops::Config::quarantine_frequency_n
unsigned quarantine_frequency_n
Definition: config.hpp:86
pops::Config::use_lethal_temperature
bool use_lethal_temperature
Definition: config.hpp:52
pops::Config::ns_res
double ns_res
Definition: config.hpp:43
pops::Config::network_max_time
double network_max_time
Definition: config.hpp:71
pops::Config::spreadrate_frequency
std::string spreadrate_frequency
Definition: config.hpp:94
pops::Config::network_min_time
double network_min_time
Definition: config.hpp:70
pops::Scheduler::schedule_action_yearly
std::vector< bool > schedule_action_yearly(int month, int day) const
Schedule an action at certain date each year, e.g.
Definition: scheduling.hpp:203
pops::Config::use_mortality
bool use_mortality
Definition: config.hpp:78
pops::Config::leaving_percentage
double leaving_percentage
Definition: config.hpp:98
pops::StepUnit
StepUnit
Enum for step unit.
Definition: scheduling.hpp:66
pops::Config::lethal_temperature
double lethal_temperature
Definition: config.hpp:53
pops::StepUnit::Day
@ Day
pops::Config::natural_kernel_type
std::string natural_kernel_type
Definition: config.hpp:61
pops::Config::natural_scale
double natural_scale
Definition: config.hpp:62
pops::Config::overpopulation_percentage
double overpopulation_percentage
Definition: config.hpp:97
pops::Config::mortality_time_lag
int mortality_time_lag
Definition: config.hpp:82
pops::Config::cols
int cols
Definition: config.hpp:41
pops::Config::date_start
const Date & date_start() const
Definition: config.hpp:229
pops::Config::step_unit
StepUnit step_unit() const
Definition: config.hpp:251
pops::Config::rate_num_steps
unsigned rate_num_steps()
Definition: config.hpp:207
pops::Config::mortality_rate
double mortality_rate
Definition: config.hpp:81
BBox< double >
pops::Config::anthro_scale
double anthro_scale
Definition: config.hpp:68
pops::Config::quarantine_num_steps
unsigned quarantine_num_steps()
Definition: config.hpp:218