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

Source Code for Module pyxsd.transforms.cellSizer

  1  from transform      import Transform 
  2  from atom           import Atom 
  3  from vector         import Vector 
  4  from bravaisLattice import BravaisLattice 
  5   
  6  """ 
  7  ============================ 
  8  Transform Library: CellSizer 
  9  ============================ 
 10   
 11  :Author: Karl Norby <knorby@uchicago.edu> 
 12  :Date: Fri, 1 Sept 2006 
 13  :Category: Computational Materials Science 
 14  :Description: Library containing functions to change the size and shape of a crystal lattice structure 
 15  :Copyright: pyXSD License 
 16   
 17  Includes as part of the CellSizer Library: 
 18   
 19  - Atom 
 20  - Vector 
 21  - BravaisLattice 
 22   
 23  """ 
 24   
25 -class CellSizer (Transform):
26 27 #============================================================ 28 #
29 - def cellSizerInit(self):
30 self.atoms = [] 31 self.bravaisVectors = {} 32 self.vectorOrder = []
33 34 #============================================================ 35 #
36 - def getBravaisVectors(self):
37 if not len(self.bravaisVectors) == 0: 38 return self.bravaisVectors 39 vectorNumCount = 1 40 self.vectorOrder = [] 41 vectorsInXml = self.getElementsByName(self.root, 'bravaisVector') 42 for vector in vectorsInXml: 43 vectorDef = vector._value_ 44 if not len(vectorDef) == 3: 45 raise "For now, the getBravaisVectors() method is only meant for 3D vectors" 46 i = float(vectorDef[0]) 47 j = float(vectorDef[1]) 48 k = float(vectorDef[2]) 49 obj = Vector((i, j, k)) 50 self.bravaisVectors['a%i' % vectorNumCount] = obj 51 self.vectorOrder.append('a%i' % vectorNumCount) 52 vectorNumCount+=1 53 return self.bravaisVectors
54 55 #============================================================ 56 #
57 - def getVectorList(self):
58 vectorDict = self.getBravaisVectors() 59 vectors = [] 60 for vectorName in self.vectorOrder: 61 vectors.append(vectorDict[vectorName]) 62 return vectors
63 64 65 #============================================================ 66 #
67 - def getAtoms(self):
68 if not len(self.atoms) == 0: 69 return self.atoms 70 atomsInXml = self.getElementsByName(self.root, 'site') 71 for atom in atomsInXml: 72 atomDict = self.getAllSubElements(atom) 73 position = atomDict['position'][0]._value_ 74 atomType = atomDict['atom'][0]._attribs_['ref'] 75 obj = Atom(position, atomType) 76 self.atoms.append(obj) 77 return self.atoms
78 79 #============================================================ 80 #
81 - def makeNewXml (self, bravaisLattice):
82 vectors = bravaisLattice.vectors 83 basis = bravaisLattice.basis 84 xmlCrystalBasis = self.getElementsByName(self.root, 'crystalBasis')[0] 85 xmlCrystalBasis._children_ = [] 86 for atom in basis: 87 newAtomElement = self.makeNewXmlAtomElements(atom) 88 xmlCrystalBasis._children_.append(newAtomElement) 89 xmlBravaisLattice = self.getElementsByName(self.root, 'bravaisLattice')[0] 90 xmlBravaisLattice._children_ = [] 91 for vectorName in self.vectorOrder: 92 vector = vectors[vectorName] 93 bravaisVector = self.makeElemObj('bravaisVector') 94 bravaisVector._value_ = vector 95 xmlBravaisLattice._children_.append(bravaisVector) 96 continue 97 98 return self.root
99 100 #============================================================ 101 #
102 - def makeNewXmlAtomElements(self, atom):
103 104 position = atom.position 105 atomType = atom.atomType 106 siteObj = self.makeElemObj('site') 107 positionObj = self.makeElemObj('position') 108 positionObj._value_ = position 109 occupantObj = self.makeElemObj('occupant') 110 atomObj = self.makeElemObj('atom') 111 atomObj._attribs_['ref'] = atomType 112 113 occupantObj._children_.append(atomObj) 114 siteObj._children_.append(occupantObj) 115 siteObj._children_.append(positionObj) 116 117 return siteObj
118 #============================================================ 119 #
120 - def makeAtom(self, position, atomType):
121 return Atom(position, atomType)
122 #============================================================ 123 #
124 - def makeBravaisLattice(self, newVectors, newAtoms):
125 return BravaisLattice(newVectors, newAtoms)
126 127 #============================================================ 128 #
129 - def getCartesianCoords(self, vectors, position):
130 coords = [] 131 for g in range(0, 3): 132 total = 0 133 currentPos = position[g] 134 for vector in vectors: 135 total+= currentPos * vector[g] 136 coords.append(total) 137 return coords
138 139 #============================================================ 140 #
141 - def findCenter(self, vectors):
142 centerPos = [.5, .5, .5] #in terms of vector 143 return self.getCartesianCoords(vectors, centerPos) #centerPos
144