diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index ba5f677c70305f01617802cea6375a56110fdd27..507983858bf43318b8d9ff6320575557390d39d4 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -298,9 +298,11 @@ class Population(object): # Evolution functions ################################################### - def evolve_single(self): + def evolve_single(self, parse_function=None): """ Function to run a single system + + The output of the run gets returned, unless a parse function is given to this function. """ ### Custom logging code: @@ -318,7 +320,11 @@ class Population(object): # TODO: add call to function that cleans up the temp customlogging dir, and unloads the loaded libraries. - return out + if parse_function: + parse_function(self, out) + + else: + return out def evolve_population(self, custom_arg_file=None): """ diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py index 66dd4b29052b30dee92e0c1edfe4821420103fbe..538ae76ba2d96214ce98b8e60b420e018c8263cd 100644 --- a/tests/population/grid_tests.py +++ b/tests/population/grid_tests.py @@ -1,9 +1,17 @@ import os import json +import time +import pickle +import sys + +import matplotlib.pyplot as plt + from binarycpython.utils.grid import Population from binarycpython.utils.functions import get_help_all, get_help +## Script is intended for some testing of grid functionality. Its a bit random, not really structured tbh + test_pop = Population() @@ -39,10 +47,9 @@ test_pop.set( # test_pop.return_argline() ## return version info -version_info = test_pop.return_binary_c_version_info() -print(version_info) +# version_info = test_pop.return_binary_c_version_info() +# print(version_info) -quit() ## 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') @@ -76,11 +83,11 @@ test_pop.set( ); }; /* Kill the simulation to save time */ - //stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm; + // stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm; }; """ ) -test_pop.evolve_population() +# test_pop.evolve_population() ## Help all @@ -90,6 +97,109 @@ test_pop.evolve_population() # print(get_help('M_1', print_help=False, return_dict=True)) -# return all info: +## return all info: # print(json.dumps(test_pop.return_all_info(), indent=4)) -test_pop.export_all_info(outfile=os.path.join(os.getcwd(), "test_output.txt")) +# test_pop.export_all_info(outfile=os.path.join(os.getcwd(), "test_output.txt")) + +################# Parse function +## Testing some stuff out with giving a parse_function. +test_pop.set( + C_logging_code=""" + if(stardata->star[0].stellar_type>=NS) + { + if (stardata->model.time < stardata->model.max_evolution_time) + { + Printf("DAVID_SCO %30.12e %g %g %g %g %d %d\\n", + // + stardata->model.time, // 1 + + stardata->star[0].mass, //2 + stardata->previous_stardata->star[0].mass, //3 + + stardata->star[0].radius, //4 + stardata->previous_stardata->star[0].radius, //5 + + stardata->star[0].stellar_type, //6 + stardata->previous_stardata->star[0].stellar_type //7 + ); + }; + /* Kill the simulation to save time */ + stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm; + }; +""" +) + + +def output_lines(output): + """ + Function that outputs the lines that were recieved from the binary_c run. + """ + 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'] + outfilename = os.path.join(data_dir, base_filename) + + # TODO: make population settings available in this function + for el in output_lines(output): + headerline = el.split()[0] + + if (headerline=='DAVID_SCO'): + parameters = ['time', 'mass_1', 'prev_mass_1', 'radius_1', 'prev_radius_1', 'stellar_type_1', 'prev_stellar_type_1'] + values = el.split()[1:] + seperator='\t' + + if not os.path.exists(outfilename): + with open(outfilename, 'w') as f: + f.write(seperator.join(parameters)+'\n') + + with open(outfilename, 'a') as f: + f.write(seperator.join(values)+'\n') + + +#test_pop.evolve_single(parse_function) +test_pop.set(M_1=90, data_dir=os.path.join(os.environ['BINARYC_DATA_ROOT'], 'testing_python'), base_filename='test_pop.dat') +test_pop.evolve_single(parse_function) +test_pop.set(M_1=80) +test_pop.evolve_single(parse_function) +test_pop.set(M_1=70) +test_pop.evolve_single(parse_function) +test_pop.set(M_1=60) +test_pop.evolve_single(parse_function) + +################## +## CHeck size of commands: +## Using pickle to dump it. +## https://stackoverflow.com/questions/563840/how-can-i-check-the-memory-usage-of-objects-in-ipython/565382#565382 + +# def generate_commands_return_size(amt_systems): +# print("Doing {} systems".format(amt_systems)) +# start_generate_time = time.time() +# commands = [test_pop.return_argline() for el in range(amt_systems)] +# stop_generate_time = time.time() + +# size = sys.getsizeof(pickle.dumps(commands)) +# size_in_mb = size/(1024*1024) +# return size_in_mb + +# amounts = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000] +# sizes = [] +# for amount in amounts: +# sizes.append(generate_commands_return_size(amount)) + + +# plt.title('size scaling for binary_c commands with:\n`{}`'.format(test_pop.return_argline())) +# plt.plot(amounts, sizes, 'bo', label='MB for ') +# plt.legend() +# plt.xlabel("Amount of systems") +# plt.ylabel("Size in MB") +# plt.xscale('log') +# plt.yscale('log') +# plt.savefig('sizes_for_commands.png') +# plt.show() +