18.11.2012 Views

ASE Manual Release 3.6.1.2825 CAMd - CampOS Wiki

ASE Manual Release 3.6.1.2825 CAMd - CampOS Wiki

ASE Manual Release 3.6.1.2825 CAMd - CampOS Wiki

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ASE</strong> <strong>Manual</strong>, <strong>Release</strong> 3.6.1.2828<br />

7.15.10 Making your own constraint class<br />

A constraint class must have these two methods:<br />

constraints.adjust_positions(oldpositions, newpositions)<br />

Adjust the newpositions array inplace.<br />

constraints.adjust_forces(positions, forces)<br />

Adjust the forces array inplace.<br />

A simple example:<br />

import numpy as np<br />

class MyConstraint:<br />

"""Constrain an atom to move along a given direction only."""<br />

def __init__(self, a, direction):<br />

self.a = a<br />

self.dir = direction / sqrt(np.dot(direction, direction))<br />

def adjust_positions(self, oldpositions, newpositions):<br />

step = newpositions[self.a] - oldpositions[self.a]<br />

step = np.dot(step, self.dir)<br />

newpositions[self.a] = oldpositions[self.a] + step * self.dir<br />

def adjust_forces(self, positions, forces):<br />

forces[self.a] = self.dir * np.dot(forces[self.a], self.dir)<br />

7.15.11 The Filter class<br />

Constraints can also be applied via filters, which acts as a wrapper around an atoms object. A typical use case will<br />

look like this:<br />

------- -------- ----------<br />

| | | | | |<br />

| Atoms |> atoms = Atoms(...)<br />

>>> filter = Filter(atoms, ...)<br />

>>> dyn = Dynamics(filter, ...)<br />

This class hides some of the atoms in an Atoms object.<br />

class constraints.Filter(atoms, indices=None, mask=None)<br />

You must supply either the indices of the atoms that should be kept visible or a mask. The mask is a list of<br />

booleans, one for each atom, being true if the atom should be kept visible.<br />

Example of use:<br />

>>> from ase import Atoms, Filter<br />

>>> atoms=Atoms(positions=[[ 0 , 0 , 0],<br />

... [ 0.773, 0.600, 0],<br />

... [-0.773, 0.600, 0]],<br />

... symbols=’OH2’)<br />

>>> f1 = Filter(atoms, indices=[1, 2])<br />

>>> f2 = Filter(atoms, mask=[0, 1, 1])<br />

>>> f3 = Filter(atoms, mask=[a.Z == 1 for a in atoms])<br />

>>> f1.get_positions()<br />

[[ 0.773 0.6 0. ]<br />

[-0.773 0.6 0. ]]<br />

142 Chapter 7. Documentation for modules in <strong>ASE</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!