diff --git a/include/binary_c_python.h b/include/binary_c_python.h index b75b13825ef323d87871676629e9f3e391e76f74..d6dac96fbf9d7f74dd737294495f8abd182e89cf 100644 --- a/include/binary_c_python.h +++ b/include/binary_c_python.h @@ -14,6 +14,7 @@ int run_system(char * argstring, long int custom_logging_func_memaddr, long int store_memaddr, + long int persistent_data_memaddr, int write_logfile, int population, char ** const buffer, diff --git a/src/binary_c_python.c b/src/binary_c_python.c index 7fd7f0f056e5a80442d436ee930bd9eeec7ae788..48e0320f49c434b70bd221442bd89a852949563f 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -221,21 +221,25 @@ static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *kwargs) { - static char* keywords[] = {"argstring", "custom_logging_func_memaddr", "store_memaddr", "write_logfile", "population", NULL}; + static char* keywords[] = {"argstring", "custom_logging_func_memaddr", "store_memaddr", "persistent_data_memaddr", "write_logfile", "population", NULL}; /* set vars and default values for some*/ char *argstring; long int custom_logging_func_memaddr = -1; long int store_memaddr = -1; + long int persistent_data_memaddr = -1; int write_logfile = 0; int population = 0; + // By using the keywords argument it scans over the given set of kwargs, but if they are not given then the default value is used /* Parse the input tuple */ - if(!PyArg_ParseTupleAndKeywords(args, kwargs, "s|llii", keywords, &argstring, &custom_logging_func_memaddr, &store_memaddr, &write_logfile, &population)) + if(!PyArg_ParseTupleAndKeywords(args, kwargs, "s|lllii", keywords, &argstring, &custom_logging_func_memaddr, &store_memaddr, &persistent_data_memaddr, &write_logfile, &population)) { return NULL; } + printf("Input persistent_Data_memaddr: %lu", persistent_data_memaddr); + /* Call c-function */ char * buffer; char * error_buffer; @@ -243,6 +247,7 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k int out MAYBE_UNUSED = run_system(argstring, // the argstring custom_logging_func_memaddr, // memory adress for the function for custom logging store_memaddr, // memory adress for the store object + persistent_data_memaddr, // memory adress for the persistent data write_logfile, // boolean for whether to write the logfile population, // boolean for whether this is part of a population. &buffer, @@ -415,7 +420,7 @@ static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args) PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); PyObject * store_memaddr = Py_BuildValue("l", out); - printf("store_memaddr: %ld\n", out); + // printf("store_memaddr: %ld\n", out); if(error_buffer != NULL && strlen(error_buffer)>0) { @@ -455,7 +460,7 @@ static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObjec PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); PyObject * persistent_data_memaddr = Py_BuildValue("l", out); - printf("persistent_data_memaddr: %ld\n", out); + // printf("persistent_data_memaddr: %ld\n", out); if(error_buffer != NULL && strlen(error_buffer)>0) { @@ -468,4 +473,81 @@ static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObjec Safe_free(error_buffer); return persistent_data_memaddr; -} \ No newline at end of file +} + +/* Memory freeing functions */ +// static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args) +// { +// /* Python binding that calls the c function that free's the store memory */ + +// /* Parse the input tuple */ +// char *store_memaddr; + +// if(!PyArg_ParseTuple(args, "l", &store_memaddr)) +// { +// return NULL; +// } + +// char * buffer; +// char * error_buffer; +// size_t nbytes; + +// long int out MAYBE_UNUSED = free_store_memaddr(store_memaddr, +// &buffer, +// &error_buffer, +// &nbytes); + +// /* copy the buffer to a python string */ +// PyObject * return_string MAYBE_UNUSED = 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); + +// return 0; +// } + +// static PyObject* binary_c_free_persistent_data_memaddr(PyObject *self, PyObject *args) +// { +// /* Python binding that calls the c function that free's the store memory */ + +// /* Parse the input tuple */ +// char *persistent_data_memaddr; + +// if(!PyArg_ParseTuple(args, "l", &persistent_data_memaddr)) +// { +// return NULL; +// } + +// char * buffer; +// char * error_buffer; +// size_t nbytes; + +// long int out MAYBE_UNUSED = free_persistent_data_memaddr(persistent_data_memaddr, +// &buffer, +// &error_buffer, +// &nbytes); + +// /* copy the buffer to a python string */ +// PyObject * return_string MAYBE_UNUSED = 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); + +// return 0; +// } diff --git a/src/binary_c_python_api.c b/src/binary_c_python_api.c index 78381ce98f495a6b07fbb6f594d3cf209c8ee6f0..0b62a67d824810fc1070132efdbf26a6cee67553 100644 --- a/src/binary_c_python_api.c +++ b/src/binary_c_python_api.c @@ -41,12 +41,15 @@ int stdoutwas; /* Functions to evolve systems */ /* =================================================================== */ -/* Function that runs a system. Has multiple input parameters: +/* +Function that runs a system. Has multiple input parameters: Big function. Takes several arguments. See binary_c_python.c docstring. +TODO: Describe each input */ int run_system(char * argstring, long int custom_logging_func_memaddr, long int store_memaddr, + long int persistent_data_memaddr, int write_logfile, int population, char ** const buffer, @@ -62,7 +65,6 @@ int run_system(char * argstring, if(store_memaddr != -1) { // load the store from the integer that has been passed - // struct libbinary_c_store_t * store = (void*)store_memaddr; store = (void*)store_memaddr; } else @@ -71,8 +73,17 @@ int run_system(char * argstring, store = NULL; } - // persistent_data - struct persistent_data_t * persistent_data = NULL; + // persistent_data: + struct libbinary_c_persistent_data_t *persistent_data; + if(persistent_data_memaddr != -1) + { + // load the persistent data from the long int that has been passed + persistent_data = (void*)persistent_data_memaddr; + } + else + { + persistent_data = NULL; + } /* make new stardata */ stardata = NULL; @@ -80,7 +91,7 @@ int run_system(char * argstring, NULL, // previous_stardatas NULL, // preferences &store, // store - &persistent_data, // persistent_data TODO: see if this use is correct + &persistent_data, // persistent_data &argstring, // argv -1 // argc ); @@ -126,13 +137,20 @@ int run_system(char * argstring, Boolean free_store = TRUE; } + /* Determine whether to free the persistent data memory adress*/ + Boolean free_persistent_data = FALSE; + if (persistent_data_memaddr == -1) + { + Boolean free_persistent_data = TRUE; + } + /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata free_store, // free_store FALSE, // free_raw_buffer - TRUE // free_persistent TODO: check if this is correct here + free_persistent_data // free_persistent TODO: check if this is correct here ); return 0; @@ -382,8 +400,8 @@ long int return_store_memaddr(char * argstring, /* convert the pointer */ uintptr_t store_memaddr_int = (uintptr_t)store; // C Version converting ptr to int - printf("store is at address: %p\n", (void*)&store); - printf("store_memaddr_int: %ld\n", store_memaddr_int); + // printf("store is at address: %p\n", (void*)&store); + // printf("store_memaddr_int: %ld\n", store_memaddr_int); /* Return the memaddr as an int */ return store_memaddr_int; @@ -420,9 +438,8 @@ long int return_persistent_data_memaddr(char * argstring, /* convert the pointer */ uintptr_t persistent_data_memaddr_int = (uintptr_t)stardata->persistent_data; // C Version converting ptr to int - printf("persistent_data is at address: %p\n", (void*)stardata->persistent_data); - printf("persistent_data_memaddr_int: %lu\n", persistent_data_memaddr_int); - + // printf("persistent_data is at address: %p\n", (void*)stardata->persistent_data); + // printf("persistent_data_memaddr_int: %lu\n", persistent_data_memaddr_int); /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata @@ -435,4 +452,21 @@ long int return_persistent_data_memaddr(char * argstring, /* Return the memaddr as an int */ return persistent_data_memaddr_int; -} \ No newline at end of file +} + +// /* Memory freeing functions */ +// int free_store_memaddr(long int * store_memaddr, +// char ** const buffer, +// char ** const error_buffer, +// size_t * const nbytes) +// { + + + +// return 0; +// } + +// int free_persistent_data_memaddr +// { + +// } diff --git a/tests/test_return_persistent_data_memaddr.py b/tests/test_return_persistent_data_memaddr.py index 52a6a92fdf5586c9ddf7c724c3a2a19bd95af35e..d5551ff724e46a30ef66f3638bd781d3dce8f9e4 100644 --- a/tests/test_return_persistent_data_memaddr.py +++ b/tests/test_return_persistent_data_memaddr.py @@ -1,6 +1,4 @@ import binary_c_python_api - - import textwrap @@ -12,6 +10,37 @@ def test_return_persistent_data_memaddr(): print("Binary_c output:") print(textwrap.indent(str(output), "\t")) + return output + +def test_passing_persistent_data_to_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, + ) + + persistent_data_memaddr = test_return_persistent_data_memaddr() + print(persistent_data_memaddr) + output = binary_c_python_api.run_system(argstring=argstring, persistent_data_memaddr=persistent_data_memaddr) + + print("function: test_run_system") + print("Binary_c output:") + print(textwrap.indent(output, "\t")) + + + #### if __name__ == "__main__": test_return_persistent_data_memaddr() + test_passing_persistent_data_to_run_system() \ No newline at end of file