Commit a08c5ca9 authored by jclamber's avatar jclamber

template to develop programs based on uns

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@131 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 89d2ad37
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2010-2012
# 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 library
# ============================================================================
cmake_minimum_required(VERSION 2.6)
# project name
project (uns_devel)
# extra path for NEMO
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH} )
MESSAGE( STATUS "CMAKE_MODULE_PATH=" ${CMAKE_MODULE_PATH} )
# load setup flags
include(SetupFlags)
# require NEMO
FIND_PACKAGE(NEMO REQUIRED)
# require UNSIO
FIND_PACKAGE(UNSIO REQUIRED)
# Sqlite3
FIND_PACKAGE(SQLITE3)
# Check fortran compiler
include(CheckFortranCompiler)
# contains the full path to the top level directory of your build tree
MESSAGE( STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR} )
# contains the full path to the root of your project source directory,
# i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command
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 "UNSIOPATH =" ${UNSIOPATH} )
# Boost (deprecated)
#FIND_PACKAGE(BOOST)
#FIND_PACKAGE(NEMO REQUIRED)
MESSAGE( STATUS "NEMOLIB : " ${NEMOLIB} )
# contains the full path to the top level directory of your build tree
MESSAGE( STATUS "PROJECT_BINARY_DIR: " ${PROJECT_BINARY_DIR} )
#MESSAGE ( STATUS "Boost_LIBRARY_DIRS : " ${Boost_LIBRARY_DIRS})
# contains the full path to the root of your project source directory,
# i.e. to the nearest directory where CMakeLists.txt contains the PROJECT() command
MESSAGE( STATUS "PROJECT_SOURCE_DIR: " ${PROJECT_SOURCE_DIR} )
# Find all the sources for the utils LIB
if (CMAKE_Fortran_COMPILER)
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/src/*.cc ${PROJECT_SOURCE_DIR}/lib/src/*.c ${PROJECT_SOURCE_DIR}/lib/src/*.F )
else ()
FILE(GLOB LIBUTILS ${PROJECT_SOURCE_DIR}/lib/src/*.cc ${PROJECT_SOURCE_DIR}/lib/src/*.c)
endif ()
# create the library "MYutils"
add_library (MYutils SHARED ${LIBUTILS})
if(OSX)
set_target_properties(JCLutils PROPERTIES LINK_FLAGS "-undefined suppress -flat_namespace")
endif(OSX)
# Destination path for the lib
SET(LIBRARY_OUTPUT_PATH lib)
# compilation options
#add_definitions(${OPT} -DNO_CUDA -DfalcON_NEMO -DfalcON_SINGLE -funroll-loops -Wall ${DNOBOOST})
add_definitions(${OPT} -DNO_CUDA -DfalcON_NEMO -DfalcON_SINGLE -Wall ${DNOBOOST})
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${WARNCPP} )
SET(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${WARNC} )
SET(CMAKE_Fortran_FLAGS ${CMAKE_Fortran_FLAGS} ${WARNF})
# remove -DNDEBUG in RElease mode to keep activated assert calls
STRING(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
#message (STATUS "CMAKE_CXX_FLAGS_RELEASE = " ${CMAKE_CXX_FLAGS_RELEASE} )
# Executbale output path
set(EXECUTABLE_OUTPUT_PATH bin)
MESSAGE( STATUS "EXECUTABLE_OUTPUT_PATH: " ${EXECUTABLE_OUTPUT_PATH} )
# Make sure the compiler can find include files from our Hello library.
include_directories (${PROJECT_SOURCE_DIR}/src )
include_directories (${PROJECT_SOURCE_DIR}/lib/src)
include_directories (${UNSIOPATH}/lib ${UNSIOPATH}/inc ${UNSIOPATH}/inc/uns)
# Make sure the linker can find libraries once it is built.
link_directories ( ${UNSIOPATH}/lib $ENV{PGPLOT_DIR} ${G2C_DIR} ${PROJECT_BINARY_DIR}/lib /usr/lib64 /usr/X11/lib ${FC_GFORT_PATH} ${FC_G77_PATH})
# Find all c/c++ exes sources files
FILE(GLOB execpp_sources ${PROJECT_SOURCE_DIR}/src/*.cc ${PROJECT_SOURCE_DIR}/src/*.c)
# build cpp executables according to the source
FOREACH(exe_cpp ${execpp_sources})
get_filename_component(exe ${exe_cpp} NAME_WE)
MESSAGE( STATUS "New executable ==> " ${exe})
add_executable (${exe} ${exe_cpp})
#
target_link_libraries (${exe} MYutils ${FC_GFORT_LIB} ${FC_G77_LIB} unsio nemo ${SQLITE3_LIB} dl)
ENDFOREACH(exe_cpp ${execpp_sources})
#
# find all fortran source files
#
if (CMAKE_Fortran_COMPILER)
# Find all LIB's sources files
FILE(GLOB exe_sources ${PROJECT_SOURCE_DIR}/src/*.F)
FOREACH(exe_f ${exe_sources})
get_filename_component(exe ${exe_f} NAME_WE)
MESSAGE( STATUS "New executable ==> " ${exe})
add_executable (${exe} ${exe_f})
# Link the executable to the Hello library.
#target_link_libraries (${exe} unsio nemomaing77 nemo g2c sqlite3 stdc++)
target_link_libraries (${exe} MYutils unsio nemomaing77 nemo ${SQLITE3_LIB} ${FC_LIB} stdc++)
SET_TARGET_PROPERTIES(${exe} PROPERTIES LINKER_LANGUAGE Fortran)
# add "-lstdc++"
#SET(CMAKE_Fortran_LINK_FLAGS "-L${NEMOLIB} ") #-lstdc++")
ENDFOREACH(exe_f ${exe_sources})
endif()
# ----------------------------------------------------------
# install target
#set (CMAKE_INSTALL_PREFIX ${UNSIOPATH})
mark_as_advanced(CMAKE_INSTALL_PREFIX)
IF (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
IF (NEMO_INSTALLED)
set (CMAKE_INSTALL_PREFIX "$ENV{NEMO}" CACHE PATH "" FORCE) #if NEMO exist => default install into NEMO path
ELSE()
set (CMAKE_INSTALL_PREFIX ${UNSIOPATH} CACHE PATH "" FORCE) #if NEMO NOT exist => default install into ${HOME}/local/unsio
ENDIF()
ENDIF ()#NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
MESSAGE( STATUS "--------> CMAKE_INSTALL_PREFIX =" ${CMAKE_INSTALL_PREFIX})
IF (OSX) # Mac OSX
SET(SOEXT "dylib")
ELSE (OSX) # Linux
SET(SOEXT "so")
ENDIF(OSX)
#
# install bin targets c/c++
FOREACH(exe_cpp ${execpp_sources})
get_filename_component(exe ${exe_cpp} NAME_WE)
INSTALL(PROGRAMS ${PROJECT_BINARY_DIR}/bin/${exe} DESTINATION bin)
ENDFOREACH(exe_cpp ${execpp_sources})
#
# install bin targets Fortran
FOREACH(exe_f ${exe_sources})
get_filename_component(exe ${exe_f} NAME_WE)
INSTALL(PROGRAMS ${PROJECT_BINARY_DIR}/bin/${exe} DESTINATION bin)
ENDFOREACH(exe_f ${exe_sources})
INSTALL(FILES ${PROJECT_BINARY_DIR}/lib/libMYutils.${SOEXT} DESTINATION lib)
#
You need to install CMAKE utility
(Cf http://www.cmake.org/)
1) put your main sources into "src" directory
2) if you want to make a library, put files into lib/src
3) to compile
mkdir build
cd build
cmake ..
make
binaries will be located in "bin" directory
4) to install
make install
(binaries will be copied in ${HOME}/local/unsio/bin )
# check if fortran compiler is installed
if(DEFINED CMAKE_Fortran_COMPILER AND CMAKE_Fortran_COMPILER MATCHES "^$")
set(CMAKE_Fortran_COMPILER CMAKE_Fortran_COMPILER-NOTFOUND)
endif(DEFINED CMAKE_Fortran_COMPILER AND CMAKE_Fortran_COMPILER MATCHES "^$")
enable_language(Fortran OPTIONAL)
message(STATUS "CMAKE_Fortran_COMPILER_WORKS = ${CMAKE_Fortran_COMPILER_WORKS}")
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2009-2012
# 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
# ============================================================================
# CMake module to detect SQLITE3 library
# ============================================================================
# require boost
find_package( Boost 1.36.0)
set(LIBBOOST boost_thread-mt.a boost_system-mt.a)
set(Boost_FOUND FALSE)
set(DNOBOOST "")
if(NOT Boost_FOUND)
MESSAGE (STATUS " Boost not found, uns_2dplot will run slowly.....")
set(DNOBOOST "-DNOBOOST")
set(LIBBOOST "")
endif(NOT Boost_FOUND)
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2009-2012
# 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 nemo library
# ============================================================================
SET(NEMO_INSTALLED FALSE)
FILE(GLOB GLOB_TEMP_VAR $ENV{NEMO})
IF(GLOB_TEMP_VAR)
SET(NEMO_INSTALLED TRUE)
# Detect fortran compiler from ${NEMOLIB}/makedefs
# files (variable FC = )
FILE(READ $ENV{NEMOLIB}/makedefs MAKEDEFS)
STRING(REGEX MATCH "FC = gfortran" GFORTRAN_IS_SET ${MAKEDEFS})
IF ("FC = gfortran" STREQUAL "${GFORTRAN_IS_SET}")
MESSAGE("GFortran compiler Detected......")
SET (FORT_COMP gfortran)
ELSE("FC = gfortran" STREQUAL "${GFORTRAN_IS_SET}")
# g77 stuffs
MESSAGE("Assuming G77 compiler")
SET (FORT_COMP g77)
ENDIF ("FC = gfortran" STREQUAL "${GFORTRAN_IS_SET}")
# gfortran stuffs
SET (FC_GFORT_LIB "")
SET (FC_GFORT_PATH "")
set (GFORTPATH "")
execute_process(COMMAND gfortran -print-file-name=libgfortran.a
OUTPUT_VARIABLE GFORTPATH OUTPUT_STRIP_TRAILING_WHITESPACE)
IF (NOT ${GFORTPATH} STREQUAL "")
MESSAGE("GFORTRAN compiler exist, adding libgfortran......")
execute_process(COMMAND dirname ${GFORTPATH}
OUTPUT_VARIABLE FC_GFORT_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
SET (FC_GFORT_LIB gfortran)
ENDIF(NOT ${GFORTPATH} STREQUAL "")
# g77 stuffs
SET (FC_G77_LIB "")
SET (FC_G77_PATH "")
set (G2CFPATH "")
execute_process(COMMAND g77 -print-file-name=libg2c.a
OUTPUT_VARIABLE G2CFPATH OUTPUT_STRIP_TRAILING_WHITESPACE)
IF (NOT ${G2CFPATH} STREQUAL "")
MESSAGE("G77 compiler exist, adding libg2c......")
execute_process(COMMAND dirname ${G2CFPATH}
OUTPUT_VARIABLE FC_G77_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
SET (FC_G77_LIB g2c)
ENDIF(NOT ${G2CFPATH} STREQUAL "")
ENDIF(GLOB_TEMP_VAR)
if (NOT NEMO_INSTALLED)
MESSAGE(STATUS "NEMO environement not loaded.... using nemo_light")
else () # try to find dehnen's stuff
find_library(ISFALCONLIB NAMES falcON PATHS $ENV{NEMO}/usr/dehnen/falcON/lib)
if (ISFALCONLIB)
MESSAGE(STATUS "Found Falcon lib :" ${ISFALCONLIB} )
set (FALCON_INSTALLED TRUE)
set (FALCON_LIB_PATH $ENV{NEMO}/usr/dehnen/falcON)
set (FALCON_UTIL_PATH $ENV{NEMO}/usr/dehnen/utils)
set(DEHNEN $ENV{NEMO}/usr/dehnen)
else ()
set (FALCON_INSTALLED FALSE)
endif()
endif (NOT NEMO_INSTALLED)
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2009-2012
# 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
# ============================================================================
# CMake module to detect SQLITE3 library
# ============================================================================
SET(SQLITE3_FOUND FALSE)
SET(DNOSQLITE3 "-DNOSQLITE3") # if sqlite3 does not exist
SET(SQLITE3_LIB_PATH "")
SET(SQLITE3_H_PATH "")
SET(SQLITE3_LIB "")
find_file(SQLITE3_H NAMES "sqlite3.h" PATH /usr/local/include)
IF (SQLITE3_H)
get_filename_component(SQLITE3_H_PATH ${SQLITE3_H} PATH)
MESSAGE(STATUS "Found sqlite3.h:" ${SQLITE3_H})
MESSAGE(STATUS "Found sqlite3.h path:" ${SQLITE3_H_PATH})
find_library(SQLITE3 NAMES sqlite3 PATH /usr/local/lib)
IF (SQLITE3)
SET(SQLITE3_FOUND TRUE)
SET(DNOSQLITE3 "") # SQLITE3 exist
SET(SQLITE3_LIB sqlite3)
MESSAGE(STATUS "Found library here :" ${SQLITE3})
get_filename_component(SQLITE3_LIB_PATH ${SQLITE3} PATH)
MESSAGE(STATUS "Found library PATH :" ${SQLITE3_LIB_PATH})
ENDIF(SQLITE3)
ENDIF(SQLITE3_H)
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2012
# 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
# ============================================================================
# CMake module to detect UNSIO library
# ============================================================================
SET(UNSIO_FOUND FALSE)
set (UNSIOLIB UNSIOLIB-NOTFOUND)
if ( UNSIOPATH ) # user configure cmake with variable -DUNSIO_INSTALLPATH="/unsio/path"
find_library(UNSIOLIB NAMES unsio PATHS ${UNSIOPATH}/lib)
MESSAGE (STATUS "UNSIOLIB = " ${UNSIOLIB})
if (NOT ${UNSIOLIB} STREQUAL UNSIOLIB-NOTFOUND)
MESSAGE(STATUS "Found UNSIOLIB =" ${UNSIOLIB})
SET(UNSIO_FOUND TRUE)
else ()
endif()
endif ()
if (NOT UNSIO_FOUND) # try NEMO
find_library(UNSIOLIB NAMES unsio PATHS $ENV{NEMO}/lib)
MESSAGE (STATUS "UNSIOLIB = " ${UNSIOLIB})
if ( NOT ${UNSIOLIB} STREQUAL UNSIOLIB-NOTFOUND)
MESSAGE(STATUS "Found UNSIOLIB in NEMO =" ${UNSIOLIB})
SET(UNSIO_FOUND TRUE)
SET(UNSIOPATH $ENV{NEMO})
else ()
endif()
endif()
if (NOT UNSIO_FOUND) # try $HOME/local
find_library(UNSIOLIB NAMES unsio PATHS $ENV{HOME}/local/unsio/lib)
MESSAGE (STATUS "UNSIOLIB = " ${UNSIOLIB} " -- " $ENV{HOME})
if (NOT ${UNSIOLIB} STREQUAL UNSIOLIB-NOTFOUND)
MESSAGE(STATUS "Found UNSIOLIB in $ENV{HOME}/local/unsio/ =" ${UNSIOLIB})
SET(UNSIO_FOUND TRUE)
SET(UNSIOPATH $ENV{HOME}/local/unsio)
else ()
endif()
endif()
if (NOT UNSIO_FOUND) # ABORT
message(SEND_ERROR "UNSIO not found - skipping building tests")
else()
endif()
# ============================================================================
# Copyright Jean-Charles LAMBERT - 2009-2012
# 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
# ============================================================================
if ( NOT SETUP_FLAGS_INCLUDED )
set( SETUP_FLAGS_INCLUDED 1 )
macro( typed_cache_set type doc var )
set ( ${var} ${ARGN} CACHE ${type} ${doc} FORCE )
set ( ${var} ${ARGN} CACHE ${type} ${doc} FORCE )
endmacro()
typed_cache_set ( STRING "setup flags" SETUP_FLAGS_INCLUDED 1 )
# Set a default build type if none is given
if ( NOT CMAKE_BUILD_TYPE ) # Debug default
typed_cache_set ( STRING "Build type: Release or Debug" CMAKE_BUILD_TYPE Debug )
else () # else Release
typed_cache_set ( STRING "Build type: Release or Debug" CMAKE_BUILD_TYPE Release )
endif()
# set optimizer flag
if ( ${CMAKE_BUILD_TYPE} STREQUAL "Debug" )
typed_cache_set (STRING "Optimizer" OPT "-g -ggdb")
else ()
typed_cache_set (STRING "Optimizer" OPT "-O3")
endif ()
#typed_cache_set (STRING "compilation warnings" WARNCPP "-Waddress -Warray-bounds -Wc++0x-compat -Wchar-subscripts -Wenum-compare -Wcomment -Wformat -Wmain -Wmissing-braces -Wparentheses -Wreorder -Wreturn-type -Wsequence-point -Wsign-compare -Wstrict-aliasing -Wstrict-overflow=1 -Wswitch -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wvolatile-register-var")
typed_cache_set (STRING "compilation warnings" WARNCPP "-Wall")
# check C compiler flags
include(CheckCCompilerFlag)
set (XX -Warray-bounds -Wchar-subscripts -Wcomment -Wformat -Wmain -Wmissing-braces -Wsequence-point -Wstrict-aliasing -Wstrict-overflow=1 -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused-label -Wvolatile-register-var)
set (WARNC_TMP "")
FOREACH(CWC ${XX})
MESSAGE(STATUS "Test C options==> " ${CWC})
check_c_compiler_flag(${CWC} HAS_OPTION)
if (HAS_OPTION)
set(WARNC_TMP "${WARNC_TMP} ${CWC}")
endif()
endforeach()
typed_cache_set (STRING "compilation warnings" WARNC ${WARNC_TMP})
message(STATUS "Warnc = " ${WARNC} )
typed_cache_set (STRING "compilation warnings" WARNF "-Wall" )
# just for testing
option(RELEASE "RELEASE " ON) # SHARED lib default
if ( NOT RELEASE ) # Debug TRUE
typed_cache_set ( BOOL "RELEASE Version" RELEASE FALSE )
else () # else FALSE
typed_cache_set ( BOOL "RELEASE Version" RELEASE TRUE )
endif()
# detect MacOS
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
typed_cache_set ( BOOL "OSX detection" OSX TRUE )
else()
typed_cache_set ( BOOL "OSX detection" OSX FALSE )
endif()
# SHARED lib ?
option(BUILD_SHARED_LIBS "Shared lib " ON) # SHARED lib default
if ( NOT BUILD_SHARED_LIBS ) # Debug TRUE
typed_cache_set ( BOOL "Build SHARED" BUILD_SHARED_LIBS FALSE )
else () # else FALSE
typed_cache_set ( BOOL "Build SHARED" BUILD_SHARED_LIBS TRUE)
endif()
# set library type , SHARED or STATIC + LIB extension
if ( BUILD_SHARED_LIBS )
typed_cache_set ( STRING "LIB TYPE" LIBTYPE SHARED )
if (OSX)
typed_cache_set ( STRING "LIB TYPE" LIBEXT "dylib" )
else()
typed_cache_set ( STRING "LIB TYPE" LIBEXT "so" )
endif()
else ()
typed_cache_set ( STRING "LIB TYPE" LIBTYPE STATIC )
typed_cache_set ( STRING "LIB TYPE" LIBEXT "a" )
endif()
if (UNSIO_INSTALLPATH)
typed_cache_set ( STRING "UNSIO loation" UNSIOPATH ${UNSIO_INSTALLPATH} )
endif()
#
endif( NOT SETUP_FLAGS_INCLUDED )
# ============================================================================
#include <iostream>
#include <cstdlib>
#include <string>
#include <uns.h>
using namespace std;
int main(int argc, char ** argv)
{
if (argc < 3) {
cerr << "\nsyntaxe: "<<argv[0]<< " filename component\n";
cerr << "example: "<<argv[0]<< " list all\n\n";
exit(1);
}
string simname(argv[1]);
string component(argv[2]);
bool verbose=false;
// -----------------------------------------------
// instantiate a new UNS input object (for reading)
uns::CunsIn * uns = new uns::CunsIn(simname,component,"all",verbose);
if (uns->isValid()) { // input file is known by UNS lib
while(uns->snapshot->nextFrame()) { // there is a new frame
string stype = uns->snapshot->getInterfaceType();
cout << "File name : "<<uns->snapshot->getFileName()<<"\n";
cout << "File type : "<<stype<<"\n";
int nbody; float time;
// get the input number of bodies according to the selection
uns->snapshot->getData("nsel",&nbody);
// get the simulation time
uns->snapshot->getData("time",&time);
// read position
float * pos;
bool ok = uns->snapshot->getData("all","pos",&nbody,&pos); // we get positions here
// in one dimensionnal array
if (ok) {
for (int i=0; i<nbody; i++) {
// display positions x,y,z
cerr << "x="<<pos[i*3+0] << " y="<<pos[i*3+1] << " z="<<pos[i*3+2] <<"\n" ;
}
}
}
}
delete uns;
}
//
program unsio_test_fortran
implicit none
integer lnblnk, narg, uns_init, uns_load, valid, ident,
$ nbody, uns_sim_type, cpt, uns_get_value_i,
$ status
!external iargc,getarg
character ain * 80, acomp * 80
character simdir * 100
!narg = iargc()
narg = command_argument_count()
write(0,*) 'narg=',narg
if (narg.ne.2) then
call getarg(0,ain)
write(0,*) "You must give 2 parameters:"
write(0,*) ain(1:lnblnk(ain)),
$ " inputfile selected_component"
write(0,*) ""
write(0,*) "Example : "
write(0,*) ain(1:lnblnk(ain)),
$ " list all"
stop
endif
! format fortran string to c string
call get_command_argument(1,ain)
call get_command_argument(2,acomp)
! initialyze UNS engine
ident=uns_init(ain, acomp, "all") ! return identifier for the snaphot
! ident must be positive
write(0,*) "Uns type = ",uns_sim_type(ident)
write (0,*) "UNS_INIT return :",ident
if (ident.gt.0) then
valid = 1
cpt = 0
call uns_sim_dir(ident,simdir)
write (0,*) "Simulation DIR = [",simdir(1:lnblnk(simdir)),"]"
do while (valid .gt. 0) ! loop on all the time step
valid = uns_load(ident) ! load data belonging to ident snapshot
write(0,*) "uns_load return=>",valid
if (valid .gt. 0) then ! it goes fine
!! get nbody
status=uns_get_value_i(ident,"nsel", nbody) ! get #bodies
write (0,*) "nbody =", nbody
!! start main subroutine
call start(ident,nbody)
endif
enddo
endif
end
! =============================================================================
subroutine start(ident,nbody)
implicit none
! input parameters
integer ident,nbody,i
! UNS variable
integer status
integer uns_get_value_f, uns_get_array_f, nsel
real *4 time, pos(3,nbody), vel(3,nbody), mass(nbody)
status=uns_get_value_f(ident,"time",time )
write (0,*) 'time =',time
! load pos vel mass
nsel=uns_get_array_f(ident,"all","pos" ,pos ,nbody)
if (nsel .gt. 0 ) then ! there are POSITIONS
do i=1,nbody
write (0,*) pos(1,i), pos(2,i), pos(3,i)
enddo
endif
nsel=uns_get_array_f(ident,"all","vel" ,vel ,nbody)
if (nsel .gt. 0 ) then ! there are VELOCITIES
do i=1,nbody
write (0,*) vel(1,i), vel(2,i), vel(3,i)
enddo
endif
nsel=uns_get_array_f(ident,"all","mass",mass,nbody)
if (nsel .gt. 0 ) then ! there are MASSES
do i=1,nbody
write (0,*) mass(i)
enddo
endif
write (0,*) 'nsel =',nsel,' nbody=',nbody
end
program unsio_fortran
implicit none
integer lnblnk, narg, uns_init, uns_load, valid, ident,
$ nbody, uns_sim_type, cpt, uns_get_value_i,
$ status
!external iargc,getarg
character ain * 80, aout * 80, atyp * 80, acomp * 80, atim * 80
character out * 80, ii * 6, simdir * 100
!narg = iargc()
narg = command_argument_count()
write(0,*) 'narg=',narg
if (narg.ne.5) then
call getarg(0,ain)
write(0,*) "You must give 5 parameters:"
write(0,*) ain(1:lnblnk(ain)),
$ " inputfile outputfile selected_component",
$ " outttype selected_time"
write(0,*) ""
write(0,*) "Example : "
write(0,*) ain(1:lnblnk(ain)),
$ " sgs029 sgs029.gas gas gadget2 all"
stop
endif
! format fortran string to c string
call get_command_argument(1,ain)
call get_command_argument(2,aout)
call get_command_argument(3,acomp)
call get_command_argument(4,atyp)
call get_command_argument(5,atim)
! initialyze UNS engine
ident=uns_init(ain, acomp, atim) ! return identifier for the snaphot
! ident must be positive
write(0,*) "Uns type = ",uns_sim_type(ident)