Skip to content

Commit 02bfed0

Browse files
authored
1496 smm model output is misaligned with time points (#1497)
SMM advance function now works on future status vector instead of past status vector.
1 parent c796281 commit 02bfed0

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

cpp/models/smm/simulation.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,20 @@ class Simulation
8383
update_current_rates_and_waiting_times();
8484
size_t next_event = determine_next_event(); // index of the next event
8585
FP current_time = m_result.get_last_time();
86-
// set in the past to add a new time point immediately
87-
FP last_result_time = current_time - m_dt;
86+
// set current time to add next time point in the future
87+
FP last_result_time = current_time;
8888
// iterate over time
8989
while (current_time + m_waiting_times[next_event] < tmax) {
90-
// update time
91-
current_time += m_waiting_times[next_event];
92-
// regularily save current state in m_results
93-
if (current_time > last_result_time + m_dt) {
94-
last_result_time = current_time;
95-
m_result.add_time_point(current_time);
90+
// If the next event happens further in the future than the next stored time point, add a new one.
91+
if (current_time + m_waiting_times[next_event] >= last_result_time) {
92+
auto num_dt = std::ceil((current_time + m_waiting_times[next_event] - last_result_time) / m_dt);
93+
last_result_time = std::min(tmax, last_result_time + num_dt * m_dt);
94+
m_result.add_time_point(last_result_time);
9695
// copy from the previous last value
9796
m_result.get_last_value() = m_result[m_result.get_num_time_points() - 2];
9897
}
98+
// update time
99+
current_time += m_waiting_times[next_event];
99100
// decide event type by index and perform it
100101
if (next_event < adoption_rates().size()) {
101102
// perform adoption event

0 commit comments

Comments
 (0)