gen_surface_property.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_surface_property.h"
  15. #include "api/surface_property.h"
  16. #include "api/complex.h"
  17. namespace MCell {
  18. namespace API {
  19. void GenSurfaceProperty::check_semantics() const {
  20. }
  21. void GenSurfaceProperty::set_initialized() {
  22. if (is_set(affected_complex_pattern)) {
  23. affected_complex_pattern->set_initialized();
  24. }
  25. initialized = true;
  26. }
  27. void GenSurfaceProperty::set_all_attributes_as_default_or_unset() {
  28. class_name = "SurfaceProperty";
  29. type = SurfacePropertyType::UNSET;
  30. affected_complex_pattern = nullptr;
  31. concentration = FLT_UNSET;
  32. }
  33. std::shared_ptr<SurfaceProperty> GenSurfaceProperty::copy_surface_property() const {
  34. std::shared_ptr<SurfaceProperty> res = std::make_shared<SurfaceProperty>(DefaultCtorArgType());
  35. res->class_name = class_name;
  36. res->type = type;
  37. res->affected_complex_pattern = affected_complex_pattern;
  38. res->concentration = concentration;
  39. return res;
  40. }
  41. std::shared_ptr<SurfaceProperty> GenSurfaceProperty::deepcopy_surface_property(py::dict) const {
  42. std::shared_ptr<SurfaceProperty> res = std::make_shared<SurfaceProperty>(DefaultCtorArgType());
  43. res->class_name = class_name;
  44. res->type = type;
  45. res->affected_complex_pattern = is_set(affected_complex_pattern) ? affected_complex_pattern->deepcopy_complex() : nullptr;
  46. res->concentration = concentration;
  47. return res;
  48. }
  49. bool GenSurfaceProperty::__eq__(const SurfaceProperty& other) const {
  50. return
  51. type == other.type &&
  52. (
  53. (is_set(affected_complex_pattern)) ?
  54. (is_set(other.affected_complex_pattern) ?
  55. (affected_complex_pattern->__eq__(*other.affected_complex_pattern)) :
  56. false
  57. ) :
  58. (is_set(other.affected_complex_pattern) ?
  59. false :
  60. true
  61. )
  62. ) &&
  63. concentration == other.concentration;
  64. }
  65. bool GenSurfaceProperty::eq_nonarray_attributes(const SurfaceProperty& other, const bool ignore_name) const {
  66. return
  67. type == other.type &&
  68. (
  69. (is_set(affected_complex_pattern)) ?
  70. (is_set(other.affected_complex_pattern) ?
  71. (affected_complex_pattern->__eq__(*other.affected_complex_pattern)) :
  72. false
  73. ) :
  74. (is_set(other.affected_complex_pattern) ?
  75. false :
  76. true
  77. )
  78. ) &&
  79. concentration == other.concentration;
  80. }
  81. std::string GenSurfaceProperty::to_str(const bool all_details, const std::string ind) const {
  82. std::stringstream ss;
  83. ss << get_object_name() << ": " <<
  84. "type=" << type << ", " <<
  85. "\n" << ind + " " << "affected_complex_pattern=" << "(" << ((affected_complex_pattern != nullptr) ? affected_complex_pattern->to_str(all_details, ind + " ") : "null" ) << ")" << ", " << "\n" << ind + " " <<
  86. "concentration=" << concentration;
  87. return ss.str();
  88. }
  89. py::class_<SurfaceProperty> define_pybinding_SurfaceProperty(py::module& m) {
  90. return py::class_<SurfaceProperty, std::shared_ptr<SurfaceProperty>>(m, "SurfaceProperty", "Single property for a SurfaceClass.")
  91. .def(
  92. py::init<
  93. const SurfacePropertyType,
  94. std::shared_ptr<Complex>,
  95. const double
  96. >(),
  97. py::arg("type") = SurfacePropertyType::UNSET,
  98. py::arg("affected_complex_pattern") = nullptr,
  99. py::arg("concentration") = FLT_UNSET
  100. )
  101. .def("check_semantics", &SurfaceProperty::check_semantics)
  102. .def("__copy__", &SurfaceProperty::copy_surface_property)
  103. .def("__deepcopy__", &SurfaceProperty::deepcopy_surface_property, py::arg("memo"))
  104. .def("__str__", &SurfaceProperty::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  105. .def("__eq__", &SurfaceProperty::__eq__, py::arg("other"))
  106. .def("dump", &SurfaceProperty::dump)
  107. .def_property("type", &SurfaceProperty::get_type, &SurfaceProperty::set_type, "Must be set. See SurfacePropertyType for options.\n")
  108. .def_property("affected_complex_pattern", &SurfaceProperty::get_affected_complex_pattern, &SurfaceProperty::set_affected_complex_pattern, "A complex pattern with optional orientation must be set.\nDefault orientation means that the pattern matches any orientation.\nFor concentration or flux clamp the orientation specifies on which side \nwill be the concentration held (UP is front or outside, DOWN is back or \ninside, and DEFAULT, ANY or NONE is on both sides).\nThe complex pattern must not use compartments.\n")
  109. .def_property("concentration", &SurfaceProperty::get_concentration, &SurfaceProperty::set_concentration, "Specifies concentration when type is SurfacePropertyType.CLAMP_CONCENTRATION or \nSurfacePropertyType.CLAMP_FLUX. Represents concentration of the imagined opposide side \nof the wall that has this concentration or flux clamped.\n")
  110. ;
  111. }
  112. std::string GenSurfaceProperty::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  113. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  114. return ctx.get_exported_name(this);
  115. }
  116. std::string exported_name = "surface_property_" + std::to_string(ctx.postinc_counter("surface_property"));
  117. if (!export_even_if_already_exported()) {
  118. ctx.add_exported(this, exported_name);
  119. }
  120. bool str_export = export_as_string_without_newlines();
  121. std::string nl = "";
  122. std::string ind = " ";
  123. std::stringstream ss;
  124. if (!str_export) {
  125. nl = "\n";
  126. ind = " ";
  127. ss << exported_name << " = ";
  128. }
  129. ss << "m.SurfaceProperty(" << nl;
  130. if (type != SurfacePropertyType::UNSET) {
  131. ss << ind << "type = " << type << "," << nl;
  132. }
  133. if (is_set(affected_complex_pattern)) {
  134. ss << ind << "affected_complex_pattern = " << affected_complex_pattern->export_to_python(out, ctx) << "," << nl;
  135. }
  136. if (concentration != FLT_UNSET) {
  137. ss << ind << "concentration = " << f_to_str(concentration) << "," << nl;
  138. }
  139. ss << ")" << nl << nl;
  140. if (!str_export) {
  141. out << ss.str();
  142. return exported_name;
  143. }
  144. else {
  145. return ss.str();
  146. }
  147. }
  148. } // namespace API
  149. } // namespace MCell