1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00

works for nucleus_coord; text backend adapted

This commit is contained in:
q-posev 2021-03-12 11:56:03 +01:00
parent adf44a7ca7
commit cbddfa6010
3 changed files with 134 additions and 38 deletions

View File

@ -108,6 +108,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->to_flush = 0;
@ -141,16 +142,32 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "dim_coord") == 0);
assert (strcmp(buffer, "rank_coord") == 0);
rc = fscanf(f, "%lu", &(nucleus->dim_coord));
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1);
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
uint64_t dim_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
rc = fscanf(f, "%lu", &(nucleus->dims_coord[i]));
assert (rc == 1);
dim_coord *= nucleus->dims_coord[i];
}
/* Allocate arrays */
nucleus->charge = (double*) calloc(nucleus->dim_charge, sizeof(double));
assert (nucleus->charge != NULL);
nucleus->coord = (double*) calloc(nucleus->dim_coord, sizeof(double));
nucleus->coord = (double*) calloc(dim_coord, sizeof(double));
assert (nucleus->coord != NULL);
/* Read data */
@ -174,7 +191,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
assert (rc == 1);
assert (strcmp(buffer, "coord") == 0);
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
for (uint64_t i=0 ; i<dim_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert (rc == 1);
}
@ -208,8 +225,14 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
rewind(f);
/* Write the dimensioning variables */
fprintf(f, "dim_charge %ld\n", nucleus->dim_charge);
fprintf(f, "dim_coord %ld\n", nucleus->dim_coord );
fprintf(f, "dim_charge %ld\n", nucleus->dim_charge );
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
uint64_t dim_coord = (nucleus->dims_coord[0])*(nucleus->dims_coord[1]);
for (uint i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]);
//dim_coord *= nucleus->dims_coord[i];
}
/* Write arrays */
fprintf(f, "num %ld\n", nucleus->num);
@ -219,7 +242,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
}
fprintf(f, "coord\n");
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
for (uint64_t i=0 ; i<dim_coord ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]);
}
fflush(f);
@ -245,6 +268,11 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
free (nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
if (nucleus->coord != NULL) {
free (nucleus->coord);
nucleus->coord = NULL;
@ -289,7 +317,7 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord) {
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
@ -297,9 +325,15 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3;
if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3;
for (uint64_t i=0 ; i<dim_coord ; i++) {
for (uint i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
}
//if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = dims[0]*dims[1];
for (uint64_t i=0 ; i<dim_size ; i++) {
coord[i] = nucleus->coord[i];
}
@ -307,7 +341,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
}
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord) {
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
@ -321,10 +355,23 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
nucleus->coord = NULL;
}
nucleus->dim_coord = dim_coord;
nucleus->coord = (double*) calloc(dim_coord, sizeof(double));
if (nucleus->dims_coord != NULL) {
free(nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
for (uint64_t i=0 ; i<dim_coord ; i++) {
nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
for (uint i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
}
uint64_t dim_size = dims[0]*dims[1];
nucleus->coord = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->coord[i] = coord[i];
}

View File

@ -22,11 +22,12 @@
typedef struct nucleus_s {
FILE* file;
uint64_t dim_coord;
uint64_t dim_charge;
double* coord;
double* charge;
uint64_t num;
uint64_t dim_charge;
uint64_t* dims_coord;
uint32_t rank_coord;
int to_flush;
} nucleus_t;
@ -71,8 +72,8 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file);
trexio_exit_code trexio_text_read_nucleus_num(const trexio_t* file, uint64_t* num);
trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint64_t num);
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord);
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord);
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint64_t dim_charge);
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint64_t dim_charge);

View File

