Newer
Older
#!/usr/bin/python3
David Hendriks
committed
from binarycpython.utils.custom_logging_functions import (
autogen_C_logging_code,
binary_c_log_code,
create_and_load_logging_function,
)
David Hendriks
committed
from binarycpython.utils.functions import temp_dir
David Hendriks
committed
import tempfile
############################################################
David Hendriks
committed
# Test script for the api functions
David Hendriks
committed
# TODO: Add asserts to these functions and use em as
David Hendriks
committed
# unittests
############################################################
David Hendriks
committed
# Evolution functionality
David Hendriks
committed
David Hendriks
committed
def test_run_system():
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,
)
David Hendriks
committed
output = binary_c_python_api.run_system(argstring=argstring)
David Hendriks
committed
print("\n\nBinary_c output:")
David Hendriks
committed
David Hendriks
committed
def test_run_system_with_log():
David Hendriks
committed
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
David Hendriks
committed
log_filename = temp_dir() + "/test_log.txt"
api_log_filename_prefix = temp_dir() + "/test_log"
David Hendriks
committed
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} log_filename {7:s} api_log_filename_prefix {8:s}".format(
m1,
m2,
separation,
orbital_period,
eccentricity,
metallicity,
max_evolution_time,
log_filename,
api_log_filename_prefix,
)
David Hendriks
committed
output = binary_c_python_api.run_system(argstring=argstring, write_logfile=1)
David Hendriks
committed
print("\n\nBinary_c output:")
print(output)
David Hendriks
committed
David Hendriks
committed
def test_run_system_with_custom_logging():
David Hendriks
committed
"""
"""
# 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"],}
)
# Generate entire shared lib code around logging lines
custom_logging_code = binary_c_log_code(logging_line)
# Load memory adress
David Hendriks
committed
func_memaddr, shared_lib_filename = create_and_load_logging_function(
custom_logging_code
)
David Hendriks
committed
David Hendriks
committed
# Some values
David Hendriks
committed
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
David Hendriks
committed
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(
David Hendriks
committed
m1,
m2,
separation,
orbital_period,
eccentricity,
metallicity,
max_evolution_time,
)
David Hendriks
committed
print(func_memaddr)
David Hendriks
committed
out = binary_c_python_api.run_system(
argstring, custom_logging_func_memaddr=func_memaddr
)
David Hendriks
committed
print(out)
David Hendriks
committed
# Testing other utility functions
def test_return_help():
out = binary_c_python_api.return_help("M_1")
print(out)
def test_return_arglines():
out = binary_c_python_api.return_arglines()
print(out)
David Hendriks
committed
def test_return_help_all():
out = binary_c_python_api.return_help_all("M_1")
print(out)
David Hendriks
committed
David Hendriks
committed
def test_return_version_info():
out = binary_c_python_api.return_version_info()
print(out)
David Hendriks
committed
David Hendriks
committed
# Testing other functions
David Hendriks
committed
David Hendriks
committed
David Hendriks
committed
def test_return_store():
David Hendriks
committed
out = binary_c_python_api.return_store("")
David Hendriks
committed
print(out)
David Hendriks
committed
David Hendriks
committed
####
if __name__ == "__main__":
David Hendriks
committed
test_run_system()
David Hendriks
committed
David Hendriks
committed
test_run_system_with_log()
David Hendriks
committed
test_run_system_with_custom_logging()
David Hendriks
committed
David Hendriks
committed
test_return_help()
David Hendriks
committed
David Hendriks
committed
test_return_arglines()
David Hendriks
committed
David Hendriks
committed
test_return_help_all()
David Hendriks
committed
test_return_version_info()
David Hendriks
committed
David Hendriks
committed
test_return_store()