gen_initial_surface_release.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_initial_surface_release.h"
  15. #include "api/initial_surface_release.h"
  16. #include "api/complex.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenInitialSurfaceRelease::check_semantics() const {
  20. if (!is_set(complex)) {
  21. throw ValueError("Parameter 'complex' must be set.");
  22. }
  23. }
  24. void GenInitialSurfaceRelease::set_initialized() {
  25. if (is_set(complex)) {
  26. complex->set_initialized();
  27. }
  28. initialized = true;
  29. }
  30. void GenInitialSurfaceRelease::set_all_attributes_as_default_or_unset() {
  31. class_name = "InitialSurfaceRelease";
  32. complex = nullptr;
  33. number_to_release = INT_UNSET;
  34. density = FLT_UNSET;
  35. }
  36. std::shared_ptr<InitialSurfaceRelease> GenInitialSurfaceRelease::copy_initial_surface_release() const {
  37. std::shared_ptr<InitialSurfaceRelease> res = std::make_shared<InitialSurfaceRelease>(DefaultCtorArgType());
  38. res->class_name = class_name;
  39. res->complex = complex;
  40. res->number_to_release = number_to_release;
  41. res->density = density;
  42. return res;
  43. }
  44. std::shared_ptr<InitialSurfaceRelease> GenInitialSurfaceRelease::deepcopy_initial_surface_release(py::dict) const {
  45. std::shared_ptr<InitialSurfaceRelease> res = std::make_shared<InitialSurfaceRelease>(DefaultCtorArgType());
  46. res->class_name = class_name;
  47. res->complex = is_set(complex) ? complex->deepcopy_complex() : nullptr;
  48. res->number_to_release = number_to_release;
  49. res->density = density;
  50. return res;
  51. }
  52. bool GenInitialSurfaceRelease::__eq__(const InitialSurfaceRelease& 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. number_to_release == other.number_to_release &&
  66. density == other.density;
  67. }
  68. bool GenInitialSurfaceRelease::eq_nonarray_attributes(const InitialSurfaceRelease& other, const bool ignore_name) const {
  69. return
  70. (
  71. (is_set(complex)) ?
  72. (is_set(other.complex) ?
  73. (complex->__eq__(*other.complex)) :
  74. false
  75. ) :
  76. (is_set(other.complex) ?
  77. false :
  78. true
  79. )
  80. ) &&
  81. number_to_release == other.number_to_release &&
  82. density == other.density;
  83. }
  84. std::string GenInitialSurfaceRelease::to_str(const bool all_details, const std::string ind) const {
  85. std::stringstream ss;
  86. ss << get_object_name() << ": " <<
  87. "\n" << ind + " " << "complex=" << "(" << ((complex != nullptr) ? complex->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  88. "number_to_release=" << number_to_release << ", " <<
  89. "density=" << density;
  90. return ss.str();
  91. }
  92. py::class_<InitialSurfaceRelease> define_pybinding_InitialSurfaceRelease(py::module& m) {
  93. return py::class_<InitialSurfaceRelease, std::shared_ptr<InitialSurfaceRelease>>(m, "InitialSurfaceRelease", "Defines molecules to be released onto a SurfaceRegion right when simulation starts")
  94. .def(
  95. py::init<
  96. std::shared_ptr<Complex>,
  97. const int,
  98. const double
  99. >(),
  100. py::arg("complex"),
  101. py::arg("number_to_release") = INT_UNSET,
  102. py::arg("density") = FLT_UNSET
  103. )
  104. .def("check_semantics", &InitialSurfaceRelease::check_semantics)
  105. .def("__copy__", &InitialSurfaceRelease::copy_initial_surface_release)
  106. .def("__deepcopy__", &InitialSurfaceRelease::deepcopy_initial_surface_release, py::arg("memo"))
  107. .def("__str__", &InitialSurfaceRelease::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  108. .def("__eq__", &InitialSurfaceRelease::__eq__, py::arg("other"))
  109. .def("dump", &InitialSurfaceRelease::dump)
  110. .def_property("complex", &InitialSurfaceRelease::get_complex, &InitialSurfaceRelease::set_complex, "Defines the species of the molecule that will be released.\n")
  111. .def_property("number_to_release", &InitialSurfaceRelease::get_number_to_release, &InitialSurfaceRelease::set_number_to_release, "Number of molecules to be released onto a region,\nonly one of number_to_release and density can be set.\n")
  112. .def_property("density", &InitialSurfaceRelease::get_density, &InitialSurfaceRelease::set_density, "Density of molecules to be released onto a region,\nonly one of number_to_release and density can be set.\n")
  113. ;
  114. }
  115. std::string GenInitialSurfaceRelease::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  116. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  117. return ctx.get_exported_name(this);
  118. }
  119. std::string exported_name = "initial_surface_release_" + std::to_string(ctx.postinc_counter("initial_surface_release"));
  120. if (!export_even_if_already_exported()) {
  121. ctx.add_exported(this, exported_name);
  122. }
  123. bool str_export = export_as_string_without_newlines();
  124. std::string nl = "";
  125. std::string ind = " ";
  126. std::stringstream ss;
  127. if (!str_export) {
  128. nl = "\n";
  129. ind = " ";
  130. ss << exported_name << " = ";
  131. }
  132. ss << "m.InitialSurfaceRelease(" << nl;
  133. ss << ind << "complex = " << complex->export_to_python(out, ctx) << "," << nl;
  134. if (number_to_release != INT_UNSET) {
  135. ss << ind << "number_to_release = " << number_to_release << "," << nl;
  136. }
  137. if (density != FLT_UNSET) {
  138. ss << ind << "density = " << f_to_str(density) << "," << nl;
  139. }
  140. ss << ")" << nl << nl;
  141. if (!str_export) {
  142. out << ss.str();
  143. return exported_name;
  144. }
  145. else {
  146. return ss.str();
  147. }
  148. }
  149. } // namespace API
  150. } // namespace MCell