Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Fchl module #34

Merged
merged 6 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 32 additions & 151 deletions qml/fchl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import numpy as np
import copy

from .ffchl_kernels import fget_kernels_fchl
from .ffchl_kernels import fget_symmetric_kernels_fchl
from .ffchl_kernels import fget_global_kernels_fchl
from .ffchl_kernels import fget_global_symmetric_kernels_fchl
from .ffchl_kernels import fget_atomic_kernels_fchl
from .ffchl_kernels import fget_atomic_symmetric_kernels_fchl
from .ffchl_kernels import fget_atomic_force_alphas_fchl
from .ffchl_kernels import fget_atomic_force_kernels_fchl
from .ffchl_module import fget_kernels_fchl
from .ffchl_module import fget_symmetric_kernels_fchl
from .ffchl_module import fget_global_kernels_fchl
from .ffchl_module import fget_global_symmetric_kernels_fchl
from .ffchl_module import fget_atomic_kernels_fchl
from .ffchl_module import fget_atomic_symmetric_kernels_fchl

from .alchemy import get_alchemy

Expand Down Expand Up @@ -102,7 +100,7 @@ def generate_fchl_representation(coordinates, nuclear_charges,


def get_local_kernels_fchl(A, B, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table", two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
Expand All @@ -123,6 +121,8 @@ def get_local_kernels_fchl(A, B, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -181,10 +181,10 @@ def get_local_kernels_fchl(A, B, sigmas, \
assert len(sigmas.shape) == 1, "Third argument (sigmas) is not a 1D list/numpy.array!"

return fget_kernels_fchl(A, B, N1, N2, neighbors1, neighbors2, sigmas, \
nm1, nm2, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
nm1, nm2, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)

def get_local_symmetric_kernels_fchl(A, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table", two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
Expand All @@ -203,6 +203,8 @@ def get_local_symmetric_kernels_fchl(A, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -246,10 +248,10 @@ def get_local_symmetric_kernels_fchl(A, sigmas, \
assert len(sigmas.shape) == 1, "Second argument (sigmas) is not a 1D list/numpy.array!"

return fget_symmetric_kernels_fchl(A, N1, neighbors1, sigmas, \
nm1, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
nm1, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)

def get_global_symmetric_kernels_fchl(A, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table", two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
Expand All @@ -268,6 +270,8 @@ def get_global_symmetric_kernels_fchl(A, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -311,11 +315,11 @@ def get_global_symmetric_kernels_fchl(A, sigmas, \
assert len(sigmas.shape) == 1, "Second argument (sigmas) is not a 1D list/numpy.array!"

return fget_global_symmetric_kernels_fchl(A, N1, neighbors1, sigmas, \
nm1, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
nm1, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)


def get_global_kernels_fchl(A, B, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table", two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
Expand All @@ -336,6 +340,10 @@ def get_global_kernels_fchl(A, B, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -394,11 +402,11 @@ def get_global_kernels_fchl(A, B, sigmas, \
assert len(sigmas.shape) == 1, "Third argument (sigmas) is not a 1D list/numpy.array!"

return fget_global_kernels_fchl(A, B, N1, N2, neighbors1, neighbors2, sigmas, \
nm1, nm2, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
nm1, nm2, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)


def get_atomic_kernels_fchl(A, B, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table", two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:
Expand All @@ -419,6 +427,8 @@ def get_atomic_kernels_fchl(A, B, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -464,11 +474,11 @@ def get_atomic_kernels_fchl(A, B, sigmas, \
assert len(sigmas.shape) == 1

return fget_atomic_kernels_fchl(A, B, neighbors1, neighbors2, sigmas, \
na1, na2, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
na1, na2, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)


def get_atomic_symmetric_kernels_fchl(A, sigmas, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
t_width=np.pi/1.0, d_width=0.2, cut_start=1.0, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table",
two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
Expand All @@ -488,6 +498,8 @@ def get_atomic_symmetric_kernels_fchl(A, sigmas, \
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_start: The fraction of the cut-off radius at which cut-off damping start
:type cut_start: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
Expand Down Expand Up @@ -524,135 +536,4 @@ def get_atomic_symmetric_kernels_fchl(A, sigmas, \
assert len(sigmas.shape) == 1, "Second argument (sigmas) is not a 1D list/numpy.array!"

return fget_atomic_symmetric_kernels_fchl(A, neighbors1, sigmas, \
na1, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)


def get_atomic_force_alphas_fchl(A, F, sigmas, llambda=1e-7, \
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0, \
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table",
two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:

:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - B_j\\|_2^2}{2\sigma^2} \\big)`

Where :math:`A_{i}` and :math:`B_{j}` are FCHL representation vectors.
K is calculated analytically using an OpenMP parallel Fortran routine.
Note, that this kernel will ONLY work with FCHL representations as input.

:param A: Array of FCHL representation - shape=(N, maxsize, 5, size).
:type A: numpy array
:param sigma: List of kernel-widths.
:type sigma: list
:param t_width: Gaussian width for the angular (theta) terms.
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
:type r_width: float
:param c_width: Gaussian width along columns in the periodic table.
:type c_width: float
:param order: Fourier-expansion truncation order.
:type order: integer
:param scale_distance: Weight for distance-dependent terms.
:type scale_distance: float
:param scale_angular: Weight for angle-dependent terms.
:type scale_angular: float

:return: Array of FCHL kernel matrices matrix - shape=(n_sigmas, N, M),
:rtype: numpy array
"""

assert len(A.shape) == 3

na1 = A.shape[0]

neighbors1 = np.zeros((na1), dtype=np.int32)

for i, x in enumerate(A):
neighbors1[i] = len(np.where(x[0]< cut_distance)[0])

nsigmas = len(sigmas)

doalchemy, pd = get_alchemy(alchemy, emax=100, r_width=r_width, c_width=c_width,
n_width = n_width, m_width = m_width, l_width = l_width, s_width = s_width,
elemental_vectors=elemental_vectors)

sigmas = np.array(sigmas)
assert len(sigmas.shape) == 1, "Second argument (sigmas) is not a 1D list/numpy.array!"

return fget_atomic_force_alphas_fchl(A, F, neighbors1, sigmas, llambda, \
na1, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)


def get_atomic_force_kernels_fchl(A, B, sigmas,
t_width=np.pi/1.0, d_width=0.2, cut_distance=5.0,
r_width=1.0, order=1, c_width=0.5, scale_distance=1.0, scale_angular=0.1,
n_width = 1.0, m_width = 1.0, l_width = 1.0, s_width = 1.0, alchemy="periodic-table",
two_body_power=6.0, three_body_power=3.0, elemental_vectors=None):
""" Calculates the Gaussian kernel matrix K, where :math:`K_{ij}`:

:math:`K_{ij} = \\exp \\big( -\\frac{\\|A_i - B_j\\|_2^2}{2\sigma^2} \\big)`

Where :math:`A_{i}` and :math:`B_{j}` are FCHL representation vectors.
K is calculated analytically using an OpenMP parallel Fortran routine.
Note, that this kernel will ONLY work with FCHL representations as input.

:param A: Array of FCHL representation - shape=(N, maxsize, 5, size).
:type A: numpy array
:param B: Array of FCHL representation - shape=(M, maxsize, 5, size).
:type B: numpy array
:param sigma: List of kernel-widths.
:type sigma: list
:param t_width: Gaussian width for the angular (theta) terms.
:type t_width: float
:param d_width: Gaussian width for the distance terms.
:type d_width: float
:param cut_distance: Cut-off radius.
:type cut_distance: float
:param r_width: Gaussian width along rows in the periodic table.
:type r_width: float
:param c_width: Gaussian width along columns in the periodic table.
:type c_width: float
:param order: Fourier-expansion truncation order.
:type order: integer
:param scale_distance: Weight for distance-dependent terms.
:type scale_distance: float
:param scale_angular: Weight for angle-dependent terms.
:type scale_angular: float

:return: Array of FCHL kernel matrices matrix - shape=(n_sigmas, N, M),
:rtype: numpy array
"""

assert len(A.shape) == 3
assert len(B.shape) == 3

# assert B.shape[1] == atoms_max, "ERROR: Check FCHL representation sizes! code = 2"
# assert B.shape[3] == neighbors_max, "ERROR: Check FCHL representation sizes! code = 3"

na1 = A.shape[0]
na2 = B.shape[0]

neighbors1 = np.zeros((na1), dtype=np.int32)
neighbors2 = np.zeros((na2), dtype=np.int32)

for i, x in enumerate(A):
neighbors1[i] = len(np.where(x[0]< cut_distance)[0])

for i, x in enumerate(B):
neighbors2[i] = len(np.where(x[0]< cut_distance)[0])

nsigmas = len(sigmas)

doalchemy, pd = get_alchemy(alchemy, emax=100, r_width=r_width, c_width=c_width,
n_width = n_width, m_width = m_width, l_width = l_width, s_width = s_width,
elemental_vectors=elemental_vectors)

sigmas = np.array(sigmas)
assert len(sigmas.shape) == 1

return fget_atomic_force_kernels_fchl(A, B, neighbors1, neighbors2, sigmas, \
na1, na2, nsigmas, t_width, d_width, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
na1, nsigmas, t_width, d_width, cut_start, cut_distance, order, pd, scale_distance, scale_angular, doalchemy, two_body_power, three_body_power)
Loading