Coverage for ase/io/runner/writer.py: 95.83%
48 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""RuNNer input.data support
3Write files in RuNNer's input.data file format.
5Contains
6--------
7* `write_runnerdata`: Write structures to a RuNNer input.data file.
9Reference
10---------
11* [The online documentation of RuNNer 2.0](https://runner-suite.gitlab.io/runner2)
13Contributors
14------------
15* Maintainer and Author: [Alexander Knoll](mailto:alexander.knoll@rub.de)
16* Author: [Redouan El Haouari](mailto:redouan.elhaouari@rub.de)
17"""
19from typing import TextIO
21from ase.atoms import Atoms
22from ase.utils import writer
24from .runneratoms import RuNNerAtoms, Units
27def _format_atom_section(runneratoms: RuNNerAtoms, fmt: str) -> str:
28 """Format the per-structure block of lines containing atom information."""
29 lines = []
31 for i, position in enumerate(runneratoms.positions):
32 fields = [f'{j:{fmt}}' for j in position]
33 fields.append(f'{runneratoms.symbols[i]:2s}')
35 for name, length in runneratoms.atom_layout:
36 atom_arr = runneratoms.atom_arrays[name][i]
38 if length == 1:
39 fields.append(f'{atom_arr:{fmt}}')
40 else:
41 assert isinstance(atom_arr, list)
42 fields.extend(f'{v:{fmt}}' for v in atom_arr)
44 if runneratoms.forces is not None:
45 fields.extend(f'{f:{fmt}}' for f in runneratoms.forces[i])
47 lines.append('atom ' + ' '.join(fields))
49 return '\n'.join(lines) + '\n'
52def _compose_begin_line(atoms: RuNNerAtoms) -> str:
53 """Compose the field names in the begin line."""
54 begin_fields = ['begin', 'position(3)', 'element']
56 if atoms.atom_arrays is not None:
57 for name, length in atoms.atom_layout:
58 if length == 1:
59 begin_fields.append(name)
60 else:
61 begin_fields.append(f'{name}({length})')
63 if atoms.forces is not None:
64 begin_fields.append('forces(3)')
66 return ' '.join(begin_fields) + '\n'
69@writer
70def write_runnerdata(
71 outfile: TextIO,
72 images: list[Atoms],
73 comment: str = '',
74 fmt: str = '20.10f',
75 input_units: Units = Units.ASE,
76) -> None:
77 """Write series of ASE Atoms to a RuNNer input.data file.
79 For further details see the `read_runnerdata` routine.
81 Parameters
82 ----------
83 outfile: TextIOWrapper
84 Python fileobj with the target input.data file.
85 images: array-like
86 List of `Atoms` objects.
87 comment: str
88 A comment message to be added to each structure.
89 fmt: str
90 A format specifier for float values.
91 input_units: str
92 The given input units. Can be 'Units.ASE' or 'Units.ATOMIC'.
94 Raises
95 ------
96 ValueError: exception
97 Raised if the comment line contains newline characters.
98 """
99 # Preprocess the comment.
100 comment = comment.rstrip()
101 if '\n' in comment:
102 raise ValueError('Comment line cannot contain line breaks.')
104 for atoms in images:
105 runneratoms = RuNNerAtoms.from_ase_atoms(atoms, input_units=input_units)
106 runneratoms.convert(Units.ATOMIC)
108 outfile.write(_compose_begin_line(runneratoms))
110 if comment:
111 outfile.write(f'comment {comment}\n')
113 for vec in runneratoms.cell:
114 outfile.write(
115 f'lattice {vec[0]:{fmt}} {vec[1]:{fmt}} {vec[2]:{fmt}}\n'
116 )
118 outfile.write(_format_atom_section(runneratoms, fmt))
120 if runneratoms.energy is not None:
121 outfile.write(f'energy {runneratoms.energy:{fmt}}\n')
123 if runneratoms.total_charge is not None:
124 outfile.write(f'charge {runneratoms.total_charge:{fmt}}\n')
126 outfile.write('end\n')