species.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2020 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 "api/complex.h"
  12. #include "api/species.h"
  13. #include "api/elementary_molecule.h"
  14. #include "api/elementary_molecule_type.h"
  15. #include "api/python_export_utils.h"
  16. using namespace std;
  17. namespace MCell {
  18. namespace API {
  19. std::string Species::to_str(const bool all_details, const std::string ind) const {
  20. if (!all_details) {
  21. return ind + to_bngl_str();
  22. }
  23. else {
  24. return GenComplex::to_str(true, ind);
  25. }
  26. }
  27. // TODO: how to make this consistent with API definition?
  28. bool Species::__eq__(const Species& other) const {
  29. return
  30. name == other.name &&
  31. diffusion_constant_2d == other.diffusion_constant_2d &&
  32. diffusion_constant_3d == other.diffusion_constant_3d &&
  33. custom_time_step == other.custom_time_step &&
  34. custom_space_step == other.custom_space_step &&
  35. target_only == other.target_only &&
  36. orientation == other.orientation &&
  37. compartment_name == other.compartment_name &&
  38. Complex::__eq__(other) // make canonical comparison
  39. ;
  40. }
  41. // species name is sufficient
  42. std::string Species::export_to_python(std::ostream& out, PythonExportContext& ctx) {
  43. if (ctx.already_exported(this)) {
  44. return ctx.get_exported_name(this);
  45. }
  46. if (is_species_superclass()) {
  47. if (name == BNG::ALL_MOLECULES) {
  48. return S(MDOT) + NAME_CV_AllMolecules;
  49. }
  50. else if (name == BNG::ALL_VOLUME_MOLECULES) {
  51. return S(MDOT) + NAME_CV_AllVolumeMolecules;
  52. }
  53. else if (name == BNG::ALL_SURFACE_MOLECULES) {
  54. return S(MDOT) + NAME_CV_AllSurfaceMolecules;
  55. }
  56. else {
  57. assert(false);
  58. }
  59. }
  60. std::string exported_name = "species_";
  61. if (name.size() > MAX_SPECIES_NAME_LENGTH) {
  62. exported_name += to_string(ctx.postinc_counter("species"));
  63. }
  64. else {
  65. exported_name += fix_id(name);
  66. }
  67. ctx.add_exported(this, exported_name);
  68. std::stringstream ss;
  69. ss << exported_name << " = m.Species(\n";
  70. assert(is_set(name));
  71. ss << " name = " << "'" << name << "'" << "\n";
  72. ss << ")\n\n";
  73. out << ss.str();
  74. return exported_name;
  75. }
  76. } // namespace API
  77. } // namespace MCell