Author: bugman Date: Sun Mar 24 16:14:19 2013 New Revision: 19112 URL: http://svn.gna.org/viewcvs/relax?rev=19112&view=rev Log: Split the pipe_control.structure.mass module into two with the CoM code going to lib.structure.mass. Added: trunk/lib/structure/mass.py - copied, changed from r19105, trunk/pipe_control/structure/mass.py Modified: trunk/lib/structure/__init__.py trunk/pipe_control/structure/mass.py Modified: trunk/lib/structure/__init__.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/structure/__init__.py?rev=19112&r1=19111&r2=19112&view=diff ============================================================================== --- trunk/lib/structure/__init__.py (original) +++ trunk/lib/structure/__init__.py Sun Mar 24 16:14:19 2013 @@ -23,6 +23,7 @@ """The relax-lib structure package - a library of functions handling structural information.""" __all__ = [ + 'mass', 'pdb_read', 'pdb_write', 'rotor', Copied: trunk/lib/structure/mass.py (from r19105, trunk/pipe_control/structure/mass.py) URL: http://svn.gna.org/viewcvs/relax/trunk/lib/structure/mass.py?p2=trunk/lib/structure/mass.py&p1=trunk/pipe_control/structure/mass.py&r1=19105&r2=19112&rev=19112&view=diff ============================================================================== --- trunk/pipe_control/structure/mass.py (original) +++ trunk/lib/structure/mass.py Sun Mar 24 16:14:19 2013 @@ -24,31 +24,24 @@ from warnings import warn # relax module imports. -from pipe_control.mol_res_spin import return_molecule, return_residue, return_spin from lib.physical_constants import return_atomic_mass -from lib.errors import RelaxError, RelaxNoPdbError +from lib.errors import RelaxError from lib.warnings import RelaxWarning -def centre_of_mass(atom_id=None, model=None, return_mass=False, verbosity=1): - """Calculate and return the centre of mass of the structure. +def centre_of_mass(pos=None, elements=None, verbosity=1): + """Calculate and return the centre of mass for the given atomic coordinates. - @keyword atom_id: The molecule, residue, and atom identifier string. Only atoms matching this selection will be used. - @type atom_id: str or None - @keyword model: Only use a specific model. - @type model: int or None - @keyword return_mass: A flag which if False will cause only the centre of mass to be returned, but if True will cause the centre of mass and the mass itself to be returned as a tuple. - @type return_mass: bool + @keyword pos: The list of atomic coordinates. + @type pos: list of lists of float + @keyword elements: The list of elements corresponding to the atoms. + @type elements: list of str @keyword verbosity: The amount of text to print out. 0 results in no printouts, 1 the full amount. @type verbosity: int - @return: The centre of mass vector, and additionally the mass. - @rtype: list of 3 floats (or tuple of a list of 3 floats and one float) + @return: The centre of mass vector and the mass. + @rtype: 3D list of floats, float """ - - # Test if a structure has been loaded. - if not hasattr(cdp, 'structure'): - raise RelaxNoPdbError # Print out. if verbosity: @@ -61,43 +54,10 @@ M = 0.0 # Loop over all atoms. - for mol_name, res_num, res_name, atom_num, atom_name, element, pos in cdp.structure.atom_loop(atom_id=atom_id, model_num=model, mol_name_flag=True, res_num_flag=True, res_name_flag=True, atom_num_flag=True, atom_name_flag=True, element_flag=True, pos_flag=True, ave=True): - # Initialise the spin id string. - id = '' - - # Get the corresponding molecule container. - if mol_name == None: - mol_cont = cdp.mol[0] - else: - id = id + '#' + mol_name - mol_cont = return_molecule(id) - - # Get the corresponding residue container. - if res_name == None and res_num == None: - res_cont = mol_cont.res[0] - else: - id = id + ':' + repr(res_num) - res_cont = return_residue(id) - - # Get the corresponding spin container. - if atom_name == None and atom_num == None: - spin_cont = res_cont.spin[0] - else: - id = id + '@' + repr(atom_num) - spin_cont = return_spin(id) - - # Deselected spins. - if spin_cont and not spin_cont.select: - continue - - # No element? - if element == None: - warn(RelaxWarning("Skipping the atom '%s' as the element type cannot be determined." % id)) - continue - + for i in range(len(pos)): # Atomic mass. try: - mass = return_atomic_mass(element) + mass = return_atomic_mass(elements[i]) except RelaxError: warn(RelaxWarning("Skipping the atom '%s' as the element '%s' is unknown." % (id, element))) @@ -105,7 +65,7 @@ M = M + mass # Sum of mass * position. - R = R + mass * pos + R = R + mass * pos[i] # Normalise. R = R / M @@ -115,8 +75,5 @@ print(" Total mass: M = " + repr(M)) print(" Centre of mass: R = " + repr(R)) - # Return the centre of mass. - if return_mass: - return R, M - else: - return R + # Return the centre of mass and total mass + return R, M Modified: trunk/pipe_control/structure/mass.py URL: http://svn.gna.org/viewcvs/relax/trunk/pipe_control/structure/mass.py?rev=19112&r1=19111&r2=19112&view=diff ============================================================================== --- trunk/pipe_control/structure/mass.py (original) +++ trunk/pipe_control/structure/mass.py Sun Mar 24 16:14:19 2013 @@ -20,13 +20,12 @@ ############################################################################### # Python module imports. -from numpy import float64, zeros from warnings import warn # relax module imports. from pipe_control.mol_res_spin import return_molecule, return_residue, return_spin -from lib.physical_constants import return_atomic_mass -from lib.errors import RelaxError, RelaxNoPdbError +from lib.errors import RelaxNoPdbError +from lib import mass from lib.warnings import RelaxWarning @@ -50,17 +49,9 @@ if not hasattr(cdp, 'structure'): raise RelaxNoPdbError - # Print out. - if verbosity: - print("Calculating the centre of mass.") - - # Initialise the centre of mass. - R = zeros(3, float64) - - # Initialise the total mass. - M = 0.0 - # Loop over all atoms. + coord = [] + element_list = [] for mol_name, res_num, res_name, atom_num, atom_name, element, pos in cdp.structure.atom_loop(atom_id=atom_id, model_num=model, mol_name_flag=True, res_num_flag=True, res_name_flag=True, atom_num_flag=True, atom_name_flag=True, element_flag=True, pos_flag=True, ave=True): # Initialise the spin id string. id = '' @@ -95,28 +86,15 @@ warn(RelaxWarning("Skipping the atom '%s' as the element type cannot be determined." % id)) continue - # Atomic mass. - try: - mass = return_atomic_mass(element) - except RelaxError: - warn(RelaxWarning("Skipping the atom '%s' as the element '%s' is unknown." % (id, element))) + # Store the position and element. + coord.append(pos) + element_list.append(element) - # Total mass. - M = M + mass - - # Sum of mass * position. - R = R + mass * pos - - # Normalise. - R = R / M - - # Final printout. - if verbosity: - print(" Total mass: M = " + repr(M)) - print(" Centre of mass: R = " + repr(R)) + # Calculate the CoM. + com, mass = mass.centre_of_mass(pos=coord, elements=element_list) # Return the centre of mass. if return_mass: - return R, M + return com, mass else: - return R + return com