pops-core  0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
model.hpp
Go to the documentation of this file.
1 /*
2  * Tests for the PoPS Model class.
3  *
4  * Copyright (C) 2020-2021 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_MODEL_HPP
25 #define POPS_MODEL_HPP
26 
27 #include "config.hpp"
28 #include "treatments.hpp"
29 #include "spread_rate.hpp"
30 #include "simulation.hpp"
31 #include "kernel.hpp"
32 #include "scheduling.hpp"
33 #include "quarantine.hpp"
34 
35 #include <vector>
36 
37 namespace pops {
38 
39 template<typename IntegerRaster, typename FloatRaster, typename RasterIndex>
40 class Model
41 {
42 protected:
50  unsigned last_index{0};
51 
62  const IntegerRaster& dispersers, const Network<RasterIndex>& network)
63  {
71  config_.shape);
74  dispersers,
79  config_.shape);
84  radial_kernel,
85  deterministic_kernel,
87  network_kernel,
90  return selectable_kernel;
91  }
92 
106  const IntegerRaster& dispersers, const Network<RasterIndex>& network)
107  {
109  config_.ew_res,
110  config_.ns_res,
115  config_.shape);
116  DeterministicDispersalKernel<IntegerRaster> deterministic_kernel(
118  dispersers,
120  config_.ew_res,
121  config_.ns_res,
123  config_.shape);
128  radial_kernel,
129  deterministic_kernel,
131  network_kernel,
134  return selectable_kernel;
135  }
136 
148  const IntegerRaster& dispersers, const Network<RasterIndex>& network)
149  {
151  config_.ew_res,
152  config_.ns_res,
157  config_.shape);
158  DeterministicDispersalKernel<IntegerRaster> deterministic_kernel(
160  dispersers,
162  config_.ew_res,
163  config_.ns_res,
165  config_.shape);
170  radial_kernel,
171  deterministic_kernel,
173  network_kernel,
176  return selectable_kernel;
177  }
178 
179 public:
180  Model(const Config& config)
181  : config_(config),
182  natural_kernel(kernel_type_from_string(config.natural_kernel_type)),
183  anthro_kernel(kernel_type_from_string(config.anthro_kernel_type)),
184  uniform_kernel(config.rows, config.cols),
185  natural_neighbor_kernel(direction_from_string(config.natural_direction)),
186  anthro_neighbor_kernel(direction_from_string(config.anthro_direction)),
187  simulation_(
188  config.random_seed,
189  config.rows,
190  config.cols,
191  model_type_from_string(config.model_type),
192  config.latency_period_steps,
193  config.generate_stochasticity,
194  config.establishment_stochasticity,
195  config.movement_stochasticity)
196  {}
197 
248  void run_step(
249  int step,
250  IntegerRaster& infected,
251  IntegerRaster& susceptible,
252  IntegerRaster& total_populations,
253  IntegerRaster& total_hosts,
254  IntegerRaster& dispersers,
255  IntegerRaster& total_exposed,
256  std::vector<IntegerRaster>& exposed,
257  std::vector<IntegerRaster>& mortality_tracker,
258  IntegerRaster& died,
259  const std::vector<FloatRaster>& temperatures,
260  const FloatRaster& weather_coefficient,
262  IntegerRaster& resistant,
263  std::vector<std::tuple<int, int>>& outside_dispersers, // out
264  SpreadRate<IntegerRaster>& spread_rate, // out
265  QuarantineEscape<IntegerRaster>& quarantine, // out
266  const IntegerRaster& quarantine_areas,
267  const std::vector<std::vector<int>> movements,
268  const Network<RasterIndex>& network,
269  std::vector<std::vector<int>>& suitable_cells)
270  {
271 
272  // removal of dispersers due to lethal temperatures
274  int lethal_step =
276  simulation_.remove(
277  infected,
278  susceptible,
279  temperatures[lethal_step],
281  suitable_cells);
282  }
283  // actual spread
284  if (config_.spread_schedule()[step]) {
285  simulation_.generate(
286  dispersers,
287  infected,
289  weather_coefficient,
291  suitable_cells);
292 
294  create_natural_kernel(dispersers, network),
295  create_anthro_kernel(dispersers, network),
298  auto overpopulation_kernel =
299  create_overpopulation_movement_kernel(dispersers, network);
300 
301  simulation_.disperse_and_infect(
302  step,
303  dispersers,
304  susceptible,
305  exposed,
306  infected,
307  mortality_tracker.back(),
308  total_populations,
309  total_exposed,
310  outside_dispersers,
312  weather_coefficient,
313  dispersal_kernel,
314  suitable_cells,
317  simulation_.move_overpopulated_pests(
318  susceptible,
319  infected,
320  total_hosts,
321  outside_dispersers,
322  overpopulation_kernel,
323  suitable_cells,
326  }
327  if (config_.use_movements) {
328  // to do fix movements to use entire mortality tracker
329  last_index = simulation_.movement(
330  infected,
331  susceptible,
332  mortality_tracker,
333  exposed,
334  resistant,
335  total_hosts,
336  total_exposed,
337  step,
338  last_index,
339  movements,
341  suitable_cells);
342  }
343  }
344  // treatments
345  if (config_.use_treatments) {
346  bool managed = treatments.manage(
347  step,
348  infected,
349  exposed,
350  susceptible,
351  resistant,
352  total_hosts,
353  suitable_cells);
354  if (managed && config_.use_mortality) {
355  // treatments apply to all mortality tracker cohorts
356  for (auto& raster : mortality_tracker) {
357  treatments.manage_mortality(step, raster, suitable_cells);
358  }
359  }
360  }
362  // expectation is that mortality tracker is of length (1/mortality_rate
363  // + mortality_time_lag).
364  // TODO: died.zero(); should be done by the caller if needed, document!
365  simulation_.mortality(
366  infected,
367  total_hosts,
370  died,
371  mortality_tracker,
372  suitable_cells);
373  }
374  // compute spread rate
376  unsigned rates_step =
378  spread_rate.compute_step_spread_rate(infected, rates_step, suitable_cells);
379  }
380  // compute quarantine escape
382  unsigned action_step =
384  quarantine.infection_escape_quarantine(
385  infected, quarantine_areas, action_step, suitable_cells);
386  }
387  }
388 };
389 
390 } // namespace pops
391 
392 #endif // POPS_MODEL_HPP
pops::Model
Definition: model.hpp:40
pops::Model::Model
Model(const Config &config)
Definition: model.hpp:180
pops::Config::weather
bool weather
Definition: config.hpp:55
pops::Model::last_index
unsigned last_index
Definition: model.hpp:50
pops::Config::shape
double shape
Definition: config.hpp:74
pops::Model::anthro_kernel
DispersalKernelType anthro_kernel
Definition: model.hpp:45
pops::kernel_type_from_string
DispersalKernelType kernel_type_from_string(const std::string &text)
Get a corresponding enum value for a string which is a kernel name.
Definition: kernel_types.hpp:76
pops::Treatments::manage_mortality
bool manage_mortality(unsigned current, IntegerRaster &infected, const std::vector< std::vector< int >> &suitable_cells)
Separately manage mortality infected cohorts.
Definition: treatments.hpp:421
pops::SwitchDispersalKernel
Dispersal kernel providing all the radial kernels.
Definition: switch_kernel.hpp:39
pops::Config::anthro_kappa
double anthro_kappa
Definition: config.hpp:73
config.hpp
pops::SpreadRate::compute_step_spread_rate
void compute_step_spread_rate(const Raster &raster, unsigned step, const std::vector< std::vector< int >> &suitable_cells)
Computes spread rate in n, s, e, w directions for certain simulation year based on provided infection...
Definition: spread_rate.hpp:151
pops::Config::use_movements
bool use_movements
Definition: config.hpp:88
pops::Config::ew_res
double ew_res
Definition: config.hpp:42
pops::Model::create_anthro_kernel
SwitchDispersalKernel< IntegerRaster, RasterIndex > create_anthro_kernel(const IntegerRaster &dispersers, const Network< RasterIndex > &network)
Create anthropogenic kernel.
Definition: model.hpp:147
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::DeterministicDispersalKernel
Dispersal kernel for deterministic spread to cell with highest probability of spread.
Definition: deterministic_kernel.hpp:59
pops::QuarantineEscape
Class storing and computing quarantine escap metrics for one simulation.
Definition: quarantine.hpp:45
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::Treatments
Treatments class manages all treatments.
Definition: treatments.hpp:329
pops::Model::config_
Config config_
Definition: model.hpp:43
pops::Config::natural_kappa
double natural_kappa
Definition: config.hpp:64
pops::QuarantineEscape::infection_escape_quarantine
void infection_escape_quarantine(const IntegerRaster &infected, const IntegerRaster &quarantine_areas, unsigned step, const std::vector< std::vector< int >> &suitable_cells)
Computes whether infection in certain step escaped from quarantine areas and if not,...
Definition: quarantine.hpp:162
pops::Simulation
The main class to control the spread simulation.
Definition: simulation.hpp:126
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::Model::anthro_neighbor_kernel
DeterministicNeighborDispersalKernel anthro_neighbor_kernel
Definition: model.hpp:48
pops::Config::reproductive_rate
double reproductive_rate
Definition: config.hpp:56
pops::NetworkDispersalKernel
Dispersal kernel for dispersal over a network.
Definition: network_kernel.hpp:725
kernel.hpp
Main entry point to dispersal kernel functionality.
pops::DispersalKernelType
DispersalKernelType
Type of dispersal kernel.
Definition: kernel_types.hpp:53
pops::Model::natural_neighbor_kernel
DeterministicNeighborDispersalKernel natural_neighbor_kernel
Definition: model.hpp:47
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::natural_direction
std::string natural_direction
Definition: config.hpp:63
pops::Config::use_treatments
bool use_treatments
Definition: config.hpp:76
pops::Model::create_overpopulation_movement_kernel
SwitchDispersalKernel< IntegerRaster, RasterIndex > create_overpopulation_movement_kernel(const IntegerRaster &dispersers, const Network< RasterIndex > &network)
Create overpopulation movement kernel.
Definition: model.hpp:105
pops::SpreadRate
Class storing and computing step spread rate for one simulation.
Definition: spread_rate.hpp:31
pops::Model::create_natural_kernel
SwitchDispersalKernel< IntegerRaster, RasterIndex > create_natural_kernel(const IntegerRaster &dispersers, const Network< RasterIndex > &network)
Create natural kernel.
Definition: model.hpp:61
spread_rate.hpp
pops::Model::natural_kernel
DispersalKernelType natural_kernel
Definition: model.hpp:44
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::simulation_step_to_action_step
unsigned simulation_step_to_action_step(const std::vector< bool > &action_schedule, unsigned step)
Converts simulation step to step of actions.
Definition: scheduling.hpp:373
pops::Config::use_spreadrates
bool use_spreadrates
Definition: config.hpp:93
quarantine.hpp
pops::NaturalAnthropogenicDispersalKernel
Dispersal kernel template for natural and anthropogenic distance dispersal.
Definition: natural_anthropogenic_kernel.hpp:43
pops::Config::use_anthropogenic_kernel
bool use_anthropogenic_kernel
Definition: config.hpp:65
pops::UniformDispersalKernel
Dispersal kernel for random uniform dispersal over the whole landscape.
Definition: uniform_kernel.hpp:34
pops::model_type_from_string
ModelType model_type_from_string(const std::string &text)
Get a corresponding enum value for a string which is a model type name.
Definition: simulation.hpp:71
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::Treatments::manage
bool manage(unsigned current, IntegerRaster &infected, std::vector< IntegerRaster > &exposed, IntegerRaster &susceptible, IntegerRaster &resistant, IntegerRaster &total_hosts, const std::vector< std::vector< int >> &suitable_cells)
Do management if needed.
Definition: treatments.hpp:387
pops::Config::deterministic
bool deterministic
Definition: config.hpp:49
pops::DeterministicNeighborDispersalKernel
Dispersal kernel for deterministic spread to a next cell.
Definition: neighbor_kernel.hpp:35
scheduling.hpp
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::Model::run_step
void run_step(int step, IntegerRaster &infected, IntegerRaster &susceptible, IntegerRaster &total_populations, IntegerRaster &total_hosts, IntegerRaster &dispersers, IntegerRaster &total_exposed, std::vector< IntegerRaster > &exposed, std::vector< IntegerRaster > &mortality_tracker, IntegerRaster &died, const std::vector< FloatRaster > &temperatures, const FloatRaster &weather_coefficient, Treatments< IntegerRaster, FloatRaster > &treatments, IntegerRaster &resistant, std::vector< std::tuple< int, int >> &outside_dispersers, SpreadRate< IntegerRaster > &spread_rate, QuarantineEscape< IntegerRaster > &quarantine, const IntegerRaster &quarantine_areas, const std::vector< std::vector< int >> movements, const Network< RasterIndex > &network, std::vector< std::vector< int >> &suitable_cells)
Run one step of the simulation.
Definition: model.hpp:248
treatments.hpp
pops::Config::network_min_time
double network_min_time
Definition: config.hpp:70
pops::Config::use_mortality
bool use_mortality
Definition: config.hpp:78
pops::direction_from_string
Direction direction_from_string(const std::string &text)
Get a corresponding enum value for a string which direction.
Definition: radial_kernel.hpp:51
pops::Config::leaving_percentage
double leaving_percentage
Definition: config.hpp:98
pops::Model::simulation_
Simulation< IntegerRaster, FloatRaster, RasterIndex > simulation_
Definition: model.hpp:49
pops::Model::uniform_kernel
UniformDispersalKernel uniform_kernel
Definition: model.hpp:46
pops::Config::lethal_temperature
double lethal_temperature
Definition: config.hpp:53
pops::Network
Network structure and algorithms.
Definition: network_kernel.hpp:51
pops::RadialDispersalKernel
Dispersal kernel providing all the radial kernels.
Definition: radial_kernel.hpp:94
simulation.hpp
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::mortality_rate
double mortality_rate
Definition: config.hpp:81
pops::Config::anthro_scale
double anthro_scale
Definition: config.hpp:68