1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-07-22 18:57:39 +02:00
trexio/src/test.c

191 lines
3.9 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-02 11:27:09 +01:00
test_h5write();
test_h5read();
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
uint64_t num = 12;
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-02 13:56:30 +01:00
file = trexio_create(file_name, TREXIO_HDF5);
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);
rc = trexio_write_nucleus_coord(file,coord);
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-02 11:27:09 +01:00
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-02 11:27:09 +01:00
2021-03-02 13:56:30 +01:00
if (rc == TREXIO_SUCCESS) {
2021-03-02 11:27:09 +01:00
printf("SUCCESS\n");
} else {
printf("FAILURE\n");
}
2021-03-02 13:56:30 +01:00
trexio_close(file);
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;
uint64_t num;
double* coord;
2021-03-02 13:56:30 +01:00
file = trexio_create(file_name, TREXIO_HDF5);
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_num(file,&num);
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-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-02 13:56:30 +01:00
if (rc == TREXIO_SUCCESS) {
printf("SUCCESS\n");
} else {
printf("FAILURE\n");
}
2021-03-02 11:27:09 +01:00
2021-03-02 13:56:30 +01:00
trexio_close(file);
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
uint64_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-02 13:56:30 +01:00
file = trexio_create(file_name, TREXIO_TEXT);
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
rc = trexio_write_nucleus_num(file,num);
rc = trexio_write_nucleus_charge(file,charge);
rc = trexio_write_nucleus_coord(file,coord);
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
if (rc == TREXIO_SUCCESS) {
2021-02-20 18:16:10 +01:00
printf("SUCCESS\n");
} else {
printf("FAILURE\n");
}
2021-03-02 13:56:30 +01:00
trexio_close(file);
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
uint64_t num;
2021-02-20 18:16:10 +01:00
double* charge;
double* coord;
2021-03-02 13:56:30 +01:00
file = trexio_create(file_name, TREXIO_TEXT);
2021-02-20 18:16:10 +01:00
2021-03-02 13:56:30 +01:00
rc = trexio_read_nucleus_num(file,&num);
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-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-02-20 18:16:10 +01:00
double x = coord[30] - 2.14171677;
assert( x*x < 1.e-12);
2021-03-02 13:56:30 +01:00
if (rc == TREXIO_SUCCESS) {
2021-02-20 18:16:10 +01:00
printf("SUCCESS\n");
} else {
printf("FAILURE\n");
}
2021-03-02 13:56:30 +01:00
trexio_close(file);
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;
}