Package pyxsd :: Package transforms :: Module sphereCutter
[hide private]
[frames] | no frames]

Source Code for Module pyxsd.transforms.sphereCutter

 1  from cellSizer import CellSizer 
 2   
3 -class SphereCutter(CellSizer):
4 5 #============================================================ 6 #
7 - def __init__(self, root):
8 self.root = root 9 self.cellSizerInit()
10 #============================================================ 11 #
12 - def __call__(self, rad, sphereCenter=None):
13 #sphereCenter is the center of the material if left None, 14 #and it is (x, y, z) in Cartesian Coords if it is not. 15 return self.cut(rad, sphereCenter)
16 17 #============================================================ 18 #
19 - def testSphereMembership(self, coords, sphereCenter, rad):
20 calc = 0.0 21 for f in range(0, 3): 22 cms = coords[f]-sphereCenter[f] 23 calc+=pow(cms, 2) 24 test = (self.radSquared >= calc) 25 return test
26 27 #============================================================ 28 #
29 - def cut(self, rad, sphereCenter=None):
30 vectorDict = self.getBravaisVectors() 31 vectors = [] 32 self.radSquared = pow(rad, 2) 33 for vectorName in self.vectorOrder: 34 vectors.append(vectorDict[vectorName]) 35 atoms = self.getAtoms() 36 newAtoms = [] 37 if not isinstance(rad, float): 38 try: 39 rad = float(rad) 40 except: 41 raise TypeError, "the radius of a sphere must be expressed as a number" 42 if not sphereCenter: 43 sphereCenter = self.findCenter(vectors) 44 else: 45 if not len(sphereCenter) == 3 or not isinstance(sphereCenter, tuple): 46 raise TypeError, "there must be three coords for the center of a sphere and they must be in a tuple" 47 for atom in atoms: 48 atomPos = atom.position 49 atomCoords = self.getCartesianCoords(vectors, atomPos) 50 if self.testSphereMembership(atomCoords, sphereCenter, rad): 51 newAtoms.append(atom) 52 53 54 if len(newAtoms) == 0: 55 print "Sphere Cutter Error: no atoms remain after the cut. Please check the data." 56 elif len(newAtoms) == len(atoms): 57 print "Sphere Cutter Error: no atoms were removed. Please check your data." 58 59 newLattice = self.makeBravaisLattice(vectorDict, newAtoms) 60 return self.makeNewXml(newLattice)
61