Package lib :: Package structure :: Module mass
[hide private]
[frames] | no frames]

Source Code for Module lib.structure.mass

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2003-2008,2012-2013 Edward d'Auvergne                         # 
 4  # Copyright (C) 2008 Sebastien Morin                                          # 
 5  #                                                                             # 
 6  # This file is part of the program relax (http://www.nmr-relax.com).          # 
 7  #                                                                             # 
 8  # This program is free software: you can redistribute it and/or modify        # 
 9  # it under the terms of the GNU General Public License as published by        # 
10  # the Free Software Foundation, either version 3 of the License, or           # 
11  # (at your option) any later version.                                         # 
12  #                                                                             # 
13  # This program is distributed in the hope that it will be useful,             # 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
16  # GNU General Public License for more details.                                # 
17  #                                                                             # 
18  # You should have received a copy of the GNU General Public License           # 
19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Python module imports. 
24  from numpy import float64, zeros 
25  from warnings import warn 
26   
27  # relax module imports. 
28  from lib.periodic_table import periodic_table 
29  from lib.errors import RelaxError 
30  from lib.warnings import RelaxWarning 
31   
32   
33   
34 -def centre_of_mass(pos=None, elements=None, verbosity=1):
35 """Calculate and return the centre of mass for the given atomic coordinates. 36 37 @keyword pos: The list of atomic coordinates. 38 @type pos: list of lists of float 39 @keyword elements: The list of elements corresponding to the atoms. 40 @type elements: list of str 41 @keyword verbosity: The amount of text to print out. 0 results in no printouts, 1 the full amount. 42 @type verbosity: int 43 @return: The centre of mass vector and the mass. 44 @rtype: 3D list of floats, float 45 """ 46 47 # Print out. 48 if verbosity: 49 print("Calculating the centre of mass.") 50 51 # Initialise the centre of mass. 52 R = zeros(3, float64) 53 54 # Initialise the total mass. 55 M = 0.0 56 57 # Loop over all atoms. 58 for i in range(len(pos)): 59 # Atomic mass. 60 try: 61 mass = periodic_table.atomic_mass(elements[i]) 62 except RelaxError: 63 warn(RelaxWarning("Skipping the atom index %s as the element '%s' is unknown." % (i, elements[i]))) 64 65 # Total mass. 66 M = M + mass 67 68 # Sum of mass * position. 69 R = R + mass * pos[i] 70 71 # Normalise. 72 R = R / M 73 74 # Final printout. 75 if verbosity: 76 print(" Total mass: M = " + repr(M)) 77 print(" Centre of mass: R = " + repr(R)) 78 79 # Return the centre of mass and total mass 80 return R, M
81