Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising errors when restart files don't exist #422

Merged
merged 22 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
84f4315
replace warning by error
samwaseda Nov 11, 2021
84ce96e
Function to check if a file exists in a job's working dir
sudarsan-surendralal Nov 11, 2021
b13d0b9
Avoid duplicate code: use base method
sudarsan-surendralal Nov 11, 2021
4d3f929
more explicit warnings
freyso Nov 11, 2021
31bb772
Improving testing
sudarsan-surendralal Nov 11, 2021
1bec4d8
Add the working directory to the error mssg
sudarsan-surendralal Nov 11, 2021
363dfa3
Fix tests
sudarsan-surendralal Nov 11, 2021
e366550
Use this for LAMMPS and SPHINX too!
sudarsan-surendralal Nov 11, 2021
c336ee5
Merge remote-tracking branch 'origin/spx_restart_from_charge_density_…
sudarsan-surendralal Nov 11, 2021
25112e4
Refactor to single function that checks and returns correct path
sudarsan-surendralal Nov 11, 2021
2fb990f
rename ensure_file_exists_and_return to get_workdir_file
freyso Nov 11, 2021
bd5b8d4
Update pyiron_base version
sudarsan-surendralal Nov 18, 2021
b0716ea
Update pyiron_base version
sudarsan-surendralal Nov 18, 2021
6c886e3
Fix mamba version (https://github.com/pyiron/pyiron_base/pull/530/com…
sudarsan-surendralal Nov 18, 2021
012e0e7
bump h5io version
sudarsan-surendralal Nov 18, 2021
5b57c94
Merge remote-tracking branch 'origin/master' into restart_error
sudarsan-surendralal Nov 27, 2021
8f8011e
Merge remote-tracking branch 'origin/master' into restart_error
sudarsan-surendralal Jan 18, 2022
0009111
Don't start automatically from wave functions/ charge density
sudarsan-surendralal Jan 18, 2022
7c9560b
revert mamba version
sudarsan-surendralal Jan 18, 2022
6672604
Update tests_sphinx_sphinx_check_all.ipynb
sudarsan-surendralal Jan 19, 2022
8165bee
Merge branch 'master' into restart_error
jan-janssen Feb 22, 2022
cff6549
Format black
pyiron-runner Feb 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions pyiron_atomistics/atomistics/job/atomistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import copy
import warnings
import numpy as np
import os
from ase.io import write as ase_write
import posixpath
from pyiron_atomistics.atomistics.structure.atoms import Atoms
from pyiron_atomistics.atomistics.structure.neighbors import NeighborsTrajectory
from pyiron_atomistics.atomistics.structure.has_structure import HasStructure
Expand Down Expand Up @@ -413,6 +415,21 @@ def restart(self, job_name=None, job_type=None):
new_ham._generic_input['structure'] = 'atoms'
return new_ham

def check_if_file_exists(self, filename) -> None:
"""
Checks if a given file exists within the job directory

ToDo: Move this to pyiron_base since this is more generic.

Args:
filename (str): The name of the file

Raises:
FileNotFoundError: Raised if the given file does not exist.
"""
if not os.path.isfile(posixpath.join(self.working_directory, filename)):
raise FileNotFoundError(f"File {filename} not found")
freyso marked this conversation as resolved.
Show resolved Hide resolved

# Required functions
def continue_with_restart_files(self, job_type=None, job_name=None):
"""
Expand Down
57 changes: 16 additions & 41 deletions pyiron_atomistics/vasp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1525,17 +1525,11 @@ def restart_from_charge_density(
new_ham (vasp.vasp.Vasp instance): New job
"""
new_ham = self.restart(job_name=job_name, job_type=job_type)
self.check_if_file_exists("CHGCAR")
if new_ham.__name__ == self.__name__:
try:
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "CHGCAR")
)
except IOError:
self.logger.warning(
msg="A CHGCAR from job: {} is not generated and therefore it can't be read.".format(
self.job_name
)
)
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "CHGCAR")
)
new_ham.input.incar["ICHARG"] = self.get_icharg_value(
icharg=icharg,
self_consistent_calc=self_consistent_calc,
Expand Down Expand Up @@ -1583,29 +1577,16 @@ def restart_from_wave_and_charge(
new_ham (vasp.vasp.Vasp instance): New job
"""
new_ham = self.restart(job_name=job_name, job_type=job_type)
self.check_if_file_exists("WAVECAR")
self.check_if_file_exists("CHGCAR")
if new_ham.__name__ == self.__name__:
try:
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "CHGCAR")
)
except IOError:
self.logger.warning(
msg="A CHGCAR from job: {} is not generated and therefore it can't be read.".format(
self.job_name
)
)
try:
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "WAVECAR")
)
except IOError:
self.logger.warning(
msg="A WAVECAR from job: {} is not generated and therefore it can't be read.".format(
self.job_name
)
)
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "CHGCAR")
)
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "WAVECAR")
)
new_ham.input.incar["ISTART"] = istart

new_ham.input.incar["ICHARG"] = self.get_icharg_value(
icharg=icharg,
self_consistent_calc=self_consistent_calc,
Expand Down Expand Up @@ -1652,17 +1633,11 @@ def restart_from_wave_functions(
new_ham (vasp.vasp.Vasp instance): New job
"""
new_ham = self.restart(job_name=job_name, job_type=job_type)
self.check_if_file_exists("WAVECAR")
if new_ham.__name__ == self.__name__:
try:
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "WAVECAR")
)
except IOError:
self.logger.warning(
msg="A WAVECAR from job: {} is not generated and therefore it can't be read.".format(
self.job_name
)
)
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "WAVECAR")
)
new_ham.input.incar["ISTART"] = istart
return new_ham

