Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance poscar_scale function (/data/gen.py)to detect Selective dynamics #1439

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions dpgen/data/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,24 @@
def poscar_scale(poscar_in, poscar_out, scale):
with open(poscar_in) as fin:
lines = list(fin)
if "D" == lines[7][0] or "d" == lines[7][0]:

# Determine if "Selective dynamics" is present
if lines[7].strip().lower().startswith("s"):
njzjz marked this conversation as resolved.
Show resolved Hide resolved
njzjz marked this conversation as resolved.
Show resolved Hide resolved
coord_type_line = 8 # If present, coordinates type is on line 9

Check warning on line 243 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L243

Added line #L243 was not covered by tests
else:
coord_type_line = 7 # If not, coordinates type is on line 8
njzjz marked this conversation as resolved.
Show resolved Hide resolved

# Process according to the coordinate type
if "D" == lines[coord_type_line][0] or "d" == lines[coord_type_line][0]:
lines = poscar_scale_direct(lines, scale)
elif "C" == lines[7][0] or "c" == lines[7][0]:
elif "C" == lines[coord_type_line][0] or "c" == lines[coord_type_line][0]:

Check warning on line 250 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L250

Added line #L250 was not covered by tests
lines = poscar_scale_cartesian(lines, scale)
Comment on lines +248 to 251

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use the str.startswith() method to check the starting character of a string in a case-insensitive manner, instead of checking equality with both the uppercase and lowercase versions of the character. [best practice]

Suggested change
if "D" == lines[coord_type_line][0] or "d" == lines[coord_type_line][0]:
lines = poscar_scale_direct(lines, scale)
elif "C" == lines[7][0] or "c" == lines[7][0]:
elif "C" == lines[coord_type_line][0] or "c" == lines[coord_type_line][0]:
lines = poscar_scale_cartesian(lines, scale)
if lines[coord_type_line].startswith(("D", "d")):
lines = poscar_scale_direct(lines, scale)
elif lines[coord_type_line].startswith(("C", "c")):
lines = poscar_scale_cartesian(lines, scale)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this comment

njzjz marked this conversation as resolved.
Show resolved Hide resolved
njzjz marked this conversation as resolved.
Show resolved Hide resolved
else:
raise RuntimeError("Unknow poscar style at line 7: %s" % lines[7])
raise RuntimeError(

Check warning on line 253 in dpgen/data/gen.py

View check run for this annotation

Codecov / codecov/patch

dpgen/data/gen.py#L253

Added line #L253 was not covered by tests
f"Unknown poscar style at line {coord_type_line + 1}: {lines[coord_type_line]}"
)
Comment on lines +253 to +255

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The error message in the RuntimeError could be more informative by specifying that the unknown style is related to the coordinate type. [enhancement]

Suggested change
raise RuntimeError(
f"Unknown poscar style at line {coord_type_line + 1}: {lines[coord_type_line]}"
)
raise RuntimeError(
f"Unknown coordinate type at line {coord_type_line + 1}: {lines[coord_type_line]}"
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, is it poscar style or coordinate type?


# Write scaled positions back to output file
with open(poscar_out, "w") as fout:
fout.write("".join(lines))

Expand Down
Loading