maxwelllink.matter.md.kernels.kernels_cs2compass module

Compiled cs2compass forces for the CPU and the GPU.

Note: the CPU runs in float64 throughout, while the GPU runs the bonded term in float64 and the non-bonded sums in float32.

class maxwelllink.matter.md.kernels.kernels_cs2compass.CS2COMPASSForceKernels[source]

Bases: object

Compiled cs2compass forces for a batch of identical systems.

The batch driver reads n_atoms, n_molecules, masses and charges_eff, then calls forces_cpu() or forces_gpu() every step. Nothing else about this force field leaks out, so another force field only has to provide the same attributes and the same two methods.

__init__(ff, xp, threads_per_block=THREADS_PER_BLOCK)[source]

Compile the kernels and upload the constants of one CS2 force field.

Parameters:
  • ff (cs2compass.CS2COMPASSForceField) – A constructed force field; its geometry, box and cutoffs are read here.

  • xp (module) – Array module: numpy for the CPU path, cupy for the GPU path.

  • threads_per_block (int, default: 128) – CUDA block size, ignored on the CPU path.

forces_cpu(x, F, efield, terms=None)[source]

Evaluate the force and potential of one system.

Parameters:
  • x (numpy.ndarray of float, shape (na, 3)) – Atomic positions in atomic units (Bohr).

  • F (numpy.ndarray of float, shape (na, 3)) – Output force array, overwritten in place.

  • efield (numpy.ndarray of float, shape (3,)) – Effective electric field in atomic units.

  • terms (numpy.ndarray of float, shape (2,), optional) – Output stretch and bend energy, in the order of reported_term_names.

Returns:

Mechanical potential energy in atomic units (Hartree).

Return type:

float

forces_gpu(x, F, potential, efield, terms=None)[source]

Evaluate the force and potential of every system with two kernel launches.

Parameters:
  • x (cupy.ndarray of float, shape (num, na, 3)) – Atomic positions of every system in atomic units (Bohr).

  • F (cupy.ndarray of float, shape (num, na, 3)) – Output force array, overwritten in place.

  • potential (cupy.ndarray of float, shape (num,)) – Output potential energy of every system.

  • efield (cupy.ndarray of float, shape (num, 3)) – Effective electric field of every system in atomic units.

  • terms (cupy.ndarray of float, shape (num, 2), optional) – Output stretch and bend energy of every system, in the order of reported_term_names.

reported_term_names = ('stretch_au', 'bend_au')

energy terms reported next to the potential, in this column order

maxwelllink.matter.md.kernels.kernels_cs2compass.build_cpu_kernel()[source]

Build (once per process) the njit cs2compass force evaluator.

Returns:

compute(x, F, efield, s_re, s_im, qa, lj_a, lj_b, lj_c, kvec, ak, par, terms) returning the potential energy and filling terms with the stretch and the bend energy; all arrays float64.

Return type:

callable

maxwelllink.matter.md.kernels.kernels_cs2compass.build_cuda_kernels(n_atoms, n_kvec, threads_per_block=THREADS_PER_BLOCK)[source]

Build (once per shape) the CUDA force kernels for a batch of CS2 systems.

One block handles one system with two kernels:

intra

Bonded stretches and bends in float64; writes the force array.

nonbonded

Lennard-Jones, Ewald and the external field in float32; adds to it.

Parameters:
  • n_atoms (int) – Atoms and half-space k-vectors per system; both become compile-time loop bounds and size the per-system work arrays. Those live in static shared memory when they fit its 48 KB (the fastest path) and in global memory otherwise (see CS2COMPASSForceKernels.forces_gpu()), so the system size is bounded by the device memory.

  • n_kvec (int) – Atoms and half-space k-vectors per system; both become compile-time loop bounds and size the per-system work arrays. Those live in static shared memory when they fit its 48 KB (the fastest path) and in global memory otherwise (see CS2COMPASSForceKernels.forces_gpu()), so the system size is bounded by the device memory.

  • threads_per_block (int, default: 128) – CUDA block size, a multiple of the warp size (32).

Returns:

The kernels, keyed "intra" and "nonbonded", and the flag "work_in_shared" telling which non-bonded variant was chosen.

Return type:

dict

Raises:

ImportError – If numba’s CUDA target is unavailable.