Statistiques
| Révision :

root / prepareQMX.py @ 9

Historique | Voir | Annoter | Télécharger (7,3 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 line.lower() == "turbomole":
35
        data.str_import = "from ase.io.turbomole import read_turbomole"
36
        data.str_class = "read_turbomole"
37
    elif line.lower() == "vasp":
38
        data.str_import = "from ase.io.vasp import read_vasp"
39
        data.str_class = "read_vasp"
40
        
41
    data.str_structure = getline(section, "structure", lines)
42
    return data
43

    
44
def analyzeCalculator(section, lines):
45
    data = Data(section)
46
    #read programs
47
    line = getline(section, "program", lines)
48
    if line.lower() == "turbomole":
49
        data.str_import = "from ase.calculators.turbomole import Turbomole"
50
        data.str_class = "Turbomole"
51
    elif line.lower() == "vasp":
52
        data.str_import = "from ase.calculators.vasp import Vasp"
53
        data.str_class = "Vasp"
54
    
55
    str = getline(section, "class", lines)
56
    if str is not None:
57
        data.str_class = str
58

    
59
    str = getline(section, "import", lines)
60
    if str is not None:
61
        data.str_import = str
62

    
63
    str = getline(section, "options", lines)
64
    if str is not None:
65
        data.str_option = str
66
    return data
67

    
68
def analyzeDynamics(section, lines):
69
    data = Data(section)
70
    line = getline(section, "method", lines)
71
    if line.lower() == "optimizer":
72
        data.str_import = "from ase.optimize import QuasiNewton"
73
        data.str_class = "QuasiNewton"
74
    return data
75

    
76
#--- setting default values ------------------------------------------------------------------------
77
#define separators
78
separator=":"
79
subseparator="."
80
lineseparator="\n"
81

    
82
#--- parse arguments -------------------------------------------------------------------------------
83
#parse arguments (python 2.6)
84
parser = OptionParser()
85
parser.add_option("-i", "--infile",  dest="inputFile",  help="specifies the input file", metavar="FILE", default="qmx.in")
86
parser.add_option("-o", "--outfile", dest="outputFile", help="specifies the output file", metavar="FILE")
87
parser.add_option("-f", action="store_true", dest="overwrite")
88
(options, args) = parser.parse_args()
89

    
90
file=open(options.inputFile, "r")
91
lines = file.readlines()
92
file.close()
93

    
94

    
95
#--- split intput file -----------------------------------------------------------------------------
96

    
97
#--- remove comments -------------------------------------------------------------------------------
98
for iline in xrange(len(lines)):
99
    lines[iline] = lines[iline].split("#")[0]    
100

    
101
#--- collect data in class -------------------------------------------------------------------------
102
data_collection=[]
103

    
104
#--- settings for QMX class ------------------------------------------------------------------------
105
data = Data("qmx")
106
data.str_class = "Qmx"
107
data.str_import = "from ase.calculators.qmx import Qmx"
108
data.str_option = "highLevel, lowLevel"
109

    
110
data_collection.append(data)
111

    
112
#--- settings for Embed class ----------------------------------------------------------------------
113
data = Data("embed")
114
data.str_class = "Embed"
115
data.str_import = "from ase.embed import Embed"
116
data.str_option = "system, cluster"
117
data.str_methods = ["embed()", "set_calculator(qmx)"]
118

    
119
data_collection.append(data)
120

    
121

    
122
#--- analyze the input file ------------------------------------------------------------------------
123
for section in "high-level", "low-level", "cluster", "system", "dynamics":
124
    #search lines for section
125
    lines_section=[]
126
    for line in lines:
127
        line = line.split(separator)
128
        if line[0].find(section) >= 0:
129
            line[0] = line[0].replace(section+subseparator, "")
130
            lines_section.append(line)
131
    
132
    #calculators
133
    if section == "high-level" or section == "low-level":
134
        data_collection.append(analyzeCalculator(section, lines_section))
135
        
136
    #systems
137
    if section == "cluster" or section == "system":
138
        data_collection.append(analyzeAtoms(section, lines_section))
139
        
140
    #dynamics
141
    if section == "dynamics":
142
        data_collection.append(analyzeDynamics(section, lines_section))
143

    
144
#--- write output file -----------------------------------------------------------------------------
145
if options.outputFile is not None and os.path.exists(options.outputFile) and not options.overwrite:
146
    sys.stderr.write(lineseparator+"the output file <"+options.outputFile+"> already exists"+lineseparator)
147
    sys.exit(1)
148

    
149
output = None
150
if options.outputFile is None:
151
    output=sys.stdout
152
else:
153
    output=open(options.outputFile, "w")
154

    
155
#--- write header ----------------------------------------------------------------------------------
156
output.write("#!/usr/bin/env python\n")
157

    
158
#--- write import lines ----------------------------------------------------------------------------
159
xlines=[]
160
for data in data_collection:
161
    value = data.str_import
162
    if value is None or value in xlines:
163
        continue
164
    xlines.append(value)
165
    output.write(value+lineseparator)
166
output.write(lineseparator)
167

    
168
#--- define the methods ----------------------------------------------------------------------------
169
for system in 'high', 'low':
170
    for data in data_collection:
171
        if data.__name__ == system+"-level":
172
            output.write(system+"Level = "+data.str_class+"("+data.str_option+")"+lineseparator)
173
output.write(lineseparator)
174

    
175
#--- qmx class (substraction scheme ----------------------------------------------------------------
176
for data in data_collection:
177
    if data.__name__ == "qmx":
178
        output.write("qmx = "+data.str_class+"("+data.str_option+")"+lineseparator)
179
output.write(lineseparator)
180

    
181
#--- read all the systems --------------------------------------------------------------------------
182
for system in "cluster", "system":
183
    for data in data_collection:
184
        if data.__name__ == system:
185
            if data.str_structure is None:
186
                continue
187
            output.write(system+" = "+data.str_class+"(\""+data.str_structure+"\")"+lineseparator)
188
output.write(lineseparator)
189

    
190
#--- embeding class --------------------------------------------------------------------------------
191
for data in data_collection:
192
    if data.__name__ == "embed":
193
        output.write("embed = "+data.str_class+"("+data.str_option+")"+lineseparator)
194
        for method in data.str_methods:
195
            output.write("embed."+method+lineseparator)
196
output.write(lineseparator)
197

    
198
#--- dynamics class --------------------------------------------------------------------------------
199
for data in data_collection:
200
    if data.__name__ == "dynamics":
201
        output.write("dyn="+data.str_class+"(embed)"+lineseparator)
202
        output.write("dyn.run("+data.str_option+")"+lineseparator)
203

    
204
output.close()
205
sys.exit(0)