1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00

Merge pull request #2 from TREX-CoE/hdf5-backend

HDF5 backend
This commit is contained in:
Evgeny Posenitskiy 2021-03-02 11:04:01 +01:00 committed by GitHub
commit ff28a435d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1157 additions and 83 deletions

View File

@ -6,7 +6,8 @@ ifeq ($(COMPILER),GNU)
CC=gcc -g CC=gcc -g
CFLAGS=-fPIC -fexceptions -Wall -Werror -Wpedantic -Wextra CFLAGS=-fPIC -fexceptions -Wall -Werror -Wpedantic -Wextra
LIBS= #-lgfortran -lm LIBS= -L/usr/lib/x86_64-linux-gnu/hdf5/serial/ -lz -lm -lhdf5 -lhdf5_hl #-lgfortran
INCLUDE = -I/usr/include/hdf5/serial
endif endif
ifeq ($(COMPILER),INTEL) ifeq ($(COMPILER),INTEL)
@ -31,8 +32,8 @@ LIBS=-lm
endif endif
OBJECT_FILES= trio.o trio_text.o OBJECT_FILES= trio.o trio_text.o trio_hdf5.o
HEADER_FILES= trio.h trio_text.h trio_s.h HEADER_FILES= trio.h trio_text.h trio_hdf5.h trio_s.h
export CC CFLAGS LIBS export CC CFLAGS LIBS
@ -42,11 +43,11 @@ libtrio.so: $(OBJECT_FILES) $(HEADER_FILES)
$(CC) -shared $(OBJECT_FILES) -o libtrio.so $(CC) -shared $(OBJECT_FILES) -o libtrio.so
test: libtrio.so test.c test: libtrio.so test.c
$(CC) $(CFLAGS) -Wl,-rpath,$(PWD) -L. test.c -ltrio $(LIBS) -o test $(CC) $(CFLAGS) $(INCLUDE) -Wl,-rpath,$(PWD) -L. test.c -ltrio $(LIBS) -o test
clean: clean:
rm -f *.o libtrio.so rm -f *.o libtrio.so
%.o: %.c $(HEADER_FILES) %.o: %.c $(HEADER_FILES)
$(CC) $(CFLAGS) -c $*.c -o $*.o $(CC) $(CFLAGS) $(INCLUDE) -c $*.c -o $*.o

View File

