diff --git a/binary_c_api.c b/binary_c_api.c index 48e776882d344d9b2bc34710bcdae48e68b8b0a5..afd31aa6473fab49237764cce1211885802f42bd 100644 --- a/binary_c_api.c +++ b/binary_c_api.c @@ -124,3 +124,50 @@ int run_binary(char * argstring, return 0; } + +int return_arglines(char ** buffer, + size_t * nbytes) +{ + /* memory for N binary systems */ + struct libbinary_c_stardata_t *stardata; + struct libbinary_c_store_t * store = NULL; + + /* make new stardata */ + stardata = NULL; + char * empty_str = ""; + binary_c_new_system(&stardata, + NULL, + NULL, + &store, + &empty_str, + -1); + + /* disable logging */ + snprintf(stardata->preferences->log_filename, + STRING_LENGTH-1, + "%s", + "/dev/null"); + snprintf(stardata->preferences->api_log_filename_prefix, + STRING_LENGTH-1, + "%s", + "/dev/null"); + + /* output to strings */ + stardata->preferences->internal_buffering = 2; + stardata->preferences->internal_buffering_compression = 0; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + + /* List available arguments */ + binary_c_list_args(stardata); + + /* get buffer pointer */ + binary_c_buffer_info(stardata,buffer,nbytes); + + /* set raw_buffer_size = -1 to prevent it being freed */ + stardata->tmpstore->raw_buffer_size = -1; + + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE); + binary_c_free_store_contents(store); + return 0; +} \ No newline at end of file diff --git a/binary_c_api.h b/binary_c_api.h index ef9f2ccdbb9d5f28b9d822d12ec7a9da432af606..950f505aac9f6df0ba55c152c7fc80788f3d8211 100644 --- a/binary_c_api.h +++ b/binary_c_api.h @@ -9,6 +9,9 @@ int run_binary(char * argstring, char ** buffer, size_t * nbytes); +int return_arglines(char ** buffer, + size_t * nbytes); + /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 #define APIprint(...) APIprintf(__VA_ARGS__); diff --git a/binary_c_api_python.h b/binary_c_api_python.h index ef9f2ccdbb9d5f28b9d822d12ec7a9da432af606..950f505aac9f6df0ba55c152c7fc80788f3d8211 100644 --- a/binary_c_api_python.h +++ b/binary_c_api_python.h @@ -9,6 +9,9 @@ int run_binary(char * argstring, char ** buffer, size_t * nbytes); +int return_arglines(char ** buffer, + size_t * nbytes); + /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 #define APIprint(...) APIprintf(__VA_ARGS__); diff --git a/binary_c_python.c b/binary_c_python.c index 7e122d212c46b0c7a7aa85322556b8020ea0b20f..3496da885ad035b4f4e8b08f7b8d331bc2ed0353 100644 --- a/binary_c_python.c +++ b/binary_c_python.c @@ -35,7 +35,8 @@ static char new_binary_system_docstring[] = "Return an object containing a binary, ready for evolution"; static char function_prototype_docstring[] = "The prototype for a binary_c python function"; - +static char return_arglines_docstring[] = + "Return the default args for a binary_c system"; static struct libbinary_c_store_t *store = NULL; #ifdef __DEPRECATED @@ -44,6 +45,8 @@ static PyObject* binary_c_create_binary(PyObject *self, PyObject *args); static PyObject* binary_c_run_binary(PyObject *self, PyObject *args); static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args); static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args); +static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args); + /* @@ -59,6 +62,7 @@ static PyMethodDef module_methods[] = { {"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring}, {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring}, {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring}, + {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring}, {NULL, NULL, 0, NULL} }; @@ -186,3 +190,17 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) return ret; } } + +static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) +{ + /* Binary structures */ + char * buffer; + int nbytes; + int out MAYBE_UNUSED = return_arglines(&buffer, + &nbytes); + /* copy the buffer to a python string */ + PyObject * ret = Py_BuildValue("s", buffer); + free(buffer); + /* Return an object containing the arg list */ + return ret; +} \ No newline at end of file diff --git a/binary_c_python.h b/binary_c_python.h index d208a52b3a8a7a55f1513dfc58467bb1950bafae..87de6de73bb06afa2b38f84c658d3c5f1c95c37e 100644 --- a/binary_c_python.h +++ b/binary_c_python.h @@ -13,6 +13,9 @@ int run_binary (char * argstring, char ** outstring, int * nbytes); +int return_arglines(char ** buffer, + int * nbytes); + /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 #define APIprint(...) APIprintf(__VA_ARGS__); diff --git a/binary_c_python_api.c b/binary_c_python_api.c index 360a449996eaa0f6c04cbf4489ea2803c3e30335..4e0086d277c9614611a8306da3050a40c36cbbde 100644 --- a/binary_c_python_api.c +++ b/binary_c_python_api.c @@ -120,3 +120,50 @@ int run_binary(char * argstring, binary_c_free_store_contents(store); return 0; } + +int return_arglines(char ** buffer, + int * nbytes) +{ + /* memory for N binary systems */ + struct libbinary_c_stardata_t *stardata; + struct libbinary_c_store_t * store = NULL; + + /* make new stardata */ + stardata = NULL; + char * empty_str = ""; + binary_c_new_system(&stardata, + NULL, + NULL, + &store, + &empty_str, + -1); + + /* disable logging */ + snprintf(stardata->preferences->log_filename, + STRING_LENGTH-1, + "%s", + "/dev/null"); + snprintf(stardata->preferences->api_log_filename_prefix, + STRING_LENGTH-1, + "%s", + "/dev/null"); + + /* output to strings */ + stardata->preferences->internal_buffering = 2; + stardata->preferences->internal_buffering_compression = 0; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + + /* List available arguments */ + binary_c_list_args(stardata); + + /* get buffer pointer */ + binary_c_buffer_info(stardata,buffer,nbytes); + + /* set raw_buffer_size = -1 to prevent it being freed */ + stardata->tmpstore->raw_buffer_size = -1; + + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE); + binary_c_free_store_contents(store); + return 0; +} \ No newline at end of file diff --git a/binaryc_python_utils/defaults.py b/binaryc_python_utils/defaults.py index f2e6906cd3f7e375bc915aaac1c6097b32cd6274..aba0d28e9bb06819c8280885ae788180416a275d 100644 --- a/binaryc_python_utils/defaults.py +++ b/binaryc_python_utils/defaults.py @@ -1,3 +1,5 @@ +# WARNING DEPRECATED FROM 11-aug-2019 + # File containing physics_defaults physics_defaults = { diff --git a/binaryc_python_utils/functions.py b/binaryc_python_utils/functions.py index 2cf2ed3a3cc2d77985f670b68a9c4e2d9640fa32..ace2c62cbb8fca0b9d7178da1d0d2d809d1a2324 100644 --- a/binaryc_python_utils/functions.py +++ b/binaryc_python_utils/functions.py @@ -14,13 +14,30 @@ def create_arg_string(arg_dict): arg_string = arg_string.strip() return arg_string +def get_defaults(): + """ + Function that calls the binaryc get args function and cast it into a dictionary + All the values are strings + """ + default_output = binary_c.return_arglines() + default_dict = {} + + for default in default_output.split('\n'): + if not default in ['__ARG_BEGIN', '__ARG_END', '']: + key, value = default.split(' = ') + + # Filter out NULLS (not compiled anyway) + if not value=='NULL': + default_dict[key] = value + return default_dict + def run_system(**kwargs): """ Wrapper to run a system with settings """ - # Load args - physics_args = physics_defaults.copy() + # Load default args + physics_args = get_defaults() # For example # physics_args['M_1'] = 20 @@ -81,4 +98,5 @@ def parse_output(output, selected_header): for key in keys: final_values_dict[key].append(value_dict[key]) - return final_values_dict \ No newline at end of file + return final_values_dict + diff --git a/build/temp.linux-x86_64-3.6/binary_c_python.o b/build/temp.linux-x86_64-3.6/binary_c_python.o index af46f6afbc91b58d8074d7de4f69635428a0328d..7f9c6db721791a3a435987a618f423c5bfbb65b6 100644 Binary files a/build/temp.linux-x86_64-3.6/binary_c_python.o and b/build/temp.linux-x86_64-3.6/binary_c_python.o differ diff --git a/test_new_api_command.py b/test_new_api_command.py new file mode 100644 index 0000000000000000000000000000000000000000..45cb036ed3509bb04ee1070aa75e326199694ea6 --- /dev/null +++ b/test_new_api_command.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +import binary_c + +############################################################ +# Test script to run a binary using the binary_c Python +# module. +############################################################ + +def run_test_binary(): + 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 + buffer = "" + 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.run_binary(argstring) + + print ("\n\nBinary_c output:\n\n") + print (output) + + + +binary_star=binary_c.new_system() + +print(dir(binary_c)) +# print(binary_star) +run_test_binary() +ding = binary_c.return_arglines() +print(ding) \ No newline at end of file diff --git a/tests_david/testing_automatic_log_readout.py b/tests_david/testing_automatic_log_readout.py index 941fd595c2ef0db075267657ac277be2dcad98e3..5cb370e2bf2adb945b5dfca8e65d51fce3d1ecd2 100644 --- a/tests_david/testing_automatic_log_readout.py +++ b/tests_david/testing_automatic_log_readout.py @@ -5,7 +5,10 @@ from collections import defaultdict import numpy as np import pandas as pd +# sys.path.append('../') import binary_c + + from binaryc_python_utils.defaults import physics_defaults from binaryc_python_utils.functions import create_arg_string, parse_output, run_system @@ -28,10 +31,13 @@ print("The following keys are present in the results:\n{}".format(result.keys()) # Cast the data into a dataframe. df = pd.DataFrame.from_dict(result, dtype=np.float64) -sliced_df = df[df.t < 1000] # Cut off late parts of evolution -print(sliced_df["t"]) +print(df) + + +# sliced_df = df[df.t < 1000] # Cut off late parts of evolution +# print(sliced_df["t"]) -plt.plot(sliced_df['t'], sliced_df['radius']) -plt.xlabel('Time (Myr)') -plt.ylabel('Radius (Rsol)') -plt.show() \ No newline at end of file +# plt.plot(sliced_df['t'], sliced_df['radius']) +# plt.xlabel('Time (Myr)') +# plt.ylabel('Radius (Rsol)') +# plt.show() \ No newline at end of file