2 #ifndef SYDEVS_SYSTEMS_REAL_TIME_SIMULATION_H_
3 #define SYDEVS_SYSTEMS_REAL_TIME_SIMULATION_H_
32 template<
typename Node>
69 typename Node::injection_type&
injection();
70 const typename Node::observation_type&
observation();
90 std::unique_ptr<typename Node::interaction_data> interaction_data_ptr_;
95 template<
typename Node>
97 :
simulation<Node>(start_t, end_t, can_end_early, seed, stream)
98 , interaction_data_ptr_()
99 , ta_buffer_(std::numeric_limits<
float64>::infinity(), 0.0)
101 interaction_data_ptr_ = this->
top.acquire_interaction_data();
105 template<
typename Node>
108 , interaction_data_ptr_()
109 , ta_buffer_(std::numeric_limits<
float64>::infinity(), 0.0)
111 interaction_data_ptr_ = this->
top.acquire_interaction_data();
115 template<
typename Node>
118 return interaction_data_ptr_->injection();
122 template<
typename Node>
125 return interaction_data_ptr_->observation();
129 template<
typename Node>
132 return ta_buffer_.time_advancement_rate();
136 template<
typename Node>
139 return ta_buffer_.time_synchronization_rate();
143 template<
typename Node>
146 if (t_adv_rate <= 0.0)
throw std::invalid_argument(
"Time advancement rate must be positive");
147 ta_buffer_.update_time_advancement_rate(t_adv_rate);
151 template<
typename Node>
154 if (t_syn_rate < 0.0)
throw std::invalid_argument(
"Time advancement depth must be non-negative");
155 ta_buffer_.update_time_synchronization_rate(t_syn_rate);
159 template<
typename Node>
162 return ta_buffer_.synchronization_time();
166 template<
typename Node>
169 return ta_buffer_.synchronization_clock_time();
173 template<
typename Node>
176 ta_buffer_.update_synchronization_time(sim_t, clk_t);
180 template<
typename Node>
183 return this->top.frame_index();
187 template<
typename Node>
190 return ta_buffer_.current_time();
194 template<
typename Node>
197 return ta_buffer_.current_clock_time();
201 template<
typename Node>
204 int64 event_count = 0;
205 if (!this->finished()) {
206 auto t = this->time().t();
207 auto clock_t = clock::now();
208 if (clock_t >= ta_buffer_.planned_clock_time()) {
209 int64 current_frame_index = frame_index();
212 event_count += this->process_next_events();
213 done = this->finished() || (frame_index() > current_frame_index);
215 t = this->time().t();
216 clock_t = clock::now();
219 ta_buffer_.update_current_time(t, clock_t, this->top.planned_duration());
time_point frame_time() const
Returns the simulated time point of the most recently processed frame of the simulation.
Definition: real_time_simulation.h:188
bool can_end_early() const
Returns true if the simulation can end before the specified end time.
Definition: simulation.h:209
float64 time_advancement_rate() const
Returns the time advancement rate.
Definition: real_time_simulation.h:130
Node top
The topmost system node.
Definition: simulation.h:153
A class template for running simulations in real time.
Definition: real_time_simulation.h:33
A data structure which suggests event wallclock times to aid in the synchronization of a simulation's...
Definition: real_time_buffer.h:40
void update_synchronization_time(const time_point &sim_t, const clock_time &clk_t)
Updates the synchonization reference point.
Definition: real_time_simulation.h:174
clock_time synchronization_clock_time() const
Returns the wallclock time of the synchronization reference point.
Definition: real_time_simulation.h:167
void update_time_synchronization_rate(float64 t_syn_rate)
Updates the time synchonization rate.
Definition: real_time_simulation.h:152
time_point synchronization_time() const
Returns the simulated time of the synchronization reference point.
Definition: real_time_simulation.h:160
const Node::observation_type & observation()
Returns a reference to the observation object that supports communication from the interactive system...
Definition: real_time_simulation.h:123
A data structure which represents a point in time as an arbitrary-precision multiple of its shortest ...
Definition: time_point.h:84
std::chrono::time_point< clock > clock_time
Definition: timer.h:11
Node::injection_type & injection()
Returns a reference to the injection object that supports communication into the interactive system n...
Definition: real_time_simulation.h:116
A class template for running simulations.
Definition: simulation.h:73
float64 time_synchronization_rate() const
Returns the time synchronization rate.
Definition: real_time_simulation.h:137
void update_time_advancement_rate(float64 t_adv_rate)
Updates the time advancement rate.
Definition: real_time_simulation.h:144
int64 process_frame_if_time_reached()
If a sufficent amount of wallclock time has elapsed, processes all events until the frame index has a...
Definition: real_time_simulation.h:202
real_time_simulation(const time_point &start_t, const time_point &end_t, bool can_end_early, int64 seed, std::ostream &stream)
Constructs a real_time_simulation with the full set of configuration options.
Definition: real_time_simulation.h:96
virtual ~real_time_simulation()=default
Destructor.
clock_time frame_clock_time() const
Returns the wallclock time point of the most recently processed frame of the simulation.
Definition: real_time_simulation.h:195
double float64
Definition: number_types.h:22
int64 frame_index() const
Returns the index of the most recently processed frame of the simulation.
Definition: real_time_simulation.h:181