2021-03-02 13:56:30 +01:00
|
|
|
#include "trexio.h"
|
2021-02-20 18:16:10 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-05-31 16:37:00 +02:00
|
|
|
#include <string.h>
|
2021-02-20 18:16:10 +01:00
|
|
|
|
|
|
|
int test_read();
|
|
|
|
int test_write();
|
2021-02-23 18:20:14 +01:00
|
|
|
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-30 14:20:46 +02:00
|
|
|
/*============== Main test launcher ================*/
|
|
|
|
|
2021-03-02 11:27:09 +01:00
|
|
|
test_h5write();
|
2021-02-23 18:20:14 +01:00
|
|
|
test_h5read();
|
2021-03-03 02:28:00 +01:00
|
|
|
|
2021-02-20 18:16:10 +01:00
|
|
|
test_write();
|
|
|
|
test_read();
|
2021-03-30 14:20:46 +02:00
|
|
|
|
2021-04-07 17:37:04 +02:00
|
|
|
printf("Test error message: %s\n", trexio_string_of_error(TREXIO_INVALID_ARG_2));
|
2021-02-20 18:16:10 +01:00
|
|
|
return 0 ;
|
|
|
|
}
|
|
|
|
|
2021-03-02 11:27:09 +01:00
|
|
|
int test_h5write() {
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*======== Test write using HDF5 backend ===========*/
|
|
|
|
|
|
|
|
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-30 14:20:46 +02:00
|
|
|
// parameters to be written
|
2021-04-07 17:37:04 +02:00
|
|
|
int num = 12;
|
|
|
|
double charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.};
|
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-05-31 16:37:00 +02:00
|
|
|
const char* label[] = {"C" ,
|
|
|
|
"Na",
|
|
|
|
"C" ,
|
|
|
|
"C" ,
|
|
|
|
"C" ,
|
|
|
|
"C" ,
|
|
|
|
"H" ,
|
|
|
|
"Ru",
|
|
|
|
"H" ,
|
|
|
|
"H" ,
|
|
|
|
"H" ,
|
|
|
|
"H" };
|
2021-06-01 14:59:08 +02:00
|
|
|
|
2021-06-01 18:12:15 +02:00
|
|
|
//char labelxxx[] = "C C C Na C C H H H Ru H H";
|
|
|
|
char labelxxx[128] = "";
|
|
|
|
for (int i=0; i<num; i++){
|
|
|
|
strcat(labelxxx,label[i]);
|
2021-06-03 13:42:56 +02:00
|
|
|
strcat(labelxxx,TREXIO_DELIM);
|
2021-06-01 18:12:15 +02:00
|
|
|
}
|
2021-05-31 16:37:00 +02:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= START OF TEST ==================*/
|
|
|
|
|
|
|
|
// open file in 'write' mode
|
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
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// check that certain data does not exist in the file
|
2021-03-30 11:56:04 +02:00
|
|
|
rc = trexio_has_nucleus_num(file);
|
|
|
|
assert (rc == TREXIO_HAS_NOT);
|
|
|
|
rc = trexio_has_nucleus_coord(file);
|
|
|
|
assert (rc == TREXIO_HAS_NOT);
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// write 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-04-07 17:37:04 +02:00
|
|
|
rc = trexio_write_nucleus_charge(file,charge);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-06-01 14:59:08 +02:00
|
|
|
//rc = trexio_write_nucleus_label(file,label);
|
|
|
|
rc = trexio_write_nucleus_label(file,labelxxx);
|
2021-05-31 16:37:00 +02:00
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-03-02 11:27:09 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// check if the written data exists in the file
|
2021-03-30 11:56:04 +02:00
|
|
|
rc = trexio_has_nucleus_num(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
|
|
|
rc = trexio_has_nucleus_coord(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-05-31 16:37:00 +02:00
|
|
|
rc = trexio_has_nucleus_label(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-03-30 11:56:04 +02:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// should not work: try to overwrite the nucleus_num
|
2021-03-02 13:56:30 +01:00
|
|
|
rc = trexio_write_nucleus_num(file,25);
|
2021-05-06 17:44:31 +02:00
|
|
|
assert (rc == TREXIO_NUM_ALREADY_EXISTS);
|
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);
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// open file again in 'append' mode
|
2021-03-03 11:49:42 +01:00
|
|
|
file = trexio_open(file_name, 'a', TREXIO_HDF5);
|
|
|
|
assert (file != NULL);
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// read the nucleus_num from existing file
|
2021-03-03 11:49:42 +01:00
|
|
|
rc = trexio_read_nucleus_num(file,&num);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
|
|
|
assert (num == 12);
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// overwrite 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-30 14:20:46 +02:00
|
|
|
// close current session
|
2021-03-03 02:28:00 +01:00
|
|
|
rc = trexio_close(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-03-02 11:27:09 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= END OF TEST ==================*/
|
|
|
|
|
2021-03-02 11:27:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:20:14 +01:00
|
|
|
int test_h5read() {
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*========= Test read using HDF5 backend ===========*/
|
|
|
|
|
|
|
|
const char* file_name = "test_write.h5";
|
2021-03-02 13:56:30 +01:00
|
|
|
trexio_t* file = NULL;
|
|
|
|
trexio_exit_code rc;
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-04-07 17:37:04 +02:00
|
|
|
int num;
|
2021-02-23 18:20:14 +01:00
|
|
|
double* coord;
|
2021-05-31 16:37:00 +02:00
|
|
|
char** label;
|
2021-06-01 14:59:08 +02:00
|
|
|
char* labelxxx;
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= START OF TEST ==================*/
|
|
|
|
|
|
|
|
// open existing file on 'read' mode [created by test_h5write()]
|
2021-03-03 02:28:00 +01:00
|
|
|
file = trexio_open(file_name, 'r', TREXIO_HDF5);
|
|
|
|
assert (file != NULL);
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// read nucleus_num
|
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);
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// read nucleus_coord
|
2021-02-23 18:20:14 +01:00
|
|
|
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:41:25 +01:00
|
|
|
double x = coord[30] - 2.14171677;
|
2021-03-30 14:20:46 +02:00
|
|
|
assert( x*x < 1.e-14);
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-05-31 16:37:00 +02:00
|
|
|
// read nucleus_label
|
|
|
|
size_t max_str_len = 16;
|
|
|
|
label = (char**) malloc(num*sizeof(char*));
|
|
|
|
for (int i=0; i<num; i++){
|
|
|
|
label[i] = (char*) malloc(max_str_len*sizeof(char));
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:59:08 +02:00
|
|
|
labelxxx = (char*) malloc(num*4*sizeof(char*));
|
|
|
|
|
|
|
|
rc = trexio_read_nucleus_label(file,labelxxx);
|
|
|
|
//rc = trexio_read_nucleus_label(file,label);
|
2021-05-31 16:37:00 +02:00
|
|
|
assert (rc == TREXIO_SUCCESS);
|
|
|
|
|
2021-06-01 14:59:08 +02:00
|
|
|
printf("%s\n", labelxxx);
|
|
|
|
//assert( strcmp(label[1], "Na") == 0 );
|
|
|
|
char * pch;
|
2021-06-03 13:42:56 +02:00
|
|
|
pch = strtok(labelxxx, TREXIO_DELIM);
|
2021-06-01 14:59:08 +02:00
|
|
|
assert( strcmp(pch, "C") == 0 );
|
2021-06-03 13:42:56 +02:00
|
|
|
pch = strtok(NULL, TREXIO_DELIM);
|
|
|
|
assert( strcmp(pch, "Na") == 0 );
|
2021-05-31 16:37:00 +02:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// close current session
|
2021-03-03 02:28:00 +01:00
|
|
|
rc = trexio_close(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
2021-03-02 11:27:09 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// read non-existing file, should fail and return NULL
|
2021-03-03 13:19:47 +01:00
|
|
|
const char* file_name2 = "test_nonexisting.h5";
|
|
|
|
trexio_t* file2 = NULL;
|
|
|
|
|
|
|
|
file2 = trexio_open(file_name2, 'r', TREXIO_HDF5);
|
|
|
|
assert (file2 == NULL);
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
// append non-existing file, should fail and return NULL
|
2021-03-03 13:19:47 +01:00
|
|
|
trexio_t* file3 = NULL;
|
|
|
|
|
2021-03-04 09:41:27 +01:00
|
|
|
file3 = trexio_open(file_name2, 'a', TREXIO_HDF5);
|
|
|
|
assert (file3 == NULL);
|
2021-03-03 13:19:47 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= END OF TEST =====================*/
|
2021-02-23 18:20:14 +01:00
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
free(coord);
|
2021-05-31 16:37:00 +02:00
|
|
|
|
|
|
|
for (int i=0; i<num; i++){
|
|
|
|
free(label[i]);
|
|
|
|
}
|
|
|
|
free(label);
|
|
|
|
|
2021-06-01 14:59:08 +02:00
|
|
|
free(labelxxx);
|
|
|
|
|
2021-02-23 18:20:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-20 18:16:10 +01:00
|
|
|
int test_write() {
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*========= Test write using TEXT backend ===========*/
|
|
|
|
|
|
|
|
const char* file_name = "trexio_test";
|
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-30 14:20:46 +02:00
|
|
|
// parameters to be written
|
2021-04-07 17:37:04 +02:00
|
|
|
int num = 12;
|
|
|
|
float charge[12] = {6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.};
|
2021-02-20 18:16:10 +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-30 14:20:46 +02:00
|
|
|
/*================= START OF TEST ==================*/
|
|
|
|
|
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-30 11:56:04 +02:00
|
|
|
rc = trexio_has_nucleus_num(file);
|
|
|
|
assert (rc == TREXIO_HAS_NOT);
|
|
|
|
rc = trexio_has_nucleus_coord(file);
|
|
|
|
assert (rc == TREXIO_HAS_NOT);
|
|
|
|
|
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-04-07 17:37:04 +02:00
|
|
|
rc = trexio_write_nucleus_charge_32(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-30 11:56:04 +02:00
|
|
|
rc = trexio_has_nucleus_num(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
|
|
|
rc = trexio_has_nucleus_coord(file);
|
|
|
|
assert (rc == TREXIO_SUCCESS);
|
|
|
|
|
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-30 14:20:46 +02:00
|
|
|
/*================= END OF TEST =====================*/
|
|
|
|
|
2021-02-20 18:16:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_read() {
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*========= Test read using TEXT backend ===========*/
|
|
|
|
|
|
|
|
const char* file_name = "trexio_test";
|
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-04-07 17:37:04 +02:00
|
|
|
int num;
|
|
|
|
float* charge;
|
2021-02-20 18:16:10 +01:00
|
|
|
double* coord;
|
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= START OF TEST ==================*/
|
|
|
|
|
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);
|
|
|
|
|
2021-04-07 17:37:04 +02:00
|
|
|
charge = (float*) calloc(num, sizeof(float));
|
|
|
|
rc = trexio_read_nucleus_charge_32(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;
|
2021-03-30 14:20:46 +02:00
|
|
|
assert(x*x < 1.e-14);
|
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
|
|
|
|
2021-03-30 14:20:46 +02:00
|
|
|
/*================= END OF TEST =====================*/
|
|
|
|
|
2021-03-02 11:27:09 +01:00
|
|
|
free(charge);
|
|
|
|
free(coord);
|
2021-02-20 18:16:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|