root / prepareQMX.py @ 8
Historique | Voir | Annoter | Télécharger (7,18 ko)
1 |
#!/usr/bin/env python
|
---|---|
2 |
import os, sys |
3 |
from optparse import OptionParser |
4 |
|
5 |
class Data: |
6 |
def __init__(self, name): |
7 |
self.__name__ = name
|
8 |
self.str_option = "" |
9 |
self.str_import = None |
10 |
self.str_class = None |
11 |
self.str_methods = []
|
12 |
self.str_structure = None |
13 |
|
14 |
def __str__(self): |
15 |
return self.__name__ |
16 |
|
17 |
def getline(section, subsection, lines): |
18 |
nline = -1
|
19 |
for iline in xrange(len(lines)): |
20 |
if lines[iline][0].strip() == subsection: |
21 |
if nline != -1: |
22 |
sys.stderr.write("section: <"+section+separator+subsection+"> has been definied more than once\n") |
23 |
sys.exit(1)
|
24 |
nline = iline |
25 |
if nline == -1: |
26 |
return None |
27 |
return lines[nline][1].strip() |
28 |
|
29 |
def analyzeAtoms(section, lines): |
30 |
data = Data(section) |
31 |
line = getline(section, "program", lines)
|
32 |
if line is None: |
33 |
line=""
|
34 |
if True or line.lower() == "turbomole": |
35 |
data.str_import = "from ase.io.turbomole import read_turbomole"
|
36 |
data.str_class = "read_turbomole"
|
37 |
|
38 |
data.str_structure = getline(section, "structure", lines)
|
39 |
return data
|
40 |
|
41 |
def analyzeCalculator(section, lines): |
42 |
data = Data(section) |
43 |
#read programs
|
44 |
line = getline(section, "program", lines)
|
45 |
if line.lower() == "turbomole": |
46 |
data.str_import = "from ase.calculators.turbomole import Turbomole"
|
47 |
data.str_class = "Turbomole"
|
48 |
elif line.lower() == "vasp": |
49 |
data.str_import = "from ase.calculators.vasp import Vasp"
|
50 |
data.str_class = "Vasp"
|
51 |
|
52 |
str = getline(section, "class", lines)
|
53 |
if str is not None: |
54 |
data.str_class = str
|
55 |
|
56 |
str = getline(section, "import", lines)
|
57 |
if str is not None: |
58 |
data.str_import = str
|
59 |
|
60 |
str = getline(section, "options", lines)
|
61 |
if str is not None: |
62 |
data.str_option = str
|
63 |
return data
|
64 |
|
65 |
def analyzeDynamics(section, lines): |
66 |
data = Data(section) |
67 |
line = getline(section, "method", lines)
|
68 |
if line.lower() == "optimizer": |
69 |
data.str_import = "from ase.optimize import QuasiNewton"
|
70 |
data.str_class = "QuasiNewton"
|
71 |
return data
|
72 |
|
73 |
#--- setting default values ------------------------------------------------------------------------
|
74 |
#define separators
|
75 |
separator=":"
|
76 |
subseparator="."
|
77 |
lineseparator="\n"
|
78 |
|
79 |
#--- parse arguments -------------------------------------------------------------------------------
|
80 |
#parse arguments (python 2.6)
|
81 |
parser = OptionParser() |
82 |
parser.add_option("-i", "--infile", dest="inputFile", help="specifies the input file", metavar="FILE", default="qmx.in") |
83 |
parser.add_option("-o", "--outfile", dest="outputFile", help="specifies the output file", metavar="FILE") |
84 |
parser.add_option("-f", action="store_true", dest="overwrite") |
85 |
(options, args) = parser.parse_args() |
86 |
|
87 |
file=open(options.inputFile, "r") |
88 |
lines = file.readlines()
|
89 |
file.close()
|
90 |
|
91 |
|
92 |
#--- split intput file -----------------------------------------------------------------------------
|
93 |
|
94 |
#--- remove comments -------------------------------------------------------------------------------
|
95 |
for iline in xrange(len(lines)): |
96 |
lines[iline] = lines[iline].split("#")[0] |
97 |
|
98 |
#--- collect data in class -------------------------------------------------------------------------
|
99 |
data_collection=[] |
100 |
|
101 |
#--- settings for QMX class ------------------------------------------------------------------------
|
102 |
data = Data("qmx")
|
103 |
data.str_class = "Qmx"
|
104 |
data.str_import = "from ase.calculators.qmx import Qmx"
|
105 |
data.str_option = "highLevel, lowLevel"
|
106 |
|
107 |
data_collection.append(data) |
108 |
|
109 |
#--- settings for Embed class ----------------------------------------------------------------------
|
110 |
data = Data("embed")
|
111 |
data.str_class = "Embed"
|
112 |
data.str_import = "from ase.embed import Embed"
|
113 |
data.str_option = "system, cluster"
|
114 |
data.str_methods = ["embed()", "set_calculator(qmx)"] |
115 |
|
116 |
data_collection.append(data) |
117 |
|
118 |
|
119 |
#--- analyze the input file ------------------------------------------------------------------------
|
120 |
for section in "high-level", "low-level", "cluster", "system", "dynamics": |
121 |
#search lines for section
|
122 |
lines_section=[] |
123 |
for line in lines: |
124 |
line = line.split(separator) |
125 |
if line[0].find(section) >= 0: |
126 |
line[0] = line[0].replace(section+subseparator, "") |
127 |
lines_section.append(line) |
128 |
|
129 |
#calculators
|
130 |
if section == "high-level" or section == "low-level": |
131 |
data_collection.append(analyzeCalculator(section, lines_section)) |
132 |
|
133 |
#systems
|
134 |
if section == "cluster" or section == "system": |
135 |
data_collection.append(analyzeAtoms(section, lines_section)) |
136 |
|
137 |
#dynamics
|
138 |
if section == "dynamics": |
139 |
data_collection.append(analyzeDynamics(section, lines_section)) |
140 |
|
141 |
#--- write output file -----------------------------------------------------------------------------
|
142 |
if options.outputFile is not None and os.path.exists(options.outputFile) and not options.overwrite: |
143 |
sys.stderr.write(lineseparator+"the output file <"+options.outputFile+"> already exists"+lineseparator) |
144 |
sys.exit(1)
|
145 |
|
146 |
output = None
|
147 |
if options.outputFile is None: |
148 |
output=sys.stdout |
149 |
else:
|
150 |
output=open(options.outputFile, "w") |
151 |
|
152 |
#--- write header ----------------------------------------------------------------------------------
|
153 |
output.write("#!/usr/bin/env python\n")
|
154 |
|
155 |
#--- write import lines ----------------------------------------------------------------------------
|
156 |
xlines=[] |
157 |
for data in data_collection: |
158 |
value = data.str_import |
159 |
if value is None or value in xlines: |
160 |
continue
|
161 |
xlines.append(value) |
162 |
output.write(value+lineseparator) |
163 |
output.write(lineseparator) |
164 |
|
165 |
#--- define the methods ----------------------------------------------------------------------------
|
166 |
for system in 'high', 'low': |
167 |
for data in data_collection: |
168 |
if data.__name__ == system+"-level": |
169 |
output.write(system+"Level = "+data.str_class+"("+data.str_option+")"+lineseparator) |
170 |
output.write(lineseparator) |
171 |
|
172 |
#--- qmx class (substraction scheme ----------------------------------------------------------------
|
173 |
for data in data_collection: |
174 |
if data.__name__ == "qmx": |
175 |
output.write("qmx = "+data.str_class+"("+data.str_option+")"+lineseparator) |
176 |
output.write(lineseparator) |
177 |
|
178 |
#--- read all the systems --------------------------------------------------------------------------
|
179 |
for system in "cluster", "system": |
180 |
for data in data_collection: |
181 |
if data.__name__ == system:
|
182 |
if data.str_structure is None: |
183 |
continue
|
184 |
output.write(system+" = "+data.str_class+"(\""+data.str_structure+"\")"+lineseparator) |
185 |
output.write(lineseparator) |
186 |
|
187 |
#--- embeding class --------------------------------------------------------------------------------
|
188 |
for data in data_collection: |
189 |
if data.__name__ == "embed": |
190 |
output.write("embed = "+data.str_class+"("+data.str_option+")"+lineseparator) |
191 |
for method in data.str_methods: |
192 |
output.write("embed."+method+lineseparator)
|
193 |
output.write(lineseparator) |
194 |
|
195 |
#--- dynamics class --------------------------------------------------------------------------------
|
196 |
for data in data_collection: |
197 |
if data.__name__ == "dynamics": |
198 |
output.write("dyn="+data.str_class+"(embed)"+lineseparator) |
199 |
output.write("dyn.run("+data.str_option+")"+lineseparator) |
200 |
|
201 |
output.close() |
202 |
sys.exit(0)
|