diff --git a/binary_c_python.c b/binary_c_python.c index 3496da885ad035b4f4e8b08f7b8d331bc2ed0353..17d85dedd9462ae947a5b8997abeab4a2be8a541 100644 --- a/binary_c_python.c +++ b/binary_c_python.c @@ -31,6 +31,8 @@ static char create_binary_docstring[] = #endif static char run_binary_docstring[] = "Run one binary using binary_c"; +static char run_binary_with_logdocstring[] = + "Run one binary using binary_c and allow the logfile to be written. Do not use for populations!"; static char new_binary_system_docstring[] = "Return an object containing a binary, ready for evolution"; static char function_prototype_docstring[] = @@ -43,6 +45,7 @@ static struct libbinary_c_store_t *store = NULL; static PyObject* binary_c_create_binary(PyObject *self, PyObject *args); #endif static PyObject* binary_c_run_binary(PyObject *self, PyObject *args); +static PyObject* binary_c_run_binary_with_log(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); @@ -60,6 +63,7 @@ static PyMethodDef module_methods[] = { {"create_binary", binary_c_create_binary, METH_VARARGS, create_binary_docstring}, #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}, {"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}, @@ -191,6 +195,29 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) } } +static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args) +{ + /* Parse the input tuple */ + char *argstring; + + if(!PyArg_ParseTuple(args, "s", &argstring)) + { + return NULL; + } + else + { + char * buffer; + int nbytes; + int out MAYBE_UNUSED = run_binary_with_log(argstring, + &buffer, + &nbytes); + /* copy the buffer to a python string */ + PyObject * ret = Py_BuildValue("s", buffer); + free(buffer); + return ret; + } +} + static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) { /* Binary structures */ diff --git a/binary_c_python.h b/binary_c_python.h index 87de6de73bb06afa2b38f84c658d3c5f1c95c37e..d4381f6885e5596556dc5bda3291af5e3540a7d7 100644 --- a/binary_c_python.h +++ b/binary_c_python.h @@ -13,6 +13,12 @@ int run_binary (char * argstring, char ** outstring, int * nbytes); +int run_binary_with_log (char * argstring, + char ** outstring, + int * nbytes); + + + int return_arglines(char ** buffer, int * nbytes); diff --git a/binary_c_python_api.c b/binary_c_python_api.c index 4e0086d277c9614611a8306da3050a40c36cbbde..511516859613aa2d1f1ae5584164f09f6355dba3 100644 --- a/binary_c_python_api.c +++ b/binary_c_python_api.c @@ -166,4 +166,52 @@ int return_arglines(char ** buffer, binary_c_free_memory(&stardata,TRUE,TRUE,FALSE); binary_c_free_store_contents(store); return 0; -} \ No newline at end of file +} + +int run_binary_with_log(char * argstring, + 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; + 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 = 2; + stardata->preferences->internal_buffering_compression = 0; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + + /* do binary evolution */ + binary_c_evolve_for_dt(stardata, + stardata->model.max_evolution_time); + + /* 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; +} + + diff --git a/binaryc_python_utils/functions.py b/binaryc_python_utils/functions.py index cb1efb0e8991967667749c53175e48dc651f24ff..41ec7208106b58ffbfe65acb1ed2b8da41023da8 100644 --- a/binaryc_python_utils/functions.py +++ b/binaryc_python_utils/functions.py @@ -59,6 +59,7 @@ def run_system(**kwargs): # Construct arguments string and final execution string arg_string = create_arg_string(args) arg_string = f'binary_c {arg_string}' + # print(arg_string) # Run it and get output @@ -67,6 +68,37 @@ def run_system(**kwargs): return output + +def run_system_with_log(**kwargs): + """ + Wrapper to run a system with settings AND logs the files to a designated place defined by the log_filename parameter. + """ + + # Load default args + args = get_defaults() + # args = {} + + # For example + # physics_args['M_1'] = 20 + # physics_args['separation'] = 0 # 0 = ignored, use period + # physics_args['orbital_period'] = 100000000000 # To make it single + + # Use kwarg value to override defaults and add new args + for key in kwargs.keys(): + args[key] = kwargs[key] + + # Construct arguments string and final execution string + arg_string = create_arg_string(args) + arg_string = f'binary_c {arg_string}' + + # print(arg_string) + + # Run it and get output + buffer = "" + output = binary_c.run_binary_with_log(arg_string) + + return output + def parse_output(output, selected_header): """ Function that parses output of binaryc when it is construction like this: 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 06cd0dddcddbe24fbba2cc788caf5ba4888b4eff..eec0359b8b8d7d9b290f427c942610b2af844757 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