Tutorial: Running populations with binary_c-python¶
This notebook will show you how to evolve a population of stars
Much of the code in the binarycpython package is written to evolve a population of stars through the Population object, rather than running a single system. Let’s go through the functionality of this object step by step and set up some example populations.
At the bottom of this notebook there are some complete example scripts
[1]:
import os
from binarycpython.utils.custom_logging_functions import temp_dir
from binarycpython.utils.grid import Population
TMP_DIR = temp_dir("notebooks", "notebook_population")
# help(Population) # Uncomment to see the public functions of this object
Setting up the Population object¶
To set up and configure the population object we need to make an object instance of the Population
object, and add configuration via the .set()
function.
There are three categories of options that the Population object can set: - BSE options: these options will be used for the binary_c calls, and are recognized by comparing the arguments to a known list of available arguments of binary_c. To see which options are available, see section `binary_c parameters
in the documentation <https://ri0005.pages.surrey.ac.uk/binary_c-python/binary_c_parameters.html>`__. You can access these through population.bse_options['<bse option name>']
after you
have set them.
Grid options: these options will be used to configure the behaviour of the Population object. To see which options are available, see section
`Population grid code options
in the documentation <https://ri0005.pages.surrey.ac.uk/binary_c-python/grid_options_descriptions.html>`__. They can be accessed viapopulation.grid_options['<grid option name>']
after you have set them.Custom options: these options are not recognized as either of the above, so they will be stored in the custom_options, and can be accessed via
population.custom_options['<custom option name>']
[2]:
# Create population object
example_pop = Population()
# If you want verbosity, set this before other things
example_pop.set(verbosity=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.
# Those that are present in the default grid_options are set in grid_options
# All other values that you set are put in a custom_options dict
example_pop.set(
# binary_c physics options
M_1=10, # bse_options
orbital_period=45000000080, # bse_options
max_evolution_time=15000, # bse_options
eccentricity=0.02, # bse_options
# grid_options
num_cores=2,
tmp_dir=TMP_DIR,
# Custom options # TODO: need to be set in grid_options probably
data_dir=os.path.join(
TMP_DIR, "example_python_population_result"
), # custom_options
base_filename="example_pop.dat", # custom_options
)
# We can use the options through
print(example_pop.grid_options['verbosity'])
print(example_pop.custom_options['base_filename'])
print(example_pop.bse_options['M_1'])
adding: M_1=10 to BSE_options
adding: orbital_period=45000000080 to BSE_options
adding: max_evolution_time=15000 to BSE_options
adding: eccentricity=0.02 to BSE_options
adding: num_cores=2 to grid_options
adding: tmp_dir=/tmp/binary_c_python-izzard/notebooks/notebook_population to grid_options
<<<< Warning: Key does not match previously known parameter: adding: data_dir=/tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result to custom_options >>>>
<<<< Warning: Key does not match previously known parameter: adding: base_filename=example_pop.dat to custom_options >>>>
1
example_pop.dat
10
After configuring the population, but before running the actual population, its usually a good idea to export the full configuration (including version info of binary_c and all the parameters) to a file. To do this we use example_pop.export_all_info()
.
On default this exports everything, each of the sections can be disabled:
population settings (bse_options, grid_options, custom_options), turn off with include_population settings=False
binary_c_defaults (all the commandline arguments that binary c accepts, and their defaults). turn off with include_binary_c_defaults=False
include_binary_c_version_info (all the compilation info, and information about the compiled parameters), turn off with include_binary_c_version_info=False
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=<>
[3]:
example_pop.export_all_info()
Writing settings to /tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result/example_pop_settings.json
[3]:
'/tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result/example_pop_settings.json'
Adding grid variables¶
The main purpose of the Population object is to handle the population synthesis side of running a set of stars. The main method to do this with binarycpython, as is the case with Perl binarygrid, is to use grid variables. These are loops over a predefined range of values, where a probability will be assigned to the systems based on the chosen probability distributions.
Usually we use either 1 mass grid variable, or a trio of mass, mass ratio and period (See below for full examples of all of these). We can, however, also add grid sampling for e.g. eccentricity, metallicity or other parameters.
In some cases it could be easier to set up a for loop that sets that parameter and calls the evolve function several times, e.g. when you want to vary a prescription (usually a discrete, unweighted parameter)
A notable special type of grid variable is that of the Moe & di Stefano 2017 dataset (see further down in the notebook).
To add a grid variable to the population object we use example_pop.add_grid_variable
(see next cell)
[4]:
help(example_pop.add_grid_variable)
Help on method add_grid_variable in module binarycpython.utils.grid:
add_grid_variable(name: str, parameter_name: str, longname: str, valuerange: Union[list, str], samplerfunc: str, probdist: str, dphasevol: Union[str, int], gridtype: str = 'centred', branchpoint: int = 0, branchcode: Optional[str] = None, precode: Optional[str] = None, postcode: Optional[str] = None, topcode: Optional[str] = None, bottomcode: Optional[str] = None, condition: Optional[str] = None) -> None method of binarycpython.utils.grid.Population instance
Function to add grid variables to the grid_options.
The execution of the grid generation will be through a nested for loop.
Each of the grid variables will get create a deeper for loop.
The real function that generates the numbers will get written to a new file in the TMP_DIR,
and then loaded imported and evaluated.
beware that if you insert some destructive piece of code, it will be executed anyway.
Use at own risk.
Tasks:
- TODO: Fix this complex function.
Args:
name:
name of parameter used in the grid Python code.
This is evaluated as a parameter and you can use it throughout
the rest of the function
Examples:
name = 'lnm1'
parameter_name:
name of the parameter in binary_c
This name must correspond to a Python variable of the same name,
which is automatic if parameter_name == name.
Note: if parameter_name != name, you must set a
variable in "precode" or "postcode" to define a Python variable
called parameter_name
longname:
Long name of parameter
Examples:
longname = 'Primary mass'
range:
Range of values to take. Does not get used really, the samplerfunc is used to
get the values from
Examples:
range = [math.log(m_min), math.log(m_max)]
samplerfunc:
Function returning a list or numpy array of samples spaced appropriately.
You can either use a real function, or a string representation of a function call.
Examples:
samplerfunc = "const(math.log(m_min), math.log(m_max), {})".format(resolution['M_1'])
precode:
Extra room for some code. This code will be evaluated within the loop of the
sampling function (i.e. a value for lnm1 is chosen already)
Examples:
precode = 'M_1=math.exp(lnm1);'
postcode:
Code executed after the probability is calculated.
probdist:
Function determining the probability that gets assigned to the sampled parameter
Examples:
probdist = 'Kroupa2001(M_1)*M_1'
dphasevol:
part of the parameter space that the total probability is calculated with. Put to -1
if you want to ignore any dphasevol calculations and set the value to 1
Examples:
dphasevol = 'dlnm1'
condition:
condition that has to be met in order for the grid generation to continue
Examples:
condition = 'self.grid_options['binary']==1'
gridtype:
Method on how the value range is sampled. Can be either 'edge' (steps starting at
the lower edge of the value range) or 'centred'
(steps starting at lower edge + 0.5 * stepsize).
topcode:
Code added at the very top of the block.
bottomcode:
Code added at the very bottom of the block.
All the distribution functions that we can use are stored in the binarycpython.utils.distribution_functions
or binarycpython/utils/distribution_functions.py
on git. If you uncomment the help statement below you can see which functions are available now:
[5]:
# import binarycpython.utils.distribution_functions
# help(binarycpython.utils.distribution_functions)
The next cell contains an example of adding the mass grid variable, but sampling in log mass. The commented grid variables are examples of the mass ratio sampling and the period sampling.
[6]:
# Add grid variables
resolution = {"M_1": 20}
# Mass
example_pop.add_grid_variable(
name="lnm1",
longname="Primary mass",
valuerange=[2, 150],
samplerfunc="const(math.log(2), math.log(150), {})".format(resolution["M_1"]),
precode="M_1=math.exp(lnm1)",
probdist="three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)*M_1",
dphasevol="dlnm1",
parameter_name="M_1",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
# # Mass ratio
# test_pop.add_grid_variable(
# name="q",
# longname="Mass ratio",
# valuerange=["0.1/M_1", 1],
# samplerfunc="const(0.1/M_1, 1, {})".format(resolution['q']),
# probdist="flatsections(q, [{'min': 0.1/M_1, 'max': 1.0, 'height': 1}])",
# dphasevol="dq",
# precode="M_2 = q * M_1",
# parameter_name="M_2",
# condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
# )
# #
# test_pop.add_grid_variable(
# name="log10per", # in days
# longname="log10(Orbital_Period)",
# valuerange=[0.15, 5.5],
# samplerfunc="const(0.15, 5.5, {})".format(resolution["per"]),
# precode="""orbital_period = 10** log10per
# sep = calc_sep_from_period(M_1, M_2, orbital_period)
# sep_min = calc_sep_from_period(M_1, M_2, 10**0.15)
# sep_max = calc_sep_from_period(M_1, M_2, 10**5.5)""",
# probdist="sana12(M_1, M_2, sep, orbital_period, sep_min, sep_max, math.log10(10**0.15), math.log10(10**5.5), -0.55)",
# parameter_name="orbital_period",
# dphasevol="dlog10per",
# )
Added grid variable: {
"name": "lnm1",
"parameter_name": "M_1",
"longname": "Primary mass",
"valuerange": [
2,
150
],
"samplerfunc": "const(math.log(2), math.log(150), 20)",
"precode": "M_1=math.exp(lnm1)",
"postcode": null,
"probdist": "three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)*M_1",
"dphasevol": "dlnm1",
"condition": "",
"gridtype": "centred",
"branchpoint": 0,
"branchcode": null,
"topcode": null,
"bottomcode": null,
"grid_variable_number": 0
}
Setting logging and handling the output¶
On default, binary_c will not output anything (except for ‘SINGLE STAR LIFETIME’). It is up to us to determine what will be printed. We can either do that by hardcoding the print statements into binary_c
(see documentation binary_c). Or, we can use the custom logging functionality of binarycpython (see notebook notebook_custom_logging.ipynb
), which is faster to set up and requires no recompilation of binary_c, but is somewhat more limited in its functionality.
After configuring what will be printed, we need to make a function to parse the output. This can be done by setting the parse_function parameter in the population object (see also notebook notebook_individual_systems.ipynb
).
In the code below we will set up both the custom logging, and a parse function to handle that output
[7]:
# Create custom logging statement: in this case we will log when the star turns into a compact object, and then terminate the evolution.
custom_logging_statement = """
if(stardata->star[0].stellar_type >= 13)
{
if (stardata->model.time < stardata->model.max_evolution_time)
{
Printf("EXAMPLE_COMPACT_OBJECT %30.12e %g %g %g %d\\n",
//
stardata->model.time, // 1
stardata->star[0].mass, // 2
stardata->common.zero_age.mass[0], // 3
stardata->model.probability, // 4
stardata->star[0].stellar_type // 5
);
};
/* Kill the simulation to save time */
stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
};
"""
example_pop.set(
C_logging_code=custom_logging_statement
)
adding: C_logging_code=
if(stardata->star[0].stellar_type >= 13)
{
if (stardata->model.time < stardata->model.max_evolution_time)
{
Printf("EXAMPLE_COMPACT_OBJECT %30.12e %g %g %g %d\n",
//
stardata->model.time, // 1
stardata->star[0].mass, // 2
stardata->common.zero_age.mass[0], // 3
stardata->model.probability, // 4
stardata->star[0].stellar_type // 5
);
};
/* Kill the simulation to save time */
stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
};
to grid_options
The parse function must now catch lines that start with “EXAMPLE_COMPACT_OBJECT”, and write that line to a file
[8]:
def parse_function(self, output):
"""
Example parse function
"""
# get info from the population instance
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)
seperator = " "
# Create filename
outfilename = os.path.join(data_dir, base_filename)
parameters = ["time", "mass", "zams_mass", "probability", "stellar_type"]
# Go over the output.
for line in output.splitlines():
headerline = line.split()[0]
# CHeck the header and act accordingly
if headerline == "EXAMPLE_COMPACT_OBJECT":
values = line.split()[1:]
print(line)
if not len(parameters) == len(values):
print("Number of column names isnt equal to number of columns")
raise ValueError
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")
# Add the parsing function
example_pop.set(
parse_function=parse_function,
)
adding: parse_function=<function parse_function at 0x1528ac7290d0> to grid_options
Evolving the grid¶
Now that we configured all the main parts of the population object, we can actually run the population! Doing this is straightforward: example_pop.evolve()
This will start up the processing of all the systems. We can control how many cores are used by settings num_cores
. By setting the verbosity
of the population object to a higher value we can get a lot of verbose information about the run, but for now we will set it to 0.
There are many grid_options that can lead to different behaviour of the evolution of the grid. Please do have a look at those: grid options docs, and try
[9]:
# change verbosity
example_pop.set(verbosity=0)
## Executing a population
## This uses the values generated by the grid_variables
analytics = example_pop.evolve() # TODO: update this function call
adding: verbosity=0 to grid_options
Doing dry run to calculate total starcount and probability
Generating grid code
Grid has handled 20 stars with a total probability of 0.0444029
**************************************
* Total starcount for this run is 20 *
* Total probability is 0.0444029 *
**************************************
Generating grid code
EXAMPLE_COMPACT_OBJECT 4.139293101586e+01 1.29427 8.13626 0.00202467 13
EXAMPLE_COMPACT_OBJECT 2.802986496151e+01 1.33699 10.0967 0.00152924 13
EXAMPLE_COMPACT_OBJECT 1.963621764679e+01 1.39754 12.5294 0.00115504 13
EXAMPLE_COMPACT_OBJECT 1.427601421985e+01 1.47745 15.5483 0.000872405 13
EXAMPLE_COMPACT_OBJECT 1.094409257247e+01 1.57571 19.2947 0.00065893 13
EXAMPLE_COMPACT_OBJECT 9.181971798545e+00 1.68748 23.9436 0.000497691 13
EXAMPLE_COMPACT_OBJECT 7.905335716621e+00 1.77287 29.7128 0.000375908 13
EXAMPLE_COMPACT_OBJECT 7.451192744924e+00 1.81495 36.872 0.000283924 13
EXAMPLE_COMPACT_OBJECT 7.396133472739e+00 1.82088 45.7561 0.000214449 13
EXAMPLE_COMPACT_OBJECT 7.396675941641e+00 1.82123 56.7809 0.000161974 13
EXAMPLE_COMPACT_OBJECT 7.404641347602e+00 1.82074 70.4621 0.000122339 13
EXAMPLE_COMPACT_OBJECT 7.444217227690e+00 1.81636 87.4397 9.2403e-05 13
EXAMPLE_COMPACT_OBJECT 7.453317880232e+00 1.81536 108.508 6.97923e-05 13
EXAMPLE_COMPACT_OBJECT 7.450828476487e+00 1.81563 134.653 5.27143e-05 13
**********************************************************
* Population-50fb66cc659c46c8bbc29fe0c8651c2f finished! *
* The total probability is 0.0444029. *
* It took a total of 3.30s to run 20 systems on 2 cores *
* = 6.60s of CPU time. *
* Maximum memory use 433.070 MB *
**********************************************************
There were no errors found in this run.
After the run is complete, some technical report on the run is returned. I stored that in analytics
. As we can see below, this dictionary is like a status report of the evolution. Useful for e.g. debugging.
[10]:
print(analytics)
{'population_name': '50fb66cc659c46c8bbc29fe0c8651c2f', 'evolution_type': 'grid', 'failed_count': 0, 'failed_prob': 0, 'failed_systems_error_codes': [], 'errors_exceeded': False, 'errors_found': False, 'total_probability': 0.04440288843805411, 'total_count': 20, 'start_timestamp': 1635760967.3245144, 'end_timestamp': 1635760970.6249793, 'total_mass_run': 684.2544031669784, 'total_probability_weighted_mass_run': 0.28134439269236855, 'zero_prob_stars_skipped': 0}
Noteworthy functionality¶
Some extra features that are available from via the population object are: - write_binary_c_calls_to_file: Function to write the calls that would be passed to binary_c to a file
[11]:
help(example_pop.write_binary_c_calls_to_file)
Help on method write_binary_c_calls_to_file in module binarycpython.utils.grid:
write_binary_c_calls_to_file(output_dir: Optional[str] = None, output_filename: Optional[str] = None, include_defaults: bool = False) -> None method of binarycpython.utils.grid.Population instance
Function that loops over the grid code and writes the generated parameters to a file.
In the form of a command line call
Only useful when you have a variable grid as system_generator. MC wouldn't be that useful
Also, make sure that in this export there are the basic parameters
like m1,m2,sep, orb-per, ecc, probability etc.
On default this will write to the datadir, if it exists
Tasks:
- TODO: test this function
- TODO: make sure the binary_c_python .. output file has a unique name
Args:
output_dir: (optional, default = None) directory where to write the file to. If custom_options['data_dir'] is present, then that one will be used first, and then the output_dir
output_filename: (optional, default = None) filename of the output. If not set it will be called "binary_c_calls.txt"
include_defaults: (optional, default = None) whether to include the defaults of binary_c in the lines that are written. Beware that this will result in very long lines, and it might be better to just export the binary_c defaults and keep them in a separate file.
Returns:
filename: filename that was used to write the calls to
[12]:
example_pop.set(verbosity=1)
calls_filename = example_pop.write_binary_c_calls_to_file()
print(calls_filename)
with open(calls_filename, 'r') as f:
print('\n'.join(f.read().splitlines()[:4]))
Generating grid code
Generating grid code
Saving grid code to grid_options
Writing grid code to /tmp/binary_c_python-izzard/notebooks/notebook_population/binary_c_grid_50fb66cc659c46c8bbc29fe0c8651c2f.py [dry_run = False]
Symlinked grid code to /tmp/binary_c_python-izzard/notebooks/notebook_population/binary_c_grid-latest2
Loading grid code function from /tmp/binary_c_python-izzard/notebooks/notebook_population/binary_c_grid_50fb66cc659c46c8bbc29fe0c8651c2f.py
Grid code loaded
Writing binary_c calls to /tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result/binary_c_calls.txt
Grid has handled 20 stars with a total probability of 0.0444029
/tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result/binary_c_calls.txt
binary_c M_1 2.227955577093495 eccentricity 0.02 max_evolution_time 15000 orbital_period 45000000080 phasevol 0.21587440567681548 probability 0.010905083645619543
binary_c M_1 2.7647737053496777 eccentricity 0.02 max_evolution_time 15000 orbital_period 45000000080 phasevol 0.2158744056768156 probability 0.00823663875514986
binary_c M_1 3.430936289925951 eccentricity 0.02 max_evolution_time 15000 orbital_period 45000000080 phasevol 0.21587440567681537 probability 0.0062211552141636295
binary_c M_1 4.2576084265970895 eccentricity 0.02 max_evolution_time 15000 orbital_period 45000000080 phasevol 0.2158744056768156 probability 0.004698855121516281
Full examples of population scripts¶
Below is a full setup for a population of single stars
[13]:
def parse_function(self, output):
"""
Example parsing function
"""
# extract info from the population instance
# 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)
#
seperator = " "
# Create filename
outfilename = os.path.join(data_dir, base_filename)
# The header columns matching this
parameters = ["time", "mass", "zams_mass", "probability", "radius", "stellar_type"]
# Go over the output.
for el in output.splitlines():
headerline = el.split()[0]
# CHeck the header and act accordingly
if headerline == "MY_STELLAR_DATA":
values = el.split()[1:]
if not len(parameters) == len(values):
print("Number of column names isnt equal to number of columns")
raise ValueError
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")
# Create population object
example_pop = Population()
# If you want verbosity, set this before other things
example_pop.set(verbosity=0)
# Setting values can be done via .set(<parameter_name>=<value>)
example_pop.set(
# binary_c physics options
M_1=10, # bse_options
separation=0, # bse_options
orbital_period=45000000080, # bse_options
max_evolution_time=15000, # bse_options
eccentricity=0.02, # bse_options
# grid_options
num_cores=2,
tmp_dir=TMP_DIR,
# Custom options: the data directory and the output filename
data_dir=os.path.join(
TMP_DIR, "example_python_population_result"
), # custom_options
base_filename="example_pop.dat", # custom_options
)
# Creating a parsing function
example_pop.set(
parse_function=parse_function, # Setting the parse function thats used in the evolve_population
)
### Custom logging
# Log the moment when the star turns into neutron
example_pop.set(
C_logging_code="""
if(stardata->star[0].stellar_type >= 13)
{
if (stardata->model.time < stardata->model.max_evolution_time)
{
Printf("MY_STELLAR_DATA %30.12e %g %g %g %g %d\\n",
//
stardata->model.time, // 1
stardata->star[0].mass, // 2
stardata->common.zero_age.mass[0], // 4
stardata->model.probability, // 5
stardata->star[0].radius, // 6
stardata->star[0].stellar_type // 7
);
};
/* Kill the simulation to save time */
stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
};
"""
)
# Add grid variables
resolution = {"M_1": 20}
# Mass
example_pop.add_grid_variable(
name="lnm1",
longname="Primary mass",
valuerange=[2, 150],
samplerfunc="const(math.log(2), math.log(150), {})".format(resolution["M_1"]),
precode="M_1=math.exp(lnm1)",
probdist="three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)*M_1",
dphasevol="dlnm1",
parameter_name="M_1",
condition="",
)
# Exporting of all the settings can be done with .export_all_info()
example_pop.export_all_info()
# remove the result file if it exists
if os.path.isfile(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat")):
os.remove(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat"))
# Evolve the population
example_pop.evolve()
#
with open(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat"), 'r') as f:
output = f.read()
print("\n")
print(output)
<<<< Warning: Key does not match previously known parameter: adding: data_dir=/tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result to custom_options >>>>
<<<< Warning: Key does not match previously known parameter: adding: base_filename=example_pop.dat to custom_options >>>>
Doing dry run to calculate total starcount and probability
Generating grid code
Grid has handled 20 stars with a total probability of 0.0444029
**************************************
* Total starcount for this run is 20 *
* Total probability is 0.0444029 *
**************************************
Generating grid code
**********************************************************
* Population-05e51ba114934b37bab48f1db40b7333 finished! *
* The total probability is 0.0444029. *
* It took a total of 3.46s to run 20 systems on 2 cores *
* = 6.93s of CPU time. *
* Maximum memory use 437.047 MB *
**********************************************************
There were no errors found in this run.
time mass zams_mass probability radius stellar_type
4.139293101586e+01 1.29427 8.13626 0.00202467 1.72498e-05 13
2.802986496151e+01 1.33699 10.0967 0.00152924 1.72498e-05 13
1.963621764679e+01 1.39754 12.5294 0.00115504 1.72498e-05 13
1.427601421985e+01 1.47745 15.5483 0.000872405 1.72498e-05 13
1.094409257247e+01 1.57571 19.2947 0.00065893 1.72498e-05 13
9.181971798545e+00 1.68748 23.9436 0.000497691 1.72498e-05 13
7.905335716621e+00 1.77287 29.7128 0.000375908 1.72498e-05 13
7.451192744924e+00 1.81495 36.872 0.000283924 1.72498e-05 13
7.396133472739e+00 1.82088 45.7561 0.000214449 1.72498e-05 13
7.396675941641e+00 1.82123 56.7809 0.000161974 1.72498e-05 13
7.404641347602e+00 1.82074 70.4621 0.000122339 1.72498e-05 13
7.444217227690e+00 1.81636 87.4397 9.2403e-05 1.72498e-05 13
7.453317880232e+00 1.81536 108.508 6.97923e-05 1.72498e-05 13
7.450828476487e+00 1.81563 134.653 5.27143e-05 1.72498e-05 13
We can also set up a population that samples biinary systems, by adding extra grid variables. Below is an example of a full script that runs a binary population and registers when a double compact object is formed. The logging is rather compact and should be expanded top be more useful
[14]:
def parse_function(self, output):
"""
Example parsing function
"""
# extract info from the population instance
# 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)
#
seperator = " "
# Create filename
outfilename = os.path.join(data_dir, base_filename)
# The header columns matching this
parameters = [
"time",
"mass_1", "zams_mass_1", "mass_2", "zams_mass_2",
"stellar_type_1", "prev_stellar_type_1", "stellar_type_2", "prev_stellar_type_2",
"metallicity", "probability"
]
# Go over the output.
for el in output.splitlines():
headerline = el.split()[0]
# CHeck the header and act accordingly
if headerline == "EXAMPLE_DCO":
values = el.split()[1:]
if not len(parameters) == len(values):
print("Number of column names isnt equal to number of columns")
raise ValueError
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")
# Create population object
example_pop = Population()
# If you want verbosity, set this before other things
example_pop.set(verbosity=0)
# Setting values can be done via .set(<parameter_name>=<value>)
example_pop.set(
# binary_c physics options
M_1=10, # bse_options
separation=0, # bse_options
orbital_period=45000000080, # bse_options
max_evolution_time=15000, # bse_options
eccentricity=0.02, # bse_options
# grid_options
num_cores=2, # grid_options
tmp_dir=TMP_DIR,
# Custom options: the data directory and the output filename
data_dir=os.path.join(
TMP_DIR, "example_python_population_result"
), # custom_options
base_filename="example_pop.dat", # custom_options
)
# Creating a parsing function
example_pop.set(
parse_function=parse_function, # Setting the parse function thats used in the evolve_population
)
### Custom logging
# Log the moment when the star turns into neutron
example_pop.set(
C_logging_code="""
// logger to find gravitational wave progenitors
if(stardata->star[0].stellar_type>=NS && stardata->star[1].stellar_type>=NS)
{
if (stardata->model.time < stardata->model.max_evolution_time)
{
Printf("EXAMPLE_DCO %30.12e " // 1
"%g %g %g %g " // 2-5
"%d %d %d %d " // 6-9
"%g %g\\n", // 10-11
stardata->model.time, // 1
stardata->star[0].mass, //2
stardata->common.zero_age.mass[0], //3
stardata->star[1].mass, //4
stardata->common.zero_age.mass[1], //5
stardata->star[0].stellar_type, //6
stardata->previous_stardata->star[0].stellar_type, //7
stardata->star[1].stellar_type, //8
stardata->previous_stardata->star[1].stellar_type, //9
// model stuff
stardata->common.metallicity, //10
stardata->model.probability //11
);
}
/* Kill the simulation to safe time */
stardata->model.max_evolution_time = stardata->model.time - stardata->model.dtm;
}
"""
)
# Add grid variables
resolution = {"M_1": 3, "q": 3, "per": 3}
# Mass
example_pop.add_grid_variable(
name="lnm1",
longname="Primary mass",
valuerange=[2, 150],
samplerfunc="const(math.log(2), math.log(150), {})".format(resolution["M_1"]),
precode="M_1=math.exp(lnm1)",
probdist="three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)*M_1",
dphasevol="dlnm1",
parameter_name="M_1",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
# Mass ratio
example_pop.add_grid_variable(
name="q",
longname="Mass ratio",
valuerange=["0.1/M_1", 1],
samplerfunc="const(0.1/M_1, 1, {})".format(resolution['q']),
probdist="flatsections(q, [{'min': 0.1/M_1, 'max': 1.0, 'height': 1}])",
dphasevol="dq",
precode="M_2 = q * M_1",
parameter_name="M_2",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
#
example_pop.add_grid_variable(
name="log10per", # in days
longname="log10(Orbital_Period)",
valuerange=[0.15, 5.5],
samplerfunc="const(0.15, 5.5, {})".format(resolution["per"]),
precode="""orbital_period = 10** log10per
sep = calc_sep_from_period(M_1, M_2, orbital_period)
sep_min = calc_sep_from_period(M_1, M_2, 10**0.15)
sep_max = calc_sep_from_period(M_1, M_2, 10**5.5)""",
probdist="sana12(M_1, M_2, sep, orbital_period, sep_min, sep_max, math.log10(10**0.15), math.log10(10**5.5), -0.55)",
parameter_name="orbital_period",
dphasevol="dlog10per",
)
# Exporting of all the settings can be done with .export_all_info()
example_pop.export_all_info()
# remove the result file if it exists
if os.path.isfile(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat")):
os.remove(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat"))
# Evolve the population
example_pop.evolve()
#
with open(os.path.join(TMP_DIR, "example_python_population_result", "example_pop.dat"), 'r') as f:
output = f.read()
print("\n")
print(output)
<<<< Warning: Key does not match previously known parameter: adding: data_dir=/tmp/binary_c_python-izzard/notebooks/notebook_population/example_python_population_result to custom_options >>>>
<<<< Warning: Key does not match previously known parameter: adding: base_filename=example_pop.dat to custom_options >>>>
Doing dry run to calculate total starcount and probability
Generating grid code
Grid has handled 27 stars with a total probability of 0.0248684
**************************************
* Total starcount for this run is 27 *
* Total probability is 0.0248684 *
**************************************
Generating grid code
**********************************************************
* Population-8bc1eafea1c34b05894c1618639d8c37 finished! *
* The total probability is 0.0248684. *
* It took a total of 16.10s to run 27 systems on 2 cores *
* = 32.20s of CPU time. *
* Maximum memory use 437.695 MB *
**********************************************************
There were no errors found in this run.
time mass_1 zams_mass_1 mass_2 zams_mass_2 stellar_type_1 prev_stellar_type_1 stellar_type_2 prev_stellar_type_2 metallicity probability
1.219029061236e+01 1.60007 17.3205 0 2.97008 13 5 15 15 0.02 0.000498487
1.935920339886e+01 1.29448 17.3205 0 8.71025 13 13 15 2 0.02 0.000498487
2.123794969278e+01 1.30902 17.3205 1.58518 8.71025 13 13 13 5 0.02 0.000287968
3.579099761269e+01 1.52414 17.3205 1.30642 8.71025 13 13 13 5 0.02 0.000220016
1.674063083432e+01 1.29457 17.3205 0 14.4504 13 13 15 2 0.02 0.000498487
1.548740826516e+01 1.52415 17.3205 1.45407 14.4504 13 13 13 5 0.02 0.000220016
1.779197348711e+01 1.3228 17.3205 1.71196 14.4504 13 13 13 8 0.02 0.000287968
1.367065497322e+01 1.66003 73.0434 1.79487 12.2572 13 13 13 8 0.02 7.67586e-05
1.772169325355e+01 1.81957 73.0434 1.46573 12.2572 13 13 13 5 0.02 4.43422e-05
2.021960493499e+01 1.82061 73.0434 1.39205 12.2572 13 13 13 5 0.02 3.38788e-05
9.012246630357e+00 1.81529 73.0434 0 36.5717 13 8 15 15 0.02 7.67586e-05
7.462779538274e+00 1.82255 73.0434 1.81499 36.5717 13 13 13 8 0.02 3.38788e-05
1.030499912298e+01 1.80592 73.0434 1.81066 36.5717 13 13 13 8 0.02 4.43422e-05
9.823059079115e+00 2.43711 73.0434 1.81689 60.8862 14 14 13 8 0.02 7.67586e-05
7.394722435913e+00 1.79092 73.0434 1.79092 60.8862 13 8 13 8 0.02 4.43422e-05
7.396288708628e+00 1.8216 73.0434 1.8216 60.8862 13 8 13 8 0.02 3.38788e-05