1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

Switch from get/set_state to new read/write_state_id

This commit is contained in:
q-posev 2022-10-07 14:54:37 +02:00
parent d4ffa5f1f7
commit f4c51e8c1f
No known key found for this signature in database
GPG Key ID: 7C477A462FBF9B87
3 changed files with 43 additions and 30 deletions

View File

@ -766,7 +766,7 @@ class File:
One of the currently supported TREXIO open modes.
For example, 'r' or 'w'.
state: int
Active state of the file (needed to write $group_dset$).
Active (excited or ground) state of the file.
Default is 0.
isOpen: bool
Flag indicating whether the current object is still open for I/O
@ -1587,12 +1587,20 @@ def _inquire(file_name: str) -> bool:
** File state
~trexio_set_state~ set an existing ~trexio_t~ file handle to a given state.
**Note:** the use of the functions below is discouraged as of version 2.3.0.
Please use ~trexio_write_state_id~ and ~trexio_read_state_id~ to get the state
corresponding to a given ~trexio_t~ file. There can be only one ~state_id~ per file,
namely data for excited state wave functions have to go in a file different from
the ground state one (see the ~state~ group description for more info about how
you can link the ground state TREXIO file with its excited states).
~trexio_set_state~ set an existing ~trexio_t~ file handle to a given state
and write it as ~state_id~ attribute.
~trexio_get_state~ returns current state of the ~trexio_t~ file handle.
input parameters:
~file~ -- TREXIO file handle.
~state~ -- ~int32_t~ number of state.
~state~ -- ~int32_t~ ID of a state (0 for ground state).
output:
~trexio_exit_code~ exit code.
@ -1610,6 +1618,13 @@ trexio_set_state (trexio_t* file, const int32_t num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
/* Write state_id in the file (as of v.2.3.0) */
trexio_exit_code rc = trexio_has_state_id(file);
if (rc == TREXIO_HAS_NOT || file->mode == 'u') {
trexio_exit_code rc_w = trexio_write_state_id(file, num);
if (rc_w != TREXIO_SUCCESS) return rc_w;
}
file->state = num;
return TREXIO_SUCCESS;
@ -1621,7 +1636,16 @@ trexio_get_state (trexio_t* file, int32_t* const num)
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num == NULL) return TREXIO_INVALID_ARG_2;
*num = file->state;
/* Read state_id from the file (as of v.2.3.0) */
int32_t state_id = 0;
trexio_exit_code rc = trexio_has_state_id(file);
if (rc == TREXIO_SUCCESS) {
trexio_exit_code rc_r = trexio_read_state_id(file, &state_id);
if (rc_r != TREXIO_SUCCESS) return rc_r;
}
/* If the state is not in a file then state_id=0, i.e. ground state */
*num = state_id;
return TREXIO_SUCCESS;
}

View File

@ -66,7 +66,7 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
// 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);
rc = trexio_write_state_id(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
}
@ -79,15 +79,6 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
// The block below will check the set_state
rc = trexio_set_state(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
// set state back to the default 0 (ground state)
rc = trexio_set_state(file, 0);
assert(rc == TREXIO_SUCCESS);
// =================================================
offset_d += chunk_size;
offset_f += chunk_size;
}
@ -134,6 +125,9 @@ static int test_has_determinant(const char* file_name, const back_end_t backend)
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
rc = trexio_has_state_id(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
@ -209,10 +203,10 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
int32_t read_state_id = 666;
rc = trexio_read_state_id(file, &read_state_id);
int32_t state_id = 666;
rc = trexio_read_state_id(file, &state_id);
assert(rc == TREXIO_SUCCESS);
assert(read_state_id == 0);
assert(state_id == STATE_TEST);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;
@ -312,6 +306,7 @@ int main(){
/*============== Test launcher ================*/
int rc;
rc = system(RM_COMMAND);
assert (rc == 0);

View File

@ -66,7 +66,7 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
// 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);
rc = trexio_write_state_id(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
}
@ -79,15 +79,6 @@ static int test_write_determinant (const char* file_name, const back_end_t backe
rc = trexio_write_determinant_coefficient(file, offset_f, chunk_size, &det_coef[offset_d]);
assert(rc == TREXIO_SUCCESS);
// The block below will check the set_state
rc = trexio_set_state(file, STATE_TEST);
assert(rc == TREXIO_SUCCESS);
// set state back to the default 0 (ground state)
rc = trexio_set_state(file, 0);
assert(rc == TREXIO_SUCCESS);
// =================================================
offset_d += chunk_size;
offset_f += chunk_size;
}
@ -134,6 +125,9 @@ static int test_has_determinant(const char* file_name, const back_end_t backend)
rc = trexio_has_determinant_list(file);
assert(rc==TREXIO_SUCCESS);
rc = trexio_has_state_id(file);
assert(rc==TREXIO_SUCCESS);
// now check that previously written determinant_coefficient exists
rc = trexio_has_determinant_coefficient(file);
assert(rc==TREXIO_SUCCESS);
@ -209,10 +203,10 @@ static int test_read_determinant (const char* file_name, const back_end_t backen
//printf("%lf %lf\n", check_diff, det_coef_read[offset_data_read]);
assert(check_diff*check_diff < 1e-14);
int32_t read_state_id = 666;
rc = trexio_read_state_id(file, &read_state_id);
int32_t state_id = 666;
rc = trexio_read_state_id(file, &state_id);
assert(rc == TREXIO_SUCCESS);
assert(read_state_id == 0);
assert(state_id == STATE_TEST);
// now attempt to read so that one encounters end of file during reading (i.e. offset_file_read + chunk_read > size_max)
offset_file_read = 97L;