Commit b0c51327 authored by David Hendriks's avatar David Hendriks
Browse files

got a working code

parent 47376ac6
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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
+69 −72
Original line number Diff line number Diff line
@@ -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);
//         }
        
//         Safe_free(buffer);
//         Safe_free(error_buffer);

//         /* 
//          * TODO
//          * return the return_error_string as well!
//          */
//         return return_string;
//     }
// }
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);

        /* 
         * 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);
//     }
    
//     Safe_free(buffer);
//     Safe_free(error_buffer);

//     /* 
//      * TODO
//      * return the return_error_string as well!
//      */
//     return return_string;
// }
 No newline at end of file
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
+8 −8
Original line number Diff line number Diff line
@@ -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
+86 −120
Original line number Diff line number Diff line
@@ -35,49 +35,69 @@
 * 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[])
int run_binary(char * argstring,
               char ** const buffer,
               char ** const error_buffer,
               size_t * const nbytes)
{
    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;
}
    /* 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);
    
int run_binary(char * argstring,
    /* 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;

    /* 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 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;
    
    /* free stardata (except the buffer) */
    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
    binary_c_free_store_contents(store);
        
    return 0;
}

int run_binary_custom_logging(char * argstring,
               long int str_1,
               char ** const buffer,
               char ** const error_buffer,
               size_t * const nbytes)
@@ -107,6 +127,7 @@ int run_binary(char * argstring,
    /* 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,
@@ -128,109 +149,56 @@ 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;

//     /* 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;

//     /* 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 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;
    
//     /* free stardata (except the buffer) */
//     binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
//     binary_c_free_store_contents(store);
        
//     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;

//     /* 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");

//     /* output to strings */
//     stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
//     stardata->preferences->batchmode = BATCHMODE_LIBRARY;

//     /* List available arguments */
//     binary_c_list_args(stardata);

//     /* get buffer pointer */
//     binary_c_buffer_info(stardata,buffer,nbytes);
    
//     /* 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;
    
//     /* free stardata (except the buffer) */
//     binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
//     binary_c_free_store_contents(store);

//     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;

    /* 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");

    /* output to strings */
    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

    /* List available arguments */
    binary_c_list_args(stardata);

    /* get buffer pointer */
    binary_c_buffer_info(stardata,buffer,nbytes);
    
    /* 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;
    
    /* free stardata (except the buffer) */
    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
    binary_c_free_store_contents(store);

    return 0;
}

int run_binary_with_log(char * argstring,
               char ** const buffer,
@@ -272,5 +240,3 @@ int run_binary_with_log(char * argstring,
    binary_c_free_store_contents(store);
    return 0;
}
 No newline at end of file

+1 −0
Original line number Diff line number Diff line
@@ -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
Loading