gen_chkpt_surf_mol.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_surf_mol.h"
  15. #include "api/chkpt_surf_mol.h"
  16. #include "api/geometry_object.h"
  17. #include "api/species.h"
  18. namespace MCell {
  19. namespace API {
  20. void GenChkptSurfMol::check_semantics() const {
  21. if (!is_set(pos)) {
  22. throw ValueError("Parameter 'pos' must be set.");
  23. }
  24. if (!is_set(orientation)) {
  25. throw ValueError("Parameter 'orientation' must be set.");
  26. }
  27. if (!is_set(geometry_object)) {
  28. throw ValueError("Parameter 'geometry_object' must be set.");
  29. }
  30. if (!is_set(wall_index)) {
  31. throw ValueError("Parameter 'wall_index' must be set.");
  32. }
  33. if (!is_set(grid_tile_index)) {
  34. throw ValueError("Parameter 'grid_tile_index' must be set.");
  35. }
  36. if (!is_set(id)) {
  37. throw ValueError("Parameter 'id' must be set.");
  38. }
  39. if (!is_set(species)) {
  40. throw ValueError("Parameter 'species' must be set.");
  41. }
  42. if (!is_set(diffusion_time)) {
  43. throw ValueError("Parameter 'diffusion_time' must be set.");
  44. }
  45. if (!is_set(birthday)) {
  46. throw ValueError("Parameter 'birthday' must be set.");
  47. }
  48. if (!is_set(flags)) {
  49. throw ValueError("Parameter 'flags' must be set.");
  50. }
  51. }
  52. void GenChkptSurfMol::set_initialized() {
  53. if (is_set(geometry_object)) {
  54. geometry_object->set_initialized();
  55. }
  56. if (is_set(species)) {
  57. species->set_initialized();
  58. }
  59. initialized = true;
  60. }
  61. void GenChkptSurfMol::set_all_attributes_as_default_or_unset() {
  62. class_name = "ChkptSurfMol";
  63. pos = VEC2_UNSET;
  64. orientation = Orientation::NOT_SET;
  65. geometry_object = nullptr;
  66. wall_index = INT_UNSET;
  67. grid_tile_index = INT_UNSET;
  68. id = INT_UNSET;
  69. species = nullptr;
  70. diffusion_time = FLT_UNSET;
  71. birthday = FLT_UNSET;
  72. flags = INT_UNSET;
  73. unimol_rxn_time = FLT_UNSET;
  74. }
  75. std::shared_ptr<ChkptSurfMol> GenChkptSurfMol::copy_chkpt_surf_mol() const {
  76. std::shared_ptr<ChkptSurfMol> res = std::make_shared<ChkptSurfMol>(DefaultCtorArgType());
  77. res->class_name = class_name;
  78. res->pos = pos;
  79. res->orientation = orientation;
  80. res->geometry_object = geometry_object;
  81. res->wall_index = wall_index;
  82. res->grid_tile_index = grid_tile_index;
  83. res->id = id;
  84. res->species = species;
  85. res->diffusion_time = diffusion_time;
  86. res->birthday = birthday;
  87. res->flags = flags;
  88. res->unimol_rxn_time = unimol_rxn_time;
  89. return res;
  90. }
  91. std::shared_ptr<ChkptSurfMol> GenChkptSurfMol::deepcopy_chkpt_surf_mol(py::dict) const {
  92. std::shared_ptr<ChkptSurfMol> res = std::make_shared<ChkptSurfMol>(DefaultCtorArgType());
  93. res->class_name = class_name;
  94. res->pos = pos;
  95. res->orientation = orientation;
  96. res->geometry_object = is_set(geometry_object) ? geometry_object->deepcopy_geometry_object() : nullptr;
  97. res->wall_index = wall_index;
  98. res->grid_tile_index = grid_tile_index;
  99. res->id = id;
  100. res->species = is_set(species) ? species->deepcopy_species() : nullptr;
  101. res->diffusion_time = diffusion_time;
  102. res->birthday = birthday;
  103. res->flags = flags;
  104. res->unimol_rxn_time = unimol_rxn_time;
  105. return res;
  106. }
  107. bool GenChkptSurfMol::__eq__(const ChkptSurfMol& other) const {
  108. return
  109. pos == other.pos &&
  110. orientation == other.orientation &&
  111. (
  112. (is_set(geometry_object)) ?
  113. (is_set(other.geometry_object) ?
  114. (geometry_object->__eq__(*other.geometry_object)) :
  115. false
  116. ) :
  117. (is_set(other.geometry_object) ?
  118. false :
  119. true
  120. )
  121. ) &&
  122. wall_index == other.wall_index &&
  123. grid_tile_index == other.grid_tile_index &&
  124. id == other.id &&
  125. (
  126. (is_set(species)) ?
  127. (is_set(other.species) ?
  128. (species->__eq__(*other.species)) :
  129. false
  130. ) :
  131. (is_set(other.species) ?
  132. false :
  133. true
  134. )
  135. ) &&
  136. diffusion_time == other.diffusion_time &&
  137. birthday == other.birthday &&
  138. flags == other.flags &&
  139. unimol_rxn_time == other.unimol_rxn_time;
  140. }
  141. bool GenChkptSurfMol::eq_nonarray_attributes(const ChkptSurfMol& other, const bool ignore_name) const {
  142. return
  143. pos == other.pos &&
  144. orientation == other.orientation &&
  145. (
  146. (is_set(geometry_object)) ?
  147. (is_set(other.geometry_object) ?
  148. (geometry_object->__eq__(*other.geometry_object)) :
  149. false
  150. ) :
  151. (is_set(other.geometry_object) ?
  152. false :
  153. true
  154. )
  155. ) &&
  156. wall_index == other.wall_index &&
  157. grid_tile_index == other.grid_tile_index &&
  158. id == other.id &&
  159. (
  160. (is_set(species)) ?
  161. (is_set(other.species) ?
  162. (species->__eq__(*other.species)) :
  163. false
  164. ) :
  165. (is_set(other.species) ?
  166. false :
  167. true
  168. )
  169. ) &&
  170. diffusion_time == other.diffusion_time &&
  171. birthday == other.birthday &&
  172. flags == other.flags &&
  173. unimol_rxn_time == other.unimol_rxn_time;
  174. }
  175. std::string GenChkptSurfMol::to_str(const bool all_details, const std::string ind) const {
  176. std::stringstream ss;
  177. ss << get_object_name() << ": " <<
  178. "pos=" << pos << ", " <<
  179. "orientation=" << orientation << ", " <<
  180. "\n" << ind + " " << "geometry_object=" << "(" << ((geometry_object != nullptr) ? geometry_object->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  181. "wall_index=" << wall_index << ", " <<
  182. "grid_tile_index=" << grid_tile_index << ", " <<
  183. "id=" << id << ", " <<
  184. "\n" << ind + " " << "species=" << "(" << ((species != nullptr) ? species->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  185. "diffusion_time=" << diffusion_time << ", " <<
  186. "birthday=" << birthday << ", " <<
  187. "flags=" << flags << ", " <<
  188. "unimol_rxn_time=" << unimol_rxn_time;
  189. return ss.str();
  190. }
  191. py::class_<ChkptSurfMol> define_pybinding_ChkptSurfMol(py::module& m) {
  192. return py::class_<ChkptSurfMol, BaseChkptMol, std::shared_ptr<ChkptSurfMol>>(m, "ChkptSurfMol", "Class representing a checkpointed surface molecule.\nNot to be used directly.\n")
  193. .def(
  194. py::init<
  195. const Vec2&,
  196. const Orientation,
  197. std::shared_ptr<GeometryObject>,
  198. const int,
  199. const int,
  200. const int,
  201. std::shared_ptr<Species>,
  202. const double,
  203. const double,
  204. const int,
  205. const double
  206. >(),
  207. py::arg("pos"),
  208. py::arg("orientation"),
  209. py::arg("geometry_object"),
  210. py::arg("wall_index"),
  211. py::arg("grid_tile_index"),
  212. py::arg("id"),
  213. py::arg("species"),
  214. py::arg("diffusion_time"),
  215. py::arg("birthday"),
  216. py::arg("flags"),
  217. py::arg("unimol_rxn_time") = FLT_UNSET
  218. )
  219. .def("check_semantics", &ChkptSurfMol::check_semantics)
  220. .def("__copy__", &ChkptSurfMol::copy_chkpt_surf_mol)
  221. .def("__deepcopy__", &ChkptSurfMol::deepcopy_chkpt_surf_mol, py::arg("memo"))
  222. .def("__str__", &ChkptSurfMol::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  223. .def("__eq__", &ChkptSurfMol::__eq__, py::arg("other"))
  224. .def("dump", &ChkptSurfMol::dump)
  225. .def_property("pos", &ChkptSurfMol::get_pos, &ChkptSurfMol::set_pos)
  226. .def_property("orientation", &ChkptSurfMol::get_orientation, &ChkptSurfMol::set_orientation)
  227. .def_property("geometry_object", &ChkptSurfMol::get_geometry_object, &ChkptSurfMol::set_geometry_object)
  228. .def_property("wall_index", &ChkptSurfMol::get_wall_index, &ChkptSurfMol::set_wall_index)
  229. .def_property("grid_tile_index", &ChkptSurfMol::get_grid_tile_index, &ChkptSurfMol::set_grid_tile_index)
  230. ;
  231. }
  232. std::string GenChkptSurfMol::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  233. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  234. return ctx.get_exported_name(this);
  235. }
  236. std::string exported_name = "chkpt_surf_mol_" + std::to_string(ctx.postinc_counter("chkpt_surf_mol"));
  237. if (!export_even_if_already_exported()) {
  238. ctx.add_exported(this, exported_name);
  239. }
  240. bool str_export = export_as_string_without_newlines();
  241. std::string nl = "";
  242. std::string ind = " ";
  243. std::stringstream ss;
  244. if (!str_export) {
  245. nl = "\n";
  246. ind = " ";
  247. ss << exported_name << " = ";
  248. }
  249. ss << "m.ChkptSurfMol(" << nl;
  250. ss << ind << "id = " << id << "," << nl;
  251. ss << ind << "species = " << species->export_to_python(out, ctx) << "," << nl;
  252. ss << ind << "diffusion_time = " << f_to_str(diffusion_time) << "," << nl;
  253. ss << ind << "birthday = " << f_to_str(birthday) << "," << nl;
  254. ss << ind << "flags = " << flags << "," << nl;
  255. if (unimol_rxn_time != FLT_UNSET) {
  256. ss << ind << "unimol_rxn_time = " << f_to_str(unimol_rxn_time) << "," << nl;
  257. }
  258. ss << ind << "pos = " << "m.Vec2(" << f_to_str(pos.u) << ", " << f_to_str(pos.v)<< ")," << nl;
  259. ss << ind << "orientation = " << orientation << "," << nl;
  260. ss << ind << "geometry_object = " << geometry_object->export_to_python(out, ctx) << "," << nl;
  261. ss << ind << "wall_index = " << wall_index << "," << nl;
  262. ss << ind << "grid_tile_index = " << grid_tile_index << "," << nl;
  263. ss << ")" << nl << nl;
  264. if (!str_export) {
  265. out << ss.str();
  266. return exported_name;
  267. }
  268. else {
  269. return ss.str();
  270. }
  271. }
  272. } // namespace API
  273. } // namespace MCell