reaction_rule.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_REACTION_RULE_H
  12. #define API_REACTION_RULE_H
  13. #include "generated/gen_reaction_rule.h"
  14. #include "api/api_common.h"
  15. #include "bng/bng.h"
  16. namespace MCell {
  17. class World;
  18. namespace API {
  19. class ReactionRule: public GenReactionRule {
  20. public:
  21. REACTION_RULE_CTOR()
  22. void postprocess_in_ctor() override {
  23. set_all_custom_attributes_to_default();
  24. }
  25. void set_all_custom_attributes_to_default() override {
  26. fwd_rxn_rule_id = BNG::RXN_RULE_ID_INVALID;
  27. rev_rxn_rule_id = BNG::RXN_RULE_ID_INVALID;
  28. }
  29. void check_semantics() const override {
  30. GenReactionRule::check_semantics();
  31. if (is_set(name) && is_set(rev_rate)) {
  32. if (!is_set(rev_name)) {
  33. throw ValueError(
  34. S("If name of a reaction is set, reversible reaction must have its ") + NAME_REV_NAME + " set as well."
  35. " Error for " + name + "."
  36. );
  37. }
  38. }
  39. if (is_set(rev_name) && !is_set(rev_rate)) {
  40. throw ValueError(
  41. S("Parameter ") + NAME_REV_NAME + " must not be set when " + NAME_REV_RATE + " is not set."
  42. " Error for " + name + "."
  43. );
  44. }
  45. if (is_set(variable_rate)) {
  46. if (is_set(fwd_rate) || is_set(rev_rate)) {
  47. throw ValueError(S("Variable rates cannot be set along with fwd_rate or rev_rate in the constructor of ") +
  48. NAME_CLASS_REACTION_RULE + ".");
  49. }
  50. check_variable_rate();
  51. }
  52. }
  53. void check_variable_rate() const {
  54. for (auto& time_and_rate: variable_rate) {
  55. if (time_and_rate.size() != 2) {
  56. std::string msg = "[]";
  57. if (time_and_rate.size() >= 1) {
  58. msg = " item with time " + std::to_string(time_and_rate[0]);
  59. }
  60. throw ValueError("Variable rate array must contain pairs [time, rate], error for " + msg + ".");
  61. }
  62. }
  63. }
  64. bool __eq__(const ReactionRule& other) const override;
  65. std::string to_bngl_str() const override {
  66. return to_bngl_str_w_orientation();
  67. }
  68. void set_fwd_rate(const double new_fwd_rate_) override;
  69. void set_rev_rate(const double new_rev_rate_) override;
  70. void set_variable_rate(const std::vector<std::vector<double>> new_variable_rate_) override;
  71. // added methods
  72. bool is_reversible() const {
  73. return is_set(rev_rate);
  74. }
  75. bool eq_reactants_and_products(const ReactionRule& other) const;
  76. std::string to_bngl_str_w_orientation(bool replace_orientation_w_up_down_compartments = false) const;
  77. std::string get_canonical_name() const;
  78. bool warn_if_adding_identical_object() const { return true; }
  79. // simulation engine mapping
  80. bool is_initialized() const {
  81. assert((fwd_rxn_rule_id == BNG::RXN_RULE_ID_INVALID) == (world == nullptr) &&
  82. "When initialized, both values must be set");
  83. return world != nullptr;
  84. }
  85. BNG::rxn_rule_id_t fwd_rxn_rule_id;
  86. BNG::rxn_rule_id_t rev_rxn_rule_id;
  87. World* world;
  88. private:
  89. void update_reaction_rate(const BNG::rxn_rule_id_t rxn_rule_id, const double new_rate);
  90. };
  91. } // namespace API
  92. } // namespace MCell
  93. #endif // API_REACTION_RULE_H