Skip to content
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"):
Comment thread
njzjz marked this conversation as resolved.
Comment thread
njzjz marked this conversation as 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
Comment thread
njzjz marked this conversation as 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
Copy link
Copy Markdown

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
Copy Markdown
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

Comment thread
njzjz marked this conversation as resolved.
Comment thread
njzjz marked this conversation as 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
Copy link
Copy Markdown

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
Copy Markdown
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