Coverage for /builds/kinetik161/ase/ase/io/vasp_parsers/incar_writer.py: 100.00%
30 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-12-10 11:04 +0000
1from collections.abc import Iterable
4def write_incar(directory, parameters, header=None):
5 incar_string = generate_incar_lines(parameters)
6 with open(f"{directory}/INCAR", "w") as incar:
7 if header is not None:
8 incar.write(header + "\n")
9 incar.write(incar_string)
12def generate_incar_lines(parameters):
13 if isinstance(parameters, str):
14 return parameters
15 elif parameters is None:
16 return ""
17 else:
18 incar_lines = []
19 for item in parameters.items():
20 incar_lines += list(generate_line(*item))
21 # Adding a newline at the end of the file
22 return "\n".join(incar_lines) + "\n"
25def generate_line(key, value, num_spaces=1):
26 indent = " " * num_spaces
27 if isinstance(value, str):
28 if value.find("\n") != -1:
29 value = '"' + value + '"'
30 yield indent + f"{key.upper()} = {value}"
31 elif isinstance(value, dict):
32 yield indent + f"{key.upper()} {{"
33 for item in value.items():
34 yield from generate_line(*item, num_spaces + 4)
35 yield indent + "}"
36 elif isinstance(value, Iterable):
37 yield indent + f"{key.upper()} = {' '.join(str(x) for x in value)}"
38 else:
39 yield indent + f"{key.upper()} = {value}"