Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
History.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __HISTORY_H_INCLUDED__
10#define __HISTORY_H_INCLUDED__
11
12#include "HistoryBase.hpp"
13#include "demographic_policy.hpp"
14
15namespace quetzal
16{
17namespace demography
18{
28template <typename Space, typename DispersalPolicy, typename MemoryPolicy>
29class History : public HistoryBase<Space, DispersalPolicy, MemoryPolicy>
30{
31};
32
40template <typename Space, typename Memory>
41class History<Space, demographic_policy::individual_based, Memory>
42 : public HistoryBase<Space, demographic_policy::individual_based, Memory>
43{
44 // Using the HistoryBase class constructor
45 using HistoryBase<Space, demographic_policy::individual_based, Memory>::HistoryBase;
46
47 public:
67 template <typename T, typename U, typename V> void simulate_forward(T sim_growth, U kernel, V &gen)
68 {
69 for (unsigned int t = 0; t < this->nb_generations(); ++t)
70 {
71 unsigned int landscape_individuals_count = 0;
72 for (auto x : this->distribution_area(t))
73 {
74 auto N_tilde = sim_growth(gen, x, t);
75 landscape_individuals_count += N_tilde;
76 if (N_tilde >= 1)
77 {
78 for (unsigned int ind = 1; ind <= N_tilde; ++ind)
79 {
80 auto y = kernel(gen, x);
81 this->m_flows->add_to_flow_from_to(x, y, t, 1);
82 this->pop_size(y, t + 1) += 1;
83 }
84 }
85 }
86 if (landscape_individuals_count == 0)
87 {
88 throw std::domain_error("Landscape populations went extinct before sampling");
89 }
90 }
91 }
92};
99template <typename Space, typename Memory>
100class History<Space, demographic_policy::mass_based, Memory>
101 : public HistoryBase<Space, demographic_policy::mass_based, Memory>
102{
103 // Using the HistoryBase constructor
104 using HistoryBase<Space, demographic_policy::mass_based, Memory>::HistoryBase;
105
106 public:
127 template <typename T, typename U, typename V> void simulate_forward(T sim_growth, U kernel, V &gen)
128 {
129 for (unsigned int t = 0; t < this->nb_generations(); ++t)
130 {
131 unsigned int landscape_individuals_count = 0;
132 for (auto x : this->distribution_area(t))
133 {
134 auto N_tilde = sim_growth(gen, x, t);
135 for (auto const &y : kernel.arrival_space(x))
136 {
137 auto m = kernel(x, y);
138 assert(m >= 0.0 && m <= 1.0);
139 // Use of std::ceil to allow oversea migration
140 double nb_migrants = std::ceil(m * static_cast<double>(N_tilde));
141 if (nb_migrants >= 0)
142 {
143 landscape_individuals_count += nb_migrants;
144 this->m_flows->set_flow_from_to(x, y, t, nb_migrants);
145 this->pop_size(y, t + 1) += nb_migrants;
146 }
147 }
148 }
149 if (landscape_individuals_count == 0)
150 {
151 throw std::domain_error("Landscape populations went extinct before sampling");
152 }
153 }
154 }
155};
156} // namespace demography
157} // namespace quetzal
158
159#endif
Base class for simulating and recording spatial population history, encpasulating backward migration ...
Definition HistoryBase.hpp:40
auto & pop_size(coord_type const &x, time_type const &t)
Population size at deme x at time t.
Definition HistoryBase.hpp:83
auto distribution_area(time_type t) const
Retrieve demes where N > 0 at time t.
Definition HistoryBase.hpp:74
unsigned int const & nb_generations() const
Last time recorded in the foward-in-time database history.
Definition HistoryBase.hpp:112
void simulate_forward(T sim_growth, U kernel, V &gen)
Expands the demographic database.
Definition History.hpp:67
void simulate_forward(T sim_growth, U kernel, V &gen)
Simulate forward the demographic history.
Definition History.hpp:127
Unspecialized class (CRTP design pattern)
Definition History.hpp:30
Policy class to specialize the quetzal::demography::History class for expansion of populations with v...
Definition demographic_policy.hpp:40
Policy class to specialize the quetzal::demography::History class for simulation of population with l...
Definition demographic_policy.hpp:207
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21