1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

adapt recent changes in master text backend

This commit is contained in:
q-posev 2021-03-15 14:13:38 +01:00
parent 44f615857b
commit 0da57b6b6c

View File

@ -2,6 +2,19 @@
* File prefixes :noxport:
#+begin_src emacs-lisp
(setq-local org-babel-default-header-args:c '((:comments . "both")))
org-babel-default-header-args:c
#+end_src
#+RESULTS:
: ((:comments . both))
# Local Variables:
# eval: (setq-local org-babel-default-header-args:Python '((:session . "foo")))
# End:
#+NAME:header
#+begin_src c
/* This file was generated from the trexio.org org-mode file.
@ -19,6 +32,7 @@
#define _TREXIO_TEXT_H
#include "trexio.h"
#include "trexio_private.h"
#include "trexio_s.h"
#include <errno.h>
#include <stdint.h>
@ -33,7 +47,13 @@
#+end_src
#+begin_src c :tangle trexio_text.c :noweb yes
<<header>>
/* This file was generated from the trexio.org org-mode file.
To generate it, open trexio.org in Emacs and execute
M-x org-babel-tangle
*/
#include "trexio_text.h"
#+end_src
@ -55,11 +75,13 @@
#+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 dims_charge[16];
uint64_t dims_coord[16];
uint32_t rank_charge;
uint32_t rank_coord;
int to_flush;
} nucleus_t;
@ -78,13 +100,6 @@ typedef struct rdm_s {
int to_flush;
} rdm_t;
#+end_src
*** TO DO
- to_flush = 1 in write
- to_flush = 0 when flushed
- name
*** Structs for the text back end
#+begin_src c :tangle trexio_text.h
@ -111,6 +126,9 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
trexio_text_t* f = (trexio_text_t*) file;
/* Put all pointers to NULL but leave parent untouched */
memset(&(f->parent)+1,0,sizeof(trexio_text_t)-sizeof(trexio_t));
/* If directory doesn't exist, create it in write mode */
struct stat st;
@ -126,24 +144,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
/* Create the lock file in the directory */
const char* lock_file_name = "/.lock";
char* file_name = (char*)
calloc( strlen(file->file_name) + strlen(lock_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
char* file_name =
CALLOC(strlen(file->file_name) + strlen(lock_file_name) + 1, char);
if (file_name == NULL) {
return TREXIO_ALLOCATION_FAILED;
}
strcpy (file_name, file->file_name);
strcat (file_name, lock_file_name);
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
assert (f->lock_file > 0);
free(file_name);
FREE(file_name);
if (f->lock_file <= 0) {
return TREXIO_FAILURE;
}
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
@ -169,7 +188,6 @@ trexio_exit_code trexio_text_lock(trexio_t* file) {
return TREXIO_SUCCESS;
}
#+end_src
@ -226,27 +244,34 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
#define DEBUG printf("%s : line %d\n", __FILE__, __LINE__);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
if (file == NULL) return NULL;
if (file->nucleus != NULL) return file->nucleus;
/* If the data structure exists, return it */
if (file->nucleus != NULL) {
return file->nucleus;
}
/* Allocate the data structure */
nucleus_t* nucleus = (nucleus_t*) malloc(sizeof(nucleus_t));
assert (nucleus != NULL);
nucleus_t* nucleus = MALLOC(nucleus_t);
if (nucleus == NULL) return NULL;
nucleus->file = NULL;
nucleus->num = 0;
nucleus->coord = NULL;
nucleus->charge = NULL;
nucleus->to_flush = 0;
memset(nucleus,0,sizeof(nucleus_t));
/* Try to open the file. If the file does not exist, return */
/* Build the file name */
const char* nucleus_file_name = "/nucleus.txt";
char * file_name = (char*)
calloc( strlen(file->parent.file_name) + strlen(nucleus_file_name) + 1,
sizeof(char));
assert (file_name != NULL);
if (file_name == NULL) {
FREE(nucleus);
DEBUG
return NULL;
}
strcpy (file_name, file->parent.file_name);
strcat (file_name, nucleus_file_name);
@ -258,66 +283,219 @@ nucleus_t* trexio_text_read_nucleus(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = (char*) malloc(sz*sizeof(char));
char* buffer = CALLOC(sz,char);
if (buffer == NULL) {
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read the dimensioning variables */
int rc;
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "dim_charge") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dim_charge));
assert (rc == 1);
rc = fscanf(f, "%u", &(nucleus->rank_charge));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_charge = 1;
for (unsigned int i=0; i<nucleus->rank_charge; i++){
unsigned int j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_charge") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu\n", &(nucleus->dims_charge[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_charge *= nucleus->dims_charge[i];
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "dim_coord") == 0);
if ((rc != 1) || (strcmp(buffer, "rank_coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dim_coord));
assert (rc == 1);
rc = fscanf(f, "%u", &(nucleus->rank_coord));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
uint64_t size_coord = 1;
for (unsigned int i=0; i<nucleus->rank_coord; i++){
unsigned int j=-1;
rc = fscanf(f, "%s %u", buffer, &j);
if ((rc != 2) || (strcmp(buffer, "dims_coord") != 0) || (j!=i)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->dims_coord[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
size_coord *= nucleus->dims_coord[i];
}
/* Allocate arrays */
nucleus->charge = (double*) calloc(nucleus->dim_charge, sizeof(double));
assert (nucleus->charge != NULL);
nucleus->charge = (double*) calloc(size_charge, sizeof(double));
assert (!(nucleus->charge == NULL));
if (nucleus->charge == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus);
DEBUG
return NULL;
}
nucleus->coord = (double*) calloc(nucleus->dim_coord, sizeof(double));
assert (nucleus->coord != NULL);
nucleus->coord = (double*) calloc(size_coord, sizeof(double));
assert (!(nucleus->coord == NULL));
if (nucleus->coord == NULL) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
/* Read data */
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "num") == 0);
assert(!((rc != 1) || (strcmp(buffer, "num") != 0)));
if ((rc != 1) || (strcmp(buffer, "num") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%lu", &(nucleus->num));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "charge") == 0);
assert(!((rc != 1) || (strcmp(buffer, "charge") != 0)));
if ((rc != 1) || (strcmp(buffer, "charge") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
for (uint64_t i=0 ; i<nucleus->dim_charge ; i++) {
for (uint64_t i=0 ; i<size_charge ; i++) {
rc = fscanf(f, "%lf", &(nucleus->charge[i]));
assert (rc == 1);
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
assert (strcmp(buffer, "coord") == 0);
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert (rc == 1);
assert(!((rc != 1) || (strcmp(buffer, "coord") != 0)));
if ((rc != 1) || (strcmp(buffer, "coord") != 0)) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
free(buffer);
for (uint64_t i=0 ; i<size_coord ; i++) {
rc = fscanf(f, "%lf", &(nucleus->coord[i]));
assert(!(rc != 1));
if (rc != 1) {
FREE(buffer);
FREE(file_name);
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
}
FREE(buffer);
fclose(f);
f = NULL;
}
if (file->parent.mode == 'w') {
nucleus->file = fopen(file_name,"a");
} else {
nucleus->file = fopen(file_name,"r");
}
free(file_name);
FREE(file_name);
assert (!(nucleus->file == NULL));
if (nucleus->file == NULL) {
FREE(nucleus->charge);
FREE(nucleus);
DEBUG
return NULL;
}
fseek(nucleus->file, 0L, SEEK_SET);
file->nucleus = nucleus;
return nucleus;
}
@ -345,19 +523,32 @@ trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file) {
assert (f != NULL);
rewind(f);
/* Write the dimensioning variables */
fprintf(f, "dim_charge %ld\n", nucleus->dim_charge);
fprintf(f, "dim_coord %ld\n", nucleus->dim_coord );
/* Write the dimensioning variables */
fprintf(f, "rank_charge %d\n", nucleus->rank_charge);
uint64_t size_charge = 1;
for (unsigned int i=0; i<nucleus->rank_charge; i++){
fprintf(f, "dims_charge %d %ld\n", i, nucleus->dims_charge[i]);
size_charge *= nucleus->dims_charge[i];
}
fprintf(f, "rank_coord %d\n", nucleus->rank_coord);
uint64_t size_coord = 1;
for (unsigned int i=0; i<nucleus->rank_coord; i++){
fprintf(f, "dims_coord %d %ld\n", i, nucleus->dims_coord[i]);
size_coord *= nucleus->dims_coord[i];
}
/* Write arrays */
fprintf(f, "num %ld\n", nucleus->num);
fprintf(f, "charge\n");
for (uint64_t i=0 ; i<nucleus->dim_charge ; i++) {
for (uint64_t i=0 ; i<size_charge ; i++) {
fprintf(f, "%lf\n", nucleus->charge[i]);
}
fprintf(f, "coord\n");
for (uint64_t i=0 ; i<nucleus->dim_coord ; i++) {
for (uint64_t i=0 ; i<size_coord ; i++) {
fprintf(f, "%lf\n", nucleus->coord[i]);
}
fflush(f);
@ -394,17 +585,14 @@ trexio_exit_code trexio_text_free_nucleus(trexio_text_t* file) {
}
if (nucleus->coord != NULL) {
free (nucleus->coord);
nucleus->coord = NULL;
FREE (nucleus->coord);
}
if (nucleus->charge != NULL) {
free (nucleus->charge);
nucleus->charge = NULL;
FREE (nucleus->charge);
}
free (nucleus);
file->nucleus = NULL;
FREE (nucleus);
return TREXIO_SUCCESS;
}
#+end_src
@ -452,12 +640,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 +653,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++) {
uint64_t dim_size = 1;
for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_coord[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i];
}
for (uint64_t i=0 ; i<dim_size ; i++) {
coord[i] = nucleus->coord[i];
}
@ -475,7 +669,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;
@ -485,14 +679,20 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->coord != NULL) {
free(nucleus->coord);
nucleus->coord = NULL;
FREE(nucleus->coord);
}
nucleus->dim_coord = dim_coord;
nucleus->coord = (double*) calloc(dim_coord, sizeof(double));
nucleus->rank_coord = rank;
uint64_t dim_size = 1;
for (unsigned int i=0; i<nucleus->rank_coord; i++){
nucleus->dims_coord[i] = dims[i];
dim_size *= dims[i];
}
for (uint64_t i=0 ; i<dim_coord ; i++) {
nucleus->coord = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->coord[i] = coord[i];
}
@ -505,12 +705,12 @@ trexio_exit_code trexio_text_write_nucleus_coord(const trexio_t* file, const dou
The ~charge~ array is assumed allocated with the appropriate size.
#+begin_src c :tangle trexio_text.h
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);
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims);
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint64_t dim_charge) {
trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* charge, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (charge == NULL) return TREXIO_INVALID_ARG_2;
@ -518,9 +718,15 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c
nucleus_t* nucleus = trexio_text_read_nucleus((trexio_text_t*) file);
if (nucleus == NULL) return TREXIO_FAILURE;
if (dim_charge != nucleus->dim_charge) return TREXIO_INVALID_ARG_3;
if (rank != nucleus->rank_charge) return TREXIO_INVALID_ARG_3;
for (uint64_t i=0 ; i<dim_charge ; i++) {
uint64_t dim_size = 1;
for (unsigned int i=0; i<rank; i++){
if (dims[i] != nucleus->dims_charge[i]) return TREXIO_INVALID_ARG_4;
dim_size *= dims[i];
}
for (uint64_t i=0 ; i<dim_size ; i++) {
charge[i] = nucleus->charge[i];
}
@ -528,7 +734,7 @@ trexio_exit_code trexio_text_read_nucleus_charge(const trexio_t* file, double* c
}
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint64_t dim_charge) {
trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const double* charge, const uint32_t rank, const uint64_t* dims) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (charge == NULL) return TREXIO_INVALID_ARG_2;
@ -538,14 +744,20 @@ trexio_exit_code trexio_text_write_nucleus_charge(const trexio_t* file, const do
if (nucleus == NULL) return TREXIO_FAILURE;
if (nucleus->charge != NULL) {
free(nucleus->charge);
nucleus->charge = NULL;
FREE(nucleus->charge);
}
nucleus->dim_charge = dim_charge;
nucleus->charge = (double*) calloc(dim_charge, sizeof(double));
nucleus->rank_charge = rank;
for (uint64_t i=0 ; i<dim_charge ; i++) {
uint64_t dim_size = 1;
for (unsigned int i=0; i<nucleus->rank_charge; i++){
nucleus->dims_charge[i] = dims[i];
dim_size *= dims[i];
}
nucleus->charge = (double*) calloc(dim_size, sizeof(double));
for (uint64_t i=0 ; i<dim_size ; i++) {
nucleus->charge[i] = charge[i];
}
@ -568,7 +780,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
if (file->rdm != NULL) return file->rdm;
/* Allocate the data structure */
rdm_t* rdm = (rdm_t*) malloc(sizeof(rdm_t));
rdm_t* rdm = MALLOC(rdm_t);
assert (rdm != NULL);
rdm->one_e = NULL;
@ -593,7 +805,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
fseek(f, 0L, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0L, SEEK_SET);
char* buffer = (char*) malloc(sz*sizeof(char));
char* buffer = CALLOC(sz,char);
/* Read the dimensioning variables */
int rc;
@ -625,10 +837,10 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
rc = fscanf(f, "%s", buffer);
assert (rc == 1);
rdm->two_e_file_name = (char*) malloc (strlen(buffer)*sizeof(char));
rdm->two_e_file_name = CALLOC (strlen(buffer),char);
strcpy(rdm->two_e_file_name, buffer);
free(buffer);
FREE(buffer);
fclose(f);
f = NULL;
}
@ -637,7 +849,7 @@ rdm_t* trexio_text_read_rdm(trexio_text_t* file) {
} else {
rdm->file = fopen(file_name,"r");
}
free(file_name);
FREE(file_name);
file->rdm = rdm ;
return rdm;
}
@ -709,13 +921,11 @@ trexio_exit_code trexio_text_free_rdm(trexio_text_t* file) {
}
if (rdm->one_e != NULL) {
free (rdm->one_e);
rdm->one_e = NULL;
FREE (rdm->one_e);
}
if (rdm->two_e_file_name != NULL) {
free (rdm->two_e_file_name);
rdm->two_e_file_name = NULL;
FREE (rdm->two_e_file_name);
}
free (rdm);