From 99d0ef70bd85e45c5a624b0e3022b7683ad9bd85 Mon Sep 17 00:00:00 2001 From: q-posev Date: Wed, 28 Sep 2022 10:22:56 +0200 Subject: [PATCH] Remove excited state switch for I/O and add state_id --- src/templates_front/templator_front.org | 2 +- src/templates_hdf5/templator_hdf5.org | 52 ++++--------------- src/templates_text/templator_text.org | 68 ++++++------------------- tests/io_determinant_hdf5.c | 49 ++++-------------- tests/io_determinant_text.c | 47 ++++------------- tools/generator_tools.py | 16 +----- trex.org | 33 ++++++------ 7 files changed, 67 insertions(+), 200 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 2f0fd6d..7eda456 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -1588,7 +1588,7 @@ def _inquire(file_name: str) -> bool: ** File state ~trexio_set_state~ set an existing ~trexio_t~ file handle to a given state. - ~trexio_get_state~ returns current sate of the ~trexio_t~ file handle. + ~trexio_get_state~ returns current state of the ~trexio_t~ file handle. input parameters: ~file~ -- TREXIO file handle. diff --git a/src/templates_hdf5/templator_hdf5.org b/src/templates_hdf5/templator_hdf5.org index 494dace..a695d6a 100644 --- a/src/templates_hdf5/templator_hdf5.org +++ b/src/templates_hdf5/templator_hdf5.org @@ -693,15 +693,7 @@ trexio_exit_code trexio_hdf5_read_$group_dset$(trexio_t* const file, if (eof_read_size == NULL) return TREXIO_INVALID_ARG_5; if (dset == NULL) return TREXIO_INVALID_ARG_6; - char dset_dset_name[256]; - memset(dset_dset_name, 0, sizeof(dset_dset_name)); - const int32_t trexio_state = $with_state$ ; - - if (trexio_state != 0) { - sprintf(dset_dset_name, "$group_dset$_state_%" PRId32, trexio_state); - } else { - strncpy(dset_dset_name, "$group_dset$", 256); - } + const char dset_name[256] = "$group_dset$"; const trexio_hdf5_t* f = (const trexio_hdf5_t*) file; @@ -710,7 +702,7 @@ trexio_exit_code trexio_hdf5_read_$group_dset$(trexio_t* const file, /* Attempt to read values (if EOF -> eof_read_size is modified with the number of elements read and return code is TREXIO_END) 0 argument below is requires to skip internal treatment specific to sparse indices (i.e. their de-compression).*/ - return trexio_hdf5_open_read_dset_sparse(f->$group$_group, dset_dset_name, 1, offset, count, eof_read_size, 0, dset); + return trexio_hdf5_open_read_dset_sparse(f->$group$_group, dset_name, 1, offset, count, eof_read_size, 0, dset); } #+end_src @@ -725,15 +717,7 @@ trexio_exit_code trexio_hdf5_write_$group_dset$(trexio_t* const file, if (file == NULL) return TREXIO_INVALID_ARG_1; if (dset == NULL) return TREXIO_INVALID_ARG_5; - char dset_dset_name[256]; - memset(dset_dset_name, 0, sizeof(dset_dset_name)); - const int32_t trexio_state = $with_state$ ; - - if (trexio_state != 0) { - sprintf(dset_dset_name, "$group_dset$_state_%" PRId32, trexio_state); - } else { - strncpy(dset_dset_name, "$group_dset$", 256); - } + const char dset_name[256] = "$group_dset$"; trexio_hdf5_t* f = (trexio_hdf5_t*) file; @@ -744,11 +728,11 @@ trexio_exit_code trexio_hdf5_write_$group_dset$(trexio_t* const file, trexio_exit_code rc_write = TREXIO_FAILURE; /* NOTE: chunk size is set upon creation of the HDF5 dataset and cannot be changed ! */ - if ( H5LTfind_dataset(f->$group$_group, dset_dset_name) != 1 ) { + if ( H5LTfind_dataset(f->$group$_group, dset_name) != 1 ) { /* If the file does not exist -> create it and write */ /* Create chunked dataset with dtype datatype and write indices into it */ - rc_write = trexio_hdf5_create_write_dset_sparse(f->$group$_group, dset_dset_name, dtype, chunk_dims, dset); + rc_write = trexio_hdf5_create_write_dset_sparse(f->$group$_group, dset_name, dtype, chunk_dims, dset); if (rc_write != TREXIO_SUCCESS) return rc_write; } else { @@ -756,7 +740,7 @@ trexio_exit_code trexio_hdf5_write_$group_dset$(trexio_t* const file, hsize_t offset_data[1] = {(hsize_t) offset_file}; /* Create chunked dataset with dtype datatype and write indices into it */ - rc_write = trexio_hdf5_open_write_dset_sparse(f->$group$_group, dset_dset_name, dtype, chunk_dims, offset_data, dset); + rc_write = trexio_hdf5_open_write_dset_sparse(f->$group$_group, dset_name, dtype, chunk_dims, offset_data, dset); if (rc_write != TREXIO_SUCCESS) return rc_write; } @@ -771,19 +755,11 @@ trexio_hdf5_read_$group_dset$_size (trexio_t* const file, int64_t* const size_ma if (file == NULL) return TREXIO_INVALID_ARG_1; if (size_max == NULL) return TREXIO_INVALID_ARG_2; - char dset_dset_name[128]; - memset(dset_dset_name, 0, sizeof(dset_dset_name)); - const int32_t trexio_state = file->state; - - if (trexio_state != 0) { - sprintf(dset_dset_name, "$group_dset$_state_%" PRId32, trexio_state); - } else { - strncpy(dset_dset_name, "$group_dset$", 24); - } + const char dset_name[256] = "$group_dset$"; const trexio_hdf5_t* f = (const trexio_hdf5_t*) file; - hid_t dset_id = H5Dopen(f->$group$_group, dset_dset_name, H5P_DEFAULT); + hid_t dset_id = H5Dopen(f->$group$_group, dset_name, H5P_DEFAULT); if (dset_id <= 0) return TREXIO_INVALID_ID; hid_t fspace_id = H5Dget_space(dset_id); @@ -815,17 +791,9 @@ trexio_exit_code trexio_hdf5_has_$group_dset$(trexio_t* const file) trexio_hdf5_t* f = (trexio_hdf5_t*) file; if (f->$group$_group == (hsize_t) 0) return TREXIO_HAS_NOT; - char dset_dset_name[128]; - memset(dset_dset_name, 0, sizeof(dset_dset_name)); - const int32_t trexio_state = file->state; + const char dset_name[256] = "$group_dset$"; - if (trexio_state != 0) { - sprintf(dset_dset_name, "$group_dset$_state_%" PRId32, trexio_state); - } else { - strncpy(dset_dset_name, "$group_dset$", 24); - } - - herr_t status = H5LTfind_dataset(f->$group$_group, dset_dset_name); + herr_t status = H5LTfind_dataset(f->$group$_group, dset_name); /* H5LTfind_dataset returns 1 if dataset exists, 0 otherwise */ if (status == 1){ return TREXIO_SUCCESS; diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 7a08d72..99ff8a2 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -1359,23 +1359,14 @@ trexio_exit_code trexio_text_read_$group_dset$(trexio_t* const file, if (eof_read_size == NULL) return TREXIO_INVALID_ARG_5; if (dset == NULL) return TREXIO_INVALID_ARG_6; - char file_name[256]; - memset(file_name, 0, sizeof(file_name)); - const int32_t trexio_state = $with_state$; - - if (trexio_state != 0) { - sprintf(file_name, "/$group_dset$_state_%" PRId32 ".txt", trexio_state); - } else { - strncpy(file_name, "/$group_dset$.txt", 256); - } + const char file_name[256] = "/$group_dset$.txt"; /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); /* Append name of the file with sparse data */ - strncat (file_full_path, file_name, - TREXIO_MAX_FILENAME_LENGTH-strlen(file_name)); + strncat (file_full_path, file_name, sizeof(file_name)); /* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */ FILE* f = fopen(file_full_path, "r"); @@ -1430,23 +1421,14 @@ trexio_text_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; - char file_name[256]; - memset(file_name, 0, sizeof(file_name)); - const int32_t trexio_state = $with_state$; - - if (trexio_state != 0) { - sprintf(file_name, "/$group_dset$_state_%" PRId32 ".txt.size", trexio_state); - } else { - strncpy(file_name, "/$group_dset$.txt.size", 256); - } + const char file_name[256] = "/$group_dset$.txt.size"; /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); /* Append name of the file with sparse data */ - strncat (file_full_path, file_name, - TREXIO_MAX_FILENAME_LENGTH-strlen(file_name)); + strncat (file_full_path, file_name, sizeof(file_name)); /* Open the file in "r" (read) mode to guarantee that no truncation happens upon consecutive reads */ FILE* f = fopen(file_full_path, "r"); @@ -1488,23 +1470,15 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, if (file == NULL) return TREXIO_INVALID_ARG_1; if (dset == NULL) return TREXIO_INVALID_ARG_5; - char file_name[256]; - memset(file_name, 0, sizeof(file_name)); - const int32_t trexio_state = $with_state$; - - if (trexio_state != 0) { - sprintf(file_name, "/$group_dset$_state_%" PRId32 ".txt", trexio_state); - } else { - strncpy(file_name, "/$group_dset$.txt", 256); - } + const char file_name[256] = "/$group_dset$.txt"; + const int append_str_len = 6; /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name) - append_str_len); /* Append name of the file with sparse data */ - strncat (file_full_path, file_name, - TREXIO_MAX_FILENAME_LENGTH-strlen(file_name)); + strncat (file_full_path, file_name, sizeof(file_name)); /* Open the file in "a" (append) mode to guarantee that no truncation happens upon consecutive writes */ FILE* f = fopen(file_full_path, "a"); @@ -1527,7 +1501,7 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, if (rc != 0) return TREXIO_FILE_ERROR; /* Append .size to the file_full_path in order to write additional info about the written buffer of data */ - strncat(file_full_path, ".size", 6); + strncat(file_full_path, ".size", append_str_len); /* Open the new file in "a" (append) mode to append info about the buffer that has been just written */ FILE *f_wSize = fopen(file_full_path, "a"); @@ -1549,10 +1523,9 @@ trexio_exit_code trexio_text_write_$group_dset$(trexio_t* const file, memset (file_full_path, 0, TREXIO_MAX_FILENAME_LENGTH); /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(group_file_name)); /* Append name of the file with sparse data */ - strncat (file_full_path, group_file_name, - TREXIO_MAX_FILENAME_LENGTH-strlen(group_file_name)); + strncat (file_full_path, group_file_name, sizeof(group_file_name)); bool file_exists = trexio_text_file_exists(file_full_path); @@ -1573,23 +1546,14 @@ trexio_exit_code trexio_text_has_$group_dset$(trexio_t* const file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - char file_name[256]; - memset(file_name, 0, sizeof(file_name)); - const int32_t trexio_state = $with_state$; - - if (trexio_state != 0) { - sprintf(file_name, "/$group_dset$_state_%" PRId32 ".txt", trexio_state); - } else { - strncpy(file_name, "/$group_dset$.txt", 256); - } + const char file_name[256] = "/$group_dset$.txt"; /* The full path to the destination TXT file with sparse data. This will include TREXIO directory name. */ char file_full_path[TREXIO_MAX_FILENAME_LENGTH]; /* Copy directory name in file_full_path */ - strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH); + strncpy (file_full_path, file->file_name, TREXIO_MAX_FILENAME_LENGTH - sizeof(file_name)); /* Append name of the file with sparse data */ - strncat (file_full_path, file_name, - TREXIO_MAX_FILENAME_LENGTH-strlen(file_name)); + strncat (file_full_path, file_name, sizeof(file_name)); /* Check the return code of access function to determine whether the file with data exists or not */ if (access(file_full_path, F_OK) == 0){ diff --git a/tests/io_determinant_hdf5.c b/tests/io_determinant_hdf5.c index c90a281..a11b351 100644 --- a/tests/io_determinant_hdf5.c +++ b/tests/io_determinant_hdf5.c @@ -6,7 +6,7 @@ #define TEST_BACKEND TREXIO_HDF5 #define TREXIO_FILE "test_determinant.h5" -#define RM_COMMAND "rm -f " TREXIO_FILE +#define RM_COMMAND "rm -rf " TREXIO_FILE #define SIZE 100 #define N_CHUNKS 5 #define STATE_TEST 2 @@ -64,6 +64,12 @@ static int test_write_determinant (const char* file_name, const back_end_t backe uint64_t offset_d = 0UL; if (offset != 0L) offset_f += offset; + // write the state_id of a given file: 0 is ground state + if (trexio_has_state_id(file) == TREXIO_HAS_NOT) { + rc = trexio_write_state_id(file, 0); + assert(rc == TREXIO_SUCCESS); + } + // write n_chunks times using write_sparse for(int i=0; i size_max) offset_file_read = 97L; @@ -284,7 +259,6 @@ static int test_read_determinant (const char* file_name, const back_end_t backen assert(rc == TREXIO_SUCCESS); assert(det_num == size_check); - // check conversion of determinants into orbital lists int64_t size_list = TREXIO_NORB_PER_INT * int_num; int32_t* orb_list_up = (int32_t*) calloc(size_list, sizeof(int32_t)); @@ -323,7 +297,6 @@ static int test_read_determinant (const char* file_name, const back_end_t backen // free the memory free(det_list_read); free(det_coef_read); - free(det_coef_s2_read); free(det_list_check); free(orb_list_up); free(orb_list_dn); diff --git a/tests/io_determinant_text.c b/tests/io_determinant_text.c index 50def05..1856703 100644 --- a/tests/io_determinant_text.c +++ b/tests/io_determinant_text.c @@ -64,6 +64,12 @@ static int test_write_determinant (const char* file_name, const back_end_t backe uint64_t offset_d = 0UL; if (offset != 0L) offset_f += offset; + // write the state_id of a given file: 0 is ground state + if (trexio_has_state_id(file) == TREXIO_HAS_NOT) { + rc = trexio_write_state_id(file, 0); + assert(rc == TREXIO_SUCCESS); + } + // write n_chunks times using write_sparse for(int i=0; i size_max) offset_file_read = 97L; @@ -284,7 +259,6 @@ static int test_read_determinant (const char* file_name, const back_end_t backen assert(rc == TREXIO_SUCCESS); assert(det_num == size_check); - // check conversion of determinants into orbital lists int64_t size_list = TREXIO_NORB_PER_INT * int_num; int32_t* orb_list_up = (int32_t*) calloc(size_list, sizeof(int32_t)); @@ -323,7 +297,6 @@ static int test_read_determinant (const char* file_name, const back_end_t backen // free the memory free(det_list_read); free(det_coef_read); - free(det_coef_s2_read); free(det_list_check); free(orb_list_up); free(orb_list_dn); diff --git a/tools/generator_tools.py b/tools/generator_tools.py index 7b61f59..481672f 100644 --- a/tools/generator_tools.py +++ b/tools/generator_tools.py @@ -111,7 +111,7 @@ def recursive_populate_file(fname: str, paths: dict, detailed_source: dict) -> N 'group_dset_rank', 'group_dset_dim_list', 'group_dset_f_dims', 'group_num_f_dtype_default', 'group_num_f_dtype_double', 'group_num_f_dtype_single', 'group_num_dtype_default', 'group_num_dtype_double', 'group_num_dtype_single', - 'group_num_h5_dtype', 'group_num_py_dtype', 'with_state', + 'group_num_h5_dtype', 'group_num_py_dtype', 'group_dset_format_scanf', 'group_dset_format_printf', 'group_dset_sparse_dim', 'group_dset_sparse_indices_printf', 'group_dset_sparse_indices_scanf', 'sparse_format_printf_8', 'sparse_format_printf_16', 'sparse_format_printf_32', @@ -715,19 +715,7 @@ def split_dset_dict_detailed (datasets: dict) -> tuple: tmp_dict['is_index'] = 'false' # add the list of dimensions - if 'state.num' in v[1] and len(v[1]) > 1: - tmp_dict['with_state'] = 'file->state' - # we have to remove state.num from the list of dimensions - # because it is opaque dimension, namely it is controlled by a global state switch - tmp_dict['dims'] = [dim.replace('.','_') for dim in v[1] if 'state.num' not in dim] - rank -= 1 - elif 'state.num' in v[1] and len(v[1]) == 1: - tmp_dict['with_state'] = '0' - tmp_dict['dims'] = ['state_num'] - rank = 1 - else: - tmp_dict['with_state'] = '0' - tmp_dict['dims'] = [dim.replace('.','_') for dim in v[1]] + tmp_dict['dims'] = [dim.replace('.','_') for dim in v[1]] # add the rank tmp_dict['rank'] = rank diff --git a/trex.org b/trex.org index 3850fd1..25ff482 100644 --- a/trex.org +++ b/trex.org @@ -702,11 +702,11 @@ prim_factor = An illustration on how to read determinants is presented in the [[./examples.html][examples]]. #+NAME: determinant - | Variable | Type | Dimensions | Description | - |---------------+----------------+-------------------------------+--------------------------------------------------------| - | ~num~ | ~dim readonly~ | | Number of determinants | - | ~list~ | ~int special~ | ~(determinant.num)~ | List of determinants as integer bit fields | - | ~coefficient~ | ~float buffer~ | ~(state.num,determinant.num)~ | Coefficients of the determinants from the CI expansion | + | Variable | Type | Dimensions | Description | + |---------------+----------------+---------------------+--------------------------------------------------------| + | ~num~ | ~dim readonly~ | | Number of determinants | + | ~list~ | ~int special~ | ~(determinant.num)~ | List of determinants as integer bit fields | + | ~coefficient~ | ~float buffer~ | ~(determinant.num)~ | Coefficients of the determinants from the CI expansion | #+CALL: json(data=determinant, title="determinant") @@ -714,9 +714,9 @@ prim_factor = :results: #+begin_src python :tangle trex.json "determinant": { - "num" : [ "dim readonly", [] ] - , "list" : [ "int special" , [ "determinant.num" ] ] - , "coefficient" : [ "float buffer", [ "determinant.num", "state.num" ] ] + "num" : [ "dim readonly", [] ] + , "list" : [ "int special" , [ "determinant.num" ] ] + , "coefficient" : [ "float buffer", [ "determinant.num" ] ] } , #+end_src :end: @@ -741,7 +741,7 @@ prim_factor = | Variable | Type | Dimensions | Description | |-------------------+----------------+-----------------------------+------------------------------------------------| | ~num~ | ~dim readonly~ | | Number of CSFs | - | ~coefficient~ | ~float buffer~ | ~(state.num,csf.num)~ | Coefficients of the CSFs from the CI expansion | + | ~coefficient~ | ~float buffer~ | ~(csf.num)~ | Coefficients of the CSFs from the CI expansion | | ~det_coefficient~ | ~float sparse~ | ~(determinant.num,csf.num)~ | Projection on the determinant basis | #+CALL: json(data=csf, title="csf") @@ -751,7 +751,7 @@ prim_factor = #+begin_src python :tangle trex.json "csf": { "num" : [ "dim readonly", [] ] - , "coefficient" : [ "float buffer", [ "csf.num", "state.num" ] ] + , "coefficient" : [ "float buffer", [ "csf.num" ] ] , "det_coefficient" : [ "float sparse", [ "csf.num", "determinant.num" ] ] } , #+end_src @@ -759,15 +759,15 @@ prim_factor = * Excited states (state group) - By default, the ~determinant~ group corresponds to the ground state. - However, it should be also possible to store the coefficients that - correspond to excited state wave functions for the same set of - determinants. This is the goal of the present group + This group contains information about excited state. Since TREXIO version 2.3.0 + the state-specific data (e.g. CI/CSF coeffcients, RDMs) is written in a separate + file in order to avoid over-complicated internal logics and global state switches. #+NAME: state | Variable | Type | Dimensions | Description | |----------+-------+---------------+------------------------------------------------| | ~num~ | ~dim~ | | Number of states (including the ground state) | + | ~id~ | ~int~ | | Index of a current state (0 is ground state) | | ~label~ | ~str~ | ~(state.num)~ | Label of a given state (e.g. 'S' for singlets) | #+CALL: json(data=state, title="state") @@ -776,8 +776,9 @@ prim_factor = :results: #+begin_src python :tangle trex.json "state": { - "num" : [ "dim", [] ] - , "label" : [ "str", [ "state.num" ] ] + "num" : [ "dim", [] ] + , "id" : [ "int", [] ] + , "label" : [ "str", [ "state.num" ] ] } , #+end_src :end: