Skip to content

Commit

Permalink
Fix rob issues in basis
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-Tehrani committed Aug 5, 2020
1 parent 8eb2ea7 commit ef08c7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
34 changes: 19 additions & 15 deletions iodata/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@

def _alsolist(f):
"""Wrap a function to accepts also list as first argument and then return list."""

@wraps(f)
def wrapper(firsts, *args, **kwargs):
if isinstance(firsts, (Integral, str)):
return f(firsts, *args, **kwargs)
return [f(first, *args, **kwargs) for first in firsts]

return wrapper


Expand Down Expand Up @@ -83,7 +85,7 @@ def angmom_its(angmom: Union[int, List[int]]) -> Union[str, List[str]]:


class Shell(NamedTuple):
"""Contracted shell representing (generalized) contractions with the same exponents.
r"""Contracted shell representing (generalized) contractions with the same exponents.
Attributes
----------
Expand Down Expand Up @@ -123,7 +125,7 @@ class Shell(NamedTuple):
C_{lm}(r, \theta, \phi) &:= \sqrt{2} (-1)^m \sqrt{\frac{(l - m)!}{(l + m)!}}
r^l P^m_l(\cos(\theta)) \cos(m \phi) & m \in \{1, \cdots, l\} \\
S_{lm}(r, \theta, \phi) &:= \sqrt{2} (-1)^m \sqrt{\frac{(l - m)!}{(l + m)!}}
r^l P^m_l(\cos(\theta)) \sin(m \phi) & m \in \{1, \cdots, l\} \\
r^l P^m_l(\cos(\theta)) \sin(m \phi) & m \in \{1, \cdots, l\}\\
\end{align*}
where :math:`P_l^m` is the associated Legrende function and :math:`N(\alpha, l)` is the
Expand All @@ -132,7 +134,7 @@ class Shell(NamedTuple):
- Contraction of Primitive Gaussians is the linear combination of primitive Gaussian functions
of the same shell-type l, meant as a basis function. It has the form
.. math:: P(\cdots) \sum^M_m c_m N e^{-\alpha_m r_A^2},
.. math:: P(\cdots) \sum^M_m c_m N e^{-\alpha_m r_A^2},
where N is the normalization constant and P is either the Cartesian polynomial for a fixed
(i, j, k) or the real regular solid harmonic for a fixed (l, m). The degree is the number
Expand All @@ -154,7 +156,7 @@ class Shell(NamedTuple):
coeffs: np.ndarray

@property
def nbasis(self) -> int: # noqa: D401
def nbasis(self) -> int: # noqa: D401
"""Number of basis functions (e.g. 3 for a P shell and 4 for an SP shell)."""
result = 0
for angmom, kind in zip(self.angmoms, self.kinds):
Expand All @@ -167,21 +169,21 @@ def nbasis(self) -> int: # noqa: D401
return result

@property
def nprim(self) -> int: # noqa: D401
def nprim(self) -> int: # noqa: D401
"""Number of primitive contracted shells, also known as the contraction length."""
return len(self.exponents)

@property
def ncon(self) -> int: # noqa: D401
def ncon(self) -> int: # noqa: D401
"""Number of contractions with distinct angular momentum numbers.
This is usually 1; e.g., it would be 2 for an SP shell.
"""
This is usually 1; e.g., it would be 2 for an SP shell.
"""
return len(self.angmoms)


class MolecularBasis(NamedTuple):
"""A complete molecular orbital or density basis set as a collection of contracted shells.
r"""A complete molecular orbital or density basis set as a collection of contracted shells.
Attributes
----------
Expand Down Expand Up @@ -256,7 +258,7 @@ class MolecularBasis(NamedTuple):
primitive_normalization: str

@property
def nbasis(self) -> int: # noqa: D401
def nbasis(self) -> int: # noqa: D401
"""Number of basis functions."""
return sum(shell.nbasis for shell in self.shells)

Expand Down Expand Up @@ -319,7 +321,7 @@ def convert_kind(self, to: str):
convert_primitive_kind : Converts primitive shells with no averaging.
"""
if to != "c" and to != "p":
if to not in ("c", "p"):
raise ValueError("The to string was not recognized: %s" % to)

shells = []
Expand Down Expand Up @@ -349,15 +351,17 @@ def convert_kind(self, to: str):
coeffs_new.append(coeffs)

shells.append(
Shell(shell.icenter, angmom, kind_new, shell.exponents, np.array(coeffs_new).T)
Shell(
shell.icenter, shell.angmoms, kind_new, shell.exponents, np.array(coeffs_new).T
)
)
# pylint: disable=no-member
return self._replace(shells=shells)


def convert_primitive_kind(angmom: int, kind: str, coeffs: np.ndarray, to: str) -> np.ndarray:
r"""
Converts coefficients in Cartesian to Pure or vice-versa of a Primitive shell.
Convert coefficients in Cartesian to Pure or vice-versa of a Primitive shell.
Parameters
----------
Expand Down Expand Up @@ -398,9 +402,9 @@ def convert_primitive_kind(angmom: int, kind: str, coeffs: np.ndarray, to: str)
>> new_coeffs = convert_primitive_kind(1, "c", coeff, "p")
"""
if to != "c" and to != "p":
if to not in ("p", "c"):
raise ValueError("The to string was not recognized: %s" % to)
if kind != "c" and kind != "p":
if kind not in ("p", "c"):
raise ValueError("The kind string was not recognized: %s" % kind)
if to != kind:
if kind == "c":
Expand Down
4 changes: 2 additions & 2 deletions iodata/test/test_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def test_get_decontracted():
ncontshells = 8 # Number of contracted shells now is equal to the number of primitives.
assert_equal(len(obasis.shells), ncontshells)
# Assert each shell has length one exponents
for i in range(0, ncontshells):
for _ in range(0, ncontshells):
assert_equal(len(obasis.shells[0].exponents), 1)
# Assert they have the right exponents.
assert_equal(obasis.shells[0].exponents, 0.01)
Expand Down Expand Up @@ -339,7 +339,7 @@ def test_convert_primitive_kind():
# Test converting D-type from kind="p" to kind="c".
coeff = np.array([1., 2., 3., 4., 5.])
ccoeff = convert_primitive_kind(2, "p", coeff, "c")
desired = np.array([1.97606774, 5., 2., -2.64273441, 3., 0.66666667])
desired = np.array([1.97606774, 5., 2., -2.64273441, 3., 0.66666667])
assert_allclose(ccoeff, desired)


Expand Down

0 comments on commit ef08c7e

Please sign in to comment.