diff --git a/Makefile b/Makefile index c5e711f68e830fc833881a2bf06619622032d07d..5a818ec6d6fc741b2724d1e20469a68cb3d40af4 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,12 @@ all: $(CC) -DBINARY_C=$(BINARY_C) $(SO_FLAGS) -o $(SO_NAME) $(OBJECTS) $(PY_EXEC) $(PY_SETUP) $(PY_OPTIONS) +debug: + $(CC) -DBINARY_C=$(BINARY_C) -DBINARY_C_PYTHON_DEBUG $(CFLAGS) $(INCDIRS) $(C_SRC) -o $(OBJECTS) $(OBJ_FLAGS) $(LIBS) + $(CC) -DBINARY_C=$(BINARY_C) -DBINARY_C_PYTHON_DEBUG $(SO_FLAGS) -o $(SO_NAME) $(OBJECTS) + $(PY_EXEC) $(PY_SETUP) $(PY_OPTIONS) + + test: @echo OBJECTS: $(OBJECTS) @echo LIBS: $(LIBS) diff --git a/include/binary_c_python.h b/include/binary_c_python.h index 99aeeeb371f12dbd69166455ec294e1a8b10ae5c..8373b511a777e8c1ffb62ce599d819ba020f18f2 100644 --- a/include/binary_c_python.h +++ b/include/binary_c_python.h @@ -59,9 +59,21 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data char ** const error_buffer, size_t * const nbytes); +int free_store_memaddr(long int store_memaddr, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes); + + /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 #define APIprint(...) APIprintf(__VA_ARGS__); #define NO_OUTPUT +#ifdef BINARY_C_PYTHON_DEBUG + #define debug_printf(fmt, ...) printf(fmt, __VA_ARGS__); +#else + #define debug_printf(fmt, ...) /* Do nothing */ +#endif + #endif // BINARY_C_C_PYTHON_H diff --git a/src/binary_c_python.c b/src/binary_c_python.c index 6a45e8db7c64c6bb7b2fe49d1b76aa72719037b9..1bcafb324284d49e1a7ca04582bcec223832687c 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -18,6 +18,7 @@ * http://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html */ +// TODO: Put in clear debug statements /* list of variables used in the Py<>C interface */ /************************************************************/ @@ -63,8 +64,8 @@ static char return_persistent_data_memaddr_docstring[] = static char free_persistent_data_memaddr_and_return_json_output_docstring[] = "Frees the persistent_data memory and returns the json output"; - - +static char free_store_memaddr_docstring[] = + "Frees the store memaddr"; static struct libbinary_c_store_t *store = NULL; @@ -91,6 +92,7 @@ static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObjec // Free functions static PyObject* binary_c_free_persistent_data_memaddr_and_return_json_output(PyObject *self, PyObject *args); +static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args); /* Set the module functions */ static PyMethodDef module_methods[] = { @@ -114,7 +116,9 @@ static PyMethodDef module_methods[] = { {"return_store_memaddr", binary_c_return_store_memaddr, METH_VARARGS, return_store_memaddr_docstring}, {"return_persistent_data_memaddr", binary_c_return_persistent_data_memaddr, METH_NOARGS, return_persistent_data_memaddr_docstring}, + {"free_persistent_data_memaddr_and_return_json_output", binary_c_free_persistent_data_memaddr_and_return_json_output, METH_VARARGS, free_persistent_data_memaddr_and_return_json_output_docstring}, + {"free_store_memaddr", binary_c_free_store_memaddr, METH_VARARGS, free_store_memaddr_docstring}, {NULL, NULL, 0, NULL} }; @@ -269,7 +273,7 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run (via binary_c_run_system): %s\n", + "Error (in function: binary_c_run_system): %s\n", error_buffer); } @@ -300,7 +304,7 @@ static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_return_arglines): %s\n", error_buffer); } @@ -319,32 +323,30 @@ static PyObject* binary_c_return_help_info(PyObject *self, PyObject *args) { return NULL; } - else + + char * buffer; + char * error_buffer; + size_t nbytes; + int out MAYBE_UNUSED = return_help_info(argstring, + &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) { - char * buffer; - char * error_buffer; - size_t nbytes; - int out MAYBE_UNUSED = return_help_info(argstring, - &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); - - return return_string; + fprintf(stderr, + "Error (in function: binary_c_return_help_info): %s\n", + error_buffer); } + + Safe_free(buffer); + Safe_free(error_buffer); + + return return_string; } static PyObject* binary_c_return_help_all_info(PyObject *self, PyObject *args) @@ -363,7 +365,7 @@ static PyObject* binary_c_return_help_all_info(PyObject *self, PyObject *args) if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_return_help_all_info): %s\n", error_buffer); } @@ -389,7 +391,7 @@ static PyObject* binary_c_return_version_info(PyObject *self, PyObject *args) if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_return_version_info): %s\n", error_buffer); } @@ -422,7 +424,7 @@ static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args) if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_return_store_memaddr): %s\n", error_buffer); } @@ -452,7 +454,7 @@ static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObjec if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_return_persistent_data_memaddr): %s\n", error_buffer); } @@ -463,54 +465,52 @@ static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObjec } /* 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 */ +static PyObject* binary_c_free_persistent_data_memaddr_and_return_json_output(PyObject *self, PyObject *args) +{ + /* Python binding that calls the c function that free's the persistent data memory and prints out the json */ -// /* 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); + long int persistent_data_memaddr = -1; + + /* Parse the input tuple */ + if(!PyArg_ParseTuple(args, "l", &persistent_data_memaddr)) + { + return NULL; + } -// return 0; -// } + char * buffer; + char * error_buffer; + size_t nbytes; + long int out MAYBE_UNUSED = free_persistent_data_memaddr_and_return_json_output(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); -static PyObject* binary_c_free_persistent_data_memaddr_and_return_json_output(PyObject *self, PyObject *args) + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error (in function: binary_c_free_persistent_data_memaddr_and_return_json_output): %s\n", + error_buffer); + } + + Safe_free(buffer); + Safe_free(error_buffer); + + return return_string; +} + +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 */ - long int persistent_data_memaddr = -1; + long int store_memaddr = -1; - if(!PyArg_ParseTuple(args, "l", &persistent_data_memaddr)) + /* Parse the input tuple */ + if(!PyArg_ParseTuple(args, "l", &store_memaddr)) { return NULL; } @@ -519,7 +519,7 @@ static PyObject* binary_c_free_persistent_data_memaddr_and_return_json_output(Py char * error_buffer; size_t nbytes; - long int out MAYBE_UNUSED = free_persistent_data_memaddr_and_return_json_output(persistent_data_memaddr, + long int out MAYBE_UNUSED = free_store_memaddr(store_memaddr, &buffer, &error_buffer, &nbytes); @@ -531,12 +531,12 @@ static PyObject* binary_c_free_persistent_data_memaddr_and_return_json_output(Py if(error_buffer != NULL && strlen(error_buffer)>0) { fprintf(stderr, - "Error in binary_c run : %s\n", + "Error (in function: binary_c_free_store): %s\n", error_buffer); } Safe_free(buffer); Safe_free(error_buffer); - return return_string; + return 0; } diff --git a/src/binary_c_python_api.c b/src/binary_c_python_api.c index 87b827f19c35c64a760dbff6af35aa588f433be7..9d84849318fdb2df6ae9b78474ee1e87d40d8662 100644 --- a/src/binary_c_python_api.c +++ b/src/binary_c_python_api.c @@ -79,12 +79,12 @@ int run_system(char * argstring, { // load the persistent data from the long int that has been passed persistent_data = (void*)persistent_data_memaddr; - printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); + debug_printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); } else { - printf("persistent_data memory adress was -1, now setting it to NULL\n"); persistent_data = NULL; + debug_printf("persistent_data memory adress was -1, now setting it to NULL\n"); } /* Set up new system */ @@ -121,7 +121,7 @@ int run_system(char * argstring, stardata->preferences->custom_output_function = (void*)(struct stardata_t *)custom_logging_func_memaddr; } - printf("ensemble_defer: %d\n", stardata->preferences->ensemble_defer); + debug_printf("ensemble_defer: %d\n", stardata->preferences->ensemble_defer); /* do binary evolution */ binary_c_evolve_for_dt(stardata, @@ -144,7 +144,7 @@ int run_system(char * argstring, Boolean free_persistent_data = FALSE; if (persistent_data_memaddr == -1) { - printf("Decided to free the persistent_data memaddr\n"); + debug_printf("Decided to free the persistent_data memaddr\n"); Boolean free_persistent_data = TRUE; } @@ -351,7 +351,7 @@ int return_version_info(char ** const buffer, } /* =================================================================== */ -/* Functions to call other functionality */ +/* Functions set up memoery adresses */ /* =================================================================== */ long int return_store_memaddr(char ** const buffer, @@ -393,8 +393,7 @@ long int return_store_memaddr(char ** const buffer, /* 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); + debug_printf("store is at address: %p store_memaddr_int: %ld\n", (void*)&store, store_memaddr_int); /* Return the memaddr as an int */ return store_memaddr_int; @@ -429,7 +428,7 @@ long int return_persistent_data_memaddr(char ** const buffer, /* 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 persistent_data_memaddr_int: %ld\n", (void*)&stardata->persistent_data, persistent_data_memaddr_int); + debug_printf("persistent_data is at address: %p persistent_data_memaddr_int: %ld\n", (void*)&stardata->persistent_data, persistent_data_memaddr_int); /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata @@ -444,6 +443,10 @@ long int return_persistent_data_memaddr(char ** const buffer, return persistent_data_memaddr_int; } +/* =================================================================== */ +/* Functions free memory */ +/* =================================================================== */ + int free_persistent_data_memaddr_and_return_json_output(long int persistent_data_memaddr, char ** const buffer, char ** const error_buffer, @@ -459,7 +462,7 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data { // load the persistent data from the long int that has been passed persistent_data = (void*)persistent_data_memaddr; - printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); + debug_printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); } else { @@ -502,3 +505,57 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data return 0; } + +int free_store_memaddr(long int store_memaddr, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes) +{ + struct libbinary_c_stardata_t *stardata = NULL; + struct libbinary_c_persistent_data_t *persistent_data = NULL; + char * empty_str = ""; + + // Store: + /* Check the value of the store_memaddr */ + struct libbinary_c_store_t *store; + if(store_memaddr != -1) + { + // load the store from the integer that has been passed + store = (void*)store_memaddr; + } + else + { + store = NULL; + } + + /* Set up new system */ + binary_c_new_system(&stardata, // stardata + NULL, // previous_stardatas + NULL, // preferences + &store, // store + &persistent_data, // persistent_data + &empty_str, // argv + -1 // argc + ); + + /* output to strings */ + stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + + /* get buffer pointer */ + binary_c_buffer_info(stardata, buffer, nbytes); + + /* get error buffer pointer */ + binary_c_error_buffer(stardata, error_buffer); + + /* free the reststardata (except the buffer) */ + binary_c_free_memory(&stardata, // Stardata + TRUE, // free_preferences + TRUE, // free_stardata + TRUE, // free_store + FALSE, // free_raw_buffer + TRUE // free_persistent + ); + + return 0; +} diff --git a/tests/test_persistent_data.py b/tests/test_persistent_data.py index b1ecc360444a1ce4deb977fb870ca86a0873da67..e7c24cc0273dc98c5de1784babcd722c5d1b69ba 100644 --- a/tests/test_persistent_data.py +++ b/tests/test_persistent_data.py @@ -2,19 +2,37 @@ Module containing tests regarding the persistent_data memory and the ensemble output """ +import os +import sys import time - import json import textwrap import binary_c_python_api -from binarycpython.utils.functions import binarycDecoder +from binarycpython.utils.functions import binarycDecoder, temp_dir + +TMP_DIR = temp_dir() +os.makedirs(os.path.join(TMP_DIR, "test"), exist_ok=True) + #### +def return_argstring(m1=15.0, m2=14.0, separation=0, orbital_period=453000000000, eccentricity=0.0, metallicity=0.02, max_evolution_time=15000, defer_ensemble=0, ensemble_filters_off=1, ensemble_filter='SUPERNOVAE'): + """ + Function to make a argstring that we can use in these tests + """ + + # Make the argstrings + argstring_template = "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} ensemble 1 ensemble_defer {7} \ +ensemble_filters_off {8} ensemble_filter_{9} 1 probability 0.1" + + argstring = argstring_template.format(m1, m2, separation, orbital_period, eccentricity, + metallicity, max_evolution_time, defer_ensemble, ensemble_filters_off, ensemble_filter) + + return argstring -# Evolution functions def test_return_persistent_data_memaddr(): - output = binary_c_python_api.return_persistent_data_memaddr("") + output = binary_c_python_api.return_persistent_data_memaddr() print("function: test_run_system") print("Binary_c output:") @@ -24,23 +42,10 @@ def test_passing_persistent_data_to_run_system(): # Function to test the passing of the persistent data memoery adress, and having ensemble_defer = True # We should see that the results of multiple systems have been added to the one output json - # Some values - m1 = 15.0 # Msun - m2 = 14.0 # Msun - separation = 0 # 0 = ignored, use period - orbital_period = 453000000000.0 # days - eccentricity = 0.0 - metallicity = 0.02 - max_evolution_time = 15000 - - # Make the argstrings - argstring_template = "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} ensemble 1 ensemble_defer {7}\ - ensemble_filters_off 1 ensemble_filter_SUPERNOVAE 1 probability 0.1" - - argstring_1 = argstring_template.format(m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "0") - argstring_1_deferred = argstring_template.format(m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "1") - argstring_2 = argstring_template.format(m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "0") + # Make argstrings + argstring_1 = return_argstring(defer_ensemble=0) + argstring_1_deferred = return_argstring(defer_ensemble=1) + argstring_2 = return_argstring(defer_ensemble=0) # persistent_data_memaddr = binary_c_python_api.return_persistent_data_memaddr() @@ -60,32 +65,15 @@ def test_passing_persistent_data_to_run_system(): ensemble_jsons_1 = [line for line in output_1_again.splitlines() if line.startswith("ENSEMBLE_JSON")] json_1_again = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):]) - print("Twice the same system are they equal?:") - print(json_1 == json_1_again) - - print("first system vs deferred double system?:") - print(json_1 == json_2) - - # printf("combined double system vs deferred double system?:") - -def ensemble_output(): - m1 = 15.0 # Msun - m2 = 14.0 # Msun - separation = 0 # 0 = ignored, use period - orbital_period = 453000000000.0 # days - eccentricity = 0.0 - metallicity = 0.02 - max_evolution_time = 15000 - - argstring_1 = "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} ensemble 1 ensemble_defer 0 ensemble_filters_off 1 ensemble_filter_SUPERNOVAE 1".format( - m1, - m2, - separation, - orbital_period, - eccentricity, - metallicity, - max_evolution_time, - ) + assert json_1 == json_1_again, "The system with the same initial settings did not give the same output" + assert json_1 != json_2, "The output of the deferred two systems should not be the same as the first undeferred output" + +def test_full_ensemble_output(): + """ + Function to just output the whole ensemble + """ + + argstring_1 = return_argstring(defer_ensemble=0, ensemble_filters_off=0) output_1 = binary_c_python_api.run_system(argstring=argstring_1) ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")] @@ -93,34 +81,35 @@ def ensemble_output(): json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder) stop = time.time() - print(json.dumps(json_1, indent=4)) + with open(os.path.join(TMP_DIR, "test", "json_full_ensemble.json"), 'w') as f: + f.write(json.dumps(json_1, indent=4)) + print("took {}s to decode".format(stop-start)) + print("Size of the json in memory: {}".format(sys.getsizeof(json_1))) + # assert statements: + assert "number_counts" in json_1.keys() + assert "HRD" in json_1.keys() + assert "HRD(t)" in json_1.keys() + assert "Xyield" in json_1.keys() + assert "distributions" in json_1.keys() + assert "scalars" in json_1.keys() +def test_adding_ensemble_output(): + """ + Function that adds the output of 2 ensembles and compares it to the output that we get by deferring the first output + """ -def adding_ensemble_output(): m1 = 2 # Msun m2 = 0.1 # Msun - separation = 0 # 0 = ignored, use period - orbital_period = 453000000.0 # days - eccentricity = 0.0 - metallicity = 0.02 - max_evolution_time = 15000 - - # Set up argstrings - argstring_template = "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} ensemble 1 ensemble_defer {7}\ - ensemble_filters_off 1 ensemble_filter_STELLAR_TYPE_COUNTS 1 probability 0.1" ############################################################################################# # The 2 runs below use the ensemble but do not defer the output to anything else, so that the # results are returned directly after the run # Direct output commands - argstring_1 = argstring_template.format( - m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "0") - argstring_2 = argstring_template.format( - m1+1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "0") + argstring_1 = return_argstring(m1=m1, m2=m2, ensemble_filter="STELLAR_TYPE_COUNTS", defer_ensemble=0) + argstring_2 = return_argstring(m1=m1+1, m2=m2, ensemble_filter="STELLAR_TYPE_COUNTS", defer_ensemble=0) # Get outputs output_1 = binary_c_python_api.run_system(argstring=argstring_1) @@ -135,9 +124,9 @@ def adding_ensemble_output(): # test_1_total_dict = SumDict(json_1) # test_1_total_dict.merge(json_2) - with open("json_1.json", 'w') as f: + with open(os.path.join(TMP_DIR, "test", "adding_json_1.json"), 'w') as f: f.write(json.dumps(json_1, indent=4)) - with open("json_2.json", 'w') as f: + with open(os.path.join(TMP_DIR, "test", "adding_json_2.json"), 'w') as f: f.write(json.dumps(json_2, indent=4)) print("Single runs done\n") @@ -148,10 +137,8 @@ def adding_ensemble_output(): # have the output returned in that way # Deferred commands - argstring_1_deferred = argstring_template.format( - m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "1") - argstring_2_deferred = argstring_template.format( - m1+1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "1") + argstring_1_deferred = return_argstring(m1=m1, m2=m2, ensemble_filter="STELLAR_TYPE_COUNTS", defer_ensemble=1) + argstring_2_deferred = return_argstring(m1=m1+1, m2=m2, ensemble_filter="STELLAR_TYPE_COUNTS", defer_ensemble=1) # Get a memory location persistent_data_memaddr = binary_c_python_api.return_persistent_data_memaddr() @@ -173,7 +160,7 @@ def adding_ensemble_output(): json_deferred = json.loads(ensemble_jsons_deferred[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder) - with open("json_deferred.json", 'w') as f: + with open(os.path.join(TMP_DIR, "test", "adding_json_deferred.json"), 'w') as f: f.write(json.dumps(json_deferred, indent=4)) print("Double deferred done\n") @@ -199,7 +186,7 @@ def adding_ensemble_output(): json_deferred_and_output = json.loads(ensemble_jsons_deferred_and_output[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder) - with open("json_deferred_and_output.json", 'w') as f: + with open(os.path.join(TMP_DIR, "test", "adding_json_deferred_and_output.json"), 'w') as f: f.write(json.dumps(json_deferred_and_output, indent=4)) print("Single deferred done\n") @@ -211,22 +198,13 @@ def test_free_and_json_output(): m1 = 2 # Msun m2 = 0.1 # Msun - separation = 0 # 0 = ignored, use period - orbital_period = 453000000000.0 # days - eccentricity = 0.0 - metallicity = 0.02 - max_evolution_time = 15000 + + # Get argstring: + argstring_1 = return_argstring(m1=m2, m2=m2, ensemble_filter="STELLAR_TYPE_COUNTS", defer_ensemble=1) # Get a memory adress: persistent_data_memaddr = binary_c_python_api.return_persistent_data_memaddr("") - # Set up argstrings - argstring_template = "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} ensemble 1 ensemble_defer {7}\ - ensemble_filters_off 1 ensemble_filter_STELLAR_TYPE_COUNTS 1 probability 0.1" - argstring_1 = argstring_template.format( - m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "1") - # Evolve and defer output print("evolving") output_1_deferred = binary_c_python_api.run_system(argstring=argstring_1, persistent_data_memaddr=persistent_data_memaddr) @@ -241,46 +219,10 @@ def test_free_and_json_output(): print("Output:") print(textwrap.indent(str(json_output_by_freeing), "\t")) - -def full_output(): - m1 = 15.0 # Msun - m2 = 14.0 # Msun - separation = 0 # 0 = ignored, use period - orbital_period = 453000000000.0 # days - eccentricity = 0.0 - metallicity = 0.02 - max_evolution_time = 15000 - - argstring_1 = "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} ensemble 1 ensemble_defer 0".format( - m1, - m2, - separation, - orbital_period, - eccentricity, - metallicity, - max_evolution_time, - ) - output_1 = binary_c_python_api.run_system(argstring=argstring_1) - ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")] - - start = time.time() - json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder) - stop = time.time() - - print("took {}s to decode".format(stop-start)) - - with open("json_full_ensemble.json", 'w') as f: - f.write(json.dumps(json_1, indent=4)) - - - - - #### if __name__ == "__main__": - # test_return_persistent_data_memaddr() + test_return_persistent_data_memaddr() # test_passing_persistent_data_to_run_system() - # ensemble_output() - # adding_ensemble_output() + # test_full_ensemble_output() + # test_adding_ensemble_output() # test_free_and_json_output() - full_output()