validate.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. """
  3. This is free and unencumbered software released into the public domain.
  4. Anyone is free to copy, modify, publish, use, compile, sell, or
  5. distribute this software, either in source code form or as a compiled
  6. binary, for any purpose, commercial or non-commercial, and by any
  7. means.
  8. In jurisdictions that recognize copyright laws, the author or authors
  9. of this software dedicate any and all copyright interest in the
  10. software to the public domain. We make this dedication for the benefit
  11. of the public at large and to the detriment of our heirs and
  12. successors. We intend this dedication to be an overt act of
  13. relinquishment in perpetuity of all present and future rights to this
  14. software under copyright law.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. For more information, please refer to [http://unlicense.org]
  23. """
  24. import sys
  25. import utils
  26. import random
  27. import multiprocessing
  28. from collections import Counter
  29. MAIN_MDL_FILE = 'Scene.main.mdl'
  30. # TODO: find automatically, allow to override
  31. MCELL_EXECUTABLE = '/home/ahusar/src4_display/mcell/build/release/mcell'
  32. g_extra_arg = ''
  33. def run_mcell(seed):
  34. cmd = [MCELL_EXECUTABLE, '-seed', str(seed), MAIN_MDL_FILE]
  35. if g_extra_arg:
  36. cmd.append(g_extra_arg)
  37. ec = utils.run(cmd, fout_name='mcell_' + str(seed) + '.out')
  38. if ec != 0:
  39. print("Fatal error, run with seed " + str(seed) + "failed.")
  40. sys.exit(1)
  41. def get_molecule_counts_from_ascii_file(filename):
  42. counts = {}
  43. with open(filename, 'r') as fin:
  44. for line in fin:
  45. # the first item is the ID
  46. id = line.split(' ')[0]
  47. if id in counts:
  48. counts[id] = counts[id] + 1
  49. else:
  50. counts[id] = 1
  51. return counts
  52. # single threaded execution for now
  53. def get_molecule_counts_for_multiple_runs(seeds):
  54. counts = {}
  55. current_run = 1
  56. #for s in seeds:
  57. # print("RUN " + str(current_run) + ", SEED " + str(s))
  58. # current_run += 1
  59. # run_mcell(s)
  60. # Set up the parallel task pool to use all available processors
  61. count = 12 # multiprocessing.cpu_count()
  62. pool = multiprocessing.Pool(processes=count)
  63. # Run the jobs
  64. pool.map(run_mcell, seeds)
  65. for s in seeds:
  66. # TODO: find the last .ascii file automatically
  67. curr_counts = get_molecule_counts_from_ascii_file('viz_data/seed_' + str(s).zfill(5) + '/Scene.ascii.1000.dat')
  68. # add values with common key
  69. counts = Counter(counts) + Counter(curr_counts)
  70. return counts
  71. def generate_seeds(count):
  72. res = []
  73. random.seed(a=200)
  74. for i in range(0, count):
  75. res.append(random.randint(1, 65535))
  76. return res
  77. # just one variant for now, but this could grow int oa more versatile tool
  78. def main():
  79. nr_runs = 1
  80. if len(sys.argv) >= 2:
  81. nr_runs = int(sys.argv[1])
  82. if len(sys.argv) >= 3:
  83. global g_extra_arg
  84. g_extra_arg = sys.argv[2]
  85. seeds = generate_seeds(nr_runs)
  86. res = get_molecule_counts_for_multiple_runs(seeds)
  87. print(str(res))
  88. if __name__ == "__main__":
  89. main()