gen_base_chkpt_mol.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2021 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. #include <sstream>
  12. #include "api/pybind11_stl_include.h"
  13. #include "api/python_export_utils.h"
  14. #include "gen_base_chkpt_mol.h"
  15. #include "api/base_chkpt_mol.h"
  16. #include "api/species.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenBaseChkptMol::check_semantics() const {
  20. if (!is_set(id)) {
  21. throw ValueError("Parameter 'id' must be set.");
  22. }
  23. if (!is_set(species)) {
  24. throw ValueError("Parameter 'species' must be set.");
  25. }
  26. if (!is_set(diffusion_time)) {
  27. throw ValueError("Parameter 'diffusion_time' must be set.");
  28. }
  29. if (!is_set(birthday)) {
  30. throw ValueError("Parameter 'birthday' must be set.");
  31. }
  32. if (!is_set(flags)) {
  33. throw ValueError("Parameter 'flags' must be set.");
  34. }
  35. }
  36. void GenBaseChkptMol::set_initialized() {
  37. if (is_set(species)) {
  38. species->set_initialized();
  39. }
  40. initialized = true;
  41. }
  42. void GenBaseChkptMol::set_all_attributes_as_default_or_unset() {
  43. class_name = "BaseChkptMol";
  44. id = INT_UNSET;
  45. species = nullptr;
  46. diffusion_time = FLT_UNSET;
  47. birthday = FLT_UNSET;
  48. flags = INT_UNSET;
  49. unimol_rxn_time = FLT_UNSET;
  50. }
  51. std::shared_ptr<BaseChkptMol> GenBaseChkptMol::copy_base_chkpt_mol() const {
  52. std::shared_ptr<BaseChkptMol> res = std::make_shared<BaseChkptMol>(DefaultCtorArgType());
  53. res->class_name = class_name;
  54. res->id = id;
  55. res->species = species;
  56. res->diffusion_time = diffusion_time;
  57. res->birthday = birthday;
  58. res->flags = flags;
  59. res->unimol_rxn_time = unimol_rxn_time;
  60. return res;
  61. }
  62. std::shared_ptr<BaseChkptMol> GenBaseChkptMol::deepcopy_base_chkpt_mol(py::dict) const {
  63. std::shared_ptr<BaseChkptMol> res = std::make_shared<BaseChkptMol>(DefaultCtorArgType());
  64. res->class_name = class_name;
  65. res->id = id;
  66. res->species = is_set(species) ? species->deepcopy_species() : nullptr;
  67. res->diffusion_time = diffusion_time;
  68. res->birthday = birthday;
  69. res->flags = flags;
  70. res->unimol_rxn_time = unimol_rxn_time;
  71. return res;
  72. }
  73. bool GenBaseChkptMol::__eq__(const BaseChkptMol& other) const {
  74. return
  75. id == other.id &&
  76. (
  77. (is_set(species)) ?
  78. (is_set(other.species) ?
  79. (species->__eq__(*other.species)) :
  80. false
  81. ) :
  82. (is_set(other.species) ?
  83. false :
  84. true
  85. )
  86. ) &&
  87. diffusion_time == other.diffusion_time &&
  88. birthday == other.birthday &&
  89. flags == other.flags &&
  90. unimol_rxn_time == other.unimol_rxn_time;
  91. }
  92. bool GenBaseChkptMol::eq_nonarray_attributes(const BaseChkptMol& other, const bool ignore_name) const {
  93. return
  94. id == other.id &&
  95. (
  96. (is_set(species)) ?
  97. (is_set(other.species) ?
  98. (species->__eq__(*other.species)) :
  99. false
  100. ) :
  101. (is_set(other.species) ?
  102. false :
  103. true
  104. )
  105. ) &&
  106. diffusion_time == other.diffusion_time &&
  107. birthday == other.birthday &&
  108. flags == other.flags &&
  109. unimol_rxn_time == other.unimol_rxn_time;
  110. }
  111. std::string GenBaseChkptMol::to_str(const bool all_details, const std::string ind) const {
  112. std::stringstream ss;
  113. ss << get_object_name() << ": " <<
  114. "id=" << id << ", " <<
  115. "\n" << ind + " " << "species=" << "(" << ((species != nullptr) ? species->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  116. "diffusion_time=" << diffusion_time << ", " <<
  117. "birthday=" << birthday << ", " <<
  118. "flags=" << flags << ", " <<
  119. "unimol_rxn_time=" << unimol_rxn_time;
  120. return ss.str();
  121. }
  122. py::class_<BaseChkptMol> define_pybinding_BaseChkptMol(py::module& m) {
  123. return py::class_<BaseChkptMol, std::shared_ptr<BaseChkptMol>>(m, "BaseChkptMol", "Base class for checkpointed molecules.\nNot to be used directly. All times are in seconds.\n")
  124. .def(
  125. py::init<
  126. const int,
  127. std::shared_ptr<Species>,
  128. const double,
  129. const double,
  130. const int,
  131. const double
  132. >(),
  133. py::arg("id"),
  134. py::arg("species"),
  135. py::arg("diffusion_time"),
  136. py::arg("birthday"),
  137. py::arg("flags"),
  138. py::arg("unimol_rxn_time") = FLT_UNSET
  139. )
  140. .def("check_semantics", &BaseChkptMol::check_semantics)
  141. .def("__copy__", &BaseChkptMol::copy_base_chkpt_mol)
  142. .def("__deepcopy__", &BaseChkptMol::deepcopy_base_chkpt_mol, py::arg("memo"))
  143. .def("__str__", &BaseChkptMol::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  144. .def("__eq__", &BaseChkptMol::__eq__, py::arg("other"))
  145. .def("dump", &BaseChkptMol::dump)
  146. .def_property("id", &BaseChkptMol::get_id, &BaseChkptMol::set_id)
  147. .def_property("species", &BaseChkptMol::get_species, &BaseChkptMol::set_species)
  148. .def_property("diffusion_time", &BaseChkptMol::get_diffusion_time, &BaseChkptMol::set_diffusion_time)
  149. .def_property("birthday", &BaseChkptMol::get_birthday, &BaseChkptMol::set_birthday)
  150. .def_property("flags", &BaseChkptMol::get_flags, &BaseChkptMol::set_flags)
  151. .def_property("unimol_rxn_time", &BaseChkptMol::get_unimol_rxn_time, &BaseChkptMol::set_unimol_rxn_time)
  152. ;
  153. }
  154. std::string GenBaseChkptMol::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  155. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  156. return ctx.get_exported_name(this);
  157. }
  158. std::string exported_name = "base_chkpt_mol_" + std::to_string(ctx.postinc_counter("base_chkpt_mol"));
  159. if (!export_even_if_already_exported()) {
  160. ctx.add_exported(this, exported_name);
  161. }
  162. bool str_export = export_as_string_without_newlines();
  163. std::string nl = "";
  164. std::string ind = " ";
  165. std::stringstream ss;
  166. if (!str_export) {
  167. nl = "\n";
  168. ind = " ";
  169. ss << exported_name << " = ";
  170. }
  171. ss << "m.BaseChkptMol(" << nl;
  172. ss << ind << "id = " << id << "," << nl;
  173. ss << ind << "species = " << species->export_to_python(out, ctx) << "," << nl;
  174. ss << ind << "diffusion_time = " << f_to_str(diffusion_time) << "," << nl;
  175. ss << ind << "birthday = " << f_to_str(birthday) << "," << nl;
  176. ss << ind << "flags = " << flags << "," << nl;
  177. if (unimol_rxn_time != FLT_UNSET) {
  178. ss << ind << "unimol_rxn_time = " << f_to_str(unimol_rxn_time) << "," << nl;
  179. }
  180. ss << ")" << nl << nl;
  181. if (!str_export) {
  182. out << ss.str();
  183. return exported_name;
  184. }
  185. else {
  186. return ss.str();
  187. }
  188. }
  189. } // namespace API
  190. } // namespace MCell