Commit 545eab17 authored by David Hendriks's avatar David Hendriks
Browse files

added new function to run the system with a log for further plotting or so

parent 030ae155
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -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 */
+6 −0
Original line number Diff line number Diff line
@@ -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);

+49 −1
Original line number Diff line number Diff line
@@ -167,3 +167,51 @@ int return_arglines(char ** buffer,
    binary_c_free_store_contents(store);
    return 0;
}

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

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