forked from gfrd/egfrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkRulesWrapper.hpp
129 lines (122 loc) · 5.04 KB
/
NetworkRulesWrapper.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
#ifndef NETWORK_RULES_WRAPPER_HPP
#define NETWORK_RULES_WRAPPER_HPP
#include <map>
#include <vector>
#include <boost/scoped_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include "twofold_container.hpp"
#include "utils/range.hpp"
#include "ReactionRuleInfo.hpp"
#include "generator.hpp"
template<typename T_, typename Trri_>
class NetworkRulesWrapper
{
public:
typedef T_ backend_type;
typedef Trri_ reaction_rule_type;
typedef typename reaction_rule_type::species_id_type species_id_type;
typedef std::vector<reaction_rule_type> reaction_rule_vector;
typedef reaction_rule_vector reaction_rules;
typedef std::map<species_id_type, reaction_rule_vector> first_order_reaction_rule_vector_map;
typedef std::map<std::pair<species_id_type, species_id_type>, reaction_rule_vector> second_order_reaction_rule_vector_map;
public:
reaction_rule_vector const& query_reaction_rule(species_id_type const& r1) const
{
typename first_order_reaction_rule_vector_map::const_iterator i(
first_order_cache_.find(r1));
if (i == first_order_cache_.end())
{
std::pair<
typename first_order_reaction_rule_vector_map::iterator,
bool> x(first_order_cache_.insert(
std::make_pair(r1, reaction_rule_vector())));
boost::scoped_ptr<typename backend_type::reaction_rule_generator>
gen(backend_.query_reaction_rule(r1));
if (gen)
{
while (::valid(*gen))
{
typename backend_type::reaction_rule_type const r((*gen)());
typedef typename reaction_rule_type::rate_type rate_type;
rate_type rate;
try{
rate = boost::lexical_cast<rate_type>(r["k"]);
}
catch (boost::bad_lexical_cast &)
// There is no standard textual representation of infinity
// in the C++ standard, so boost throws a bad_lexical_cast
// for 'inf', just like for any other non-numerical text.
{
if(r["k"].compare("inf") == 0){
rate = std::numeric_limits<rate_type>::infinity();
}
else{
throw;
}
}
(*x.first).second.push_back(reaction_rule_type(
r.id(),
rate,
r.get_reactants(),
r.get_products()));
}
}
return (*x.first).second;
}
return (*i).second;
}
reaction_rule_vector const& query_reaction_rule(
species_id_type const& r1, species_id_type const& r2) const
{
typename second_order_reaction_rule_vector_map::const_iterator i(
second_order_cache_.find(std::make_pair(r1, r2)));
if (i == second_order_cache_.end())
{
std::pair<
typename second_order_reaction_rule_vector_map::iterator,
bool> x(second_order_cache_.insert(
std::make_pair(std::make_pair(r1, r2),
reaction_rule_vector())));
boost::scoped_ptr<typename backend_type::reaction_rule_generator>
gen(backend_.query_reaction_rule(r1, r2));
if (gen)
{
while (::valid(*gen))
{
typename backend_type::reaction_rule_type const r((*gen)());
typedef typename reaction_rule_type::rate_type rate_type;
rate_type rate;
try{
rate = boost::lexical_cast<rate_type>(r["k"]);
}
catch (boost::bad_lexical_cast &)
// There is no standard textual representation of infinity
// in the C++ standard, so boost throws a bad_lexical_cast
// for 'inf', just like for any other non-numerical text.
{
if(r["k"].compare("inf") == 0){
rate = std::numeric_limits<rate_type>::infinity();
}
else{
throw;
}
}
(*x.first).second.push_back(reaction_rule_type(
r.id(),
rate,
r.get_reactants(),
r.get_products()));
}
}
return (*x.first).second;
}
return (*i).second;
}
NetworkRulesWrapper(backend_type const& backend): backend_(backend) {}
private:
mutable first_order_reaction_rule_vector_map first_order_cache_;
mutable second_order_reaction_rule_vector_map second_order_cache_;
backend_type const& backend_;
};
#endif /* NETWORK_RULES_WRAPPER_HPP */