diff --git a/FindNumPy.cmake b/FindNumPy.cmake new file mode 100644 index 0000000..eafed16 --- /dev/null +++ b/FindNumPy.cmake @@ -0,0 +1,102 @@ +# - Find the NumPy libraries +# This module finds if NumPy is installed, and sets the following variables +# indicating where it is. +# +# TODO: Update to provide the libraries and paths for linking npymath lib. +# +# NUMPY_FOUND - was NumPy found +# NUMPY_VERSION - the version of NumPy found as a string +# NUMPY_VERSION_MAJOR - the major version number of NumPy +# NUMPY_VERSION_MINOR - the minor version number of NumPy +# NUMPY_VERSION_PATCH - the patch version number of NumPy +# NUMPY_VERSION_DECIMAL - e.g. version 1.6.1 is 10601 +# NUMPY_INCLUDE_DIRS - path to the NumPy include files + +#============================================================================ +# Copyright 2012 Continuum Analytics, Inc. +# +# MIT License +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files +# (the "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to permit +# persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +# +#============================================================================ + +# Finding NumPy involves calling the Python interpreter +if(NumPy_FIND_REQUIRED) + find_package(PythonInterp REQUIRED) +else() + find_package(PythonInterp) +endif() + +if(NOT PYTHONINTERP_FOUND) + set(NUMPY_FOUND FALSE) + return() +endif() + +execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" + "import numpy as n; print(n.__version__); print(n.get_include());" + RESULT_VARIABLE _NUMPY_SEARCH_SUCCESS + OUTPUT_VARIABLE _NUMPY_VALUES_OUTPUT + ERROR_VARIABLE _NUMPY_ERROR_VALUE + OUTPUT_STRIP_TRAILING_WHITESPACE) + +if(NOT _NUMPY_SEARCH_SUCCESS MATCHES 0) + if(NumPy_FIND_REQUIRED) + message(FATAL_ERROR + "NumPy import failure:\n${_NUMPY_ERROR_VALUE}") + endif() + set(NUMPY_FOUND FALSE) + return() +endif() + +# Convert the process output into a list +string(REGEX REPLACE ";" "\\\\;" _NUMPY_VALUES ${_NUMPY_VALUES_OUTPUT}) +string(REGEX REPLACE "\n" ";" _NUMPY_VALUES ${_NUMPY_VALUES}) +# Just in case there is unexpected output from the Python command. +list(GET _NUMPY_VALUES -2 NUMPY_VERSION) +list(GET _NUMPY_VALUES -1 NUMPY_INCLUDE_DIRS) + +string(REGEX MATCH "^[0-9]+\\.[0-9]+\\.[0-9]+" _VER_CHECK "${NUMPY_VERSION}") +if("${_VER_CHECK}" STREQUAL "") + # The output from Python was unexpected. Raise an error always + # here, because we found NumPy, but it appears to be corrupted somehow. + message(FATAL_ERROR + "Requested version and include path from NumPy, got instead:\n${_NUMPY_VALUES_OUTPUT}\n") + return() +endif() + +# Make sure all directory separators are '/' +string(REGEX REPLACE "\\\\" "/" NUMPY_INCLUDE_DIRS ${NUMPY_INCLUDE_DIRS}) + +# Get the major and minor version numbers +string(REGEX REPLACE "\\." ";" _NUMPY_VERSION_LIST ${NUMPY_VERSION}) +list(GET _NUMPY_VERSION_LIST 0 NUMPY_VERSION_MAJOR) +list(GET _NUMPY_VERSION_LIST 1 NUMPY_VERSION_MINOR) +list(GET _NUMPY_VERSION_LIST 2 NUMPY_VERSION_PATCH) +string(REGEX MATCH "[0-9]*" NUMPY_VERSION_PATCH ${NUMPY_VERSION_PATCH}) +math(EXPR NUMPY_VERSION_DECIMAL + "(${NUMPY_VERSION_MAJOR} * 10000) + (${NUMPY_VERSION_MINOR} * 100) + ${NUMPY_VERSION_PATCH}") + +find_package_message(NUMPY + "Found NumPy: version \"${NUMPY_VERSION}\" ${NUMPY_INCLUDE_DIRS}" + "${NUMPY_INCLUDE_DIRS}${NUMPY_VERSION}") + +set(NUMPY_FOUND TRUE) + diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index c3138a2..e7d4653 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -42,7 +42,7 @@ if (WIN32 AND NOT CYGWIN) endif (WIN32 AND NOT CYGWIN) INSTALL(TARGETS _cosmotool - LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/flints + LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/cosmotool ) INSTALL(DIRECTORY cosmotool DESTINATION ${PYTHON_SITE_PACKAGES} diff --git a/python/_cosmotool.pyx b/python/_cosmotool.pyx index d745876..488171b 100644 --- a/python/_cosmotool.pyx +++ b/python/_cosmotool.pyx @@ -1,6 +1,9 @@ from libcpp cimport bool import numpy as np cimport numpy as np +from cpython cimport PyObject, Py_INCREF + +np.import_array() cdef extern from "loadSimu.hpp" namespace "CosmoTool": @@ -12,11 +15,11 @@ cdef extern from "loadSimu.hpp" namespace "CosmoTool": np.float_t Omega_M np.float_t Omega_Lambda - np.int64_t TotalNumParticles - np.int64_t NumParticles + np.int64_t TotalNumPart + np.int64_t NumPart np.int64_t *Id - np.float_t *Pos[3] - np.float_t *Vel[3] + float *Pos[3] + float *Vel[3] int *type cdef const int NEED_GADGET_ID @@ -29,23 +32,94 @@ cdef extern from "loadGadget.hpp" namespace "CosmoTool": SimuData *loadGadgetMulti(const char *fname, int id, int flags) except + - cdef class Simulation: - cdef float BoxSize - cdef float Hubble - cdef list Position - cdef list Velocities + cdef list positions + cdef list velocities cdef SimuData *data + property BoxSize: + def __get__(Simulation self): + return self.data.BoxSize + + property Hubble: + def __get__(Simulation self): + return self.data.Hubble + + property Omega_M: + def __get__(Simulation self): + return self.data.Omega_M + + property positions: + def __get__(Simulation self): + return self.positions + + property velocities: + def __get__(Simulation self): + return self.velocities + + property numParticles: + def __get__(Simulation self): + return self.data.NumPart + def __cinit__(Simulation self): self.data = 0 def __dealloc__(Simulation self): if self.data != 0: + print("Clearing simulation data") del self.data + +cdef class ArrayWrapper: + cdef void* data_ptr + cdef int size + + cdef set_data(self, int size, void* data_ptr): + """ Set the data of the array + +This cannot be done in the constructor as it must recieve C-level +arguments. + +Parameters: +----------- +size: int +Length of the array. +data_ptr: void* +Pointer to the data + + """ + self.data_ptr = data_ptr + self.size = size + + def __array__(self): + """ Here we use the __array__ method, that is called when numpy +tries to get an array from the object.""" + cdef np.npy_intp shape[1] + + shape[0] = self.size + # Create a 1D array, of length 'size' + ndarray = np.PyArray_SimpleNewFromData(1, shape, np.NPY_FLOAT, self.data_ptr) + return ndarray + + def __dealloc__(self): + """ Frees the array. This is called by Python when all the +references to the object are gone. """ + pass + +cdef object wrap_float_array(float *p, np.uint64_t s): + cdef np.ndarray ndarray + cdef ArrayWrapper wrapper + + wrapper = ArrayWrapper() + wrapper.set_data(s, p) + ndarray = np.array(wrapper, copy=False) + ndarray.base = wrapper + Py_INCREF(wrapper) + + return ndarray + def loadGadget(str filename, int snapshot_id, bool loadPosition = True, bool loadVelocity = True): cdef int flags @@ -62,4 +136,9 @@ def loadGadget(str filename, int snapshot_id, bool loadPosition = True, bool loa simu = Simulation() simu.data = data + if loadPosition: + simu.positions = [wrap_float_array(data.Pos[i], data.NumPart) for i in xrange(3)] + if loadVelocity: + simu.velocities = [wrap_float_array(data.Vel[i], data.NumPart) for i in xrange(3)] + return simu diff --git a/python/cosmotool/__init__.py b/python/cosmotool/__init__.py new file mode 100644 index 0000000..70fc1f1 --- /dev/null +++ b/python/cosmotool/__init__.py @@ -0,0 +1 @@ +from _cosmotool import *