model.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 by
  4. * The Salk Institute for Biological Studies
  5. *
  6. * Use of this source code is governed by an MIT-style
  7. * license that can be found in the LICENSE file or at
  8. * https://opensource.org/licenses/MIT.
  9. *
  10. ******************************************************************************/
  11. #ifndef API_MODEL_H
  12. #define API_MODEL_H
  13. #include "generated/gen_model.h"
  14. #include "api/api_common.h"
  15. #include "api/globals.h"
  16. #include "api/subsystem.h"
  17. #include "api/instantiation.h"
  18. #include "api/observables.h"
  19. #include "api/api_config.h"
  20. #include "api/warnings.h"
  21. #include "api/notifications.h"
  22. #include "api/shared_structs.h"
  23. #include "api/callbacks.h"
  24. #include "api/introspection.h"
  25. #include "api/geometry_object.h"
  26. namespace MCell {
  27. class World;
  28. namespace API {
  29. class MolWallHitInfo;
  30. class WallWallHitInfo;
  31. class Model: public GenModel {
  32. public:
  33. Model() : initialized(false), world(nullptr), callbacks(this) {
  34. }
  35. Model(DefaultCtorArgType) : initialized(false), world(nullptr), callbacks(this) {
  36. }
  37. virtual ~Model();
  38. // from generated template
  39. void initialize(const bool print_copyright = true) override;
  40. uint64_t run_iterations(const double iterations) override;
  41. void end_simulation(const bool print_final_report = true) override;
  42. void add_subsystem(std::shared_ptr<Subsystem> subsystem) override;
  43. void add_instantiation(std::shared_ptr<Instantiation> instantiation) override;
  44. void add_observables(std::shared_ptr<Observables> observables) override;
  45. void dump_internal_state(const bool with_geometry = false) override;
  46. void export_data_model(const std::string& file = STR_UNSET) override {
  47. export_data_model_viz_or_full(file, false, NAME_EXPORT_DATA_MODEL);
  48. }
  49. void export_viz_data_model(const std::string& file = STR_UNSET) override {
  50. export_data_model_viz_or_full(file, true, NAME_EXPORT_VIZ_DATA_MODEL);
  51. }
  52. void export_geometry(const std::string& output_files_prefix = STR_UNSET) override;
  53. void release_molecules(std::shared_ptr<ReleaseSite> release_site) override;
  54. std::vector<int> run_reaction(
  55. std::shared_ptr<ReactionRule> reaction_rule,
  56. const std::vector<int> reactant_ids,
  57. const double time) override;
  58. void add_vertex_move(
  59. std::shared_ptr<GeometryObject> object, const int vertex_index, const std::vector<double> displacement
  60. ) override;
  61. std::vector<std::shared_ptr<WallWallHitInfo>> apply_vertex_moves(
  62. const bool collect_wall_wall_hits = false,
  63. const bool randomize_order = true
  64. ) override;
  65. void pair_molecules(const int id1, const int id2) override;
  66. void unpair_molecules(const int id1, const int id2) override;
  67. int get_paired_molecule(const int id) override;
  68. std::map<uint, uint> get_paired_molecules() override;
  69. void register_mol_wall_hit_callback(
  70. const std::function<void(std::shared_ptr<MolWallHitInfo>, py::object)> function,
  71. py::object context,
  72. std::shared_ptr<GeometryObject> object = nullptr,
  73. std::shared_ptr<Species> species = nullptr
  74. ) override;
  75. void register_reaction_callback(
  76. const std::function<bool(std::shared_ptr<ReactionInfo>, py::object)> function,
  77. py::object context,
  78. std::shared_ptr<ReactionRule> reaction_rule
  79. ) override;
  80. void load_bngl(
  81. const std::string& file_name,
  82. const std::string& observables_path_or_file = "",
  83. std::shared_ptr<Region> default_release_region = nullptr,
  84. const std::map<std::string, double>& parameter_overrides = std::map<std::string, double>(),
  85. const CountOutputFormat observables_output_format = CountOutputFormat::AUTOMATIC_FROM_EXTENSION
  86. ) override;
  87. void export_to_bngl(
  88. const std::string& file_name,
  89. const BNGSimulationMethod simulation_method = BNGSimulationMethod::NONE) override;
  90. void save_checkpoint(const std::string& custom_dir = STR_UNSET) override;
  91. void schedule_checkpoint(
  92. const uint64_t iteration = 0,
  93. const bool continue_simulation = false,
  94. const std::string& custom_dir = STR_UNSET) override;
  95. // overrides from derived classes Subsystem, Instantiation, and Observables
  96. void add_species(std::shared_ptr<Species> s) override;
  97. void add_reaction_rule(std::shared_ptr<ReactionRule> r) override;
  98. void add_surface_class(std::shared_ptr<SurfaceClass> sc) override;
  99. void add_release_site(std::shared_ptr<ReleaseSite> s) override;
  100. void add_geometry_object(std::shared_ptr<GeometryObject> o) override;
  101. void add_viz_output(std::shared_ptr<VizOutput> viz_output) override;
  102. void add_count(std::shared_ptr<Count> count) override;
  103. // TODO: this belongs to Instantiation
  104. std::shared_ptr<GeometryObject> get_geometry_object_with_id(const geometry_object_id_t id);
  105. // TODO: this belongs to Subsystem
  106. std::shared_ptr<ReactionRule> get_reaction_rule_with_fwd_id(const BNG::rxn_rule_id_t id);
  107. bool is_initialized() const {
  108. return initialized;
  109. }
  110. void error_if_initialized(const char* what) {
  111. if (initialized) {
  112. throw RuntimeError(S("It is not possible to add ") + what + " once a model was initialized.");
  113. }
  114. }
  115. void dump() const;
  116. const World* get_world() const {
  117. return world;
  118. }
  119. World* get_world() {
  120. return world;
  121. }
  122. private:
  123. void update_api_vertex_position_using_vertex_move(
  124. const VertexMoveInfo& move_info);
  125. void export_data_model_viz_or_full(
  126. const std::string& file,
  127. const bool only_for_visualization,
  128. const char* method_name
  129. );
  130. volatile bool initialized;
  131. World* world;
  132. Callbacks callbacks;
  133. std::vector<VertexMoveInfo> vertex_moves;
  134. // used in get_geometry_object_with_id
  135. std::map<geometry_object_id_t, std::shared_ptr<API::GeometryObject>> geometry_object_id_to_obj_cache;
  136. };
  137. } // namespace API
  138. } // namespace MCell
  139. #endif // API_MODEL_H