-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDensitySub.py
More file actions
131 lines (105 loc) · 4.56 KB
/
DensitySub.py
File metadata and controls
131 lines (105 loc) · 4.56 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
# -*-coding:utf-8-*-
__version__ = "v1.0.0"
__author__ = "Jia Liu"
__str__ = """
********** HELP MANUSCRIPT **********
[INTRODUCTION]
Calculate electron density difference manually. (for DMol3)
For CASTEP please use calculation property "Electron density difference" with "Sets of atoms".
Note that the total density and densities of different sets should be calculated before running this script.
This script is released under GPL v3.0 license.
Current version: %s
[AUTHOR] %s
[WEBSITE] https://github.com/liujiacode/EffectiveMass
[USAGE]
>> python DensitySub.py -t "total_file" -s "sub_files" -a "add_files" -o "output_file" [-h] [-v]
[PARAMETERS]
total electron density file ("sub_files" and "add_files"):
Total electron density file.
Note that the density files are usually hidden files with ".grd" extensions.
electron density files ("sub_files" and "add_files"):
Electron density files for subtracting and adding from/to total density.
The file paths should be separated by ';' without spaces.
output density file ("output_file")
For output.
**************** END ****************
""" % (__version__, __author__)
import os
import sys
import getopt
class DensitySub(object):
def __init__(self, total, subs, adds):
if os.path.isfile(total):
self._total = open(total,'r')
else:
raise ValueError("Invalid total density file: %s." % total)
if isinstance(subs, list) or isinstance(subs, tuple):
self._subs = []
for sub in subs:
if os.path.isfile(sub):
self._subs.append(open(sub, 'r'))
else:
raise ValueError("Invalid sub density file: %s." % sub)
if isinstance(adds, list) or isinstance(adds, tuple):
self._adds = []
for add in adds:
if os.path.isfile(add):
self._adds.append(open(add, 'r'))
else:
raise ValueError("Invalid add density file: %s." % add)
def calculate(self):
total_data = self._total.read().split('\n')
while '' in total_data:
total_data.remove('')
for sub in self._subs:
sub_data = sub.read().split('\n')
while '' in sub_data:
sub_data.remove('')
if sub_data[:5] != total_data[:5]:
raise ValueError("The crystal infos for sub are mismatching.")
if len(sub_data) != len(total_data):
raise ValueError("The points length for sub is mismatching.")
for poi in range(len(total_data) - 5):
total_data[5 + poi] = float(total_data[5 + poi]) - float(sub_data[5 + poi])
for add in self._adds:
add_data = add.read().split('\n')
while '' in add_data:
add_data.remove('')
if add_data[:5] != total_data[:5]:
raise ValueError("The crystal infos for add are mismatching.")
if len(add_data) != len(total_data):
raise ValueError("The points length for add is mismatching.")
for poi in range(len(total_data) - 5):
total_data[5 + poi] = float(total_data[5 + poi]) + float(add_data[5 + poi])
return total_data
def write(self, output_path):
total_data = self.calculate()
with open(output_path, 'w') as output:
for data in total_data:
output.write(str(data) + '\n')
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "t:s:a:o:hv", ["total=", "subs=", "adds=", "output", "help", "version"])
total_file = ""
sub_files = []
add_files = []
output_file = ""
for op, value in opts:
if op in ("-h", "--help"):
print(__str__)
exit()
elif op in ("-v", "--version"):
print("DensitySub %s" % __version__)
exit()
elif op in ("-t", "--total"):
total_file = value
elif op in ("-s", "--subs"):
sub_files = value.split(';')
elif op in ("-a", "--adds"):
add_files = value.split(';')
elif op in ("-o", "--output"):
output_file = value
else:
raise ValueError("Invalid option: %s." % op)
ds = DensitySub(total=total_file, subs=sub_files, adds=add_files)
ds.write(output_file)