diff --git a/TODO.org b/TODO.org index 6f73f99db80ebc77c1578beb2601dbce49d2c579..ec732ae7569eb543c8db8bc1127eb79eee62c18d 100644 --- a/TODO.org +++ b/TODO.org @@ -143,7 +143,8 @@ That went very deep haha. alot of memory allocation stuff CLOSED: [2019-11-08 Fri 15:00] *** DONE Implement the autogeneration of the library CLOSED: [2019-11-08 Fri 15:48] -*** TODO Load all the things with the c-types +*** DONE Load all the things with the c-types + CLOSED: [2019-11-08 Fri 18:49] *** TODO Implement new function for run_binary_with_custom_logging *** TODO Make new c function run_binary_with_custom_logging *** TODO Put in some new tests in the python test api diff --git a/binary_c_python.c b/binary_c_python.c index ff67ec101ce59d6b16fa9e085d3b80edd98d260b..420fcedab10b0796f828c6b9184460311dbb76c3 100644 --- a/binary_c_python.c +++ b/binary_c_python.c @@ -71,10 +71,10 @@ static PyMethodDef module_methods[] = { #endif {"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring}, {"run_binary_with_log", binary_c_run_binary_with_log, METH_VARARGS, run_binary_with_logdocstring}, - // {"run_binary_custom_logging", binary_c_run_binary_custom_logging, METH_VARARGS, run_binary_custom_loggingdocstring}, + {"run_binary_custom_logging", binary_c_run_binary_custom_logging, METH_VARARGS, run_binary_custom_loggingdocstring}, {"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}, + {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring}, {NULL, NULL, 0, NULL} }; @@ -104,7 +104,6 @@ PyMODINIT_FUNC PyInit_binary_c(void) * NOT TESTED THOROUGHLY! */ - PyMODINIT_FUNC initbinary_c(void) { PyObject *m = Py_InitModule3("binary_c", module_methods, module_docstring); @@ -160,7 +159,6 @@ static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args) return ret; } - static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) { @@ -181,7 +179,6 @@ static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) } } - static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) { /* Parse the input tuple */ @@ -222,47 +219,47 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) } } -// static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args) -// { -// /* Parse the input tuple */ -// char *argstring; -// long int str_1; - -// if(!PyArg_ParseTuple(args, "sl", &argstring, &str_1)) -// { -// return NULL; -// } -// else -// { -// char * buffer; -// char * error_buffer; -// size_t nbytes; -// int out MAYBE_UNUSED = run_binary_custom_logging(argstring, -// str_1, -// &buffer, -// &error_buffer, -// &nbytes); -// /* copy the buffer to a python string */ -// PyObject * return_string = Py_BuildValue("s", buffer); -// PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); - -// if(error_buffer != NULL && strlen(error_buffer)>0) -// { -// fprintf(stderr, -// "Error in binary_c run : %s\n", -// error_buffer); -// } +static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args) +{ + /* Parse the input tuple */ + char *argstring; + long int str_1; + + if(!PyArg_ParseTuple(args, "sl", &argstring, &str_1)) + { + return NULL; + } + else + { + char * buffer; + char * error_buffer; + size_t nbytes; + int out MAYBE_UNUSED = run_binary_custom_logging(argstring, + str_1, + &buffer, + &error_buffer, + &nbytes); + /* copy the buffer to a python string */ + PyObject * return_string = Py_BuildValue("s", buffer); + PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); + + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error in binary_c run : %s\n", + error_buffer); + } -// Safe_free(buffer); -// Safe_free(error_buffer); + Safe_free(buffer); + Safe_free(error_buffer); -// /* -// * TODO -// * return the return_error_string as well! -// */ -// return return_string; -// } -// } + /* + * TODO + * return the return_error_string as well! + */ + return return_string; + } +} static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args) { @@ -305,32 +302,32 @@ static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args) } } -// static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) -// { -// char * buffer; -// char * error_buffer; -// size_t nbytes; -// int out MAYBE_UNUSED = return_arglines(&buffer, -// &error_buffer, -// &nbytes); - -// /* copy the buffer to a python string */ -// PyObject * return_string = Py_BuildValue("s", buffer); -// PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); - -// if(error_buffer != NULL && strlen(error_buffer)>0) -// { -// fprintf(stderr, -// "Error in binary_c run : %s\n", -// error_buffer); -// } +static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) +{ + char * buffer; + char * error_buffer; + size_t nbytes; + int out MAYBE_UNUSED = return_arglines(&buffer, + &error_buffer, + &nbytes); + + /* copy the buffer to a python string */ + PyObject * return_string = Py_BuildValue("s", buffer); + PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); + + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error in binary_c run : %s\n", + error_buffer); + } -// Safe_free(buffer); -// Safe_free(error_buffer); - -// /* -// * TODO -// * return the return_error_string as well! -// */ -// return return_string; -// } \ No newline at end of file + Safe_free(buffer); + Safe_free(error_buffer); + + /* + * TODO + * return the return_error_string as well! + */ + return return_string; +} \ No newline at end of file diff --git a/binary_c_python.h b/binary_c_python.h index 7643dcfccf58b8dc94d0f8d52d3cb0cc2fd69b32..9b8c632e42937b33bfcc3dd23161e0df80ef119d 100644 --- a/binary_c_python.h +++ b/binary_c_python.h @@ -19,15 +19,15 @@ int run_binary_with_log (char * argstring, char ** const errorstring, size_t * const nbytes); -// int run_binary_custom_logging(char * argstring, -// long int str_1, -// char ** const buffer, -// char ** const error_buffer, -// size_t * const nbytes); +int run_binary_custom_logging(char * argstring, + long int str_1, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes); -// int return_arglines(char ** const outstring, -// char ** const errorstring, -// size_t * const nbytes); +int return_arglines(char ** const outstring, + char ** const errorstring, + size_t * const nbytes); /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 diff --git a/binary_c_python_api.c b/binary_c_python_api.c index 00ddc349d237a01f45049c834f9443d54ac9899b..ab7d0c6badd16d8892609ee34a1e0307799025a2 100644 --- a/binary_c_python_api.c +++ b/binary_c_python_api.c @@ -35,48 +35,16 @@ * I have tested this with gcc 4.7.2 (Ubuntu 12.10) only. */ - // #define _CAPTURE #ifdef _CAPTURE static void show_stdout(void); static void capture_stdout(void); #endif - /* global variables */ int out_pipe[2]; int stdoutwas; -int main(int argc, - char * argv[]) -{ - char * argstring = Malloc(sizeof(char) * (size_t)STRING_LENGTH); - snprintf(argstring, - STRING_LENGTH, - "binary_c M_1 %g M_2 %g separation %g orbital_period %g metallicity %g max_evolution_time %g\n", - 20.0, - 15.0, - 0.0, - 3.0, - 0.02, - 15000.0); - - char * buffer; - char * error_buffer; - size_t nbytes; - int out = run_binary(argstring, - &buffer, - &error_buffer, - &nbytes); - - printf("output (binary_c returned %d)\n%s\n",out,buffer); - - free(buffer); - - return out; -} - - int run_binary(char * argstring, char ** const buffer, char ** const error_buffer, @@ -128,109 +96,109 @@ int run_binary(char * argstring, return 0; } -// int run_binary_custom_logging(char * argstring, -// long int str_1, -// char ** const buffer, -// char ** const error_buffer, -// size_t * const nbytes) -// { -// /* memory for N binary systems */ -// struct libbinary_c_store_t * store = NULL; +int run_binary_custom_logging(char * argstring, + long int str_1, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes) +{ + /* memory for N binary systems */ + struct libbinary_c_stardata_t *stardata; + struct libbinary_c_store_t * store = NULL; -// /* make new stardata */ -// stardata = NULL; -// binary_c_new_system(&stardata, -// NULL, -// NULL, -// &store, -// &argstring, -// -1); + /* make new stardata */ + stardata = NULL; + binary_c_new_system(&stardata, + NULL, + NULL, + &store, + &argstring, + -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 = INTERNAL_BUFFERING_STORE; -// stardata->preferences->batchmode = BATCHMODE_LIBRARY; -// stardata->preferences->custom_output_function = 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 = INTERNAL_BUFFERING_STORE; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + stardata->preferences->custom_output_function = str_1; -// /* do binary evolution */ -// binary_c_evolve_for_dt(stardata, -// stardata->model.max_evolution_time); + /* do binary evolution */ + binary_c_evolve_for_dt(stardata, + stardata->model.max_evolution_time); -// /* get buffer pointer */ -// binary_c_buffer_info(stardata,buffer,nbytes); + /* get buffer pointer */ + binary_c_buffer_info(stardata,buffer,nbytes); -// /* get error buffer pointer */ -// binary_c_error_buffer(stardata,error_buffer); + /* get error buffer pointer */ + binary_c_error_buffer(stardata,error_buffer); -// /* set raw_buffer_size = -1 to prevent it being freed */ -// stardata->tmpstore->raw_buffer_size = -1; + /* 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,FALSE); -// binary_c_free_store_contents(store); + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); + binary_c_free_store_contents(store); -// return 0; -// } - + return 0; +} -// int return_arglines(char ** const buffer, -// char ** const error_buffer, -// size_t * const nbytes) -// { -// /* memory for N binary systems */ -// struct libbinary_c_stardata_t *stardata; -// struct libbinary_c_store_t * store = NULL; +int return_arglines(char ** const buffer, + char ** const error_buffer, + size_t * const 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); + /* 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"); + /* 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 = INTERNAL_BUFFERING_STORE; -// stardata->preferences->batchmode = BATCHMODE_LIBRARY; + /* output to strings */ + stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; -// /* List available arguments */ -// binary_c_list_args(stardata); + /* List available arguments */ + binary_c_list_args(stardata); -// /* get buffer pointer */ -// binary_c_buffer_info(stardata,buffer,nbytes); + /* get buffer pointer */ + binary_c_buffer_info(stardata,buffer,nbytes); -// /* get error buffer pointer */ -// binary_c_error_buffer(stardata,error_buffer); + /* get error buffer pointer */ + binary_c_error_buffer(stardata,error_buffer); -// /* set raw_buffer_size = -1 to prevent it being freed */ -// stardata->tmpstore->raw_buffer_size = -1; + /* 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,FALSE); -// binary_c_free_store_contents(store); + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); + binary_c_free_store_contents(store); -// return 0; -// } + return 0; +} int run_binary_with_log(char * argstring, char ** const buffer, @@ -271,6 +239,4 @@ int run_binary_with_log(char * argstring, binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_store_contents(store); return 0; -} - - +} \ No newline at end of file diff --git a/setup.py b/setup.py index e5e1062595eb77ca48c98e97833dbbcb8522fea3..cbd77df3804dce2ca919f783908d14803dc9cdd1 100644 --- a/setup.py +++ b/setup.py @@ -11,6 +11,7 @@ binary_c_config = os.environ['BINARY_C']+'/binary_c-config' binary_c_incdirs = subprocess.run([binary_c_config,'incdirs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() binary_c_libdirs = subprocess.run([binary_c_config,'libdirs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() binary_c_cflags = subprocess.run([binary_c_config,'cflags'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() +# binary_c_cflags.remove('-fvisibility=hidden') binary_c_libs = subprocess.run([binary_c_config,'libs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() # create list of tuples of defined macros diff --git a/testing.py b/testing.py index f0f5a52e1ac478e0b74c60e847ee1c6d58a791aa..418e4486a9a25490f8bfee2924b3763f612c3b8d 100644 --- a/testing.py +++ b/testing.py @@ -1,7 +1,7 @@ import ctypes from binaryc_python_utils.custom_logging_functions import autogen_C_logging_code, binary_c_log_code, compile_shared_lib -# import binary_c +import binary_c # generate logging lines logging_line = autogen_C_logging_code( @@ -18,9 +18,27 @@ created_code = binary_c_log_code(logging_line) compile_shared_lib(created_code, sourcefile_name='custom_logging.c', outfile_name='libcustom_logging.so') # Loading library +dll1 = ctypes.CDLL('libgslcblas.so', mode=ctypes.RTLD_GLOBAL) +dll2 = ctypes.CDLL('libgsl.so', mode=ctypes.RTLD_GLOBAL) +dll3 = ctypes.CDLL('libbinary_c.so', mode=ctypes.RTLD_GLOBAL) libmean = ctypes.CDLL("libcustom_logging.so", mode=1) # loads the shared library # Get memory adress of function. mimicking a pointer mem = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value +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_custom_logging(argstring, mem) +# output = binary_c.run_binary(argstring) + +# print ("\n\nBinary_c output:\n\n") +print (output)