diff --git a/binarycpython/utils/grid.py b/binarycpython/utils/grid.py index c69bc71452d5ac3fd9e538fb6a57663fa6d8315f..f22688d7aeeaa84a5c6650188949ffee19dc4f54 100644 --- a/binarycpython/utils/grid.py +++ b/binarycpython/utils/grid.py @@ -706,17 +706,23 @@ class Population(object): ####################### # Dry run and getting starcount + self.grid_options['probtot'] = 0 + self.generate_grid_code(dry_run=True) self.load_grid_function() self.dry_run() - print(self.grid_options['total_starcount']) - + total_starcount_run = self.grid_options['total_starcount'] + print("Total starcount for this run will be: {}".format(total_starcount_run)) ####################### # Linear run + start_lin = time.time() + + self.grid_options['probtot'] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way + self.generate_grid_code(dry_run=False) self.load_grid_function() @@ -726,35 +732,46 @@ class Population(object): full_system_dict.update(system) binary_cmdline_string = self.return_argline(full_system_dict) - print(i+1, binary_cmdline_string) + out = binary_c_python_api.run_population( + binary_cmdline_string, + self.grid_options["custom_logging_func_memaddr"], + self.grid_options["store_memaddr"], + ) + print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) + + stop_lin = time.time() ####################### # MP run + self.grid_options['probtot'] = 0 # To make sure that the values are reset. TODO: fix this in a cleaner way + start_mp = time.time() self.generate_grid_code(dry_run=False) self.load_grid_function() - def evolve_system(system): - full_system_dict = self.bse_options.copy() - full_system_dict.update(system) - - binary_cmdline_string = self.return_argline(full_system_dict) - print(binary_cmdline_string) - + def evolve_system(binary_cmdline_string): + # print(binary_cmdline_string) + # pass + # print('next') # 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"], - # ) + out = binary_c_python_api.run_population( + binary_cmdline_string, + self.grid_options["custom_logging_func_memaddr"], + self.grid_options["store_memaddr"], + ) # # parse_function(self, out) def yield_system(): for i, system in enumerate(self.grid_options["system_generator"](self)): - print(i+1) - yield system + full_system_dict = self.bse_options.copy() + full_system_dict.update(system) + + binary_cmdline_string = self.return_argline(full_system_dict) + print("{}/{}".format(i+1, total_starcount_run), binary_cmdline_string) + + yield binary_cmdline_string print("generator done") # Create pool @@ -767,6 +784,8 @@ class Population(object): # Give feedback + print( + "Without mp: {} systems took {}s".format(total_starcount_run, stop_lin-start_lin)) print( "with mp: {} systems took {}s using {} cores".format( self.grid_options['total_starcount'], @@ -775,39 +794,39 @@ class Population(object): ) ) - quit() - ######################## + # quit() + # ######################## - # evolve with mp - start_mp = time.time() + # # evolve with mp + # start_mp = time.time() - def evolve_mp(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"], - ) - parse_function(self, out) + # def evolve_mp(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"], + # ) + # 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: - yield mass - print("generator done") + # def g(mass_distribution): + # masses = mass_distribution + # for mass in masses: + # yield mass + # print("generator done") - r = list(p.imap(evolve_mp, g(mass_distribution))) - stop_mp = time.time() + # 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"], + # ) + # ) def test_evolve_single(self): @@ -937,7 +956,7 @@ class Population(object): ) ) code_string += indent * depth + "parameter_dict = {}\n" - code_string += indent * depth + "phasevol = 0\n" + code_string += indent * depth + "phasevol = 1\n" code_string += indent * depth + "\n" code_string += indent * depth + "# setting probability lists\n" @@ -1021,6 +1040,13 @@ class Population(object): + "\n" ) + # # Some print statement + # code_string += ( + # indent * depth + # + "print('phasevol_{}:', phasevol_{})".format(grid_variable["name"], grid_variable["name"]) + # + "\n" + # ) + # Adding for loop structure code_string += ( indent * depth @@ -1042,8 +1068,8 @@ class Population(object): ) # Set phasevol - code_string += indent * (depth + 1) + "if {} > 0: phasevol *= {}\n".format( - grid_variable["name"], grid_variable["name"] + code_string += indent * (depth + 1) + "phasevol *= phasevol_{}\n".format( + grid_variable["name"], ) ####################### @@ -1155,6 +1181,8 @@ class Population(object): # indent * (depth + 1) + 'print("total_starcount: ", total_starcount)\n' # ) + # code_string += indent * (depth + 1) + "print(probability)\n" + # Increment total probability code_string += ( indent * (depth + 1) + "self.increment_probtot(probability)\n" @@ -1189,6 +1217,7 @@ class Population(object): reverse=True, ) ): + grid_variable = el[1] code_string += indent * (depth + 1) + "#" * 40 + "\n" code_string += ( indent * (depth + 1) @@ -1197,23 +1226,25 @@ class Population(object): # Set phasevol # TODO: fix. this isnt supposed to be the value that we give it here. discuss - code_string += indent * (depth + 1) + "if {} > 0: phasevol /= {}\n".format( - grid_variable["name"], grid_variable["name"] + code_string += indent * (depth + 1) + "phasevol /= phasevol_{}\n".format( + grid_variable["name"] ) + code_string += indent * (depth + 1) + "\n" + depth -= 1 ################ # Finalising print statements # - code_string += indent * (depth + 1) + "\n" + # code_string += indent * (depth + 1) + "\n" code_string += indent * (depth + 1) + "#" * 40 + "\n" code_string += ( indent * (depth + 1) + "print('Grid has handled {} stars'.format(total_starcount))\n" ) code_string += ( - indent * (depth + 1) + "print('with a total probability of {}'.format(2))\n" + indent * (depth + 1) + "print('with a total probability of {}'.format(self.grid_options['probtot']))\n" ) if dry_run: diff --git a/tests/population/grid_tests.py b/tests/population/grid_tests.py index 3b45b999c8e807cd676f44feed133295b71595e9..787e64690b649c6e5efe7d5695bf089a4a383fe7 100644 --- a/tests/population/grid_tests.py +++ b/tests/population/grid_tests.py @@ -252,59 +252,17 @@ test_pop.set( #### # Dry run: -test_pop.set(verbose=1) -test_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="", -) - -# ### Grid generating test. -# test_pop.add_grid_variable( -# name="period", -# longname="period", -# valuerange=["M_1", 20], -# resolution="10", -# spacingfunc="np.linspace(1, 10, 10)", -# precode="orbital_period = period**2", -# probdist="flat(orbital_period)", -# parameter_name="orbital_period", -# dphasevol="", -# condition='self.grid_options["binary"]==0', -# ) - -test_pop.generate_grid_code(dry_run=True) - -# test_pop.load_grid_function() - -# test_pop.dry_run() - -#### -# testing population: - -# test_pop.set(verbose=1, -# amt_cores=1, -# binary=0, -# ) +# test_pop.set(verbose=1) # test_pop.add_grid_variable( -# name="M_1", +# name="lnm1", # longname="log primary mass", # valuerange=[10, 20], # resolution="10", -# spacingfunc="const(10, 20, 4)", -# precode="", -# # precode="M_1=math.exp(lnm1)", +# 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="dlnm1", +# dphasevol="", # parameter_name="M_1", # condition="", # ) @@ -315,14 +273,64 @@ test_pop.generate_grid_code(dry_run=True) # longname="period", # valuerange=["M_1", 20], # resolution="10", -# spacingfunc="", # spacingfunc="np.linspace(1, 10, 10)", # precode="orbital_period = period**2", # probdist="flat(orbital_period)", # parameter_name="orbital_period", -# dphasevol="dper", +# dphasevol="", # condition='self.grid_options["binary"]==0', # ) -# test_pop.test_evolve_population() # test_pop.generate_grid_code(dry_run=True) + +# test_pop.load_grid_function() + +# test_pop.dry_run() + +# ### +# testing population: + +test_pop.set(verbose=1, + amt_cores=1, + binary=0, +) + +test_pop.add_grid_variable( + name="M_1", + longname="log primary mass", + valuerange=[10, 100], + resolution="20", + spacingfunc="const(10, 100, 20)", + precode="", + # precode="M_1=math.exp(lnm1)", + probdist="flat(M_1)", + # probdist='self.custom_options["extra_prob_function"](M_1)', + dphasevol="dlnm1", + parameter_name="M_1", + condition="", +) + +### Grid generating test. +test_pop.add_grid_variable( + name="period", + longname="period", + valuerange=["M_1", 20], + resolution="20", + spacingfunc="np.linspace(1, 10, 20)", + precode="orbital_period = period**2", + probdist="flat(orbital_period)", + parameter_name="orbital_period", + dphasevol="dper", + condition='self.grid_options["binary"]==0', +) + +test_pop.set(verbose=1, + amt_cores=4, + binary=0, +) + + +test_pop.test_evolve_population() + +# print(test_pop.grid_options["probtot"]) +# print(test_pop.grid_options["count"]) \ No newline at end of file diff --git a/tests/population/profile_run.txt b/tests/population/profile_run.txt new file mode 100644 index 0000000000000000000000000000000000000000..ccdeddae2bb5a06ceca311d9b53de11dfb074072 Binary files /dev/null and b/tests/population/profile_run.txt differ diff --git a/tests/population/profile_test.txt b/tests/population/profile_test.txt new file mode 100644 index 0000000000000000000000000000000000000000..34a8484623b8fcdd999256a15ff7f525f58c1b78 Binary files /dev/null and b/tests/population/profile_test.txt differ diff --git a/tests/population/readout_profile.py b/tests/population/readout_profile.py new file mode 100644 index 0000000000000000000000000000000000000000..5975c324f599ac8db9aebdaaf41f31ee35d8982e --- /dev/null +++ b/tests/population/readout_profile.py @@ -0,0 +1,5 @@ +import pstats +p = pstats.Stats('profile_run.txt') +p.sort_stats('tottime').print_stats(10) + +