Skip to content

Commit

Permalink
Ignore conv. failure for CP2K during optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
WardLT committed Apr 10, 2024
1 parent 0677d7d commit a175e55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 9 additions & 3 deletions mofa/simulation/cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ def run_optimization(self, mof: MOFRecord,
"""

atoms = _load_structure(mof, structure_source)
return self._run_cp2k(mof.name, atoms, 'optimize', level, steps, fmax)
return self._run_cp2k(mof.name, atoms, 'optimize', level, steps, fmax, ignore_failure=True)

def _run_cp2k(self, name: str, atoms: ase.Atoms, action: str, level: str, steps: int = 8, fmax: float = 1e-2) -> tuple[ase.Atoms, Path]:
def _run_cp2k(self, name: str, atoms: ase.Atoms, action: str, level: str,
steps: int = 8, fmax: float = 1e-2, ignore_failure: bool = False) -> tuple[ase.Atoms, Path]:
"""Run CP2K in a special directory
Args:
Expand All @@ -151,6 +152,7 @@ def _run_cp2k(self, name: str, atoms: ase.Atoms, action: str, level: str, steps:
level: Level of accuracy to use
steps: Number of steps to run
fmax: Convergence threshold for optimization
ignore_failure: Whether to ignore failures
Returns:
- Relaxed structure
- Absolute path to the run directory
Expand All @@ -159,6 +161,10 @@ def _run_cp2k(self, name: str, atoms: ase.Atoms, action: str, level: str, steps:
template_file = _file_dir / f'cp2k-{level}-template.inp'
if not template_file.is_file():
raise ValueError(f'Template not found for {level}')
inp = template_file.read_text()
if ignore_failure:
# Does not work with CP2K<2024.1, which is what we're running on Polaris but not what comes with Ubuntu
inp = inp.replace("&SCF\n", "&SCF\n IGNORE_CONVERGENCE_FAILURE\n")

# Get the other settings
if level not in _cp2k_options:
Expand All @@ -177,7 +183,7 @@ def _run_cp2k(self, name: str, atoms: ase.Atoms, action: str, level: str, steps:
with CP2K(
command=self.cp2k_invocation,
directory=".",
inp=template_file.read_text(),
inp=inp,
max_scf=128,
**options,
) as calc:
Expand Down
5 changes: 4 additions & 1 deletion tests/simulation/test_cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_cp2k_optimize(cif_name, cif_dir, tmpdir):

# Make a CP2k simulator that reads and writes to a temporary directory
runner = CP2KRunner(
cp2k_invocation="cp2k_shell.psmp",
cp2k_invocation="cp2k_shell", # Maps to the 2024.1 CP2K on wardlt's machine (another reason I skip it on CI)
)

test_file = cif_dir / f'{cif_name}.cif'
Expand All @@ -45,5 +45,8 @@ def test_cp2k_optimize(cif_name, cif_dir, tmpdir):
assert cp2k_path.is_absolute()
assert 'opt' in cp2k_path.name

# Make sure IGNORE_CONVERGENCE was turned on
assert 'IGNORE_CONVER' in (cp2k_path / 'cp2k.inp').read_text()

charged_mof = compute_partial_charges(cp2k_path, threads=2)
assert charged_mof.arrays["q"].shape[0] == charged_mof.arrays["positions"].shape[0]

0 comments on commit a175e55

Please sign in to comment.