-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (61 loc) · 2.61 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/python3
import numpy as np
def read_orbitals(filename, norbs):
''' reads any file containing a square matrix into an numpy squaree array'''
with open(filename) as f:
lines = f.readlines()
coefficients = []
for line in lines:
entries = line.split()
for entry in entries:
coefficients.append(float(entry))
orbitals = np.reshape(coefficients, (norbs, norbs))
return orbitals
def read_header(filename):
header = []
with open(filename,'r') as f:
for line in f:
if 'ORBITALS' in line:
return header
header.append(line)
def write_matrop(fname, mat1, mat2, new_dim, nelec,header):
''' takes matrix, extracts upper traingale, writes it in columns of 3 into file'''
# write header
with open(fname, 'w') as fin:
for line in header:
fin.write(line.replace('RHF','FCI'))
n = nelec[0]+nelec[1]
excess = nelec[0]-nelec[1]
microheaders = [' DENSITY CHARGE 1 1 1 '+str(n)+' '+str(excess)+'\n', ' DENSITY SPIN 2 1 1 '+str(n)+' '+str(excess)+'\n']
for j,mat in enumerate([mat1, mat2]):
vector = mat[0,0]
for i in range(1,new_dim):
vector = np.append(vector, mat[:i+1,i])
with open(fname, 'a') as fin:
fin.write(microheaders[j])
if (len(vector)%3==0):
for i in range(0,len(vector),3):
fin.write("%0.15E, %0.15E, %0.15E,\n"%(vector[i], vector[i+1], vector[i+2]))
if (len(vector)%3==1):
for i in range(0,len(vector)-3,3):
fin.write("%0.15E, %0.15E, %0.15E,\n"%(vector[i], vector[i+1], vector[i+2]))
fin.write("%0.15E,\n"%vector[-1])
if (len(vector)%3==2):
for i in range(0,len(vector)-3,3):
fin.write("%0.15E, %0.15E, %0.15E,\n"%(vector[i], vector[i+1], vector[i+2]))
fin.write("%0.15E, %0.15E,\n"%vector[-2], vector[-1])
with open(fname, 'a') as fin:
fin.write(" ---")
def read_orbitals_from_record(filename, norbs):
''' Reads Molpro record file and parses Orbitals intpó matrix'''
with open(filename) as f:
lines = f.readlines()
words = []
for line in lines:
line.replace('D+','E+')
for word in line.replace(',','').replace('D+','E+').replace('D-','E-').split():
words.append(word)
start = words.index("ORBITALS")+7
end = words.index('EIG')
orbitals = np.reshape(np.array(words[start:end]), (norbs, norbs)).astype(float).transpose()
return orbitals