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

new read/write for numerical attributes

This commit is contained in:
q-posev 2021-03-05 15:49:05 +01:00
parent e88a228bb5
commit 3110a35562
2 changed files with 106 additions and 80 deletions

View File

@ -345,15 +345,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_num(const trexio_t* file, uint64_t* nu
assert (file != NULL);
assert (num != NULL);
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
/* Quit if the dimensioning attribute is missing in the file */
if (H5Aexists(f->nucleus_group, NUCLEUS_NUM_NAME) == 0) return TREXIO_FAILURE;
if (nucleus == NULL) {
return TREXIO_FAILURE;
}
/* Read the nucleus_num attribute of nucleus group */
hid_t num_id = H5Aopen(f->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
/**/ *num = nucleus->num;
herr_t status = H5Aread(num_id, H5T_NATIVE_ULLONG, num);
if (status < 0) return TREXIO_FAILURE;
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_SUCCESS;
}
@ -363,43 +365,55 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
assert (file != NULL);
assert (num > 0L);
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
hid_t num_id;
herr_t status;
/* Write the dimensioning variables */
hid_t dtype = H5Tcopy(H5T_NATIVE_ULLONG);
assert (nucleus != NULL);
if (H5Aexists(f->nucleus_group, NUCLEUS_NUM_NAME) == 0) {
if (nucleus->num != num) {
hid_t dspace = H5Screate(H5S_SCALAR);
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable already exists. Overwriting it is not supported");
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_FAILURE;
}
num_id = H5Acreate(f->nucleus_group, NUCLEUS_NUM_NAME, dtype, dspace,
H5P_DEFAULT, H5P_DEFAULT);
assert (num_id > 0);
nucleus->num = num;
status = H5Awrite(num_id, dtype, &(num));
assert (status >= 0);
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);
H5Sclose(dspace);
} else {
nucleus->num = num;
uint64_t nucleus_num;
trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num));
if (rc != TREXIO_SUCCESS) return rc;
if (nucleus_num != num) {
if (nucleus_num != 0) {
printf("%ld -> %ld %s \n", num, nucleus_num,
"This variable already exists. Overwriting it is not supported");
H5Tclose(dtype);
return TREXIO_FAILURE;
} else {
num_id = H5Aopen(f->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (num_id > 0);
status = H5Awrite(num_id, dtype, &(num));
assert (status >= 0);
}
}
}
trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus);
assert (rc == TREXIO_SUCCESS);
trexio_hdf5_free_nucleus(nucleus);
H5Aclose(num_id);
H5Tclose(dtype);
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_hdf5_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) {

View File

@ -496,15 +496,17 @@ trexio_exit_code trexio_hdf5_read_nucleus_num(const trexio_t* file, uint64_t* nu
assert (file != NULL);
assert (num != NULL);
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
/* Quit if the dimensioning attribute is missing in the file */
if (H5Aexists(f->nucleus_group, NUCLEUS_NUM_NAME) == 0) return TREXIO_FAILURE;
if (nucleus == NULL) {
return TREXIO_FAILURE;
}
/* Read the nucleus_num attribute of nucleus group */
hid_t num_id = H5Aopen(f->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
if (num_id <= 0) return TREXIO_INVALID_ID;
/**/ *num = nucleus->num;
herr_t status = H5Aread(num_id, H5T_NATIVE_ULLONG, num);
if (status < 0) return TREXIO_FAILURE;
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_SUCCESS;
}
@ -514,47 +516,57 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
assert (file != NULL);
assert (num > 0L);
h5nucleus_t* nucleus = trexio_hdf5_read_nucleus((trexio_hdf5_t*) file);
trexio_hdf5_t* f = (trexio_hdf5_t*) file;
hid_t num_id;
herr_t status;
/* Write the dimensioning variables */
hid_t dtype = H5Tcopy(H5T_NATIVE_ULLONG);
assert (nucleus != NULL);
if (H5Aexists(f->nucleus_group, NUCLEUS_NUM_NAME) == 0) {
if (nucleus->num != num) {
hid_t dspace = H5Screate(H5S_SCALAR);
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable already exists. Overwriting it is not supported");
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_FAILURE;
}
num_id = H5Acreate(f->nucleus_group, NUCLEUS_NUM_NAME, dtype, dspace,
H5P_DEFAULT, H5P_DEFAULT);
assert (num_id > 0);
nucleus->num = num;
status = H5Awrite(num_id, dtype, &(num));
assert (status >= 0);
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);
H5Sclose(dspace);
} else {
nucleus->num = num;
uint64_t nucleus_num;
trexio_exit_code rc = trexio_hdf5_read_nucleus_num(file, &(nucleus_num));
if (rc != TREXIO_SUCCESS) return rc;
if (nucleus_num != num) {
if (nucleus_num != 0) {
printf("%ld -> %ld %s \n", num, nucleus_num,
"This variable already exists. Overwriting it is not supported");
H5Tclose(dtype);
return TREXIO_FAILURE;
} else {
num_id = H5Aopen(f->nucleus_group, NUCLEUS_NUM_NAME, H5P_DEFAULT);
assert (num_id > 0);
status = H5Awrite(num_id, dtype, &(num));
assert (status >= 0);
}
}
}
trexio_exit_code rc = trexio_hdf5_write_nucleus((trexio_hdf5_t*) file, nucleus);
assert (rc == TREXIO_SUCCESS);
trexio_hdf5_free_nucleus(nucleus);
H5Aclose(num_id);
H5Tclose(dtype);
return TREXIO_SUCCESS;
}
#+end_src
** Read/Write the coord attribute
The ~coord~ array is assumed allocated with the appropriate size.