From 08674345a877d0b4e93bc056edaccd0bc6621e8a Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sun, 10 Nov 2024 19:20:09 +0100 Subject: [PATCH] Fix text interface for determinants --- src/templates_front/templator_front.org | 53 +++++++++++++++++- src/templates_text/templator_text.org | 10 ++-- tests/io_determinant.c | 12 ++++- tests/test_f.f90 | 72 +++++++++++++++++++------ 4 files changed, 124 insertions(+), 23 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 60ae70a..e8a66ca 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -5398,7 +5398,10 @@ trexio_read_determinant_list (trexio_t* const file, const int64_t offset_file, i { if (file == NULL) return TREXIO_INVALID_ARG_1; - if (dset == NULL) return TREXIO_INVALID_ARG_2; + if (offset_file < 0) return TREXIO_INVALID_ARG_2; + if (buffer_size_read == NULL) return TREXIO_INVALID_ARG_3; + if (*buffer_size_read < 0) return TREXIO_INVALID_ARG_3; + if (dset == NULL) return TREXIO_INVALID_ARG_4; if (trexio_has_determinant_list(file) != TREXIO_SUCCESS) return TREXIO_DSET_MISSING; /* Get the number of int bit fields per determinant */ @@ -5459,7 +5462,9 @@ trexio_write_determinant_list (trexio_t* const file, const int64_t offset_file, { if (file == NULL) return TREXIO_INVALID_ARG_1; - if (dset == NULL) return TREXIO_INVALID_ARG_2; + if (offset_file < 0) return TREXIO_INVALID_ARG_2; + if (buffer_size < 0) return TREXIO_INVALID_ARG_3; + if (dset == NULL) return TREXIO_INVALID_ARG_4; /* Get the number of int bit fields per determinant */ int32_t int_num = 0; @@ -5471,6 +5476,50 @@ trexio_write_determinant_list (trexio_t* const file, const int64_t offset_file, assert(file->back_end < TREXIO_INVALID_BACK_END); + /* Read the number of mos */ + int64_t mo_num = 0L; + rc = trexio_read_mo_num_64(file, &mo_num); + if (rc != TREXIO_SUCCESS) return rc; + + // Read up/dn num + int32_t nup = 0; + rc = trexio_read_electron_up_num(file, &nup); + if (rc != TREXIO_SUCCESS) return rc; + + int32_t ndn = 0; + rc = trexio_read_electron_dn_num(file, &ndn); + if (rc != TREXIO_SUCCESS) return rc; + + /* Check all determinants */ + int32_t list_up[nup]; + int32_t list_dn[ndn]; + int32_t occ_num_up; + int32_t occ_num_dn; + for (int64_t i=0 ; i= mo_num) { + return TREXIO_INVALID_ARG_4; + } + } + for (int32_t j=0 ; j= mo_num) { + return TREXIO_INVALID_ARG_4; + } + } + + } + + /* Up to this point, all the determinants have been checked to + have the correct sizes (number of electrons), and MOs in the + correct range */ + switch (file->back_end) { case TREXIO_TEXT: diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 39874fe..d20f626 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -1660,12 +1660,12 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file, if (f == NULL) return TREXIO_FILE_ERROR; /* Specify the line length in order to offset properly. - Each 64-bit integer takes at most 10 slots and requires one space, + Each 64-bit integer takes at most 20 slots and requires one space, we have int_num integers per up-spin determinant, then this number is doubled because we have the same number for down-spin electrons, and then one newline char. ,*/ - uint64_t line_length = dims[1]*11UL + 1UL; // 10 digits per int64_t bitfield + 1 space = 11 spots + 1 newline char + uint64_t line_length = dims[1]*21UL + 1UL; // 20 digits per int64_t bitfield + 1 space = 11 spots + 1 newline char /* Offset in the file according to the provided value of offset_file and optimal line_length */ fseek(f, (long) offset_file * line_length, SEEK_SET); @@ -1677,7 +1677,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file, uint32_t buf_size = sizeof(buffer); /* Parameters to post-process the buffer and to get bit fields integers */ uint64_t accum = 0UL; - uint32_t shift_int64 = 11U; + uint32_t shift_int64 = 21U; /* Counter for number of elements beind processed */ uint64_t count = 0UL; for (uint64_t i=0UL; i < dims[0]; ++i) { @@ -1697,7 +1697,7 @@ trexio_exit_code trexio_text_read_determinant_list(trexio_t* const file, Thus, we parse the buffer string int_num*2 times to get the bit field determinants. ,*/ for (uint32_t j=0; j < (uint32_t) dims[1]; ++j) { - rc = sscanf(buffer+accum, "%10" SCNd64, list + dims[1]*i + j); + rc = sscanf(buffer+accum, "%20" SCNd64, list + dims[1]*i + j); if (rc <= 0) { fclose(f); return TREXIO_FAILURE; @@ -1747,7 +1747,7 @@ trexio_exit_code trexio_text_write_determinant_list(trexio_t* const file, /* The loop below is needed to write a line with int bit fields for alpha and beta electrons */ for (uint32_t j=0; j < (uint32_t) dims[1]; ++j) { - rc = fprintf(f, "%10" PRId64 " ", *(list + i*dims[1] + j)); + rc = fprintf(f, "%20" PRId64 " ", *(list + i*dims[1] + j)); if (rc <= 0) { fclose(f); return TREXIO_FAILURE; diff --git a/tests/io_determinant.c b/tests/io_determinant.c index bd861d2..43849cc 100644 --- a/tests/io_determinant.c +++ b/tests/io_determinant.c @@ -32,6 +32,16 @@ static int test_write_determinant (const char* file_name, const back_end_t backe assert(rc == TREXIO_SUCCESS); } + // write number of up and down electrons for checking consistency of determinants + if (trexio_has_electron_up_num(file) == TREXIO_HAS_NOT) { + rc = trexio_write_electron_up_num(file, 4); + assert(rc == TREXIO_SUCCESS); + } + if (trexio_has_electron_dn_num(file) == TREXIO_HAS_NOT) { + rc = trexio_write_electron_dn_num(file, 3); + assert(rc == TREXIO_SUCCESS); + } + // get the number of int64 bit fields per determinant int int_num; rc = trexio_get_int64_num(file, &int_num); @@ -218,7 +228,7 @@ static int test_read_determinant (const char* file_name, const back_end_t backen /* printf("%s\n", trexio_string_of_error(rc)); for (int i=0; i