BUGFIX: Make FixedMode work with dict2constraint.
And add a test to keep it working.
Merge request reports
Activity
- ase/test/constraints/test_fixedmode.py 0 → 100644
9 from ase.constraints import FixedMode, dict2constraint 10 11 # Create a vibrational mode. 12 atoms = molecule('N2') 13 atoms.calc = EMT() 14 opt = BFGS(atoms) 15 opt.run(fmax=0.01) 16 vib = Vibrations(atoms) 17 vib.run() 18 mode = vib.get_mode(-1) 19 20 # Test the constraint. 21 constraint = FixedMode(mode) 22 dict_constraint = constraint.todict() 23 new_constraint = dict2constraint(dict_constraint) 24 assert np.isclose(new_constraint.mode, constraint.mode).all() We're testing whether the constraint can be encoded/decoded. This doesn't require running a calculation (which then depends on a lot of stuff which we are not testing). It only requires creating the constraint then encoding/decoding it.
Removing the vibrations run will also remove the need to write a file, which is what causes the PermissionError. (The other way to remove the PermissionError is to depend on the
testdir
fixture so a directory is created for the test, and then it's free to leave files inside that.That being said, the FixedMode constraint is not tested at all, so indeed it makes sense to have an integration test of it. A very good test would be to 1) put atoms in random positions, 2) specify a random mode, then set_positions() to
positions + <some random positions>
, then verify that the atoms only moved along the mode.Edited by Ask Hjorth LarsenThanks! I seem to remember the
testdir
from some other commit, but I clearly forgot it since then. The command is kind of magical to me. Is it documented somewhere? I can't find it.I changed the test to something similar to what you desribed before I saw this message. I'll just go ahead and change it to what you describe and commit one more time.
Thanks @andrew_peterson!
mentioned in commit 748e0c5d