gen_molecule_release_info.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_release_info.h"
  15. #include "api/molecule_release_info.h"
  16. #include "api/complex.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenMoleculeReleaseInfo::check_semantics() const {
  20. if (!is_set(complex)) {
  21. throw ValueError("Parameter 'complex' must be set.");
  22. }
  23. if (!is_set(location)) {
  24. throw ValueError("Parameter 'location' must be set and the value must not be an empty list.");
  25. }
  26. }
  27. void GenMoleculeReleaseInfo::set_initialized() {
  28. if (is_set(complex)) {
  29. complex->set_initialized();
  30. }
  31. initialized = true;
  32. }
  33. void GenMoleculeReleaseInfo::set_all_attributes_as_default_or_unset() {
  34. class_name = "MoleculeReleaseInfo";
  35. complex = nullptr;
  36. location = std::vector<double>();
  37. }
  38. std::shared_ptr<MoleculeReleaseInfo> GenMoleculeReleaseInfo::copy_molecule_release_info() const {
  39. std::shared_ptr<MoleculeReleaseInfo> res = std::make_shared<MoleculeReleaseInfo>(DefaultCtorArgType());
  40. res->class_name = class_name;
  41. res->complex = complex;
  42. res->location = location;
  43. return res;
  44. }
  45. std::shared_ptr<MoleculeReleaseInfo> GenMoleculeReleaseInfo::deepcopy_molecule_release_info(py::dict) const {
  46. std::shared_ptr<MoleculeReleaseInfo> res = std::make_shared<MoleculeReleaseInfo>(DefaultCtorArgType());
  47. res->class_name = class_name;
  48. res->complex = is_set(complex) ? complex->deepcopy_complex() : nullptr;
  49. res->location = location;
  50. return res;
  51. }
  52. bool GenMoleculeReleaseInfo::__eq__(const MoleculeReleaseInfo& other) const {
  53. return
  54. (
  55. (is_set(complex)) ?
  56. (is_set(other.complex) ?
  57. (complex->__eq__(*other.complex)) :
  58. false
  59. ) :
  60. (is_set(other.complex) ?
  61. false :
  62. true
  63. )
  64. ) &&
  65. location == other.location;
  66. }
  67. bool GenMoleculeReleaseInfo::eq_nonarray_attributes(const MoleculeReleaseInfo& other, const bool ignore_name) const {
  68. return
  69. (
  70. (is_set(complex)) ?
  71. (is_set(other.complex) ?
  72. (complex->__eq__(*other.complex)) :
  73. false
  74. ) :
  75. (is_set(other.complex) ?
  76. false :
  77. true
  78. )
  79. ) &&
  80. true /*location*/;
  81. }
  82. std::string GenMoleculeReleaseInfo::to_str(const bool all_details, const std::string ind) const {
  83. std::stringstream ss;
  84. ss << get_object_name() << ": " <<
  85. "\n" << ind + " " << "complex=" << "(" << ((complex != nullptr) ? complex->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  86. "location=" << vec_nonptr_to_str(location, all_details, ind + " ");
  87. return ss.str();
  88. }
  89. py::class_<MoleculeReleaseInfo> define_pybinding_MoleculeReleaseInfo(py::module& m) {
  90. return py::class_<MoleculeReleaseInfo, std::shared_ptr<MoleculeReleaseInfo>>(m, "MoleculeReleaseInfo", "Defines a pair (molecule, location). Used in ReleaseSite when its shape is Shape.LIST.\n")
  91. .def(
  92. py::init<
  93. std::shared_ptr<Complex>,
  94. const std::vector<double>
  95. >(),
  96. py::arg("complex"),
  97. py::arg("location")
  98. )
  99. .def("check_semantics", &MoleculeReleaseInfo::check_semantics)
  100. .def("__copy__", &MoleculeReleaseInfo::copy_molecule_release_info)
  101. .def("__deepcopy__", &MoleculeReleaseInfo::deepcopy_molecule_release_info, py::arg("memo"))
  102. .def("__str__", &MoleculeReleaseInfo::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  103. .def("__eq__", &MoleculeReleaseInfo::__eq__, py::arg("other"))
  104. .def("dump", &MoleculeReleaseInfo::dump)
  105. .def_property("complex", &MoleculeReleaseInfo::get_complex, &MoleculeReleaseInfo::set_complex, "Complex instance defining the molecule that will be released.\nOrientation of the complex instance is used to define orientation of the released molecule,\nwhen Orientation.DEFAULT is set, volume molecules are released with Orientation.NONE and\nsurface molecules are released with Orientation.UP.\nCompartment must not be set because this specific release definition states the location. \n")
  106. .def_property("location", &MoleculeReleaseInfo::get_location, &MoleculeReleaseInfo::set_location, py::return_value_policy::reference, "3D position where the molecule will be released. \nIf a molecule has a 2D diffusion constant, it will be\nplaced on the surface closest to the coordinate given. \nArgument must have exactly three floating point values [x, y, z].\n \n")
  107. ;
  108. }
  109. std::string GenMoleculeReleaseInfo::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  110. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  111. return ctx.get_exported_name(this);
  112. }
  113. std::string exported_name = "molecule_release_info_" + std::to_string(ctx.postinc_counter("molecule_release_info"));
  114. if (!export_even_if_already_exported()) {
  115. ctx.add_exported(this, exported_name);
  116. }
  117. bool str_export = export_as_string_without_newlines();
  118. std::string nl = "";
  119. std::string ind = " ";
  120. std::stringstream ss;
  121. if (!str_export) {
  122. nl = "\n";
  123. ind = " ";
  124. ss << exported_name << " = ";
  125. }
  126. ss << "m.MoleculeReleaseInfo(" << nl;
  127. ss << ind << "complex = " << complex->export_to_python(out, ctx) << "," << nl;
  128. ss << ind << "location = " << export_vec_location(out, ctx, exported_name) << "," << nl;
  129. ss << ")" << nl << nl;
  130. if (!str_export) {
  131. out << ss.str();
  132. return exported_name;
  133. }
  134. else {
  135. return ss.str();
  136. }
  137. }
  138. std::string GenMoleculeReleaseInfo::export_vec_location(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  139. // does not print the array itself to 'out' and returns the whole list
  140. std::stringstream ss;
  141. ss << "[";
  142. for (size_t i = 0; i < location.size(); i++) {
  143. const auto& item = location[i];
  144. if (i == 0) {
  145. ss << " ";
  146. }
  147. else if (i % 16 == 0) {
  148. ss << "\n ";
  149. }
  150. ss << f_to_str(item) << ", ";
  151. }
  152. ss << "]";
  153. return ss.str();
  154. }
  155. } // namespace API
  156. } // namespace MCell