@ -7,14 +7,58 @@
int test_read(); int test_read();
int test_write(); int test_write();
int test_h5read();
int main() { int main() {
test_h5read();
test_write(); test_write();
test_read(); test_read();
return 0 ; return 0 ;
} }
int test_h5read() {
const char* file_name = "test.h5";
trio_t* file = NULL;
trio_exit_code rc;
uint64_t num;
//double* charge;
double* coord;
file = trio_create(file_name, TRIO_HDF5);
/*rc = trio_read_nucleus_num(file,&num);
assert (num == 12);
charge = (double*) calloc(num, sizeof(double));
rc = trio_read_nucleus_charge(file,charge);
assert(charge[10] == 1.);*/
num = 4;
coord = (double*) calloc(3*num, sizeof(double));
rc = trio_read_nucleus_coord(file,coord);
for (size_t i=0; i<3*num; i++){
printf("%lf \n", coord[i]);
}
double x = coord[0] - 2.14171677;
assert( x*x < 1000.);
if (rc == TRIO_SUCCESS) {
printf("SUCCESS\n");
} else {
printf("FAILURE\n");
}
trio_close(file);
return 0;
}
int test_write() { int test_write() {
const char* file_name = "trio_test"; const char* file_name = "trio_test";
@ -22,7 +66,7 @@ int test_write() {
trio_exit_code rc; trio_exit_code rc;
int64_t num = 12; uint64_t num = 12;
double charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.}; double charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.};
@ -64,7 +108,7 @@ int test_read() {
trio_t* file = NULL; trio_t* file = NULL;
trio_exit_code rc; trio_exit_code rc;
int64_t num; uint64_t num;
double* charge; double* charge;
double* coord; double* coord;

View File

@ -14,8 +14,8 @@
#include "trio.h" #include "trio.h"
#include "trio_s.h" #include "trio_s.h"
#include "trio_text.h" #include "trio_text.h"
/*
#include "trio_hdf5.h" #include "trio_hdf5.h"
/*
#include "trio_json.h" #include "trio_json.h"
*/ */
@ -35,11 +35,11 @@ trio_t* trio_create(const char* file_name, back_end_t back_end) {
case TRIO_TEXT: case TRIO_TEXT:
result = (trio_t*) malloc (sizeof(trio_text_t)); result = (trio_t*) malloc (sizeof(trio_text_t));
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
result = (trio_t*) malloc (sizeof(trio_hdf5_t)); result = (trio_t*) malloc (sizeof(trio_hdf5_t));
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
result = (trio_t*) malloc (sizeof(trio_json_t)); result = (trio_t*) malloc (sizeof(trio_json_t));
break; break;
@ -65,11 +65,11 @@ trio_t* trio_create(const char* file_name, back_end_t back_end) {
case TRIO_TEXT: case TRIO_TEXT:
rc = trio_text_init(result); rc = trio_text_init(result);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
rc = trio_hdf5_init(result); rc = trio_hdf5_init(result);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
rc = trio_json_init(result); rc = trio_json_init(result);
break; break;
@ -93,11 +93,11 @@ trio_exit_code trio_close(trio_t* file) {
case TRIO_TEXT: case TRIO_TEXT:
rc = trio_text_finalize(file); rc = trio_text_finalize(file);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
rc = trio_hdf5_finalize(file); rc = trio_hdf5_finalize(file);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
rc = trio_json_finalize(file); rc = trio_json_finalize(file);
break; break;
@ -121,7 +121,7 @@ trio_exit_code trio_close(trio_t* file) {
return TRIO_SUCCESS; return TRIO_SUCCESS;
} }
trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) { trio_exit_code trio_read_nucleus_num(trio_t* file, uint64_t* num) {
if (file == NULL) return TRIO_FAILURE; if (file == NULL) return TRIO_FAILURE;
switch (file->back_end) { switch (file->back_end) {
@ -129,11 +129,11 @@ trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_read_nucleus_num(file, num); return trio_text_read_nucleus_num(file, num);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_read_nucleus_num(file, num); return trio_hdf5_read_nucleus_num(file, num);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_read_nucleus_num(file, num); return trio_json_read_nucleus_num(file, num);
break; break;
@ -143,7 +143,7 @@ trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) {
} }
} }
trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num) { trio_exit_code trio_write_nucleus_num(trio_t* file, uint64_t num) {
if (file == NULL) return TRIO_FAILURE; if (file == NULL) return TRIO_FAILURE;
switch (file->back_end) { switch (file->back_end) {
@ -151,11 +151,11 @@ trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_write_nucleus_num(file, num); return trio_text_write_nucleus_num(file, num);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_write_nucleus_num(file, num); return trio_hdf5_write_nucleus_num(file, num);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_write_nucleus_num(file, num); return trio_json_write_nucleus_num(file, num);
break; break;
@ -173,11 +173,11 @@ trio_exit_code trio_read_nucleus_coord(trio_t* file, double* coord) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_read_nucleus_coord(file, coord); return trio_text_read_nucleus_coord(file, coord);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_read_nucleus_coord(file, coord); return trio_hdf5_read_nucleus_coord(file, coord);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_read_nucleus_coord(file, coord); return trio_json_read_nucleus_coord(file, coord);
break; break;
@ -195,11 +195,11 @@ trio_exit_code trio_write_nucleus_coord(trio_t* file, double* coord) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_write_nucleus_coord(file, coord); return trio_text_write_nucleus_coord(file, coord);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_write_nucleus_coord(file, coord); return trio_hdf5_write_nucleus_coord(file, coord);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_write_nucleus_coord(file, coord); return trio_json_write_nucleus_coord(file, coord);
break; break;

View File

@ -28,8 +28,8 @@ trio_t* trio_create(const char* file_name, back_end_t back_end);
trio_exit_code trio_close(trio_t* file); trio_exit_code trio_close(trio_t* file);
trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num); trio_exit_code trio_read_nucleus_num(trio_t* file, uint64_t* num);
trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num); trio_exit_code trio_write_nucleus_num(trio_t* file, uint64_t num);
trio_exit_code trio_read_nucleus_coord(trio_t* file, double* coord); trio_exit_code trio_read_nucleus_coord(trio_t* file, double* coord);
trio_exit_code trio_write_nucleus_coord(trio_t* file, double* coord); trio_exit_code trio_write_nucleus_coord(trio_t* file, double* coord);

View File

@ -1,4 +1,4 @@
#+TITLE: TREX Input/Ouput library (TRIO) #+Title: TREX Input/Ouput library (TRIO)
* File prefixes :noxport: * File prefixes :noxport:
@ -33,8 +33,8 @@
#include "trio.h" #include "trio.h"
#include "trio_s.h" #include "trio_s.h"
#include "trio_text.h" #include "trio_text.h"
/*
#include "trio_hdf5.h" #include "trio_hdf5.h"
/*
#include "trio_json.h" #include "trio_json.h"
,*/ ,*/
@ -72,6 +72,30 @@
#include "trio_text.h" #include "trio_text.h"
#+end_src #+end_src
#+begin_src c :tangle trio_hdf5.h :noweb yes
<<header>>
#ifndef _TRIO_HDF5_H
#define _TRIO_HDF5_H
#include "trio.h"
#include "trio_s.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include "hdf5.h"
#include "hdf5_hl.h" // needed for high-level APIs like H5LT, requires additional linking in Makefile
#+end_src
#+begin_src c :tangle trio_hdf5.c :noweb yes
<<header>>
#include "trio_hdf5.h"
#+end_src
* Coding conventions * Coding conventions
- integer types will be defined using types given in ~stdint.h~ - integer types will be defined using types given in ~stdint.h~
@ -189,11 +213,11 @@ trio_t* trio_create(const char* file_name, back_end_t back_end) {
case TRIO_TEXT: case TRIO_TEXT:
result = (trio_t*) malloc (sizeof(trio_text_t)); result = (trio_t*) malloc (sizeof(trio_text_t));
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
result = (trio_t*) malloc (sizeof(trio_hdf5_t)); result = (trio_t*) malloc (sizeof(trio_hdf5_t));
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
result = (trio_t*) malloc (sizeof(trio_json_t)); result = (trio_t*) malloc (sizeof(trio_json_t));
break; break;
@ -219,15 +243,15 @@ trio_t* trio_create(const char* file_name, back_end_t back_end) {
case TRIO_TEXT: case TRIO_TEXT:
rc = trio_text_init(result); rc = trio_text_init(result);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
rc = trio_hdf5_init(result); rc = trio_hdf5_init(result);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
rc = trio_json_init(result); rc = trio_json_init(result);
break; break;
*/ ,*/
default: default:
assert (1 == 0); /* Impossible case */ assert (1 == 0); /* Impossible case */
} }
@ -256,11 +280,11 @@ trio_exit_code trio_close(trio_t* file) {
case TRIO_TEXT: case TRIO_TEXT:
rc = trio_text_finalize(file); rc = trio_text_finalize(file);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
rc = trio_hdf5_finalize(file); rc = trio_hdf5_finalize(file);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
rc = trio_json_finalize(file); rc = trio_json_finalize(file);
break; break;
@ -287,12 +311,12 @@ trio_exit_code trio_close(trio_t* file) {
** Reading/writing data ** Reading/writing data
#+begin_src c :tangle trio.h #+begin_src c :tangle trio.h
trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num); trio_exit_code trio_read_nucleus_num(trio_t* file, uint64_t* num);
trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num); trio_exit_code trio_write_nucleus_num(trio_t* file, uint64_t num);
#+end_src #+end_src
#+begin_src c :tangle trio.c #+begin_src c :tangle trio.c
trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) { trio_exit_code trio_read_nucleus_num(trio_t* file, uint64_t* num) {
if (file == NULL) return TRIO_FAILURE; if (file == NULL) return TRIO_FAILURE;
switch (file->back_end) { switch (file->back_end) {
@ -300,11 +324,11 @@ trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_read_nucleus_num(file, num); return trio_text_read_nucleus_num(file, num);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_read_nucleus_num(file, num); return trio_hdf5_read_nucleus_num(file, num);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_read_nucleus_num(file, num); return trio_json_read_nucleus_num(file, num);
break; break;
@ -314,7 +338,7 @@ trio_exit_code trio_read_nucleus_num(trio_t* file, int64_t* num) {
} }
} }
trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num) { trio_exit_code trio_write_nucleus_num(trio_t* file, uint64_t num) {
if (file == NULL) return TRIO_FAILURE; if (file == NULL) return TRIO_FAILURE;
switch (file->back_end) { switch (file->back_end) {
@ -322,11 +346,11 @@ trio_exit_code trio_write_nucleus_num(trio_t* file, int64_t num) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_write_nucleus_num(file, num); return trio_text_write_nucleus_num(file, num);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_write_nucleus_num(file, num); return trio_hdf5_write_nucleus_num(file, num);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_write_nucleus_num(file, num); return trio_json_write_nucleus_num(file, num);
break; break;
@ -352,11 +376,11 @@ trio_exit_code trio_read_nucleus_coord(trio_t* file, double* coord) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_read_nucleus_coord(file, coord); return trio_text_read_nucleus_coord(file, coord);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_read_nucleus_coord(file, coord); return trio_hdf5_read_nucleus_coord(file, coord);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_read_nucleus_coord(file, coord); return trio_json_read_nucleus_coord(file, coord);
break; break;
@ -374,11 +398,11 @@ trio_exit_code trio_write_nucleus_coord(trio_t* file, double* coord) {
case TRIO_TEXT: case TRIO_TEXT:
return trio_text_write_nucleus_coord(file, coord); return trio_text_write_nucleus_coord(file, coord);
break; break;
/*
case TRIO_HDF5: case TRIO_HDF5:
return trio_hdf5_write_nucleus_coord(file, coord); return trio_hdf5_write_nucleus_coord(file, coord);
break; break;
/*
case TRIO_JSON: case TRIO_JSON:
return trio_json_write_nucleus_coord(file, coord); return trio_json_write_nucleus_coord(file, coord);
break; break;
@ -455,12 +479,12 @@ trio_exit_code trio_write_nucleus_charge(trio_t* file, double* charge) {
typedef struct nucleus_s { typedef struct nucleus_s {
double* coord; double* coord;
double* charge; double* charge;
int64_t num; uint64_t num;
} nucleus_t; } nucleus_t;
typedef struct electron_s { typedef struct electron_s {
int64_t alpha_num; uint64_t alpha_num;
int64_t beta_num; uint64_t beta_num;
} electron_t; } electron_t;
typedef struct trio_text_s { typedef struct trio_text_s {
@ -566,7 +590,7 @@ nucleus_t* trio_text_read_nucleus(const trio_text_t* file) {
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "num") == 0); assert (strcmp(buffer, "num") == 0);
fscanf(f, "%ld", &(nucleus->num)); fscanf(f, "%lu", &(nucleus->num));
assert (nucleus->num > 0); assert (nucleus->num > 0);
/* Allocate arrays */ /* Allocate arrays */
@ -580,14 +604,14 @@ nucleus_t* trio_text_read_nucleus(const trio_text_t* file) {
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "charge") == 0); assert (strcmp(buffer, "charge") == 0);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
fscanf(f, "%lf", &(nucleus->charge[i])); fscanf(f, "%lf", &(nucleus->charge[i]));
} }
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "coord") == 0); assert (strcmp(buffer, "coord") == 0);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
fscanf(f, "%lf", &(nucleus->coord[i])); fscanf(f, "%lf", &(nucleus->coord[i]));
} }
free(buffer); free(buffer);
@ -607,12 +631,12 @@ trio_exit_code trio_text_write_nucleus(const trio_text_t* file, nucleus_t* nucle
/* Write arrays */ /* Write arrays */
fprintf(f, "charge\n"); fprintf(f, "charge\n");
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
fprintf(f, "%lf\n", nucleus->charge[i]); fprintf(f, "%lf\n", nucleus->charge[i]);
} }
fprintf(f, "coord\n"); fprintf(f, "coord\n");
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]); fprintf(f, "%lf\n", nucleus->coord[i]);
} }
@ -650,12 +674,12 @@ trio_exit_code trio_text_free_nucleus(nucleus_t* nucleus) {
*** Read/Write the num attribute *** Read/Write the num attribute
#+begin_src c :tangle trio_text.h #+begin_src c :tangle trio_text.h
trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num); trio_exit_code trio_text_read_nucleus_num(const trio_t* file, uint64_t* num);
trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const int64_t num); trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const uint64_t num);
#+end_src #+end_src
#+begin_src c :tangle trio_text.c #+begin_src c :tangle trio_text.c
trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num) { trio_exit_code trio_text_read_nucleus_num(const trio_t* file, uint64_t* num) {
assert (file != NULL); assert (file != NULL);
assert (num != NULL); assert (num != NULL);
@ -673,7 +697,7 @@ trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num) {
} }
trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const int64_t num) { trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const uint64_t num) {
assert (num > 0L); assert (num > 0L);
assert (file != NULL); assert (file != NULL);
@ -733,7 +757,7 @@ trio_exit_code trio_text_read_nucleus_coord(const trio_t* file, double* coord) {
assert (coord != NULL); assert (coord != NULL);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
coord[i] = nucleus->coord[i]; coord[i] = nucleus->coord[i];
} }
@ -750,7 +774,7 @@ trio_exit_code trio_text_write_nucleus_coord(const trio_t* file, const double* c
nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*) file); nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*) file);
assert (nucleus != NULL); assert (nucleus != NULL);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
nucleus->coord[i] = coord[i]; nucleus->coord[i] = coord[i];
} }
@ -784,7 +808,7 @@ trio_exit_code trio_text_read_nucleus_charge(const trio_t* file, double* charge)
assert (charge != NULL); assert (charge != NULL);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
charge[i] = nucleus->charge[i]; charge[i] = nucleus->charge[i];
} }
@ -801,7 +825,7 @@ trio_exit_code trio_text_write_nucleus_charge(const trio_t* file, const double*
nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*)file); nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*)file);
assert (nucleus != NULL); assert (nucleus != NULL);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
nucleus->charge[i] = charge[i]; nucleus->charge[i] = charge[i];
} }
@ -814,6 +838,518 @@ trio_exit_code trio_text_write_nucleus_charge(const trio_t* file, const double*
} }
#+end_src #+end_src
** HDF5 Back end ** HDF5 Back end
*** HDF5 definitions
#+begin_src c :tangle trio_hdf5.c
#define NUCLEUS_GROUP_NAME "nucleus"
#define NUCLEUS_NUM_NAME "nucleus_num"
#define NUCLEUS_CHARGE_NAME "nucleus_charge"
#define NUCLEUS_COORD_NAME "nucleus_coord"
#+end_src
*** HDF5 structures
#+begin_src c :tangle trio_hdf5.h
typedef struct slab_s {
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
} slab_t;
typedef struct dset_s {
hid_t dset_id;
hid_t dspace_id;
hid_t dtype_id;
uint64_t* dims;
uint32_t rank;
const char* dset_name;
} dset_t;
typedef struct h5nucleus_s {
uint64_t num;
double *coord;
double *charge;
dset_t* h5_coord;
dset_t* h5_charge;
} h5nucleus_t;
typedef struct h5electron_s {
uint64_t alpha_num;
uint64_t beta_num;
} h5electron_t;
typedef struct trio_hdf5_s {
trio_t parent ;
hid_t file_id;
hid_t nucleus_group;
hid_t electron_group;
//... other groups' id
const char* file_name;
} trio_hdf5_t;
#+end_src
*** HDF5 basic functions
#+begin_src c :tangle trio_hdf5.h
trio_exit_code trio_hdf5_init(trio_t* file);
#+end_src
#+begin_src c :tangle trio_hdf5.c
/*
* Currently H5LTread_dataset_ is used instead of this function
* but keep it for later if we decide to get rid of the H5LT API
*/
dset_t* trio_hdf5_read_dset_low(const trio_hdf5_t* file, const char *dset_name, void *buf) {
assert (file != NULL);
assert (dset_name != NULL);
assert (buf != NULL);
/*
* Low-level implementation. Involves dealing with all HDF5 handles and dimensions
*/
dset_t* dset = (dset_t*) malloc(sizeof(dset_t));
assert (dset != NULL);
dset->dset_id = H5Dopen(file->nucleus_group,
dset_name,
H5P_DEFAULT);
assert (dset->dset_id > 0);
/*
* Get dataspace, datatype and dimensions
* dspace and dtype handles created below have to be closed when not used
*/
dset->dspace_id = H5Dget_space(dset->dset_id);
assert (dset->dspace_id > 0);
dset->dtype_id = H5Dget_type(dset->dset_id);
assert (dset->dtype_id > 0);
/* Check dimensions. Usefull, but then additional parameters
* ranks and dims[] have to be passed to the function
int rrank;
const int rank = 1;
hsize_t dims[1] = {0};
rrank = H5Sget_simple_extent_dims(nucleus->h5_charge->dspace_id,
dims, NULL);
assert (rrank == rank);
for (int i=0; i<rank; i++){
assert (dims[i] > 0);
}
*/
herr_t status;
status = H5Dread(dset->dset_id, dset->dtype_id,
H5S_ALL, H5S_ALL, H5P_DEFAULT,
buf);
assert (status >= 0);
return dset;
}
trio_exit_code trio_hdf5_init(trio_t* file) {
trio_hdf5_t* f = (trio_hdf5_t*) file;
/* If file doesn't exist, create it */
int f_ishere = 0;
struct stat st;
if (stat(file->file_name, &st) == 0) {
printf("%s \n","HDF5 file already exists");
// RDWR OR RDONLY ???
f->file_id = H5Fopen(file->file_name, H5F_ACC_RDWR, H5P_DEFAULT);
f_ishere = 1;
} else {
f->file_id = H5Fcreate(file->file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
f_ishere = 0;
}
/* Create groups in the hdf5 file */
if (f_ishere == 0){
f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
//f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
} else {
f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
}
assert (f->nucleus_group > 0L);
//assert (f->electron_group > 0L);
return TRIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trio_hdf5.h
trio_exit_code trio_hdf5_finalize(trio_t* file);
#+end_src
#+begin_src c :tangle trio_hdf5.c
trio_exit_code trio_hdf5_finalize(trio_t* file) {
trio_hdf5_t* f = (trio_hdf5_t*) file;
H5Gclose(f->nucleus_group);
f->nucleus_group = 0;
/*
H5Gclose(f->electron_group);
f->electron_group = 0;
*/
H5Fclose(f->file_id);
f->file_id = 0;
return TRIO_SUCCESS;
}
#+end_src
*** Read/write the nucleus struct
#+begin_src c :tangle trio_hdf5.c
h5nucleus_t* trio_hdf5_read_nucleus(const trio_hdf5_t* file) {
/* Allocate the data structure */
h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t));
assert (nucleus != NULL);
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->charge = NULL;
nucleus->h5_coord = NULL;
nucleus->h5_charge = NULL;
/* Check that the file was opened/created correctly, return */
if (file->file_id < 0) return nucleus;
/* Quit if the dimensioning attribute is missing in the file */
if (H5Aexists(file->nucleus_group, NUCLEUS_NUM_NAME) == 0) return nucleus;
herr_t status;
/* Read the nucleus_num attribute of nucleus group */
hid_t num_id;
num_id = H5Aopen(file->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (num_id > 0);
status = H5Aread(num_id, H5T_NATIVE_ULLONG, &(nucleus->num));
assert (status >= 0);
/* Allocate and read nucleus_charge array */
nucleus->charge = (double*) calloc(nucleus->num, sizeof(double));
assert (nucleus->charge != NULL);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(file->nucleus_group,
NUCLEUS_CHARGE_NAME,
nucleus->charge);
/* Allocate and read nucleus_coord array */
nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double));
assert (nucleus->coord != NULL);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(file->nucleus_group,
NUCLEUS_COORD_NAME,
nucleus->coord);
assert (status >= 0);
/* Low-level read. Do not forget to close the associated IDs (dset,dtype,dspace)
* when not used anymore, see below. Note how this function is similar to H5LTread_dataset_double
*/
/*
nucleus->h5_coord = trio_hdf5_read_dset_low(file, NUCLEUS_COORD_NAME,
nucleus->coord);
H5Sclose(nucleus->h5_coord->dspace_id);
H5Tclose(nucleus->h5_coord->dtype_id);
H5Dclose(nucleus->h5_coord->dset_id);
*/
H5Aclose(num_id);
return nucleus;
}
trio_exit_code trio_hdf5_write_nucleus(const trio_hdf5_t* file, h5nucleus_t* nucleus) {
assert (file != NULL);
assert (nucleus != NULL);
herr_t status;
hid_t dspace, dtype;
hid_t attr_id;
dtype = H5Tcopy(H5T_NATIVE_ULLONG);
/* Write the dimensioning variables */
if (H5Aexists(file->nucleus_group, NUCLEUS_NUM_NAME) == 0) {
dspace = H5Screate(H5S_SCALAR);
attr_id = H5Acreate(file->nucleus_group, NUCLEUS_NUM_NAME, dtype, dspace,
H5P_DEFAULT, H5P_DEFAULT);
assert (attr_id > 0);
/* High-level routine does not work for some reason
* status = H5LTset_attribute_ulong (file->nucleus_group, "nucleus", NUCLEUS_NUM_NAME,
* &(nucleus->num), 1);
*/
} else {
attr_id = H5Aopen(file->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (attr_id > 0);
}
status = H5Awrite(attr_id, dtype, &(nucleus->num));
assert (status >= 0);
H5Aclose(attr_id);
/* Write arrays */
hid_t dset_id;
int charge_rank = 1;
const hsize_t charge_dims[1] = {nucleus->num};
if ( H5LTfind_dataset(file->nucleus_group, NUCLEUS_CHARGE_NAME) != 1) {
status = H5LTmake_dataset_double (file->nucleus_group, NUCLEUS_CHARGE_NAME,
charge_rank, charge_dims, nucleus->charge);
assert (status >= 0);
} else {
dset_id = H5Dopen(file->nucleus_group, NUCLEUS_CHARGE_NAME, H5P_DEFAULT);
assert (dset_id > 0);
dspace = H5Dget_space(dset_id);
assert (dspace > 0);
dtype = H5Dget_type(dset_id);
assert (dtype > 0);
int rrank;
hsize_t dims[1] = {0};
rrank = H5Sget_simple_extent_dims(dspace,
dims, NULL);
assert (rrank == charge_rank);
// disabling asserts like this allows to overwrite _num variable
for (int i=0; i<rrank; i++){
assert (dims[i] == charge_dims[i]);
}
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus->charge);
assert (status >= 0);
H5Sclose(dspace);
H5Tclose(dtype);
H5Dclose(dset_id);
}
int coord_rank = 2;
const hsize_t coord_dims[2] = {nucleus->num, 3};
if ( H5LTfind_dataset(file->nucleus_group, NUCLEUS_COORD_NAME) != 1) {
status = H5LTmake_dataset_double (file->nucleus_group, NUCLEUS_COORD_NAME,
coord_rank, coord_dims, nucleus->coord);
assert (status >= 0);
} else {
dset_id = H5Dopen(file->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT);
assert (dset_id > 0);
dspace = H5Dget_space(dset_id);
assert (dspace > 0);
dtype = H5Dget_type(dset_id);
assert (dtype > 0);
int rrank;
hsize_t dims[2] = {0, 0};
rrank = H5Sget_simple_extent_dims(dspace,
dims, NULL);
assert (rrank == coord_rank);
for (int i=0; i<rrank; i++){
assert (dims[i] == coord_dims[i]);
}
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus->coord);
assert (status >= 0);
H5Sclose(dspace);
H5Tclose(dtype);
H5Dclose(dset_id);
}
return TRIO_SUCCESS;
}
#+end_src
*** Free memory
Memory is allocated when reading. The followig function frees memory.
#+begin_src c :tangle trio_hdf5.c
trio_exit_code trio_hdf5_free_nucleus(h5nucleus_t* nucleus) {
if (nucleus == NULL) return TRIO_FAILURE;
if (nucleus->coord != NULL) free (nucleus->coord);
nucleus->coord = NULL;
if (nucleus->charge != NULL) free (nucleus->charge);
nucleus->charge = NULL;
if (nucleus->h5_coord != NULL) free (nucleus->h5_coord);
nucleus->h5_coord = NULL;
if (nucleus->h5_charge != NULL) free (nucleus->h5_charge);
nucleus->h5_charge = NULL;
free (nucleus);
return TRIO_SUCCESS;
}
#+end_src
*** Read/Write the num attribute
#+begin_src c :tangle trio_hdf5.h
trio_exit_code trio_hdf5_read_nucleus_num(const trio_t* file, uint64_t* num);
trio_exit_code trio_hdf5_write_nucleus_num(const trio_t* file, const uint64_t num);
#+end_src
#+begin_src c :tangle trio_hdf5.c
trio_exit_code trio_hdf5_read_nucleus_num(const trio_t* file, uint64_t* num) {
assert (file != NULL);
assert (num != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) {
return TRIO_FAILURE;
}
/**/ *num = nucleus->num;
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_write_nucleus_num(const trio_t* file, const uint64_t num) {
assert (file != NULL);
assert (num > 0L);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
assert (nucleus != NULL);
if (nucleus->num != num) {
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable alreasy exists. Overwriting it is not supported");
trio_hdf5_free_nucleus(nucleus);
return TRIO_FAILURE;
}
nucleus->num = num;
if (nucleus->charge != NULL) free(nucleus->charge);
nucleus->charge = NULL;
nucleus->charge = (double*) calloc(num, sizeof(double));
assert (nucleus->charge != NULL);
if (nucleus->coord != NULL) free(nucleus->coord );
nucleus->coord = NULL;
nucleus->coord = (double*) calloc(3*num, sizeof(double));
assert (nucleus->coord != NULL);
} else {
nucleus->num = num;
}
trio_exit_code rc = trio_hdf5_write_nucleus((trio_hdf5_t*) file, nucleus);
assert (rc == TRIO_SUCCESS);
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
#+end_src
*** Read/Write the coord attribute
The ~coord~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trio_hdf5.h
trio_exit_code trio_hdf5_read_nucleus_coord(const trio_t* file, double* coord);
trio_exit_code trio_hdf5_write_nucleus_coord(const trio_t* file, const double* coord);
#+end_src
#+begin_src c :tangle trio_hdf5.c
trio_exit_code trio_hdf5_read_nucleus_coord(const trio_t* file, double* coord) {
assert (file != NULL);
assert (coord != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) return TRIO_FAILURE;
assert (nucleus->coord != NULL);
for (size_t i=0 ; i<3*nucleus->num ; i++) {
coord[i] = nucleus->coord[i];
}
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_write_nucleus_coord(const trio_t* file, const double* coord) {
assert (file != NULL);
assert (coord != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) return TRIO_FAILURE;
assert (nucleus->coord != NULL);
for (size_t i=0 ; i<3*nucleus->num ; i++) {
nucleus->coord[i] = coord[i];
}
trio_exit_code rc = trio_hdf5_write_nucleus((trio_hdf5_t*) file, nucleus);
assert (rc == TRIO_SUCCESS);
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
#+end_src
*** TODO Read/Write the charge attribute
* File suffixes :noxport: * File suffixes :noxport:
#+begin_src c :tangle trio.h #+begin_src c :tangle trio.h
@ -827,8 +1363,15 @@ trio_exit_code trio_text_write_nucleus_charge(const trio_t* file, const double*
#+begin_src c :tangle trio_text.h #+begin_src c :tangle trio_text.h
#endif #endif
#+end_src #+end_src
#+begin_src c :tangle trio_hdf5.h
#endif
#+end_src
* TODO Things to be done :noexport: * TODO Things to be done :noexport:
- [ ] Thread safety - [ ] Thread safety
- [ ] Error handling with errno - [ ] Error handling with errno
- [ ] HDF5 back-end
- [ ] JSON back-end
- [ ] File locking with flock - [ ] File locking with flock
- [ ] Caching of the struct saving last modification date in structs - [ ] Caching of the struct saving last modification date in structs

415
src/trio_hdf5.c Normal file
View File

@ -0,0 +1,415 @@
/* This file was generated from the trio.org org-mode file.
To generate it, open trio.org in Emacs and execute
M-x org-babel-tangle
*/
#include "trio_hdf5.h"
#define NUCLEUS_GROUP_NAME "nucleus"
#define NUCLEUS_NUM_NAME "nucleus_num"
#define NUCLEUS_CHARGE_NAME "nucleus_charge"
#define NUCLEUS_COORD_NAME "nucleus_coord"
/*
* Currently H5LTread_dataset_ is used instead of this function
* but keep it for later if we decide to get rid of the H5LT API
*/
dset_t* trio_hdf5_read_dset_low(const trio_hdf5_t* file, const char *dset_name, void *buf) {
assert (file != NULL);
assert (dset_name != NULL);
assert (buf != NULL);
/*
* Low-level implementation. Involves dealing with all HDF5 handles and dimensions
*/
dset_t* dset = (dset_t*) malloc(sizeof(dset_t));
assert (dset != NULL);
dset->dset_id = H5Dopen(file->nucleus_group,
dset_name,
H5P_DEFAULT);
assert (dset->dset_id > 0);
/*
* Get dataspace, datatype and dimensions
* dspace and dtype handles created below have to be closed when not used
*/
dset->dspace_id = H5Dget_space(dset->dset_id);
assert (dset->dspace_id > 0);
dset->dtype_id = H5Dget_type(dset->dset_id);
assert (dset->dtype_id > 0);
/* Check dimensions. Usefull, but then additional parameters
* ranks and dims[] have to be passed to the function
int rrank;
const int rank = 1;
hsize_t dims[1] = {0};
rrank = H5Sget_simple_extent_dims(nucleus->h5_charge->dspace_id,
dims, NULL);
assert (rrank == rank);
for (int i=0; i<rank; i++){
assert (dims[i] > 0);
}
*/
herr_t status;
status = H5Dread(dset->dset_id, dset->dtype_id,
H5S_ALL, H5S_ALL, H5P_DEFAULT,
buf);
assert (status >= 0);
return dset;
}
trio_exit_code trio_hdf5_init(trio_t* file) {
trio_hdf5_t* f = (trio_hdf5_t*) file;
/* If file doesn't exist, create it */
int f_ishere = 0;
struct stat st;
if (stat(file->file_name, &st) == 0) {
printf("%s \n","HDF5 file already exists");
// RDWR OR RDONLY ???
f->file_id = H5Fopen(file->file_name, H5F_ACC_RDWR, H5P_DEFAULT);
f_ishere = 1;
} else {
f->file_id = H5Fcreate(file->file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
f_ishere = 0;
}
/* Create groups in the hdf5 file */
if (f_ishere == 0){
f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
//f->electron_group = H5Gcreate(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
} else {
f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
}
assert (f->nucleus_group > 0L);
//assert (f->electron_group > 0L);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_finalize(trio_t* file) {
trio_hdf5_t* f = (trio_hdf5_t*) file;
H5Gclose(f->nucleus_group);
f->nucleus_group = 0;
/*
H5Gclose(f->electron_group);
f->electron_group = 0;
*/
H5Fclose(f->file_id);
f->file_id = 0;
return TRIO_SUCCESS;
}
h5nucleus_t* trio_hdf5_read_nucleus(const trio_hdf5_t* file) {
/* Allocate the data structure */
h5nucleus_t* nucleus = (h5nucleus_t*) malloc(sizeof(h5nucleus_t));
assert (nucleus != NULL);
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->charge = NULL;
nucleus->h5_coord = NULL;
nucleus->h5_charge = NULL;
/* Check that the file was opened/created correctly, return */
if (file->file_id < 0) return nucleus;
/* Quit if the dimensioning attribute is missing in the file */
if (H5Aexists(file->nucleus_group, NUCLEUS_NUM_NAME) == 0) return nucleus;
herr_t status;
/* Read the nucleus_num attribute of nucleus group */
hid_t num_id;
num_id = H5Aopen(file->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (num_id > 0);
status = H5Aread(num_id, H5T_NATIVE_ULLONG, &(nucleus->num));
assert (status >= 0);
/* Allocate and read nucleus_charge array */
nucleus->charge = (double*) calloc(nucleus->num, sizeof(double));
assert (nucleus->charge != NULL);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(file->nucleus_group,
NUCLEUS_CHARGE_NAME,
nucleus->charge);
/* Allocate and read nucleus_coord array */
nucleus->coord = (double*) calloc(3 * nucleus->num, sizeof(double));
assert (nucleus->coord != NULL);
/* High-level H5LT API. No need to deal with dataspaces and datatypes */
status = H5LTread_dataset_double(file->nucleus_group,
NUCLEUS_COORD_NAME,
nucleus->coord);
assert (status >= 0);
/* Low-level read. Do not forget to close the associated IDs (dset,dtype,dspace)
* when not used anymore, see below. Note how this function is similar to H5LTread_dataset_double
*/
/*
nucleus->h5_coord = trio_hdf5_read_dset_low(file, NUCLEUS_COORD_NAME,
nucleus->coord);
H5Sclose(nucleus->h5_coord->dspace_id);
H5Tclose(nucleus->h5_coord->dtype_id);
H5Dclose(nucleus->h5_coord->dset_id);
*/
H5Aclose(num_id);
return nucleus;
}
trio_exit_code trio_hdf5_write_nucleus(const trio_hdf5_t* file, h5nucleus_t* nucleus) {
assert (file != NULL);
assert (nucleus != NULL);
herr_t status;
hid_t dspace, dtype;
hid_t attr_id;
dtype = H5Tcopy(H5T_NATIVE_ULLONG);
/* Write the dimensioning variables */
if (H5Aexists(file->nucleus_group, NUCLEUS_NUM_NAME) == 0) {
dspace = H5Screate(H5S_SCALAR);
attr_id = H5Acreate(file->nucleus_group, NUCLEUS_NUM_NAME, dtype, dspace,
H5P_DEFAULT, H5P_DEFAULT);
assert (attr_id > 0);
/* High-level routine does not work for some reason
* status = H5LTset_attribute_ulong (file->nucleus_group, "nucleus", NUCLEUS_NUM_NAME,
* &(nucleus->num), 1);
*/
} else {
attr_id = H5Aopen(file->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (attr_id > 0);
}
status = H5Awrite(attr_id, dtype, &(nucleus->num));
assert (status >= 0);
H5Aclose(attr_id);
/* Write arrays */
hid_t dset_id;
int charge_rank = 1;
const hsize_t charge_dims[1] = {nucleus->num};
if ( H5LTfind_dataset(file->nucleus_group, NUCLEUS_CHARGE_NAME) != 1) {
status = H5LTmake_dataset_double (file->nucleus_group, NUCLEUS_CHARGE_NAME,
charge_rank, charge_dims, nucleus->charge);
assert (status >= 0);
} else {
dset_id = H5Dopen(file->nucleus_group, NUCLEUS_CHARGE_NAME, H5P_DEFAULT);
assert (dset_id > 0);
dspace = H5Dget_space(dset_id);
assert (dspace > 0);
dtype = H5Dget_type(dset_id);
assert (dtype > 0);
int rrank;
hsize_t dims[1] = {0};
rrank = H5Sget_simple_extent_dims(dspace,
dims, NULL);
assert (rrank == charge_rank);
// disabling asserts like this allows to overwrite _num variable
for (int i=0; i<rrank; i++){
assert (dims[i] == charge_dims[i]);
}
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus->charge);
assert (status >= 0);
H5Sclose(dspace);
H5Tclose(dtype);
H5Dclose(dset_id);
}
int coord_rank = 2;
const hsize_t coord_dims[2] = {nucleus->num, 3};
if ( H5LTfind_dataset(file->nucleus_group, NUCLEUS_COORD_NAME) != 1) {
status = H5LTmake_dataset_double (file->nucleus_group, NUCLEUS_COORD_NAME,
coord_rank, coord_dims, nucleus->coord);
assert (status >= 0);
} else {
dset_id = H5Dopen(file->nucleus_group, NUCLEUS_COORD_NAME, H5P_DEFAULT);
assert (dset_id > 0);
dspace = H5Dget_space(dset_id);
assert (dspace > 0);
dtype = H5Dget_type(dset_id);
assert (dtype > 0);
int rrank;
hsize_t dims[2] = {0, 0};
rrank = H5Sget_simple_extent_dims(dspace,
dims, NULL);
assert (rrank == coord_rank);
for (int i=0; i<rrank; i++){
assert (dims[i] == coord_dims[i]);
}
status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nucleus->coord);
assert (status >= 0);
H5Sclose(dspace);
H5Tclose(dtype);
H5Dclose(dset_id);
}
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_free_nucleus(h5nucleus_t* nucleus) {
if (nucleus == NULL) return TRIO_FAILURE;
if (nucleus->coord != NULL) free (nucleus->coord);
nucleus->coord = NULL;
if (nucleus->charge != NULL) free (nucleus->charge);
nucleus->charge = NULL;
if (nucleus->h5_coord != NULL) free (nucleus->h5_coord);
nucleus->h5_coord = NULL;
if (nucleus->h5_charge != NULL) free (nucleus->h5_charge);
nucleus->h5_charge = NULL;
free (nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_read_nucleus_num(const trio_t* file, uint64_t* num) {
assert (file != NULL);
assert (num != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) {
return TRIO_FAILURE;
}
/**/ *num = nucleus->num;
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_write_nucleus_num(const trio_t* file, const uint64_t num) {
assert (file != NULL);
assert (num > 0L);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
assert (nucleus != NULL);
if (nucleus->num != num) {
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable alreasy exists. Overwriting it is not supported");
trio_hdf5_free_nucleus(nucleus);
return TRIO_FAILURE;
}
nucleus->num = num;
if (nucleus->charge != NULL) free(nucleus->charge);
nucleus->charge = NULL;
nucleus->charge = (double*) calloc(num, sizeof(double));
assert (nucleus->charge != NULL);
if (nucleus->coord != NULL) free(nucleus->coord );
nucleus->coord = NULL;
nucleus->coord = (double*) calloc(3*num, sizeof(double));
assert (nucleus->coord != NULL);
} else {
nucleus->num = num;
}
trio_exit_code rc = trio_hdf5_write_nucleus((trio_hdf5_t*) file, nucleus);
assert (rc == TRIO_SUCCESS);
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_read_nucleus_coord(const trio_t* file, double* coord) {
assert (file != NULL);
assert (coord != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) return TRIO_FAILURE;
assert (nucleus->coord != NULL);
for (size_t i=0 ; i<3*nucleus->num ; i++) {
coord[i] = nucleus->coord[i];
}
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}
trio_exit_code trio_hdf5_write_nucleus_coord(const trio_t* file, const double* coord) {
assert (file != NULL);
assert (coord != NULL);
h5nucleus_t* nucleus = trio_hdf5_read_nucleus((trio_hdf5_t*) file);
if (nucleus == NULL) return TRIO_FAILURE;
assert (nucleus->coord != NULL);
for (size_t i=0 ; i<3*nucleus->num ; i++) {
nucleus->coord[i] = coord[i];
}
trio_exit_code rc = trio_hdf5_write_nucleus((trio_hdf5_t*) file, nucleus);
assert (rc == TRIO_SUCCESS);
trio_hdf5_free_nucleus(nucleus);
return TRIO_SUCCESS;
}

71
src/trio_hdf5.h Normal file
View File

@ -0,0 +1,71 @@
/* This file was generated from the trio.org org-mode file.
To generate it, open trio.org in Emacs and execute
M-x org-babel-tangle
*/
#ifndef _TRIO_HDF5_H
#define _TRIO_HDF5_H
#include "trio.h"
#include "trio_s.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include "hdf5.h"
#include "hdf5_hl.h" // needed for high-level APIs like H5LT, requires additional linking in Makefile
typedef struct slab_s {
uint64_t a;
uint64_t b;
uint64_t c;
uint64_t d;
} slab_t;
typedef struct dset_s {
hid_t dset_id;
hid_t dspace_id;
hid_t dtype_id;
uint64_t* dims;
uint32_t rank;
const char* dset_name;
} dset_t;
typedef struct h5nucleus_s {
uint64_t num;
double *coord;
double *charge;
dset_t* h5_coord;
dset_t* h5_charge;
} h5nucleus_t;
typedef struct h5electron_s {
uint64_t alpha_num;
uint64_t beta_num;
} h5electron_t;
typedef struct trio_hdf5_s {
trio_t parent ;
hid_t file_id;
hid_t nucleus_group;
hid_t electron_group;
//... other groups' id
const char* file_name;
} trio_hdf5_t;
trio_exit_code trio_hdf5_init(trio_t* file);
trio_exit_code trio_hdf5_finalize(trio_t* file);
trio_exit_code trio_hdf5_read_nucleus_num(const trio_t* file, uint64_t* num);
trio_exit_code trio_hdf5_write_nucleus_num(const trio_t* file, const uint64_t num);
trio_exit_code trio_hdf5_read_nucleus_coord(const trio_t* file, double* coord);
trio_exit_code trio_hdf5_write_nucleus_coord(const trio_t* file, const double* coord);
#endif

View File

@ -82,7 +82,7 @@ nucleus_t* trio_text_read_nucleus(const trio_text_t* file) {
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "num") == 0); assert (strcmp(buffer, "num") == 0);
fscanf(f, "%ld", &(nucleus->num)); fscanf(f, "%lu", &(nucleus->num));
assert (nucleus->num > 0); assert (nucleus->num > 0);
/* Allocate arrays */ /* Allocate arrays */
@ -96,14 +96,14 @@ nucleus_t* trio_text_read_nucleus(const trio_text_t* file) {
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "charge") == 0); assert (strcmp(buffer, "charge") == 0);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
fscanf(f, "%lf", &(nucleus->charge[i])); fscanf(f, "%lf", &(nucleus->charge[i]));
} }
fscanf(f, "%s", buffer); fscanf(f, "%s", buffer);
assert (strcmp(buffer, "coord") == 0); assert (strcmp(buffer, "coord") == 0);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
fscanf(f, "%lf", &(nucleus->coord[i])); fscanf(f, "%lf", &(nucleus->coord[i]));
} }
free(buffer); free(buffer);
@ -123,12 +123,12 @@ trio_exit_code trio_text_write_nucleus(const trio_text_t* file, nucleus_t* nucle
/* Write arrays */ /* Write arrays */
fprintf(f, "charge\n"); fprintf(f, "charge\n");
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
fprintf(f, "%lf\n", nucleus->charge[i]); fprintf(f, "%lf\n", nucleus->charge[i]);
} }
fprintf(f, "coord\n"); fprintf(f, "coord\n");
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]); fprintf(f, "%lf\n", nucleus->coord[i]);
} }
@ -156,7 +156,7 @@ trio_exit_code trio_text_free_nucleus(nucleus_t* nucleus) {
return TRIO_SUCCESS; return TRIO_SUCCESS;
} }
trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num) { trio_exit_code trio_text_read_nucleus_num(const trio_t* file, uint64_t* num) {
assert (file != NULL); assert (file != NULL);
assert (num != NULL); assert (num != NULL);
@ -174,7 +174,7 @@ trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num) {
} }
trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const int64_t num) { trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const uint64_t num) {
assert (num > 0L); assert (num > 0L);
assert (file != NULL); assert (file != NULL);
@ -223,7 +223,7 @@ trio_exit_code trio_text_read_nucleus_coord(const trio_t* file, double* coord) {
assert (coord != NULL); assert (coord != NULL);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
coord[i] = nucleus->coord[i]; coord[i] = nucleus->coord[i];
} }
@ -240,7 +240,7 @@ trio_exit_code trio_text_write_nucleus_coord(const trio_t* file, const double* c
nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*) file); nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*) file);
assert (nucleus != NULL); assert (nucleus != NULL);
for (int i=0 ; i<3*nucleus->num ; i++) { for (size_t i=0 ; i<3*nucleus->num ; i++) {
nucleus->coord[i] = coord[i]; nucleus->coord[i] = coord[i];
} }
@ -264,7 +264,7 @@ trio_exit_code trio_text_read_nucleus_charge(const trio_t* file, double* charge)
assert (charge != NULL); assert (charge != NULL);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
charge[i] = nucleus->charge[i]; charge[i] = nucleus->charge[i];
} }
@ -281,7 +281,7 @@ trio_exit_code trio_text_write_nucleus_charge(const trio_t* file, const double*
nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*)file); nucleus_t* nucleus = trio_text_read_nucleus((trio_text_t*)file);
assert (nucleus != NULL); assert (nucleus != NULL);
for (int i=0 ; i<nucleus->num ; i++) { for (size_t i=0 ; i<nucleus->num ; i++) {
nucleus->charge[i] = charge[i]; nucleus->charge[i] = charge[i];
} }

