Skip to content

Commit

Permalink
Fix max_scf_cycles assignment #48
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Jun 21, 2023
1 parent 36e88d8 commit b10ba41
Show file tree
Hide file tree
Showing 5 changed files with 954 additions and 8 deletions.
8 changes: 3 additions & 5 deletions pymuonsuite/io/castep.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def _read_castep_gamma_phonons(

if gamma_i is None:
raise CastepError(
"Could not find gamma point phonons in" " CASTEP phonon file"
"Could not find gamma point phonons in CASTEP phonon file"
)

atoms.info["ph_evals"] = evals[gamma_i]
Expand Down Expand Up @@ -364,10 +364,8 @@ def _create_geom_opt_castep_calculator(self):
self._calc.param.geom_force_tol = 0.05

max_scf_cycles_param = self.params.get("max_scc_steps")

if max_scf_cycles_param is not None:
self._calc.param.max_scf_cycles.value = max_scf_cycles_param

self._calc.param.max_scf_cycles = max_scf_cycles_param
else:
if self._calc.param.max_scf_cycles.value is None:
self._calc.param.max_scf_cycles = 30
Expand All @@ -376,7 +374,7 @@ def _create_geom_opt_castep_calculator(self):

# Remove settings for magres calculator:
self._calc.param.magres_task = None

print(self._calc.param.max_scf_cycles.value)
return self._calc


Expand Down
96 changes: 96 additions & 0 deletions pymuonsuite/test/test_castep.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests for ReadWriteCastep methods"""

import tempfile
import unittest

import os
import shutil
import numpy as np

from ase import Atoms, io
from ase.calculators.castep import Castep
from ase.io.castep import read_param

from pymuonsuite.utils import list_to_string, get_element_from_custom_symbol
Expand Down Expand Up @@ -80,6 +82,7 @@ def test_write(self):
cell_file = os.path.join(input_folder, "Si2.cell")
param_file = os.path.join(input_folder, "Si2.param")
input_params = load_input_file(yaml_file, MuAirssSchema)
input_params["max_scc_steps"] = 31

with silence_stdio():
castep_param = read_param(param_file).param
Expand All @@ -93,6 +96,7 @@ def test_write(self):
sname="Si2_geom_opt",
calc_type="GEOM_OPT",
)
self.assertEqual(reader._calc.param.max_scf_cycles.value, "31")

reader.write(atoms, output_folder, sname="Si2_magres", calc_type="MAGRES")

Expand Down Expand Up @@ -267,6 +271,98 @@ def test_write_uses_correct_particle_mass_and_element(self):
finally:
shutil.rmtree(output_folder)

def test_calc_overwrite(self):
# Ensure that if a calculator is provided, it is initialised properly
calculator = Castep()
read_write_castep = ReadWriteCastep(calc=calculator)

# Original calculator should have been deep copied, so remain as it was
self.assertNotEqual(
"radsectesla\nH:mu 851615456.5978916\n",
calculator.cell.species_gamma.value,
)
self.assertNotEqual(
"AMU\nH:mu 0.1134289259\n",
calculator.cell.species_mass.value,
)

# Copy stored should have appropriate muon values set
self.assertEqual(
"radsectesla\nH:mu 851615456.5978916\n",
read_write_castep._calc.cell.species_gamma.value,
)
self.assertEqual(
"AMU\nH:mu 0.1134289259\n",
read_write_castep._calc.cell.species_mass.value,
)

def test_set_params(self):
# Ensure set_params changes the calculator object
read_write_castep = ReadWriteCastep()
read_write_castep.set_params({"mu_symbol": "H:nu"})

self.assertEqual(
"radsectesla\nH:nu 851615456.5978916\n",
read_write_castep._calc.cell.species_gamma.value,
)
self.assertEqual(
"AMU\nH:nu 0.1134289259\n",
read_write_castep._calc.cell.species_mass.value,
)

def test_read_error(self):
# Ensure user receives an error if .castep output file is missing
read_write_castep = ReadWriteCastep()
si_folder = _TESTDATA_DIR + "/Si2"
ethylene_mu_folder = _TESTDATA_DIR + "/ethyleneMu"

with self.assertRaises(IOError) as e:
read_write_castep.read(".")
self.assertIn("ERROR: No .castep files found in", str(e.exception))

with self.assertRaises(IOError) as e:
read_write_castep.read(folder=si_folder, read_magres=True)
self.assertIn("No .magres files found in", str(e.exception))

with self.assertRaises(IOError) as e:
read_write_castep.read(folder=si_folder, read_phonons=True)
self.assertIn("No .phonon files found in", str(e.exception))

with self.assertRaises(IOError) as e:
read_write_castep.read(
folder=ethylene_mu_folder,
sname="ethyleneMu_no_gamma",
read_phonons=True,
)
self.assertIn(
"Could not find gamma point phonons in CASTEP phonon file",
str(e.exception),
)

def test_write_cell_calc_type(self):
# Ensure calc_type is propagated by write_cell
read_write_castep = ReadWriteCastep()

with tempfile.TemporaryDirectory() as tmp_dir:
read_write_castep.write_cell(
a=Atoms(["H"]), folder=tmp_dir, sname="H", calc_type="MAGRES"
)
self.assertEqual("Magres", read_write_castep._calc.param.task.value)

def test_write_cell_calc_type_error(self):
# Ensure user receives an error if using a bad calc_type
read_write_castep = ReadWriteCastep()

with self.assertRaises(NotImplementedError) as e:
read_write_castep.write_cell(
a=None, folder=None, sname=None, calc_type=None
)
self.assertIn(
"Calculation type None is not implemented. "
"Please choose 'GEOM_OPT' or 'MAGRES'",
str(e.exception),
)


if __name__ == "__main__":

Expand Down
Loading

0 comments on commit b10ba41

Please sign in to comment.