From 52512b7985e53f6a2714f7b7a904557cd7357952 Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 14 Apr 2022 15:06:57 +0200 Subject: [PATCH 1/2] Fix backwards compativility of the HDF5 back end --- src/templates_hdf5/templator_hdf5.org | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 5d3c41f..d1027ac 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); From aa14f9e477d345f3200b238707123bca935be90c Mon Sep 17 00:00:00 2001 From: q-posev Date: Thu, 14 Apr 2022 16:07:48 +0200 Subject: [PATCH 2/2] Fix pre_close + handle inconsistent electron_num --- src/templates_front/templator_front.org | 168 +++++++++++++----------- 1 file changed, 90 insertions(+), 78 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 16583e6..55c3587 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -158,39 +158,40 @@ __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_STR_LEN~ | 30 | 'Invalid max_str_len' | - | ~TREXIO_INT_SIZE_OVERFLOW~ | 31 | 'Possible integer overflow' | - | ~TREXIO_SAFE_MODE~ | 32 | 'Unsafe operation in safe mode' | + | 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_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' | # We need to force Emacs not to indent the Python code: # -*- org-src-preserve-indentation: t @@ -267,6 +268,7 @@ 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_ELECTRON_NUM ((trexio_exit_code) 33) #+end_src #+begin_src f90 :tangle prefix_fortran.f90 :exports none @@ -301,6 +303,7 @@ 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_ELECTRON_NUM = 33 #+end_src #+begin_src python :tangle prefix_python.py :exports none @@ -336,6 +339,7 @@ return '\n'.join(result) TREXIO_INVALID_STR_LEN = 30 TREXIO_INT_SIZE_OVERFLOW = 31 TREXIO_SAFE_MODE = 32 + TREXIO_INVALID_ELECTRON_NUM = 33 #+end_src :END: @@ -471,6 +475,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; #+end_example **** C source code @@ -1209,7 +1216,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; @@ -1413,55 +1420,60 @@ trexio_pre_close (trexio_t* file) bool has_dn = (trexio_has_electron_dn_num(file) == TREXIO_SUCCESS); bool has_updn = (trexio_has_electron_num(file) == TREXIO_SUCCESS); - if (has_updn && has_up && has_dn) { - rc = trexio_read_electron_up_num(file, &nup); - if (rc != TREXIO_SUCCESS) return rc; - - rc = trexio_read_electron_dn_num(file, &ndn); - if (rc != TREXIO_SUCCESS) return rc; - - rc = trexio_read_electron_num(file, &nelec); - if (rc != TREXIO_SUCCESS) return rc; - - if (nelec != nup + ndn) { + 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; + + rc = trexio_read_electron_dn_num(file, &ndn); + if (rc != TREXIO_SUCCESS) return rc; + + rc = trexio_read_electron_num(file, &nelec); + if (rc != TREXIO_SUCCESS) return rc; + + if (nelec != nup + ndn) { + 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); + if (rc != TREXIO_SUCCESS) return rc; + + rc = trexio_read_electron_dn_num(file, &ndn); + if (rc != TREXIO_SUCCESS) return rc; + nelec = nup + ndn; rc = trexio_write_electron_num(file, nelec); if (rc != TREXIO_SUCCESS) return rc; + } else if (has_up) { + rc = trexio_read_electron_up_num(file, &nup); + if (rc != TREXIO_SUCCESS) return rc; + + ndn = 0; + rc = trexio_write_electron_dn_num(file, ndn); + if (rc != TREXIO_SUCCESS) return rc; + + nelec = nup; + rc = trexio_write_electron_num(file, nelec); + if (rc != TREXIO_SUCCESS) return rc; + } else if (has_dn) { + rc = trexio_read_electron_dn_num(file, &ndn); + if (rc != TREXIO_SUCCESS) return rc; + + nup = 0; + rc = trexio_write_electron_up_num(file, nup); + if (rc != TREXIO_SUCCESS) return rc; + + nelec = ndn; + rc = trexio_write_electron_num(file, nelec); + if (rc != TREXIO_SUCCESS) return rc; } - } else if (has_up && has_dn) { - rc = trexio_read_electron_up_num(file, &nup); - if (rc != TREXIO_SUCCESS) return rc; - - rc = trexio_read_electron_dn_num(file, &ndn); - if (rc != TREXIO_SUCCESS) return rc; - - nelec = nup + ndn; - rc = trexio_write_electron_num(file, nelec); - if (rc != TREXIO_SUCCESS) return rc; - } else if (has_up) { - rc = trexio_read_electron_up_num(file, &nup); - if (rc != TREXIO_SUCCESS) return rc; - - ndn = 0; - rc = trexio_write_electron_dn_num(file, ndn); - if (rc != TREXIO_SUCCESS) return rc; - - nelec = nup; - rc = trexio_write_electron_num(file, nelec); - if (rc != TREXIO_SUCCESS) return rc; - } else if (has_dn) { - rc = trexio_read_electron_dn_num(file, &ndn); - if (rc != TREXIO_SUCCESS) return rc; - - nup = 0; - rc = trexio_write_electron_up_num(file, nup); - if (rc != TREXIO_SUCCESS) return rc; - - nelec = ndn; - rc = trexio_write_electron_num(file, nelec); - if (rc != TREXIO_SUCCESS) return rc; } - } return TREXIO_SUCCESS;