Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions t3/pdep/hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ class QMEnergySettings:
Required and must not be blank: this is what lets Arkane apply
atom energy corrections so a QM'd TS's E0 lands on the same
enthalpy-of-formation scale as the RMG wells around it.
frequency_scale_factor (float, optional): Arkane's ``frequencyScaleFactor``. Omitted from
the header entirely when ``None``.
frequency_scale_factor (float, optional): Arkane's ``frequencyScaleFactor``.
``write_hybrid_network_input_file`` raises
``ValueError`` when it is ``None`` rather than
let Arkane fall back to unscaled frequencies.
use_hindered_rotors (bool, optional): Arkane's ``useHinderedRotors``. Default: ``True``.
use_bond_corrections (bool, optional): Arkane's ``useBondCorrections``. Default: ``False``.
Must not be ``True``: Arkane's input DSL has no
Expand Down Expand Up @@ -638,7 +640,8 @@ def write_hybrid_network_input_file(source_path: str,
ValueError: If ``energy_settings.model_chemistry`` is missing/blank or fails validation; if
``energy_settings.use_atom_corrections`` is ``False``; if
``energy_settings.use_atom_corrections`` is ``True`` and ``atom_energies`` is
``None``; if ``energy_settings.use_bond_corrections`` is ``True``; if
``None``; if ``energy_settings.frequency_scale_factor`` is ``None``; if
``energy_settings.use_bond_corrections`` is ``True``; if
``qm_transition_states`` is empty; if a key of ``qm_transition_states`` is not a
transition state label, or a key of ``qm_wells`` is not an isomer (species)
label, in the source network; if any conformer-data dict is malformed; or if the
Expand Down Expand Up @@ -1239,13 +1242,20 @@ def _build_energy_header(energy_settings: QMEnergySettings) -> str:

Returns:
str: The header text, terminated with a blank line.

Raises:
ValueError: If ``energy_settings.frequency_scale_factor`` is ``None``.
"""
if energy_settings.frequency_scale_factor is None:
raise ValueError(f"QMEnergySettings.frequency_scale_factor is None for the model chemistry "
f"{energy_settings.model_chemistry}: no frequency scale factor is known for this level, "
f"and omitting frequencyScaleFactor would make Arkane use unscaled frequencies. Provide "
f"the scale factor this level's frequencies were computed for.")
if _model_chemistry_ast_call(energy_settings.model_chemistry) is not None:
lines = [f'modelChemistry = {energy_settings.model_chemistry}']
else:
lines = [f'modelChemistry = "{energy_settings.model_chemistry}"']
if energy_settings.frequency_scale_factor is not None:
lines.append(f'frequencyScaleFactor = {energy_settings.frequency_scale_factor}')
lines.append(f'frequencyScaleFactor = {energy_settings.frequency_scale_factor}')
lines.append(f'useHinderedRotors = {energy_settings.use_hindered_rotors}')
lines.append(f'useBondCorrections = {energy_settings.use_bond_corrections}')
if energy_settings.use_bond_corrections:
Expand Down
126 changes: 116 additions & 10 deletions t3/pdep/pes_qm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
``t3.main`` does -- ARC is an allowed dependency; only RMG-Py and Arkane are not.
"""

import copy
import logging
import os
import re

from arc.common import save_yaml_file
from arc.exceptions import InputError
from arc.level import Level, assign_frequency_scale_factor
from arc.main import ARC
from arc.statmech.arkane import get_arkane_model_chemistry
Expand All @@ -55,6 +58,13 @@

_logger = logging.getLogger(__name__)

# A four-digit year suffix on a corrections-database method name, as ARC splits it.
_METHOD_YEAR_SUFFIX = re.compile(r'^(.*?)(\d{4})$')
# Every year suffix carried by a method inside ARC's LevelOfTheory(...) text.
_MODEL_CHEMISTRY_METHOD_YEAR = re.compile(r"method='[^']*?(\d{4})'")
# Sentinel: resolve the single-point level with whatever year its own spelling carries.
_AS_WRITTEN = object()

# The name ARC's own input YAML is written under, inside the round's ARC project directory --
# mirrors t3.main.T3.run_arc's 'ARC input' path, kept local here since this loop's ARC project
# directory is per-round rather than per-T3-project.
Expand Down Expand Up @@ -580,7 +590,8 @@ def arc_qm_runner(candidates: tuple, paths: RoundPaths, config: PESLoopConfig,
f"artifact it references, so combining these would render the adopted barriers "
f"against an energy reference they were never computed under.")
energy_settings_frozen = captured_settings if captured_settings is not None else adopted_settings
energy_settings = QMEnergySettings.from_frozen(energy_settings_frozen)
energy_settings = QMEnergySettings.from_frozen(
_emitted_energy_settings(energy_settings_frozen, config.qm.sp_level, config.qm.freq_level))
if capture_result is not None:
# I1: the hybrid network is built from the CAPTURE's own vendored network copy and
# recorded method (CaptureResult.networks is the authoritative source -- see
Expand Down Expand Up @@ -624,6 +635,43 @@ def arc_qm_runner(candidates: tuple, paths: RoundPaths, config: PESLoopConfig,
return converged_labels, queued_ts_labels


def _emitted_energy_settings(frozen: dict | None, sp_level: str, freq_level: str | None) -> dict | None:
"""
The frozen energy settings a hybrid network is written under, labelled with this run's level.

A hybrid network carries one ``modelChemistry``, and it is this run's own resolved level, never
the spelling a capture froze: one raw level under two corrections-database spellings (e.g.
``wb97xd`` and ``wb97xd2023``) makes Arkane apply two different sets of fitted corrections.
Every number the capture froze is kept. A capture that froze no frequency scale factor gets the
one ARC assigns to this run's frequency level; if ARC has none, the factor stays ``None`` and
``write_hybrid_network_input_file`` refuses the network.

Args:
frozen (dict, optional): The frozen ``energy_settings`` block the network is built on.
sp_level (str): This run's single-point level, e.g. ``config.qm.sp_level``.
freq_level (str, optional): This run's frequency level, e.g. ``config.qm.freq_level``.

Returns:
dict, optional: A copy of ``frozen`` with the run's ``model_chemistry``, or ``None`` if
``frozen`` is ``None``.

Raises:
ValueError: If ARC cannot resolve this run's level into a model chemistry.
"""
if frozen is None:
return None
model_chemistry = _normalized_model_chemistry(sp_level, freq_level)
if model_chemistry is None:
raise ValueError(f'Refusing to write a hybrid network: ARC cannot resolve this run\'s level '
f'of theory {sp_level!r} (frequencies at {freq_level or sp_level!r}) into '
f'a model chemistry, so the network has no level to be written under.')
emitted = dict(frozen, model_chemistry=model_chemistry)
if emitted.get('frequency_scale_factor') is None:
emitted['frequency_scale_factor'] = assign_frequency_scale_factor(
Level(repr=freq_level or sp_level))
return emitted


def _normalized_model_chemistry(sp_level: str, freq_level: str | None = None) -> str | None:
"""
Normalize a T3 ``sp_level``/``freq_level`` pair into ARC's own ``model_chemistry`` text.
Expand Down Expand Up @@ -676,7 +724,58 @@ def _normalized_model_chemistry(sp_level: str, freq_level: str | None = None) ->
return normalized[0] if normalized else None


def _normalized_model_chemistries(sp_level: str, freq_level: str | None = None) -> tuple[str, ...]:
def _level_with_year(level: Level, year: int | None) -> Level:
"""
A copy of ``level`` whose method carries no year suffix and whose ``year`` is ``year``.

Args:
level (Level): The level to copy.
year (int, optional): The year ARC should match, or ``None`` for the no-year entry.

Returns:
Level: The copy.
"""
variant = copy.deepcopy(level)
match = _METHOD_YEAR_SUFFIX.match(variant.method)
if match is not None:
variant.method = match.group(1)
variant.year = year
return variant


def _admissible_model_chemistries(sp_level: str, freq_level: str | None,
model_chemistry: str | None) -> tuple[str, ...]:
"""
Every ``model_chemistry`` spelling under which a capture's raw output may be reused for this
``sp_level``/``freq_level`` request.

ARC can resolve one request to corrections-database keys that differ only by a four-digit year
suffix on the method (``wb97xd`` and ``wb97xd2023``): the same functional and basis, with
different fitted corrections. A capture holds raw QM output, so its spelling's year does not
make it a different level. Beside the request as written, this accepts what ARC resolves for
the same request with the single-point year removed, and pinned to each year the capture's
``model_chemistry`` carries. Only the name comparison widens; whether the frozen numbers agree
stays the energy-reference check in ``arc_qm_runner``.

Args:
sp_level (str): The requested single-point level.
freq_level (str, optional): The requested frequency level, defaulting to ``sp_level``.
model_chemistry (str, optional): The capture's frozen ``model_chemistry`` text.

Returns:
tuple[str, ...]: The admissible spellings, the request's own first.
"""
spellings = list(_normalized_model_chemistries(sp_level, freq_level))
years = sorted({int(year) for year in _MODEL_CHEMISTRY_METHOD_YEAR.findall(model_chemistry or '')})
for year in [None] + years:
for spelling in _normalized_model_chemistries(sp_level, freq_level, sp_year=year):
if spelling not in spellings:
spellings.append(spelling)
return tuple(spellings)


def _normalized_model_chemistries(sp_level: str, freq_level: str | None = None,
sp_year=_AS_WRITTEN) -> tuple[str, ...]:
"""
Every ``model_chemistry`` spelling a capture made at this ``sp_level``/``freq_level`` pair may
legitimately carry.
Expand All @@ -700,12 +799,17 @@ def _normalized_model_chemistries(sp_level: str, freq_level: str | None = None)
Args:
sp_level (str): The single-point level-of-theory string.
freq_level (str, optional): The frequency level, defaulting to ``sp_level``.
sp_year (int, optional): A year to pin the single-point method to (``None`` for the no-year
entry), replacing any year its spelling carries. Left unset, the
level resolves as written.

Returns:
tuple[str, ...]: The accepted spellings, most-likely first; empty if ARC could not resolve
any.
"""
level = Level(repr=sp_level)
if sp_year is not _AS_WRITTEN:
level = _level_with_year(level, sp_year)
frequency_level = Level(repr=freq_level) if freq_level else level
# Mirrors ARC.check_freq_scaling_factor: the factor comes from the FREQUENCY level, and is
# never left as None once a frequency level exists.
Expand All @@ -717,14 +821,14 @@ def _normalized_model_chemistries(sp_level: str, freq_level: str | None = None)
try:
resolved = get_arkane_model_chemistry(level, freq_level=frequency_level,
freq_scale_factor=freq_scale_factor)
except ValueError as e:
except (ValueError, InputError) as e:
_logger.debug(f'PES loop: ARC could not normalize the level of theory {sp_level!r} '
f'(frequencies at {freq_level or sp_level!r}) with '
f'freq_scale_factor={freq_scale_factor!r}: {e}')
continue
if resolved is not None and resolved not in spellings:
spellings.append(resolved)
if not spellings:
if not spellings and sp_year is _AS_WRITTEN:
_logger.warning(f'PES loop: ARC could not normalize the level of theory {sp_level!r} '
f'(frequencies at {freq_level or sp_level!r}) into a model_chemistry; '
f'treating it as unresolvable.')
Expand All @@ -748,11 +852,12 @@ def adopt_prior_qm(from_t3_projects: list, network_id: str, level_of_theory: str
(the same re-hashing, tamper/torn-capture detection a live consumer gets -- see
``t3.main``'s own use of it). An entry is adopted only when ALL of the following hold:

- the capture's recorded ``energy_settings['model_chemistry']``, once normalized through
``_normalized_model_chemistry``, matches the requested ``level_of_theory``: mixing levels
- the capture's recorded ``energy_settings['model_chemistry']`` is one of the spellings
``_admissible_model_chemistries`` gives for the requested ``level_of_theory``: mixing levels
of theory inside one barrier's rate makes it inconsistent, so a prior result computed at a
different level is refused even though it exists and converged (never adopted "close
enough"). A refusal is logged with the manifest path, its raw ``model_chemistry``, and the
enough"). Spellings that differ only by a method's year suffix name one raw level and are
admitted. A refusal is logged with the manifest path, its raw ``model_chemistry``, and the
requested level, so a silent "nothing was reused" is always explainable.
- ``record.network_id`` is NEVER a gate -- it is logged only. Arkane names TS artifacts
positionally (``'TS{i+1}'``, see ``arkane/pdep.py``), so a pruned or reordered exploration
Expand Down Expand Up @@ -855,12 +960,13 @@ def adopt_prior_qm(from_t3_projects: list, network_id: str, level_of_theory: str
continue
energy_settings = verified.energy_settings or {}
manifest_model_chemistry = energy_settings.get('model_chemistry')
if manifest_model_chemistry not in normalized_requested:
admissible = _admissible_model_chemistries(level_of_theory, freq_level,
manifest_model_chemistry)
if manifest_model_chemistry not in admissible:
_logger.info(
f"PES loop: refusing prior QM at {manifest_path!r} -- its model_chemistry "
f"{manifest_model_chemistry!r} does not match the requested level of theory "
f"{level_of_theory!r} (accepted spellings: "
f"{list(normalized_requested)}).")
f"{level_of_theory!r} (accepted spellings: {list(admissible)}).")
continue
# The capture's own VENDORED network copy is the authoritative structural record of
# what each of its transition states connects (verified.networks -- see
Expand Down
1 change: 1 addition & 0 deletions tests/test_pdep/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def _write_energy_settings_fixture(arc_dir: str, statmech_subdir: str = 'kinetic
with open(input_py_path, 'w') as f:
f.write(
"modelChemistry = 'CBS-QB3'\n\n"
"frequencyScaleFactor = 0.99\n\n"
"useHinderedRotors = True\n\n"
"useAtomCorrections = True\n\n"
"atomEnergies = {'C': -37.844411, 'H': -0.499818, 'N': -54.581501, 'O': -75.062219}\n\n"
Expand Down
24 changes: 22 additions & 2 deletions tests/test_pdep/test_hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
# for gfn2/torchani model chemistries), so this fixture must carry values, not omit them.
DEFAULT_ENERGY_SETTINGS = QMEnergySettings(
model_chemistry='wb97xd/def2tzvp',
frequency_scale_factor=0.988,
atom_energies={
'Br': -2574.174533595486,
'C': -37.84706210301937,
Expand Down Expand Up @@ -285,7 +286,8 @@ def test_accepts_and_renders_bare_level_of_theory_model_chemistry(tmp_path, mode
is accepted (it legitimately contains quote characters that _validate_no_injection_chars alone
would reject) and rendered bare (unquoted), not as a quoted string containing that syntax."""
dest_path = str(tmp_path / 'input.py')
energy_settings = QMEnergySettings(model_chemistry=model_chemistry_expr, atom_energies={'C': -37.79, 'H': -0.5})
energy_settings = QMEnergySettings(model_chemistry=model_chemistry_expr, frequency_scale_factor=0.988,
atom_energies={'C': -37.79, 'H': -0.5})
write_hybrid_network_input_file(
source_path=NETWORK_FIXTURE,
dest_path=dest_path,
Expand Down Expand Up @@ -412,11 +414,28 @@ def test_qm_reaction_drops_kinetics_and_adds_tunneling(tmp_path):
assert 'tunneling' not in reaction2_block


def test_a_missing_frequency_scale_factor_is_refused_not_omitted(tmp_path):
"""A level with no frequency scale factor raises, naming the level, and writes nothing."""
dest_path = str(tmp_path / 'input.py')
energy_settings = QMEnergySettings(model_chemistry='wb97xd/def2tzvp', frequency_scale_factor=None,
atom_energies={'C': -37.79, 'H': -0.5})
with pytest.raises(ValueError, match='frequency_scale_factor is None for the model chemistry wb97xd/def2tzvp'):
write_hybrid_network_input_file(
source_path=NETWORK_FIXTURE,
dest_path=dest_path,
method='CSE',
qm_transition_states={'TS1': ts_conformer()},
energy_settings=energy_settings,
)
assert not os.path.exists(dest_path)


def test_qm_reaction_omits_tunneling_when_not_configured(tmp_path):
"""tunneling=None on QMEnergySettings means no tunneling = ... is written."""
dest_path = str(tmp_path / 'input.py')
energy_settings = QMEnergySettings(
model_chemistry='wb97xd/def2tzvp', tunneling=None, atom_energies={'C': -37.79, 'H': -0.5})
model_chemistry='wb97xd/def2tzvp', tunneling=None, frequency_scale_factor=0.988,
atom_energies={'C': -37.79, 'H': -0.5})
write_hybrid_network_input_file(
source_path=NETWORK_FIXTURE,
dest_path=dest_path,
Expand Down Expand Up @@ -1464,6 +1483,7 @@ def test_an_ordinary_network_without_any_unpacking_still_writes(self, tmp_path):
CHO2_CONFORMERS = os.path.join(CHO2_DIR, 'conformers.json')
CHO2_ENERGY_SETTINGS = QMEnergySettings(
model_chemistry="LevelOfTheory(method='wb97xd2023',basis='def2tzvp',software='gaussian')",
frequency_scale_factor=0.988,
atom_energies={
'Br': -2574.174533595486, 'C': -37.84706210301937, 'Cl': -460.1467876783656,
'F': -99.73955550924293, 'H': -0.5006557872395249, 'N': -54.584995947182875,
Expand Down
1 change: 1 addition & 0 deletions tests/test_pdep/test_main_wiring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ def _write_energy_settings_fixture(arc_dir: str, statmech_subdir: str = 'kinetic
with open(input_py_path, 'w') as f:
f.write(
"modelChemistry = 'CBS-QB3'\n\n"
"frequencyScaleFactor = 0.99\n\n"
"useHinderedRotors = True\n\n"
"useAtomCorrections = True\n\n"
"atomEnergies = {'C': -37.844411, 'H': -0.499818, 'N': -54.581501, 'O': -75.062219}\n\n"
Expand Down
Loading
Loading