generator_structs.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 UTILS_DATA_MODEL_TO_PYMCELL_GENERATOR_STRUCTS_H_
  12. #define UTILS_DATA_MODEL_TO_PYMCELL_GENERATOR_STRUCTS_H_
  13. #include <string>
  14. #include <algorithm>
  15. #include <set>
  16. #include "json/json.h"
  17. #include "datamodel_defines.h"
  18. #include "generator_utils.h"
  19. namespace MCell {
  20. // auxiliary struct used when generating species or molecule types
  21. struct SpeciesOrMolType {
  22. SpeciesOrMolType(const std::string& name_, const bool is_species_ = true)
  23. : name(name_), is_species(is_species_) {
  24. }
  25. SpeciesOrMolType(const SpeciesOrMolType& other)
  26. : name(other.name), is_species(other.is_species) {
  27. }
  28. // ignores type
  29. bool operator == (const SpeciesOrMolType& other) const {
  30. return name == other.name;
  31. }
  32. std::string name;
  33. bool is_species; // mol type when false
  34. };
  35. struct IdLoc {
  36. IdLoc(const std::string& name_, const bool in_python_ = true)
  37. : name(name_), in_python(in_python_) {
  38. }
  39. // ignores type
  40. bool operator == (const IdLoc& other) const {
  41. return name == other.name;
  42. }
  43. std::string name;
  44. bool in_python; // BNGL when false
  45. };
  46. // data and configuration shared among generators
  47. struct SharedGenData {
  48. void reset() {
  49. input_file = "";
  50. output_files_prefix = "";
  51. debug_mode = false;
  52. testing_mode = false;
  53. bng_mode = false;
  54. not_overridable_python_params = false;
  55. unnamed_rxn_counter = 0;
  56. all_species_and_mol_type_names.clear();
  57. all_reaction_rules_names.clear();
  58. bngl_reaction_rules_used_in_observables.clear();
  59. all_count_term_names.clear();
  60. defined_python_objects.clear();
  61. surface_to_volume_compartments_map.clear();
  62. has_default_compartment_object = false;
  63. }
  64. uint unnamed_rxn_counter;
  65. std::string input_file;
  66. std::string output_files_prefix;
  67. bool bng_mode;
  68. bool debug_mode;
  69. bool testing_mode;
  70. std::vector<int> checkpoint_iterations;
  71. bool not_overridable_python_params;
  72. std::vector<SpeciesOrMolType> all_species_and_mol_type_names;
  73. std::vector<IdLoc> all_reaction_rules_names;
  74. std::vector<std::string> bngl_reaction_rules_used_in_observables;
  75. std::vector<std::string> all_count_term_names;
  76. // set in PythonGenerator::generate_geometry
  77. bool has_default_compartment_object;
  78. // set in MCell4Generator::analyze_and_generate_bngl_compartments
  79. std::set<std::string> used_compartments;
  80. // key is object name, value is class name
  81. std::map<std::string, std::string> defined_python_objects;
  82. // key is surface compartment name, value is volume compartment name
  83. std::map<std::string, std::string> surface_to_volume_compartments_map;
  84. // output file name to be used when loading observables from BNGL,
  85. // empty for DAT format
  86. std::string bng_observables_output_gdat_file;
  87. const SpeciesOrMolType* find_species_or_mol_type_info(const std::string& name) const {
  88. auto it = std::find(
  89. all_species_and_mol_type_names.begin(), all_species_and_mol_type_names.end(),
  90. SpeciesOrMolType(name));
  91. if (it != all_species_and_mol_type_names.end()) {
  92. return &*it;
  93. }
  94. else {
  95. return nullptr;
  96. }
  97. }
  98. const IdLoc* find_reaction_rule_info(const std::string& rxn_name) const {
  99. auto it = std::find(all_reaction_rules_names.begin(), all_reaction_rules_names.end(), IdLoc(rxn_name));
  100. if (it != all_reaction_rules_names.end()) {
  101. return &*it;
  102. }
  103. else {
  104. return nullptr;
  105. }
  106. }
  107. bool is_used_compartment(Json::Value& model_object) {
  108. const std::string& vol_comp = model_object[KEY_NAME].asString();
  109. assert(vol_comp != "");
  110. const std::string& surf_comp = model_object[KEY_MEMBRANE_NAME].asString();
  111. return used_compartments.count(vol_comp) != 0 || (surf_comp != "" && used_compartments.count(surf_comp) != 0);
  112. }
  113. bool is_already_defined(const std::string& obj_name, const char* class_name) {
  114. //return false;
  115. if (defined_python_objects.count(obj_name) != 0) {
  116. return defined_python_objects[obj_name] == std::string(class_name);
  117. }
  118. else {
  119. return false;
  120. }
  121. }
  122. // also adds
  123. void check_if_already_defined_and_add(const std::string& obj_name, const char* class_name) {
  124. //return;
  125. if (defined_python_objects.count(obj_name) != 0) {
  126. ERROR("Duplicate object name '" + obj_name + "', used for '" + class_name + "' and '" +
  127. defined_python_objects[obj_name] + "'. Object names must be unique.");
  128. }
  129. else {
  130. defined_python_objects[obj_name] = class_name;
  131. }
  132. }
  133. // mcell node of the loaded JSON file
  134. Json::Value mcell;
  135. };
  136. } // namespace MCell
  137. #endif /* UTILS_DATA_MODEL_TO_PYMCELL_GENERATOR_STRUCTS_H_ */