diff --git a/mctools/generic/get_primitive.py b/mctools/generic/get_primitive.py index 543f74b..02ef6ab 100755 --- a/mctools/generic/get_primitive.py +++ b/mctools/generic/get_primitive.py @@ -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))) @@ -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