diff --git a/binarycpython/utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py index a9c0ffff6293eaeaa69d34195a40a1256e422f1f..239cf1a0583f88293aee48d11b1b8edc67bad734 100644 --- a/binarycpython/utils/custom_logging_functions.py +++ b/binarycpython/utils/custom_logging_functions.py @@ -8,7 +8,7 @@ import random import uuid -def autogen_C_logging_code(logging_dict): +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\ that will be put in that logging line @@ -33,6 +33,12 @@ def autogen_C_logging_code(logging_dict): code = "" # Loop over dict keys for key in logging_dict: + if verbose > 0: + print( + "Generating Print statement for custom logging code with {} as a header".format( + key + ) + ) logging_dict_entry = logging_dict[key] # Check if item is of correct type: @@ -54,16 +60,20 @@ def autogen_C_logging_code(logging_dict): "Error: please use a list for the list of parameters that you want to have logged" ) code = code.strip() - # print("MADE AUTO CODE\n\n{}\n\n{}\n\n{}\n".format('*'*60, repr(code), '*'*60)) return code #################################################################################### -def binary_c_log_code(code): +def binary_c_log_code(code, verbose): """ Function to construct the code to construct the custom logging function """ + + if verbose > 0: + print("Creating the code for the shared library for the custom logging") + + # Create code custom_logging_function_string = """\ #pragma push_macro(\"MAX\") #pragma push_macro(\"MIN\") @@ -91,7 +101,7 @@ void binary_c_API_function custom_output_function(struct stardata_t * stardata) return textwrap.dedent(custom_logging_function_string) -def binary_c_write_log_code(code, filename): +def binary_c_write_log_code(code, filename, verbose=0): """ Function to write the generated logging code to a file """ @@ -105,6 +115,8 @@ def binary_c_write_log_code(code, filename): except: print("Error while deleting file {}".format(filePath)) + if verbose > 0: + print("Writing the custom logging code to {}".format(filePath)) with open(filePath, "w") as f: f.write(code) @@ -125,7 +137,7 @@ def from_binary_c_config(config_file, flag): return res -def return_compilation_dict(verbose=False): +def return_compilation_dict(verbose=0): """ Function to build the compile command for the shared library @@ -139,6 +151,10 @@ def return_compilation_dict(verbose=False): - string containing the command to build the shared library """ + if verbose > 0: + print( + "Calling the binary_c config code to get the info to build the shared library" + ) # use binary_c-config to get necessary flags BINARY_C_DIR = os.getenv("BINARY_C") if BINARY_C_DIR: @@ -209,12 +225,7 @@ def return_compilation_dict(verbose=False): ] libs = "{} {}".format(" ".join(library_paths), " ".join(non_library_paths)) - if verbose: - print( - "Building shared library for custom logging with (binary_c.h) at {} on {}\n".format( - BINARY_C_SRC_DIR, socket.gethostname() - ) - ) + if verbose > 0: print( "With options:\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 @@ -224,13 +235,13 @@ def return_compilation_dict(verbose=False): return {"cc": cc, "ld": ld, "ccflags": ccflags, "libs": libs, "inc": inc} -def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False): +def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=0): """ Function to write the custom logging code to a file and then compile it. """ # Write code to file - binary_c_write_log_code(code, sourcefile_name) + binary_c_write_log_code(code, sourcefile_name, verbose) # Remove the library if present: if os.path.exists(outfile_name): @@ -241,7 +252,7 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False): print("Error while deleting file {}".format(outfile_name)) # create compilation command - compilation_dict = return_compilation_dict() + compilation_dict = return_compilation_dict(verbose) # Construct full command command = "{cc} {ccflags} {libs} -o {outfile_name} {sourcefile_name} {inc}".format( @@ -257,11 +268,22 @@ def compile_shared_lib(code, sourcefile_name, outfile_name, verbose=False): command = " ".join(command.split()) # Execute compilation and create the library - if verbose: - print("Executing following command:\n{command}".format(command=command)) + if verbose > 0: + 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() + ) + ) + print( + "Executing following command to compile the shared library:\n{command}".format( + command=command + ) + ) res = subprocess.check_output("{command}".format(command=command), shell=True) - if verbose: + if verbose > 0: if res: print("Output of compilation command:\n{}".format(res)) @@ -282,7 +304,7 @@ def temp_dir(): return path -def create_and_load_logging_function(custom_logging_code): +def create_and_load_logging_function(custom_logging_code, verbose): """ Function to automatically compile the shared library with the given custom logging code and load it with ctypes @@ -300,9 +322,12 @@ def create_and_load_logging_function(custom_logging_code): custom_logging_code, sourcefile_name=os.path.join(temp_dir(), "custom_logging.c"), outfile_name=library_name, - # verbose=True + verbose=verbose, ) + if verbose > 0: + print("loading shared library for custom logging") + # Loading library dll1 = ctypes.CDLL("libgslcblas.so", mode=ctypes.RTLD_GLOBAL) dll2 = ctypes.CDLL("libgsl.so", mode=ctypes.RTLD_GLOBAL) @@ -316,4 +341,11 @@ def create_and_load_logging_function(custom_logging_code): libcustom_logging.custom_output_function, ctypes.c_void_p ).value + if verbose > 0: + print( + "loaded shared library for custom logging. custom_output_function is loaded in memory at {}".format( + func_memaddr + ) + ) + return func_memaddr diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 29e413f4800c0635a341c1a7ac05554279cf14a9..71590f1202f52168fcc0df66d7b56c07d9891828 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -112,9 +112,10 @@ def create_hdf5(data_dir): content_data_dir = os.listdir(data_dir) # Settings - settings_file = os.path.join(data_dir, [ - file for file in content_data_dir if file.endswith("_settings.json") - ][0]) + settings_file = os.path.join( + data_dir, + [file for file in content_data_dir if file.endswith("_settings.json")][0], + ) with open(settings_file, "r") as f: settings_json = json.load(f) diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index 31c5edfd71edf299b31a2f4ecf5c3d5e8fb5b117..5c3de15b3e21527204579d9865160eaf31663039 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -38,6 +38,7 @@ from binarycpython.utils.functions import get_defaults, parse_binary_c_version_i # TODO: add functionality to return the nuclear_mass_list # TODO: add functionality to return the source_list # TODO: add functionality to return the ensemble_list +# TODO: add verbosity options # DONE: add functionality to return the evcode_version_string # Make this function also an API call. Doest seem to get written to a buffer that is stored into a python object. rather its just written to stdout @@ -98,13 +99,14 @@ class Population(object): for key in kwargs.keys(): # Filter out keys for the bse_options if key in self.defaults.keys(): - print("adding: {}={} to BSE_options".format(key, kwargs[key])) + if self.grid_options["verbose"] > 0: + print("adding: {}={} to BSE_options".format(key, kwargs[key])) self.bse_options[key] = kwargs[key] # Filter out keys for the grid_options elif key in self.grid_options.keys(): - print("adding: {}={} to grid_options".format(key, kwargs[key])) - + if self.grid_options["verbose"] > 0: + print("adding: {}={} to grid_options".format(key, kwargs[key])) self.grid_options[key] = kwargs[key] # The of the keys go into a custom_options dict else: @@ -133,6 +135,8 @@ class Population(object): # How its set up now is that as input you need to give --cmdline "metallicity=0.002" # Its checked if this exists and handled accordingly. if args.cmdline: + if self.grid_options["verbose"] > 0: + print("Found cmdline args. Parsing them now") # Grab the input and split them up, while accepting only non-empty entries cmdline_args = args.cmdline split_args = [ @@ -163,7 +167,7 @@ class Population(object): parameter_dict = self.bse_options argline = "binary_c " - # TODO: check if that sort actually works + for param_name in sorted(parameter_dict): argline += "{} {} ".format(param_name, parameter_dict[param_name]) argline = argline.strip() @@ -243,6 +247,8 @@ 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))) ################################################### # Return functions @@ -282,7 +288,13 @@ class Population(object): return self.defaults - def return_all_info(self): + def return_all_info( + self, + include_population_settings=True, + include_binary_c_defaults=True, + include_binary_c_version_info=True, + include_binary_c_help_all=True, + ): """ Function that returns all the information about the population and binary_c """ @@ -290,21 +302,37 @@ class Population(object): from binarycpython.utils.functions import get_help_all # - population_settings = self.return_population_settings() - binary_c_defaults = self.return_binary_c_defaults() - binary_c_version_info = self.return_binary_c_version_info(parsed=True) - binary_c_help_all_info = get_help_all(print_help=False, return_dict=True) + all_info = {} # - all_info = {} - all_info["population_settings"] = population_settings - all_info["binary_c_defaults"] = binary_c_defaults - all_info["binary_c_version_info"] = binary_c_version_info - all_info["binary_c_help_all"] = binary_c_help_all_info + if include_population_settings: + population_settings = self.return_population_settings() + all_info["population_settings"] = population_settings + + # + if include_binary_c_defaults: + binary_c_defaults = self.return_binary_c_defaults() + all_info["binary_c_defaults"] = binary_c_defaults + + if include_binary_c_version_info: + binary_c_version_info = self.return_binary_c_version_info(parsed=True) + all_info["binary_c_version_info"] = binary_c_version_info + + if include_binary_c_help_all: + binary_c_help_all_info = get_help_all(print_help=False, return_dict=True) + all_info["binary_c_help_all"] = binary_c_help_all_info return all_info - def export_all_info(self, use_datadir=False, outfile=None): + def export_all_info( + self, + use_datadir=True, + outfile=None, + include_population_settings=True, + include_binary_c_defaults=True, + include_binary_c_version_info=True, + include_binary_c_help_all=True, + ): """ Function that exports the all_info to a json file @@ -314,7 +342,12 @@ class Population(object): TODO: Fix to write things to the directory. which options do which etc """ - all_info = self.return_all_info() + all_info = self.return_all_info( + include_population_settings=include_population_settings, + include_binary_c_defaults=include_binary_c_defaults, + include_binary_c_version_info=include_binary_c_version_info, + include_binary_c_help_all=include_binary_c_help_all, + ) if use_datadir: base_name = os.path.splitext(self.custom_options["base_filename"])[0] @@ -327,11 +360,15 @@ class Population(object): self.custom_options["data_dir"], settings_name ) + if self.grid_options["verbose"] > 0: + print("Writing settings to {}".format(settings_fullname)) # if not outfile.endswith('json'): with open(settings_fullname, "w") as f: f.write(json.dumps(all_info, indent=4)) else: + if self.grid_options["verbose"] > 0: + print("Writing settings to {}".format(outfile)) # if not outfile.endswith('json'): with open(outfile, "w") as f: f.write(json.dumps(all_info, indent=4)) @@ -342,26 +379,39 @@ class Population(object): """ # C_logging_code gets priority of C_autogen_code + if self.grid_options["C_auto_logging"]: # Generate real logging code - logging_line = autogen_C_logging_code(self.grid_options["C_auto_logging"]) + 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(logging_line) + custom_logging_code = binary_c_log_code( + logging_line, verbose=self.grid_options["verbose"] + ) # Load memory adress self.grid_options[ "custom_logging_func_memaddr" - ] = create_and_load_logging_function(custom_logging_code) + ] = create_and_load_logging_function( + custom_logging_code, 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(self.grid_options["C_logging_code"]) + custom_logging_code = binary_c_log_code( + 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(custom_logging_code) + ] = create_and_load_logging_function( + custom_logging_code, verbose=self.grid_options["verbose"] + ) ################################################### # Evolution functions @@ -447,63 +497,67 @@ class Population(object): # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries. - def evolve_population_comparison(self, parse_function, amt, nodes, custom_arg_file=None): + def evolve_population_comparison( + self, parse_function, amt, nodes, custom_arg_file=None + ): """ The function that will evolve the population. This function contains many steps """ ### Custom logging code: self.set_custom_logging() - + ### Load store self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") # Execute. - ### Part to test running this with and without multiprocessing. + ### Part to test running this with and without multiprocessing. import time import multiprocessing as mp from pathos.multiprocessing import ProcessingPool as Pool import random start_no_mp = time.time() - self.set(base_filename='no_mp_{}.dat'.format(amt)) + self.set(base_filename="no_mp_{}.dat".format(amt)) # amt = 1000 - masses = range(1, amt+1) + masses = range(1, amt + 1) for i in masses: mass = random.randint(1, 500) # print(mass) - self.set_bse_option('M_1', mass) + self.set_bse_option("M_1", mass) out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) + self.return_argline(), + self.grid_options["custom_logging_func_memaddr"], + self.grid_options["store_memaddr"], + ) # parse_function(self, out) stop_no_mp = time.time() - print("without mp: {} systems took {}s".format(amt, stop_no_mp-start_no_mp)) + print("without mp: {} systems took {}s".format(amt, stop_no_mp - start_no_mp)) ######################################################### start_mp = time.time() - self.set(base_filename='mp_{}.dat'.format(amt)) + self.set(base_filename="mp_{}.dat".format(amt)) + def evolve_mp(mass): # print(mass) - self.set_bse_option('M_1', mass) + self.set_bse_option("M_1", mass) # self.set(M_1=mass) out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) + self.return_argline(), + self.grid_options["custom_logging_func_memaddr"], + self.grid_options["store_memaddr"], + ) # parse_function(self, out) p = Pool(nodes=nodes) + def g(amt): # amt = 1000 - masses = range(1, amt+1) + masses = range(1, amt + 1) for i in masses: mass = random.randint(1, 500) yield mass @@ -512,12 +566,15 @@ class Population(object): r = list(p.imap(evolve_mp, g(amt))) stop_mp = time.time() - print("with mp: {} systems took {}s".format(amt, stop_mp-start_mp)) + print("with mp: {} systems took {}s".format(amt, stop_mp - start_mp)) ######################################################### - print("Running mp versus no mp is {} times faster!".format((start_no_mp-stop_no_mp)/(start_mp-stop_mp))) - return (nodes, amt, stop_no_mp-start_no_mp, stop_mp-start_mp) - + print( + "Running mp versus no mp is {} times faster!".format( + (start_no_mp - stop_no_mp) / (start_mp - stop_mp) + ) + ) + return (nodes, amt, stop_no_mp - start_no_mp, stop_mp - start_mp) def evolve_population_mp(self, parse_function, mass_distribution): """ @@ -526,22 +583,24 @@ class Population(object): ### Custom logging code: self.set_custom_logging() - + ### Load store self.grid_options["store_memaddr"] = binary_c_python_api.return_store("") # evolve with mp start_mp = time.time() + def evolve_mp(mass): - self.set_bse_option('M_1', mass) + self.set_bse_option("M_1", mass) out = binary_c_python_api.run_population( - self.return_argline(), - self.grid_options["custom_logging_func_memaddr"], - self.grid_options["store_memaddr"], - ) + self.return_argline(), + self.grid_options["custom_logging_func_memaddr"], + self.grid_options["store_memaddr"], + ) parse_function(self, out) - p = Pool(nodes=self.grid_options['amt_cores']) + p = Pool(nodes=self.grid_options["amt_cores"]) + def g(mass_distribution): masses = mass_distribution for mass in masses: @@ -551,8 +610,13 @@ class Population(object): r = list(p.imap(evolve_mp, g(mass_distribution))) stop_mp = time.time() - print("with mp: {} systems took {}s using {} cores".format(len(mass_distribution), stop_mp-start_mp, self.grid_options['amt_cores'])) - + print( + "with mp: {} systems took {}s using {} cores".format( + len(mass_distribution), + stop_mp - start_mp, + self.grid_options["amt_cores"], + ) + ) # TODO: add functionality to unload all the stores etc @@ -627,17 +691,32 @@ class Population(object): # Write some info in the function code_string += ( indent * depth - + "# Grid code generated on {}\n".format(datetime.datetime.now().isoformat()) - + indent * depth + "# This function generates the systems that will be evolved with binary_c\n\n" + + "# Grid code generated on {}\n".format( + datetime.datetime.now().isoformat() + ) + + indent * depth + + "# This function generates the systems that will be evolved with binary_c\n\n" ) # Set some values in the generated code: - code_string += indent * depth + '# Setting initial values\n' + code_string += indent * depth + "# Setting initial values\n" code_string += indent * depth + "total_starcount = 0\n" - code_string += indent * depth + "starcounts = [0 for i in range({})]\n".format(total_grid_variables) + code_string += indent * depth + "starcounts = [0 for i in range({})]\n".format( + total_grid_variables + ) code_string += indent * depth + "probabilities = {}\n" - code_string += indent * depth + "probabilities_list = [0 for i in range({})]\n".format(total_grid_variables) - code_string += indent * depth + "probabilities_sum = [0 for i in range({})]\n".format(total_grid_variables) + code_string += ( + indent * depth + + "probabilities_list = [0 for i in range({})]\n".format( + total_grid_variables + ) + ) + code_string += ( + indent * depth + + "probabilities_sum = [0 for i in range({})]\n".format( + total_grid_variables + ) + ) code_string += indent * depth + "parameter_dict = {}\n" code_string += indent * depth + "\n" @@ -656,7 +735,7 @@ class Population(object): ################################################################################# # Start of code generation ################################################################################# - code_string += indent * depth + "\n" + code_string += indent * depth + "\n" # Generate code print("Generating grid code") for el in sorted( @@ -674,7 +753,7 @@ class Population(object): # Add comment code_string += ( indent * depth - + "# Condition for {}".format(grid_variable['parameter_name']) + + "# Condition for {}".format(grid_variable["parameter_name"]) + "\n" ) @@ -688,29 +767,25 @@ class Population(object): # Add condition failed action: #TODO: add correct exception error code_string += ( indent * (depth + 1) - + 'print("Condition for {} not met!")'.format(grid_variable["parameter_name"]) - + "\n" - ) - code_string += ( - indent * (depth + 1) - + "raise ValueError" + + 'print("Condition for {} not met!")'.format( + grid_variable["parameter_name"] + ) + "\n" ) + code_string += indent * (depth + 1) + "raise ValueError" + "\n" # Add some whiteline - code_string += (indent * (depth + 1) + "\n") + code_string += indent * (depth + 1) + "\n" ######################### # Setting up the forloop # Add comment for forloop code_string += ( indent * depth - + "# for loop for {}".format( - grid_variable["parameter_name"]) + + "# for loop for {}".format(grid_variable["parameter_name"]) + "\n" ) - # Adding for loop structure code_string += ( indent * depth @@ -720,9 +795,8 @@ class Population(object): + "\n" ) - ######################### - # Setting up pre-code and + # Setting up pre-code and # Add pre-code code_string += ( indent * (depth + 1) @@ -735,46 +809,49 @@ class Population(object): ####################### # Probabilities # Calculate probability - code_string += indent * (depth + 1) + "\n" + code_string += indent * (depth + 1) + "\n" code_string += indent * (depth + 1) + "# Setting probabilities\n" code_string += ( indent * (depth + 1) - + 'd{} = {}'.format(grid_variable['name'], grid_variable["probdist"]) + + "d{} = {}".format(grid_variable["name"], grid_variable["probdist"]) + "\n" ) # Saving probability sum code_string += ( indent * (depth + 1) - + 'probabilities_sum[{}] += d{}'.format( - grid_variable['grid_variable_number'], - grid_variable["name"] - ) + + "probabilities_sum[{}] += d{}".format( + grid_variable["grid_variable_number"], grid_variable["name"] + ) + "\n" ) - if grid_variable['grid_variable_number'] == 0: + if grid_variable["grid_variable_number"] == 0: code_string += ( - indent * (depth + 1) - + 'probabilities_list[0] = d{}'.format(grid_variable['name']) - + "\n" - ) + indent * (depth + 1) + + "probabilities_list[0] = d{}".format(grid_variable["name"]) + + "\n" + ) else: code_string += ( - indent * (depth + 1) - + 'probabilities_list[{}] = probabilities_list[{}] * d{}'.format(grid_variable['grid_variable_number'], grid_variable['grid_variable_number']-1, grid_variable['name']) - + "\n" - ) + indent * (depth + 1) + + "probabilities_list[{}] = probabilities_list[{}] * d{}".format( + grid_variable["grid_variable_number"], + grid_variable["grid_variable_number"] - 1, + grid_variable["name"], + ) + + "\n" + ) ####################### # Increment starcount for this parameter code_string += "\n" - code_string += indent * (depth + 1) + "# Increment starcount for {}\n".format(grid_variable['parameter_name']) + code_string += indent * ( + depth + 1 + ) + "# Increment starcount for {}\n".format(grid_variable["parameter_name"]) code_string += ( indent * (depth + 1) - + 'starcounts[{}] += 1'.format( - grid_variable['grid_variable_number'], - ) + + "starcounts[{}] += 1".format(grid_variable["grid_variable_number"],) + "\n" ) @@ -793,71 +870,68 @@ class Population(object): # increment depth depth += 1 - ################################################################################# # Here are the calls to the queuing or other solution. this part is for every system # Add comment - code_string += indent * (depth) + "# Code below will get evaluated for every generated system\n" + code_string += ( + indent * (depth) + + "# Code below will get evaluated for every generated system\n" + ) # Calculate value - code_string += indent * (depth) + 'probability = self.grid_options["weight"] * probabilities_list[{}]'.format(grid_variable['grid_variable_number']) + "\n" - code_string += indent * (depth) + 'repeat_probability = probability / self.grid_options["repeat"]' + "\n" + code_string += ( + indent * (depth) + + 'probability = self.grid_options["weight"] * probabilities_list[{}]'.format( + grid_variable["grid_variable_number"] + ) + + "\n" + ) + code_string += ( + indent * (depth) + + 'repeat_probability = probability / self.grid_options["repeat"]' + + "\n" + ) code_string += indent * (depth) + "total_starcount += 1\n" code_string += indent * (depth) + "print(probabilities)\n" - code_string += indent * (depth) + 'print("total_starcount: ", total_starcount)\n' + code_string += ( + indent * (depth) + 'print("total_starcount: ", total_starcount)\n' + ) code_string += indent * (depth) + "yield(parameter_dict)\n" - - - - - - - - - - - # { - # { - # $self->increment_probtot($prob); - # { - # my $system; - # if($self->{_grid_options}->{binary}) - # { - # $system={ - # M_1=>$m1, - # M_2=>$m2, - # metallicity=>$self->metallicity(), - # orbital_period=>$per, - # eccentricity=>$eccentricity, - # probability=>$repeat_prob, - # phasevol=>$phasevol - # }; - # } - # else - # { - # $system={ - # M_1=>$m1, - # M_2=>0.01, - # metallicity=>$self->metallicity(), - # orbital_period=>$self->{_grid_options}->{single_star_period}, - # eccentricity=>0.0, - # probability=>$repeat_prob, - # phasevol=>$phasevol - # }; - # } - - # $self->queue_evolution_code_run($self->{_flexigrid}->{thread_q}, - # $system); - - # } - - - - - - - + # { + # { + # $self->increment_probtot($prob); + # { + # my $system; + # if($self->{_grid_options}->{binary}) + # { + # $system={ + # M_1=>$m1, + # M_2=>$m2, + # metallicity=>$self->metallicity(), + # orbital_period=>$per, + # eccentricity=>$eccentricity, + # probability=>$repeat_prob, + # phasevol=>$phasevol + # }; + # } + # else + # { + # $system={ + # M_1=>$m1, + # M_2=>0.01, + # metallicity=>$self->metallicity(), + # orbital_period=>$self->{_grid_options}->{single_star_period}, + # eccentricity=>0.0, + # probability=>$repeat_prob, + # phasevol=>$phasevol + # }; + # } + + # $self->queue_evolution_code_run($self->{_flexigrid}->{thread_q}, + # $system); + + # } ################################################################################# # Stop of code generation. Here the code is saved and written @@ -873,8 +947,6 @@ class Population(object): with open(gridcode_filename, "w") as f: f.write(code_string) - - def load_grid_function(self): """ Test function to run grid stuff. mostly to test the import diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 533aec119d130969ed6faf386b7c8a41e9118150..51ec55aee76b001f616dbfe81e590aae71e545a7 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -28,10 +28,9 @@ grid_options_defaults_dict = { "binary_c_dir": os.environ["BINARYC_DIR"], "tmp_dir": temp_dir(), # Probability: - "weight": 1.0, # weighting for the probability - "repeat": 1.0, # number of times to repeat each system (probability is adjusted to be 1/repeat) + "weight": 1.0, # weighting for the probability + "repeat": 1.0, # number of times to repeat each system (probability is adjusted to be 1/repeat) ## - # return_array_refs=>1, # quicker data parsing mode # sort_args=>1, # save_args=>1, @@ -89,7 +88,6 @@ grid_options_defaults_dict = { # merge_datafiles=>'', # merge_datafiles_filelist=>'', # # parameter space options - # binary=>0, # set to 0 for single stars, 1 for binaries # # if use_full_resolution is 1, then run a dummy grid to # # calculate the resolution. this could be slow... diff --git a/setup.py b/setup.py index 326e163596b269bf09ff13e07bb192226aba18e9..36a2278eee6b36974005a9bf6026e4048c4194ed 100644 --- a/setup.py +++ b/setup.py @@ -8,9 +8,11 @@ import re import sys # TODO: make this clean -GSL_DIR = os.getenv('GSL_DIR', None) +GSL_DIR = os.getenv("GSL_DIR", None) if not GSL_DIR: - print("Warning: GSL_DIR is not set, this might lead to errors along the installation if there is no other version of GSL in the include dirs") + print( + "Warning: GSL_DIR is not set, this might lead to errors along the installation if there is no other version of GSL in the include dirs" + ) # TODO: write code to know exact parent directory of this file. @@ -63,27 +65,30 @@ for x in defines: API_h = os.environ["BINARY_C"] + "/src/API/binary_c_API.h" binary_c_define_macros.extend([("BINARY_C_API_H", API_h)]) -# -include_dirs = [ - os.environ["BINARY_C"] + "/src", - os.environ["BINARY_C"] + "/src/API", - "include", ] + binary_c_incdirs + [os.path.join(GSL_DIR,'include')] if GSL_DIR else [] +# +include_dirs = ( + [os.environ["BINARY_C"] + "/src", os.environ["BINARY_C"] + "/src/API", "include",] + + binary_c_incdirs + + [os.path.join(GSL_DIR, "include")] + if GSL_DIR + else [] +) libraries = ["binary_c"] + binary_c_libs + ["binary_c_api"] library_dirs = [ - os.environ["BINARY_C"] + "/src", - "./", - os.path.join(CWD, "lib/"), - # os.path.join(CWD, "binarycpython/core/"), - ] + binary_c_libdirs + os.environ["BINARY_C"] + "/src", + "./", + os.path.join(CWD, "lib/"), + # os.path.join(CWD, "binarycpython/core/"), +] + binary_c_libdirs runtime_library_dirs = [ - os.environ["BINARY_C"] + "/src", - "./", - os.path.join(CWD, "lib/"), - # os.path.join(CWD, "binarycpython/core/"), - ] + binary_c_libdirs + os.environ["BINARY_C"] + "/src", + "./", + os.path.join(CWD, "lib/"), + # os.path.join(CWD, "binarycpython/core/"), +] + binary_c_libdirs # TODO: move the creeation of all the include stuff to here so its easier to print everything # print('\n') @@ -102,7 +107,7 @@ binary_c_python_api_module = Extension( # name="binarycpython.core.binary_c", name="binary_c_python_api", sources=["src/binary_c_python.c"], - include_dirs= include_dirs, + include_dirs=include_dirs, libraries=libraries, library_dirs=library_dirs, runtime_library_dirs=runtime_library_dirs, @@ -112,6 +117,7 @@ binary_c_python_api_module = Extension( language="C", ) + def readme(): with open("README.md") as f: return f.read() diff --git a/tests/population/multiprocessing_via_population_comparison.py b/tests/population/multiprocessing_via_population_comparison.py index f8d46679784c55c3c2fe8c89dff8c9c55e37ed9f..2fca56d4ed3e27aec6b82f0cb067f53903bf53a3 100644 --- a/tests/population/multiprocessing_via_population_comparison.py +++ b/tests/population/multiprocessing_via_population_comparison.py @@ -13,22 +13,17 @@ from binarycpython.utils.functions import get_help_all, get_help, create_hdf5 import argparse - - parser = argparse.ArgumentParser() parser.add_argument( - "amt_systems", - help='the amount of systems', + "amt_systems", help="the amount of systems", ) parser.add_argument( - "amt_nodes", - help='the amount of nodes that are used for the multiprocessing', + "amt_nodes", help="the amount of nodes that are used for the multiprocessing", ) parser.add_argument( - "name_testcase", - help='The name of the testcase (e.g. laptop, cluster etc)', + "name_testcase", help="The name of the testcase (e.g. laptop, cluster etc)", ) @@ -46,13 +41,14 @@ def output_lines(output): """ return output.splitlines() + def parse_function(self, output): # extract info from the population instance # TODO: think about whether this is smart. Passing around this object might be an overkill - # Get some information from the - data_dir = self.custom_options['data_dir'] - base_filename = self.custom_options['base_filename'] + # Get some information from the + data_dir = self.custom_options["data_dir"] + base_filename = self.custom_options["base_filename"] # Check directory, make if necessary os.makedirs(data_dir, exist_ok=True) @@ -65,17 +61,19 @@ def parse_function(self, output): headerline = el.split()[0] # CHeck the header and act accordingly - if (headerline=='DAVID_SN'): - parameters = ['time', 'mass_1', 'prev_mass_1', 'zams_mass_1', 'SN_type'] + if headerline == "DAVID_SN": + parameters = ["time", "mass_1", "prev_mass_1", "zams_mass_1", "SN_type"] values = el.split()[1:] - seperator='\t' + seperator = "\t" if not os.path.exists(outfilename): - with open(outfilename, 'w') as f: - f.write(seperator.join(parameters)+'\n') + with open(outfilename, "w") as f: + f.write(seperator.join(parameters) + "\n") + + with open(outfilename, "a") as f: + f.write(seperator.join(values) + "\n") + - with open(outfilename, 'a') as f: - f.write(seperator.join(values)+'\n') ## Set values test_pop = Population() test_pop.set( @@ -96,20 +94,29 @@ if(stardata->star[0].SN_type != SN_NONE) /* Kill the simulation to save time */ stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm; }; -""") - -test_pop.set(separation=1000000000, - orbital_period=400000000, - metallicity=0.002, - data_dir=os.path.join(os.environ['BINARYC_DATA_ROOT'], 'testing_python', 'multiprocessing2', name_testcase)) +""" +) -res = test_pop.evolve_population_comparison(parse_function, amt=int(amt_systems), nodes=int(amt_nodes)) -with open('comparison_result.dat', 'a') as f: - f.write(str(res)+'\n') +test_pop.set( + separation=1000000000, + orbital_period=400000000, + metallicity=0.002, + data_dir=os.path.join( + os.environ["BINARYC_DATA_ROOT"], + "testing_python", + "multiprocessing2", + name_testcase, + ), +) +res = test_pop.evolve_population_comparison( + parse_function, amt=int(amt_systems), nodes=int(amt_nodes) +) +with open("comparison_result.dat", "a") as f: + f.write(str(res) + "\n") mass_distribution = np.arange(1, 200) -# evolve_population_mp(parse_function, mass_distribution) \ No newline at end of file +# evolve_population_mp(parse_function, mass_distribution) diff --git a/tests/python_API_test.py b/tests/python_API_test.py index 6c79074efe4c9a613622ac4eab1ff67c98467963..8e05cd65e3be02d03348ec786847e2cb346e57c4 100755 --- a/tests/python_API_test.py +++ b/tests/python_API_test.py @@ -155,6 +155,7 @@ def test_run_system(): out = binary_c_python_api.run_system(argstring, -1, -1) print(out) + #### if __name__ == "__main__": # test_run_binary()