Skip to content
Snippets Groups Projects
Commit b0c51327 authored by David Hendriks's avatar David Hendriks
Browse files

got a working code

parent 47376ac6
No related branches found
No related tags found
No related merge requests found
...@@ -143,7 +143,8 @@ That went very deep haha. alot of memory allocation stuff ...@@ -143,7 +143,8 @@ That went very deep haha. alot of memory allocation stuff
CLOSED: [2019-11-08 Fri 15:00] CLOSED: [2019-11-08 Fri 15:00]
*** DONE Implement the autogeneration of the library *** DONE Implement the autogeneration of the library
CLOSED: [2019-11-08 Fri 15:48] 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 Implement new function for run_binary_with_custom_logging
*** TODO Make new c function 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 *** TODO Put in some new tests in the python test api
......
...@@ -71,10 +71,10 @@ static PyMethodDef module_methods[] = { ...@@ -71,10 +71,10 @@ static PyMethodDef module_methods[] = {
#endif #endif
{"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring}, {"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_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}, {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring},
{"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_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} {NULL, NULL, 0, NULL}
}; };
...@@ -104,7 +104,6 @@ PyMODINIT_FUNC PyInit_binary_c(void) ...@@ -104,7 +104,6 @@ PyMODINIT_FUNC PyInit_binary_c(void)
* NOT TESTED THOROUGHLY! * NOT TESTED THOROUGHLY!
*/ */
PyMODINIT_FUNC initbinary_c(void) PyMODINIT_FUNC initbinary_c(void)
{ {
PyObject *m = Py_InitModule3("binary_c", module_methods, module_docstring); 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) ...@@ -160,7 +159,6 @@ static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args)
return ret; return ret;
} }
static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args)
{ {
...@@ -181,7 +179,6 @@ 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) static PyObject* binary_c_run_binary(PyObject *self, PyObject *args)
{ {
/* Parse the input tuple */ /* Parse the input tuple */
...@@ -222,47 +219,47 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) ...@@ -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) static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args)
// { {
// /* Parse the input tuple */ /* Parse the input tuple */
// char *argstring; char *argstring;
// long int str_1; long int str_1;
// if(!PyArg_ParseTuple(args, "sl", &argstring, &str_1)) if(!PyArg_ParseTuple(args, "sl", &argstring, &str_1))
// { {
// return NULL; return NULL;
// } }
// else else
// { {
// char * buffer; char * buffer;
// char * error_buffer; char * error_buffer;
// size_t nbytes; size_t nbytes;
// int out MAYBE_UNUSED = run_binary_custom_logging(argstring, int out MAYBE_UNUSED = run_binary_custom_logging(argstring,
// str_1, str_1,
// &buffer, &buffer,
// &error_buffer, &error_buffer,
// &nbytes); &nbytes);
// /* copy the buffer to a python string */ /* copy the buffer to a python string */
// PyObject * return_string = Py_BuildValue("s", buffer); PyObject * return_string = Py_BuildValue("s", buffer);
// PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);
// if(error_buffer != NULL && strlen(error_buffer)>0) if(error_buffer != NULL && strlen(error_buffer)>0)
// { {
// fprintf(stderr, fprintf(stderr,
// "Error in binary_c run : %s\n", "Error in binary_c run : %s\n",
// error_buffer); error_buffer);
// } }
// Safe_free(buffer); Safe_free(buffer);
// Safe_free(error_buffer); Safe_free(error_buffer);
// /* /*
// * TODO * TODO
// * return the return_error_string as well! * return the return_error_string as well!
// */ */
// return return_string; return return_string;
// } }
// } }
static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args) 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) ...@@ -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) static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
// { {
// char * buffer; char * buffer;
// char * error_buffer; char * error_buffer;
// size_t nbytes; size_t nbytes;
// int out MAYBE_UNUSED = return_arglines(&buffer, int out MAYBE_UNUSED = return_arglines(&buffer,
// &error_buffer, &error_buffer,
// &nbytes); &nbytes);
// /* copy the buffer to a python string */ /* copy the buffer to a python string */
// PyObject * return_string = Py_BuildValue("s", buffer); PyObject * return_string = Py_BuildValue("s", buffer);
// PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);
// if(error_buffer != NULL && strlen(error_buffer)>0) if(error_buffer != NULL && strlen(error_buffer)>0)
// { {
// fprintf(stderr, fprintf(stderr,
// "Error in binary_c run : %s\n", "Error in binary_c run : %s\n",
// error_buffer); error_buffer);
// } }
// Safe_free(buffer); Safe_free(buffer);
// Safe_free(error_buffer); Safe_free(error_buffer);
// /* /*
// * TODO * TODO
// * return the return_error_string as well! * return the return_error_string as well!
// */ */
// return return_string; return return_string;
// } }
\ No newline at end of file \ No newline at end of file
...@@ -19,15 +19,15 @@ int run_binary_with_log (char * argstring, ...@@ -19,15 +19,15 @@ int run_binary_with_log (char * argstring,
char ** const errorstring, char ** const errorstring,
size_t * const nbytes); size_t * const nbytes);
// int run_binary_custom_logging(char * argstring, int run_binary_custom_logging(char * argstring,
// long int str_1, long int str_1,
// char ** const buffer, char ** const buffer,
// char ** const error_buffer, char ** const error_buffer,
// size_t * const nbytes); size_t * const nbytes);
// int return_arglines(char ** const outstring, int return_arglines(char ** const outstring,
// char ** const errorstring, char ** const errorstring,
// size_t * const nbytes); size_t * const nbytes);
/* C macros */ /* C macros */
#define BINARY_C_APITEST_VERSION 0.1 #define BINARY_C_APITEST_VERSION 0.1
......
...@@ -35,48 +35,16 @@ ...@@ -35,48 +35,16 @@
* I have tested this with gcc 4.7.2 (Ubuntu 12.10) only. * I have tested this with gcc 4.7.2 (Ubuntu 12.10) only.
*/ */
// #define _CAPTURE // #define _CAPTURE
#ifdef _CAPTURE #ifdef _CAPTURE
static void show_stdout(void); static void show_stdout(void);
static void capture_stdout(void); static void capture_stdout(void);
#endif #endif
/* global variables */ /* global variables */
int out_pipe[2]; int out_pipe[2];
int stdoutwas; 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, int run_binary(char * argstring,
char ** const buffer, char ** const buffer,
char ** const error_buffer, char ** const error_buffer,
...@@ -128,109 +96,109 @@ int run_binary(char * argstring, ...@@ -128,109 +96,109 @@ int run_binary(char * argstring,
return 0; return 0;
} }
// int run_binary_custom_logging(char * argstring, int run_binary_custom_logging(char * argstring,
// long int str_1, long int str_1,
// char ** const buffer, char ** const buffer,
// char ** const error_buffer, char ** const error_buffer,
// size_t * const nbytes) size_t * const nbytes)
// { {
// /* memory for N binary systems */ /* memory for N binary systems */
// struct libbinary_c_store_t * store = NULL; struct libbinary_c_stardata_t *stardata;
struct libbinary_c_store_t * store = NULL;
// /* make new stardata */ /* make new stardata */
// stardata = NULL; stardata = NULL;
// binary_c_new_system(&stardata, binary_c_new_system(&stardata,
// NULL, NULL,
// NULL, NULL,
// &store, &store,
// &argstring, &argstring,
// -1); -1);
// /* disable logging */ /* disable logging */
// snprintf(stardata->preferences->log_filename, snprintf(stardata->preferences->log_filename,
// STRING_LENGTH-1, STRING_LENGTH-1,
// "%s", "%s",
// "/dev/null"); "/dev/null");
// snprintf(stardata->preferences->api_log_filename_prefix, snprintf(stardata->preferences->api_log_filename_prefix,
// STRING_LENGTH-1, STRING_LENGTH-1,
// "%s", "%s",
// "/dev/null"); "/dev/null");
// /* output to strings */ /* output to strings */
// stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
// stardata->preferences->batchmode = BATCHMODE_LIBRARY; stardata->preferences->batchmode = BATCHMODE_LIBRARY;
// stardata->preferences->custom_output_function = str_1; stardata->preferences->custom_output_function = str_1;
// /* do binary evolution */ /* do binary evolution */
// binary_c_evolve_for_dt(stardata, binary_c_evolve_for_dt(stardata,
// stardata->model.max_evolution_time); stardata->model.max_evolution_time);
// /* get buffer pointer */ /* get buffer pointer */
// binary_c_buffer_info(stardata,buffer,nbytes); binary_c_buffer_info(stardata,buffer,nbytes);
// /* get error buffer pointer */ /* get error buffer pointer */
// binary_c_error_buffer(stardata,error_buffer); binary_c_error_buffer(stardata,error_buffer);
// /* set raw_buffer_size = -1 to prevent it being freed */ /* set raw_buffer_size = -1 to prevent it being freed */
// stardata->tmpstore->raw_buffer_size = -1; stardata->tmpstore->raw_buffer_size = -1;
// /* free stardata (except the buffer) */ /* free stardata (except the buffer) */
// binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
// binary_c_free_store_contents(store); binary_c_free_store_contents(store);
// return 0; return 0;
// } }
// int return_arglines(char ** const buffer, int return_arglines(char ** const buffer,
// char ** const error_buffer, char ** const error_buffer,
// size_t * const nbytes) size_t * const nbytes)
// { {
// /* memory for N binary systems */ /* memory for N binary systems */
// struct libbinary_c_stardata_t *stardata; struct libbinary_c_stardata_t *stardata;
// struct libbinary_c_store_t * store = NULL; struct libbinary_c_store_t * store = NULL;
// /* make new stardata */ /* make new stardata */
// stardata = NULL; stardata = NULL;
// char * empty_str = ""; char * empty_str = "";
// binary_c_new_system(&stardata, binary_c_new_system(&stardata,
// NULL, NULL,
// NULL, NULL,
// &store, &store,
// &empty_str, &empty_str,
// -1); -1);
// /* disable logging */ /* disable logging */
// snprintf(stardata->preferences->log_filename, snprintf(stardata->preferences->log_filename,
// STRING_LENGTH-1, STRING_LENGTH-1,
// "%s", "%s",
// "/dev/null"); "/dev/null");
// snprintf(stardata->preferences->api_log_filename_prefix, snprintf(stardata->preferences->api_log_filename_prefix,
// STRING_LENGTH-1, STRING_LENGTH-1,
// "%s", "%s",
// "/dev/null"); "/dev/null");
// /* output to strings */ /* output to strings */
// stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
// stardata->preferences->batchmode = BATCHMODE_LIBRARY; stardata->preferences->batchmode = BATCHMODE_LIBRARY;
// /* List available arguments */ /* List available arguments */
// binary_c_list_args(stardata); binary_c_list_args(stardata);
// /* get buffer pointer */ /* get buffer pointer */
// binary_c_buffer_info(stardata,buffer,nbytes); binary_c_buffer_info(stardata,buffer,nbytes);
// /* get error buffer pointer */ /* get error buffer pointer */
// binary_c_error_buffer(stardata,error_buffer); binary_c_error_buffer(stardata,error_buffer);
// /* set raw_buffer_size = -1 to prevent it being freed */ /* set raw_buffer_size = -1 to prevent it being freed */
// stardata->tmpstore->raw_buffer_size = -1; stardata->tmpstore->raw_buffer_size = -1;
// /* free stardata (except the buffer) */ /* free stardata (except the buffer) */
// binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
// binary_c_free_store_contents(store); binary_c_free_store_contents(store);
// return 0; return 0;
// } }
int run_binary_with_log(char * argstring, int run_binary_with_log(char * argstring,
char ** const buffer, char ** const buffer,
...@@ -271,6 +239,4 @@ int run_binary_with_log(char * argstring, ...@@ -271,6 +239,4 @@ int run_binary_with_log(char * argstring,
binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
binary_c_free_store_contents(store); binary_c_free_store_contents(store);
return 0; return 0;
} }
\ No newline at end of file
...@@ -11,6 +11,7 @@ binary_c_config = os.environ['BINARY_C']+'/binary_c-config' ...@@ -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_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_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 = 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() 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 # create list of tuples of defined macros
......
import ctypes import ctypes
from binaryc_python_utils.custom_logging_functions import autogen_C_logging_code, binary_c_log_code, compile_shared_lib 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 # generate logging lines
logging_line = autogen_C_logging_code( logging_line = autogen_C_logging_code(
...@@ -18,9 +18,27 @@ created_code = binary_c_log_code(logging_line) ...@@ -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') compile_shared_lib(created_code, sourcefile_name='custom_logging.c', outfile_name='libcustom_logging.so')
# Loading library # 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 libmean = ctypes.CDLL("libcustom_logging.so", mode=1) # loads the shared library
# Get memory adress of function. mimicking a pointer # Get memory adress of function. mimicking a pointer
mem = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment