1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-31 16:45:59 +01:00

BUG: truncated precision for doubles [text]

This commit is contained in:
q-posev 2021-03-30 14:20:46 +02:00
parent 53d51a7902
commit 5fb1418f9b
2 changed files with 78 additions and 35 deletions

View File

@ -6,27 +6,31 @@
int test_read();
int test_write();
int test_h5read();
int test_h5write();
int main() {
/*============== Main test launcher ================*/
test_h5write();
test_h5read();
test_write();
test_read();
return 0 ;
}
int test_h5write() {
const char* file_name = "test_write.h5";
/*======== Test write using HDF5 backend ===========*/
const char* file_name = "test_write.h5";
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int64_t num = 12;
double coord[36] = {
@ -44,26 +48,31 @@ int test_h5write() {
0.00000000 , 2.47304151 , 0.00000000 ,
};
/*================= START OF TEST ==================*/
// open file in 'write' mode
file = trexio_open(file_name, 'w', TREXIO_HDF5);
assert (file != NULL);
// check that certain data does not exist in the file
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_HAS_NOT);
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_HAS_NOT);
// works: try writing info in an empty file
// write info in an empty file
rc = trexio_write_nucleus_num(file,num);
assert (rc == TREXIO_SUCCESS);
rc = trexio_write_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
// check if the written data exists in the file
rc = trexio_has_nucleus_num(file);
assert (rc == TREXIO_SUCCESS);
rc = trexio_has_nucleus_coord(file);
assert (rc == TREXIO_SUCCESS);
// should not work: try to rewrite the nucleus_num
// should not work: try to overwrite the nucleus_num
rc = trexio_write_nucleus_num(file,25);
assert (rc != TREXIO_SUCCESS);
@ -71,82 +80,94 @@ int test_h5write() {
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// open file again in 'append' mode
file = trexio_open(file_name, 'a', TREXIO_HDF5);
assert (file != NULL);
// works: try to read the nucleus_num from existing file
// read the nucleus_num from existing file
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
// works: try to rewrite the nucleus_coord
// overwrite the nucleus_coord
coord[0] = 666.666;
rc = trexio_write_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST ==================*/
return 0;
}
int test_h5read() {
const char* file_name = "test_write.h5";
/*========= Test read using HDF5 backend ===========*/
const char* file_name = "test_write.h5";
trexio_t* file = NULL;
trexio_exit_code rc;
int64_t num;
double* coord;
// test reading existing file [created by test_h5write()], should work
/*================= START OF TEST ==================*/
// open existing file on 'read' mode [created by test_h5write()]
file = trexio_open(file_name, 'r', TREXIO_HDF5);
assert (file != NULL);
// read nucleus_num
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
// read nucleus_coord
coord = (double*) calloc(3*num, sizeof(double));
rc = trexio_read_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
double x = coord[30] - 2.14171677;
assert( x*x < 1.e-12);
assert( x*x < 1.e-14);
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
// test reading non-existing file, should fail and return NULL
// read non-existing file, should fail and return NULL
const char* file_name2 = "test_nonexisting.h5";
trexio_t* file2 = NULL;
file2 = trexio_open(file_name2, 'r', TREXIO_HDF5);
assert (file2 == NULL);
// test appending non-existing file, should fail and return NULL
// append non-existing file, should fail and return NULL
trexio_t* file3 = NULL;
file3 = trexio_open(file_name2, 'a', TREXIO_HDF5);
assert (file3 == NULL);
free(coord);
/*================= END OF TEST =====================*/
free(coord);
return 0;
}
int test_write() {
const char* file_name = "trexio_test";
/*========= Test write using TEXT backend ===========*/
const char* file_name = "trexio_test";
trexio_t* file = NULL;
trexio_exit_code rc;
// parameters to be written
int64_t num = 12;
double charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.};
double coord[36] = {
0.00000000 , 1.39250319 , 0.00000000 ,
-1.20594314 , 0.69625160 , 0.00000000 ,
@ -162,6 +183,8 @@ int test_write() {
0.00000000 , 2.47304151 , 0.00000000 ,
};
/*================= START OF TEST ==================*/
file = trexio_open(file_name, 'w', TREXIO_TEXT);
assert (file != NULL);
@ -187,12 +210,16 @@ int test_write() {
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST =====================*/
return 0;
}
int test_read() {
const char* file_name = "trexio_test";
/*========= Test read using TEXT backend ===========*/
const char* file_name = "trexio_test";
trexio_t* file = NULL;
trexio_exit_code rc;
@ -200,6 +227,8 @@ int test_read() {
double* charge;
double* coord;
/*================= START OF TEST ==================*/
file = trexio_open(file_name, 'r', TREXIO_TEXT);
assert (file != NULL);
@ -217,14 +246,15 @@ int test_read() {
assert (rc == TREXIO_SUCCESS);
double x = coord[30] - 2.14171677;
assert( x*x < 1.e-12);
assert(x*x < 1.e-14);
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
/*================= END OF TEST =====================*/
free(charge);
free(coord);
return 0;
}

View File

@ -6,6 +6,8 @@ program test_trexio
end program test_trexio
subroutine test_write()
! ============ Test write functionality =============== !
use trexio
implicit none
@ -18,20 +20,23 @@ subroutine test_write()
double precision :: charge(12)
double precision :: coord(36)
! parameters to be written
num = 12
charge = (/6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1. /)
coord = (/ 0.00000000 , 1.39250319 , 0.00000000 , &
-1.20594314 , 0.69625160 , 0.00000000 , &
-1.20594314 , -0.69625160 , 0.00000000 , &
0.00000000 , -1.39250319 , 0.00000000 , &
1.20594314 , -0.69625160 , 0.00000000 , &
1.20594314 , 0.69625160 , 0.00000000 , &
-2.14171677 , 1.23652075 , 0.00000000 , &
-2.14171677 , -1.23652075 , 0.00000000 , &
0.00000000 , -2.47304151 , 0.00000000 , &
2.14171677 , -1.23652075 , 0.00000000 , &
2.14171677 , 1.23652075 , 0.00000000 , &
0.00000000 , 2.47304151 , 0.00000000 /)
coord = (/ 0.00000000d0, 1.39250319d0 , 0.00000000d0 , &
-1.20594314d0, 0.69625160d0 , 0.00000000d0 , &
-1.20594314d0, -0.69625160d0 , 0.00000000d0 , &
0.00000000d0, -1.39250319d0 , 0.00000000d0 , &
1.20594314d0, -0.69625160d0 , 0.00000000d0 , &
1.20594314d0, 0.69625160d0 , 0.00000000d0 , &
-2.14171677d0, 1.23652075d0 , 0.00000000d0 , &
-2.14171677d0, -1.23652075d0 , 0.00000000d0 , &
0.00000000d0, -2.47304151d0 , 0.00000000d0 , &
2.14171677d0, -1.23652075d0 , 0.00000000d0 , &
2.14171677d0, 1.23652075d0 , 0.00000000d0 , &
0.00000000d0, 2.47304151d0 , 0.00000000d0 /)
! ================= START OF TEST ===================== !
trex_file = trexio_open('trexio_test_fort', 'w', TREXIO_TEXT)
! trex_file = trexio_open('test_hdf5_fort.h5', 'w', TREXIO_HDF5)
@ -75,10 +80,14 @@ subroutine test_write()
! rc = trexio_close(trex_file)
! if (rc == TREXIO_SUCCESS) write(*,*) 'SUCCESS CLOSE'
! ================= END OF TEST ===================== !
end subroutine test_write
subroutine test_read()
! ============ Test read functionality =============== !
use trexio
implicit none
@ -92,6 +101,8 @@ subroutine test_read()
num = 12
! ================= START OF TEST ===================== !
trex_file = trexio_open('trexio_test_fort', 'r', TREXIO_TEXT)
! trex_file = trexio_open('test_hdf5_fort.h5', 'r', TREXIO_HDF5)
@ -101,14 +112,16 @@ subroutine test_read()
rc = trexio_read_nucleus_charge(trex_file, charge)
if (rc == TREXIO_SUCCESS .and. (abs (charge(11) - 1.0) < 1.0D-8) ) write(*,*) 'SUCCESS READ CHARGE'
if (rc == TREXIO_SUCCESS .and. (abs(charge(11) - 1.0) < 1.0D-8) ) write(*,*) 'SUCCESS READ CHARGE'
rc = trexio_read_nucleus_coord(trex_file, coord)
if (rc == TREXIO_SUCCESS .and. (abs (coord(2,1) - 1.39250319) < 1.0D-8) ) write(*,*) 'SUCCESS READ COORD'
if (rc == TREXIO_SUCCESS .and. (abs(coord(2,1) - 1.39250319d0) < 1.0D-8) ) write(*,*) 'SUCCESS READ COORD'
rc = trexio_close(trex_file)
if (rc == TREXIO_SUCCESS) write(*,*) 'SUCCESS CLOSE'
! ================= END OF TEST ===================== !
end subroutine test_read