From 4b03a95f7625116d8d805275ad66cdd644c5ec92 Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Sun, 9 May 2021 11:32:50 +0100
Subject: [PATCH] Small changes to grid

---
 binarycpython/utils/grid.py                  | 24 ++++++++++++++++----
 binarycpython/utils/grid_options_defaults.py |  4 ++++
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 77fbb94c5..93ceafb7a 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -724,6 +724,8 @@ class Population:
             "total_count": self.grid_options["_count"],
             "start_timestamp": self.grid_options["_start_time_evolution"],
             "end_timestamp": self.grid_options["_end_time_evolution"],
+            "total_mass_run": self.grid_options["_total_mass_run"],
+            "total_probability_weighted_mass_run": self.grid_options["_total_probability_weighted_mass_run"],
         }
 
         ##
@@ -894,6 +896,8 @@ class Population:
         self.grid_options["_errors_found"] = combined_output_dict["_errors_found"]
         self.grid_options["_probtot"] = combined_output_dict["_probtot"]
         self.grid_options["_count"] = combined_output_dict["_count"]
+        self.grid_options["_total_mass_run"] = combined_output_dict["_total_mass_run"]
+        self.grid_options["_total_probability_weighted_mass_run"] = combined_output_dict["_total_probability_weighted_mass_run"]
 
     def _evolve_system_mp(self, full_system_dict):
         """
@@ -999,6 +1003,9 @@ class Population:
 
         total_time_calling_binary_c = 0
 
+        total_mass_run = 0
+        total_probability_weighted_mass_run = 0
+
         # Go over the generator
         while running:
             # round_number_mod = (localcounter+1)%self.grid_options["amt_cores"]
@@ -1007,7 +1014,7 @@ class Population:
                 # Get the system
                 system = next(generator)
 
-                # Check if the ID is the correct one for this process
+                # Check if the ID is the correct one for this process. This is the method we use to split this calculation over many cores and or machines
                 if (localcounter + (ID + round_number_mod)) % self.grid_options[
                     "amt_cores"
                 ] == 0:
@@ -1040,12 +1047,9 @@ class Population:
                         binary_cmdline_string = self._return_argline(full_system_dict)
                         f.write(binary_cmdline_string)
 
-                    start_runtime_binary_c = time.time()
-
                     # In the first system, explicitly check all the keys that are passed to see if
                     # they match the keys known to binary_c.
                     # Won't do that every system cause that is a bit of a waste of computing time.
-
                     if localcounter == 0:
                         for key in full_system_dict.keys():
                             if not key in self.available_keys:
@@ -1065,7 +1069,8 @@ class Population:
                                         key
                                     )
                                     raise ValueError(msg)
-
+                    
+                    start_runtime_binary_c = time.time()
                     # TODO: build flag to actually evolve the system
                     # Evolve the system
                     self._evolve_system_mp(full_system_dict)
@@ -1099,6 +1104,11 @@ class Population:
                     probability_of_systems_run += full_system_dict["probability"]
                     number_of_systems_run += 1
 
+                    # Tally up some numbers
+                    total_mass_system = full_system_dict.get("M_1", 0) + full_system_dict.get("M_1", 0) + full_system_dict.get("M_1", 0) + full_system_dict.get("M_1", 0)
+                    total_mass_run += total_mass_system
+                    total_probability_weighted_mass_run += total_mass_system * full_system_dict["probability"]
+
             except StopIteration:
                 running = False
 
@@ -1183,6 +1193,8 @@ class Population:
             "_errors_found": self.grid_options["_errors_found"],
             "_probtot": probability_of_systems_run,
             "_count": number_of_systems_run,
+            "_total_mass_run": total_mass_run,
+            "_total_probability_weighted_mass_run": total_probability_weighted_mass_run,
         }
 
         end_process_time = datetime.datetime.now()
@@ -1412,6 +1424,8 @@ class Population:
         self.grid_options["_errors_found"] = False
         self.grid_options["_errors_exceeded"] = False
         self.grid_options["_failed_systems_error_codes"] = []
+        self.grid_options["_total_mass_run"] = 0
+        self.grid_options["_total_probability_weighted_mass_run"] = 0
 
         # Remove files
         # TODO: remove files
diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py
index dd0b43464..45c4e017a 100644
--- a/binarycpython/utils/grid_options_defaults.py
+++ b/binarycpython/utils/grid_options_defaults.py
@@ -97,6 +97,8 @@ grid_options_defaults_dict = {
     "failed_systems_threshold": 20,  # Maximum failed systems per process allowed to fail before the process stops logging the failing systems.
     "_failed_systems_error_codes": [],  # List to store the unique error codes
     "_population_id": 0,  # Random id of this grid/population run, Unique code for the population. Should be set only once by the controller process.
+    "_total_mass_run": 0, # To count the total mass that thread/process has ran
+    "_total_probability_weighted_mass_run": 0, # To count the total mass * probability for each system that thread/process has ran 
     "modulo": 1,  # run modulo n of the grid. #TODO: fix this
     ## Grid type evolution
     "_grid_variables": {},  # grid variables
@@ -465,6 +467,8 @@ grid_options_descriptions = {
     "evolution_type": "Variable containing the type of evolution used of the grid. Multiprocessing or linear processing",
     "combine_ensemble_with_thread_joining": "BOolean flag on whether to combine everything and return it to the user or if false: write it to data_dir/ensemble_output_{popuation_id}_{thread_id}.json",
     "log_runtime_systems": "Whether to log the runtime of the systems . Each systems run by the thread is logged to a file and is stored in the tmp_dir. (1 file per thread). Don't use this if you are planning to run alot of systems. This is mostly for debugging and finding systems that take long to run. Integer, default = 0. if value is 1 then the systems are logged",
+    "_total_mass_run": "To count the total mass that thread/process has ran",
+    "_total_probability_weighted_mass_run": "To count the total mass * probability for each system that thread/process has ran",
 }
 
 ###
-- 
GitLab