count.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/count.h"
  12. #include "mol_or_rxn_count_event.h"
  13. namespace MCell {
  14. namespace API {
  15. void Count::postprocess_in_ctor() {
  16. set_all_custom_attributes_to_default();
  17. // determine count type
  18. set_automatically_output_format_if_needed();
  19. }
  20. void Count::check_semantics() const {
  21. GenCount::check_semantics(); // calls also CountTerm::check_semantics
  22. if (!is_set(expression)) {
  23. throw ValueError(S("Attribute ") + NAME_EXPRESSION + " must be set.");
  24. }
  25. expression->check_that_species_or_reaction_rule_is_set();
  26. if (every_n_timesteps < 0) {
  27. throw ValueError(
  28. S("The value of ") + NAME_EVERY_N_TIMESTEPS + " must be higher or equal to 0.");
  29. }
  30. if (output_format == CountOutputFormat::UNSET) {
  31. throw ValueError(S("Attribute ") + NAME_OUTPUT_FORMAT + " must be set.");
  32. }
  33. }
  34. void Count::set_automatically_output_format_if_needed() {
  35. if (output_format != CountOutputFormat::AUTOMATIC_FROM_EXTENSION) {
  36. // specific value set
  37. return;
  38. }
  39. const std::string gdat = ".gdat";
  40. size_t gdat_sz = gdat.size();
  41. const std::string dat = ".dat";
  42. size_t dat_sz = dat.size();
  43. size_t sz = file_name.size();
  44. if (!is_set(file_name) || (sz > dat.size() && file_name.substr(sz - dat_sz) == dat)) {
  45. output_format = CountOutputFormat::DAT;
  46. }
  47. else if (sz > gdat.size() && file_name.substr(sz - gdat_sz) == gdat) {
  48. output_format = CountOutputFormat::GDAT;
  49. }
  50. else {
  51. throw ValueError(S("Cannot automatically determine ") + NAME_OUTPUT_FORMAT + ", " + NAME_FILE_NAME +
  52. " must have either .dat or .gdat extension for automatic detection.");
  53. }
  54. }
  55. double Count::get_current_value() {
  56. if (count_event == nullptr) {
  57. throw RuntimeError(S(NAME_CLASS_COUNT) + " with name " + name + " was not initialized.");
  58. }
  59. return count_event->get_single_count_value();
  60. }
  61. } // namespace API
  62. } // namespace MCell