Ich habe eine g.out
Datei (unten eingefügt).Slice Linien und speichern Sie Parameter in verschiedenen Dateien
Diese Datei besteht aus mehreren FINAL OPTIMIZED
Geometrien, die ich extrahieren möchte.
Für eine gegebene FINAL OPTIMIZED GEOMETRY
diese markierten Werte sind diejenigen, die ich möchte extrahieren:
Ich habe in das unten stehende Programm verwaltet extrahieren die ersten drei: VOLUME
und A
und B
:
Mein Code:
import os
import sys
import re
initial_pattern = '^ FINAL OPTIMIZED GEOMETRY - DIMENSIONALITY OF THE SYSTEM 3$'
middle_pattern = '^ CRYSTALLOGRAPHIC CELL '
end_pattern = '^ T = ATOM BELONGING TO THE ASYMMETRIC UNIT$'
VOLUMES = []
P0 = []
P2 = []
atomic_number = []
coord_x = []
coord_y = []
coord_z = []
with open('g.out') as file:
for line in file:
if re.match(initial_pattern, line):
print file.next()
print file.next()
print file.next()
volume_line = file.next()
print volume_line
aux = volume_line.split()
each_volume = aux[7]
print each_volume
VOLUMES.append(each_volume)
if re.match(middle_pattern, line):
print line
print file.next()
parameters_line = file.next()
aux = parameters_line.split()
p0 = aux[0]
p1 = aux[1]
p2 = aux[2]
p3 = aux[3]
p4 = aux[4]
p5 = aux[5] #
print p0
print p2
P0.append(p0)
P2.append(p2)
print file.next()
print file.next()
print file.next()
print file.next()
first_coord_line = file.next()
print first_coord_line
if re.match(end_pattern, line):
end_pattern = line
print end_pattern
all_coordinates = [first_coord_line:end_pattern]
for line in all_coordinates:
del('F ') # delete those that contain 'F '
aux2 = line.split()
coords = []
sys.exit()
#Template =
"""
some stuff
other stuff
p0 p2
3
A B C D
E F G H
I J K L
other stuff
some other stuff
"""
Ich bin nicht in der Lage die COORDINATES
, zu extrahieren, weil ich nicht den Weg first_coord_line
-end_pattern
, wie in diesem Pseudo-Code zu schneiden Zeilen finden:
if re.match(end_pattern, line):
end_pattern = line
print end_pattern
all_coordinates = [first_coord_line:end_pattern]
for line in all_coordinates:
del('F ') # delete those that contain 'F '
aux2 = line.split() # split lines
atomic_number = aux2[2]
coord_x = aux2[4]
coord_y = aux2[5]
coord_z = aux2[6]
Gibt es eine Möglichkeit, diesen Pseudo-Code zu erreichen?
In meinem Code, VOLUMES
, P0
, P2
, atomic_number
, coord_x
, coord_y
coord_z
werden mit Listen, weil initialisiert, bevor die for-Schleife endet Ich möchte in verschiedenen Dateien mit dem Namen des "VOLUME
.inp" genannt speichern diese Informationen:
#Template =
"""
some stuff
other stuff
p0 p2
3
A B C D
E F G H
I J K L
other stuff
some other stuff
"""
wo p0
und p2
die Werte in meinem Code extrahiert werden (2. und 3. Werte im Screenshot hervorgehoben) und A
-L
sind die 0.123.und coord_x
, coord_y
, coord_z
.
Gibt es einen Weg, dies zu erreichen?
Die g.out
Datei:
more lines
more lines
more lines
FINAL OPTIMIZED GEOMETRY - DIMENSIONALITY OF THE SYSTEM 3
(NON PERIODIC DIRECTION: LATTICE PARAMETER FORMALLY SET TO 500)
*******************************************************************************
LATTICE PARAMETERS (ANGSTROMS AND DEGREES) - BOHR = 0.5291772083 ANGSTROM
PRIMITIVE CELL - CENTRING CODE 7/0 VOLUME= 119.823364 - DENSITY 2.770 g/cm^3
A B C ALPHA BETA GAMMA
6.28373604 6.28373604 6.28373604 46.646397 46.646397 46.646397
*******************************************************************************
ATOMS IN THE ASYMMETRIC UNIT 3 - ATOMS IN THE UNIT CELL: 10
ATOM X/A Y/B Z/C
*******************************************************************************
1 T 20 CA 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
2 F 20 CA -5.000000000000E-01 -5.000000000000E-01 -5.000000000000E-01
3 T 6 C 2.500000000000E-01 2.500000000000E-01 2.500000000000E-01
4 F 6 C -2.500000000000E-01 -2.500000000000E-01 -2.500000000000E-01
5 T 8 O -4.924094276183E-01 -7.590572381674E-03 2.500000000000E-01
6 F 8 O 2.500000000000E-01 -4.924094276183E-01 -7.590572381674E-03
7 F 8 O -7.590572381674E-03 2.500000000000E-01 -4.924094276183E-01
8 F 8 O 4.924094276183E-01 7.590572381674E-03 -2.500000000000E-01
9 F 8 O -2.500000000000E-01 4.924094276183E-01 7.590572381674E-03
10 F 8 O 7.590572381674E-03 -2.500000000000E-01 4.924094276183E-01
TRANSFORMATION MATRIX PRIMITIVE-CRYSTALLOGRAPHIC CELL
1.0000 0.0000 1.0000 -1.0000 1.0000 1.0000 0.0000 -1.0000 1.0000
*******************************************************************************
CRYSTALLOGRAPHIC CELL (VOLUME= 359.47009054)
A B C ALPHA BETA GAMMA
4.97568007 4.97568007 16.76591397 90.000000 90.000000 120.000000
COORDINATES IN THE CRYSTALLOGRAPHIC CELL
ATOM X/A Y/B Z/C
*******************************************************************************
1 T 20 CA 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
2 F 20 CA -5.491739570355E-17 -2.745869785177E-17 -5.000000000000E-01
3 T 6 C 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02
4 F 6 C -3.333333333333E-01 3.333333333333E-01 8.333333333333E-02
5 T 8 O -4.090760942850E-01 -3.333333333333E-01 -8.333333333333E-02
6 F 8 O 3.333333333333E-01 -7.574276095166E-02 -8.333333333333E-02
7 F 8 O 7.574276095166E-02 4.090760942850E-01 -8.333333333333E-02
8 F 8 O 4.090760942850E-01 3.333333333333E-01 8.333333333333E-02
9 F 8 O -3.333333333333E-01 7.574276095166E-02 8.333333333333E-02
10 F 8 O -7.574276095166E-02 -4.090760942850E-01 8.333333333333E-02
T = ATOM BELONGING TO THE ASYMMETRIC UNIT
INFORMATION **** fort.34 **** GEOMETRY OUTPUT FILE
more lines
more lines
more lines
FINAL OPTIMIZED GEOMETRY - DIMENSIONALITY OF THE SYSTEM 3
(NON PERIODIC DIRECTION: LATTICE PARAMETER FORMALLY SET TO 500)
*******************************************************************************
LATTICE PARAMETERS (ANGSTROMS AND DEGREES) - BOHR = 0.5291772083 ANGSTROM
PRIMITIVE CELL - CENTRING CODE 7/0 VOLUME= 121.143469 - DENSITY 2.740 g/cm^3
A B C ALPHA BETA GAMMA
6.32229536 6.32229536 6.32229536 46.436583 46.436583 46.436583
*******************************************************************************
ATOMS IN THE ASYMMETRIC UNIT 3 - ATOMS IN THE UNIT CELL: 10
ATOM X/A Y/B Z/C
*******************************************************************************
1 T 20 CA 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
2 F 20 CA 5.000000000000E-01 -5.000000000000E-01 -5.000000000000E-01
3 T 6 C 2.500000000000E-01 2.500000000000E-01 2.500000000000E-01
4 F 6 C -2.500000000000E-01 -2.500000000000E-01 -2.500000000000E-01
5 T 8 O -4.927088991116E-01 -7.291100888437E-03 2.500000000000E-01
6 F 8 O 2.500000000000E-01 -4.927088991116E-01 -7.291100888437E-03
7 F 8 O -7.291100888437E-03 2.500000000000E-01 -4.927088991116E-01
8 F 8 O 4.927088991116E-01 7.291100888437E-03 -2.500000000000E-01
9 F 8 O -2.500000000000E-01 4.927088991116E-01 7.291100888437E-03
10 F 8 O 7.291100888437E-03 -2.500000000000E-01 4.927088991116E-01
TRANSFORMATION MATRIX PRIMITIVE-CRYSTALLOGRAPHIC CELL
1.0000 0.0000 1.0000 -1.0000 1.0000 1.0000 0.0000 -1.0000 1.0000
*******************************************************************************
CRYSTALLOGRAPHIC CELL (VOLUME= 363.43040599)
A B C ALPHA BETA GAMMA
4.98494429 4.98494429 16.88768068 90.000000 90.000000 120.000000
COORDINATES IN THE CRYSTALLOGRAPHIC CELL
ATOM X/A Y/B Z/C
*******************************************************************************
1 T 20 CA 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
2 F 20 CA -5.471726358381E-17 -2.735863179191E-17 -5.000000000000E-01
3 T 6 C 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02
4 F 6 C -3.333333333333E-01 3.333333333333E-01 8.333333333333E-02
5 T 8 O -4.093755657782E-01 -3.333333333333E-01 -8.333333333333E-02
6 F 8 O 3.333333333333E-01 -7.604223244490E-02 -8.333333333333E-02
7 F 8 O 7.604223244490E-02 4.093755657782E-01 -8.333333333333E-02
8 F 8 O 4.093755657782E-01 3.333333333333E-01 8.333333333333E-02
9 F 8 O -3.333333333333E-01 7.604223244490E-02 8.333333333333E-02
10 F 8 O -7.604223244490E-02 -4.093755657782E-01 8.333333333333E-02
T = ATOM BELONGING TO THE ASYMMETRIC UNIT
INFORMATION **** fort.34 **** GEOMETRY OUTPUT FILE
more lines
more lines
more lines
Aktualisiert Code:
Basierend auf @nos Flagge Ansatz, der folgende Code ist in der Lage, die Informationen zu extrahieren. VOLUMES
ist eine Liste von 2 Elementen. Die folgenden Listen sind das Ergebnis:
VOLUMES = ['119.823364', '121.143469']
P0 = ['4.97568007', '4.98494429']
P2 = ['16.76591397', '16.88768068']
Xs = ['0.000000000000E+00', '3.333333333333E-01', '-4.090760942850E-01', '0.000000000000E+00', '3.333333333333E-01', '-4.093755657782E-01']
Ys = ['0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01', '0.000000000000E+00', '-3.333333333333E-01', '-3.333333333333E-01']
Zs = ['0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02', '0.000000000000E+00', '-8.333333333333E-02', '-8.333333333333E-02']
ATOMIC_NUMBERS = ['20', '6', '8', '20', '6', '8']
Der zweite Teil dieses Beitrags, diese Informationen zu schreiben war (P0
, P2
, ATOMIC_NUMBERS
, Xs
, Ys
, Zs
) in den beiden VOLUME.inp
Dateien.Mit anderen Worten, so etwas wie:
V_119.823364.inp
Datei:
some stuff
other stuff
4.97568007 4.98494429
3
20 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
6 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02
8 -4.090760942850E-01 -3.333333333333E-01 -8.333333333333E-02
other stuff
V_121.143469.inp
Datei:
some stuff
other stuff
4.97568007 4.98494429
3
20 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00
6 3.333333333333E-01 -3.333333333333E-01 -8.333333333333E-02
8 -4.093755657782E-01 -3.333333333333E-01 -8.333333333333E-02
other stuff
Basierend auf @ die nos atoms_per_frame
und atoms_all_frames
Vorschlag, ich habe den folgenden Code versucht. Ich finde Schwierigkeiten beim elementweisen Schreiben in die Dateien, d. H.
import os
import sys
import re
import glob
initial_pattern = '^ FINAL OPTIMIZED GEOMETRY - DIMENSIONALITY OF THE SYSTEM 3$'
middle_pattern = '^ CRYSTALLOGRAPHIC CELL '
end_pattern = '^ T = ATOM BELONGING TO THE ASYMMETRIC UNIT$'
global N_atom_irreducible_unit
N_atom_irreducible_unit = 3
VOLUMES = []
P0 = []
P2 = []
ATOMIC_NUMBERS = []
Xs = []
Ys = []
Zs = []
with open('g.out') as file:
passed_mid_point = False
for line in file:
if re.match(initial_pattern, line):
print file.next()
print file.next()
print file.next()
volume_line = file.next()
print volume_line
aux = volume_line.split()
each_volume = aux[7]
print each_volume
VOLUMES.append(each_volume)
if re.match(middle_pattern, line):
print line
print file.next()
parameters_line = file.next()
aux = parameters_line.split()
p0 = aux[0]
p1 = aux[1]
p2 = aux[2]
p3 = aux[3]
p4 = aux[4]
p5 = aux[5] #
print p0
print p2
P0.append(p0)
P2.append(p2)
print file.next()
print file.next()
print file.next()
print file.next()
if re.match(middle_pattern, line):
passed_mid_point = True
print 'line = ', line
if re.match(end_pattern, line):
passed_mid_point = False
elif passed_mid_point:
# parse the coordinates
print 'line2 =', line
terms = line.split()
print 'terms =', terms
if terms and terms[1] == 'T':
print terms[1]
atomic_number = terms[2]
print 'atomic_number = ', atomic_number
ATOMIC_NUMBERS.append(atomic_number)
x = terms[4]
print 'x =', x
Xs.append(x)
y = terms[5]
print 'y = ', y
Ys.append(y)
z = terms[6]
print 'z = ', z
Zs.append(z)
print 'VOLUMES = ', VOLUMES
print 'P0 = ', P0
print 'P2 = ', P2
print 'Xs = ', Xs
print 'Ys = ', Ys
print 'Zs = ', Zs
print 'ATOMIC_NUMBERS = ', ATOMIC_NUMBERS
# create the empty list of lists:
atoms_all_frames = [[] for _ in xrange(len(VOLUMES))]
print atoms_all_frames
for index_vol in range(len(VOLUMES)):
for index in range(len(ATOMIC_NUMBERS)):
atoms_per_frame = [ATOMIC_NUMBERS[index], Xs[index], Ys[index], Zs[index]]
atoms_all_frames[index_vol].append(atoms_per_frame)
# "atoms_all_frames" would be an appropriate list for looping
print atoms_all_frames
# Remove any existing V*.inp files, to clean first:
for f in glob.glob("V*.inp"):
os.remove(f)
# create the files:
for V in VOLUMES:
filename = "V_{}.d12".format(V)
print filename
# open them:
with open(filename,"a") as f:
# the following is a pseudo-code, because I cannot manage to
# find the way to write element-wise each string to the files:
for p0, p2, atoms_all_frames:
f.write("""some stuff
other stuff
%s %s
%s
%s %s %s %s
%s %s %s %s
%s %s %s %s
other stuff
some other stuff\n""" % p0 % p2 %N_atom_irreducible_unit %atoms_all_frames)
Zu viel Code und Text ... –
Ich vermute, Sie analysieren einige Ergebnisse pro (Zeit) Rahmen, und für jeden Rahmen gibt es Volumen und möglicherweise mehrere Atome mit ihren Koordinaten. In diesem Fall erstellen Sie zunächst eine Liste (sagen Sie 'atoms_all_frames = []'), um alle Atome zu speichern. Erstellen Sie dann beim Analysieren der Datei eine Liste der Atomkoordinaten (sagen Sie 'atoms_per_frame = []') für jeden Rahmen und fügen Sie die (x, y, z) -Koordinaten für jedes Atom an. Fügen Sie dann atoms_per_frame in atoms_all_frames ein. Auf diese Weise haben Ihre Volumenliste und Koordinatenliste die gleiche Größe, die die Anzahl der Rahmen ist. – nos
@nos Danke für Ihren Vorschlag. Ich bin diesem Ansatz gefolgt, aber ich kann es nicht schaffen, elementweise in die Dateien zu schreiben. Bitte siehe aktualisierten Beitrag –