forked from ecell/epdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleContainerBase.hpp
273 lines (232 loc) · 8.98 KB
/
ParticleContainerBase.hpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef PARTICLE_CONTAINER_BASE_HPP
#define PARTICLE_CONTAINER_BASE_HPP
#include "utils/range.hpp"
#include "utils/get_mapper_mf.hpp"
#include "utils/unassignable_adapter.hpp"
#include "MatrixSpace.hpp"
#include "abstract_set.hpp"
#include "generator.hpp"
#include "exceptions.hpp"
#include "ParticleContainer.hpp"
#include "Transaction.hpp"
template<typename Ttraits_>
struct ParticleContainerUtils
{
typedef Ttraits_ traits_type;
typedef typename traits_type::length_type length_type;
typedef typename traits_type::particle_type particle_type;
typedef typename traits_type::particle_id_type particle_id_type;
typedef std::pair<const particle_id_type, particle_type> particle_id_pair;
typedef std::pair<particle_id_pair, length_type> particle_id_pair_and_distance;
typedef unassignable_adapter<particle_id_pair_and_distance, get_default_impl::std::vector> particle_id_pair_and_distance_list;
struct distance_comparator:
public std::binary_function<
typename particle_id_pair_and_distance_list::placeholder,
typename particle_id_pair_and_distance_list::placeholder,
bool>
{
typedef typename particle_id_pair_and_distance_list::placeholder
first_argument_type;
typedef typename particle_id_pair_and_distance_list::const_caster const_caster;
bool operator()(first_argument_type const& lhs,
first_argument_type const& rhs) const
{
return c_(lhs).second < c_(rhs).second;
}
const_caster c_;
};
template<typename Tset_>
struct overlap_checker
{
overlap_checker(Tset_ const& ignore = Tset_()): ignore_(ignore), result_(0) {}
template<typename Titer_>
void operator()(Titer_ const& i, length_type const& dist)
{
if (!contains(ignore_, (*i).first))
{
if (!result_)
{
result_ = new particle_id_pair_and_distance_list();
}
result_->push_back(std::make_pair(*i, dist));
}
}
particle_id_pair_and_distance_list* result() const
{
if (result_)
{
std::sort(result_->pbegin(), result_->pend(), compare_);
}
return result_;
}
private:
Tset_ const& ignore_;
particle_id_pair_and_distance_list* result_;
distance_comparator compare_;
};
};
template<typename Tderived_, typename Ttraits_ = typename Tderived_::traits_type>
class ParticleContainerBase
: public ParticleContainer<Ttraits_>
{
public:
typedef ParticleContainerUtils<Ttraits_> utils;
typedef ParticleContainer<Ttraits_> base_type;
typedef Ttraits_ traits_type;
typedef typename traits_type::length_type length_type;
typedef typename traits_type::species_type species_type;
typedef typename traits_type::position_type position_type;
typedef typename traits_type::particle_type particle_type;
typedef typename traits_type::particle_id_type particle_id_type;
typedef typename traits_type::particle_id_generator particle_id_generator;
typedef typename traits_type::species_id_type species_id_type;
typedef typename traits_type::particle_type::shape_type particle_shape_type;
typedef typename traits_type::size_type size_type;
typedef typename traits_type::structure_id_type structure_id_type;
typedef typename traits_type::structure_type structure_type;
typedef std::pair<const particle_id_type, particle_type> particle_id_pair;
typedef Transaction<traits_type> transaction_type;
typedef MatrixSpace<particle_type, particle_id_type, get_mapper_mf> particle_matrix_type;
typedef abstract_limited_generator<particle_id_pair> particle_id_pair_generator;
typedef std::pair<particle_id_pair, length_type> particle_id_pair_and_distance;
typedef sized_iterator_range<typename particle_matrix_type::const_iterator> particle_id_pair_range;
typedef unassignable_adapter<particle_id_pair_and_distance, get_default_impl::std::vector> particle_id_pair_and_distance_list;
protected:
public:
ParticleContainerBase(length_type world_size, size_type size)
: pmat_(world_size, size) {}
virtual size_type num_particles() const
{
return pmat_.size();
}
virtual length_type world_size() const
{
return pmat_.world_size();
}
length_type cell_size() const
{
return pmat_.cell_size();
}
size_type matrix_size() const
{
return pmat_.matrix_size();
}
template<typename T_>
length_type distance(T_ const& lhs, position_type const& rhs) const
{
return traits_type::distance(lhs, rhs, world_size());
}
virtual length_type distance(position_type const& lhs,
position_type const& rhs) const
{
return traits_type::distance(lhs, rhs, world_size());
}
virtual position_type apply_boundary(position_type const& v) const
{
return traits_type::apply_boundary(v, world_size());
}
virtual length_type apply_boundary(length_type const& v) const
{
return traits_type::apply_boundary(v, world_size());
}
virtual position_type cyclic_transpose(position_type const& p0, position_type const& p1) const
{
return traits_type::cyclic_transpose(p0, p1, world_size());
}
virtual length_type cyclic_transpose(length_type const& p0, length_type const& p1) const
{
return traits_type::cyclic_transpose(p0, p1, world_size());
}
template<typename T1_>
T1_ calculate_pair_CoM(
T1_ const& p1, T1_ const& p2,
typename element_type_of<T1_>::type const& D1,
typename element_type_of<T1_>::type const& D2)
{
typedef typename element_type_of< T1_ >::type element_type;
T1_ retval;
const T1_ p2t(cyclic_transpose(p2, p1));
return modulo(
divide(
add(multiply(p1, D2), multiply(p2t, D1)),
add(D1, D2)),
world_size());
}
virtual particle_id_pair_and_distance_list* check_overlap(particle_shape_type const& s) const
{
return check_overlap<particle_shape_type>(s);
}
virtual particle_id_pair_and_distance_list* check_overlap(particle_shape_type const& s, particle_id_type const& ignore) const
{
return check_overlap(s, array_gen(ignore));
}
virtual particle_id_pair_and_distance_list* check_overlap(particle_shape_type const& s, particle_id_type const& ignore1, particle_id_type const& ignore2) const
{
return check_overlap(s, array_gen(ignore1, ignore2));
}
template<typename Tsph_, typename Tset_>
particle_id_pair_and_distance_list* check_overlap(Tsph_ const& s, Tset_ const& ignore,
typename boost::disable_if<boost::is_same<Tsph_, particle_id_pair> >::type* =0) const
{
typename utils::template overlap_checker<Tset_> oc(ignore);
traits_type::take_neighbor(pmat_, oc, s);
return oc.result();
}
template<typename Tsph_>
particle_id_pair_and_distance_list* check_overlap(Tsph_ const& s,
typename boost::disable_if<boost::is_same<Tsph_, particle_id_pair> >::type* =0) const
{
typename utils::template overlap_checker<boost::array<particle_id_type, 0> > oc;
traits_type::take_neighbor(pmat_, oc, s);
return oc.result();
}
particle_id_pair get_particle(particle_id_type const& id, bool& found) const
{
typename particle_matrix_type::const_iterator i(pmat_.find(id));
if (pmat_.end() == i) {
found = false;
return particle_id_pair();
}
found = true;
return *i;
}
virtual particle_id_pair get_particle(particle_id_type const& id) const
{
typename particle_matrix_type::const_iterator i(pmat_.find(id));
if (pmat_.end() == i) {
throw not_found(std::string("No such particle: id=")
+ boost::lexical_cast<std::string>(id));
}
return *i;
}
virtual bool has_particle(particle_id_type const& id) const
{
return pmat_.end() != pmat_.find(id);
}
virtual transaction_type* create_transaction();
virtual particle_id_pair_generator* get_particles() const
{
return make_range_generator<particle_id_pair>(pmat_);
}
particle_id_pair_range get_particles_range() const
{
return particle_id_pair_range(pmat_.begin(), pmat_.end(), pmat_.size());
}
virtual bool update_particle(particle_id_pair const& pi_pair)
{
return pmat_.update(pi_pair).second;
}
virtual bool remove_particle(particle_id_type const& id)
{
return pmat_.erase(id);
}
protected:
particle_matrix_type pmat_;
};
template<typename Tderived_, typename Ttraits_>
inline Transaction<Ttraits_>*
ParticleContainerBase<Tderived_, Ttraits_>::create_transaction()
{
return new TransactionImpl<ParticleContainerBase>(*this);
}
#endif /* PARTICLE_CONTAINER_BASE_HPP */