Skip to content

Commit 2db833d

Browse files
committed
Refactor job handling to improve scratch directory management and enhance test paths
1 parent bcf9b0c commit 2db833d

2 files changed

Lines changed: 38 additions & 26 deletions

File tree

autotst/job/job.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,32 @@ def write_input(self, conformer, ase_calculator):
149149
"""
150150

151151
ase_calculator.write_input(conformer.ase_molecule)
152-
try:
153-
os.makedirs(ase_calculator.scratch)
154-
except OSError:
155-
pass
152+
scratch = getattr(ase_calculator, "scratch", None) or ase_calculator.parameters.get("scratch")
153+
if not scratch:
154+
raise AttributeError("ASE Gaussian calculator has no scratch location (no .scratch and no parameters['scratch']).")
155+
os.makedirs(scratch, exist_ok=True)
156156

157-
shutil.move(
158-
ase_calculator.label + ".com",
159-
os.path.join(
160-
ase_calculator.scratch,
161-
ase_calculator.label + ".com"
162-
))
157+
base = ase_calculator.label
158+
com_src = f"{base}.com"
159+
if not os.path.exists(com_src):
160+
raise FileNotFoundError(f"Expected ASE to write {com_src} in CWD={os.getcwd()}")
163161

164162
shutil.move(
165-
ase_calculator.label + ".ase",
163+
com_src,
166164
os.path.join(
167-
ase_calculator.scratch,
168-
ase_calculator.label + ".ase"
165+
scratch,
166+
com_src
169167
))
170168

169+
ase_src = f"{base}.ase"
170+
if os.path.exists(ase_src):
171+
shutil.move(
172+
ase_src,
173+
os.path.join(
174+
scratch,
175+
ase_src
176+
))
177+
171178
def check_complete(self, label):
172179
"""
173180
A method to determine if a job is still running
@@ -271,7 +278,10 @@ def submit_conformer(self, conformer, restart=False):
271278
self.calculator.conformer = conformer
272279
ase_calculator = self.calculator.get_conformer_calc()
273280
label = conformer.smiles + f"_{conformer.index}"
274-
file_path = os.path.join(ase_calculator.scratch, label)
281+
scratch = getattr(ase_calculator, "scratch", None) or ase_calculator.parameters.get("scratch")
282+
if not scratch:
283+
raise AttributeError("ASE Gaussian calculator has no scratch location (no .scratch and no parameters['scratch']).")
284+
file_path = os.path.join(scratch, label)
275285
# for testing
276286
os.environ["FILE_PATH"] = file_path
277287

@@ -604,7 +614,9 @@ def submit_transitionstate(self, transitionstate, opt_type, restart=False):
604614
self.write_input(transitionstate, ase_calculator)
605615

606616
label = ase_calculator.label
607-
scratch = ase_calculator.scratch
617+
scratch = getattr(ase_calculator, "scratch", None) or ase_calculator.parameters.get("scratch")
618+
if not scratch:
619+
raise AttributeError("ASE Gaussian calculator has no scratch location (no .scratch and no parameters['scratch']).")
608620
file_path = os.path.join(scratch, label)
609621
# for testing
610622
os.environ["FILE_PATH"] = file_path
@@ -853,7 +865,10 @@ def submit_rotor(self, conformer, torsion_index, restart=False):
853865
self.calculator.conformer = conformer
854866
ase_calculator = self.calculator.get_rotor_calc(torsion_index)
855867
label = ase_calculator.label
856-
file_path = os.path.join(ase_calculator.scratch, ase_calculator.label)
868+
scratch = getattr(ase_calculator, "scratch", None) or ase_calculator.parameters.get("scratch")
869+
if not scratch:
870+
raise AttributeError("ASE Gaussian calculator has no scratch location (no .scratch and no parameters['scratch']).")
871+
file_path = os.path.join(scratch, ase_calculator.label)
857872

858873
os.environ["FILE_PATH"] = file_path
859874

autotst/job/job_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,18 @@
3535
from autotst.data.base import TransitionStates
3636
from autotst.job.job import Job
3737
from autotst.calculator.gaussian import Gaussian
38+
from autotst.utils.paths import test_bin_dir, test_data_dir, test_dir
3839
import multiprocessing
3940
import subprocess
4041
import time
4142

4243
class JobTest(unittest.TestCase):
4344

4445
def setUp(self):
45-
os.environ["PATH"] = os.path.expandvars("$AUTOTST/test/bin:") + os.environ["PATH"]
46+
os.environ["PATH"] = f"{test_bin_dir()}:{os.environ['PATH']}"
4647
os.environ["TEST_STATUS"] = "None"
4748
self.reaction = Reaction("CC+[O]O_[CH2]C+OO")
48-
self.calculator = Gaussian(directory=os.path.expandvars("$AUTOTST/test"))
49+
self.calculator = Gaussian(directory=str(test_dir()))
4950
self.job = Job(
5051
reaction=self.reaction,
5152
calculator=self.calculator,
@@ -77,9 +78,9 @@ def test_setup2(self):
7778

7879
def test_read_log(self):
7980

80-
path = os.path.expandvars("$AUTOTST/test/bin/log-files/CC_0.log")
81+
path = test_data_dir() / "CC_0.log"
8182

82-
atoms = self.job.read_log(path)
83+
atoms = self.job.read_log(str(path))
8384

8485
self.assertEqual(len(atoms), 8)
8586

@@ -132,11 +133,8 @@ def test_calculate_species(self):
132133
for species in self.reaction.reactants + self.reaction.products:
133134
self.job.calculate_species(species)
134135
for smiles in species.conformers.keys():
135-
self.assertTrue(os.path.exists(os.path.join(
136-
os.path.expandvars("$AUTOTST/test/species/"),
137-
smiles,
138-
smiles + ".log"
139-
)))
136+
log_path = test_dir() / "species" / smiles / f"{smiles}.log"
137+
self.assertTrue(log_path.exists())
140138

141139
def test_submit_transitionstate(self):
142140
ts = self.reaction.ts["forward"][0]
@@ -173,4 +171,3 @@ def test_calculate_reaction(self):
173171
if __name__ == "__main__":
174172
unittest.main(testRunner=unittest.TextTestRunner(verbosity=2))
175173

176-

0 commit comments

Comments
 (0)