Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
utils.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 __SIMULATORS_UTILS_H_INCLUDED__
10#define __SIMULATORS_UTILS_H_INCLUDED__
11
12#include "../coalescence/container/Forest.hpp"
13#include "../coalescence/merger_policy.hpp"
14#include "../demography/History.hpp"
15
16#include <map>
17
18namespace quetzal
19{
20
21template <typename Env, typename F> struct neighbor_migration
22{
23
24 using coord_type = typename Env::coord_type;
25 using env_type = Env;
26 using F_type = F;
27
28 env_type const &_landscape;
29 F_type _f;
30
31 neighbor_migration(env_type const &env, F f) : _landscape(env), _f(f)
32 {
33 }
34
35 template <typename T> std::vector<coord_type> arrival_space(coord_type const &x, const T &) const
36 {
37 auto v = _landscape.four_nearest_defined_cells(x);
38 v.push_back(x);
39 return v;
40 }
41
42 template <typename T> double operator()(coord_type const &x, coord_type const &y, T const &t) const
43 {
44 auto v = arrival_space(x, t);
45 auto binop = [this, t](auto const &a, auto const &b) { return a + _f(b, t); };
46 double sum = std::accumulate(v.begin(), v.end(), 0.0, binop);
47 return _f(y, t) / sum;
48 }
49};
50
51template <typename Env, typename F> auto make_neighbor_migration(Env const &env, F f)
52{
53 return neighbor_migration<Env, F>(env, f);
54}
55
56#endif
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21