SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
real_time_simulation.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_SYSTEMS_REAL_TIME_SIMULATION_H_
3 #define SYDEVS_SYSTEMS_REAL_TIME_SIMULATION_H_
4 
8 
9 namespace sydevs {
10 namespace systems {
11 
12 
32 template<typename Node>
33 class real_time_simulation : public simulation<Node>
34 {
35 public:
51  real_time_simulation(const time_point& start_t, const time_point& end_t, bool can_end_early, int64 seed, std::ostream& stream);
52 
65  real_time_simulation(duration total_dt, int64 seed, std::ostream& stream);
66 
67  virtual ~real_time_simulation() = default;
68 
69  typename Node::injection_type& injection();
70  const typename Node::observation_type& observation();
71 
74 
75  void update_time_advancement_rate(float64 t_adv_rate);
76  void update_time_synchronization_rate(float64 t_syn_rate);
77 
80 
81  void update_synchronization_time(const time_point& sim_t, const clock_time& clk_t);
82 
83  int64 frame_index() const;
84  time_point frame_time() const;
86 
88 
89 private:
90  std::unique_ptr<typename Node::interaction_data> interaction_data_ptr_;
91  real_time_buffer ta_buffer_;
92 };
93 
94 
95 template<typename Node>
96 inline real_time_simulation<Node>::real_time_simulation(const time_point& start_t, const time_point& end_t, bool can_end_early, int64 seed, std::ostream& stream)
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)
100 {
101  interaction_data_ptr_ = this->top.acquire_interaction_data();
102 }
103 
104 
105 template<typename Node>
106 inline real_time_simulation<Node>::real_time_simulation(duration total_dt, int64 seed, std::ostream& stream)
107  : simulation<Node>(total_dt, seed, stream)
108  , interaction_data_ptr_()
109  , ta_buffer_(std::numeric_limits<float64>::infinity(), 0.0)
110 {
111  interaction_data_ptr_ = this->top.acquire_interaction_data();
112 }
113 
114 
115 template<typename Node>
116 inline typename Node::injection_type& real_time_simulation<Node>::injection()
117 {
118  return interaction_data_ptr_->injection();
119 }
120 
121 
122 template<typename Node>
123 inline const typename Node::observation_type& real_time_simulation<Node>::observation()
124 {
125  return interaction_data_ptr_->observation();
126 }
127 
128 
129 template<typename Node>
131 {
132  return ta_buffer_.time_advancement_rate();
133 }
134 
135 
136 template<typename Node>
138 {
139  return ta_buffer_.time_synchronization_rate();
140 }
141 
142 
143 template<typename Node>
145 {
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);
148 }
149 
150 
151 template<typename Node>
153 {
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);
156 }
157 
158 
159 template<typename Node>
161 {
162  return ta_buffer_.synchronization_time();
163 }
164 
165 
166 template<typename Node>
168 {
169  return ta_buffer_.synchronization_clock_time();
170 }
171 
172 
173 template<typename Node>
175 {
176  ta_buffer_.update_synchronization_time(sim_t, clk_t);
177 }
178 
179 
180 template<typename Node>
182 {
183  return this->top.frame_index();
184 }
185 
186 
187 template<typename Node>
189 {
190  return ta_buffer_.current_time();
191 }
192 
193 
194 template<typename Node>
196 {
197  return ta_buffer_.current_clock_time();
198 }
199 
200 
201 template<typename Node>
203 {
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();
210  auto done = false;
211  while (!done) {
212  event_count += this->process_next_events();
213  done = this->finished() || (frame_index() > current_frame_index);
214  if (!done) {
215  t = this->time().t();
216  clock_t = clock::now();
217  }
218  }
219  ta_buffer_.update_current_time(t, clock_t, this->top.planned_duration());
220  }
221  }
222  return event_count;
223 }
224 
225 
226 } // namespace
227 } // namespace
228 
229 #endif
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