1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 04:14:40 +01:00

Merge branch 'master' into add-unsafe-open-mode

This commit is contained in:
q-posev 2022-01-24 18:25:52 +01:00
commit 8947f6caa7
8 changed files with 142 additions and 21 deletions

View File

@ -32,8 +32,25 @@ if(EXISTS "${CMAKE_SOURCE_DIR}/.git/config")
else()
message(FATAL_ERROR "EMACS not found. It is required to produce TREXIO source code from org-mode files.")
endif()
# in case Git is not available, we default to "unknown"
set(GIT_HASH "unknown")
# find Git and if available set GIT_HASH variable
find_package(Git QUIET)
if(GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} --no-pager show -s --pretty=format:%H -n 1
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
ERROR_QUIET)
endif()
# get the user name from the ${USER} env variable
set(TREXIO_USER_NAME $ENV{USER})
# replace placeholders in the templace config.h.in file to produce config.h
# config.h is needed to insert TREXIO_PACKAGE_VERSION and TREXIO_GIT_HASH into trexio.h
configure_file(${CMAKE_SOURCE_DIR}/include/cmake_config.h.in
${CMAKE_SOURCE_DIR}/include/config.h)
${CMAKE_SOURCE_DIR}/include/config.h
@ONLY)
endif()
# Set directories to be included at build time.

View File

@ -3,3 +3,5 @@
#define VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define VERSION_MINOR @PROJECT_VERSION_MINOR@
#define VERSION_PATCH @PROJECT_VERSION_PATCH@
#define GIT_HASH "@GIT_HASH@"
#define TREXIO_USER_NAME "@TREXIO_USER_NAME@"

View File

@ -38,6 +38,8 @@ except:
#============ WRITE THE DATA IN THE TEST FILE ============#
#=========================================================#
trexio.info()
# create TREXIO file and open it for writing
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
assert test_file.exists

View File

