diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index 239cf1a0583f88293aee48d11b1b8edc67bad734..fd6b77f4521bcc804f9e37de18084063c3bf7867 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -10,7 +10,8 @@ import uuid def autogen_C_logging_code(logging_dict, verbose=0): """ - Function that autogenerates PRINTF statements for binaryc. intput is a dictionary where the key is the header of that logging line and items which are lists of parameters\ + Function that autogenerates PRINTF statements for binaryc. + Input is a dictionary where the key is the header of that logging line and items which are lists of parameters that will be put in that logging line Example:: @@ -227,7 +228,7 @@ def return_compilation_dict(verbose=0): if verbose > 0: print( - "With options:\n\tcc = {cc}\n\tccflags = {ccflags}\n\tld = {ld}\n\tlibs = {libs}\n\tinc = {inc}\n\n".format( + "Got options to compile:\n\tcc = {cc}\n\tccflags = {ccflags}\n\tld = {ld}\n\tlibs = {libs}\n\tinc = {inc}\n\n".format( cc=cc, ccflags=ccflags, ld=ld, libs=libs, inc=inc ) ) @@ -269,11 +270,11 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=0): # Execute compilation and create the library if verbose > 0: - BINARY_C_DIR = os.getenv("BINARY_C") - BINARY_C_SRC_DIR = os.path.join(BINARY_C_DIR, "src") + # BINARY_C_DIR = os.getenv("BINARY_C") + # BINARY_C_SRC_DIR = os.path.join(BINARY_C_DIR, "src") print( - "Building shared library for custom logging with (binary_c.h) at {} on {}\n".format( - BINARY_C_SRC_DIR, socket.gethostname() + "Building shared library for custom logging with (binary_c.h) on {}\n".format( + socket.gethostname() ) ) print( diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 5c3de15b3e21527204579d9865160eaf31663039..90d5edda61b7f6eba19c3dc7633f784c3fe4baae 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -248,7 +248,7 @@ class Population(object): # Load it into the grid_options self.grid_options["grid_variables"][grid_variable["name"]] = grid_variable if self.grid_options["verbose"] > 0: - print("Added grid variable: {}".format(json.dumps(grid_variable))) + print("Added grid variable: {}".format(json.dumps(grid_variable, indent=4))) ################################################### # Return functions @@ -379,7 +379,9 @@ class Population(object): """ # C_logging_code gets priority of C_autogen_code + 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( @@ -448,12 +450,15 @@ class Population(object): def evolve_population(self, parse_function, custom_arg_file=None): """ The function that will evolve the population. This function contains many steps + + TODO: fix the verbosity in this function when this function is finished """ ### Custom logging code: self.set_custom_logging() ### Load store + if self.grid_options['verbose'] > 0: print('loading binary_c store information') self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") # Execute. @@ -629,6 +634,12 @@ class Population(object): ################################################### def test_evolve_single(self): + """ + Function to test the evolution of a system. Calls the api binding directly. + """ + + if self.grid_options['verbose'] > 0: print('running a single system as a test') + m1 = 15.0 # Msun m2 = 14.0 # Msun separation = 0 # 0 = ignored, use period @@ -669,6 +680,8 @@ class Population(object): # TODO: add centering center left right for the spacing """ + if self.grid_options['verbose'] > 0: print("Generating grid code") + # Some local values code_string = "" depth = 0 @@ -937,13 +950,19 @@ class Population(object): # Stop of code generation. Here the code is saved and written # Save the gridcode to the grid_options + if self.grid_options['verbose'] > 0: print("Saving grid code to grid_options") + self.grid_options["code_string"] = code_string # Write to file + gridcode_filename = os.path.join( self.grid_options["tmp_dir"], "example_grid.py" ) self.grid_options["gridcode_filename"] = gridcode_filename + + if self.grid_options['verbose'] > 0: print("Writing grid code to {}".format(gridcode_filename)) + with open(gridcode_filename, "w") as f: f.write(code_string) @@ -955,6 +974,8 @@ class Population(object): # Code to load the import importlib.util + if self.grid_options['verbose'] > 0: print("Loading grid code function from {}".format(self.grid_options['gridcode_filename'])) + spec = importlib.util.spec_from_file_location( "binary_c_python_grid", os.path.join(self.grid_options["tmp_dir"], "example_grid.py"), @@ -963,6 +984,10 @@ class Population(object): spec.loader.exec_module(grid_file) generator = grid_file.grid_code(self) + if self.grid_options['verbose'] > 0: print("Grid code loaded") + + + print(next(generator)) print(next(generator)) print(next(generator)) diff --git a/examples/example_population.py b/examples/example_population.py index 955e1e04fef0490e8f63b4f13f46e871a8287d33..ef57243f6f1488d911061455fb45a008b0b35c05 100644 --- a/examples/example_population.py +++ b/examples/example_population.py @@ -17,7 +17,7 @@ from binarycpython.utils.functions import get_help_all, get_help example_pop = Population() # If you want verbosity, set this before other things -example_pop.set(verbose=0) +example_pop.set(verbose=1) # Setting values can be done via .set(<parameter_name>=<value>) # Values that are known to be binary_c_parameters are loaded into bse_options. @@ -49,9 +49,22 @@ 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() +# Adding grid variables: +example_pop.add_grid_variable( + name="lnm1", + longname="log primary mass", + valuerange=[10, 20], + resolution="10", + spacingfunc="np.linspace(0.213, 10.2, 10)", + precode="M_1=math.exp(lnm1)", + probdist="flat(M_1)", + # probdist='self.custom_options["extra_prob_function"](M_1)', + dphasevol="", + parameter_name="M_1", + condition="", +) # Exporting of all the settings can be done with .export_all_info() # on default it exports everything, but can be supressed by turning it off: @@ -61,3 +74,18 @@ example_pop.set_custom_logging() # include_binary_c_help_all (all the help information for all the binary_c parameters), turn off with include_binary_c_help_all=Fase # On default it will write this to the custom_options['data_dir'], but that can be overriden by setting use_datadir=False and providing an outfile=<> example_pop.export_all_info() + +# Creating a parsing function +# TODO: add example of setting up a parsing function + +# Executing a single system +# TODO: add example of running a single system + +# Executing a population +# TODO: add example of running a population + +# 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']) +