View File

@ -20,12 +20,12 @@
typedef struct nucleus_s { typedef struct nucleus_s {
double* coord; double* coord;
double* charge; double* charge;
int64_t num; uint64_t num;
} nucleus_t; } nucleus_t;
typedef struct electron_s { typedef struct electron_s {
int64_t alpha_num; uint64_t alpha_num;
int64_t beta_num; uint64_t beta_num;
} electron_t; } electron_t;
typedef struct trio_text_s { typedef struct trio_text_s {
@ -38,8 +38,8 @@ trio_exit_code trio_text_init(trio_t* file);
trio_exit_code trio_text_finalize(trio_t* file); trio_exit_code trio_text_finalize(trio_t* file);
trio_exit_code trio_text_read_nucleus_num(const trio_t* file, int64_t* num); trio_exit_code trio_text_read_nucleus_num(const trio_t* file, uint64_t* num);
trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const int64_t num); trio_exit_code trio_text_write_nucleus_num(const trio_t* file, const uint64_t num);
trio_exit_code trio_text_read_nucleus_coord(const trio_t* file, double* coord); trio_exit_code trio_text_read_nucleus_coord(const trio_t* file, double* coord);
trio_exit_code trio_text_write_nucleus_coord(const trio_t* file, const double* coord); trio_exit_code trio_text_write_nucleus_coord(const trio_t* file, const double* coord);