Nucleus

1. Introduction

The nucleus module holds the geometry of the molecule: how many nuclei there are, their charges and their coordinates. From these it computes the inter-nuclear distance matrix and the nuclear repulsion energy

Enn=∑i<jZiZjrij,

and keeps both in the context. The other modules read the geometry from the same fields: the atomic basis, the electron-nucleus distances and the force kernels take the coordinates, and the electron-nucleus potential and the molecular-orbital cusp take the charges.

The module does not cache its two derived quantities. Both getters recompute from the current inputs on every call and stamp the current context date; what a coordinate change propagates to the other modules is that context date, which qmckl_set_nucleus_coord increments.

2. Context

The nucleus module stores the input data (nuclear positions and charges) and the two quantities derived from it (inter-nuclear distances and repulsion energy) in the context. The input data is what the other modules read; the derived quantities are only read back here.

The input data:

uninitialized int32_t Bits of the input data not set yet
num int64_t Total number of nuclei
provided bool True once all three inputs are set
charge qmckl_vector Nuclear charges
coord qmckl_matrix Nuclear coordinates, one row per nucleus

Computed data:

nn_distance qmckl_matrix Nucleus-nucleus distances
nn_distance_date int64_t Date of the last computation of the distances
repulsion double Nuclear repulsion energy
repulsion_date int64_t Date of the last computation of the repulsion

2.1. Data structure

The nucleus data structure consolidates all nuclear information in a single C structure. The two computed quantities carry the value of the context date at the time they were last computed, but no function reads those two fields back: the providers recompute unconditionally. coord_date is declared beside them and is never written; the date of the coordinates is the context date, which qmckl_context_touch moves.

The charge, coord and nndistance fields own their memory, allocated with qmckl_malloc, and are released with the context.

2.2. Initialization tracking

The uninitialized integer serves as a bit field where each bit represents whether a specific initialization function has been called. It is set to 7 when the context is created, by qmckl_init_nucleus, and each setter clears its bit: bit 0 by qmckl_set_nucleus_num, bit 1 by qmckl_set_nucleus_charge and bit 2 by qmckl_set_nucleus_coord. When all three have been called, uninitialized is zero and provided is set to true.

This mechanism ensures that the nucleus data is complete before it is used in calculations. The default-values block of qmckl_init_nucleus is empty, so every input is tracked by a bit.

2.3. Access functions

Access functions return QMCKL_SUCCESS when the data has been retrieved and QMCKL_INVALID_CONTEXT when the context is not a valid one. Reading an input that has not been set returns QMCKL_NOT_PROVIDED.

Returns the number of nuclei stored in the context. When the input has not been set, the function writes 0 into num before returning QMCKL_NOT_PROVIDED.

Copies the nuclear charges into charge, which must hold at least num doubles; a smaller size_max returns QMCKL_INVALID_ARG_3. A null charge returns QMCKL_INVALID_ARG_2.

Copies the coordinates into coord, which must hold 3 * num doubles; a smaller size_max fails with QMCKL_INVALID_ARG_4 and a null coord with QMCKL_INVALID_ARG_3. With transp == 'N' the array receives one coordinate triplet per nucleus, in the order the nuclei were given; with transp == 'T' it receives all the x, then all the y, then all the z. Any other value of transp returns QMCKL_INVALID_ARG_2.

Returns true once the three inputs have been set, and false otherwise. Unlike the getters it does not record an error, and it returns false for an invalid context.

C
bool qmckl_nucleus_provided (const qmckl_context context);

2.4. Initialization functions

The number of nuclei must be set first: the charge and coordinate setters read it, and return QMCKL_NOT_PROVIDED without it. The number and charge setters accept a single call each and return QMCKL_ALREADY_SET on a second one; the coordinate setter can be called again whenever the geometry changes.

C
qmckl_exit_code
qmckl_set_nucleus_num(qmckl_context context,
                      const int64_t num);

Sets the number of nuclei. A non-positive value returns QMCKL_INVALID_ARG_2.

C
qmckl_exit_code
qmckl_set_nucleus_charge(qmckl_context context,
                         const double* charge,
                         const int64_t size_max);

Sets the charges of the num nuclei. A null charge returns QMCKL_INVALID_ARG_2; a buffer smaller than num returns QMCKL_INVALID_ARG_3.

