Commit 4e3cd225 authored by jclamber's avatar jclamber

py_unsio in progress

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@167 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 9f413318
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2010-2013
# e-mail: Jean-Charles.Lambert@oamp.fr
# address: Dynamique des galaxies
# Centre de donneeS Astrophysique de Marseille (CeSAM)
# Laboratoire d'Astrophysique de Marseille
# Pole de l'Etoile, site de Chateau-Gombert
# 38, rue Frederic Joliot-Curie
# 13388 Marseille cedex 13 France
# CNRS U.M.R 6110
# ============================================================================
# CMakeListst.txt file for UNS python library
# ============================================================================
# This is a CMake example for Python
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.6)
# project name
project (py_unsio)
# extra path for NEMO
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake )
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/../cmake )
# load setup flags
include(SetupFlags)
# required package
FIND_PACKAGE(SWIG REQUIRED)
FIND_PACKAGE(NumPy REQUIRED)
FIND_PACKAGE(UNSIO REQUIRED)
FIND_PACKAGE(SQLITE3 REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
MESSAGE(STATUS "PYTHON_INCLUDE_PATH:" ${PYTHON_INCLUDE_PATH})
# some status
MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
MESSAGE( STATUS "CMAKE_BUILD_TYPE =" ${CMAKE_BUILD_TYPE} )
MESSAGE( STATUS "CMAKE_CXX_FLAGS =" ${CMAKE_CXX_FLAGS} )
MESSAGE( STATUS "LIBRARY TYPE =" ${LIBTYPE} )
MESSAGE( STATUS "core OPT =" ${OPT} )
MESSAGE( STATUS "OSX detected =" ${OSX} )
MESSAGE( STATUS "BUILD_TYPE =" ${RELEASE} )
MESSAGE(STATUS "PYTHON_INCLUDE_PATH:" ${PYTHON_INCLUDE_PATH})
MESSAGE(STATUS "PYTHON_INCLUDE_DIRS:" ${PYTHON_INCLUDE_DIRS})
MESSAGE(STATUS "PYTHON_LIBRARY :" ${PYTHON_LIBRARY})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/swig ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/../src ${NUMPY_INCLUDE_DIRS})
#/usr/lib64/python2.7/site-packages/numpy/core/include )
# Set definitions
set (INTERFACE swig/py_unsio.i)
......@@ -33,20 +62,30 @@ FILE(GLOB execpp_sources ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h )
SET(CMAKE_SWIG_FLAGS "-c++")
SET_SOURCE_FILES_PROPERTIES(${INTERFACE} PROPERTIES CPLUSPLUS ON)
#SET_SOURCE_FILES_PROPERTIES(${INTERFACE} PROPERTIES SWIG_FLAGS "-includeall")
#SET_SOURCE_FILES_PROPERTIES(${INTERFACE} PROPERTIES SWIG_FLAGS "-I/usr/include")
set(CMAKE_SWIG_OUTDIR ${PROJECT_BINARY_DIR}/swig)
#SWIG_ADD_MODULE(${MODULENAME} python ${INTERFACE} ${execpp_sources} )
SWIG_ADD_MODULE(${MODULENAME} python ${INTERFACE} ${execpp_sources} ${UNSIOPATH}/lib/libunsio.so ${UNSIOPATH}/lib/libnemo.so )
SWIG_LINK_LIBRARIES(${MODULENAME} ${PYTHON_LIBRARIES} ${UNSIOPATH}/lib/libunsio.so ${UNSIOPATH}/lib/libnemo.so ${SQLITE3_LIB_PATH}/libsqlite3.so )
# ----------------------------------------------------------
# Install SETUP
# ----------------------------------------------------------
SWIG_ADD_MODULE(${MODULENAME} python ${INTERFACE} ${execpp_sources} $ENV{NEMOLIB}/libunsio.so $ENV{NEMOLIB}/libnemo.so )
mark_as_advanced(CMAKE_INSTALL_PREFIX)
SET (NEMOLIB $ENV{NEMOLIB})
link_directories ( ${NEMOLIB} )
IF (OSX) # Mac OSX
SET(SOEXT "dylib")
ELSE (OSX) # Linux
SET(SOEXT "so")
ENDIF(OSX)
SWIG_LINK_LIBRARIES(${MODULENAME} ${PYTHON_LIBRARIES} $ENV{NEMOLIB}/libunsio.so $ENV{NEMOLIB}/libnemo.so /usr/lib64/libsqlite3.so )
set (CMAKE_INSTALL_PREFIX ${UNSIOPATH})
MESSAGE( STATUS "--------> CMAKE_INSTALL_PREFIX =" ${CMAKE_INSTALL_PREFIX})
INSTALL(FILES ${PROJECT_BINARY_DIR}/_${MODULENAME}.${SOEXT} DESTINATION py/modules)
INSTALL(FILES ${PROJECT_BINARY_DIR}/swig/${MODULENAME}.py DESTINATION py/modules)
#
......@@ -12,6 +12,7 @@
// Swig python interface for UNSIO
// ============================================================================
// python module name
%module py_unsio
%{
......@@ -20,22 +21,34 @@
%}
%include "numpy.i"
%include "std_string.i"
%init %{
import_array();
import_array();
%}
// getData return
%apply ( int* DIM1, float** ARGOUTVIEW_ARRAY1 )
{( int* size , float** farray )};
// getArrayX return 1D numpy array
%apply ( int* DIM1, float ** ARGOUTVIEW_ARRAY1 )
{( int* size, float ** farray )};
%apply ( int* DIM1, int ** ARGOUTVIEW_ARRAY1 )
{( int* size, int ** iarray )};
%init %{
// import_array();
%}
// getValueX return float/int value
%apply float *OUTPUT { float * fvalue };
%apply int *OUTPUT { int * ivalue };
// rename methods because of overloading limitations with swig c++
%rename(getValueF) getData(const std::string,float *);
%rename(getArrayF) getData(const std::string,const std::string,int *,float **);
%rename(getArrayF) getData(const std::string,int *,float **);
%rename(getValueI) getData(const std::string,int *);
%rename(getArrayI) getData(const std::string,const std::string,int *,int **);
%rename(getArrayI) getData(const std::string,int *,int **);
// Parse the original header file
%include "uns.h"
//
......@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
import scipy.interpolate
# Step 1 : create UNSIO object
file="gtr111" # input file (simulation name)
file="/home/jcl/gtr118_1912" # input file (simulation name)
component="disk,stars,gas" # selected components
times="6:10" # selected times
uns=CunsIn(file,component,times) # create UNS object
......@@ -14,11 +14,11 @@ while uns.nextFrame(""): # load snapshot
comp="gas"
# Step 3 : get data
ok,pos=uns.getData(comp,"pos") # positions of stars particles
ok,pos=uns.getArrayF(comp,"pos") # positions of stars particles
# Step 3 : get data
ok,rho=uns.getData(comp ,"rho") # gas density
ok,rho=uns.getArrayF(comp ,"rho") # gas density
# Step 3 : get data
ok,mass=uns.getData(comp ,"mass") # gas density
ok,mass=uns.getArrayF(comp ,"mass") # gas density
# reshape array in x,y,z arrays
pos=np.reshape(pos,(-1,3)) # pos reshaped in a 2D array [nbody,3]
......@@ -48,17 +48,21 @@ while uns.nextFrame(""): # load snapshot
ymin = y.min()
ymax = y.max()
meshs=2000
meshs=200
# Set up a regular grid of interpolation points
xi, yi = np.linspace(xmin, xmax, meshs), np.linspace(ymin, ymax, meshs)
xi, yi = np.meshgrid(xi, yi)
# Interpolate
zi = scipy.interpolate.griddata((x, y), rho,(xi, yi), method='linear')
print "Rho :",rho, rho.min(), rho.max()
print "Zi :", zi.min(), zi.max()
#CS = plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
#CS = plt.contourf(xi,yi,zi,15,cmap=plt.cm.jet)
#plt.imshow(zi, vmin=rho.min(), vmax=rho.max(), origin='lower', interpolation='bilinear',
plt.imshow(zi, vmin=zi.min(), vmax=zi.max(), origin='lower', interpolation='bilinear',extent=[xmin, xmax, ymin, ymax])
plt.imshow(zi, vmin=rho.min(), vmax=rho.max(), origin='lower', interpolation='bilinear',
extent=[xmin, xmax, ymin, ymax])
#plt.scatter(x, y, c=rho)
plt.colorbar()
plt.show()
......@@ -4,7 +4,7 @@ import matplotlib.pyplot as plt
import scipy.interpolate
# Step 1 : create UNSIO object
file="gtr111" # input file (simulation name)
file="/home/jcl/gtr118_1912" # input file (simulation name)
component="disk,stars,gas" # selected components
times="6:10" # selected times
uns=CunsIn(file,component,times) # create UNS object
......@@ -12,14 +12,22 @@ uns=CunsIn(file,component,times) # create UNS object
# Step 2 : load frame
while uns.nextFrame(""): # load snapshot
comp="disk"
comp="gas"
# Step 3 : get data
ok,pos=uns.getData(comp,"pos") # positions of stars particles
ok,pos =uns.getArrayF(comp,"pos") # positions of stars particles
# Step 3 : get data
ok,rho=uns.getData(comp ,"rho") # gas density
ok,rho =uns.getArrayF(comp,"rho") # gas density
# Step 3 : get data
ok,mass=uns.getData(comp ,"mass") # gas density
ok,mass=uns.getArrayF(comp,"mass") # gas density
ok,time=uns.getValueF("time");
print "time=",time
ok,id=uns.getArrayI(comp, "id")
print "ID=",id
ok,postot=uns.getArrayF("pos");
print "pos tot=", postot, postot.size, pos.size
# reshape array in x,y,z arrays
pos=np.reshape(pos,(-1,3)) # pos reshaped in a 2D array [nbody,3]
x = pos[:,0] # x coordinates
......@@ -27,7 +35,8 @@ while uns.nextFrame(""): # load snapshot
z = pos[:,2] # z coordinates
if (rho.size > 0) :
rho = np.log(rho)
#rho = np.log(rho)
print
else:
rho = z
......
......@@ -160,6 +160,70 @@ namespace uns {
snapshot = new CSnapshotList(simname, sel_comp, sel_time, verbose);
valid = snapshot->isValidData();
}
// ----------------------------------------------------------------------------
// nextFrame
int CunsIn::nextFrame(const char * _bits) {
std::string bits(_bits);
int ok=snapshot->nextFrame(bits);
return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string comp,const std::string prop,
int * size,float ** farray) {
float * data=NULL;
int nbody=0;
*size=0;
bool ok=snapshot->getData(comp,prop,&nbody,&data);
if (ok) {
int dim=1;
if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
*farray = data;
*size=nbody*dim;
}
return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string prop,
int * size,float ** farray) {
float * data=NULL;
int nbody=0;
*size=0;
bool ok=snapshot->getData(prop,&nbody,&data);
if (ok) {
int dim=1;
if (prop=="pos" || prop == "vel" || prop == "acc") dim=3;
*farray = data;
*size=nbody*dim;
}
return ok;
}
// ----------------------------------------------------------------------------
// getData
bool CunsIn::getData(const std::string prop,float * fvalue) {
bool ok=snapshot->getData(prop,fvalue);
return ok;
}
// ----------------------------------------------------------------------------
// getData
// int
bool CunsIn::getData(const std::string comp,const std::string prop,
int * size,int ** iarray) {
int * data=NULL;
int nbody=0;
*size=0;
bool ok=snapshot->getData(comp,prop,&nbody,&data);
if (ok) {
int dim=1;
*iarray = data;
*size=nbody*dim;
}
return ok;
}
// ----------------------------------------------------------------------------
// WRITING OPERATIONS
// ---------------------------------------------------------------------------
......@@ -180,7 +244,7 @@ namespace uns {
}
else {
std::cerr << "Unkonwn UNS output file format => ["<<simtype<<"]"
<< " aborting program...... \n\n";
<< " aborting program...... \n\n";
std::exit(1);
}
}
......@@ -247,7 +311,7 @@ namespace uns {
CunsOut::s_mapStringValues["czsmt" ] = uns::Czsmt;
if (verbose) {
std::cout << "CunsOut::initializeStringMap s_mapStringValues contains "
<< CunsOut::s_mapStringValues.size() << " entries." << std::endl;
<< CunsOut::s_mapStringValues.size() << " entries." << std::endl;
}
}
......
......@@ -85,10 +85,28 @@ namespace uns {
CunsIn(const std::string ,const std::string,const std::string, const bool verb=false );
~CunsIn();
bool isValid() { return valid;}
CSnapshotInterfaceIn * snapshot; // object to store data
uns::CSnapshotInterfaceIn * snapshot; // object to store data
// Map to associate component with a type
static std::map<std::string, int> s_mapCompInt;
//
// py wrapper
//
int nextFrame(const char * _bits);
bool getData(const std::string comp,const std::string prop,
int * size,float ** farray);
bool getData(const std::string prop,
int * size,float ** farray);
bool getData(const std::string prop,float * fvalue);
// int
bool getData(const std::string comp,const std::string prop,
int * size,int ** iarray);
// py wrapper
private:
void init(const std::string ,const std::string,const std::string, const bool verb=false );
......@@ -113,7 +131,7 @@ namespace uns {
CunsOut(const std::string, const std::string, const bool verb=false);
~CunsOut();
bool isValid() { return valid;};
CSnapshotInterfaceOut * snapshot; // object to store data
uns::CSnapshotInterfaceOut * snapshot; // object to store data
// Map to associate the strings with the enum values
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment