def test_read_write(self): """Tests read and write functionalities.""" # save to file temp_file = tempfile.NamedTemporaryFile() self.ce.write(temp_file.name) # read from file temp_file.seek(0) ce_read = ClusterExpansion.read(temp_file.name) # check cluster space self.assertEqual(self.cs._input_structure, ce_read._cluster_space._input_structure) self.assertEqual(self.cs._cutoffs, ce_read._cluster_space._cutoffs) self.assertEqual(self.cs._input_chemical_symbols, ce_read._cluster_space._input_chemical_symbols) # check parameters self.assertIsInstance(ce_read.parameters, np.ndarray) self.assertEqual(list(ce_read.parameters), list(self.parameters)) # check metadata self.assertEqual(len(self.ce.metadata), len(ce_read.metadata)) self.assertSequenceEqual(sorted(self.ce.metadata.keys()), sorted(ce_read.metadata.keys())) for key in self.ce.metadata.keys(): self.assertEqual(self.ce.metadata[key], ce_read.metadata[key])
def test_read_write_pruned(self): """Tests read and write functionalities.""" # save to file temp_file = tempfile.NamedTemporaryFile() self.ce.prune(indices=[2, 3]) self.ce.prune(tol=3) pruned_params = self.ce.parameters pruned_cs_len = len(self.ce._cluster_space) self.ce.write(temp_file.name) # read from file temp_file.seek(0) ce_read = ClusterExpansion.read(temp_file.name) params_read = ce_read.parameters cs_len_read = len(ce_read._cluster_space) # check cluster space self.assertEqual(cs_len_read, pruned_cs_len) self.assertEqual(list(params_read), list(pruned_params))
# This scripts runs in about 6 seconds on an i7-6700K CPU. import matplotlib.pyplot as plt from ase.db import connect from icet import ClusterExpansion # step 1: Compile predicted and reference data for plotting ce = ClusterExpansion.read('mixing_energy.ce') data = {'concentration': [], 'reference_energy': [], 'predicted_energy': []} db = connect('reference_data.db') for row in db.select('natoms<=6'): data['concentration'].append(row.concentration) # the factor of 1e3 serves to convert from eV/atom to meV/atom data['reference_energy'].append(1e3 * row.mixing_energy) data['predicted_energy'].append(1e3 * ce.predict(row.toatoms())) # step 2: Plot results fig, ax = plt.subplots(figsize=(4, 3)) ax.set_xlabel(r'Pd concentration') ax.set_ylabel(r'Mixing energy (meV/atom)') ax.set_xlim([0, 1]) ax.set_ylim([-69, 15]) ax.scatter(data['concentration'], data['reference_energy'], marker='o', label='reference') ax.scatter(data['concentration'], data['predicted_energy'], marker='x', label='CE prediction') plt.savefig('mixing_energy_comparison.png', bbox_inches='tight')
from ase.db import connect from icet import (StructureContainer, CrossValidationEstimator, ClusterExpansion) from mchammer.ensembles import SemiGrandCanonicalEnsemble as SGCEnsemble from mchammer.calculators import ClusterExpansionCalculator as CECalculator from mchammer.observers import ClusterExpansionObserver as CEObserver # step 1: Read cluster expansion for mixing energies from file ce_mix_energies = ClusterExpansion.read('mixing_energy.ce') chemical_symbols = ce_mix_energies.cluster_space.chemical_symbols[0] cs = ce_mix_energies.cluster_space # step 2: Read structures from database into a structure container db = connect('reference_data.db') sc = StructureContainer(cluster_space=cs) for row in db.select('natoms<=8'): sc.add_structure(structure=row.toatoms(), user_tag=row.tag, properties={'lattice_parameter': row.lattice_parameter}) # step 3: Construct cluster expansion for lattice parameter fit_data = sc.get_fit_data(key='lattice_parameter') opt = CrossValidationEstimator(fit_data=fit_data, fit_method='lasso') opt.validate() opt.train() ce_latt_param = ClusterExpansion(cluster_space=cs, parameters=opt.parameters) # step 4: Set up the calculator and a canonical ensemble structure = cs.primitive_structure.repeat(3) structure.set_chemical_symbols([chemical_symbols[0]] * len(structure)) calculator = CECalculator(structure=structure,