From f72b40e522015a07b645f715b312c5a4ff360fee Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Sun, 15 Mar 2020 20:51:46 +0000
Subject: [PATCH] changed the examples to work again, modified the run_system
 python function to handle stuff correctly, in a cleaner fashion

---
 binarycpython/utils/grid.py               | 28 ++++---
 binarycpython/utils/run_system_wrapper.py | 97 ++++++++---------------
 examples/examples.py                      | 22 ++---
 3 files changed, 60 insertions(+), 87 deletions(-)

diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py
index 6501ca2cd..9006ebf5b 100644
--- a/binarycpython/utils/grid.py
+++ b/binarycpython/utils/grid.py
@@ -514,10 +514,10 @@ class Population(object):
         """
 
         out = binary_c_python_api.run_system(
-            arg_string=binary_cmdline_string,
-            self.grid_options["custom_logging_func_memaddr"],
-            self.grid_options["store_memaddr"],
-
+            argstring=binary_cmdline_string,
+            custom_logging_func_memaddr=self.grid_options["custom_logging_func_memaddr"],
+            store_memaddr=self.grid_options["store_memaddr"],
+            population=1,
         )
         if self.grid_options["parse_function"]:
             self.grid_options["parse_function"](self, out)
@@ -555,10 +555,11 @@ class Population(object):
         print("Running {}".format(argline))
         # Run system
         out = binary_c_python_api.run_system(
-            argline,
-            self.grid_options["custom_logging_func_memaddr"],
-            self.grid_options["store_memaddr"],
-        )  # Todo: change this to run_binary again but then build in checks in binary
+            argstring=argline,
+            custom_logging_func_memaddr=self.grid_options["custom_logging_func_memaddr"],
+            store_memaddr=self.grid_options["store_memaddr"],
+            population=0,
+        ) 
 
         # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries.
         # TODO: make a switch to turn this off
@@ -606,10 +607,11 @@ class Population(object):
             full_system_dict.update(system)
 
             binary_cmdline_string = self.return_argline(full_system_dict)
-            out = binary_c_python_api.run_population(
-                binary_cmdline_string,
-                self.grid_options["custom_logging_func_memaddr"],
-                self.grid_options["store_memaddr"],
+            out = binary_c_python_api.run_system(
+                argstring=binary_cmdline_string,
+                custom_logging_func_memaddr=self.grid_options["custom_logging_func_memaddr"],
+                store_memaddr=self.grid_options["store_memaddr"],
+                population=1,
             )
             self.print_info(
                 i + 1, self.grid_options["total_starcount"], full_system_dict
@@ -675,7 +677,7 @@ class Population(object):
             max_evolution_time,
         )
 
-        output = binary_c_python_api.run_binary(argstring)
+        output = binary_c_python_api.run_system(argstring)
 
         print("\n\nBinary_c output:")
         print(output)
diff --git a/binarycpython/utils/run_system_wrapper.py b/binarycpython/utils/run_system_wrapper.py
index 5de0de40c..eb33767a7 100644
--- a/binarycpython/utils/run_system_wrapper.py
+++ b/binarycpython/utils/run_system_wrapper.py
@@ -1,3 +1,15 @@
+import binary_c_python_api
+
+from binarycpython.utils.functions import (
+        get_defaults,
+        create_arg_string,
+        get_arg_keys,
+    )
+
+from binarycpython.utils.custom_logging_functions import (
+    create_and_load_logging_function
+)
+
 def run_system(**kwargs):
     """
     Wrapper to run a system with settings 
