Repository navigation
Expand file tree
/
Copy pathgrid_tools.hpp
More file actions
253 lines (217 loc) · 12.3 KB
/
Copy pathgrid_tools.hpp
File metadata and controls
253 lines (217 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
#include <numeric>
#include "grid_base.hpp"
#include "tuple_tools.hpp"
namespace gftools {
namespace tools {
/** A tool to extract ArgTypes(types of numbers) from a tuple of grids. */
template <typename ValueType, typename ... > struct GridArgTypeExtractor;
/** A tool to extract point types from a tuple of grids. */
template <typename ValueType, typename ... > struct GridPointExtractor;
/** A tuple of grids type traits. Allows to deduce underlying types of grids (points, values)
and convert between tuples of points, values and arrays of indices. */
template <typename> struct grid_tuple_traits;
template <typename ... GridTypes>
struct grid_tuple_traits<std::tuple<GridTypes...> >
{
constexpr static size_t N = sizeof...(GridTypes);
typedef std::tuple<GridTypes...> grid_tuple_type;
/// Static sequence N-1, N-2, ... 1,0
typedef typename tuple_tools::extra::index_gen<N>::type index_gen;
/// A typedef for a tuple of grid points.
typedef typename GridPointExtractor<std::true_type, std::tuple<GridTypes...> >::point_tuple point_tuple;
/// A typedef for a tuple of grid point values.
typedef typename GridArgTypeExtractor<std::true_type, std::tuple<GridTypes...> >::arg_tuple arg_tuple;
/// A typedef for a set of indices.
typedef std::array<size_t, N> indices;
/// Convert indices to points.
static point_tuple points(indices const& in, const grid_tuple_type& grids) { return points_(index_gen(),in,grids); }
/// Convert indices to args.
static arg_tuple get_args(indices const& in, const grid_tuple_type& grids) { return get_args_(index_gen(),in,grids); }
/// Convert points to indices. grids argument is only for checking
static indices get_indices(point_tuple const& in, const grid_tuple_type& grids) { return get_indices_(index_gen(),in,grids); }
/// Convert points to args. grids argument is only for checking
static arg_tuple get_args(point_tuple const& in, const grid_tuple_type& grids) { return get_args_(index_gen(),in,grids); }
/// Get an array of dimensions of grids.
static indices get_dimensions(const grid_tuple_type& grids) {return dims_(index_gen(), grids); }
/// Get a product of all dimensions of gris.
static int get_total_size(const grid_tuple_type& grids){auto d = get_dimensions(grids); return std::accumulate(d.begin(),d.end(),1,std::multiplies<int>()); }
//template <class F> eval(F &&f, arg_tuple in, const grid_tuple_type& grids);
/// Find a tuple of nearest points to the given tuple of values
static point_tuple find_nearest(arg_tuple const& in, const grid_tuple_type& grids) { return find_nearest_(index_gen(),in,grids); }
/// Shift a tuple of grid::point to the right (if possible)
static point_tuple shift(point_tuple const& in, point_tuple const& shift, const grid_tuple_type& grids) { return shift_(index_gen(), in, shift, grids); }
static point_tuple shift(point_tuple const& in, arg_tuple const& shift, const grid_tuple_type& grids) { return shift_(index_gen(), in, shift, grids); }
static arg_tuple shift(arg_tuple const& in, arg_tuple const& shift, const grid_tuple_type& grids) { return shift_(index_gen(), in, shift, grids); }
static bool is_equal(grid_tuple_type const& g1, grid_tuple_type const& g2) { return is_equal_(index_gen(), g1, g2); }
protected:
template <int...S>
static point_tuple points_(tuple_tools::extra::arg_seq<S...>, indices in, const grid_tuple_type& grids);
template <int...S>
static arg_tuple get_args_(tuple_tools::extra::arg_seq<S...>, indices in, const grid_tuple_type& grids);
template <int...S>
static indices get_indices_(tuple_tools::extra::arg_seq<S...>, point_tuple in, const grid_tuple_type& grids);
template <int...S>
static arg_tuple get_args_(tuple_tools::extra::arg_seq<S...>, point_tuple in, const grid_tuple_type& grids);
template <int...S>
static indices dims_(tuple_tools::extra::arg_seq<S...>, const grid_tuple_type& grids) {return {{ (std::get<S>(grids).size())... }};};
template <int...S>
static point_tuple find_nearest_(tuple_tools::extra::arg_seq<S...>, arg_tuple in, const grid_tuple_type& grids);
template <int...S>
static point_tuple shift_(tuple_tools::extra::arg_seq<S...>, point_tuple in, point_tuple shift, const grid_tuple_type&grids);
template <int...S>
static point_tuple shift_(tuple_tools::extra::arg_seq<S...>, point_tuple in, arg_tuple shift, const grid_tuple_type&grids);
template <int...S>
static arg_tuple shift_(tuple_tools::extra::arg_seq<S...>, arg_tuple in, arg_tuple shift, const grid_tuple_type&grids);
template <int...S>
static bool is_equal_(tuple_tools::extra::arg_seq<S...>, grid_tuple_type const& g1, grid_tuple_type const& g2);
};
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::arg_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::get_args_(typename tuple_tools::extra::arg_seq<S...>, indices in, const grid_tuple_type& grids)
{
#ifndef NDEBUG
if ( indices({{ (std::get<S>(grids).size())... }}) <= indices({{ (std::get<S>(in))... }}))
throw gftools::ex_generic("indices are out of grid bounds");
#endif
return std::make_tuple((std::get<S>(grids)[std::get<S>(in)].value())...);
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::arg_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::get_args_(typename tuple_tools::extra::arg_seq<S...>, point_tuple in, const grid_tuple_type& grids)
{
#ifndef NDEBUG
if ( indices({{ (std::get<S>(grids).size())... }}) <= indices({{ (std::get<S>(in).index())... }}))
throw gftools::ex_generic("indices are out of grid bounds");
#endif
return std::make_tuple((std::get<S>(in).value())...);
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::point_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::points_(typename tuple_tools::extra::arg_seq<S...>, indices in, const grid_tuple_type& grids)
{
#ifndef NDEBUG
if ( indices({{ (std::get<S>(grids).size())... }}) <= indices({{ (std::get<S>(in))... }}))
throw gftools::ex_generic("indices are out of grid bounds");
#endif
return std::make_tuple((std::get<S>(grids)[std::get<S>(in)])...);
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::indices
grid_tuple_traits<std::tuple<GridTypes...>>::get_indices_(typename tuple_tools::extra::arg_seq<S...>, point_tuple in, const grid_tuple_type& grids)
{
#ifndef NDEBUG
if ( indices({{ (std::get<S>(grids).size())... }}) <= indices({{ (std::get<S>(in).index())... }}))
throw gftools::ex_generic("indices are out of grid bounds");
if ( indices({{ (std::get<S>(grids)[std::get<S>(in).index()].index()) ... }}) != indices({{ (std::get<S>(in).index())... }}))
throw gftools::ex_generic("index mismatch");
#endif
return {{ (std::get<S>(in).index())... }};
};
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::point_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::find_nearest_(typename tuple_tools::extra::arg_seq<S...>, arg_tuple in, const grid_tuple_type& grids)
{
point_tuple out = std::make_tuple( (std::get<S>(grids).find_nearest(std::get<S>(in)))... );
return out;
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::point_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::shift_(tuple_tools::extra::arg_seq<S...>, point_tuple in, point_tuple shift, const grid_tuple_type&grids)
{
point_tuple out = std::make_tuple( (std::get<S>(grids).shift(std::get<S>(in),std::get<S>(shift)))... );
return out;
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::point_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::shift_(tuple_tools::extra::arg_seq<S...>, point_tuple in, arg_tuple shift, const grid_tuple_type&grids)
{
point_tuple out = std::make_tuple( (std::get<S>(grids).shift(std::get<S>(in),std::get<S>(shift)))... );
return out;
}
template <typename ...GridTypes>
template <int...S>
inline typename grid_tuple_traits<std::tuple<GridTypes...>>::arg_tuple
grid_tuple_traits<std::tuple<GridTypes...>>::shift_(tuple_tools::extra::arg_seq<S...>, arg_tuple in, arg_tuple shift, const grid_tuple_type&grids)
{
arg_tuple out = std::make_tuple( (std::get<S>(grids).shift(std::get<S>(in),std::get<S>(shift)))... );
return out;
}
template <typename ...GridTypes>
template <int...S>
inline bool grid_tuple_traits<std::tuple<GridTypes...>>::is_equal_(
tuple_tools::extra::arg_seq<S...>, grid_tuple_type const& g1, grid_tuple_type const& g2)
{
std::array<bool, N> arr = {{ std::get<S>(g1) == std::get<S>(g2) ... }};
bool ok = true;
for (int i=0; i<N && ok; i++) { ok = ok && arr[i]; }
return ok;
}
//impl
template <typename ValueType, template <typename ...> class T, typename GridType1, typename ...GridTypes, typename ...ArgTypes>
struct GridArgTypeExtractor<ValueType, T<GridType1, GridTypes...>, ArgTypes...> :
GridArgTypeExtractor<ValueType, T<GridTypes...>, ArgTypes..., typename GridType1::point::value_type>
{
};
template <typename ValueType, template <typename ...> class T, typename GridType1, typename ...ArgTypes>
struct GridArgTypeExtractor<ValueType, T<GridType1>, ArgTypes...> {
typedef std::function<ValueType(ArgTypes...,typename GridType1::point::value_type)> f_type;
typedef std::tuple<ArgTypes...,typename GridType1::point::value_type> arg_tuple;
};
template <typename ValueType, template <typename ...> class T, typename GridType1, typename ...GridTypes, typename ...ArgTypes>
struct GridPointExtractor<ValueType, T<GridType1, GridTypes...>, ArgTypes...> :
GridPointExtractor<ValueType, T<GridTypes...>, ArgTypes...,typename GridType1::point>
{
};
template <typename ValueType, template <typename ...> class T, typename GridType1, typename ...ArgTypes>
struct GridPointExtractor<ValueType, T<GridType1>, ArgTypes...> {
typedef std::function<ValueType(ArgTypes...,typename GridType1::point)> f_type;
typedef std::tuple<ArgTypes...,typename GridType1::point> point_tuple;
};
// obsolete
/** A tool to recursiverly integrate over a grid. */
template <typename GridType, class Obj> struct RecursiveGridIntegrator;
/* Integrate a function over a grid. */
template <typename GridType, typename ValueType, typename ArgType1>
struct RecursiveGridIntegrator<GridType, ValueType(ArgType1)>
{
inline static ValueType integrate(const GridType& grid, const std::function<ValueType(ArgType1)>& in){
//DEBUG("Using this method, N=1 with "<< sizeof...(OtherArgs) << " other args" );
return grid.integrate(in);
}
};
/** An alias for a std::function template type. */
template <typename GridType, typename ValueType, typename ArgType1>
struct RecursiveGridIntegrator<GridType, std::function<ValueType(ArgType1)>>:RecursiveGridIntegrator<GridType, ValueType(ArgType1)>{};
/** Multi-dimensional integration over the same grid. */
template <typename GridType, typename ValueType, typename ArgType1, typename ...ArgTypes>
struct RecursiveGridIntegrator<GridType, ValueType(ArgType1, ArgTypes...)>
{
typedef std::function<ValueType(ArgTypes...)> type;
inline static ValueType integrate(
const GridType& grid,
const std::function<ValueType(ArgType1, ArgTypes...)>& in)
{
//DEBUG("Using this method, N!=1");
auto f1 = [&](const ArgType1& arg1)->ValueType {
//DEBUG(arg1);
std::function<ValueType(ArgTypes...)> f0 = [&](const ArgTypes&... args){return in(arg1,args...);};
return RecursiveGridIntegrator<GridType, ValueType(ArgTypes...)>::integrate(grid, f0);
};
//DEBUG(f1(0.0));
return grid.integrate(f1);
}
};
/** An alias for a std::function template type. */
template <typename GridType, typename ValueType, typename ArgType1, typename ...ArgTypes>
struct RecursiveGridIntegrator<GridType, std::function<ValueType(ArgType1, ArgTypes...)>>:
RecursiveGridIntegrator<GridType, ValueType(ArgType1, ArgTypes...)> {};
} // end of namespace extra
} // end of namespace gftools