Skip to content

Commit 3650b3e

Browse files
authored
Merge pull request #12 from PytLab/add-poscar-cartesian-coordinate
Make XsdFile compatible with file without lattice info
2 parents c0bd98c + 619abb5 commit 3650b3e

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

vaspy/matstudio.py

+61-56
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def load(self):
5959
self.tree = tree
6060

6161
# MS version info
62-
root = tree.getroot()
62+
root = self.root = tree.getroot()
6363
ms_version = root.get('Version')
6464
if ms_version:
6565
self.ms_version = ms_version
@@ -107,7 +107,9 @@ def __get_identity_mappings(self):
107107

108108
if not identity_mappings:
109109
msg = 'No IdentityMapping tag found.'
110-
raise ValueError(msg)
110+
self.__logger.warning(msg)
111+
return
112+
#raise ValueError(msg)
111113

112114
return identity_mappings
113115

@@ -125,58 +127,60 @@ def get_atom_info(self):
125127

126128
identity_mappings = self.__get_identity_mappings()
127129

128-
# For each IdentityMapping tag.
129-
for identity_mapping in identity_mappings:
130-
131-
# For each Atom3d tag in IdentityMapping.
132-
for elem in identity_mapping.iter('Atom3d'):
133-
# Atom name and number
134-
atom = elem.attrib['Components']
135-
atom_components.append(atom)
136-
if atom not in natoms_dict:
137-
natoms_dict.setdefault(atom, 1)
138-
atom_types.append(atom)
139-
else:
140-
natoms_dict[atom] += 1
141-
142-
# Coordinates
143-
# NOTE: In bulk the origin point may not have coordinate,
144-
# so use '0.0,0.0,0.0' as default value.
145-
if 'XYZ' not in elem.attrib:
146-
xyz = '0.0,0.0,0.0'
147-
msg = ("Found an Atom3d tag without 'XYZ' attribute" +
148-
", set to {}").format(xyz)
149-
self.__logger.info(msg)
150-
else:
151-
xyz = elem.attrib['XYZ']
152-
153-
coordinate = [float(i.strip()) for i in xyz.split(',')]
154-
if atom not in atomco_dict:
155-
atomco_dict.setdefault(atom, [coordinate])
156-
else:
157-
atomco_dict[atom].append(coordinate)
158-
159-
# T&F info
160-
if 'RestrictedProperties' in elem.attrib:
161-
tf_info = ['F', 'F', 'F']
162-
else:
163-
tf_info = ['T', 'T', 'T']
164-
if atom not in tf_dict:
165-
tf_dict.setdefault(atom, [tf_info])
166-
else:
167-
tf_dict[atom].append(tf_info)
168-
169-
# atom name
170-
atom_name = elem.attrib.get('Name')
171-
# Damn, sometimes tag has no Name attr,
172-
# so customize it
173-
# 有可能个别原子是从其他文件复制过来的原因
174-
if not atom_name:
175-
atom_name = atom + '_custom'
176-
if atom not in atom_name_dict:
177-
atom_name_dict.setdefault(atom, [atom_name])
178-
else:
179-
atom_name_dict[atom].append(atom_name)
130+
if identity_mappings is None:
131+
atom3d_iter = self.root.findall('.//Atom3d')
132+
else:
133+
atom3d_iter = identity_mappings[0].iter('Atom3d')
134+
135+
# For each Atom3d tag
136+
for elem in atom3d_iter:
137+
# Atom name and number
138+
atom = elem.attrib['Components']
139+
atom_components.append(atom)
140+
if atom not in natoms_dict:
141+
natoms_dict.setdefault(atom, 1)
142+
atom_types.append(atom)
143+
else:
144+
natoms_dict[atom] += 1
145+
146+
# Coordinates
147+
# NOTE: In bulk the origin point may not have coordinate,
148+
# so use '0.0,0.0,0.0' as default value.
149+
if 'XYZ' not in elem.attrib:
150+
xyz = '0.0,0.0,0.0'
151+
msg = ("Found an Atom3d tag without 'XYZ' attribute" +
152+
", set to {}").format(xyz)
153+
self.__logger.info(msg)
154+
else:
155+
xyz = elem.attrib['XYZ']
156+
157+
coordinate = [float(i.strip()) for i in xyz.split(',')]
158+
if atom not in atomco_dict:
159+
atomco_dict.setdefault(atom, [coordinate])
160+
else:
161+
atomco_dict[atom].append(coordinate)
162+
163+
# T&F info
164+
if 'RestrictedProperties' in elem.attrib:
165+
tf_info = ['F', 'F', 'F']
166+
else:
167+
tf_info = ['T', 'T', 'T']
168+
if atom not in tf_dict:
169+
tf_dict.setdefault(atom, [tf_info])
170+
else:
171+
tf_dict[atom].append(tf_info)
172+
173+
# atom name
174+
atom_name = elem.attrib.get('Name')
175+
# Damn, sometimes tag has no Name attr,
176+
# so customize it
177+
# 有可能个别原子是从其他文件复制过来的原因
178+
if not atom_name:
179+
atom_name = atom + '_custom'
180+
if atom not in atom_name_dict:
181+
atom_name_dict.setdefault(atom, [atom_name])
182+
else:
183+
atom_name_dict[atom].append(atom_name)
180184

181185
atom_numbers = [natoms_dict[atm] for atm in atom_types]
182186

@@ -204,11 +208,12 @@ def get_name_info(self):
204208
获取文件中能量,力等数据.
205209
"""
206210
# Get info string.
211+
info = None
207212
for elem in self.tree.iter("SymmetrySystem"):
208213
info = elem.attrib.get('Name')
209-
if info is None:
210-
return
211214
break
215+
if info is None:
216+
return
212217

213218
# Get thermo data.
214219
fieldnames = ["energy", "force", "magnetism", "path"]

0 commit comments

Comments
 (0)