forked from ecell/epdp
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBDPropagator.hpp
437 lines (388 loc) · 17.1 KB
/
BDPropagator.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifndef BD_PROPAGATOR_HPP
#define BD_PROPAGATOR_HPP
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/range/size.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/const_iterator.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include "Defs.hpp"
#include "generator.hpp"
#include "exceptions.hpp"
#include "freeFunctions.hpp"
#include "utils.hpp"
#include "utils/random.hpp"
#include "utils/get_default_impl.hpp"
#include "Logger.hpp"
template<typename Ttraits_>
class BDPropagator
{
public:
typedef Ttraits_ traits_type;
typedef typename Ttraits_::world_type::particle_container_type particle_container_type;
typedef typename particle_container_type::species_id_type species_id_type;
typedef typename particle_container_type::position_type position_type;
typedef typename particle_container_type::particle_shape_type particle_shape_type;
typedef typename particle_container_type::species_type species_type;
typedef typename particle_container_type::length_type length_type;
typedef typename particle_container_type::particle_id_type particle_id_type;
typedef typename particle_container_type::particle_type particle_type;
typedef typename particle_container_type::particle_id_pair particle_id_pair;
typedef std::vector<particle_id_type> particle_id_vector_type;
typedef typename particle_container_type::particle_id_pair_generator particle_id_pair_generator;
typedef typename particle_container_type::particle_id_pair_and_distance particle_id_pair_and_distance;
typedef typename particle_container_type::particle_id_pair_and_distance_list particle_id_pair_and_distance_list;
typedef typename particle_container_type::structure_type structure_type;
typedef typename traits_type::world_type::traits_type::rng_type rng_type;
typedef typename traits_type::time_type time_type;
typedef typename traits_type::network_rules_type network_rules_type;
typedef typename network_rules_type::reaction_rules reaction_rules;
typedef typename network_rules_type::reaction_rule_type reaction_rule_type;
typedef typename traits_type::reaction_record_type reaction_record_type;
typedef typename traits_type::reaction_recorder_type reaction_recorder_type;
typedef typename traits_type::volume_clearer_type volume_clearer_type;
public:
template<typename Trange_>
BDPropagator(
particle_container_type& tx, network_rules_type const& rules,
rng_type& rng, time_type dt, int max_retry_count,
reaction_recorder_type* rrec, volume_clearer_type* vc,
Trange_ const& particles)
: tx_(tx), rules_(rules), rng_(rng), dt_(dt),
max_retry_count_(max_retry_count), rrec_(rrec), vc_(vc),
queue_(), rejected_move_count_(0)
{
call_with_size_if_randomly_accessible(
boost::bind(&particle_id_vector_type::reserve, &queue_, _1),
particles);
for (typename boost::range_const_iterator<Trange_>::type
i(boost::begin(particles)),
e(boost::end(particles)); i != e; ++i)
{
queue_.push_back(*i);
}
shuffle(rng, queue_);
}
bool operator()()
{
if (queue_.empty())
return false;
particle_id_type pid(queue_.back());
queue_.pop_back();
particle_id_pair pp(tx_.get_particle(pid));
LOG_DEBUG(("propagating particle %s", boost::lexical_cast<std::string>(pp.first).c_str()));
try
{
if (attempt_reaction(pp))
return true;
}
catch (propagation_error const& reason)
{
log_.info("first-order reaction rejected (reason: %s)", reason.what());
++rejected_move_count_;
return true;
}
const species_type species(tx_.get_species(pp.second.sid()));
if (species.D() == 0.)
return true;
position_type const displacement(drawR_free(pp));
position_type const new_pos(
tx_.apply_boundary(
add(pp.second.position(), displacement)));
particle_id_pair particle_to_update(
pp.first, particle_type(species.id(),
particle_shape_type(new_pos, species.radius()),
pp.second.structure_id(), species.D()));
boost::scoped_ptr<particle_id_pair_and_distance_list> overlapped(
tx_.check_overlap(particle_to_update.second.shape(),
particle_to_update.first));
switch (overlapped ? overlapped->size(): 0)
{
case 0:
break;
case 1:
{
particle_id_pair_and_distance const& closest(overlapped->at(0));
try
{
if (!attempt_reaction(pp, closest.first))
{
LOG_DEBUG(("collision with a nonreactive particle %s. move rejected", boost::lexical_cast<std::string>(closest.first.first).c_str()));
++rejected_move_count_;
}
}
catch (propagation_error const& reason)
{
log_.info("second-order reaction rejected (reason: %s)", reason.what());
++rejected_move_count_;
}
}
/* reject the move even if the reaction has not occurred */
return true;
default:
log_.info("collision involving two or more particles; move rejected");
++rejected_move_count_;
return true;
}
if (vc_)
{
if (!(*vc_)(particle_to_update.second.shape(),
particle_to_update.first))
{
log_.info("propagation move rejected.");
return true;
}
}
tx_.update_particle(particle_to_update);
return true;
}
std::size_t get_rejected_move_count() const
{
return rejected_move_count_;
}
private:
position_type drawR_free(particle_id_pair const& pp)
{
species_type const species(tx_.get_species( pp.second.sid()) );
return tx_.get_structure(pp.second.structure_id())->bd_displacement(species.v() * dt_, std::sqrt(2.0 * species.D() * dt_), rng_);
}
bool attempt_reaction(particle_id_pair const& pp)
{
reaction_rules const& rules(rules_.query_reaction_rule(pp.second.sid()));
if (::size(rules) == 0)
{
return false;
}
const Real rnd(rng_() / dt_);
Real prob = 0.;
for (typename boost::range_const_iterator<reaction_rules>::type
i(boost::begin(rules)), e(boost::end(rules)); i != e; ++i)
{
reaction_rule_type const& r(*i);
prob += r.k();
if (prob > rnd)
{
typename reaction_rule_type::species_id_range products(
r.get_products());
switch (::size(products))
{
case 0:
remove_particle(pp.first);
break;
case 1:
{
const species_type s0(tx_.get_species(products[0]));
const particle_id_pair new_p(
pp.first, particle_type(products[0],
particle_shape_type(pp.second.position(),
s0.radius()),
pp.second.structure_id(),
s0.D()));
boost::scoped_ptr<particle_id_pair_and_distance_list> overlapped(tx_.check_overlap(new_p.second.shape(), new_p.first));
if (overlapped && overlapped->size() > 0)
{
throw propagation_error("no space");
}
if (vc_)
{
if (!(*vc_)(new_p.second.shape(), pp.first))
{
throw propagation_error("no space");
}
}
tx_.update_particle(new_p);
if (rrec_)
{
(*rrec_)(
reaction_record_type(
r.id(), array_gen(new_p.first), pp.first));
}
}
break;
case 2:
{
const species_type s0(tx_.get_species(products[0])),
s1(tx_.get_species(products[1]));
const Real D01(s0.D() + s1.D());
const length_type r01(s0.radius() + s1.radius());
int i = max_retry_count_;
position_type np0, np1;
for (;;)
{
if (--i < 0)
{
throw propagation_error("no space");
}
boost::shared_ptr<structure_type> pp_structure(
tx_.get_structure( pp.second.structure_id()) );
position_type m( pp_structure->
dissociation_vector( rng_, r01, dt_, D01, s0.v() - s1.v() ) );
np0 = tx_.apply_boundary(pp.second.position()
- m * (s0.D() / D01));
np1 = tx_.apply_boundary(pp.second.position()
+ m * (s1.D() / D01));
boost::scoped_ptr<particle_id_pair_and_distance_list> overlapped_s0(
tx_.check_overlap(
particle_shape_type(np0, s0.radius()),
pp.first));
boost::scoped_ptr<particle_id_pair_and_distance_list> overlapped_s1(
tx_.check_overlap(
particle_shape_type(np1, s1.radius()),
pp.first));
if (!(overlapped_s0 && overlapped_s0->size() > 0) && !(overlapped_s1 && overlapped_s1->size() > 0))
break;
}
if (vc_)
{
if (!(*vc_)(particle_shape_type(np0, s0.radius()), pp.first) || !(*vc_)(particle_shape_type(np1, s1.radius()), pp.first))
{
throw propagation_error("no space");
}
}
tx_.remove_particle(pp.first);
const particle_id_pair
npp0(tx_.new_particle(s0.id(), pp.second.structure_id(), np0)),
npp1(tx_.new_particle(s1.id(), pp.second.structure_id(), np1));
if (rrec_)
{
(*rrec_)(
reaction_record_type(
r.id(),
array_gen(npp0.first, npp1.first),
pp.first));
}
}
break;
default:
throw not_implemented("monomolecular reactions that produce more than two products are not supported");
}
return true;
}
}
return false;
}
bool attempt_reaction(particle_id_pair const& pp0, particle_id_pair const& pp1)
{
reaction_rules const& rules(rules_.query_reaction_rule(pp0.second.sid(), pp1.second.sid()));
if (::size(rules) == 0)
{
return false;
}
const species_type s0(tx_.get_species(pp0.second.sid())),
s1(tx_.get_species(pp1.second.sid()));
length_type r01(s0.radius() + s1.radius());
const Real rnd(rng_());
Real prob = 0;
for (typename boost::range_const_iterator<reaction_rules>::type
i(boost::begin(rules)), e(boost::end(rules)); i != e; ++i)
{
reaction_rule_type const& r(*i);
boost::shared_ptr<structure_type> pp_structure(
tx_.get_structure( pp0.second.structure_id()) ); // we assume that both particles live on the same surface
const Real p(pp_structure->p_acceptance( r.k(), dt_, r01, subtract( pp1.second.position(),
pp0.second.position() ), s0.D(), s1.D(), s0.v(), s1.v() ));
BOOST_ASSERT(p >= 0.);
prob += p;
if (prob >= 1.)
{
throw propagation_error(
"invalid acceptance ratio ("
+ boost::lexical_cast<std::string>(p)
+ ") for reaction rate "
+ boost::lexical_cast<std::string>(r.k())
+ ".");
}
if (prob > rnd)
{
LOG_DEBUG(("fire reaction"));
const typename reaction_rule_type::species_id_range products(
r.get_products());
switch (::size(products))
{
case 1:
// If the number or product of the reaction is one.
{
const species_id_type product(products[0]);
const species_type sp(tx_.get_species(product));
const position_type new_pos(
tx_.apply_boundary(
divide(
add(multiply(pp0.second.position(), s1.D()),
multiply(tx_.cyclic_transpose(
pp1.second.position(),
pp0.second.position()), s0.D())),
(s0.D() + s1.D()))));
boost::scoped_ptr<particle_id_pair_and_distance_list> overlapped(
tx_.check_overlap(particle_shape_type(new_pos, sp.radius()),
pp0.first, pp1.first));
if (overlapped && overlapped->size() > 0)
{
throw propagation_error("no space");
}
if (vc_)
{
if (!(*vc_)(
particle_shape_type(new_pos, sp.radius()),
pp0.first, pp1.first))
{
throw propagation_error("no space");
}
}
remove_particle(pp0.first);
remove_particle(pp1.first);
particle_id_pair npp(tx_.new_particle(product, pp0.second.structure_id(), new_pos));
if (rrec_)
{
(*rrec_)(
reaction_record_type(
r.id(), array_gen(npp.first), pp0.first, pp1.first));
}
break;
}
case 0:
// If the two particles annihilate eachother. Just remove the particles.
remove_particle(pp0.first);
remove_particle(pp1.first);
break;
default:
throw not_implemented("bimolecular reactions that produce more than one product are not supported");
}
return true;
}
}
return false;
}
void remove_particle(particle_id_type const& pid)
{
LOG_DEBUG(("remove particle %s", boost::lexical_cast<std::string>(pid).c_str()));
tx_.remove_particle(pid);
typename particle_id_vector_type::iterator i(
std::find(queue_.begin(), queue_.end(), pid));
if (queue_.end() != i)
queue_.erase(i);
}
private:
position_type random_unit_vector()
{
position_type v(rng_() - .5, rng_() - .5, rng_() - .5);
return v / length(v);
}
private:
particle_container_type& tx_;
network_rules_type const& rules_;
rng_type& rng_;
Real const dt_;
int const max_retry_count_;
reaction_recorder_type* const rrec_;
volume_clearer_type* const vc_;
particle_id_vector_type queue_;
int rejected_move_count_;
static Logger& log_;
};
template<typename Ttraits_>
Logger& BDPropagator<Ttraits_>::log_(Logger::get_logger("ecell.BDPropagator"));
#endif /* BD_PROPAGATOR_HPP */