def test_with_cp2k(name: str, dir_name: str) -> None: """Test :class:`ARMC` and :class:`ARMCPT`.""" with open(f'tests/test_files/{name}', 'r') as f: dct = yaml.load(f.read(), Loader=UniqueLoader) dct["monte_carlo"]["keep_files"] = True armc, job_kwargs = dict_to_armc(dct) try: run_armc(armc, restart=False, **job_kwargs) except Exception as ex: err_file = PATH / dir_name / 'md' / 'md.err' out_file = PATH / dir_name / 'md' / 'md.out' has_err_file = os.path.isfile(err_file) has_out_file = os.path.isfile(out_file) if not (has_err_file or has_out_file): raise msg = "" if has_err_file: with open(err_file, 'r', encoding='utf-8') as f: msg += f"{err_file}:\n{f.read()}" if out_file: with open(out_file, 'r', encoding='utf-8') as f: msg += f"{out_file}:\n{f.read()}" raise AssertionError(msg) from ex
def test_armc(name: str) -> None: """Test :class:`ARMC` and :class:`ARMCPT`.""" file = PATH / name with open(file, 'r') as f: dct = yaml.load(f.read(), Loader=UniqueLoader) armc, job_kwargs = dict_to_armc(dct) if name == 'armc_ref.yaml': REF = ARMC_REF armc.package_manager.hook = iter(load_results(ARMC_REF, n=1)) else: iterator = _get_phi_iter() def swap_phi(*args: Any, **kwargs: Any) -> List[Tuple[int, int]]: return next(iterator) REF = ARMCPT_REF armc.swap_phi = swap_phi armc.package_manager.hook = iter(load_results(ARMCPT_REF, n=3)) hdf5_ref = REF / 'armc.hdf5' hdf5 = PATH / job_kwargs['folder'] / 'armc.hdf5' run_armc(armc, restart=False, **job_kwargs) assertion.assert_(next, armc.package_manager.hook, exception=StopIteration) with h5py.File(hdf5, 'r') as f1, h5py.File(hdf5_ref, 'r') as f2: compare_hdf5(f1, f2, skip={'/param', '/aux_error_mod'}) assertion.shape_eq(f1['param'], f2['param']) assertion.shape_eq(f1['aux_error_mod'], f2['aux_error_mod']) assertion.eq(f1['param'].dtype, f2['param'].dtype) assertion.eq(f1['aux_error_mod'].dtype, f2['aux_error_mod'].dtype) # Validate that the non-charges are updated independently of each other with h5py.File(hdf5, 'r') as f1: _index = f1['param'].attrs['index'][0] != b"charge" index = np.nonzero(_index)[0] param = f1['param'][..., index] grad = param[:, 1:] - param[:, :-1] grad[grad < 1e-8] = 0 assertion.le(np.count_nonzero(grad, axis=-1), 2, post_process=np.all)
def test_armc_restart(name: str) -> None: """Test the restart functionality of :class:`ARMC` and :class:`ARMCPT`.""" file = PATH / name with open(file, 'r') as f: dct = yaml.load(f.read(), Loader=UniqueLoader) sub_iter = 5 super_iter = 8 i = 2 + (super_iter * 10 + sub_iter) armc, job_kwargs = dict_to_armc(dct) if name == 'armc_ref.yaml': REF = ARMC_REF armc.package_manager.hook = iter(load_results(ARMC_REF, n=1)[i:]) else: iterator = _get_phi_iter() def swap_phi(*args: Any, **kwargs: Any) -> List[Tuple[int, int]]: return next(iterator) REF = ARMCPT_REF armc.swap_phi = swap_phi armc.package_manager.hook = iter(load_results(ARMCPT_REF, n=3)[i:]) _prepare_restart(REF, PATH / job_kwargs['folder'], super_iter, sub_iter) hdf5_ref = REF / 'armc.hdf5' hdf5 = PATH / job_kwargs['folder'] / 'armc.hdf5' run_armc(armc, restart=True, **job_kwargs) assertion.assert_(next, armc.package_manager.hook, exception=StopIteration) with h5py.File(hdf5, 'r') as f1, h5py.File(hdf5_ref, 'r') as f2: compare_hdf5(f1, f2, skip={'/param', '/aux_error_mod'}) assertion.shape_eq(f1['param'], f2['param']) assertion.shape_eq(f1['aux_error_mod'], f2['aux_error_mod']) assertion.eq(f1['param'].dtype, f2['param'].dtype) assertion.eq(f1['aux_error_mod'].dtype, f2['aux_error_mod'].dtype)
from scm.plams import add_to_class, Cp2kJob from FOX.armc import dict_to_armc, run_armc @add_to_class(Cp2kJob) def get_runscript(self): inp, out = self._filename('inp'), self._filename('out') return f'cp2k.ssmp -i {inp} -o {out}' # Prepare the ARMC settings file = 'armc_new.yaml' with open(file, 'r') as f: dct = yaml.load(f.read(), Loader=yaml.FullLoader) try: armc, job_kwargs = dict_to_armc(dct) except Exception as ex: exc = ex raise ex # Start ARMC try: workdir = os.path.join(job_kwargs['path'], job_kwargs['folder']) shutil.rmtree(workdir) except FileNotFoundError: pass run_armc(armc, restart=False, **job_kwargs)
"""A Recipe for MM-MD parameter optimizations with CP2K 3.1 or 6.1.""" import yaml from FOX.armc import dict_to_armc, run_armc # Prepare the ARMC settings file = str(...) with open(file, 'r') as f: dct = yaml.load(f.read(), Loader=yaml.SafeLoader) armc, kwargs = dict_to_armc(dct) run_armc(armc, **kwargs)