-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathnode_schedule.cpp
More file actions
285 lines (252 loc) · 7.51 KB
/
Copy pathnode_schedule.cpp
File metadata and controls
285 lines (252 loc) · 7.51 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
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
#include <plant/node_schedule.h>
#include <plant/parameters.h>
#include <plant/util.h>
#include <Rcpp.h>
#include <cmath> // log2, exp2
#include <limits> // std::numeric_limits
namespace plant {
NodeSchedule::NodeSchedule(size_t n_species_)
: n_species(n_species_),
max_time(std::numeric_limits<double>::infinity()),
use_ode_times(false) {
reset();
}
size_t NodeSchedule::size() const {
return events.size();
}
size_t NodeSchedule::get_n_species() const {
return n_species;
}
NodeSchedule NodeSchedule::expand(size_t n_extra,
std::vector<double> times) {
NodeSchedule ret = *this;
ret.n_species += n_extra;
for (size_t i = n_species; i < ret.n_species; ++i) {
ret.set_times(times, i);
}
return ret;
}
void NodeSchedule::clear_times(size_t species_index) {
events_iterator e = events.begin();
while (e != events.end()) {
if (e->species_index == species_index) {
e = events.erase(e);
} else {
++e;
}
}
reset();
}
double NodeSchedule::get_max_time() const {
return max_time;
}
std::vector<std::vector<double> > NodeSchedule::get_times() const {
std::vector<std::vector<double> > ret;
for (size_t i = 0; i < n_species; ++i) {
ret.push_back(times(i));
}
return ret;
}
void NodeSchedule::set_times(const std::vector<double>& times_,
size_t species_index) {
clear_times(species_index);
events_iterator e = events.begin();
for (std::vector<double>::const_iterator t = times_.begin();
t != times_.end(); ++t) {
e = add_time(*t, species_index, e);
}
reset();
}
void NodeSchedule::set_times(const std::vector<std::vector<double> >& times_) {
util::check_length(times_.size(), n_species);
for (size_t i = 0; i < n_species; ++i) {
set_times(times_[i], i);
}
}
std::vector<double> NodeSchedule::times(size_t species_index) const {
std::vector<double> ret;
for (events_const_iterator e = events.begin(); e != events.end(); ++e) {
if (e->species_index == species_index) {
ret.push_back(e->time_introduction());
}
}
return ret;
}
void NodeSchedule::reset() {
queue = events;
// NOTE: this is updating elements in *queue*, not in events; the
// events only ever have a single element in times.
events_iterator e = queue.begin();
while (e != queue.end()) {
events_iterator e_next = e;
++e_next;
e->times.push_back(e_next == queue.end() ?
max_time : e_next->time_introduction());
e = e_next;
}
if (use_ode_times) {
distribute_ode_times();
}
}
// NOTE: Using vector here is inefficient, but we need a vector in the
// end. This would not matter if we added to event and didn't do this
// each reset, but I don't imagine that this is a big cost in
// practical cases.
//
// NOTE: It should be the case that there are exactly two times in
// e->times on entry, which means that we could always just construct
// a new vector with the start time, the times in 'extra' and the end
// time, purely by pushing on the end. Ignoring this, it does mean
// that the inefficiency of using vector (over list) is confined to a
// copy of a single element, which won't be as bad as using an
// underlying list and converting to vector when passing back to the
// ode solver.
void NodeSchedule::distribute_ode_times() {
events_iterator e = queue.begin();
std::vector<double>::const_iterator t = ode_times.begin();
while (e != queue.end()) {
std::vector<double> extra;
while (t != ode_times.end() && *t < e->time_end()) {
// The condition here excludes times that exactly match one of
// the time boundaries (we'll be stopping there anyway).
if (!util::identical(*t, e->time_introduction()) &&
!util::identical(*t, e->time_end())) {
extra.push_back(*t);
}
++t;
}
if (extra.size() > 0) {
std::vector<double>::iterator at = ++e->times.begin();
e->times.insert(at, extra.begin(), extra.end());
}
++e;
}
}
void NodeSchedule::pop() {
if (queue.empty()) {
Rcpp::stop("Attempt to pop empty queue");
}
queue.pop_front();
}
NodeScheduleEvent NodeSchedule::next_event() const {
if (queue.empty()) {
Rcpp::stop("All events completed");
}
return queue.front();
}
size_t NodeSchedule::remaining() const {
return queue.size();
}
// * R interface
void NodeSchedule::r_clear_times(util::index species_index) {
clear_times(species_index.check_bounds(n_species));
}
void NodeSchedule::r_set_times(std::vector<double> times_,
util::index species_index) {
if (!util::is_sorted(times_.begin(), times_.end())) {
Rcpp::stop("Times must be sorted (increasing)");
}
if (times_.size() == 0) {
Rcpp::stop("Need at least one time");
}
if (times_.front() < 0) {
Rcpp::stop("First time must nonnegative");
}
if (times_.back() > max_time) {
Rcpp::stop("Times cannot be greater than max_time");
}
set_times(times_, species_index.check_bounds(n_species));
}
std::vector<double> NodeSchedule::r_times(util::index species_index) const {
return times(species_index.check_bounds(n_species));
}
void NodeSchedule::r_set_max_time(double x) {
if (x < 0) {
Rcpp::stop("max_time must be nonnegative");
}
if (x < events.back().time_introduction()) {
Rcpp::stop("max_time must be at least the final scheduled time");
}
max_time = x;
reset();
}
std::vector<double> NodeSchedule::r_ode_times() const {
return ode_times;
}
void NodeSchedule::r_set_ode_times(std::vector<double> x) {
if (x.empty()) {
r_clear_ode_times();
} else {
if (x.size() < 2) {
Rcpp::stop("Need at least two times");
}
if (!util::identical(x.front(), 0.0)) {
Rcpp::stop("First time must be exactly zero");
}
if (util::is_finite(max_time) && !util::identical(x.back(), max_time)) {
Rcpp::stop("Last time must be exactly max_time");
}
if (!util::is_sorted(x.begin(), x.end())) {
Rcpp::stop("ode_times must be sorted");
}
ode_times = x;
if (!util::is_finite(max_time)) {
max_time = ode_times.back();
}
reset();
}
}
void NodeSchedule::r_clear_ode_times() {
ode_times.clear();
use_ode_times = false;
// This will pull the ode times out of the events if they were set.
reset();
}
bool NodeSchedule::using_ode_times() const {
return use_ode_times;
}
void NodeSchedule::r_set_use_ode_times(bool x) {
if (x) {
// Check that we have some times before enabling.
if (ode_times.size() > 2) {
use_ode_times = true;
} else {
Rcpp::stop("No times stored in object");
}
} else { // Can always disable
use_ode_times = false;
}
reset();
}
SEXP NodeSchedule::r_all_times() const {
return Rcpp::wrap(get_times());
}
void NodeSchedule::r_set_all_times(SEXP rx) {
Rcpp::List x(Rcpp::as<Rcpp::List>(rx));
// Ensure that we can get all the times out:
std::vector< std::vector<double> > new_times;
for (Rcpp::List::iterator el = x.begin(); el != x.end(); ++el) {
new_times.push_back(Rcpp::as<std::vector<double> >(*el));
}
// set_all_times(new_times);
util::check_length(new_times.size(), n_species);
for (size_t i = 0; i < n_species; ++i) {
set_times(new_times[i], i);
}
}
NodeSchedule NodeSchedule::r_copy() const {
return *this;
}
// * Private methods
NodeSchedule::events_iterator
NodeSchedule::add_time(double time, size_t species_index,
events_iterator it) {
Event e(time, species_index);
it = events.begin();
while (it != events.end() && time > it->time_introduction()) {
++it;
}
it = events.insert(it, e);
return it;
}
}