base_event.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2019 by
  4. * The Salk Institute for Biological Studies and
  5. * Pittsburgh Supercomputing Center, Carnegie Mellon University
  6. *
  7. * Use of this source code is governed by an MIT-style
  8. * license that can be found in the LICENSE file or at
  9. * https://opensource.org/licenses/MIT.
  10. *
  11. ******************************************************************************/
  12. #ifndef SRC4_BASE_EVENT_H_
  13. #define SRC4_BASE_EVENT_H_
  14. #include "defines.h"
  15. namespace Json {
  16. class Value;
  17. }
  18. namespace MCell {
  19. class World;
  20. typedef int event_type_index_t;
  21. // Value specifies ordering when two events are scheduled for exactly the same time
  22. // The 'holes' are there on purpose for ordering of external events
  23. const event_type_index_t EVENT_TYPE_INDEX_INVALID = -1;
  24. // must be the very first event in an iteration
  25. const event_type_index_t EVENT_TYPE_INDEX_CALL_START_ITERATION_CHECKPOINT = 0;
  26. const event_type_index_t EVENT_TYPE_INDEX_RELEASE = 200;
  27. // first counting and visualization output is done after release
  28. const event_type_index_t EVENT_TYPE_INDEX_MOL_OR_RXN_COUNT = 290;
  29. // viz_output is special in the way that simulation is terminated herein the last iteration
  30. const event_type_index_t EVENT_TYPE_INDEX_VIZ_OUTPUT = 300;
  31. // simulation end check event is scheduled for each iteration,
  32. // this allows us to safely terminate after all observables were collected
  33. // (counts and viz output must be before diffuse&react for MCell3 compatibility)
  34. const event_type_index_t EVENT_TYPE_INDEX_SIMULATION_END_CHECK = 301;
  35. const event_type_index_t EVENT_TYPE_INDEX_RXN_CLASS_CLEANUP = 400;
  36. const event_type_index_t EVENT_TYPE_INDEX_SPECIES_CLEANUP = 410;
  37. const event_type_index_t EVENT_TYPE_INDEX_SORT_MOLS_BY_SUBPART = 420;
  38. const event_type_index_t EVENT_TYPE_INDEX_CLAMP_RELEASE = 490;
  39. const event_type_index_t EVENT_TYPE_INDEX_DIFFUSE_REACT = 500; // this event spans the whole time step
  40. const event_type_index_t EVENT_TYPE_INDEX_DEFRAGMENTATION = 900;
  41. const event_type_index_t EVENT_TYPE_INDEX_MOL_SHUFFLE = 910;
  42. const event_type_index_t EVENT_TYPE_INDEX_BARRIER = 980;
  43. const event_type_index_t EVENT_TYPE_INDEX_CALL_END_ITERATION = 990;
  44. /**
  45. * Base class for all events.
  46. * Should be independent on the mcell world in order to
  47. * integrate also other simulators.
  48. */
  49. class BaseEvent {
  50. public:
  51. BaseEvent(const event_type_index_t t) :
  52. event_time(TIME_INVALID), periodicity_interval(0), type_index(t) {
  53. }
  54. virtual ~BaseEvent() {};
  55. virtual void step() = 0;
  56. virtual void dump(const std::string ind = "") const;
  57. virtual void to_data_model(Json::Value& mcell_node) const;
  58. // some events such as release events have their event time set for
  59. // the beginning of a timestep but internally they need to be ordered
  60. // also according to another value such as actual release time
  61. virtual bool needs_secondary_ordering() { return false; }
  62. virtual double get_secondary_ordering_value() { return 0; }
  63. // if this event should be rescheduled, updates event_time and
  64. // possibly other attributes,
  65. // returns true if even should be rescheduled, false if event
  66. // should be removed from the schedule
  67. virtual bool update_event_time_for_next_scheduled_time() {
  68. // handling the simple case when periodicity_interval is 0 or not
  69. if (periodicity_interval == 0) {
  70. return false;
  71. }
  72. else {
  73. event_time = event_time + periodicity_interval;
  74. return true;
  75. }
  76. }
  77. // - events such as VizOutput, Count, or Release are barriers
  78. // to events whose execution spans over a certain time step
  79. // such as DiffuseReactEvent
  80. // - is_blocked_by_barrier_and_needs_set_time_step must return false
  81. // when is_barrier returns true
  82. virtual bool is_barrier() const { return false; }
  83. // - some events represent simulation over a time step such as
  84. // DiffuseReactEvent, the maximum timestep for such events
  85. // must be limited so that the barrier event (such as molecule count)
  86. // has data valid for its time
  87. // - is_barrier must return false
  88. // when is_blocked_by_barrier_and_needs_set_time_step returns true
  89. // - periodicity_interval must not be 0 when this function return true
  90. virtual bool may_be_blocked_by_barrier_and_needs_set_time_step() const { return false; }
  91. // maximum search time for a barrier, i.e. we do not care whether
  92. // there is a barrier after the time interval returned by this method
  93. virtual double get_max_time_up_to_next_barrier() const {
  94. // the subclass must return true in may_be_blocked_by_barrier_and_needs_set_time_step
  95. assert(false && "Only overridden variant of this method may be called.");
  96. return 0;
  97. }
  98. // if an event is blocked
  99. virtual void set_barrier_time_for_next_execution(const double time_step) {
  100. // the subclass must return true in may_be_blocked_by_barrier_and_needs_set_time_step
  101. assert(false && "Only overridden variant of this method may be called.");
  102. }
  103. // used by checkpointing
  104. virtual bool return_from_run_n_iterations_after_execution() const {
  105. return false;
  106. }
  107. // time when this object's step() method will be called
  108. double event_time;
  109. // once this event is executed, schedule next one after this interval
  110. // do not schedule if the value is 0
  111. double periodicity_interval;
  112. // this value specifies both id of the event and ordering when multiple
  113. // events of a different type are sheduled for the same time
  114. event_type_index_t type_index;
  115. };
  116. }
  117. #endif // SRC4_BASE_EVENT_H_