@@ -9,79 +21,34 @@ def run_system(**kwargs):
 
     """
 
-    # Load default args
-    args = get_defaults()
-    if "custom_logging_code" in kwargs:
-        # Use kwarg value to override defaults and add new args
-        for key in kwargs.keys():
-            if not key == "custom_logging_code":
-                args[key] = kwargs[key]
-
-        # Generate library and get memaddr
-        func_memaddr = create_and_load_logging_function(kwargs["custom_logging_code"])
-
-        # Construct arguments string and final execution string
-        arg_string = create_arg_string(args)
-        arg_string = "binary_c {}".format(arg_string)
-
-        # Run it and get output
-        output = binary_c_python_api.run_binary_custom_logging(arg_string, func_memaddr)
-        return output
-
-    elif "log_filename" in kwargs:
-        # Use kwarg value to override defaults and add new args
-        for key in kwargs.keys():
-            args[key] = kwargs[key]
-
-        # Construct arguments string and final execution string
-        arg_string = create_arg_string(args)
-        arg_string = "binary_c {}".format(arg_string)
-
-        # Run it and get output
-        output = binary_c_python_api.run_binary_with_logfile(arg_string)
-        return output
+    # Load available arg keywords
+    available_binary_c_arg_keywords = get_arg_keys()
 
-    else:  # run the plain basic type
+    # Set default values
+    func_memaddr = -1
+    write_logfile = 0
 
-        # Use kwarg value to override defaults and add new args
-        for key in kwargs.keys():
-            args[key] = kwargs[key]
+    # Create dict to pass as argstring
+    binary_c_args = {}
 
-        # Construct arguments string and final execution string
-        arg_string = create_arg_string(args)
-        arg_string = "binary_c {}".format(arg_string)
-
-        # Run it and get output
-        output = binary_c_python_api.run_binary(arg_string)
-
-        return output
-
-
-def run_system_with_log(**kwargs):
-    """
-    Wrapper to run a system with settings AND logs the files to a designated place defined by the log_filename parameter.
-    """
+    # Check which binary_c arguments have been passed and put them into a dict
+    for key in kwargs.keys():
+        if key in available_binary_c_arg_keywords:
+            binary_c_args[key] = kwargs[key]
 
-    # Load default args
-    args = get_defaults()
-    # args = {}
+    # Check if custom logging is required
+    if "custom_logging_code" in kwargs:
+        func_memaddr, shared_lib_filename = create_and_load_logging_function(kwargs["custom_logging_code"])
 
-    # For example
-    # physics_args['M_1'] = 20
-    # physics_args['separation'] = 0 # 0 = ignored, use period
-    # physics_args['orbital_period'] = 100000000000 # To make it single
+    # Check if writing logfile is required:
+    if "log_filename" in kwargs:
+        write_logfile = 1
 
-    # Use kwarg value to override defaults and add new args
-    for key in kwargs.keys():
-        args[key] = kwargs[key]
 
     # Construct arguments string and final execution string
-    arg_string = create_arg_string(args)
+    arg_string = create_arg_string(binary_c_args)
     arg_string = "binary_c {}".format(arg_string)
 
-    # print(arg_string)
-
-    # Run it and get output
-    output = binary_c_python_api.run_binary_with_logfile(arg_string)
+    output = binary_c_python_api.run_system(arg_string, custom_logging_func_memaddr=func_memaddr, write_logfile=write_logfile)
 
-    return output
+    return output
\ No newline at end of file
diff --git a/examples/examples.py b/examples/examples.py
index ffe5a6f45..f11bee8ef 100644
--- a/examples/examples.py
+++ b/examples/examples.py
@@ -4,11 +4,13 @@ import sys
 
 import binary_c_python_api
 
-from binarycpython.utils.functions import run_system, parse_output
+from binarycpython.utils.functions import parse_output
 from binarycpython.utils.custom_logging_functions import (
     autogen_C_logging_code,
     binary_c_log_code,
+    create_and_load_logging_function,
 )
+from binarycpython.utils.run_system_wrapper import run_system
 
 """
 Very basic scripts to run a binary system and print the output.
@@ -19,7 +21,7 @@ Use these as inspiration/base.
 
 def run_example_binary():
     """
-    Function to run a binary system. Very basic approach which directly adresses the run_binary(..) python-c wrapper function. 
+    Function to run a binary system. Very basic approach which directly adresses the run_system(..) python-c wrapper function. 
     """
 
     m1 = 15.0  # Msun
@@ -42,13 +44,12 @@ def run_example_binary():
         metallicity=metallicity,
         max_evolution_time=max_evolution_time,
     )
-    output = binary_c_python_api.run_binary(argstring)
+    output = binary_c_python_api.run_system(argstring)
     print(output)
 
 
 run_example_binary()
 
-
 def run_example_binary_with_run_system():
     """
     This function serves as an example on the function run_system and parse_output. 
@@ -86,6 +87,7 @@ def run_example_binary_with_run_system():
 
     # Cast the data into a dataframe.
     # This example automatically catches the column names because the binary_c output line is constructed as 'example_header_1 time=<number>..'
+    print(result_example_header_1)
     df = pd.DataFrame.from_dict(result_example_header_1, dtype=np.float64)
     print(df)
 
@@ -101,7 +103,7 @@ def run_example_binary_with_run_system():
     # Some routine to plot.
 
 
-run_example_binary_with_run_system()
+# run_example_binary_with_run_system()
 
 
 def run_example_custom_logging_autogenerated():
@@ -121,7 +123,7 @@ def run_example_custom_logging_autogenerated():
     custom_logging_code = binary_c_log_code(logging_line)
 
     # Generate library and get memaddr
-    func_memaddr = create_and_load_logging_function(custom_logging_code)
+    func_memaddr, shared_lib_filename = create_and_load_logging_function(custom_logging_code)
 
     #
     m1 = 15.0  # Msun
@@ -140,11 +142,11 @@ def run_example_custom_logging_autogenerated():
         metallicity,
         max_evolution_time,
     )
-    output = binary_c_python_api.run_binary_custom_logging(argstring, func_memaddr)
+    output = binary_c_python_api.run_system(argstring, custom_logging_func_memaddr=func_memaddr)
     print(output)
 
 
-run_example_custom_logging_autogenerated()
+# run_example_custom_logging_autogenerated()
 
 
 def run_example_binary_with_custom_logging():
@@ -178,6 +180,8 @@ def run_example_binary_with_custom_logging():
         custom_logging_code=custom_logging_code,
     )
 
+    print(output)
+
     # Catch results that start with a given header. (Mind that binary_c has to be configured to print them if your not using a custom logging function)
     # DOESNT WORK YET if you have the line autogenerated.
     result_example_header = parse_output(output, "MY_STELLAR_DATA")
@@ -189,7 +193,7 @@ def run_example_binary_with_custom_logging():
     print(df)
 
 
-run_example_binary_with_custom_logging()
+# run_example_binary_with_custom_logging()
 
 
 def run_example_binary_with_writing_logfile():
-- 
GitLab