From 0fea3786984179ad765875f5e82e074494d5513a Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Sat, 15 May 2021 23:19:13 +0200 Subject: [PATCH] Improve electron module --- org/Makefile | 3 + org/qmckl_electron.org | 150 +++++++++++++++++++++++++++++------------ org/qmckl_error.org | 9 ++- 3 files changed, 117 insertions(+), 45 deletions(-) create mode 100644 org/Makefile diff --git a/org/Makefile b/org/Makefile new file mode 100644 index 0000000..4278054 --- /dev/null +++ b/org/Makefile @@ -0,0 +1,3 @@ +clean check all: + make -C ../ $@ + diff --git a/org/qmckl_electron.org b/org/qmckl_electron.org index 3fac9aa..4a006bd 100644 --- a/org/qmckl_electron.org +++ b/org/qmckl_electron.org @@ -96,20 +96,20 @@ typedef struct qmckl_electron_struct { ** Access functions - #+begin_src c :comments org :tangle (eval h_private_func) :exports none -int64_t qmckl_get_electron_num (const qmckl_context context); -int64_t qmckl_get_electron_up_num (const qmckl_context context); -int64_t qmckl_get_electron_down_num (const qmckl_context context); -int64_t qmckl_get_electron_walk_num (const qmckl_context context); -double* qmckl_get_electron_coord_new (const qmckl_context context); -double* qmckl_get_electron_coord_old (const qmckl_context context); - #+end_src - - When all the data relative to electrons have been set, the - following function returns ~true~. - - #+begin_src c :comments org :tangle (eval h_func) -bool qmckl_electron_provided (const qmckl_context context); + Access functions return ~QMCKL_SUCCESS~ when the data has been + successfully retrieved. It returnes ~QMCKL_INVALID_CONTEXT~ when + the context is not a valid context, and ~QMCKL_NOT_PROVIDED~ when + the data has not been provided. If the function returns + successfully, the variable pointed by the pointer given in argument + contains the requested data. Otherwise, this variable is untouched. + + #+begin_src c :comments org :tangle (eval h_func) :exports none +qmckl_exit_code qmckl_get_electron_num (const qmckl_context context, int64_t* num); +qmckl_exit_code qmckl_get_electron_up_num (const qmckl_context context, int64_t* up_num); +qmckl_exit_code qmckl_get_electron_down_num (const qmckl_context context, int64_t* down_num); +qmckl_exit_code qmckl_get_electron_walk_num (const qmckl_context context, int64_t* walk_num); +qmckl_exit_code qmckl_get_electron_coord_new (const qmckl_context context, double* coord); +qmckl_exit_code qmckl_get_electron_coord_old (const qmckl_context context, double* coord); #+end_src #+NAME:post @@ -120,10 +120,11 @@ if ( (ctx->electron.uninitialized & mask) != 0) { #+end_src #+begin_src c :comments org :tangle (eval c) :noweb yes :exports none -int64_t qmckl_get_electron_num (const qmckl_context context) { +qmckl_exit_code +qmckl_get_electron_num (const qmckl_context context, int64_t* num) { if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) { - return (char) 0; + return QMCKL_INVALID_CONTEXT; } qmckl_context_struct* const ctx = (qmckl_context_struct* const) context; @@ -132,72 +133,88 @@ int64_t qmckl_get_electron_num (const qmckl_context context) { int32_t mask = 1; if ( (ctx->electron.uninitialized & mask) != 0) { - return (int64_t) 0; + return QMCKL_NOT_PROVIDED; } assert (ctx->electron.num > (int64_t) 0); - return ctx->electron.num; + ,*num = ctx->electron.num; + return QMCKL_SUCCESS; } -int64_t qmckl_get_electron_up_num (const qmckl_context context) { +qmckl_exit_code +qmckl_get_electron_up_num (const qmckl_context context, int64_t* up_num) { if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) { - return (int64_t) 0; + return QMCKL_INVALID_CONTEXT; } qmckl_context_struct* const ctx = (qmckl_context_struct* const) context; assert (ctx != NULL); - int32_t mask = 1 << 1; + int32_t mask = 1; if ( (ctx->electron.uninitialized & mask) != 0) { - return (int64_t) 0; + return QMCKL_NOT_PROVIDED; } assert (ctx->electron.up_num > (int64_t) 0); - return ctx->electron.up_num; + ,*up_num = ctx->electron.up_num; + return QMCKL_SUCCESS; } -int64_t qmckl_get_electron_down_num (const qmckl_context context) { +qmckl_exit_code +qmckl_get_electron_down_num (const qmckl_context context, int64_t* down_num) { if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) { - return (int64_t) 0; + return QMCKL_INVALID_CONTEXT; } qmckl_context_struct* const ctx = (qmckl_context_struct* const) context; assert (ctx != NULL); - int32_t mask = 1 << 2; + int32_t mask = 1; if ( (ctx->electron.uninitialized & mask) != 0) { - return (int64_t) 0; + return QMCKL_NOT_PROVIDED; } assert (ctx->electron.down_num >= (int64_t) 0); - return ctx->electron.down_num; + ,*down_num = ctx->electron.down_num; + return QMCKL_SUCCESS; } -int64_t qmckl_get_electron_walk_num (const qmckl_context context) { +qmckl_exit_code +qmckl_get_electron_walk_num (const qmckl_context context, int64_t* walk_num) { if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) { - return (int64_t) 0; + return QMCKL_INVALID_CONTEXT; } qmckl_context_struct* const ctx = (qmckl_context_struct* const) context; assert (ctx != NULL); - int32_t mask = 1 << 3; + int32_t mask = 2; if ( (ctx->electron.uninitialized & mask) != 0) { - return (int64_t) 0; + return QMCKL_NOT_PROVIDED; } assert (ctx->electron.walk_num > (int64_t) 0); - return ctx->electron.walk_num; + ,*walk_num = ctx->electron.walk_num; + return QMCKL_SUCCESS; } + #+end_src + + When all the data relative to electrons have been set, the + following function returns ~true~. + + #+begin_src c :comments org :tangle (eval h_func) +bool qmckl_electron_provided (const qmckl_context context); + #+end_src + #+begin_src c :comments org :tangle (eval c) :noweb yes :exports none bool qmckl_electron_provided(const qmckl_context context) { if (qmckl_context_check(context) == QMCKL_NULL_CONTEXT) { @@ -215,7 +232,8 @@ bool qmckl_electron_provided(const qmckl_context context) { To set the data relative to the electrons in the context, the following functions need to be called. When the data structure is - initialized, the ~coord_new~ and ~coord_old~ arrays are both allocated. + initialized, the internal ~coord_new~ and ~coord_old~ arrays are + both allocated. #+begin_src c :comments org :tangle (eval h_func) qmckl_exit_code qmckl_set_electron_num (qmckl_context context, const int64_t up_num, const int64_t down_num); @@ -277,9 +295,10 @@ return QMCKL_SUCCESS; down-spin electrons to the context and we set the number of walkers. #+begin_src c :comments org :tangle (eval c) :noweb yes :exports none -qmckl_exit_code qmckl_set_electron_num(qmckl_context context, - const int64_t up_num, - const int64_t down_num) { +qmckl_exit_code +qmckl_set_electron_num(qmckl_context context, + const int64_t up_num, + const int64_t down_num) { <> if (up_num <= 0) { @@ -308,7 +327,9 @@ qmckl_exit_code qmckl_set_electron_num(qmckl_context context, #+begin_src c :comments org :tangle (eval c) :noweb yes :exports none -qmckl_exit_code qmckl_set_electron_walk_num(qmckl_context context, const int64_t walk_num) { +qmckl_exit_code +qmckl_set_electron_walk_num(qmckl_context context, const int64_t walk_num) { + <> if (walk_num <= 0) { @@ -333,10 +354,16 @@ qmckl_exit_code qmckl_set_electron_walk_num(qmckl_context context, const int64_t electrons have been set. #+begin_src c :comments org :tangle (eval c) :noweb yes :exports none -qmckl_exit_code qmckl_set_electron_coord(qmckl_context context, const double* coord) { +qmckl_exit_code +qmckl_set_electron_coord(qmckl_context context, const double* coord) { + <> - const int64_t num = qmckl_get_electron_num(context); + int64_t num; + qmckl_exit_code rc; + rc = qmckl_get_electron_num(context, &num); + if (rc != QMCKL_SUCCESS) return rc; + if (num == 0L) { return qmckl_failwith( context, QMCKL_FAILURE, @@ -344,7 +371,10 @@ qmckl_exit_code qmckl_set_electron_coord(qmckl_context context, const double* c "num is not set"); } - const int64_t walk_num = qmckl_get_electron_walk_num(context); + int64_t walk_num; + rc = qmckl_get_electron_walk_num(context, &walk_num); + if (rc != QMCKL_SUCCESS) return rc; + if (walk_num == 0L) { return qmckl_failwith( context, QMCKL_FAILURE, @@ -401,21 +431,57 @@ qmckl_exit_code rc; assert(!qmckl_electron_provided(context)); +int64_t n; +rc = qmckl_get_electron_num (context, &n); +assert(rc == QMCKL_NOT_PROVIDED); + +rc = qmckl_get_electron_up_num (context, &n); +assert(rc == QMCKL_NOT_PROVIDED); + +rc = qmckl_get_electron_down_num (context, &n); +assert(rc == QMCKL_NOT_PROVIDED); + + rc = qmckl_set_electron_num (context, up_num, down_num); assert(rc == QMCKL_SUCCESS); assert(!qmckl_electron_provided(context)); +rc = qmckl_get_electron_up_num (context, &n); +assert(rc == QMCKL_SUCCESS); +assert(n == up_num); + +rc = qmckl_get_electron_down_num (context, &n); +assert(rc == QMCKL_SUCCESS); +assert(n == down_num); + +rc = qmckl_get_electron_num (context, &n); +assert(rc == QMCKL_SUCCESS); +assert(n == num); + + +int64_t w; +rc = qmckl_get_electron_walk_num (context, &w); +assert(rc == QMCKL_NOT_PROVIDED); + + rc = qmckl_set_electron_walk_num (context, walk_num); assert(rc == QMCKL_SUCCESS); + +rc = qmckl_get_electron_walk_num (context, &w); +assert(rc == QMCKL_SUCCESS); +assert(w == walk_num); + assert(qmckl_electron_provided(context)); rc = qmckl_set_electron_coord (context, coord); assert(rc == QMCKL_SUCCESS); +double coord2[walk_num*3*num]; + #+end_src * Computation - + The computed data is stored in the context so that it can be reused by different kernels. To ensure that the data is valid, for each computed data the date of the context is stored when it is computed. diff --git a/org/qmckl_error.org b/org/qmckl_error.org index 2a80783..49b6616 100644 --- a/org/qmckl_error.org +++ b/org/qmckl_error.org @@ -87,7 +87,8 @@ typedef int32_t qmckl_exit_code; | ~QMCKL_INVALID_CONTEXT~ | 103 | 'Invalid context' | | ~QMCKL_ALLOCATION_FAILED~ | 104 | 'Allocation failed' | | ~QMCKL_DEALLOCATION_FAILED~ | 105 | 'De-allocation failed' | - | ~QMCKL_INVALID_EXIT_CODE~ | 106 | 'Invalid exit code' | + | ~QMCKL_NOT_PROVIDED~ | 106 | 'Not provided' | + | ~QMCKL_INVALID_EXIT_CODE~ | 107 | 'Invalid exit code' | # We need to force Emacs not to indent the Python code: # -*- org-src-preserve-indentation: t @@ -134,7 +135,8 @@ return '\n'.join(result) #define QMCKL_INVALID_CONTEXT ((qmckl_exit_code) 103) #define QMCKL_ALLOCATION_FAILED ((qmckl_exit_code) 104) #define QMCKL_DEALLOCATION_FAILED ((qmckl_exit_code) 105) - #define QMCKL_INVALID_EXIT_CODE ((qmckl_exit_code) 106) + #define QMCKL_NOT_PROVIDED ((qmckl_exit_code) 106) + #define QMCKL_INVALID_EXIT_CODE ((qmckl_exit_code) 107) #+end_src #+begin_src f90 :comments org :tangle (eval fh_type) :exports none @@ -154,7 +156,8 @@ return '\n'.join(result) integer(qmckl_exit_code), parameter :: QMCKL_INVALID_CONTEXT = 103 integer(qmckl_exit_code), parameter :: QMCKL_ALLOCATION_FAILED = 104 integer(qmckl_exit_code), parameter :: QMCKL_DEALLOCATION_FAILED = 105 - integer(qmckl_exit_code), parameter :: QMCKL_INVALID_EXIT_CODE = 106 + integer(qmckl_exit_code), parameter :: QMCKL_NOT_PROVIDED = 106 + integer(qmckl_exit_code), parameter :: QMCKL_INVALID_EXIT_CODE = 107 #+end_src :end: