From 65bb47d6af370485dfc6df262f07e4b8961bcc1c Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Wed, 14 Oct 2026 01:44:47 +0000 Subject: [PATCH] Save diatomic example data to working directory Fixes #64. In examples/generate_diatomic.py, MolecularData instances were created without specifying data_directory, causing them to default to openfermion.config.DATA_DIRECTORY (located within the installed openfermion package). In read-only or system installations, running the example raised PermissionError when run_pyscf and molecule.save() attempted to write HDF5 data into the package directory. This change: - Sets data_directory = os.getcwd() in examples/generate_diatomic.py and passes it to MolecularData calls so output is written to the working directory. - Removes redundant explicit molecule.save() calls since run_pyscf saves by default. - Adds an integration test in openfermionpyscf/tests/_run_pyscf_test.py verifying generate_diatomic.py runs and saves files to the working directory even when DATA_DIRECTORY is read-only. --- examples/generate_diatomic.py | 9 ++++--- openfermionpyscf/tests/_run_pyscf_test.py | 33 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/examples/generate_diatomic.py b/examples/generate_diatomic.py index f81f45a..10e3f4c 100644 --- a/examples/generate_diatomic.py +++ b/examples/generate_diatomic.py @@ -39,6 +39,7 @@ run_ccsd = 1 run_fci = 1 verbose = 1 + data_directory = os.getcwd() # Run Diatomic Curve for spacing in spacings: @@ -49,7 +50,8 @@ basis, multiplicity, charge, - description) + description, + data_directory=data_directory) molecule = run_pyscf(molecule, run_scf=run_scf, @@ -58,7 +60,6 @@ run_ccsd=run_ccsd, run_fci=run_fci, verbose=verbose) - molecule.save() # Run Li H single point description = "1.45" @@ -68,7 +69,8 @@ basis, multiplicity, charge, - description) + description, + data_directory=data_directory) molecule = run_pyscf(molecule, run_scf=run_scf, @@ -77,4 +79,3 @@ run_ccsd=run_ccsd, run_fci=run_fci, verbose=verbose) - molecule.save() diff --git a/openfermionpyscf/tests/_run_pyscf_test.py b/openfermionpyscf/tests/_run_pyscf_test.py index a7083a7..004b322 100644 --- a/openfermionpyscf/tests/_run_pyscf_test.py +++ b/openfermionpyscf/tests/_run_pyscf_test.py @@ -14,8 +14,11 @@ from __future__ import absolute_import import os +import runpy +import openfermion.chem.molecular_data from openfermion.chem import MolecularData +import openfermion.config from openfermionpyscf import run_pyscf from openfermionpyscf import PyscfMolecularData @@ -58,3 +61,33 @@ def test_run_pyscf_without_save(tmp_path): run_scf=True) assert isinstance(new_mole_saved, PyscfMolecularData) assert os.path.exists(f'{new_mole_saved.filename}.hdf5') + + +def test_generate_diatomic(tmp_path, monkeypatch): + """Verify generate_diatomic.py saves files to the working directory.""" + readonly_dir = tmp_path / 'readonly_data' + readonly_dir.mkdir() + readonly_dir.chmod(0o555) + + work_dir = tmp_path / 'work' + work_dir.mkdir() + + example_path = os.path.abspath( + os.path.join(os.path.dirname(__file__), '..', '..', 'examples', + 'generate_diatomic.py')) + + try: + monkeypatch.setattr(openfermion.chem.molecular_data, 'DATA_DIRECTORY', + str(readonly_dir)) + monkeypatch.setattr(openfermion.config, 'DATA_DIRECTORY', + str(readonly_dir)) + monkeypatch.chdir(work_dir) + + runpy.run_path(example_path, run_name='__main__') + + assert (work_dir / 'H2_sto-3g_singlet_0.7414.hdf5').exists() + assert (work_dir / 'H1-Li1_sto-3g_singlet_1.45.hdf5').exists() + assert len(list(work_dir.glob('*.hdf5'))) == 32 + assert not list(readonly_dir.iterdir()) + finally: + readonly_dir.chmod(0o755)