NEST  2.6.0,not_revisioned_source_dir@0
nest_time.h
Go to the documentation of this file.
1 /*
2  * nest_time.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * NEST is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef NEST_TIME_H
24 #define NEST_TIME_H
25 #include <string>
26 #include <iostream>
27 #include <cassert>
28 #include <limits>
29 #include <cmath>
30 #include <cstdlib>
31 
32 #include "nest.h"
33 #include "numerics.h"
34 
35 class Token;
36 
37 namespace nest {
38  class Time;
39 }
40 
41 std::ostream& operator<<(std::ostream&, const nest::Time&);
42 
43 namespace nest
44 {
115 // Function to use internally
118 
119  // This is just to map llabs => abs
120  template<class N>
121  inline N time_abs(const N n) {return std::abs(n);}
122 
123  template<>
124  inline long long time_abs(long long n) {return llabs(n);}
125 
127 // Time class = tic_t
129 
130  class Time
131  {
132  // tic_t: tics in a step, signed long or long long
133  // delay: steps, signed long
134  // double_t: milliseconds (double!)
135 
137 // Range: Limits & conversion factors for different types
139 
140  protected:
141  struct Range {
145 
150 
153 
154  static const long_t INF_MARGIN = 8;
155  };
156 
157  public:
158  static tic_t compute_max();
159 
161 // The data: longest integer for tics
163 
164  protected:
166 
168 // Friend declaration for units and binary operators
170 
171  friend struct step;
172  friend struct tic;
173  friend struct ms;
174  friend struct ms_stamp;
175 
176  friend bool operator==(const Time& t1, const Time& t2);
177  friend bool operator!=(const Time& t1, const Time& t2);
178  friend bool operator<(const Time& t1, const Time& t2);
179  friend bool operator>(const Time& t1, const Time& t2);
180  friend bool operator<=(const Time& t1, const Time& t2);
181  friend bool operator>=(const Time& t1, const Time& t2);
182  friend Time operator+(const Time& t1, const Time& t2);
183  friend Time operator-(const Time& t1, const Time& t2);
184  friend Time operator*(const long_t factor, const Time& t);
185  friend Time operator*(const Time& t, long_t factor);
186  friend std::ostream& (::operator<<)(std::ostream&, const Time&);
187 
189 // Limits for time, including infinity definitions
191 
192  protected:
193  struct Limit {
197 
199  tics(tics), steps(steps), ms(ms) {}
200  Limit(const tic_t&);
201  };
202  static Limit LIM_MAX;
203  static Limit LIM_MIN;
204  Time::Limit limit(const tic_t&);
205 
206  // max is never larger than tics/INF_MARGIN, and we can use INF_MARGIN
207  // to minimize range checks on +/- operations
208  static struct LimitPosInf {
209  static const tic_t tics = tic_t_max / Range::INF_MARGIN + 1;
210  static const delay steps = delay_max;
211 #define LIM_POS_INF_ms double_t_max // because C++ bites
212  } LIM_POS_INF;
213 
214  static struct LimitNegInf {
215  static const tic_t tics = -tic_t_max / Range::INF_MARGIN - 1;
216  static const delay steps = -delay_max;
217 #define LIM_NEG_INF_ms (-double_t_max) // c++ bites
218  } LIM_NEG_INF;
219 
221 // Unit class for constructors
223 
224  public:
225  struct tic {
227  explicit tic(tic_t t) : t(t) {};
228  };
229 
230  struct step {
232  explicit step(delay t): t(t) {}
233  };
234 
235  struct ms {
237 
238  explicit ms(double_t t): t(t) {}
239  explicit ms(long_t t): t(static_cast<double_t>(t)) {}
240 
241  static double_t fromtoken(const Token& t);
242  explicit ms(const Token& t): t(fromtoken(t)) {};
243  };
244 
245  struct ms_stamp {
247  explicit ms_stamp(double_t t): t(t) {}
248  explicit ms_stamp(long_t t): t(static_cast<double_t>(t)) {}
249  };
250 
252 // Constructors
254 
255  protected:
256 
257  explicit
258  Time(tic_t tics): tics(tics) {} // This doesn't check ranges.
259  // Ergo: LIM_MAX.tics >= tics >= LIM_MIN.tics or
260  // tics == LIM_POS_INF.tics or LIM_NEG_INF.tics
261 
262  public:
263 
264  Time(): tics(0) {};
265 
266  // Default copy constructor: assumes legal time object
267  // Defined by compiler.
268  // Time(const Time& t);
269 
270  Time(tic t):
271  tics((time_abs(t.t) < LIM_MAX.tics) ?
272  t.t :
273  (t.t < 0) ?
275  {}
276 
277  Time(step t):
278  tics((time_abs(t.t) < LIM_MAX.steps) ?
279  t.t * Range::TICS_PER_STEP :
280  (t.t < 0) ?
282  {}
283 
284  Time(ms t):
285  tics((time_abs(t.t) < LIM_MAX.ms) ?
286  static_cast<tic_t>(t.t * Range::TICS_PER_MS + 0.5) :
287  (t.t < 0) ?
289  {}
290 
291  static tic_t fromstamp(ms_stamp);
293 
295 // Resolution: set tics per ms, steps per ms
297 
298  static void set_resolution(double_t tics_per_ms);
299  static void set_resolution(double_t tics_per_ms, double_t ms_per_step);
300  static void reset_resolution();
301  static void reset_to_defaults();
302 
303  static Time get_resolution() {
304  return Time(Range::TICS_PER_STEP);
305  }
306 
307  static bool resolution_is_default() {
309  }
310 
312 // Common zero-ary or unary operations
314 
315  void set_to_zero() {tics = 0;}
316 
317  void advance() {
319  range();
320  }
321 
322  Time succ() const {return tic(tics + Range::TICS_PER_STEP);} // check range
323  Time pred() const {return tic(tics - Range::TICS_PER_STEP);} // check range
324 
326 // Subtypes of Time (bool tests)
328 
329  bool is_finite() const {
330  return tics != LIM_POS_INF.tics && tics != LIM_NEG_INF.tics;
331  }
332 
333  bool is_neg_inf() const {
334  return tics == LIM_NEG_INF.tics;
335  }
336 
337  bool is_grid_time() const {
338  return (tics % Range::TICS_PER_STEP) == 0;
339  }
340  bool is_step() const {
341  return tics > 0 && is_grid_time();
342  }
343 
344  bool is_multiple_of(const Time& divisor) const {
345  assert(divisor.tics > 0);
346  return (tics % divisor.tics) == 0;
347  }
348 
350 // Singleton'ish types
352 
353  static Time max() {return Time(LIM_MAX.tics);}
354  static Time min() {return Time(LIM_MIN.tics);}
356  static Time neg_inf() {return Time(LIM_NEG_INF.tics);}
357  static Time pos_inf() {return Time(LIM_POS_INF.tics);}
358 
360 // Overflow checks & recalibrate after resolution setting
362 
363  void range() {
364  if (time_abs(tics) < LIM_MAX.tics) return;
365  tics = (tics < 0) ?
367  }
368 
369  void calibrate() {range();}
370 
372 // Unary operators
374 
375  Time& operator+=(const Time& t) {
376  tics += t.tics;
377  range();
378  return *this;
379  }
380 
382 // Convert to external units
384 
385  tic_t get_tics() const {return tics;}
388 
389  double_t get_ms() const {
390  if (tics == LIM_POS_INF.tics) return LIM_POS_INF_ms;
391  if (tics == LIM_NEG_INF.tics) return LIM_NEG_INF_ms;
392  return Range::MS_PER_TIC * tics;
393  }
394 
395  delay get_steps() const {
396  if (tics == LIM_POS_INF.tics) return LIM_POS_INF.steps;
397  if (tics == LIM_NEG_INF.tics) return LIM_NEG_INF.steps;
398 
399  // round tics up to nearest step
400  // by adding TICS_PER_STEP-1 before division
402  }
403 
412  return steps * Range::MS_PER_STEP;
413  }
414 
416  return ld_round(ms * Range::STEPS_PER_MS);
417  }
418  };
419 
421 // Non-class definitions
423 
424  // Needs to be outside the class to get internal linkage to
425  // maybe make the zero visible for optimization.
426  const Time TimeZero;
427 
429 // Binary operators
431 
432  inline
433  bool operator==(const Time& t1, const Time& t2) {
434  return t1.tics == t2.tics;
435  }
436 
437  inline
438  bool operator!=(const Time& t1, const Time& t2) {
439  return t1.tics != t2.tics;
440  }
441 
442  inline
443  bool operator<(const Time& t1, const Time& t2) {
444  return t1.tics < t2.tics;
445  }
446 
447  inline
448  bool operator>(const Time& t1, const Time& t2) {
449  return t1.tics > t2.tics;
450  }
451 
452  inline
453  bool operator<=(const Time& t1, const Time& t2) {
454  return t1.tics <= t2.tics;
455  }
456 
457  inline
458  bool operator>=(const Time& t1, const Time& t2) {
459  return t1.tics >= t2.tics;
460  }
461 
462  inline
463  Time operator+(const Time& t1, const Time& t2) {
464  return Time::tic(t1.tics + t2.tics); // check range
465  }
466 
467  inline
468  Time operator-(const Time& t1, const Time& t2) {
469  return Time::tic(t1.tics - t2.tics); // check range
470  }
471 
472  inline
473  Time operator*(const long_t factor, const Time& t) {
474  const tic_t n = factor * t.tics;
475  // if no overflow:
476  if (t.tics == 0 || n/t.tics == factor)
477  return Time::tic(n); // check range
478 
479  if ((t.tics > 0 && factor > 0) || (t.tics < 0 && factor < 0))
480  return Time(Time::LIM_POS_INF.tics);
481  else
482  return Time(Time::LIM_NEG_INF.tics);
483  }
484 
485  inline
486  Time operator*(const Time& t, long_t factor) {
487  return factor*t;
488  }
489 } // Namespace
490 
491 std::ostream& operator<<(std::ostream&, const nest::Time&);
492 
493 
494 #endif
Time operator+(const Time &t1, const Time &t2)
Definition: nest_time.h:463
static double_t TICS_PER_MS
Definition: nest_time.h:146
const Name N("N")
Specific to population point process model (pp_pop_psc_delta)
Definition: nest_names.h:203
static void reset_to_defaults()
Definition: nest_time.cpp:143
void set_to_zero()
Definition: nest_time.h:315
bool operator>(const Time &t1, const Time &t2)
Definition: nest_time.h:448
friend Time operator-(const Time &t1, const Time &t2)
Definition: nest_time.h:468
bool is_step() const
Definition: nest_time.h:340
static double_t get_ms_per_tic()
Definition: nest_time.h:355
static tic_t TICS_PER_STEP_RND
Definition: nest_time.h:143
ms(const Token &t)
Definition: nest_time.h:242
const long_t delay_max
Definition: nest.h:179
static Limit LIM_MIN
Definition: nest_time.h:203
static Time min()
Definition: nest_time.h:354
ms(long_t t)
Definition: nest_time.h:239
long ld_round(double x)
Round to nearest int, rounding midpoints upwards.
Definition: numerics.cpp:83
Time(tic t)
Definition: nest_time.h:270
Definition: nest_time.h:230
ms_stamp(long_t t)
Definition: nest_time.h:248
static double_t delay_steps_to_ms(delay steps)
Convert between delays given in steps and milliseconds.
Definition: nest_time.h:411
delay get_steps() const
Definition: nest_time.h:395
Definition: nest_time.h:214
static const tic_t tics
Definition: nest_time.h:209
bool operator==(const Time &t1, const Time &t2)
Definition: nest_time.h:433
double_t t
Definition: nest_time.h:236
friend struct tic
Definition: nest_time.h:172
Time operator-(const Time &t1, const Time &t2)
Definition: nest_time.h:468
bool operator>=(const Time &t1, const Time &t2)
Definition: nest_time.h:458
Time(ms_stamp t)
Definition: nest_time.h:292
delay t
Definition: nest_time.h:231
static struct nest::Time::LimitPosInf LIM_POS_INF
Definition: nest_time.h:141
Time()
Definition: nest_time.h:264
bool is_multiple_of(const Time &divisor) const
Definition: nest_time.h:344
assert(pNet!=0)
std::ostream & operator<<(std::ostream &, const nest::Time &)
Definition: nest_time.cpp:156
void calibrate()
Definition: nest_time.h:369
Definition: nest_time.h:235
static double_t MS_PER_TIC
Definition: nest_time.h:147
friend struct ms_stamp
Definition: nest_time.h:174
static Time neg_inf()
Definition: nest_time.h:356
tic(tic_t t)
Definition: nest_time.h:227
Definition: nest_time.h:208
static double_t STEPS_PER_MS
Definition: nest_time.h:148
Definition: nest_time.h:130
Time(tic_t tics)
Definition: nest_time.h:258
bool operator<(const Event &, const Event &)
Definition: event_priority.h:44
static const tic_t TICS_PER_STEP_DEFAULT
Definition: nest_time.h:151
friend Time operator*(const long_t factor, const Time &t)
Definition: nest_time.h:473
long long tic_t
Type for Time tics.
Definition: nest.h:82
double_t t
Definition: nest_time.h:246
bool is_neg_inf() const
Definition: nest_time.h:333
static double_t MS_PER_STEP
Definition: nest_time.h:149
static tic_t TICS_PER_STEP
Definition: nest_time.h:142
void advance()
Definition: nest_time.h:317
Limit(tic_t tics, delay steps, double_t ms)
Definition: nest_time.h:198
Time & operator+=(const Time &t)
Definition: nest_time.h:375
const Time TimeZero
Definition: nest_time.h:426
static delay delay_ms_to_steps(double_t ms)
Definition: nest_time.h:415
static double_t get_tics_per_ms()
Definition: nest_time.h:387
static Time max()
Definition: nest_time.h:353
double_t ms
Definition: nest_time.h:196
static bool resolution_is_default()
Definition: nest_time.h:307
static tic_t get_tics_per_step()
Definition: nest_time.h:386
friend bool operator>=(const Time &t1, const Time &t2)
Definition: nest_time.h:458
static tic_t compute_max()
Definition: nest_time.cpp:58
step(delay t)
Definition: nest_time.h:232
static Limit LIM_MAX
Definition: nest_time.h:202
static void set_resolution(double_t tics_per_ms)
Definition: nest_time.cpp:80
N time_abs(const N n)
Class to handle simulation time and realtime.
Definition: nest_time.h:121
friend bool operator==(const Time &t1, const Time &t2)
Definition: nest_time.h:433
void range()
Definition: nest_time.h:363
static void reset_resolution()
Definition: nest_time.cpp:102
ms_stamp(double_t t)
Definition: nest_time.h:247
tic_t tics
Definition: nest_time.h:194
tic_t tics
Definition: nest_time.h:165
ms(double_t t)
Definition: nest_time.h:238
friend bool operator>(const Time &t1, const Time &t2)
Definition: nest_time.h:448
double double_t
Double precision floating point numbers.
Definition: nest.h:93
static Time pos_inf()
Definition: nest_time.h:357
static const double_t TICS_PER_MS_DEFAULT
Definition: nest_time.h:152
Time(step t)
Definition: nest_time.h:277
static tic_t fromstamp(ms_stamp)
Definition: nest_time.cpp:127
friend bool operator<=(const Time &t1, const Time &t2)
Definition: nest_time.h:453
Definition: nest_time.h:225
tic_t t
Definition: nest_time.h:226
double_t get_ms() const
Definition: nest_time.h:389
bool operator<=(const Time &t1, const Time &t2)
Definition: nest_time.h:453
static Time get_resolution()
Definition: nest_time.h:303
bool is_finite() const
Definition: nest_time.h:329
static const delay steps
Definition: nest_time.h:216
static const tic_t tics
Definition: nest_time.h:215
Time::Limit limit(const tic_t &)
delay steps
Definition: nest_time.h:195
const Name n("n")
Number of synaptic release sites (int >=0) (Tsodyks2_connection)
Definition: nest_names.h:202
long_t delay
Delay of a connection.
Definition: nest.h:178
tic_t get_tics() const
Definition: nest_time.h:385
Definition: nest_time.h:245
Default types used by the NEST kernel.
friend bool operator!=(const Time &t1, const Time &t2)
Definition: nest_time.h:438
Time(ms t)
Definition: nest_time.h:284
static tic_t OLD_TICS_PER_STEP
Definition: nest_time.h:144
A type-independent container for C++-types.
Definition: token.h:68
const tic_t tic_t_max
Definition: nest.h:83
Time pred() const
Definition: nest_time.h:323
Time succ() const
Definition: nest_time.h:322
friend Time operator+(const Time &t1, const Time &t2)
Definition: nest_time.h:463
long long_t
Integer number with at least 32 bit.
Definition: nest.h:96
static double_t fromtoken(const Token &t)
Definition: nest_time.cpp:115
static const delay steps
Definition: nest_time.h:210
bool is_grid_time() const
Definition: nest_time.h:337
static const long_t INF_MARGIN
Definition: nest_time.h:154
bool operator!=(const Time &t1, const Time &t2)
Definition: nest_time.h:438
static struct nest::Time::LimitNegInf LIM_NEG_INF
Definition: nest_time.h:193
Time operator*(const long_t factor, const Time &t)
Definition: nest_time.h:473
friend bool operator<(const Time &t1, const Time &t2)
Definition: nest_time.h:443