gen_molecule.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_molecule.h"
  15. #include "api/molecule.h"
  16. #include "api/geometry_object.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenMolecule::check_semantics() const {
  20. }
  21. void GenMolecule::set_initialized() {
  22. if (is_set(geometry_object)) {
  23. geometry_object->set_initialized();
  24. }
  25. initialized = true;
  26. }
  27. void GenMolecule::set_all_attributes_as_default_or_unset() {
  28. class_name = "Molecule";
  29. id = ID_INVALID;
  30. type = MoleculeType::UNSET;
  31. species_id = ID_INVALID;
  32. pos3d = std::vector<double>();
  33. orientation = Orientation::NOT_SET;
  34. pos2d = std::vector<double>();
  35. geometry_object = nullptr;
  36. wall_index = -1;
  37. }
  38. std::shared_ptr<Molecule> GenMolecule::copy_molecule() const {
  39. std::shared_ptr<Molecule> res = std::make_shared<Molecule>(DefaultCtorArgType());
  40. res->class_name = class_name;
  41. res->id = id;
  42. res->type = type;
  43. res->species_id = species_id;
  44. res->pos3d = pos3d;
  45. res->orientation = orientation;
  46. res->pos2d = pos2d;
  47. res->geometry_object = geometry_object;
  48. res->wall_index = wall_index;
  49. return res;
  50. }
  51. std::shared_ptr<Molecule> GenMolecule::deepcopy_molecule(py::dict) const {
  52. std::shared_ptr<Molecule> res = std::make_shared<Molecule>(DefaultCtorArgType());
  53. res->class_name = class_name;
  54. res->id = id;
  55. res->type = type;
  56. res->species_id = species_id;
  57. res->pos3d = pos3d;
  58. res->orientation = orientation;
  59. res->pos2d = pos2d;
  60. res->geometry_object = is_set(geometry_object) ? geometry_object->deepcopy_geometry_object() : nullptr;
  61. res->wall_index = wall_index;
  62. return res;
  63. }
  64. bool GenMolecule::__eq__(const Molecule& other) const {
  65. return
  66. id == other.id &&
  67. type == other.type &&
  68. species_id == other.species_id &&
  69. pos3d == other.pos3d &&
  70. orientation == other.orientation &&
  71. pos2d == other.pos2d &&
  72. (
  73. (is_set(geometry_object)) ?
  74. (is_set(other.geometry_object) ?
  75. (geometry_object->__eq__(*other.geometry_object)) :
  76. false
  77. ) :
  78. (is_set(other.geometry_object) ?
  79. false :
  80. true
  81. )
  82. ) &&
  83. wall_index == other.wall_index;
  84. }
  85. bool GenMolecule::eq_nonarray_attributes(const Molecule& other, const bool ignore_name) const {
  86. return
  87. id == other.id &&
  88. type == other.type &&
  89. species_id == other.species_id &&
  90. true /*pos3d*/ &&
  91. orientation == other.orientation &&
  92. true /*pos2d*/ &&
  93. (
  94. (is_set(geometry_object)) ?
  95. (is_set(other.geometry_object) ?
  96. (geometry_object->__eq__(*other.geometry_object)) :
  97. false
  98. ) :
  99. (is_set(other.geometry_object) ?
  100. false :
  101. true
  102. )
  103. ) &&
  104. wall_index == other.wall_index;
  105. }
  106. std::string GenMolecule::to_str(const bool all_details, const std::string ind) const {
  107. std::stringstream ss;
  108. ss << get_object_name() << ": " <<
  109. "id=" << id << ", " <<
  110. "type=" << type << ", " <<
  111. "species_id=" << species_id << ", " <<
  112. "pos3d=" << vec_nonptr_to_str(pos3d, all_details, ind + " ") << ", " <<
  113. "orientation=" << orientation << ", " <<
  114. "pos2d=" << vec_nonptr_to_str(pos2d, all_details, ind + " ") << ", " <<
  115. "\n" << ind + " " << "geometry_object=" << "(" << ((geometry_object != nullptr) ? geometry_object->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  116. "wall_index=" << wall_index;
  117. return ss.str();
  118. }
  119. py::class_<Molecule> define_pybinding_Molecule(py::module& m) {
  120. return py::class_<Molecule, std::shared_ptr<Molecule>>(m, "Molecule", "Representation of a molecule obtained from Model \nduring simulation obtained through Model.get_molecule.\nChanges through changing attributes of this object are not allowed except \nfor complete removal of this molecule. \n")
  121. .def(
  122. py::init<
  123. >()
  124. )
  125. .def("check_semantics", &Molecule::check_semantics)
  126. .def("__copy__", &Molecule::copy_molecule)
  127. .def("__deepcopy__", &Molecule::deepcopy_molecule, py::arg("memo"))
  128. .def("__str__", &Molecule::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  129. .def("__eq__", &Molecule::__eq__, py::arg("other"))
  130. .def("remove", &Molecule::remove, "Removes this molecule from simulation. Any subsequent modifications\nof this molecule won't have any effect.\n")
  131. .def("dump", &Molecule::dump)
  132. .def_property("id", &Molecule::get_id, &Molecule::set_id, "Unique id of this molecule. MCell assigns this unique id to each created \nmolecule. All reactions change ID of molecules even in reactions such as \nA@CP -> A@EC.\n")
  133. .def_property("type", &Molecule::get_type, &Molecule::set_type, "Type of this molecule, either volume or surface. \n")
  134. .def_property("species_id", &Molecule::get_species_id, &Molecule::set_species_id, "Species id of this molecule.\nThe species_id value is only temporary. Species ids are created and removed as needed\nautomatically and if this species is removed, this particular species_id value \nwon't be valid. This can happen when a following iteration is simulated.\n")
  135. .def_property("pos3d", &Molecule::get_pos3d, &Molecule::set_pos3d, py::return_value_policy::reference, "Contains position of a molecule in 3D space. \n")
  136. .def_property("orientation", &Molecule::get_orientation, &Molecule::set_orientation, "Contains orientation for surface molecule. Volume molecules \nhave always orientation set to Orientation.NONE.\n")
  137. .def_property("pos2d", &Molecule::get_pos2d, &Molecule::set_pos2d, py::return_value_policy::reference, "Set only for surface molecules. Position on a wall in UV coordinates \nrelative to the triangle of the wall.\n \n")
  138. .def_property("geometry_object", &Molecule::get_geometry_object, &Molecule::set_geometry_object, "Set only for surface molecules.\nIs set to a reference to the geometry object on whose surface is the molecule located.\n")
  139. .def_property("wall_index", &Molecule::get_wall_index, &Molecule::set_wall_index, "Set only for surface molecules.\nIndex of wall belonging to the geometry_object where is the \nmolecule located. \n \n")
  140. ;
  141. }
  142. } // namespace API
  143. } // namespace MCell