forked from ecell/epdp
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBDSimulator.hpp
266 lines (226 loc) · 9 KB
/
BDSimulator.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
#ifndef BD_SIMULATOR_HPP
#define BD_SIMULATOR_HPP
#include <algorithm>
#include <limits>
#include <boost/foreach.hpp>
#include "NetworkRules.hpp"
#include "newBDPropagator.hpp"
#include "World.hpp"
#include "ParticleSimulator.hpp"
#include "utils/pair.hpp"
template<typename Tworld_>
struct BDSimulatorTraitsBase: public ParticleSimulatorTraitsBase<Tworld_>
{
};
template<typename Ttraits_>
class BDSimulator: public ParticleSimulator<Ttraits_>
{
public:
typedef Ttraits_ traits_type;
typedef ParticleSimulator<Ttraits_> base_type;
typedef typename traits_type::world_type world_type;
typedef typename world_type::traits_type::rng_type rng_type;
typedef typename world_type::species_id_type species_id_type;
typedef typename world_type::species_type species_type;
typedef typename world_type::length_type length_type;
typedef typename world_type::structure_type structure_type;
typedef typename traits_type::time_type time_type;
typedef typename traits_type::network_rules_type network_rules_type;
typedef typename traits_type::reaction_rule_type reaction_rule_type;
typedef typename network_rules_type::reaction_rules reaction_rules;
typedef typename traits_type::rate_type rate_type;
typedef typename traits_type::reaction_record_type reaction_record_type;
typedef typename traits_type::reaction_recorder_type reaction_recorder_type;
typedef std::pair<const Real, const Real> real_pair;
public:
Real const& dt_factor()
{
return dt_factor_;
}
virtual ~BDSimulator() {}
BDSimulator(boost::shared_ptr<world_type> world,
boost::shared_ptr<network_rules_type const> network_rules,
rng_type& rng, Real dt_factor = .1,
int dissociation_retry_moves = 1, length_type reaction_length_factor = traits_type::MULTI_SHELL_FACTOR)
: base_type(world, network_rules, rng),
dt_factor_(dt_factor), num_retries_(dissociation_retry_moves),
reaction_length_factor_(reaction_length_factor)
{
calculate_dt_and_reaction_length();
}
void calculate_dt_and_reaction_length()
{
//base_type::dt_ = dt_factor_ * determine_dt(*base_type::world_);
base_type::dt_ = determine_dt();
reaction_length_ = determine_reaction_length();
log_.info( "dt=%e, reaction length=%e", base_type::dt_, reaction_length_ );
}
virtual void step()
{
_step(base_type::dt_);
}
virtual bool step(time_type upto)
{
time_type const lt(upto - base_type::t_);
if (lt <= 0.)
return false;
if (base_type::dt_ < lt)
{
_step(base_type::dt_);
}
else
{
_step(lt);
base_type::t_ = upto;
}
return true;
}
static Real determine_dt(world_type const& world)
{
Real D_max(0.), radius_min(std::numeric_limits<Real>::max());
BOOST_FOREACH(species_type s, world.get_species())
{
if (D_max < s.D())
D_max = s.D();
if (radius_min > s.radius())
radius_min = s.radius();
}
return gsl_pow_2(radius_min * 2) / (D_max * 2);
}
Real determine_dt()
{
std::pair<const Real, const Real> maxD_minr( maxD_minsigma() );
const Real k_max( get_max_rate() );
const Real D_max( maxD_minr.first );
const Real r_min( maxD_minr.second );
const Real Pacc_max( 0.01 ); //Maximum value of the acceptance probability.
const Real tau_D( 2 * r_min * r_min / D_max ); //~step over particle - time.
Real dt;
if( k_max > 0)
{
Real dt_temp( 2 * Pacc_max * reaction_length_factor_ * r_min / k_max );
dt = std::min( dt_temp, dt_factor_ * tau_D ); // dt_factor * tau_D is upper limit of dt.
}
else
dt = dt_factor_ * tau_D;
return dt;
}
length_type determine_reaction_length()
{
real_pair maxD_minr( maxD_minsigma() );
return reaction_length_factor_ * maxD_minr.second;
}
/* Returns largest diffusion constant and smallest particle radius in the multi. */
real_pair maxD_minsigma()
{
Real D_max(0.), radius_min(std::numeric_limits<Real>::max());
BOOST_FOREACH(species_type s, base_type::world_->get_species())
{
if (D_max < s.D())
D_max = s.D();
if (radius_min > s.radius())
radius_min = s.radius();
}
return real_pair(D_max, radius_min);
}
/* Functions returns largest _1D_ intrinsic reaction rate in the world. */
Real get_max_rate()
{
Real k_max(0.);
int i = 0, j= 0;
BOOST_FOREACH(species_type s, base_type::world_->get_species())
{
if( s.structure_type_id() != base_type::world_->get_def_structure_type_id() )
continue;
BOOST_FOREACH(boost::shared_ptr<structure_type> structure, base_type::world_->get_structures())
{
species_id_type const strc_sid( structure->sid() );
reaction_rules const& rrules((*base_type::network_rules_).query_reaction_rule( s.id(), strc_sid ));
if (::size(rrules) == 0)
continue;
for (typename boost::range_const_iterator<reaction_rules>::type
it(boost::begin(rrules)), e(boost::end(rrules)); it != e; ++it)
{
Real const k( structure->get_1D_rate_surface( (*it).k(), s.radius() ) );
if ( k_max < k )
k_max = k;
}
}
}
//Since surface rates are not devided by 2 to compensate for double reaction attempts.
k_max *= 2.0;
BOOST_FOREACH(species_type s0, base_type::world_->get_species())
{
j = 0;
BOOST_FOREACH(species_type s1, base_type::world_->get_species())
{
if( j++ < i )
continue;
reaction_rules const& rrules((*base_type::network_rules_).query_reaction_rule( s0.id(), s1.id() ));
if (::size(rrules) == 0)
{
continue;
}
for (typename boost::range_const_iterator<reaction_rules>::type
it(boost::begin(rrules)), e(boost::end(rrules)); it != e; ++it)
{
// const length_type r01( s0.radius() + s1.radius() ); // TODO
Real k;
if(s0.structure_type_id() != s1.structure_type_id())
{
if(s0.structure_type_id() == base_type::world_->get_def_structure_type_id())
k = 0.001; //TODO k = base_type::world_->get_structure( s0.structure_type_id() )->get_1D_rate_geminate( (*it).k(), r01 );
else
k = 0.001; //TODO k = base_type::world_->get_structure( s1.structure_type_id() )->get_1D_rate_geminate( (*it).k(), r01 );
}
else
{
k = 0.001; //TODO k = base_type::world_->get_structure( s0.structure_id() )->get_1D_rate_geminate( (*it).k(), r01 );
}
if ( k_max < k )
k_max = k;
}
}
i++;
}
return k_max;
}
Real get_reaction_length() const
{
return reaction_length_;
}
void set_reaction_length_factor(length_type new_reaction_length_factor, time_type new_dt_factor)
{
dt_factor_ = new_dt_factor;
reaction_length_factor_ = new_reaction_length_factor;
calculate_dt_and_reaction_length();
}
protected:
void _step(time_type dt)
{
{
newBDPropagator<traits_type> propagator(
*base_type::world_,
*base_type::network_rules_,
base_type::rng_,
dt, num_retries_, reaction_length_,
base_type::rrec_.get(), 0,
make_select_first_range(base_type::world_->
get_particles_range()));
while (propagator());
LOG_DEBUG(("%d: t=%lg, dt=%lg", base_type::num_steps_,
base_type::t_, dt));
}
++base_type::num_steps_;
base_type::t_ += dt;
}
private:
Real dt_factor_;
int const num_retries_;
length_type reaction_length_factor_;
length_type reaction_length_;
static Logger& log_;
};
template<typename Ttraits_>
Logger& BDSimulator<Ttraits_>::log_(Logger::get_logger("BDSimulator"));
#endif /* BD_SIMULATOR_HPP */