From 7b181ee2161cfc2e0ac240f1c0844dee18daff86 Mon Sep 17 00:00:00 2001 From: Radek Poleski Date: Wed, 12 Aug 2020 16:04:23 +0200 Subject: [PATCH] v1.16 - improved import of C and C++ code --- README.md | 6 +- developers_board.md | 6 +- documents/macos_install.md | 18 ----- documents/windows_install.md | 28 ------- setup.py | 4 +- source/AdaptiveContouring/ac_wrap.c | 50 ++++++++++++ source/MulensModel/binarylens.py | 78 ++++++++++++------- source/MulensModel/version.py | 2 +- .../VBBinaryLensingLibrary_wrapper.cpp_NEW | 72 ----------------- source/VBBL/vbbl_wrapper.cpp | 76 ++++++++++++++++++ 10 files changed, 184 insertions(+), 156 deletions(-) delete mode 100644 documents/macos_install.md delete mode 100644 documents/windows_install.md create mode 100644 source/AdaptiveContouring/ac_wrap.c delete mode 100644 source/VBBL/VBBinaryLensingLibrary_wrapper.cpp_NEW create mode 100644 source/VBBL/vbbl_wrapper.cpp diff --git a/README.md b/README.md index 8984fd54..0b449882 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [**Detailed documentation: https://rpoleski.github.io/MulensModel/**](https://rpoleski.github.io/MulensModel/) -[Latest release: 1.14.0](https://github.com/rpoleski/MulensModel/releases/latest) and we're working on further developing the code. +[Latest release: 1.16.0](https://github.com/rpoleski/MulensModel/releases/latest) and we're working on further developing the code. MulensModel can generate a microlensing light curve for a given set of microlensing parameters, fit that light curve to some data, and return a chi2 value. That chi2 can then be input into an arbitrary likelihood function to find the best-fit parameters. @@ -42,10 +42,8 @@ pip install -r requirements.txt ``` Alternatively, you can run makefiles: go to `source/VBBL/` and run `make`, then go to `source/AdaptiveContouring/` and do the same. Then and add the path `MulensModel/source` to your `PYTHONPATH`. If you have any problems, please contact the authors and we will try to help. -If you want to **install MulensModel on Windows**, please see notes [here](documents/windows_install.md). If you have **problems with installing or running MulensModel on MacOS**, please see notes [here](documents/macos_install.md). We're now working on improving intallation process. - --- [![astropy](http://img.shields.io/badge/powered%20by-AstroPy-orange.svg?style=flat)](http://www.astropy.org/) -file revised Jul 2020 +file revised Aug 2020 diff --git a/developers_board.md b/developers_board.md index 4eef2ff3..ba2a869f 100644 --- a/developers_board.md +++ b/developers_board.md @@ -2,10 +2,10 @@ 1. windows compilation - [link](https://docs.python.org/3.7/extending/index.html) 3. UC 16 - make it ready for e-mail annoucement 4. new method for FSPL with LD -5. UC 16 - file_name removed in minimal example +5. UC 16 - file\_name removed in minimal example 6. remove unused branches 7. check notes below -8. add notes: 1) adding t_eff_1, t_eff_2, 2) example with [parallel EMCEE](https://emcee.readthedocs.io/en/stable/tutorials/parallel/); 3) example 2 - clarify first function; 4) data.bad requires example - note that one has to substitute full vector, not single values +8. add notes: 1) adding t\_eff\_1, t\_eff\_2, 2) example with [parallel EMCEE](https://emcee.readthedocs.io/en/stable/tutorials/parallel/); 3) example 2 - clarify first function; 4) data.bad requires example - note that one has to substitute full vector, not single values 8. back to triple lenses ## Nov & Dec goals: @@ -30,7 +30,7 @@ _italics_ mark important tasks -Changes for planned v2 are here: [documents/MM_v2.md](documents/MM_v2.md) +Changes for planned v2 are here: [documents/MM\_v2.md](documents/MM_v2.md) * Install * **makefile for Windows (basic instructions exist already) [good example](https://stackoverflow.com/a/145649), [checking for Windows in makefile](https://github.com/dariomanesku/cmft/issues/28)** diff --git a/documents/macos_install.md b/documents/macos_install.md deleted file mode 100644 index 9246e138..00000000 --- a/documents/macos_install.md +++ /dev/null @@ -1,18 +0,0 @@ -### MacOS installation issues - -We have found that recent versions of MacOS produces strange error messages when MulensModel is installed or run. Example error message: - -``` -(ValueError: ' + +#ifndef FLOAT +#define FLOAT double +#endif + + +FLOAT ld_linear(int n, FLOAT gam[], FLOAT rho); + +FLOAT mag_binext(FLOAT y1, FLOAT y2, FLOAT rho, FLOAT d, FLOAT q, + FLOAT (*ld_func)(int,FLOAT*,FLOAT), int n, FLOAT gam[], + FLOAT acc, FLOAT ld_acc); + +FLOAT mag_binpt(FLOAT y1, FLOAT y2, FLOAT d, FLOAT q); + + +static PyObject * Adaptive_Contouring_Linear_wrapper(PyObject *self, PyObject *args) { + double d, q, y1, y2, rho, gamma, acc, ld_acc; + double mag, gam[1]; + + if (!PyArg_ParseTuple(args, "dddddddd", &d, &q, &y1, &y2, &rho, &gamma, &acc, &ld_acc)) return NULL; + + if (gamma == 0.0) { + mag = mag_binext(y1, y2, rho, d, q, NULL, -1, NULL, acc, ld_acc); + } + else { + gam[0] = gamma; + mag = mag_binext(y1, y2, rho, d, q, ld_linear, 1, gam, acc, ld_acc); + } + + return Py_BuildValue("d", mag); +} + +static PyMethodDef ACMethods[] = { + {"Adaptive_Contouring_Linear", Adaptive_Contouring_Linear_wrapper, METH_VARARGS, "some notes here"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static struct PyModuleDef ACmodule = { + PyModuleDef_HEAD_INIT, + "AdaptiveContouring", + NULL, + -1, + ACMethods +}; + +PyMODINIT_FUNC PyInit_AdaptiveContouring(void) { + return PyModule_Create(&ACmodule); +} diff --git a/source/MulensModel/binarylens.py b/source/MulensModel/binarylens.py index ed2d58d5..0adcb174 100755 --- a/source/MulensModel/binarylens.py +++ b/source/MulensModel/binarylens.py @@ -8,6 +8,18 @@ import MulensModel from MulensModel.utils import Utils +try: + import MulensModel.VBBL as mm_vbbl +except: + _vbbl_wrapped = False +else: + _vbbl_wrapped = True +try: + import MulensModel.AdaptiveContouring as mm_ac +except: + _adaptive_contouring_wrapped = False +else: + _adaptive_contouring_wrapped = True def _try_load(path, name): @@ -29,11 +41,6 @@ def _try_load(path, name): return None -def _get_path_1(name): - """convenience function""" - return os.path.join(os.path.dirname(MulensModel.__file__), name + "*.so") - - def _get_path_2(name_1, name_2): """convenience function""" module_path = os.path.abspath(__file__) @@ -42,38 +49,53 @@ def _get_path_2(name_1, name_2): return os.path.join(module_path, 'source', name_1, name_2) -vbbl = _try_load(glob.glob(_get_path_1("VBBL")), "VBBL") -if vbbl is None: - vbbl = _try_load(_get_path_2('VBBL', "VBBinaryLensingLibrary_wrapper.so"), - "VBBL") -_vbbl_wrapped = (vbbl is not None) +def _import_compiled_VBBL(): + """try importing manually compiled VBBL package""" + vbbl = _try_load( + _get_path_2('VBBL', "VBBinaryLensingLibrary_wrapper.so"), "VBBL") + _vbbl_wrapped = (vbbl is not None) + if _vbbl_wrapped: + vbbl.VBBinaryLensing_BinaryMagDark.argtypes = 7 * [ctypes.c_double] + vbbl.VBBinaryLensing_BinaryMagDark.restype = ctypes.c_double -ac = "AdaptiveContouring" -adaptive_contour = _try_load(glob.glob(_get_path_1(ac)), ac) -if adaptive_contour is None: - adaptive_contour = _try_load(_get_path_2(ac, ac + "_wrapper.so"), ac) -_adaptive_contouring_wrapped = (adaptive_contour is not None) + vbbl.VBBL_SG12_5.argtypes = 12 * [ctypes.c_double] + vbbl.VBBL_SG12_5.restype = np.ctypeslib.ndpointer( + dtype=ctypes.c_double, shape=(10,)) + return (_vbbl_wrapped, vbbl.VBBinaryLensing_BinaryMagDark, vbbl.VBBL_SG12_5) -if _vbbl_wrapped: - vbbl.VBBinaryLensing_BinaryMagDark.argtypes = 7 * [ctypes.c_double] - vbbl.VBBinaryLensing_BinaryMagDark.restype = ctypes.c_double - _vbbl_binary_mag_dark = vbbl.VBBinaryLensing_BinaryMagDark - vbbl.VBBL_SG12_5.argtypes = 12 * [ctypes.c_double] - vbbl.VBBL_SG12_5.restype = np.ctypeslib.ndpointer( - dtype=ctypes.c_double, shape=(10,)) - _vbbl_SG12_5 = vbbl.VBBL_SG12_5 +def _import_compiled_AdaptiveContouring(): + """try importing manually compiled AdaptiveContouring package""" + ac = "AdaptiveContouring" + adaptive_contour = _try_load(_get_path_2(ac, ac + "_wrapper.so"), ac) + _adaptive_contouring_wrapped = (adaptive_contour is not None) + if _adaptive_contouring_wrapped: + adaptive_contour.Adaptive_Contouring_Linear.argtypes = ( + 8 * [ctypes.c_double]) + adaptive_contour.Adaptive_Contouring_Linear.restype = ctypes.c_double + return (_adaptive_contouring_wrapped, + adaptive_contour.Adaptive_Contouring_Linear) + +# Check import and try manually compiled versions. +if _vbbl_wrapped: + _vbbl_binary_mag_dark = mm_vbbl.VBBinaryLensing_BinaryMagDark + _vbbl_SG12_5 = mm_vbbl.VBBL_SG12_5 +else: + out = _import_compiled_VBBL() + _vbbl_wrapped = out[0] + _vbbl_binary_mag_dark = out[1] + _vbbl_SG12_5 = out[2] if not _vbbl_wrapped: _solver = 'numpy' else: _solver = 'Skowron_and_Gould_12' - if _adaptive_contouring_wrapped: - adaptive_contour.Adaptive_Contouring_Linear.argtypes = ( - 8 * [ctypes.c_double]) - adaptive_contour.Adaptive_Contouring_Linear.restype = ctypes.c_double - _adaptive_contouring_linear = adaptive_contour.Adaptive_Contouring_Linear + _adaptive_contouring_linear = mm_ac.Adaptive_Contouring_Linear +else: + out = _import_compiled_AdaptiveContouring() + _adaptive_contouring_wrapped = out[0] + _adaptive_contouring_linear = out[1] class BinaryLens(object): diff --git a/source/MulensModel/version.py b/source/MulensModel/version.py index e07e4a2c..e95c214a 100644 --- a/source/MulensModel/version.py +++ b/source/MulensModel/version.py @@ -1,2 +1,2 @@ -__version__ = "1.15.12" +__version__ = "1.16.0" diff --git a/source/VBBL/VBBinaryLensingLibrary_wrapper.cpp_NEW b/source/VBBL/VBBinaryLensingLibrary_wrapper.cpp_NEW deleted file mode 100644 index 038a9bc7..00000000 --- a/source/VBBL/VBBinaryLensingLibrary_wrapper.cpp_NEW +++ /dev/null @@ -1,72 +0,0 @@ -#include - -# Python wrapper for VBBL -# P. Mroz @ Caltech, 17 Jun 2020 - -#include "VBBinaryLensingLibrary.h" -#include "Python.h" - -static PyObject * -VBBinaryLensing_BinaryMagDark_wrapper (PyObject *self, PyObject *args) { - double a,q,y1,y2,RSv,a1,Tol,res; - static VBBinaryLensing VBBL; - if (!PyArg_ParseTuple(args,"ddddddd",&a,&q,&y1,&y2,&RSv,&a1,&Tol)) return NULL; - res = VBBL.BinaryMagDark(a, q, y1, y2, RSv, a1, Tol); - return Py_BuildValue("d",res); -} - -PyObject * makelist (double *array, size_t size) { - PyObject *l = PyList_New(size); - for (size_t i = 0; i != size; ++i) { - PyList_SET_ITEM(l,i,PyFloat_FromDouble(array[i])); - } - return l; -} - -static PyObject * -VBBL_SG12_5_wrapper (PyObject *self, PyObject *args) { - - static VBBinaryLensing VBBL; - complex complex_poly[6], complex_roots[5]; - static double roots[10]; - double p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11; - int i; - - if (!PyArg_ParseTuple(args,"dddddddddddd",&p0,&p1,&p2,&p3,&p4,&p5,&p6,&p7,&p8,&p9,&p10,&p11)) return NULL; - - complex_poly[0] = complex(p0, p6); - complex_poly[1] = complex(p1, p7); - complex_poly[2] = complex(p2, p8); - complex_poly[3] = complex(p3, p9); - complex_poly[4] = complex(p4, p10); - complex_poly[5] = complex(p5, p11); - - VBBL.cmplx_roots_gen(complex_roots, complex_poly, 5, true, true); - - for (i=0; i<5; i++) { - roots[i] = complex_roots[i].re; - roots[i+5] = complex_roots[i].im; - } - - return makelist(roots,10*sizeof(double)); -} - -static PyMethodDef VBBLMethods[] = { - {"VBBinaryLensing_BinaryMagDark", (PyCFunction) VBBinaryLensing_BinaryMagDark_wrapper, METH_VARARGS, ""}, - {"VBBL_SG12_5", (PyCFunction) VBBL_SG12_5_wrapper, METH_VARARGS, ""}, - {NULL, NULL, 0, NULL} -}; - -static struct PyModuleDef VBBL = { - PyModuleDef_HEAD_INIT, - "VBBL", - "usage", - -1, - VBBLMethods -}; - -extern "C" { -PyMODINIT_FUNC PyInit_VBBL(void) { - return PyModule_Create(&VBBL); -} -} diff --git a/source/VBBL/vbbl_wrapper.cpp b/source/VBBL/vbbl_wrapper.cpp new file mode 100644 index 00000000..faeca240 --- /dev/null +++ b/source/VBBL/vbbl_wrapper.cpp @@ -0,0 +1,76 @@ +#define PY_SSIZE_T_CLEAN +#include + +#include "VBBinaryLensingLibrary.h" + +/* +Based on code written by Przemek Mroz +*/ + +static PyObject * +VBBinaryLensing_BinaryMagDark_wrapper(PyObject *self, PyObject *args) { + double a, q, y1, y2, RSv, a1, Tol, mag; + static VBBinaryLensing VBBL; + + if (!PyArg_ParseTuple(args, "ddddddd", &a, &q, &y1, &y2, &RSv, &a1, &Tol)) return NULL; + + mag = VBBL.BinaryMagDark(a, q, y1, y2, RSv, a1, Tol); + + return Py_BuildValue("d", mag); +} + +PyObject * makelist(double *array, size_t size) { + PyObject *l = PyList_New(size); + for (size_t i = 0; i != size; ++i) { + PyList_SET_ITEM(l,i,PyFloat_FromDouble(array[i])); + } + return l; +} + +static PyObject * +VBBL_SG12_5_wrapper(PyObject *self, PyObject *args) { + static VBBinaryLensing VBBL; + complex complex_poly[6], complex_roots[5]; + double roots[10]; + double p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11; + int i; + + if (!PyArg_ParseTuple(args, "dddddddddddd", &p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10, &p11)) return NULL; + + complex_poly[0] = complex(p0, p6); + complex_poly[1] = complex(p1, p7); + complex_poly[2] = complex(p2, p8); + complex_poly[3] = complex(p3, p9); + complex_poly[4] = complex(p4, p10); + complex_poly[5] = complex(p5, p11); + + VBBL.cmplx_roots_gen(complex_roots, complex_poly, 5, true, true); + + for (i=0; i<5; i++) { + roots[i] = complex_roots[i].re; + roots[i+5] = complex_roots[i].im; + } + + return makelist(roots, 10); +} + +static PyMethodDef VBBLMethods[] = { + {"VBBinaryLensing_BinaryMagDark", VBBinaryLensing_BinaryMagDark_wrapper, METH_VARARGS, "some notes here"}, + {"VBBL_SG12_5", VBBL_SG12_5_wrapper, METH_VARARGS, "some notes here"}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static struct PyModuleDef VBBLmodule = { + PyModuleDef_HEAD_INIT, + "VBBL", + NULL, + -1, + VBBLMethods +}; + +extern "C" { +PyMODINIT_FUNC PyInit_VBBL(void) { + return PyModule_Create(&VBBLmodule); +} +} +