From 8b9b72ca82dfbbcb7b1f6ef155a57a8b99e5edbe Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Thu, 20 Nov 2014 14:04:17 +0100 Subject: [PATCH] Splitted big pyx into sub-pyx --- python/CMakeLists.txt | 32 ++++++-- python/_cosmo_cic.pyx | 66 +++++++++++++++ python/_cosmo_power.pyx | 107 ++++++++++++++++++++++++ python/_cosmotool.pyx | 155 ----------------------------------- python/cosmotool/__init__.py | 9 +- 5 files changed, 204 insertions(+), 165 deletions(-) create mode 100644 python/_cosmo_cic.pyx create mode 100644 python/_cosmo_power.pyx diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 5bc6783..3b8f206 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -3,25 +3,39 @@ set(CMAKE_SHARED_MODULE_PREFIX) include_directories(${NUMPY_INCLUDE_DIRS} ${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY_DIR}/src ${CMAKE_SOURCE_DIR}/python) IF(CYTHON) -add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_cosmotool.cpp - COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_cosmotool.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_cosmotool.pyx - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_cosmotool.pyx) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_cosmotool.cpp + COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_cosmotool.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_cosmotool.pyx + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_cosmotool.pyx) -add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp - COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_project.pyx - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_project.pyx ${CMAKE_CURRENT_SOURCE_DIR}/project_tool.hpp ) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_power.cpp + COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_power.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_cosmo_power.pyx + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_cosmo_power.pyx) + + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_cic.cpp + COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_cic.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_cosmo_cic.pyx + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_cosmo_cic.pyx) + + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp + COMMAND ${CYTHON} --cplus -o ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp ${CMAKE_CURRENT_SOURCE_DIR}/_project.pyx + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_project.pyx ${CMAKE_CURRENT_SOURCE_DIR}/project_tool.hpp ) ENDIF(CYTHON) add_library(_cosmotool MODULE ${CMAKE_CURRENT_BINARY_DIR}/_cosmotool.cpp) +add_library(_cosmo_power MODULE ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_power.cpp) +add_library(_cosmo_cic MODULE ${CMAKE_CURRENT_BINARY_DIR}/_cosmo_cic.cpp) add_library(_project MODULE ${CMAKE_CURRENT_BINARY_DIR}/_project.cpp) SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Bsymbolic-functions") target_link_libraries(_cosmotool ${CosmoTool_local} ${PYTHON_LIBRARIES} ${GSL_LIBRARIES}) +target_link_libraries(_cosmo_power ${CosmoTool_local} ${PYTHON_LIBRARIES} ${GSL_LIBRARIES}) +target_link_libraries(_cosmo_cic ${CosmoTool_local} ${PYTHON_LIBRARIES} ${GSL_LIBRARIES}) target_link_libraries(_project ${PYTHON_LIBRARIES}) # Discover where to put packages @@ -48,7 +62,7 @@ if (WIN32 AND NOT CYGWIN) SET_TARGET_PROPERTIES(_cosmotool PROPERTIES SUFFIX ".pyd") endif (WIN32 AND NOT CYGWIN) -INSTALL(TARGETS _cosmotool _project +INSTALL(TARGETS _cosmotool _project _cosmo_power _cosmo_cic LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}/cosmotool ) diff --git a/python/_cosmo_cic.pyx b/python/_cosmo_cic.pyx new file mode 100644 index 0000000..76a540a --- /dev/null +++ b/python/_cosmo_cic.pyx @@ -0,0 +1,66 @@ +from libcpp cimport bool +from libcpp cimport string as cppstring +import numpy as np +cimport numpy as np +from cpython cimport PyObject, Py_INCREF +cimport cython + +np.import_array() + +cdef extern from "cic.hpp" namespace "CosmoTool": + + ctypedef float CICType + ctypedef float Coordinates[3] + + cdef cppclass CICParticles: + float mass + Coordinates coords + + cdef cppclass CICFilter: + + CICFilter(np.uint32_t resolution, double L) nogil + + void resetMesh() nogil + void putParticles(CICParticles* particles, np.uint32_t N) nogil + void getDensityField(CICType *& field, np.uint32_t& res) nogil + + +@cython.boundscheck(False) +@cython.cdivision(True) +@cython.wraparound(False) +def leanCic(float[:,:] particles, float L, int Resolution): + cdef CICParticles p + cdef CICFilter *cic + cdef np.uint64_t i + cdef CICType *field + cdef np.uint32_t dummyRes + cdef np.ndarray[np.float64_t, ndim=3] out_field + cdef np.ndarray[np.float64_t, ndim=1] out_field0 + cdef np.float64_t[:] out_field_buf + cdef np.uint64_t j + + cic = new CICFilter(Resolution, L) + cic.resetMesh() + + if particles.shape[1] != 3: + raise ValueError("Particles must be Nx3 array") + + p.mass = 1 + for i in xrange(particles.shape[0]): + p.coords[0] = particles[i,0] + p.coords[1] = particles[i,1] + p.coords[2] = particles[i,2] + cic.putParticles(&p, 1) + + field = 0 + dummyRes = 0 + cic.getDensityField(field, dummyRes) + + out_field = np.empty((dummyRes, dummyRes, dummyRes), dtype=np.float64) + out_field0 = out_field.reshape(out_field.size) + out_field_buf = out_field + for j in xrange(out_field_buf.size): + out_field_buf[j] = field[j] + + del cic + return out_field diff --git a/python/_cosmo_power.pyx b/python/_cosmo_power.pyx new file mode 100644 index 0000000..4aebb15 --- /dev/null +++ b/python/_cosmo_power.pyx @@ -0,0 +1,107 @@ +from libcpp cimport bool +from libcpp cimport string as cppstring +import numpy as np +cimport numpy as np +from cpython cimport PyObject, Py_INCREF +cimport cython + +np.import_array() + +cdef extern from "cosmopower.hpp" namespace "CosmoTool": + + cdef enum CosmoFunction "CosmoTool::CosmoPower::CosmoFunction": + POWER_EFSTATHIOU "CosmoTool::CosmoPower::POWER_EFSTATHIOU", + HU_WIGGLES "CosmoTool::CosmoPower::HU_WIGGLES", + HU_BARYON "CosmoTool::CosmoPower::HU_BARYON", + OLD_POWERSPECTRUM, + POWER_BARDEEN "CosmoTool::CosmoPower::POWER_BARDEEN", + POWER_SUGIYAMA "CosmoTool::CosmoPower::POWER_SUGIYAMA", + POWER_BDM, + POWER_TEST + + cdef cppclass CosmoPower: + double n + double K0 + double V_LG_CMB + + double CMB_VECTOR[3] + double h + double SIGMA8 + double OMEGA_B + double OMEGA_C + double omega_B + double omega_C + double Theta_27 + double OMEGA_0 + double Omega + double beta + double OmegaEff + double Gamma0 + double normPower + + + CosmoPower() + void setFunction(CosmoFunction) + void updateCosmology() + void updatePhysicalCosmology() + void normalize() + void setNormalization(double) + double power(double) + +cdef class CosmologyPower: + + cdef CosmoPower power + + def __init__(self,**cosmo): + + self.power = CosmoPower() + self.power.OMEGA_B = cosmo['omega_B_0'] + self.power.OMEGA_C = cosmo['omega_M_0']-cosmo['omega_B_0'] + self.power.h = cosmo['h'] + if 'ns' in cosmo: + self.power.n = cosmo['ns'] + + assert self.power.OMEGA_C > 0 + + self.power.updateCosmology() + + def normalize(self,s8): + self.power.SIGMA8 = s8 + self.power.normalize() + + + def setFunction(self,funcname): + cdef CosmoFunction f + + f = POWER_EFSTATHIOU + + if funcname=='EFSTATHIOU': + f = POWER_EFSTATHIOU + elif funcname=='HU_WIGGLES': + f = HU_WIGGLES + elif funcname=='HU_BARYON': + f = HU_BARYON + elif funcname=='BARDEEN': + f = POWER_BARDEEN + elif funcname=='SUGIYAMA': + f = POWER_SUGIYAMA + + self.power.setFunction(f) + + cdef double _compute(self, double k): + k *= self.power.h + return self.power.power(k) + + def compute(self, k): + cdef np.ndarray out + cdef double kval + cdef tuple i + + if isinstance(k, np.ndarray): + out = np.empty(k.shape, dtype=np.float64) + for i,kval in np.ndenumerate(k): + out[i] = self._compute(kval) + return out + else: + return self._compute(k) + diff --git a/python/_cosmotool.pyx b/python/_cosmotool.pyx index 0b94123..380f146 100644 --- a/python/_cosmotool.pyx +++ b/python/_cosmotool.pyx @@ -7,64 +7,6 @@ cimport cython np.import_array() -cdef extern from "cosmopower.hpp" namespace "CosmoTool": - - cdef enum CosmoFunction "CosmoTool::CosmoPower::CosmoFunction": - POWER_EFSTATHIOU "CosmoTool::CosmoPower::POWER_EFSTATHIOU", - HU_WIGGLES "CosmoTool::CosmoPower::HU_WIGGLES", - HU_BARYON "CosmoTool::CosmoPower::HU_BARYON", - OLD_POWERSPECTRUM, - POWER_BARDEEN "CosmoTool::CosmoPower::POWER_BARDEEN", - POWER_SUGIYAMA "CosmoTool::CosmoPower::POWER_SUGIYAMA", - POWER_BDM, - POWER_TEST - - cdef cppclass CosmoPower: - double n - double K0 - double V_LG_CMB - - double CMB_VECTOR[3] - double h - double SIGMA8 - double OMEGA_B - double OMEGA_C - double omega_B - double omega_C - double Theta_27 - double OMEGA_0 - double Omega - double beta - double OmegaEff - double Gamma0 - double normPower - - - CosmoPower() - void setFunction(CosmoFunction) - void updateCosmology() - void updatePhysicalCosmology() - void normalize() - void setNormalization(double) - double power(double) - -cdef extern from "cic.hpp" namespace "CosmoTool": - - ctypedef float CICType - ctypedef float Coordinates[3] - - cdef cppclass CICParticles: - float mass - Coordinates coords - - cdef cppclass CICFilter: - - CICFilter(np.uint32_t resolution, double L) nogil - - void resetMesh() nogil - void putParticles(CICParticles* particles, np.uint32_t N) nogil - void getDensityField(CICType *& field, np.uint32_t& res) nogil - cdef extern from "loadSimu.hpp" namespace "CosmoTool": cdef cppclass SimuData: @@ -409,100 +351,3 @@ def loadRamses(str basepath, int snapshot_id, int cpu_id, bool doublePrecision = return _PySimulationAdaptor(wrap_simudata(data, flags)) - -cdef class CosmologyPower: - - cdef CosmoPower power - - def __init__(self,**cosmo): - - self.power = CosmoPower() - self.power.OMEGA_B = cosmo['omega_B_0'] - self.power.OMEGA_C = cosmo['omega_M_0']-cosmo['omega_B_0'] - self.power.h = cosmo['h'] - if 'ns' in cosmo: - self.power.n = cosmo['ns'] - - assert self.power.OMEGA_C > 0 - - self.power.updateCosmology() - - def normalize(self,s8): - self.power.SIGMA8 = s8 - self.power.normalize() - - - def setFunction(self,funcname): - cdef CosmoFunction f - - f = POWER_EFSTATHIOU - - if funcname=='EFSTATHIOU': - f = POWER_EFSTATHIOU - elif funcname=='HU_WIGGLES': - f = HU_WIGGLES - elif funcname=='HU_BARYON': - f = HU_BARYON - elif funcname=='BARDEEN': - f = POWER_BARDEEN - elif funcname=='SUGIYAMA': - f = POWER_SUGIYAMA - - self.power.setFunction(f) - - cdef double _compute(self, double k): - k *= self.power.h - return self.power.power(k) - - def compute(self, k): - cdef np.ndarray out - cdef double kval - cdef tuple i - - if isinstance(k, np.ndarray): - out = np.empty(k.shape, dtype=np.float64) - for i,kval in np.ndenumerate(k): - out[i] = self._compute(kval) - return out - else: - return self._compute(k) - -@cython.boundscheck(False) -@cython.cdivision(True) -@cython.wraparound(False) -def leanCic(float[:,:] particles, float L, int Resolution): - cdef CICParticles p - cdef CICFilter *cic - cdef np.uint64_t i - cdef CICType *field - cdef np.uint32_t dummyRes - cdef np.ndarray[np.float64_t, ndim=3] out_field - cdef np.ndarray[np.float64_t, ndim=1] out_field0 - cdef np.float64_t[:] out_field_buf - cdef np.uint64_t j - - cic = new CICFilter(Resolution, L) - cic.resetMesh() - - if particles.shape[1] != 3: - raise ValueError("Particles must be Nx3 array") - - p.mass = 1 - for i in xrange(particles.shape[0]): - p.coords[0] = particles[i,0] - p.coords[1] = particles[i,1] - p.coords[2] = particles[i,2] - cic.putParticles(&p, 1) - - field = 0 - dummyRes = 0 - cic.getDensityField(field, dummyRes) - - out_field = np.empty((dummyRes, dummyRes, dummyRes), dtype=np.float64) - out_field0 = out_field.reshape(out_field.size) - out_field_buf = out_field - for j in xrange(out_field_buf.size): - out_field_buf[j] = field[j] - - del cic - return out_field diff --git a/python/cosmotool/__init__.py b/python/cosmotool/__init__.py index a25b6a7..6a6485b 100644 --- a/python/cosmotool/__init__.py +++ b/python/cosmotool/__init__.py @@ -1,9 +1,16 @@ from _cosmotool import * from _project import * +from _cosmo_power import * +from _cosmo_cic import * from .grafic import writeGrafic, writeWhitePhase, readGrafic, readWhitePhase from .borg import read_borg_vol from .cic import cicParticles -from .cl_cic import cl_CIC_Density +try: + import pyopencl + from .cl_cic import cl_CIC_Density +except: + print("No opencl support") + from .simu import loadRamsesAll, simpleWriteGadget, SimulationBare from .timing import time_block, timeit, timeit_quiet