From 9e2a8bcae08e0098863fee0eb54f6996116ca802 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Tue, 28 Jan 2020 17:10:55 +0000 Subject: [PATCH] added testing and the cleaning up of the custom library. this isnt done yet, and ive hit on some sissues --- .../utils/custom_logging_functions.py | 3 +- binarycpython/utils/grid.py | 76 ++++++++++++++----- binarycpython/utils/grid_options_defaults.py | 1 + examples/example_population.py | 9 ++- tests/population/grid_tests.py | 23 ++++++ 5 files changed, 91 insertions(+), 21 deletions(-) diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index fd6b77f45..a583292fd 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -81,6 +81,7 @@ def binary_c_log_code(code, verbose): #undef MAX #undef MIN #include \"binary_c.h\" +#include \"RLOF/RLOF_prototypes.h\" // add visibility __attribute__ ((visibility ("default"))) to it void binary_c_API_function custom_output_function(struct stardata_t * stardata); @@ -349,4 +350,4 @@ def create_and_load_logging_function(custom_logging_code, verbose): ) ) - return func_memaddr + return func_memaddr, library_name diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 88a5085db..92795655b 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -382,37 +382,36 @@ class Population(object): if self.grid_options["verbose"] > 0: print("Creating and loading custom logging functionality") - # - if self.grid_options["C_auto_logging"]: - # Generate real logging code - logging_line = autogen_C_logging_code( - self.grid_options["C_auto_logging"], - verbose=self.grid_options["verbose"], - ) - + if self.grid_options["C_logging_code"]: # Generate entire shared lib code around logging lines custom_logging_code = binary_c_log_code( - logging_line, verbose=self.grid_options["verbose"] + self.grid_options["C_logging_code"], + verbose=self.grid_options["verbose"], ) # Load memory adress self.grid_options[ "custom_logging_func_memaddr" - ] = create_and_load_logging_function( + ], self.grid_options["custom_logging_shared_library_file"] = create_and_load_logging_function( custom_logging_code, verbose=self.grid_options["verbose"] ) - # - if self.grid_options["C_logging_code"]: + + elif self.grid_options["C_auto_logging"]: + # Generate real logging code + logging_line = autogen_C_logging_code( + self.grid_options["C_auto_logging"], + verbose=self.grid_options["verbose"], + ) + # Generate entire shared lib code around logging lines custom_logging_code = binary_c_log_code( - self.grid_options["C_logging_code"], - verbose=self.grid_options["verbose"], + logging_line, verbose=self.grid_options["verbose"] ) # Load memory adress self.grid_options[ "custom_logging_func_memaddr" - ] = create_and_load_logging_function( + ], self.grid_options["custom_logging_shared_library_file"] = create_and_load_logging_function( custom_logging_code, verbose=self.grid_options["verbose"] ) @@ -420,7 +419,7 @@ class Population(object): # Evolution functions ################################################### - def evolve_single(self, parse_function=None): + def evolve_single(self, parse_function=None, clean_up_custom_logging_files=True): """ Function to run a single system @@ -441,13 +440,56 @@ class Population(object): ) # Todo: change this to run_binary again but then build in checks in binary # 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 + if clean_up_custom_logging_files: + self.clean_up_custom_logging(evol_type='single') + # Parse if parse_function: parse_function(self, out) - else: return out + def remove_file(self, file, verbose): + """ + Function to remove files but with verbosity + """ + + if os.path.exists(file): + try: + if verbose > 0: print("Removed {}".format()) + os.remove(file) + except: + if verbose > 0: print("Error while deleting file {}".format(file)) + raise FileNotFoundError + + def clean_up_custom_logging(self, evol_type): + """ + Function to clean up the custom logging. + Has two types: + 'single': + - removes the compiled shared library (which name is stored in grid_options['custom_logging_shared_library_file']) + - TODO: unloads/frees the memory allocated to that shared library (which is stored in grid_options['custom_logging_func_memaddr']) + - sets both to None + 'multiple': + - TODO: make this and design this + """ + + if evol_type=='single': + if self.grid_options['verbose'] > 0: print("Cleaning up the custom logging stuff. type: single") + # TODO: Unset custom logging code + + # TODO: Unset function memory adress + print(self.grid_options["custom_logging_func_memaddr"]) + + # remove shared library files + if self.grid_options["custom_logging_shared_library_file"]: + self.remove_file(self.grid_options["custom_logging_shared_library_file"], self.grid_options['verbose']) + + if evol_type=='population': + if self.grid_options['verbose'] > 0: print("Cleaning up the custom logging stuffs. type: population") + # TODO: make sure that these also work. not fully sure if necessary tho. whether its a single file, or a dict of files/memaddresses + def evolve_population(self, parse_function, custom_arg_file=None): """ The function that will evolve the population. This function contains many steps diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 51ec55aee..11253d260 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -13,6 +13,7 @@ grid_options_defaults_dict = { "C_auto_logging": None, # Should contain a dictionary where the kes are they headers and the values are lists of parameters that should be logged. This will get parsed by autogen_C_logging_code in custom_loggion_functions.py "C_logging_code": None, # Should contain a string which holds the logging code. "custom_logging_func_memaddr": -1, # Contains the custom_logging functions memory address + "custom_logging_shared_library_file": None, # Store pre-loading: "store_memaddr": -1, # Contains the store object memory adress, useful for preloading. defaults to -1 and isnt used if thats the default then. # Log args: logging of arguments diff --git a/examples/example_population.py b/examples/example_population.py index 766e3ace4..e466c4550 100644 --- a/examples/example_population.py +++ b/examples/example_population.py @@ -5,7 +5,8 @@ import os # import sys from binarycpython.utils.grid import Population -from binarycpython.utils.functions import get_help_all, get_help +from binarycpython.utils.functions import get_help_all, get_help, create_hdf5 + ######################################################### @@ -49,7 +50,9 @@ example_pop.set( example_pop.set( C_logging_code='Printf("MY_STELLAR_DATA time=%g mass=%g radius=%g\\n", stardata->model.time, stardata->star[0].mass, stardata->star[0].radius);' ) -example_pop.set_custom_logging() + +# TODO: show when reading from file +example_pop.set_custom_logging() # TODO: remove this one # Adding grid variables: example_pop.add_grid_variable( @@ -87,4 +90,4 @@ example_pop.export_all_info() # Wrapping up the results to an hdf5 file can be done by using the create_hdf5(<directory containing data and settings>) # This function takes the settings file (ending in _settings.json) and the data files (ending in .dat) from the data_dir # and packing them into an hdf5 file, which is then written into the same data_dir directory -create_hdf5(test_pop.custom_options["data_dir"]) +create_hdf5(example_pop.custom_options["data_dir"]) \ No newline at end of file diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py index 2d14757c5..9ff0f4759 100644 --- a/tests/population/grid_tests.py +++ b/tests/population/grid_tests.py @@ -204,6 +204,29 @@ test_pop.set( # return val # test_pop.set(extra_prob_function=ding) + + + +### Cleaning up custom logging code + +test_pop.set(C_logging_code='Printf("MY_STELLAR_DATA time=%g mass=%g radius=%g\\n", stardata->model.time, stardata->star[0].mass, stardata->star[0].radius);') +print(test_pop.evolve_single()) +quit() + + + + + + + + + + + + + + + ### Grid generating testing test_pop.add_grid_variable( name="lnm1", -- GitLab