ExamplesΒΆ

The source code of the general examples.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python3
import os
import sys

from binarycpython import _binary_c_bindings

from binarycpython.utils.functions import example_parse_output
from binarycpython.utils.custom_logging_functions import (
    autogen_C_logging_code,
    binary_c_log_code,
    create_and_load_logging_function,
)
from binarycpython.utils.run_system_wrapper import run_system

"""
Very basic scripts to run a binary system and print the output.

Use these as inspiration/base.
"""


def run_example_binary():
    """
    Function to run a binary system. Very basic approach which directly adresses the run_system(..) python-c wrapper function.
    """

    m1 = 15.0  # Msun
    m2 = 14.0  # Msun
    separation = 0  # 0 = ignored, use period
    orbital_period = 4530.0  # days
    eccentricity = 0.0
    metallicity = 0.02
    max_evolution_time = 15000  # Myr. You need to include this argument.

    #
    argstring = "binary_c M_1 {m1} M_2 {m2} separation {separation} orbital_period {orbital_period} \
        eccentricity {eccentricity} metallicity {metallicity} \
        max_evolution_time {max_evolution_time}".format(
        m1=m1,
        m2=m2,
        separation=separation,
        orbital_period=orbital_period,
        eccentricity=eccentricity,
        metallicity=metallicity,
        max_evolution_time=max_evolution_time,
    )
    output = _binary_c_bindings.run_system(argstring)
    print(output)


run_example_binary()


def run_example_binary_with_run_system():
    """
    This function serves as an example on the function run_system and parse_output.
    There is more functionality with this method and several tasks are done behind the scene.

    Requires pandas, numpy to run.

    run_system: mostly just makes passing arguments to the function easier. It also loads all the necessary defaults in the background
    parse_output: Takes the raw output of binary_c and selects those lines that start with the given header.
    Note, if you dont use the custom_logging functionality binary_c should be configured to have output that starts with that given header

    The parsing of the output only works correctly if either all of the values are described inline like `mass=<number>' or none of them are.
    """

    import pandas as pd
    import numpy as np

    # Run system. all arguments can be given as optional arguments.
    output = run_system(M_1=10, M_2=20, separation=0, orbital_period=100000000000)

    # print(output)

    # Catch results that start with a given header. (Mind that binary_c has to be configured to print them if your not using a custom logging function)
    result_example_header_1 = example_parse_output(
        output, selected_header="example_header_1"
    )
    result_example_header_2 = example_parse_output(
        output, selected_header="example_header_2"
    )

    # print(result_example_header_1)

    #### Now do whatever you want with it:
    # Put it in numpy arrays
    # t_res = np.asarray(result_example_header['t'], dtype=np.float64, order='C')
    # m_res = np.asarray(result_example_header['mass'], dtype=np.float64, order='C')

    # Or put them into a pandas array

    # Cast the data into a dataframe.
    # This example automatically catches the column names because the binary_c output line is constructed as 'example_header_1 time=<number>..'
    print(result_example_header_1)
    df = pd.DataFrame.from_dict(result_example_header_1, dtype=np.float64)
    print(df)

    # This example has column headers which are numbered, but we can override that with custom headers.
    df2 = pd.DataFrame.from_dict(result_example_header_2, dtype=np.float64)
    df2.columns = ["time", "mass_1", "mass_2", "st1", "st2", "sep", "ecc"]
    print(df2)

    # print(df)
    # sliced_df = df[df.t < 1000] # Cut off late parts of evolution
    # print(sliced_df[["t","m1"]])

    # Some routine to plot.


# run_example_binary_with_run_system()


def run_example_custom_logging_autogenerated():
    """
    This is an example function for the autogeneration of logging codes that binary_c uses.
    """

    # generate logging lines
    logging_line = autogen_C_logging_code(
        {
            "MY_STELLAR_DATA": ["model.time", "star[0].mass"],
            "my_sss2": ["model.time", "star[1].mass"],
        }
    )

    # Generate code around logging lines
    custom_logging_code = binary_c_log_code(logging_line)

    # Generate library and get memaddr
    func_memaddr, shared_lib_filename = create_and_load_logging_function(
        custom_logging_code
    )

    #
    m1 = 15.0  # Msun
    m2 = 14.0  # Msun
    separation = 0  # 0 = ignored, use period
    orbital_period = 4530.0  # days
    eccentricity = 0.0
    metallicity = 0.02
    max_evolution_time = 15000
    argstring = "binary_c M_1 {0:g} M_2 {1:g} separation {2:g} orbital_period {3:g} eccentricity {4:g} metallicity {5:g} max_evolution_time {6:g}".format(
        m1,
        m2,
        separation,
        orbital_period,
        eccentricity,
        metallicity,
        max_evolution_time,
    )
    output = _binary_c_bindings.run_system(
        argstring, custom_logging_func_memaddr=func_memaddr
    )
    print(output)


# run_example_custom_logging_autogenerated()


def run_example_binary_with_custom_logging():
    """
    Function that will use a automatically generated piece of logging code. Compile it, load it
    into memory and run a binary system. See run_system on how several things are done in the background here.
    """

    import pandas as pd
    import numpy as np

    # generate logging lines. Here you can choose whatever you want to have logged, and with what header
    # this generates working print statements
    logging_line = autogen_C_logging_code(
        {
            "MY_STELLAR_DATA": ["model.time", "star[0].mass"],
        }
    )
    # OR
    # You can also decide to `write` your own logging_line, which allows you to write a more complex logging statement with conditionals.
    logging_line = 'Printf("MY_STELLAR_DATA time=%g mass=%g\\n", stardata->model.time, stardata->star[0].mass)'

    # Generate entire shared lib code around logging lines
    custom_logging_code = binary_c_log_code(logging_line)

    # Run system. all arguments can be given as optional arguments. the custom_logging_code is one of them and will be processed automatically.
    output = run_system(
        M_1=1,
        metallicity=0.002,
        M_2=0.1,
        separation=0,
        orbital_period=100000000000,
        custom_logging_code=custom_logging_code,
    )

    print(output)

    # Catch results that start with a given header. (Mind that binary_c has to be configured to print them if your not using a custom logging function)
    # DOESNT WORK YET if you have the line autogenerated.
    result_example_header = parse_output(output, "MY_STELLAR_DATA")

    # Cast the data into a dataframe.
    df = pd.DataFrame.from_dict(result_example_header, dtype=np.float64)

    # Do whatever you like with the dataframe.
    print(df)


# run_example_binary_with_custom_logging()


def run_example_binary_with_writing_logfile():
    """
    Same as above but when giving the log_filename argument the log filename will be written
    """

    import pandas as pd
    import numpy as np
    import tempfile

    # Run system. all arguments can be given as optional arguments.
    output = run_system(
        M_1=10,
        M_2=20,
        separation=0,
        orbital_period=100000000000,
        log_filename=tempfile.gettempdir() + "/test_log.txt",
    )


run_example_binary_with_writing_logfile()