Expand Down
59 changes: 30 additions & 29 deletions tests/vasp/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def setUp(self):
self.job.structure = None

def test_list_potentials(self):
self.assertRaises(ValueError, self.job.list_potentials)
self.assertEqual(sorted([
'Fe', 'Fe_GW', 'Fe_pv', 'Fe_sv', 'Fe_sv_GW', 'Se', 'Se_GW',
'O', 'O_GW', 'O_GW_new', 'O_h', 'O_s', 'O_s_GW'
Expand Down Expand Up @@ -276,9 +277,6 @@ def test_set_structure(self):
self.job.structure = atoms
self.assertEqual(self.job.structure, atoms)

def test_list_potenitals(self):
self.assertRaises(ValueError, self.job.list_potentials)

def test_run_complete(self):
self.job_complete.exchange_correlation_functional = "PBE"
self.job_complete.set_occupancy_smearing(smearing="fermi", width=0.2)
Expand All @@ -297,6 +295,12 @@ def test_run_complete(self):
self.job_complete.restart_file_list.append(
posixpath.join(file_directory, "OUTCAR")
)
self.job_complete.restart_file_list.append(
posixpath.join(file_directory, "CHGCAR")
)
self.job_complete.restart_file_list.append(
posixpath.join(file_directory, "WAVECAR")
)
self.job_complete.run(run_mode="manual")
self.job_complete.status.collect = True
self.job_complete.run()
Expand Down Expand Up @@ -334,34 +338,20 @@ def test_run_complete(self):
) as h_dft:
hdf_nodes = h_dft.list_nodes()
self.assertTrue(all([node in hdf_nodes for node in nodes]))

job_chg_den = self.job_complete.restart_from_charge_density(job_name="chg")
self.assertEqual(job_chg_den.structure, self.job_complete.get_structure(-1))
self.assertTrue(
posixpath.join(self.job_complete.working_directory, "CHGCAR")
in job_chg_den.restart_file_list
)
with job_chg_den.project_hdf5.open("output") as h_out:
self.assertTrue(h_out.list_nodes() == [])
self.assertTrue(h_out.list_groups() == [])

with job_chg_den.project_hdf5.open("input") as h_in:
self.assertFalse(h_in.list_nodes() == [])
self.assertFalse(h_in.list_groups() == [])
def check_group_is_empty(example_job, group_name):
with example_job.project_hdf5.open(group_name) as h_gr:
self.assertTrue(h_gr.list_nodes() == [])
self.assertTrue(h_gr.list_groups() == [])

job_wave = self.job_complete.restart_from_wave_functions(job_name="wave")
self.assertEqual(job_wave.structure, self.job_complete.get_structure(-1))
self.assertTrue(
posixpath.join(self.job_complete.working_directory, "WAVECAR")
in job_wave.restart_file_list
)
with job_wave.project_hdf5.open("output") as h_out:
self.assertTrue(h_out.list_nodes() == [])
self.assertTrue(h_out.list_groups() == [])

with job_wave.project_hdf5.open("input") as h_in:
self.assertFalse(h_in.list_nodes() == [])
self.assertFalse(h_in.list_groups() == [])
check_group_is_empty(job_chg_den, "output")
check_group_is_empty(job_chg_den, "input")

job_chg_wave = self.job_complete.restart_from_wave_and_charge(
job_name="chg_wave"
Expand All @@ -377,13 +367,24 @@ def test_run_complete(self):
)
for key, val in job_chg_wave.restart_file_dict.items():
self.assertTrue(key, val)
with job_chg_wave.project_hdf5.open("output") as h_out:
self.assertTrue(h_out.list_nodes() == [])
self.assertTrue(h_out.list_groups() == [])

with job_chg_wave.project_hdf5.open("input") as h_in:
self.assertFalse(h_in.list_nodes() == [])
self.assertFalse(h_in.list_groups() == [])
check_group_is_empty(job_chg_wave, "output")
check_group_is_empty(job_chg_wave, "input")

job = self.job_complete.restart()
job.restart_file_list.append(
posixpath.join(file_directory, "vasprun.xml")
)
job.restart_file_list.append(
posixpath.join(file_directory, "OUTCAR")
)
job.run(run_mode="manual")
job.status.collect = True
job.run()
# Check if error raised if the files don't exist
self.assertRaises(FileNotFoundError, job.restart_from_wave_functions, "wave_restart")
self.assertRaises(FileNotFoundError, job.restart_from_charge_density, "chg_restart")
self.assertRaises(FileNotFoundError, job.restart_from_wave_and_charge, "wave_chg_restart")

def test_vasp_metadyn(self):
self.job_metadyn.set_primitive_constraint("bond_1", "bond", atom_indices=[0, 2], increment=1e-4)
Expand Down