Commit 2ee69077 authored by David Hendriks's avatar David Hendriks
Browse files

resolved merge conflicts

parents a9718c02 57d5ced2
Loading
Loading
Loading
Loading
+27 −26
Original line number Diff line number Diff line
Python module for binary_c
# Python module for binary_c

Based on a original work by Jeff Andrews, updated and extended
for Python3 by Robert Izzard
Based on a original work by Jeff Andrews (can be found in old_solution/ directory)
updated and extended for Python3 by Robert Izzard, David hendriks

Warning : THIS CODE IS EXPERIMENTAL!

@@ -9,41 +9,42 @@ r.izzard@surrey.ac.uk
http://personal.ph.surrey.ac.uk/~ri0005/binary_c.html
09/06/2019

------------------------------------------------------------

Requirements
---------------------
- Python3
- binary_c version 2.1+
- requirements.txt (no?)

Environment variables
---------------------

Before compilation you should set the following environment variables:

required: BINARY_C should point to the root directory of your binary_c installation

recommended: LD_LIBRARY_PATH should include $BINARY_C/src and whatever directories are required to run binary_c (e.g. locations of libgsl, libmemoize, librinterpolate, etc.)
- required: `BINARY_C` should point to the root directory of your binary_c installation
- recommended: `LD_LIBRARY_PATH` should include $BINARY_C/src and whatever directories are required to run binary_c (e.g. locations of libgsl, libmemoize, librinterpolate, etc.)
- recommended: `LIBRARY_PATH` should include whatever directories are required to build binary_c (e.g. locations of libgsl, libmemoize, librinterpolate, etc.)

recommended: LIBRARY_PATH should include whatever directories are required to build binary_c (e.g. locations of libgsl, libmemoize, librinterpolate, etc.)



---------------------
Build instructions
---------------------

To build the module, make sure you have built binary_c (with "make" in the binar_c root directory), its shared library (with "make libbinary_c.so" in the binary_c root directory), and set environment variables as described above, then run:

---
To build the module, make sure you have built binary_c (with `make` in the binary_c root directory), its shared library (with `make libbinary_c.so` in the binary_c root directory), and set environment variables as described above, then run the following code in t:
```
 make clean
 make
---
```
Then to test the Python module:

then to test the Python module

---
```
 python3 ./python_API_test.py
---


```
You will require whatever libraries with which binary_c was compiled, as well as the compiler with which Python was built (usually gcc, which is easily installed on most systems).

If you want to be able to import the binary_c module correctly for child directories (or anywhere for that matter), execute or put the following code in your .bashrc/.zshrc: 
```
export LD_LIBRARY_PATH=<full path to directory containing libbinary_c_api.so>:$LD_LIBRARY_PATH
export PYTHONPATH=<full path to directory containing libbinary_c_api.so>:$PYTHONPATH
```

Usage notes
---------------------
When running a jupyter notebook and importing binary_c, it might happen that the module binary_c cannot be found. I experienced this when I executed Jupyter Notebook from a virtual environment which didnt use the same python (version/binary/shim) as the one I built this library with. Make sure jupyter does use the same underlying python version/binary/shim. That resolved the issue for me.

------------------------------------------------------------
Also: I figured that having binaryc output the log like "<LOG HEADER> t=10e4 ..." (i.e. printing the parameter names as well as their values) would be useful because in that way one can easily have python read that out automatically instead of having to manually copy the list of parameter names.

__init__.py

0 → 100644
+0 −0

Empty file added.

+4.77 KiB

File added.

No diff preview for this file type.

binary_c_api.c

deleted100644 → 0
+0 −125
Original line number Diff line number Diff line
#include "binary_c_python.h"
//#include "binary_c_api.h"
#include "binary_c_API.h"
#include "binary_c_API_prototypes.h"
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


/*
 * apitest
 *
 * Short test programme to throw random binary systems at binary_c's
 * library via its API.
 *
 * Note that it looks more complicated than it is because I have included
 * code to capture binary_c's stdout stream and output it here.
 *
 * This code sends output to stderr : you should use apitest.sh to run it
 * and hence force output to your terminal's stdout.
 *
 * Output lines:
 *
 * APITEST ....  is information about what this code is doing.
 * STATUS  ....  is information about the binary system.
 * BINARY_C .... is output from binary_c (see iterate_logging.c etc.)
 *               which would have gone to stdout
 *
 * If you define the NO_OUTPUT macro, there will be no output except
 * the memory allocation and test system information. This is useful for speed tests,
 * but note that you may end up in a race condition where the pipe which replaces
 * stdout's buffer fills and hence the code stops.
 *
 * Note:
 * 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[])
{
    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 ;
    size_t nbytes;
    int out = run_binary(argstring,
                     &buffer,
                     &nbytes);

    printf("output (binary_c returned %d)\n%s\n",out,buffer);

    free(buffer);
    
    return out;
}


int run_binary(char * argstring,
               char ** buffer,
               size_t * nbytes)
{
    /* memory for N binary systems */
    struct libbinary_c_stardata_t *stardata;
    struct libbinary_c_store_t * store = NULL;

    printf("argstring : %s\n",argstring);
    fflush(stdout);

    stardata = NULL;
    binary_c_new_system(&stardata,
                        NULL,
                        NULL,
                        &store,
                        &argstring,
                        -1);
    snprintf(stardata->preferences->log_filename,
             STRING_LENGTH-1,
             "%s",
             "/dev/null");
    snprintf(stardata->preferences->api_log_filename_prefix,
             STRING_LENGTH-1,
             "%s",
             "/dev/null");
    stardata->preferences->internal_buffering = 2;
    stardata->preferences->batchmode = BATCHMODE_LIBRARY;

    binary_c_evolve_for_dt(stardata,
                           stardata->model.max_evolution_time);

    /*
     * Save the buffer pointer
     */
    binary_c_buffer_info(stardata,&buffer,nbytes);

    /*
     * And free everything else
     */    
    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE);
    binary_c_free_store_contents(store);
    
    return 0;
}

binary_c_api.h

deleted100644 → 0
+0 −17
Original line number Diff line number Diff line
#pragma once
#ifndef BINARY_C_PYTHON_API_H
#define BINARY_C_PYTHON_API_H

/* local function prototypes */
static void APIprintf(char * format,...);

int run_binary(char * argstring,
               char ** buffer,
               size_t * nbytes);

/* C macros */
#define BINARY_C_APITEST_VERSION 0.1
#define APIprint(...) APIprintf(__VA_ARGS__);
#define NO_OUTPUT

#endif // BINARY_C_PYTHON_API_H
Loading