| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (c) 2018, the cclib development team |
| 4 |
# |
| 5 |
# This file is part of cclib (http://cclib.github.io) and is distributed under |
| 6 |
# the terms of the BSD 3-Clause License. |
| 7 |
|
| 8 |
"""Utilities often used by cclib parsers and scripts""" |
| 9 |
|
| 10 |
import sys |
| 11 |
import numpy |
| 12 |
|
| 13 |
|
| 14 |
# See https://github.com/kachayev/fn.py/commit/391824c43fb388e0eca94e568ff62cc35b543ecb |
| 15 |
if sys.version_info <= (3, 3): |
| 16 |
import operator |
| 17 |
def accumulate(iterable, func=operator.add): |
| 18 |
"""Return running totals""" |
| 19 |
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15 |
| 20 |
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120 |
| 21 |
it = iter(iterable) |
| 22 |
try: |
| 23 |
total = next(it) |
| 24 |
except StopIteration: |
| 25 |
return |
| 26 |
yield total |
| 27 |
for element in it: |
| 28 |
total = func(total, element) |
| 29 |
yield total |
| 30 |
else: |
| 31 |
from itertools import accumulate |
| 32 |
|
| 33 |
|
| 34 |
def find_package(package): |
| 35 |
"""Check if a package exists without importing it. |
| 36 |
|
| 37 |
Derived from https://stackoverflow.com/a/14050282 |
| 38 |
""" |
| 39 |
if sys.version_info.major == 2: |
| 40 |
import pkgutil |
| 41 |
return pkgutil.find_loader(package) is not None |
| 42 |
else: |
| 43 |
import importlib |
| 44 |
module_spec = importlib.util.find_spec(package) |
| 45 |
return module_spec is not None and module_spec.loader is not None |
| 46 |
|
| 47 |
|
| 48 |
def symmetrize(m, use_triangle='lower'): |
| 49 |
"""Symmetrize a square NumPy array by reflecting one triangular |
| 50 |
section across the diagonal to the other. |
| 51 |
""" |
| 52 |
|
| 53 |
if use_triangle not in ('lower', 'upper'): |
| 54 |
raise ValueError |
| 55 |
if not len(m.shape) == 2: |
| 56 |
raise ValueError |
| 57 |
if not (m.shape[0] == m.shape[1]): |
| 58 |
raise ValueError |
| 59 |
|
| 60 |
dim = m.shape[0] |
| 61 |
|
| 62 |
lower_indices = numpy.tril_indices(dim, k=-1) |
| 63 |
upper_indices = numpy.triu_indices(dim, k=1) |
| 64 |
|
| 65 |
ms = m.copy() |
| 66 |
|
| 67 |
if use_triangle == 'lower': |
| 68 |
ms[upper_indices] = ms[lower_indices] |
| 69 |
if use_triangle == 'upper': |
| 70 |
ms[lower_indices] = ms[upper_indices] |
| 71 |
|
| 72 |
return ms |
| 73 |
|
| 74 |
|
| 75 |
def convertor(value, fromunits, tounits): |
| 76 |
"""Convert from one set of units to another. |
| 77 |
|
| 78 |
Sources: |
| 79 |
NIST 2010 CODATA (http://physics.nist.gov/cuu/Constants/index.html) |
| 80 |
Documentation of GAMESS-US or other programs as noted |
| 81 |
""" |
| 82 |
|
| 83 |
_convertor = { |
| 84 |
|
| 85 |
"time_au_to_fs": lambda x: x * 0.02418884, |
| 86 |
"fs_to_time_au": lambda x: x / 0.02418884, |
| 87 |
|
| 88 |
"Angstrom_to_bohr": lambda x: x * 1.8897261245, |
| 89 |
"bohr_to_Angstrom": lambda x: x * 0.5291772109, |
| 90 |
|
| 91 |
"wavenumber_to_eV": lambda x: x / 8065.54429, |
| 92 |
"wavenumber_to_hartree": lambda x: x / 219474.6313708, |
| 93 |
"wavenumber_to_kcal/mol": lambda x: x / 349.7550112, |
| 94 |
"wavenumber_to_kJ/mol": lambda x: x / 83.5934722814, |
| 95 |
"wavenumber_to_nm": lambda x: 1e7 / x, |
| 96 |
"wavenumber_to_Hz": lambda x: x * 29.9792458, |
| 97 |
|
| 98 |
"eV_to_wavenumber": lambda x: x * 8065.54429, |
| 99 |
"eV_to_hartree": lambda x: x / 27.21138505, |
| 100 |
"eV_to_kcal/mol": lambda x: x * 23.060548867, |
| 101 |
"eV_to_kJ/mol": lambda x: x * 96.4853364596, |
| 102 |
|
| 103 |
"hartree_to_wavenumber": lambda x: x * 219474.6313708, |
| 104 |
"hartree_to_eV": lambda x: x * 27.21138505, |
| 105 |
"hartree_to_kcal/mol": lambda x: x * 627.50947414, |
| 106 |
"hartree_to_kJ/mol": lambda x: x * 2625.4996398, |
| 107 |
|
| 108 |
"kcal/mol_to_wavenumber": lambda x: x * 349.7550112, |
| 109 |
"kcal/mol_to_eV": lambda x: x / 23.060548867, |
| 110 |
"kcal/mol_to_hartree": lambda x: x / 627.50947414, |
| 111 |
"kcal/mol_to_kJ/mol": lambda x: x * 4.184, |
| 112 |
|
| 113 |
"kJ/mol_to_wavenumber": lambda x: x * 83.5934722814, |
| 114 |
"kJ/mol_to_eV": lambda x: x / 96.4853364596, |
| 115 |
"kJ/mol_to_hartree": lambda x: x / 2625.49963978, |
| 116 |
"kJ/mol_to_kcal/mol": lambda x: x / 4.184, |
| 117 |
"nm_to_wavenumber": lambda x: 1e7 / x, |
| 118 |
|
| 119 |
# Taken from GAMESS docs, "Further information", |
| 120 |
# "Molecular Properties and Conversion Factors" |
| 121 |
"Debye^2/amu-Angstrom^2_to_km/mol": lambda x: x * 42.255, |
| 122 |
|
| 123 |
# Conversion for charges and multipole moments. |
| 124 |
"e_to_coulomb": lambda x: x * 1.602176565 * 1e-19, |
| 125 |
"e_to_statcoulomb": lambda x: x * 4.80320425 * 1e-10, |
| 126 |
"coulomb_to_e": lambda x: x * 0.6241509343 * 1e19, |
| 127 |
"statcoulomb_to_e": lambda x: x * 0.2081943527 * 1e10, |
| 128 |
"ebohr_to_Debye": lambda x: x * 2.5417462300, |
| 129 |
"ebohr2_to_Buckingham": lambda x: x * 1.3450341749, |
| 130 |
"ebohr2_to_Debye.ang": lambda x: x * 1.3450341749, |
| 131 |
"ebohr3_to_Debye.ang2": lambda x: x * 0.7117614302, |
| 132 |
"ebohr4_to_Debye.ang3": lambda x: x * 0.3766479268, |
| 133 |
"ebohr5_to_Debye.ang4": lambda x: x * 0.1993134985, |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
return _convertor["%s_to_%s" % (fromunits, tounits)](value) |
| 138 |
|
| 139 |
|
| 140 |
class PeriodicTable(object): |
| 141 |
"""Allows conversion between element name and atomic no.""" |
| 142 |
|
| 143 |
def __init__(self): |
| 144 |
self.element = [ |
| 145 |
None, |
| 146 |
'H', 'He', |
| 147 |
'Li', 'Be', |
| 148 |
'B', 'C', 'N', 'O', 'F', 'Ne', |
| 149 |
'Na', 'Mg', |
| 150 |
'Al', 'Si', 'P', 'S', 'Cl', 'Ar', |
| 151 |
'K', 'Ca', |
| 152 |
'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', |
| 153 |
'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', |
| 154 |
'Rb', 'Sr', |
| 155 |
'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', |
| 156 |
'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', |
| 157 |
'Cs', 'Ba', |
| 158 |
'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', |
| 159 |
'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', |
| 160 |
'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', |
| 161 |
'Fr', 'Ra', |
| 162 |
'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', |
| 163 |
'Lr', 'Rf', 'Db', 'Sg', 'Bh', 'Hs', 'Mt', 'Ds', 'Rg', 'Cn', |
| 164 |
'Uut', 'Fl', 'Uup', 'Lv', 'Uus', 'Uuo'] |
| 165 |
self.number = {} |
| 166 |
for i in range(1, len(self.element)): |
| 167 |
self.number[self.element[i]] = i |
| 168 |
|
| 169 |
|
| 170 |
class WidthSplitter: |
| 171 |
"""Split a line based not on a character, but a given number of field |
| 172 |
widths. |
| 173 |
""" |
| 174 |
|
| 175 |
def __init__(self, widths): |
| 176 |
self.start_indices = [0] + list(accumulate(widths))[:-1] |
| 177 |
self.end_indices = list(accumulate(widths)) |
| 178 |
|
| 179 |
def split(self, line, truncate=True): |
| 180 |
"""Split the given line using the field widths passed in on class |
| 181 |
initialization. |
| 182 |
""" |
| 183 |
elements = [line[start:end].strip() |
| 184 |
for (start, end) in zip(self.start_indices, self.end_indices)] |
| 185 |
# Handle lines that contain fewer fields than specified in the |
| 186 |
# widths; they are added as empty strings, so remove them. |
| 187 |
if truncate: |
| 188 |
while len(elements) and elements[-1] == '': |
| 189 |
elements.pop() |
| 190 |
return elements |