@ -1,13 +1,26 @@
# ========= DEFINE TREXIO C LIBRARY =========
# ============= THREADS SUPPORT ==============
# the two options below add preference for the pthread library over other system-wide threads implementations
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
# find thread library
find_package(Threads REQUIRED)
# ============= STDINT SUPPORT ===============
include(CheckIncludeFile)
check_include_file(stdint.h HAVE_STDINT_H)
# ========= DEFINE TREXIO C LIBRARY =========
# Set a list of TREXIO source and header files that are always compiled.
set(TREXIO_SOURCES
set(TREXIO_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/trexio.c
${CMAKE_CURRENT_SOURCE_DIR}/trexio_text.c
)
set(TREXIO_PUBLIC_HEADERS ${CMAKE_SOURCE_DIR}/include/trexio.h)
set(TREXIO_PRIVATE_HEADERS
set(TREXIO_PRIVATE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/trexio_s.h
${CMAKE_CURRENT_SOURCE_DIR}/trexio_private.h
${CMAKE_CURRENT_SOURCE_DIR}/trexio_text.h
@ -29,7 +42,7 @@ if(BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
add_library(trexio SHARED)
elseif(NOT BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS)
message(STATUS "TREXIO :: static C library will be built")
add_library(trexio STATIC)
# Static TREXIO has to be compiled with -fPIC flag. For Shared it is done by default.
@ -40,12 +53,12 @@ else()
endif()
# Set TREXIO version and include files.
set_target_properties(trexio PROPERTIES
set_target_properties(trexio PROPERTIES
VERSION ${PROJECT_VERSION}
PUBLIC_HEADER ${TREXIO_PUBLIC_HEADERS}
)
# ============= CONFIGURE HDF5 ==============
# ============= CONFIGURE HDF5 ==============
# By defaulti, TREXIO is configured with the HDF5 support.
# To change this, append -DENABLE_HDF5=OFF to the cmake call.
@ -74,8 +87,8 @@ if(ENABLE_HDF5)
# - include directories with HDF5 header files
target_include_directories(trexio PRIVATE ${HDF5_C_INCLUDE_DIRS})
# - link to HDF5 C libraries
target_link_libraries(trexio PRIVATE
${HDF5_C_HL_LIBRARIES}
target_link_libraries(trexio PRIVATE
${HDF5_C_HL_LIBRARIES}
${HDF5_C_LIBRARIES})
endif()
@ -110,7 +123,7 @@ if(TREXIO_DEVEL)
list(APPEND ORG_FILES templates_hdf5/templator_hdf5.org)
endif()
add_custom_command(OUTPUT
add_custom_command(OUTPUT
${TREXIO_SOURCES}
${TREXIO_PUBLIC_HEADERS}
${TREXIO_PRIVATE_HEADERS}
@ -129,7 +142,7 @@ if(TREXIO_DEVEL)
endif()
# ============= INSTALL TREXIO ==============
# ============= INSTALL TREXIO ==============
include(GNUInstallDirs)
# Use standard GNU directories for installation of TREXIO (e.g. /usr/local/lib|include).
@ -153,4 +166,4 @@ if(NOT TARGET uninstall)
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()
# ===========================================
# ===========================================

View File

@ -4,18 +4,22 @@
cat prefix_front.c > trexio.c
cat prefix_front.h > trexio.h
# parse the config-defined version attributes to pass them to Fortran module file
# parse the config-defined version attributes to pass them to the header files
VERSION_VAL=`grep "PACKAGE_VERSION" ../../include/config.h | cut -d " " -f 3`
VERSION_MAJOR_VAL=`grep "VERSION_MAJOR" ../../include/config.h | cut -d " " -f 3`
VERSION_MINOR_VAL=`grep "VERSION_MINOR" ../../include/config.h | cut -d " " -f 3`
VERSION_PATCH_VAL=`grep "VERSION_PATCH" ../../include/config.h | cut -d " " -f 3`
# parse the config-defined GIT_HASH to pass them to the header files
GIT_HASH_STR=`grep "GIT_HASH" ../../include/config.h | cut -d " " -f 3`
# grep some usefull constants from the config.h
echo "" >> trexio.h
echo "#define TREXIO_PACKAGE_VERSION ${VERSION_VAL:='0.0.0'}" >> trexio.h
echo "#define TREXIO_VERSION_MAJOR ${VERSION_MAJOR_VAL:=0}" >> trexio.h
echo "#define TREXIO_VERSION_MINOR ${VERSION_MINOR_VAL:=0}" >> trexio.h
echo "#define TREXIO_VERSION_PATCH ${VERSION_PATCH_VAL:=0}" >> trexio.h
echo "#define TREXIO_GIT_HASH ${GIT_HASH_STR:='0000'}" >> trexio.h
echo "" >> trexio.h
cat prefix_s_front.h > trexio_s.h
@ -24,10 +28,11 @@ cat prefix_python.py > trexio.py
# append version string and attributes to the Fortran module file
echo "" >> trexio_f.f90
echo "character(len = 12) :: TREXIO_PACKAGE_VERSION = ${VERSION_VAL}" >> trexio_f.f90
echo "character(len = 12) :: TREXIO_PACKAGE_VERSION = ${VERSION_VAL:='0.0.0'}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_MAJOR = ${VERSION_MAJOR_VAL}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_MINOR = ${VERSION_MINOR_VAL}" >> trexio_f.f90
echo "integer :: TREXIO_VERSION_PATCH = ${VERSION_PATCH_VAL}" >> trexio_f.f90
echo "character(len = 64) :: TREXIO_GIT_HASH = ${GIT_HASH_STR:='0000'}" >> trexio_f.f90
echo "" >> trexio_f.f90
# c front end

View File

@ -1211,6 +1211,18 @@ def _close(trexio_file):
output:
~trexio_exit_code~ exit code.
It returns:
- ~TREXIO_SUCCESS~ if the ~file_name~ exists and if it correspond to a valid TREXIO file (i.e. a directory OR an HDF5 file)
- ~TREXIO_FILE_ERROR~ if ~file_name~ exists but it is not a TREXIO file
- ~TREXIO_FAILURE~ otherwise
Also, there is an implicit way to have the aforementioned functionality.
Attempt to open a non-existing file with ~trexio_open~ function will result in a ~TREXIO_OPEN_ERROR~ exit code
(see the modified value that has been passed as a 4-th argument to ~trexio_open~).
You can see examples of both functionalities in =test_f.f90= (search for calls with ~_void~ suffix).
*** C
#+begin_src c :tangle prefix_front.h :exports none
@ -1397,6 +1409,7 @@ trexio_exit_code
trexio_read_$group_num$_64 (trexio_t* const file, $group_num_dtype_double$* const num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
switch (file->back_end) {
@ -1456,6 +1469,7 @@ trexio_exit_code
trexio_read_$group_num$_32 (trexio_t* const file, $group_num_dtype_single$* const num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
$group_num_dtype_double$ num_64 = 0;
@ -2680,6 +2694,7 @@ trexio_exit_code
trexio_read_$group_dset$_size(trexio_t* const file, int64_t* const size_max)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (size_max == NULL) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) return TREXIO_DSET_MISSING;
switch (file->back_end) {
@ -3904,6 +3919,7 @@ def has_$group_str$(trexio_file) -> bool:
return False
#+end_src
<<<<<<< HEAD
** Templates for front end delete an entire group (UNSAFE MODE)
*** Introduction
@ -3985,6 +4001,69 @@ def delete_$group$(trexio_file) -> None:
if rc != TREXIO_SUCCESS:
raise Error(rc)
#+end_src
* General helper functions
This section contains general helper functions like ~trexio_info~.
~trexio_info~ prints information about the TREXIO configuration (see =config.h= file).
In particular:
1) ~TREXIO_PACKAGE_VERSION~ [string]
2) ~HAVE_HDF5~ [bool]
3) ~HDF5_VERSION~ [string] (optional, only if ~HAVE_HDF5~ is ~true~)
4) ~TREXIO_GIT_HASH~ [string]
** C
#+begin_src c :tangle prefix_front.h :exports none
trexio_exit_code trexio_info(void);
#+end_src
#+begin_src c :tangle prefix_front.c
trexio_exit_code
trexio_info (void)
{
printf("TREXIO_PACKAGE_VERSION : %s\n", TREXIO_PACKAGE_VERSION);
#ifdef TREXIO_GIT_HASH
printf("TREXIO_GIT_HASH : %s\n", TREXIO_GIT_HASH);
#else
printf("GIT_HASH is stored in the config.h file, which is missing.");
#endif
#ifdef HAVE_HDF5
printf("HAVE_HDF5 : true\n");
printf("%s\n", H5_VERS_INFO);
#else
printf("HAVE_HDF5 : false\n");
printf("TREXIO configured without the HDF5 library\n");
#endif
return TREXIO_SUCCESS;
}
#+end_src
** Fortran
#+begin_src f90 :tangle prefix_fortran.f90
interface
integer function trexio_info () bind(C)
use, intrinsic :: iso_c_binding
end function trexio_info
end interface
#+end_src
** Python
#+begin_src python :tangle basic_python.py
def info():
"""Print the info about the installed TREXIO library.
"""
rc = pytr.trexio_info()
if rc != TREXIO_SUCCESS:
raise Error(rc)
#+end_src
* Fortran helper/wrapper functions

View File

@ -14,6 +14,8 @@ int main() {
int rc;
trexio_info();
bool have_hdf5 = trexio_has_backend(TREXIO_HDF5);
if(have_hdf5) {
rc = system("rm -f -- test_all.h5");
@ -72,7 +74,7 @@ int test_write(const char* file_name, const back_end_t backend) {
"H 999asdasd" ,
"H" ,
"H" };
const char* sym = "B3U with some comments";
/*================= START OF TEST ==================*/
@ -96,11 +98,11 @@ int test_write(const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_point_group(file, sym, 32);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// reopen file in 'write' mode
file = trexio_open(file_name, 'w', backend, &rc);
assert (file != NULL);
@ -188,7 +190,7 @@ int test_read(const char* file_name, const back_end_t backend) {
assert (rc == TREXIO_SUCCESS);
assert (strcmp(label[0], "C") == 0);
assert (strcmp(label[1], "Na") == 0);
for (int i=0; i<num; i++){
free(label[i]);
}
@ -220,8 +222,6 @@ int test_read(const char* file_name, const back_end_t backend) {
assert (file2 == NULL);
/*================= END OF TEST =====================*/
return 0;
}

View File

@ -3,6 +3,7 @@ program test_trexio
use, intrinsic :: iso_c_binding
implicit none
integer :: rc
logical :: have_hdf5
print * , "============================================"
@ -11,6 +12,8 @@ program test_trexio
print'(a,i3)', " TREXIO MINOR VERSION : ", TREXIO_VERSION_MINOR
print * , "============================================"
rc = trexio_info()
call system('rm -rf -- test_write_f.dir')
print *, 'call test_write(''test_write_f.dir'', TREXIO_TEXT)'
call test_write('test_write_f.dir', TREXIO_TEXT)