From b1793395b40491e09858fefd0c9828f8e903f979 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Tue, 14 Jan 2020 00:25:40 +0000 Subject: [PATCH] building population code. added some grid options, loading custom logging code now works (but not yet on runing the population tho) --- binarycpython/utils/grid.py | 108 ++++++++++++++++--- binarycpython/utils/grid_options_defaults.py | 10 ++ tests/population/custom_arg_file.txt | 2 + 3 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 tests/population/custom_arg_file.txt diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index caf425b34..0980dd3ab 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -1,6 +1,7 @@ import os import copy import json +import sys import binary_c_python_api @@ -10,12 +11,15 @@ import binarycpython from grid_options_defaults import grid_options_defaults_dict +from custom_logging_functions import autogen_C_logging_code, binary_c_log_code, create_and_load_logging_function +# TODO: add functionality to parse cmdline args # TODO: add functionality to 'on-init' set arguments -# TODO: add functionality to export the arg string. +# DONE: add functionality to export the arg string. # TODO: add functionality to export all the options # TODO: add functionality to create the dict that goes into the arg line. + # TODO: add functionality to return the initial_abundance_hash # TODO: add functionality to return the isotope_hash # TODO: add functionality to return the isotope_list @@ -23,7 +27,9 @@ from grid_options_defaults import grid_options_defaults_dict # 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 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 # TODO: add functionality to return the evcode_args_list @@ -132,6 +138,9 @@ class Population(object): if the parameter name is not included in either of those, then it will be stored in an custom_options dict. """ + print(self.grid_options.keys()) + + for key in kwargs.keys(): # Filter out keys for the bse_options if key in self.bse_options.keys(): @@ -143,10 +152,70 @@ class Population(object): else: self.custom_options[key] = kwargs[key] - def evolve(self): + def evolve_population(self, custom_arg_file=None): + """ + The function that will evolve the population. This function contains many steps + """ + + ### Custom logging code: + # 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'] + ) + + # Generate entire shared lib code around logging lines + custom_logging_code = binary_c_log_code(logging_line) + + # Load memory adress + self.grid_options['custom_logging_func_memaddr'] = create_and_load_logging_function(custom_logging_code) + # + 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']) + + # Load memory adress + self.grid_options['custom_logging_func_memaddr'] = create_and_load_logging_function(custom_logging_code) + ### + + + + + ### Arguments + # If user inputs a file containing arg lines then use that + if custom_arg_file: + # check if file exists + if os.path.isfile(custom_arg_file): + # load file + with open(custom_arg_file) as f: + # Load lines into list + temp = f.read().splitlines() + + # Filter out all the lines that dont start with binary_c + population_arglines = [line for line in temp if line.startswith('binary_c')] + + else: + # generate population from options + pass + + ####### + # Do stuff + for line in population_arglines: + print(line) + + + + pass + def generate_population_arglines_file(self, output_file): + """ + Function to generate a file that contains all the argument lines that would be given to binary_c if the population had been run + """ + + def evolve_single(self): """ @@ -159,8 +228,8 @@ class Population(object): out = binary_c_python_api.run_binary(argline) # out = binary_c_python_api.run_binary('binary_c M_1 15 M_2 14 separation 0 orbital_period 4530 eccentricity 0 metallicity 0.02 max_evolution_time 15000') - print(out) - + # print(out) + return out def return_binary_c_version_info(self): """ @@ -168,6 +237,7 @@ class Population(object): """ out = binary_c_python_api.run_binary('binary_c version') + # out = binary_c_python_api.run_binary('binary_c M_1 15 M_2 14 separation 0 orbital_period 4530 eccentricity 0 metallicity 0.02 max_evolution_time 15000') return out def test_evolve_single(self): @@ -209,25 +279,37 @@ test_pop = Population() test_pop.set(M_1=10, separation=0, orbital_period=4580, max_evolution_time=15000, eccentricity=0.02, ) - -# Testing single evolution -test_pop.evolve_single() +## Testing single evolution +# test_pop.evolve_single() # test_pop.test_evolve_single() ## Setting custom value # test_pop.set(data_dir=os.path.join(os.environ['BINARYC_DATA_ROOT'], 'development_example')) # print(test_pop.custom_options['data_dir']) -# printing all options + +## printing all options # print(json.dumps(test_pop.return_options(), indent=4)) -# return arglines: +## return arglines: # test_pop.set(M_1=10, M_2=500) -print(test_pop.return_argline()) +# print(test_pop.return_argline()) # test_pop.return_argline(print_excluded=True) #print(test_pop.bse_options) -# return version info -a = str(test_pop.return_binary_c_version_info()) -print(a) \ No newline at end of file +## return version info +# a = test_pop.return_binary_c_version_info() +# print(a) + +## Use custom arg file +# test_pop.evolve_population(custom_arg_file='/home/david/projects/binary_c_root/binary_c-python/tests/population/custom_arg_file.txt') + + + +## Custom logging: +# test_pop.set(C_auto_logging={'MY_HEADER_LINE': ['star[0].mass', 'star[1].mass', 'model.probability']}) +# test_pop.set(C_logging_code='Printf("MY_STELLAR_DATA time=%g mass=%g\\n", stardata->model.time, stardata->star[0].mass);') +# test_pop.set(C_logging_code='Printf("MY_HEADER_LINE %g %g %g\\n",((double)stardata->star[0].mass),((double)stardata->star[1].mass),((double)stardata->model.probability));') +# test_pop.evolve_population() + diff --git a/binarycpython/utils/grid_options_defaults.py b/binarycpython/utils/grid_options_defaults.py index 155bd6f0c..f635578f9 100644 --- a/binarycpython/utils/grid_options_defaults.py +++ b/binarycpython/utils/grid_options_defaults.py @@ -1,3 +1,13 @@ grid_options_defaults_dict = { 'custom_logging_function': None, # This will hold the custom logging mem addr + 'amt_cores': 1, # total amount of cores used to evolve the population + 'verbose': 0, # Level of verbosity of the simulation + + + # Custom logging + '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 + + } diff --git a/tests/population/custom_arg_file.txt b/tests/population/custom_arg_file.txt new file mode 100644 index 000000000..a635b156f --- /dev/null +++ b/tests/population/custom_arg_file.txt @@ -0,0 +1,2 @@ +binary_c --M_1 10 --M2 5 --orbital_period 1000000 +binary_c --M_1 10 --M2 10 --orbital_period 1000000 -- GitLab