gen_rng_state.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_rng_state.h"
  15. #include "api/rng_state.h"
  16. namespace MCell {
  17. namespace API {
  18. void GenRngState::check_semantics() const {
  19. if (!is_set(randcnt)) {
  20. throw ValueError("Parameter 'randcnt' must be set.");
  21. }
  22. if (!is_set(aa)) {
  23. throw ValueError("Parameter 'aa' must be set.");
  24. }
  25. if (!is_set(bb)) {
  26. throw ValueError("Parameter 'bb' must be set.");
  27. }
  28. if (!is_set(cc)) {
  29. throw ValueError("Parameter 'cc' must be set.");
  30. }
  31. if (!is_set(randslr)) {
  32. throw ValueError("Parameter 'randslr' must be set and the value must not be an empty list.");
  33. }
  34. if (!is_set(mm)) {
  35. throw ValueError("Parameter 'mm' must be set and the value must not be an empty list.");
  36. }
  37. if (!is_set(rngblocks)) {
  38. throw ValueError("Parameter 'rngblocks' must be set.");
  39. }
  40. }
  41. void GenRngState::set_initialized() {
  42. initialized = true;
  43. }
  44. void GenRngState::set_all_attributes_as_default_or_unset() {
  45. class_name = "RngState";
  46. randcnt = 0;
  47. aa = 0;
  48. bb = 0;
  49. cc = 0;
  50. randslr = std::vector<uint64_t>();
  51. mm = std::vector<uint64_t>();
  52. rngblocks = 0;
  53. }
  54. std::shared_ptr<RngState> GenRngState::copy_rng_state() const {
  55. std::shared_ptr<RngState> res = std::make_shared<RngState>(DefaultCtorArgType());
  56. res->class_name = class_name;
  57. res->randcnt = randcnt;
  58. res->aa = aa;
  59. res->bb = bb;
  60. res->cc = cc;
  61. res->randslr = randslr;
  62. res->mm = mm;
  63. res->rngblocks = rngblocks;
  64. return res;
  65. }
  66. std::shared_ptr<RngState> GenRngState::deepcopy_rng_state(py::dict) const {
  67. std::shared_ptr<RngState> res = std::make_shared<RngState>(DefaultCtorArgType());
  68. res->class_name = class_name;
  69. res->randcnt = randcnt;
  70. res->aa = aa;
  71. res->bb = bb;
  72. res->cc = cc;
  73. res->randslr = randslr;
  74. res->mm = mm;
  75. res->rngblocks = rngblocks;
  76. return res;
  77. }
  78. bool GenRngState::__eq__(const RngState& other) const {
  79. return
  80. randcnt == other.randcnt &&
  81. aa == other.aa &&
  82. bb == other.bb &&
  83. cc == other.cc &&
  84. randslr == other.randslr &&
  85. mm == other.mm &&
  86. rngblocks == other.rngblocks;
  87. }
  88. bool GenRngState::eq_nonarray_attributes(const RngState& other, const bool ignore_name) const {
  89. return
  90. randcnt == other.randcnt &&
  91. aa == other.aa &&
  92. bb == other.bb &&
  93. cc == other.cc &&
  94. true /*randslr*/ &&
  95. true /*mm*/ &&
  96. rngblocks == other.rngblocks;
  97. }
  98. std::string GenRngState::to_str(const bool all_details, const std::string ind) const {
  99. std::stringstream ss;
  100. ss << get_object_name() << ": " <<
  101. "randcnt=" << randcnt << ", " <<
  102. "aa=" << aa << ", " <<
  103. "bb=" << bb << ", " <<
  104. "cc=" << cc << ", " <<
  105. "randslr=" << vec_nonptr_to_str(randslr, all_details, ind + " ") << ", " <<
  106. "mm=" << vec_nonptr_to_str(mm, all_details, ind + " ") << ", " <<
  107. "rngblocks=" << rngblocks;
  108. return ss.str();
  109. }
  110. py::class_<RngState> define_pybinding_RngState(py::module& m) {
  111. return py::class_<RngState, std::shared_ptr<RngState>>(m, "RngState", "Internal checkpointing structure holding state of the random number generator.")
  112. .def(
  113. py::init<
  114. const uint64_t,
  115. const uint64_t,
  116. const uint64_t,
  117. const uint64_t,
  118. const std::vector<uint64_t>,
  119. const std::vector<uint64_t>,
  120. const uint64_t
  121. >(),
  122. py::arg("randcnt"),
  123. py::arg("aa"),
  124. py::arg("bb"),
  125. py::arg("cc"),
  126. py::arg("randslr"),
  127. py::arg("mm"),
  128. py::arg("rngblocks")
  129. )
  130. .def("check_semantics", &RngState::check_semantics)
  131. .def("__copy__", &RngState::copy_rng_state)
  132. .def("__deepcopy__", &RngState::deepcopy_rng_state, py::arg("memo"))
  133. .def("__str__", &RngState::to_str, py::arg("all_details") = false, py::arg("ind") = std::string(""))
  134. .def("__eq__", &RngState::__eq__, py::arg("other"))
  135. .def("dump", &RngState::dump)
  136. .def_property("randcnt", &RngState::get_randcnt, &RngState::set_randcnt)
  137. .def_property("aa", &RngState::get_aa, &RngState::set_aa)
  138. .def_property("bb", &RngState::get_bb, &RngState::set_bb)
  139. .def_property("cc", &RngState::get_cc, &RngState::set_cc)
  140. .def_property("randslr", &RngState::get_randslr, &RngState::set_randslr, py::return_value_policy::reference, "Must contain RNG_SIZE items.")
  141. .def_property("mm", &RngState::get_mm, &RngState::set_mm, py::return_value_policy::reference, "Must contain RNG_SIZE items.")
  142. .def_property("rngblocks", &RngState::get_rngblocks, &RngState::set_rngblocks, "Must contain RNG_SIZE items.")
  143. ;
  144. }
  145. std::string GenRngState::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  146. if (!export_even_if_already_exported() && ctx.already_exported(this)) {
  147. return ctx.get_exported_name(this);
  148. }
  149. std::string exported_name = "rng_state_" + std::to_string(ctx.postinc_counter("rng_state"));
  150. if (!export_even_if_already_exported()) {
  151. ctx.add_exported(this, exported_name);
  152. }
  153. bool str_export = export_as_string_without_newlines();
  154. std::string nl = "";
  155. std::string ind = " ";
  156. std::stringstream ss;
  157. if (!str_export) {
  158. nl = "\n";
  159. ind = " ";
  160. ss << exported_name << " = ";
  161. }
  162. ss << "m.RngState(" << nl;
  163. ss << ind << "randcnt = " << randcnt << "," << nl;
  164. ss << ind << "aa = " << aa << "," << nl;
  165. ss << ind << "bb = " << bb << "," << nl;
  166. ss << ind << "cc = " << cc << "," << nl;
  167. ss << ind << "randslr = " << export_vec_randslr(out, ctx, exported_name) << "," << nl;
  168. ss << ind << "mm = " << export_vec_mm(out, ctx, exported_name) << "," << nl;
  169. ss << ind << "rngblocks = " << rngblocks << "," << nl;
  170. ss << ")" << nl << nl;
  171. if (!str_export) {
  172. out << ss.str();
  173. return exported_name;
  174. }
  175. else {
  176. return ss.str();
  177. }
  178. }
  179. std::string GenRngState::export_vec_randslr(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  180. // does not print the array itself to 'out' and returns the whole list
  181. std::stringstream ss;
  182. ss << "[";
  183. for (size_t i = 0; i < randslr.size(); i++) {
  184. const auto& item = randslr[i];
  185. if (i == 0) {
  186. ss << " ";
  187. }
  188. else if (i % 16 == 0) {
  189. ss << "\n ";
  190. }
  191. ss << item << ", ";
  192. }
  193. ss << "]";
  194. return ss.str();
  195. }
  196. std::string GenRngState::export_vec_mm(std::ostream& out, PythonExportContext& ctx, const std::string& parent_name) {
  197. // does not print the array itself to 'out' and returns the whole list
  198. std::stringstream ss;
  199. ss << "[";
  200. for (size_t i = 0; i < mm.size(); i++) {
  201. const auto& item = mm[i];
  202. if (i == 0) {
  203. ss << " ";
  204. }
  205. else if (i % 16 == 0) {
  206. ss << "\n ";
  207. }
  208. ss << item << ", ";
  209. }
  210. ss << "]";
  211. return ss.str();
  212. }
  213. } // namespace API
  214. } // namespace MCell