Skip to content

Commit

Permalink
Merge pull request #422 from pyiron/restart_error
Browse files Browse the repository at this point in the history
  • Loading branch information
sudarsan-surendralal committed Feb 23, 2022
2 parents 3d70181 + cff6549 commit a519064
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 82 deletions.
2 changes: 1 addition & 1 deletion notebooks/tests_sphinx_sphinx_check_all.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
}
],
"source": [
"job = job.restart()\n",
"job = job.restart(from_charge_density=False, from_wave_functions=False)\n",
"job.run()"
]
},
Expand Down
28 changes: 26 additions & 2 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 @@ -453,6 +455,28 @@ def restart(self, job_name=None, job_type=None):
new_ham._generic_input["structure"] = "atoms"
return new_ham

def get_workdir_file(self, filename: str) -> None:
"""
Checks if a given file exists within the job's working directory and returns the absolute path to it.
ToDo: Move this to pyiron_base since this is more generic.
Args:
filename (str): The name of the file
Returns:
str: The name absolute path of the file in the working directory
Raises:
FileNotFoundError: Raised if the given file does not exist.
"""
appended_path = posixpath.join(self.working_directory, filename)
if not os.path.isfile(appended_path):
raise FileNotFoundError(
f"File {filename} not found in working directory: {self.working_directory}"
)
return appended_path

# Required functions
def continue_with_restart_files(self, job_type=None, job_name=None):
"""
Expand Down Expand Up @@ -615,7 +639,7 @@ def write_traj(
snapshot_indices=None,
overwrite_positions=None,
overwrite_cells=None,
**kwargs
**kwargs,
):
"""
Writes the trajectory in a given file file_format based on the `ase.io.write`_ function.
Expand Down Expand Up @@ -653,7 +677,7 @@ def write_traj(
format=file_format,
parallel=parallel,
append=append,
**kwargs
**kwargs,
)

def get_neighbors_snapshots(
Expand Down
7 changes: 2 additions & 5 deletions pyiron_atomistics/lammps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,11 +1213,8 @@ def restart(self, job_name=None, job_type=None):
new_ham = super(LammpsBase, self).restart(job_name=job_name, job_type=job_type)
if new_ham.__name__ == self.__name__:
new_ham.potential = self.potential
if os.path.isfile(os.path.join(self.working_directory, "restart.out")):
new_ham.read_restart_file(filename="restart.out")
new_ham.restart_file_list.append(
posixpath.join(self.working_directory, "restart.out")
)
new_ham.read_restart_file(filename="restart.out")
new_ham.restart_file_list.append(self.get_workdir_file("restart.out"))
return new_ham

def _get_lammps_structure(self, structure=None, cutoff_radius=None):
Expand Down
10 changes: 7 additions & 3 deletions pyiron_atomistics/sphinx/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,13 @@ def restart(
with warnings.catch_warnings(record=True) as w:
try:
self.collect_output()
except AssertionError:
from_charge_density = False
from_wave_functions = False
except AssertionError as orig_error:
if from_charge_density or from_wave_functions:
raise AssertionError(
orig_error.message
+ "\nCowardly refusing to use density or wavefunctions for restart.\n"
+ "Solution: set from_charge_density and from_wave_functions to False."
)
if len(w) > 0:
self.status.not_converged = True
new_job = super(SphinxBase, self).restart(job_name=job_name, job_type=job_type)
Expand Down
46 changes: 5 additions & 41 deletions pyiron_atomistics/vasp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,17 +1600,9 @@ 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)

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(self.get_workdir_file("CHGCAR"))
new_ham.input.incar["ICHARG"] = self.get_icharg_value(
icharg=icharg,
self_consistent_calc=self_consistent_calc,
Expand Down Expand Up @@ -1659,28 +1651,9 @@ def restart_from_wave_and_charge(
"""
new_ham = self.restart(job_name=job_name, job_type=job_type)
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(self.get_workdir_file("CHGCAR"))
new_ham.restart_file_list.append(self.get_workdir_file("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 @@ -1735,16 +1708,7 @@ def restart_from_wave_functions(self, job_name=None, job_type=None, istart=1):
"""
new_ham = self.restart(job_name=job_name, job_type=job_type)
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(self.get_workdir_file("WAVECAR"))
new_ham.input.incar["ISTART"] = istart
return new_ham

Expand Down
57 changes: 27 additions & 30 deletions tests/vasp/test_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -299,9 +300,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 @@ -320,6 +318,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 @@ -357,34 +361,18 @@ 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() == [])

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() == [])
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() == [])
check_group_is_empty(job_chg_den, "output")

job_chg_wave = self.job_complete.restart_from_wave_and_charge(
job_name="chg_wave"
Expand All @@ -400,13 +388,22 @@ 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() == [])
check_group_is_empty(job_chg_wave, "output")

with job_chg_wave.project_hdf5.open("input") as h_in:
self.assertFalse(h_in.list_nodes() == [])
self.assertFalse(h_in.list_groups() == [])
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

0 comments on commit a519064

Please sign in to comment.