1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-23 03:07:45 +02:00
trexio/src/test.c

221 lines
5.0 KiB
C
Raw Normal View History

2021-03-02 13:56:30 +01:00
#include "trexio.h"
2021-02-20 18:16:10 +01:00
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int test_read();
int test_write();
int test_h5read();
2021-03-02 11:27:09 +01:00
int test_h5write();
2021-02-20 18:16:10 +01:00
int main() {
2021-03-03 02:28:00 +01:00
2021-03-02 11:27:09 +01:00
test_h5write();
test_h5read();
2021-03-03 02:28:00 +01:00
2021-02-20 18:16:10 +01:00
test_write();
test_read();
return 0 ;
}
2021-03-02 11:27:09 +01:00
int test_h5write() {
const char* file_name = "test_write.h5";
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
2021-03-02 11:27:09 +01:00
2021-03-02 13:56:30 +01:00
rc = TREXIO_SUCCESS;
2021-03-02 11:27:09 +01:00
2021-03-03 02:28:00 +01:00
int64_t num = 12;
2021-03-02 11:27:09 +01:00
double coord[36] = {
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 ,
};
2021-03-03 02:28:00 +01:00
file = trexio_open(file_name, 'w', TREXIO_HDF5);
assert (file != NULL);
2021-03-02 11:27:09 +01:00
// works: try writing info in an empty file
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
// should not work: try to rewrite the nucleus_num
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,25);
2021-03-03 02:28:00 +01:00
assert (rc != TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
2021-03-03 11:49:42 +01:00
// close current session
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
file = trexio_open(file_name, 'a', TREXIO_HDF5);
assert (file != NULL);
// works: try to read the nucleus_num from existing file
rc = trexio_read_nucleus_num(file,&num);
assert (rc == TREXIO_SUCCESS);
assert (num == 12);
2021-03-02 13:56:30 +01:00
// works: try to rewrite the nucleus_coord
2021-03-02 11:41:25 +01:00
coord[0] = 666.666;
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
return 0;
}
int test_h5read() {
2021-03-02 11:41:25 +01:00
const char* file_name = "test_write.h5";
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
2021-03-03 02:28:00 +01:00
int64_t num;
double* coord;
// test reading existing file [created by test_h5write()], should work
2021-03-03 02:28:00 +01:00
file = trexio_open(file_name, 'r', TREXIO_HDF5);
assert (file != NULL);
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_num(file,&num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:41:25 +01:00
assert (num == 12);
coord = (double*) calloc(3*num, sizeof(double));
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 13:56:30 +01:00
2021-03-02 11:27:09 +01:00
/*for (size_t i=0; i<3*num; i++){
printf("%lf \n", coord[i]);
2021-03-02 11:27:09 +01:00
}*/
2021-03-02 11:41:25 +01:00
double x = coord[30] - 2.14171677;
2021-03-02 11:27:09 +01:00
assert( x*x < 1.e-12);
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
// test reading 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 create it
const char* file_name3 = "test_append.h5";
trexio_t* file3 = NULL;
file3 = trexio_open(file_name3, 'a', TREXIO_HDF5);
assert (file3 != NULL);
rc = trexio_close(file3);
assert (rc == TREXIO_SUCCESS);
2021-03-02 11:27:09 +01:00
free(coord);
return 0;
}
2021-02-20 18:16:10 +01:00
int test_write() {
2021-03-02 13:56:30 +01:00
const char* file_name = "trexio_test";
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
2021-02-20 18:16:10 +01:00
2021-03-03 02:28:00 +01:00
int64_t num = 12;
2021-02-20 18:16:10 +01:00
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 ,
-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 ,
};
2021-03-03 02:28:00 +01:00
file = trexio_open(file_name, 'w', TREXIO_TEXT);
assert (file != NULL);
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_charge(file,charge);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
2021-03-03 02:28:00 +01:00
rc = trexio_write_nucleus_coord(file,coord);
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
return 0;
}
int test_read() {
2021-03-02 13:56:30 +01:00
const char* file_name = "trexio_test";
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
trexio_t* file = NULL;
trexio_exit_code rc;
2021-02-20 18:16:10 +01:00
2021-03-03 02:28:00 +01:00
int64_t num;
2021-02-20 18:16:10 +01:00
double* charge;
double* coord;
2021-03-03 02:28:00 +01:00
file = trexio_open(file_name, 'r', TREXIO_TEXT);
assert (file != NULL);
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_num(file,&num);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
assert (num == 12);
charge = (double*) calloc(num, sizeof(double));
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_charge(file,charge);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
assert(charge[10] == 1.);
coord = (double*) calloc(3*num, sizeof(double));
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_coord(file,coord);
2021-03-03 02:28:00 +01:00
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
double x = coord[30] - 2.14171677;
assert( x*x < 1.e-12);
2021-03-03 02:28:00 +01:00
rc = trexio_close(file);
assert (rc == TREXIO_SUCCESS);
2021-02-20 18:16:10 +01:00
2021-03-02 11:27:09 +01:00
free(charge);
free(coord);
2021-02-20 18:16:10 +01:00
return 0;
}