Debian Codegraph @

goodvibes/ase_helper.py

raw
1 """Optional helper for emitting GoodVibes ASE thermo extxyz files from an
2 ASE-driven calculation.
3
4 ASE is *not* a runtime dependency of GoodVibes. This module imports it lazily
5 so the rest of the package works without ASE installed; only direct callers
6 of ``write_thermo_extxyz`` need it. See tests/ase/README.md for the format spec.
7 """
8 from __future__ import annotations
9
10
11 def write_thermo_extxyz(
12 path,
13 atoms,
14 energy,
15 frequencies=None,
16 charge=0,
17 multiplicity=1,
18 level_of_theory=None,
19 solvation_model='gas phase',
20 empirical_dispersion='',
21 point_group=None,
22 symmno=None,
23 linear_mol=None,
24 zpe=None,
25 job_type=None,
26 energy_units='Hartree',
27 ):
28 """
29 Write a GoodVibes-compatible ASE thermo `.extxyz` file for the supplied geometry and metadata.
30
31 Parameters:
32 path (str): Output file path (typically ending with `.extxyz`).
33 atoms (ase.Atoms): Geometry; element symbols and Cartesian positions are written.
34 energy (float): Electronic (SCF) energy.
35 frequencies (iterable[float], optional): Vibrational frequencies in cm^-1; negative values indicate imaginary modes.
36 charge (int, optional): Total molecular charge. Default: 0.
37 multiplicity (int, optional): Spin multiplicity. Default: 1.
38 level_of_theory (str, optional): Free-form method/basis description (e.g., "B3LYP/6-31G*") used for GoodVibes scaling lookup.
39 solvation_model (str or None, optional): Description of solvation model (defaults to 'gas phase'); set to None to omit.
40 empirical_dispersion (str, optional): Empirical dispersion tag (included when non-empty).
41 point_group (str, optional): Molecular point group symbol.
42 symmno (int, optional): Symmetry number.
43 linear_mol (bool, optional): Whether the molecule is linear.
44 zpe (float, optional): Zero-point energy in the same units as `energy`.
45 job_type (str, optional): Job type hint (e.g., 'Freq', 'GSFreq', 'TS', 'SP'); parser may infer if omitted.
46 energy_units (str, optional): Units for `energy` (default: 'Hartree').
47
48 Raises:
49 ImportError: If ASE is not installed (suggests installing via `pip install ase`).
50 """
51 try:
52 import ase # noqa: F401
53 from ase.io import write
54 except ImportError as e:
55 raise ImportError(
56 "ASE is required to write extxyz fixtures. Install with: pip install ase"
57 ) from e
58
59 info = dict(atoms.info)
60 info['program'] = 'ase'
61 info['ase_version'] = ase.__version__
62 info['scf_energy'] = float(energy)
63 info['scf_energy_units'] = energy_units
64 info['charge'] = int(charge)
65 info['multiplicity'] = int(multiplicity)
66 if frequencies is not None:
67 info['frequencies'] = ' '.join(f'{float(f):.6f}' for f in frequencies)
68 info['frequencies_units'] = 'cm-1'
69 if level_of_theory is not None:
70 info['level_of_theory'] = str(level_of_theory)
71 if solvation_model is not None:
72 info['solvation_model'] = str(solvation_model)
73 if empirical_dispersion:
74 info['empirical_dispersion'] = str(empirical_dispersion)
75 if point_group is not None:
76 info['point_group'] = str(point_group)
77 if symmno is not None:
78 info['symmno'] = int(symmno)
79 if linear_mol is not None:
80 info['linear_mol'] = bool(linear_mol)
81 if zpe is not None:
82 info['zpe'] = float(zpe)
83 if job_type is not None:
84 info['job_type'] = str(job_type)
85
86 atoms = atoms.copy()
87 atoms.info = info
88 write(path, atoms, format='extxyz')