Skip to content

Commit

Permalink
Merge branch 'dev' of /home/qssun/workspace/program/pyscf into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed May 9, 2018
2 parents b539aa2 + e05a67b commit 1663e5a
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 18 deletions.
37 changes: 37 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# .coveragerc to control coverage.py
[run]
branch = True
omit = gto/basis/*
*test*
dmrgscf/*
fciqmcscf/*
shciscf/*
xianci/*
icmpspt/*
gen_*_param.py

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain about missing debug-only code:
def __repr__

# Don't complain if tests don't hit defensive assertion code:
raise RuntimeError
raise NotImplementedError
pass

# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
if sys.version_info.*:
if DEBUG:
except ImportError:

ignore_errors = True

[html]
directory = coverage_html_report
60 changes: 47 additions & 13 deletions pyscf/gto/basis/parse_nwchem.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def search_ecp(basisfile, symb):
symb = _std_symbol(symb)
with open(basisfile, 'r') as fin:
fdata = re.split(ECP_DELIMITER, fin.read())
if len(fdata) <= 1:
return []

fdata = fdata[1].splitlines()
for i, dat in enumerate(fdata):
dat0 = dat.split(None, 1)
Expand Down Expand Up @@ -216,7 +219,7 @@ def _parse(raw_basis, optimize=True):
try:
line = [float(x) for x in dat.replace('D','e').split()]
except BaseException as e:
raise RuntimeError('\n' + e.message +
raise RuntimeError('\n' + str(e) +
'\nor the required basis file not existed.')
if key == 'SP':
basis_add[-2].append([line[0], line[1]])
Expand All @@ -230,35 +233,63 @@ def _parse(raw_basis, optimize=True):
if optimize:
basis_sorted = optimize_contraction(basis_sorted)

basis_sorted = remove_zero(basis_sorted)
return basis_sorted

def optimize_contraction(basis):
'''
Optimize contraction: segment contraction -> general contraction
'''
bas_l = [[] for l in range(MAXL)]
basdic = {}
for b in basis:
l = b[0]
ec = numpy.array(b[1:]).T
if isinstance(b[1], int): # kappa = b[1]
key = tuple(b[:2])
ec = numpy.array(b[2:]).T
else:
key = tuple(b[:1])
ec = numpy.array(b[1:]).T
es = ec[0]
cs = [c for c in ec[1:]]
if bas_l[l]:
for e_cs in bas_l[l]:

if key not in basdic:
basdic[key] = []

if basdic[key]:
for e_cs in basdic[key]:
if numpy.array_equal(e_cs[0], es):
e_cs.extend(cs)
break
else:
bas_l[l].append([es] + cs)
basdic[key].append([es] + cs)
else:
bas_l[l].append([es] + cs)
basdic[key].append([es] + cs)

basis = []
for l, b in enumerate(bas_l):
for e_cs in b:
b_l = [l] + numpy.array(e_cs).T.tolist()
basis.append(b_l)
for key in sorted(basdic.keys()):
l_kappa = list(key)
for e_cs in basdic[key]:
b = l_kappa + numpy.array(e_cs).T.tolist()
basis.append(b)
return basis

def remove_zero(basis):
'''
Remove the exponets if their contraction coefficients are all zeros.
'''
new_basis = []
for b in basis:
if isinstance(b[1], int): # kappa = b[1]
key = list(b[:2])
ec = b[2:]
else:
key = list(b[:1])
ec = b[1:]

new_ec = [e_c for e_c in ec if any(c!=0 for c in e_c[1:])]
if new_ec:
new_basis.append(key + new_ec)
return new_basis

def _parse_ecp(raw_ecp):
ecp_add = []
nelec = None
Expand All @@ -281,7 +312,10 @@ def _parse_ecp(raw_ecp):
line = dat.replace('D','e').split()
l = int(line[0])
by_ang[l].append([float(x) for x in line[1:]])
if nelec is not None:

if nelec is None:
return []
else:
bsort = []
for l in range(-1, MAXL):
bsort.extend([b for b in ecp_add if b[0] == l])
Expand Down
52 changes: 51 additions & 1 deletion pyscf/gto/test/test_basis_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_parse_pople(self):
def test_basis_load(self):
self.assertEqual(gto.basis.load(__file__, 'H'), [])
self.assertRaises(KeyError, gto.basis.load, 'abas', 'H')
#self.assertRaises(RuntimeError, gto.basis.load(__file__, 'C'), [])

self.assertEqual(len(gto.basis.load('631++g**', 'C')), 8)
self.assertEqual(len(gto.basis.load('ccpcvdz', 'C')), 7)
Expand All @@ -50,7 +51,7 @@ def test_basis_load(self):
self.assertEqual(bas, basdat1)

def test_basis_load_ecp(self):
self.assertEqual(gto.basis.load(__file__, 'H'), [])
self.assertEqual(gto.basis.load_ecp(__file__, 'H'), [])

def test_parse_basis(self):
basis_str = '''
Expand Down Expand Up @@ -97,6 +98,55 @@ def test_parse_ecp(self):
gto.basis.parse_nwchem.convert_ecp_to_nwchem('Na', ecpdat), 'Na')
self.assertEqual(ecpdat, ecpdat1)

def test_optimize_contraction(self):
bas = gto.parse(r'''
#BASIS SET: (6s,3p) -> [2s,1p]
C S
2.9412494 -0.09996723
0.6834831 0.39951283
0.2222899 0.70011547
C S
2.9412494 0.15591627
0.6834831 0.60768372
0.2222899 0.39195739
''', optimize=True)
self.assertEqual(len(bas), 1)

bas = [[1, 0,
[2.9412494, -0.09996723],
[0.6834831, 0.39951283],
[0.2222899, 0.70011547]],
[1, 1,
[2.9412494, -0.09996723],
[0.6834831, 0.39951283],
[0.2222899, 0.70011547]],
[1, 1,
[2.9412494, 0.15591627],
[0.6834831, 0.60768372],
[0.2222899, 0.39195739]]]
bas = gto.basis.parse_nwchem.optimize_contraction(bas)
self.assertEqual(len(bas), 2)

def test_remove_zero(self):
bas = gto.parse(r'''
C S
7.2610457926 0.0000000000 0.0000000000
2.1056583087 0.0000000000 0.0000000000
0.6439906571 1.0000000000 0.0000000000
0.0797152017 0.0000000000 1.0000000000
0.0294029590 0.0000000000 0.0000000000
''')
self.assertEqual(len(bas[0]), 3)

bas = [[0, 0,
[7.2610457926, 0.0000000000, 0.0000000000],
[2.1056583087, 0.0000000000, 0.0000000000],
[0.6439906571, 1.0000000000, 0.0000000000],
[0.0797152017, 0.0000000000, 1.0000000000],
[0.0294029590, 0.0000000000, 0.0000000000]]]
bas = gto.basis.parse_nwchem.remove_zero(bas)
self.assertEqual(len(bas[0]), 4)


if __name__ == "__main__":
print("test basis module")
Expand Down
14 changes: 10 additions & 4 deletions pyscf/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ configure_file(
# to find config.h
include_directories("${PROJECT_BINARY_DIR}")

SET(CMAKE_SKIP_BUILD_RPATH True)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH True)
SET(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/deps/lib")
#TODO: test me set(CMAKE_MACOSX_RPATH OFF)
# See also https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling
if (WIN32)
#?
elseif (APPLE)
#TODO: test me set(CMAKE_MACOSX_RPATH On)
else ()
set(CMAKE_SKIP_BUILD_RPATH True)
set(CMAKE_BUILD_WITH_INSTALL_RPATH True)
set(CMAKE_INSTALL_RPATH "\$ORIGIN:\$ORIGIN/deps/lib")
endif ()

add_subdirectory(np_helper)
add_subdirectory(gto)
Expand Down
1 change: 1 addition & 0 deletions pyscf/pbc/gto/basis/parse_cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _parse(blines, optimize=False):
if optimize:
basis_sorted = parse_nwchem.optimize_contraction(basis_sorted)

basis_sorted = parse_nwchem.remove_zero(basis_sorted)
return basis_sorted

BASIS_SET_DELIMITER = re.compile('# *BASIS SET.*\n')
Expand Down

0 comments on commit 1663e5a

Please sign in to comment.