Helpful error message when applying canonical ensemble to pure supercells
Description
As a user, I would find it very useful to get a more sensible error message when trying to run a MC simulation starting from a non-mixed configuration.
Example
The following code
from ase.build import bulk
from icet import ClusterExpansion, ClusterSpace
from mchammer.calculators import ClusterExpansionCalculator
from mchammer.ensembles import CanonicalEnsemble
prim = bulk('Au')
cs = ClusterSpace(prim, cutoffs=[6.0, 4.5], chemical_symbols=['Ag', 'Au'])
ce = ClusterExpansion(cs, [0.5]*len(cs))
atoms = prim.repeat(3)
calc = ClusterExpansionCalculator(atoms, ce)
mc = CanonicalEnsemble(atoms=atoms, calculator=calc, temperature=600,
data_container='myrun_canonical.dc')
mc.run(100)
produces the following output
Traceback (most recent call last):
File "example_canonical_ensemble.py", line 17, in <module>
mc.run(100)
File "/Users/erhart/repos/icet/mchammer/ensembles/base_ensemble.py", line 228, in run
self._run(uninterrupted_steps)
File "/Users/erhart/repos/icet/mchammer/ensembles/base_ensemble.py", line 249, in _run
self._do_trial_step()
File "/Users/erhart/repos/icet/mchammer/ensembles/canonical_ensemble.py", line 133, in _do_trial_step
sublattice_index = self.get_random_sublattice_index()
File "/Users/erhart/repos/icet/mchammer/ensembles/canonical_ensemble.py", line 175, in get_random_sublattice_index
probability_distribution = [p/norm for p in probability_distribution]
File "/Users/erhart/repos/icet/mchammer/ensembles/canonical_ensemble.py", line 175, in <listcomp>
probability_distribution = [p/norm for p in probability_distribution]
ZeroDivisionError: division by zero
The error arises since the initial configuration does not included any Ag atoms.
Desired behavior
The ensemble should check on initialization whether the configuration at hand can be evolved by swaps.
If this is not the case it should return None
and print a suitable error message.
Demo
- check implemented
- tests updated (all new lines must be covered)
Edited by Paul Erhart