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
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-30 08:22 +0000
1from ase.constraints.constraint import IndexedConstraint, ints2string
4class FixAtoms(IndexedConstraint):
5 """Fix chosen atoms.
7 Examples
8 --------
9 Fix all Copper atoms:
11 >>> from ase.build import bulk
13 >>> atoms = bulk('Cu', 'fcc', a=3.6)
14 >>> mask = (atoms.symbols == 'Cu')
15 >>> c = FixAtoms(mask=mask)
16 >>> atoms.set_constraint(c)
18 Fix all atoms with z-coordinate less than 1.0 Angstrom:
20 >>> c = FixAtoms(mask=atoms.positions[:, 2] < 1.0)
21 >>> atoms.set_constraint(c)
22 """
24 def get_removed_dof(self, atoms):
25 return 3 * len(self.index)
27 def adjust_positions(self, atoms, new):
28 new[self.index] = atoms.positions[self.index]
30 def adjust_forces(self, atoms, forces):
31 forces[self.index] = 0.0
33 def __repr__(self):
34 clsname = type(self).__name__
35 indices = ints2string(self.index)
36 return f'{clsname}(indices={indices})'
38 def todict(self):
39 return {'name': 'FixAtoms', 'kwargs': {'indices': self.index.tolist()}}