Electrons
1. Introduction
The electron module holds how many electrons the system has, split into up-spin and down-spin, and where they are.
In a QMC calculation electrons of opposite spin are distinguishable: only electrons of the same spin are antisymmetric under exchange. The module keeps the two counts separately for that reason, and stores the positions as walkers: one complete set of positions per walker, with the up-spin electrons first and the down-spin electrons after them.
The point part of the context holds the positions of all the walkers.
qmckl_set_electron_coord writes them there and keeps the previous set, so
that a Monte Carlo step can be accepted or rejected. Distances and potentials
are computed on demand and cached with the date at which they were obtained.
1.1. Memory layout
The setters accept either layout and the context stores the transposed one.
With 'N' the coordinates are read as consecutive [ (x,y,z), (x,y,z), ... ]
triplets and transposed on the way in; with 'T' they are copied as they are,
[ (x,x,x,...), (y,y,y,...), (z,z,z,...) ]. The getters transpose only when
asked for 'N'.
2. Context
The electron module keeps the electronic configuration in the context,
together with the quantities computed from it: electron-electron and
electron-nucleus distances and potentials. All the points of all the walkers
live in one qmckl_point_struct, and the computed arrays carry the walker
index first.
2.1. Data stored in the context
The following data is stored in the context:
| Variable | Type | Description |
|---|---|---|
uninitialized |
int32_t |
Keeps bit set for uninitialized data |
num |
int64_t |
Total number of electrons |
up_num |
int64_t |
Number of up-spin electrons |
down_num |
int64_t |
Number of down-spin electrons |
provided |
bool |
If true, electron is valid |
walker |
qmckl_walker |
Current walker state |
walker_old |
qmckl_walker |
Previous walker state |
Computed data:
| Variable | Type | Description |
|---|---|---|
ee_distance |
double[walker.num][num][num] |
Electron-electron distances |
ee_distance_date |
uint64_t |
Last modification date of the electron-electron distances |
en_distance |
double[walker.num][num][nucl_num] |
Electron-nucleus distances |
en_distance_date |
uint64_t |
Last modification date of the electron-nucleus distances |
ee_potential |
double[walker.num] |
Electron-electron potential energy |
ee_potential_date |
uint64_t |
Last modification date of the electron-electron potential |
en_potential |
double[walker.num] |
Electron-nucleus potential energy |
en_potential_date |
uint64_t |
Last modification date of the electron-nucleus potential |
2.2. Data structures
The electron data is organized in two structures. qmckl_walker pairs the
number of walkers with a qmckl_point_struct holding the elec_num *
walk_num points of all of them.
The qmckl_electron_struct gathers the two spin counts, the current and
previous walkers (the latter for accepting or rejecting a step), the cached
distances and potentials with the date at which each was computed, and the
initialization mask.
The uninitialized integer contains one bit set to one for each
initialization function which has not been called. It becomes equal
to zero after all initialization functions have been called. The
struct is then initialized and provided == true.
Some values are initialized by default, and are not concerned by
this mechanism.
bool qmckl_electron_provided (const qmckl_context context);
2.3. Initialization functions
Two functions set the electron data: qmckl_set_electron_num for the
counts and qmckl_set_electron_coord for the positions.
qmckl_context_create zeroes the context, so no coordinate array exists and
walker.num is 0 until the first call.
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_coord (qmckl_context context, const char transp, const int64_t walk_num, const double* coord, const int64_t size_max);
Sets the number of up-spin and down-spin electrons and stores their sum in
num. The number of walkers is not set here: it comes with
qmckl_set_electron_coord. A second call returns QMCKL_ALREADY_SET;
QMCKL_INVALID_ARG_2 is returned when up_num is not positive and
QMCKL_INVALID_ARG_3 when down_num is negative.
Sets the coordinates of all the walkers. coord holds walk_num * elec_num
points and size_max must be at least 3 * walk_num * elec_num; transp
selects the layout, as for the getter.
The function swaps walker and walker_old first, so the previous
coordinates survive in walker_old. It then writes the new ones into
ctx->point and copies the result back into walker.point. On the first
call walker_old.num is 0, so the function calls itself once more to fill
it.
Exit codes: QMCKL_INVALID_CONTEXT, QMCKL_INVALID_ARG_2 when transp is
neither 'N' nor 'T', QMCKL_INVALID_ARG_3 when walk_num is not
positive, QMCKL_INVALID_ARG_4 when coord is NULL or size_max is too
small, and QMCKL_FAILURE when the number of electrons has not been set.
Changing the electron coordinates increments the date of the context, which invalidates the cached distances and potentials.
2.4. Access functions
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.
2.4.1. Number of electrons
The three getters return the counts stored by qmckl_set_electron_num, and
QMCKL_NOT_PROVIDED until it has been called.
qmckl_exit_code qmckl_get_electron_num (const qmckl_context context, int64_t* const num);
qmckl_exit_code qmckl_get_electron_up_num (const qmckl_context context, int64_t* const up_num);
qmckl_exit_code qmckl_get_electron_down_num (const qmckl_context context, int64_t* const down_num);
2.4.2. Number of walkers
A walker is a set of electron coordinates that are arguments of
the wave function. walk_num is the number of walkers.
Before any coordinates are set walker.num is 0 and the getter returns it
with QMCKL_SUCCESS: it does not check that the electron data is provided.
qmckl_exit_code qmckl_get_electron_walk_num (const qmckl_context context, int64_t* const walk_num);
2.4.3. Electron coordinates
Returns the coordinates of all the walkers. coord must point to at least
3 * elec_num * walker.num doubles, and transp selects their layout:
| Normal | Transposed | |
|---|---|---|
| C | [walker.num*elec_num][3] |
[3][walker.num*elec_num] |
| Fortran | (3,walker.num*elec_num) |
(walker.num*elec_num, 3) |
qmckl_exit_code
qmckl_get_electron_coord (const qmckl_context context,
const char transp,
double* const coord,
const int64_t size_max);
ctx->electron.walker.point is a copy of ctx->point made by the setter.
The getter checks that the two agree on the number of points and on the data
pointer, then forwards to qmckl_get_point.
Exit codes: QMCKL_INVALID_CONTEXT, QMCKL_INVALID_ARG_2 when transp is
neither 'N' nor 'T', QMCKL_INVALID_ARG_3 when coord is NULL or
size_max is smaller than 3 * elec_num * walker.num,
QMCKL_INVALID_ARG_4 when size_max is not positive, and
QMCKL_NOT_PROVIDED when the electron data has not been provided.
3. Computation
The computed data is stored in the context so that it can be reused by
different kernels. Each array carries the context date at which it was
computed. The point and nucleus setters call qmckl_context_touch, which
copies the new context date into ctx->point.date, and a provider recomputes
its array when ctx->point.date is more recent than the stored date.
3.1. Electron-electron distances
qmckl_get_electron_ee_distance copies the distances to distance, which
must hold at least elec_num * elec_num * walker.num doubles. Entry
[k][i][j] is the distance between electrons i and j of walker k.
3.1.1. Get
Exit codes: QMCKL_INVALID_CONTEXT, QMCKL_INVALID_ARG_2 when distance
is NULL, and QMCKL_INVALID_ARG_3 when size_max is too small.
qmckl_exit_code
qmckl_get_electron_ee_distance(qmckl_context context,
double* const distance,
const int64_t size_max);
3.1.2. Compute
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
elec_num |
int64_t |
in | Number of electrons |
walk_num |
int64_t |
in | Number of walkers |
coord |
double[3][walk_num][elec_num] |
in | Electron coordinates |
ee_distance |
double[walk_num][elec_num][elec_num] |
out | Electron-electron distances |
coord is the coordinate buffer of the context. It is declared
(elec_num,3,walk_num) but only the base address of each walker,
coord(1,k,1), is used, with elec_num*walk_num as the stride between the
x, y and z components; the buffer is laid out as the
[3][walk_num][elec_num] of the table.
function qmckl_compute_ee_distance(context, elec_num, walk_num, coord, ee_distance) &
result(info) bind(C)
use qmckl_constants
use qmckl, only : qmckl_distance
implicit none
integer (qmckl_context) , intent(in) , value :: context
integer (c_int64_t) , intent(in) , value :: elec_num
integer (c_int64_t) , intent(in) , value :: walk_num
real (c_double ) , intent(in) :: coord(elec_num,3,walk_num)
real (c_double ) , intent(out) :: ee_distance(elec_num,elec_num,walk_num)
integer (qmckl_exit_code) :: info
integer*8 :: k, i, j
double precision :: x, y, z
info = QMCKL_SUCCESS
if (context == QMCKL_NULL_CONTEXT) then
info = QMCKL_INVALID_CONTEXT
return
endif
if (elec_num <= 0) then
info = QMCKL_INVALID_ARG_2
return
endif
if (walk_num <= 0) then
info = QMCKL_INVALID_ARG_3
return
endif
do k=1,walk_num
info = qmckl_distance(context, 'T', 'T', elec_num, elec_num, &
coord(1,k,1), elec_num * walk_num, &
coord(1,k,1), elec_num * walk_num, &
ee_distance(1,1,k), elec_num)
if (info /= QMCKL_SUCCESS) then
exit
endif
end do
end function qmckl_compute_ee_distance
3.2. Electron-electron potential
ee_potential is given by
where is the ee potential and the ee
distance. Pairs closer than 1e-5 are skipped, so coalesced electrons do
not produce an infinity.
The destination array must hold walker.num doubles; size_max is
its length in elements.
3.2.1. Get
qmckl_exit_code
qmckl_get_electron_ee_potential(qmckl_context context,
double* const ee_potential,
const int64_t size_max);
3.2.2. Compute
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
elec_num |
int64_t |
in | Number of electrons |
walk_num |
int64_t |
in | Number of walkers |
ee_distance |
double[walk_num][elec_num][elec_num] |
in | Electron-electron distances |
ee_potential |
double[walk_num] |
out | Electron-electron potential |
function qmckl_compute_ee_potential(context, elec_num, walk_num, &
ee_distance, ee_potential) result(info) bind(C)
use qmckl_constants
implicit none
integer(qmckl_context), intent(in), value :: context
integer (c_int64_t) , intent(in) , value :: elec_num
integer (c_int64_t) , intent(in) , value :: walk_num
real (c_double ) , intent(in) :: ee_distance(elec_num,elec_num,walk_num)
real (c_double ) , intent(out) :: ee_potential(walk_num)
integer (qmckl_exit_code) :: info
integer*8 :: nw, i, j
info = QMCKL_SUCCESS
if (context == QMCKL_NULL_CONTEXT) then
info = QMCKL_INVALID_CONTEXT
return
endif
if (elec_num <= 0) then
info = QMCKL_INVALID_ARG_2
return
endif
if (walk_num <= 0) then
info = QMCKL_INVALID_ARG_3
return
endif
ee_potential = 0.0d0
do nw=1,walk_num
do j=2,elec_num
do i=1,j-1
if (dabs(ee_distance(i,j,nw)) > 1e-5) then
ee_potential(nw) = ee_potential(nw) + 1.0d0/(ee_distance(i,j,nw))
endif
end do
end do
end do
end function qmckl_compute_ee_potential
3.3. Electron-nucleus distances
qmckl_get_electron_en_distance copies the distances to distance, which
must hold at least elec_num * walk_num * nucl_num doubles. Entry
[k][i][A] is the distance between electron i of walker k and nucleus
A. The nuclear data must have been provided.
3.3.1. Get
Exit codes: QMCKL_INVALID_CONTEXT, QMCKL_INVALID_ARG_2 when distance
is NULL, QMCKL_INVALID_ARG_3 when size_max is too small, and
QMCKL_NOT_PROVIDED when the nuclear data is missing.
qmckl_exit_code
qmckl_get_electron_en_distance(qmckl_context context,
double* const distance,
const int64_t size_max);
3.3.2. Compute
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
point_num |
int64_t |
in | Number of points |
nucl_num |
int64_t |
in | Number of nuclei |
elec_coord |
double[3][point_num] |
in | Electron coordinates |
nucl_coord |
double[3][nucl_num] |
in | Nuclear coordinates |
en_distance |
double[point_num][nucl_num] |
out | Electron-nucleus distances |
function qmckl_compute_en_distance(context, &
point_num, nucl_num, elec_coord, nucl_coord, en_distance) &
result(info) bind(C)
use qmckl_constants
use qmckl, only : qmckl_distance
implicit none
integer (qmckl_context), intent(in), value :: context
integer (c_int64_t) , intent(in), value :: point_num
integer (c_int64_t) , intent(in), value :: nucl_num
real (c_double ) , intent(in) :: elec_coord(point_num,3)
real (c_double ) , intent(in) :: nucl_coord(nucl_num,3)
real (c_double ) , intent(out) :: en_distance(nucl_num,point_num)
integer(qmckl_exit_code) :: info
info = QMCKL_SUCCESS
if (context == QMCKL_NULL_CONTEXT) then
info = QMCKL_INVALID_CONTEXT
return
endif
if (point_num <= 0) then
info = QMCKL_INVALID_ARG_2
return
endif
if (nucl_num <= 0) then
info = QMCKL_INVALID_ARG_3
return
endif
info = qmckl_distance(context, 'T', 'T', nucl_num, point_num, &
nucl_coord, nucl_num, &
elec_coord, point_num, &
en_distance, nucl_num)
end function qmckl_compute_en_distance
3.4. Electron-nucleus potential
en_potential computes the en potential energy
where is the en potential, the en
distance and is the nuclear charge. Pairs closer than 1.d-6 are
skipped, as for the electron-electron potential.
The destination array must hold walker.num doubles; size_max is
its length in elements.
3.4.1. Get
qmckl_exit_code
qmckl_get_electron_en_potential(qmckl_context context,
double* const en_potential,
const int64_t size_max);
3.4.2. Compute
| Variable | Type | In/Out | Description |
|---|---|---|---|
context |
qmckl_context |
in | Global state |
elec_num |
int64_t |
in | Number of electrons |
nucl_num |
int64_t |
in | Number of nuclei |
walk_num |
int64_t |
in | Number of walkers |
charge |
double[nucl_num] |
in | charge of nucleus |
en_distance |
double[walk_num][elec_num][nucl_num] |
in | Electron-nucleus distances |
en_potential |
double[walk_num] |
out | Electron-nucleus potential |
function qmckl_compute_en_potential(context, elec_num, nucl_num, walk_num, &
charge, en_distance, en_potential) &
result(info) bind(C)
use qmckl
implicit none
integer (qmckl_context), intent(in), value :: context
integer (c_int64_t) , intent(in) , value :: elec_num
integer (c_int64_t) , intent(in) , value :: nucl_num
integer (c_int64_t) , intent(in) , value :: walk_num
real (c_double ) , intent(in) :: charge(nucl_num)
real (c_double ) , intent(in) :: en_distance(nucl_num,elec_num,walk_num)
real (c_double ) , intent(out) :: en_potential(walk_num)
integer(qmckl_exit_code) :: info
integer*8 :: nw, i, j
info = QMCKL_SUCCESS
if (context == QMCKL_NULL_CONTEXT) then
info = QMCKL_INVALID_CONTEXT
return
endif
if (elec_num <= 0) then
info = QMCKL_INVALID_ARG_2
return
endif
if (walk_num <= 0) then
info = QMCKL_INVALID_ARG_3
return
endif
en_potential = 0.0d0
do nw=1,walk_num
do i=1,elec_num
do j=1,nucl_num
if (dabs(en_distance(j,i,nw)) > 1.d-6) then
en_potential(nw) = en_potential(nw) - charge(j)/(en_distance(j,i,nw))
endif
end do
end do
end do
end function qmckl_compute_en_potential