|
7 | 7 |
|
8 | 8 | import os |
9 | 9 | import shutil |
| 10 | +import tempfile |
10 | 11 | import unittest |
11 | 12 |
|
| 13 | +from arc.exceptions import InputError |
12 | 14 | from arc.common import ARC_PATH |
13 | 15 | from arc.level import Level |
14 | 16 | from arc.reaction import ARCReaction |
15 | 17 | from arc.species import ARCSpecies |
16 | 18 | from arc.statmech.adapter import StatmechEnum |
17 | 19 | from arc.statmech.arkane import ArkaneAdapter |
18 | | -from arc.statmech.arkane import _level_to_str, _section_contains_key, get_arkane_model_chemistry |
19 | | -from arc.imports import settings |
| 20 | +from arc.statmech.arkane import ( |
| 21 | + _available_years_for_level, |
| 22 | + _find_best_level_key_for_sp_level, |
| 23 | + _get_qm_corrections_files, |
| 24 | + _level_to_str, |
| 25 | + _normalize_basis, |
| 26 | + _normalize_method, |
| 27 | + _parse_lot_params, |
| 28 | + _section_contains_key, |
| 29 | + _split_method_year, |
| 30 | + get_arkane_model_chemistry, |
| 31 | +) |
| 32 | +from unittest.mock import patch |
20 | 33 |
|
21 | 34 |
|
22 | 35 | class TestEnumerationClasses(unittest.TestCase): |
@@ -181,6 +194,105 @@ def test_get_arkane_model_chemistry_latest_year(self): |
181 | 194 | freq_scale_factor=1.0) |
182 | 195 | self.assertEqual(model_chemistry, "LevelOfTheory(method='cbsqb3',software='gaussian')") |
183 | 196 |
|
| 197 | + def test_level_helpers(self): |
| 198 | + """Test helper functions for method/basis/year parsing.""" |
| 199 | + self.assertEqual(_normalize_method("DLPNO-CCSD(T)-F12"), "dlpnoccsd(t)f12") |
| 200 | + self.assertEqual(_normalize_method("dlpnoccsd(t)f122023"), "dlpnoccsd(t)f122023") |
| 201 | + |
| 202 | + base, year = _split_method_year("dlpnoccsd(t)f122023") |
| 203 | + self.assertEqual(base, "dlpnoccsd(t)f12") |
| 204 | + self.assertEqual(year, 2023) |
| 205 | + base, year = _split_method_year("dlpnoccsd(t)f12") |
| 206 | + self.assertEqual(base, "dlpnoccsd(t)f12") |
| 207 | + self.assertIsNone(year) |
| 208 | + |
| 209 | + self.assertEqual(_normalize_basis("cc-pVTZ-F12"), "ccpvtzf12") |
| 210 | + self.assertEqual(_normalize_basis("ccpvtz f12"), "ccpvtzf12") |
| 211 | + |
| 212 | + params = _parse_lot_params( |
| 213 | + "LevelOfTheory(method='dlpnoccsd(t)f122023',basis='ccpvtzf12',software='orca')" |
| 214 | + ) |
| 215 | + self.assertEqual(params["method"], "dlpnoccsd(t)f122023") |
| 216 | + self.assertEqual(params["basis"], "ccpvtzf12") |
| 217 | + self.assertEqual(params["software"], "orca") |
| 218 | + |
| 219 | + def test_level_key_selection(self): |
| 220 | + """Test matching of LevelOfTheory keys by year and no-year preference.""" |
| 221 | + section = '\n'.join([ |
| 222 | + 'atom_energies = {', |
| 223 | + " \"LevelOfTheory(method='cbsqb3',software='gaussian')\": {},", |
| 224 | + " \"LevelOfTheory(method='cbsqb32023',software='gaussian')\": {},", |
| 225 | + "}", |
| 226 | + "pbac = {", |
| 227 | + ]) |
| 228 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False) as f: |
| 229 | + f.write(section) |
| 230 | + path = f.name |
| 231 | + try: |
| 232 | + level = Level(method="CBS-QB3", software="gaussian") |
| 233 | + best = _find_best_level_key_for_sp_level(level, path, "atom_energies = {", "pbac = {") |
| 234 | + self.assertEqual(best, "LevelOfTheory(method='cbsqb3',software='gaussian')") |
| 235 | + |
| 236 | + level_year = Level(method="CBS-QB3", software="gaussian", year=2023) |
| 237 | + best_year = _find_best_level_key_for_sp_level(level_year, path, "atom_energies = {", "pbac = {") |
| 238 | + self.assertEqual(best_year, "LevelOfTheory(method='cbsqb32023',software='gaussian')") |
| 239 | + |
| 240 | + years = _available_years_for_level(level, path, "atom_energies = {", "pbac = {") |
| 241 | + self.assertEqual(years, [None, 2023]) |
| 242 | + finally: |
| 243 | + os.remove(path) |
| 244 | + |
| 245 | + def test_conflicting_year_spec(self): |
| 246 | + """Test conflicting year in method suffix vs explicit year.""" |
| 247 | + section = '\n'.join([ |
| 248 | + 'atom_energies = {', |
| 249 | + " \"LevelOfTheory(method='b97d32023',software='gaussian')\": {},", |
| 250 | + "}", |
| 251 | + "pbac = {", |
| 252 | + ]) |
| 253 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False) as f: |
| 254 | + f.write(section) |
| 255 | + path = f.name |
| 256 | + try: |
| 257 | + level = Level(method="b97d32023", software="gaussian", year=2022) |
| 258 | + with self.assertRaises(InputError): |
| 259 | + _find_best_level_key_for_sp_level(level, path, "atom_energies = {", "pbac = {") |
| 260 | + finally: |
| 261 | + os.remove(path) |
| 262 | + |
| 263 | + def test_qm_corrections_file_path(self): |
| 264 | + """Test quantum corrections files are read from the RMG database path.""" |
| 265 | + with tempfile.TemporaryDirectory() as rmg_root: |
| 266 | + rmg_qc = os.path.join(rmg_root, 'input', 'quantum_corrections', 'data.py') |
| 267 | + os.makedirs(os.path.dirname(rmg_qc), exist_ok=True) |
| 268 | + with open(rmg_qc, 'w') as f: |
| 269 | + f.write('# rmg qc\n') |
| 270 | + |
| 271 | + with patch('arc.statmech.arkane.RMG_DB_PATH', rmg_root): |
| 272 | + paths = _get_qm_corrections_files() |
| 273 | + self.assertTrue(paths) |
| 274 | + self.assertEqual(paths[0], rmg_qc) |
| 275 | + |
| 276 | + def test_get_arkane_model_chemistry_from_qm_file(self): |
| 277 | + """Test reading LevelOfTheory keys from a quantum corrections file.""" |
| 278 | + section = '\n'.join([ |
| 279 | + 'atom_energies = {', |
| 280 | + " \"LevelOfTheory(method='cbsqb3',software='gaussian')\": {},", |
| 281 | + "}", |
| 282 | + "pbac = {", |
| 283 | + ]) |
| 284 | + with tempfile.NamedTemporaryFile(mode="w+", delete=False) as f: |
| 285 | + f.write(section) |
| 286 | + path = f.name |
| 287 | + try: |
| 288 | + with patch('arc.statmech.arkane._get_qm_corrections_files', return_value=[path]): |
| 289 | + model_chemistry = get_arkane_model_chemistry( |
| 290 | + sp_level=Level(method='CBS-QB3'), |
| 291 | + freq_scale_factor=1.0, |
| 292 | + ) |
| 293 | + self.assertEqual(model_chemistry, "LevelOfTheory(method='cbsqb3',software='gaussian')") |
| 294 | + finally: |
| 295 | + os.remove(path) |
184 | 296 | def test_generate_arkane_input(self): |
185 | 297 | """Test generating Arkane input""" |
186 | 298 | statmech_dir = os.path.join(ARC_PATH, 'arc', 'testing', 'arkane_input_tests_delete') |
|
0 commit comments