C
qmckl_exit_code
qmckl_set_nucleus_coord(qmckl_context context,
                        const char transp,
                        const double* coord,
                        const int64_t size_max);

Sets the nuclear coordinates of all the atoms. The coordinates are given in atomic units. This function can be called several times, for example during a geometry optimization: each call bumps the context date, which is what the modules that cache position-dependent results compare against.

With transp == 'N' the array holds one (x,y,z) triplet per atom in row-major order. With transp == 'T' it holds the same data in column-major (Fortran) order: all the x coordinates, then all the y, then all the z.

The new matrix is built before the stored one is released, so a call rejected for a null pointer, a buffer that is too small or an unknown transp leaves the previous coordinates in place.

3. Computation

The two computed quantities live in the context so that a caller can read them without rebuilding them, but this module does not track their validity: each getter below calls its provider, which recomputes from the current coordinates and charges and stamps the current context date. Reading the same quantity twice computes it twice.

Both providers first require provided, and return QMCKL_NOT_PROVIDED when the three inputs are not all set.

3.1. Nucleus-nucleus distances

3.1.1. Get

qmckl_get_nucleus_nn_distance copies the num × num matrix of inter-nuclear distances into distance, which must hold at least num * num doubles; a smaller size_max returns QMCKL_INVALID_ARG_3. The matrix is symmetric with a zero diagonal, and the buffer receives it in column-major order.

C
qmckl_exit_code
qmckl_get_nucleus_nn_distance(qmckl_context context,
                              double* distance,
                              const int64_t size_max);

3.1.2. Compute

Builds the symmetric matrix from the coordinates stored in the context, each entry being the Euclidean distance between two nuclei.

qmckl_context context in Global state
int64_t nucl_num in Number of nuclei
double coord[3][nucl_num] in Nuclear coordinates (au)
double nn_distance[nucl_num][nucl_num] out Nucleus-nucleus distances (au)
Fortran
function qmckl_compute_nn_distance(context, nucl_num, coord, nn_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 :: nucl_num
  real    (c_double ) , intent(in)          :: coord(nucl_num,3)
  real    (c_double ) , intent(out)         :: nn_distance(nucl_num,nucl_num)

  integer(qmckl_exit_code) :: info

  integer*8 :: k

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (nucl_num <= 0) then
     info = QMCKL_INVALID_ARG_2
     return
  endif

  info = qmckl_distance(context, 'T', 'T', nucl_num, nucl_num, &
          coord, nucl_num, &
          coord, nucl_num, &
          nn_distance, nucl_num)

end function qmckl_compute_nn_distance

3.2. Nuclear repulsion energy

Enn=∑i<jZiZjrij

Pairs closer than 1e-5 bohr are skipped instead of producing an infinite contribution.

3.2.1. Get

Writes the repulsion energy into energy, which is not checked for NULL. The energy is recomputed through the provider on every call.

C
qmckl_exit_code qmckl_get_nucleus_repulsion(qmckl_context context, double* const energy);

3.2.2. Compute

Sums ZiZj/rij over the pairs above the diagonal of the distance matrix, skipping the pairs closer than 1e-5 bohr.

qmckl_context context in Global state
int64_t nucl_num in Number of nuclei
double charge[nucl_num] in Nuclear charges (au)
double nn_distance[nucl_num][nucl_num] in Nucleus-nucleus distances (au)
double energy out Nuclear repulsion energy
Fortran
function qmckl_compute_nucleus_repulsion(context, nucl_num, charge, nn_distance, energy) &
     result(info) bind(C)
  use qmckl_constants
  implicit none

  integer(qmckl_context), intent(in), value  :: context
  integer (c_int64_t) , intent(in)  , value :: nucl_num
  real    (c_double ) , intent(in)          :: charge(nucl_num)
  real    (c_double ) , intent(in)          :: nn_distance(nucl_num,nucl_num)
  real    (c_double ) , intent(out)         :: energy

  integer(qmckl_exit_code) :: info

  integer*8 :: i, j

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (nucl_num <= 0) then
     info = QMCKL_INVALID_ARG_2
     return
  endif

  energy = 0.d0
  do j=2, nucl_num
     do i=1, j-1
        if (dabs(nn_distance(i,j)) > 1e-5) then
          energy = energy + charge(i) * charge(j) / nn_distance(i,j)
        endif
     end do
  end do

end function qmckl_compute_nucleus_repulsion