@ -55,11 +55,12 @@
#+begin_src c :tangle trexio_text.h
typedef struct nucleus_s {
FILE* file;
uint64_t dim_coord;
uint64_t dim_charge;
double* coord;
double* charge;
uint64_t num;
uint64_t dim_charge;
uint64_t* dims_coord;
uint32_t rank_coord;
int to_flush;
} nucleus_t;
@ -238,6 +239,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->dims_coord = NULL;
nucleus->charge = NULL;
nucleus->to_flush = 0;
@ -271,16 +273,32 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "dim_coord") == 0);
assert (strcmp(buffer, "rank_coord") == 0);
rc = fscanf(f, "%lu", &(nucleus->dim_coord));
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert (rc == 1);
nucleus->dims_coord = (uint64_t*) calloc(nucleus->rank_coord, sizeof(uint64_t));
assert (nucleus->dims_coord != NULL);
uint64_t dim_coord = 1;
for (uint i=0; i<nucleus->rank_coord; i++){
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
//assert (strcmp(buffer, "dims_coord") == 0);
rc = fscanf(f, "%lu", &(nucleus->dims_coord[i]));
assert (rc == 1);
dim_coord *= nucleus->dims_coord[i];
}
/* Allocate arrays */
nucleus->charge = (double*) calloc(nucleus->dim_charge, sizeof(double));
assert (nucleus->charge != NULL);
nucleus->coord = (double*) calloc(nucleus->dim_coord, sizeof(double));
nucleus->coord = (double*) calloc(dim_coord, sizeof(double));
assert (nucleus->coord != NULL);
/* Read data */
@ -304,7 +322,7 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
assert (rc == 1);
assert (strcmp(buffer, "coord") == 0);
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
for (uint64_t i=0 ; i<dim_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert (rc == 1);
}
@ -346,8 +364,14 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
rewind(f);
/* Write the dimensioning variables */
fprintf(f, "dim_charge %ld\n", nucleus->dim_charge);
fprintf(f, "dim_coord %ld\n", nucleus->dim_coord );
fprintf(f, "dim_charge %ld\n", nucleus->dim_charge );
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
uint64_t dim_coord = (nucleus->dims_coord[0])*(nucleus->dims_coord[1]);
for (uint i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord[%d] %ld\n", i, nucleus->dims_coord[i]);
//dim_coord *= nucleus->dims_coord[i];
}
/* Write arrays */
fprintf(f, "num %ld\n", nucleus->num);
@ -357,7 +381,7 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
}
fprintf(f, "coord\n");
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
for (uint64_t i=0 ; i<dim_coord ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]);
}
fflush(f);
@ -393,6 +417,11 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
nucleus->file = NULL;
}
if (nucleus->dims_coord != NULL) {
free (nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
if (nucleus->coord != NULL) {
free (nucleus->coord);
nucleus->coord = NULL;
@ -452,12 +481,12 @@ trexio_exit_code trexio_text_write_nucleus_num(const trexio_t* file, const uint6
The ~coord~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord);
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord);
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint64_t dim_coord) {
trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* coord, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
@ -465,9 +494,15 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3;
if (rank != nucleus->rank_coord) return TREXIO_INVALID_ARG_3;
for (uint64_t i=0 ; i<dim_coord ; i++) {
for (uint i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
}
//if (dim_coord != nucleus->dim_coord) return TREXIO_INVALID_ARG_3;
uint64_t dim_size = dims[0]*dims[1];
for (uint64_t i=0 ; i<dim_size ; i++) {
coord[i] = nucleus->coord[i];
}
@ -475,7 +510,7 @@ trexio_exit_code trexio_text_read_nucleus_coord(const trexio_t* file, double* co
}
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint64_t dim_coord) {
trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const double* coord, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (coord == NULL) return TREXIO_INVALID_ARG_2;
@ -489,10 +524,23 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
nucleus->coord = NULL;
}
nucleus->dim_coord = dim_coord;
nucleus->coord = (double*) calloc(dim_coord, sizeof(double));
if (nucleus->dims_coord != NULL) {
free(nucleus->dims_coord);
nucleus->dims_coord = NULL;
}
for (uint64_t i=0 ; i<dim_coord ; i++) {
nucleus->rank_coord = rank;
nucleus->dims_coord = (uint64_t*) calloc(rank, sizeof(uint64_t));
for (uint i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
}
uint64_t dim_size = dims[0]*dims[1];
nucleus->coord = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->coord[i] = coord[i];
}