gen_chkpt_vol_mol.cpp 6.5 KB

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