~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to tools/i-pi/ipi/engine/cell.py

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Contains the classes which deal with the system box.
 
2
 
 
3
Copyright (C) 2013, Joshua More and Michele Ceriotti
 
4
 
 
5
This program is free software: you can redistribute it and/or modify
 
6
it under the terms of the GNU General Public License as published by
 
7
the Free Software Foundation, either version 3 of the License, or
 
8
(at your option) any later version.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program. If not, see <http.//www.gnu.org/licenses/>.
 
17
 
 
18
 
 
19
Used for implementing the minimum image convention.
 
20
 
 
21
Classes:
 
22
   Cell: Base cell class with the generic methods and attributes.
 
23
"""
 
24
 
 
25
__all__ = ['Cell']
 
26
 
 
27
import numpy as np
 
28
from ipi.utils.depend import *
 
29
from ipi.utils.mathtools import *
 
30
from ipi.utils import units
 
31
 
 
32
 
 
33
class Cell(dobject):
 
34
   """Base class to represent the simulation cell in a periodic system.
 
35
 
 
36
   This class has the base attributes required for either flexible or
 
37
   isotropic cell dynamics. Uses an upper triangular lattice vector matrix to
 
38
   represent the cell.
 
39
 
 
40
   Depend objects:
 
41
      h: An array giving the lattice vector matrix.
 
42
      ih: An array giving the inverse of the lattice vector matrix.
 
43
      V: The volume of the cell.
 
44
   """
 
45
 
 
46
   def __init__(self, h=None):
 
47
      """Initialises base cell class.
 
48
 
 
49
      Args:
 
50
         h: Optional array giving the initial lattice vector matrix. The
 
51
            reference cell matrix is set equal to this. Must be an upper
 
52
            triangular 3*3 matrix. Defaults to a 3*3 zeroes matrix.
 
53
      """
 
54
 
 
55
      if h is None:
 
56
         #h = np.identity(3,float)
 
57
         h = np.zeros((3,3), float)
 
58
      dset(self,"h",depend_array(name = 'h', value = h) )
 
59
 
 
60
      dset(self,"ih",
 
61
         depend_array(name = "ih", value = np.zeros((3,3),float),
 
62
            func=self.get_ih, dependencies=[dget(self,"h")]) )
 
63
      dset(self,"V",
 
64
         depend_value(name = 'V', func=self.get_volume,
 
65
            dependencies=[dget(self,"h")]) )
 
66
 
 
67
   def get_ih(self):
 
68
      """Inverts the lattice vector matrix."""
 
69
 
 
70
      return invert_ut3x3(self.h)
 
71
 
 
72
   def get_volume(self):
 
73
      """Calculates the volume of the system box."""
 
74
 
 
75
      return det_ut3x3(self.h)
 
76
 
 
77
   def apply_pbc(self, atom):
 
78
      """Uses the minimum image convention to return a particle to the
 
79
         unit cell.
 
80
 
 
81
      Args:
 
82
         atom: An Atom object.
 
83
 
 
84
      Returns:
 
85
         An array giving the position of the image that is inside the
 
86
         system box.
 
87
      """
 
88
 
 
89
      s = np.dot(self.ih,atom.q)
 
90
 
 
91
 
 
92
      for i in range(3):
 
93
         s[i] = s[i] - round(s[i])
 
94
 
 
95
      return np.dot(self.h,s)
 
96
 
 
97
   def array_pbc(self, pos):
 
98
      """Uses the minimum image convention to return a list of particles to the
 
99
         unit cell.
 
100
 
 
101
      Args:
 
102
         atom: An Atom object.
 
103
 
 
104
      Returns:
 
105
         An array giving the position of the image that is inside the
 
106
         system box.
 
107
      """
 
108
 
 
109
      s = depstrip(pos).copy()
 
110
      s.shape = (len(pos)/3,3)
 
111
 
 
112
      s = np.dot(depstrip(self.ih),s.T)
 
113
      s = s - np.round(s)
 
114
 
 
115
      s = np.dot(depstrip(self.h),s).T
 
116
 
 
117
      pos[:] = s.reshape((len(s)*3))
 
118
 
 
119
   def minimum_distance(self, atom1, atom2):
 
120
      """Takes two atoms and tries to find the smallest vector between two
 
121
      images.
 
122
 
 
123
      This is only rigorously accurate in the case of a cubic cell,
 
124
      but gives the correct results as long as the cut-off radius is defined
 
125
      as smaller than the smallest width between parallel faces even for
 
126
      triclinic cells.
 
127
 
 
128
      Args:
 
129
         atom1: An Atom object.
 
130
         atom2: An Atom object.
 
131
 
 
132
      Returns:
 
133
         An array giving the minimum distance between the positions of atoms
 
134
         atom1 and atom2 in the minimum image convention.
 
135
      """
 
136
 
 
137
      s = np.dot(self.ih,atom1.q-atom2.q)
 
138
      for i in range(3):
 
139
         s[i] -= round(s[i])
 
140
      return np.dot(self.h, s)