species_cleanup_event.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <iostream>
  12. #include <fstream>
  13. #include <sstream>
  14. #include "species_cleanup_event.h"
  15. #include "world.h"
  16. using namespace std;
  17. namespace MCell {
  18. void SpeciesCleanupEvent::dump(const string ind) const {
  19. cout << ind << "Species cleanup event:\n";
  20. string ind2 = ind + " ";
  21. BaseEvent::dump(ind2);
  22. }
  23. void SpeciesCleanupEvent::remove_unused_reactant_classes() {
  24. // get a set of used reactant classes
  25. uint_set<BNG::reactant_class_id_t> used_reactant_classes;
  26. for (BNG::Species* sp: world->get_all_species().get_species_vector()) {
  27. release_assert(sp != nullptr);
  28. if (sp->has_valid_reactant_class_id()) {
  29. used_reactant_classes.insert(sp->get_reactant_class_id());
  30. }
  31. }
  32. // go through all reactant classes and select those that are not used
  33. uint_set<BNG::reactant_class_id_t> unused_reactant_classes;
  34. for (const BNG::ReactantClass* rc: world->get_all_rxns().get_reactant_classes()) {
  35. if (rc != nullptr && used_reactant_classes.count(rc->id) == 0) {
  36. unused_reactant_classes.insert(rc->id);
  37. }
  38. }
  39. // remove the unused classes
  40. for (BNG::reactant_class_id_t id: unused_reactant_classes) {
  41. // from rxn container including reacting classes
  42. world->get_all_rxns().remove_reactant_class(id);
  43. // and from partition's reactants map
  44. world->get_partition(PARTITION_ID_INITIAL).remove_reactant_class_usage(id);
  45. }
  46. }
  47. void SpeciesCleanupEvent::step() {
  48. // remove all rxn classes and all caches
  49. world->get_all_rxns().reset_caches();
  50. for (BNG::Species* sp: world->get_all_species().get_species_vector()) {
  51. release_assert(sp != nullptr);
  52. // num_instantiations tells us that there are no molecules of these species
  53. // is_removable - species were created on the fly
  54. if (sp->get_num_instantiations() == 0 && sp->is_removable()) {
  55. // tell partitions that this species is not known anymore
  56. if (sp->is_vol()) {
  57. for (Partition& p: world->get_partitions()) {
  58. p.remove_from_known_vol_species(sp->id);
  59. }
  60. }
  61. if (world->config.rxn_and_species_report) {
  62. stringstream ss;
  63. ss << sp->id << ": " << sp->to_str() << " - removed\n";
  64. BNG::append_to_report(world->config.get_species_report_file_name(), ss.str());
  65. }
  66. // delete this species
  67. world->get_all_species().remove(sp->id);
  68. // and also from caches used by RxnRules
  69. world->get_all_rxns().remove_species_id_references(sp->id);
  70. }
  71. }
  72. // cleanup the species array, we must remove nullptrs from the species array
  73. world->get_all_species().defragment();
  74. // also remove unused reactant classes
  75. remove_unused_reactant_classes();
  76. }
  77. } /* namespace MCell */