Coverage for ase / constraints / fix_atoms.py: 100.00%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-30 08:22 +0000

1from ase.constraints.constraint import IndexedConstraint, ints2string 

2 

3 

4class FixAtoms(IndexedConstraint): 

5 """Fix chosen atoms. 

6 

7 Examples 

8 -------- 

9 Fix all Copper atoms: 

10 

11 >>> from ase.build import bulk 

12 

13 >>> atoms = bulk('Cu', 'fcc', a=3.6) 

14 >>> mask = (atoms.symbols == 'Cu') 

15 >>> c = FixAtoms(mask=mask) 

16 >>> atoms.set_constraint(c) 

17 

18 Fix all atoms with z-coordinate less than 1.0 Angstrom: 

19 

20 >>> c = FixAtoms(mask=atoms.positions[:, 2] < 1.0) 

21 >>> atoms.set_constraint(c) 

22 """ 

23 

24 def get_removed_dof(self, atoms): 

25 return 3 * len(self.index) 

26 

27 def adjust_positions(self, atoms, new): 

28 new[self.index] = atoms.positions[self.index] 

29 

30 def adjust_forces(self, atoms, forces): 

31 forces[self.index] = 0.0 

32 

33 def __repr__(self): 

34 clsname = type(self).__name__ 

35 indices = ints2string(self.index) 

36 return f'{clsname}(indices={indices})' 

37 

38 def todict(self): 

39 return {'name': 'FixAtoms', 'kwargs': {'indices': self.index.tolist()}}