diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index e0cb18b..909c27c 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -158,43 +158,44 @@ __trexio_path__ = None ** Error handling #+NAME: table-exit-codes - | Macro | Code | Description | - |------------------------------+------+----------------------------------------| - | ~TREXIO_FAILURE~ | -1 | 'Unknown failure' | - | ~TREXIO_SUCCESS~ | 0 | 'Success' | - | ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' | - | ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' | - | ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' | - | ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' | - | ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' | - | ~TREXIO_END~ | 6 | 'End of file' | - | ~TREXIO_READONLY~ | 7 | 'Read-only file' | - | ~TREXIO_ERRNO~ | 8 | strerror(errno) | - | ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' | - | ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' | - | ~TREXIO_HAS_NOT~ | 11 | 'Element absent' | - | ~TREXIO_INVALID_NUM~ | 12 | 'Invalid (negative or 0) dimension' | - | ~TREXIO_ATTR_ALREADY_EXISTS~ | 13 | 'Attribute already exists' | - | ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' | - | ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' | - | ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' | - | ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' | - | ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file' | - | ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' | - | ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' | - | ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' | - | ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' | - | ~TREXIO_UNSAFE_ARRAY_DIM~ | 23 | 'Access to memory beyond allocated' | - | ~TREXIO_ATTR_MISSING~ | 24 | 'Attribute does not exist in the file' | - | ~TREXIO_DSET_MISSING~ | 25 | 'Dataset does not exist in the file' | - | ~TREXIO_BACK_END_MISSING~ | 26 | 'Requested back end is disabled' | - | ~TREXIO_INVALID_ARG_6~ | 27 | 'Invalid argument 6' | - | ~TREXIO_INVALID_ARG_7~ | 28 | 'Invalid argument 7' | - | ~TREXIO_INVALID_ARG_8~ | 29 | 'Invalid argument 8' | - | ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' | - | ~TREXIO_INT_SIZE_OVERFLOW~ | 31 | 'Possible integer overflow' | - | ~TREXIO_SAFE_MODE~ | 32 | 'Unsafe operation in safe mode' | - | ~TREXIO_INVALID_STATE~ | 33 | 'Inconsistent state of the file' | + | Macro | Code | Description | + |-------------------------------+------+----------------------------------------| + | ~TREXIO_FAILURE~ | -1 | 'Unknown failure' | + | ~TREXIO_SUCCESS~ | 0 | 'Success' | + | ~TREXIO_INVALID_ARG_1~ | 1 | 'Invalid argument 1' | + | ~TREXIO_INVALID_ARG_2~ | 2 | 'Invalid argument 2' | + | ~TREXIO_INVALID_ARG_3~ | 3 | 'Invalid argument 3' | + | ~TREXIO_INVALID_ARG_4~ | 4 | 'Invalid argument 4' | + | ~TREXIO_INVALID_ARG_5~ | 5 | 'Invalid argument 5' | + | ~TREXIO_END~ | 6 | 'End of file' | + | ~TREXIO_READONLY~ | 7 | 'Read-only file' | + | ~TREXIO_ERRNO~ | 8 | strerror(errno) | + | ~TREXIO_INVALID_ID~ | 9 | 'Invalid ID' | + | ~TREXIO_ALLOCATION_FAILED~ | 10 | 'Allocation failed' | + | ~TREXIO_HAS_NOT~ | 11 | 'Element absent' | + | ~TREXIO_INVALID_NUM~ | 12 | 'Invalid (negative or 0) dimension' | + | ~TREXIO_ATTR_ALREADY_EXISTS~ | 13 | 'Attribute already exists' | + | ~TREXIO_DSET_ALREADY_EXISTS~ | 14 | 'Dataset already exists' | + | ~TREXIO_OPEN_ERROR~ | 15 | 'Error opening file' | + | ~TREXIO_LOCK_ERROR~ | 16 | 'Error locking file' | + | ~TREXIO_UNLOCK_ERROR~ | 17 | 'Error unlocking file' | + | ~TREXIO_FILE_ERROR~ | 18 | 'Invalid file' | + | ~TREXIO_GROUP_READ_ERROR~ | 19 | 'Error reading group' | + | ~TREXIO_GROUP_WRITE_ERROR~ | 20 | 'Error writing group' | + | ~TREXIO_ELEM_READ_ERROR~ | 21 | 'Error reading element' | + | ~TREXIO_ELEM_WRITE_ERROR~ | 22 | 'Error writing element' | + | ~TREXIO_UNSAFE_ARRAY_DIM~ | 23 | 'Access to memory beyond allocated' | + | ~TREXIO_ATTR_MISSING~ | 24 | 'Attribute does not exist in the file' | + | ~TREXIO_DSET_MISSING~ | 25 | 'Dataset does not exist in the file' | + | ~TREXIO_BACK_END_MISSING~ | 26 | 'Requested back end is disabled' | + | ~TREXIO_INVALID_ARG_6~ | 27 | 'Invalid argument 6' | + | ~TREXIO_INVALID_ARG_7~ | 28 | 'Invalid argument 7' | + | ~TREXIO_INVALID_ARG_8~ | 29 | 'Invalid argument 8' | + | ~TREXIO_INVALID_STR_LEN~ | 30 | 'Invalid max_str_len' | + | ~TREXIO_INT_SIZE_OVERFLOW~ | 31 | 'Possible integer overflow' | + | ~TREXIO_SAFE_MODE~ | 32 | 'Unsafe operation in safe mode' | + | ~TREXIO_INVALID_ELECTRON_NUM~ | 33 | 'Inconsistent value of electron num' | + | ~TREXIO_INVALID_STATE~ | 34 | 'Inconsistent state of the file' | # We need to force Emacs not to indent the Python code: # -*- org-src-preserve-indentation: t @@ -274,7 +275,8 @@ return '\n'.join(result) #define TREXIO_INVALID_STR_LEN ((trexio_exit_code) 30) #define TREXIO_INT_SIZE_OVERFLOW ((trexio_exit_code) 31) #define TREXIO_SAFE_MODE ((trexio_exit_code) 32) - #define TREXIO_INVALID_STATE ((trexio_exit_code) 33) + #define TREXIO_INVALID_ELECTRON_NUM ((trexio_exit_code) 33) + #define TREXIO_INVALID_STATE ((trexio_exit_code) 34) #+end_src #+begin_src f90 :tangle prefix_fortran.f90 :exports none @@ -312,7 +314,8 @@ return '\n'.join(result) integer(trexio_exit_code), parameter :: TREXIO_INVALID_STR_LEN = 30 integer(trexio_exit_code), parameter :: TREXIO_INT_SIZE_OVERFLOW = 31 integer(trexio_exit_code), parameter :: TREXIO_SAFE_MODE = 32 - integer(trexio_exit_code), parameter :: TREXIO_INVALID_STATE = 33 + integer(trexio_exit_code), parameter :: TREXIO_INVALID_ELECTRON_NUM = 33 + integer(trexio_exit_code), parameter :: TREXIO_INVALID_STATE = 34 #+end_src #+begin_src python :tangle prefix_python.py :exports none @@ -351,7 +354,8 @@ return '\n'.join(result) TREXIO_INVALID_STR_LEN = 30 TREXIO_INT_SIZE_OVERFLOW = 31 TREXIO_SAFE_MODE = 32 - TREXIO_INVALID_STATE = 33 + TREXIO_INVALID_ELECTRON_NUM = 33 + TREXIO_INVALID_STATE = 34 #+end_src :END: @@ -496,6 +500,9 @@ return '\n'.join(result) case TREXIO_SAFE_MODE: return "Unsafe operation in safe mode"; break; + case TREXIO_INVALID_ELECTRON_NUM: + return "Inconsistent value of electron num"; + break; case TREXIO_INVALID_STATE: return "Inconsistent state of the file"; break; @@ -1262,7 +1269,7 @@ trexio_close (trexio_t* file) assert(file->back_end < TREXIO_INVALID_BACK_END); - /* Things to be done before the closing the file in the back-end */ + /* Things to be done before closing the file in the back-end */ rc = trexio_pre_close(file); if (rc != TREXIO_SUCCESS) return rc; @@ -1521,14 +1528,15 @@ trexio_pre_close (trexio_t* file) if (file == NULL) return TREXIO_FILE_ERROR; - { /* Up-spin and down-spin electrons */ - trexio_exit_code rc; + /* Up-spin and down-spin electrons */ + trexio_exit_code rc; - int32_t nup, ndn, nelec; - bool has_up = (trexio_has_electron_up_num(file) == TREXIO_SUCCESS); - bool has_dn = (trexio_has_electron_dn_num(file) == TREXIO_SUCCESS); - bool has_updn = (trexio_has_electron_num(file) == TREXIO_SUCCESS); + int32_t nup, ndn, nelec; + bool has_up = (trexio_has_electron_up_num(file) == TREXIO_SUCCESS); + bool has_dn = (trexio_has_electron_dn_num(file) == TREXIO_SUCCESS); + bool has_updn = (trexio_has_electron_num(file) == TREXIO_SUCCESS); + if (file->mode != 'r') { if (has_updn && has_up && has_dn) { rc = trexio_read_electron_up_num(file, &nup); if (rc != TREXIO_SUCCESS) return rc; @@ -1540,9 +1548,13 @@ trexio_pre_close (trexio_t* file) if (rc != TREXIO_SUCCESS) return rc; if (nelec != nup + ndn) { - nelec = nup + ndn; - rc = trexio_write_electron_num(file, nelec); - if (rc != TREXIO_SUCCESS) return rc; + if (file->mode == 'u') { + nelec = nup + ndn; + rc = trexio_write_electron_num(file, nelec); + if (rc != TREXIO_SUCCESS) return rc; + } else { + return TREXIO_INVALID_ELECTRON_NUM; + } } } else if (has_up && has_dn) { rc = trexio_read_electron_up_num(file, &nup); @@ -1577,7 +1589,6 @@ trexio_pre_close (trexio_t* file) rc = trexio_write_electron_num(file, nelec); if (rc != TREXIO_SUCCESS) return rc; } - } return TREXIO_SUCCESS; diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index f7108bb..343d175 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -137,18 +137,20 @@ trexio_hdf5_init (trexio_t* const file) /* Create or open groups in the hdf5 file assuming that they exist if file exists */ switch (file->mode) { case 'r': - f->$group$_group = H5Gopen(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT); + if (H5Lexists(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT) > 0) f->$group$_group = H5Gopen(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT); + if (H5Lexists(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT) == 0) f->$group$_group = (hid_t) 0; break; case 'u': case 'w': if (f_exists == 1) { - f->$group$_group = H5Gopen(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT); + if (H5Lexists(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT) > 0) f->$group$_group = H5Gopen(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT); + if (H5Lexists(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT) == 0) f->$group$_group = H5Gcreate(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } else { f->$group$_group = H5Gcreate(f->file_id, $GROUP$_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } break; } - if (f->$group$_group <= 0L) return TREXIO_INVALID_ID; + if (f->$group$_group < (hid_t) 0) return TREXIO_INVALID_ID; return TREXIO_SUCCESS; } @@ -161,7 +163,7 @@ trexio_hdf5_deinit (trexio_t* const file) trexio_hdf5_t* f = (trexio_hdf5_t*) file; - H5Gclose(f->$group$_group); + if (f->$group$_group != (hid_t) 0) H5Gclose(f->$group$_group); f->$group$_group = 0; H5Fclose(f->file_id);