run_utils.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "generated/gen_run_utils.h"
  12. #include "bng/filesystem_utils.h"
  13. #include "api/api_utils.h"
  14. #include "generated/gen_names.h"
  15. #include "bng/bng_defines.h"
  16. #include "src4/simulation_config.h"
  17. using namespace std;
  18. namespace MCell {
  19. namespace API {
  20. namespace run_utils {
  21. std::string get_last_checkpoint_dir(const int seed) {
  22. // only supported parameter is seed for now
  23. string chkpt_seed_dir =
  24. DEFAULT_CHECKPOINTS_DIR + BNG::PATH_SEPARATOR + get_seed_dir_name(seed);
  25. string max_it_dir_name = "";
  26. // do we have a checkpoints/seed directory?
  27. if (FSUtils::is_dir(chkpt_seed_dir)) {
  28. // find the highest iteration value
  29. int max = -1;
  30. std::vector<std::string> dirs;
  31. FSUtils::list_dir(chkpt_seed_dir, dirs);
  32. for (const string& dir_name: dirs) {
  33. // starts with "it_"?
  34. if (dir_name.find(DEFAULT_ITERATION_DIR_PREFIX) == 0) {
  35. string it_nr_str = dir_name.substr(
  36. DEFAULT_ITERATION_DIR_PREFIX.size(), dir_name.size() - DEFAULT_ITERATION_DIR_PREFIX.size());
  37. int it_nr;
  38. try {
  39. it_nr = stoi(it_nr_str);
  40. }
  41. catch (invalid_argument&) {
  42. continue;
  43. }
  44. if (it_nr > max) {
  45. // remember the directory with the highest iteration number so far
  46. max = it_nr;
  47. max_it_dir_name = dir_name;
  48. }
  49. }
  50. }
  51. }
  52. if (max_it_dir_name != "") {
  53. return chkpt_seed_dir + BNG::PATH_SEPARATOR + max_it_dir_name + BNG::PATH_SEPARATOR;
  54. }
  55. else {
  56. return "";
  57. }
  58. }
  59. std::vector<std::string> remove_cwd(const std::vector<std::string> paths) {
  60. std::vector<std::string> res;
  61. // skip everything that can be interpreted as current directory
  62. string cwd = FSUtils::get_current_dir();
  63. for (const string& p: paths) {
  64. if (p != "" && p != "." && p != cwd) {
  65. res.push_back(p);
  66. }
  67. }
  68. return res;
  69. }
  70. } // namespace run_utils
  71. } // namespace API
  72. } // namespace MCell