Skip to content

Commit

Permalink
Improve appearance of get_primitive output, add --precision param
Browse files Browse the repository at this point in the history
  • Loading branch information
ajjackson committed May 29, 2024
1 parent e82ea53 commit 77e50e9
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions mctools/generic/get_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def main():
action="store_true",
help="Print output to screen even when writing to file.",
)
parser.add_argument(
"-p",
"--precision",
type=int,
help=("Number of decimal places for float display. "
"(Output files are not affected)"),
default=6,
)
args = parser.parse_args()

get_primitive(**snake_case_args(vars(args)))
Expand All @@ -74,38 +82,50 @@ def get_primitive(input_file='POSCAR',
output_format=None,
threshold=1e-5,
angle_tolerance=-1.,
verbose=False):
verbose=False,
precision=6):

if output_file is None:
verbose = True

if verbose:

def vprint(*args):
for arg in args:
print(arg,)
print(arg, end="")
print("")

else:

def vprint(*args):
pass

float_format_str = f"{{:{precision+4}.{precision}f}}"

def format_float(x: float) -> str:
return float_format_str.format(x)

atoms = ase.io.read(input_file, format=input_format)
atoms_spglib = (atoms.cell.array, atoms.get_scaled_positions(), atoms.numbers)
atoms_spglib = (
atoms.cell.array,
atoms.get_scaled_positions(),
atoms.numbers,
)

vprint(
"# Space group: ",
str(
spglib.get_spacegroup(
atoms_spglib, symprec=threshold, angle_tolerance=angle_tolerance)),
'\n')
spacegroup = spglib.get_spacegroup(
atoms_spglib, symprec=threshold, angle_tolerance=angle_tolerance)
vprint(f"Space group: {spacegroup}")

cell, positions, atomic_numbers = spglib.find_primitive(
atoms_spglib, symprec=threshold, angle_tolerance=angle_tolerance)

vprint("Primitive cell vectors:")
vprint(cell, '\n')
for row in cell:
vprint(' '.join(map(format_float, row)))

vprint("Atomic positions and proton numbers:")
for position, number in zip(positions, atomic_numbers):
vprint(position, '\t', number)
vprint(' '.join(map(format_float, position)), '\t', number)

if output_file is None:
pass
Expand Down

0 comments on commit 77e50e9

Please sign in to comment.