Package diffpy :: Package Structure :: Package expansion :: Module makeEllipsoid
[hide private]
[frames] | no frames]

Source Code for Module diffpy.Structure.expansion.makeEllipsoid

  1  #!/usr/bin/env python 
  2  ############################################################################## 
  3  # 
  4  # PDFgui            by DANSE Diffraction group 
  5  #                   Simon J. L. Billinge 
  6  #                   (c) 2008 trustees of the Michigan State University. 
  7  #                   All rights reserved. 
  8  # 
  9  # File coded by:    Chris Farrow 
 10  # 
 11  # See AUTHORS.txt for a list of people who contributed. 
 12  # See LICENSE.txt for license information. 
 13  # 
 14  ############################################################################## 
 15   
 16  """Make a spheroid nanoparticle from a template structure.""" 
 17   
 18  __id__ = "$Id: makeEllipsoid.py 3032 2009-04-08 19:15:37Z juhas $" 
 19   
 20  from math import ceil 
 21  from numpy import array 
 22  from diffpy.Structure import Structure, Atom 
 23  from diffpy.Structure.expansion.shapeUtils import findCenter 
 24   
25 -def makeSphere(S, radius):
26 """Create a spherical nanoparticle. 27 28 Arguments 29 S -- A Structure instance 30 radius -- primary equatorial radius (along x-axis) 31 32 Returns a new structure instance 33 """ 34 return makeEllipsoid(S, radius)
35
36 -def makeEllipsoid(S, a, b=None, c=None):
37 """Cut a structure out of another one. 38 39 Arguments 40 S -- A Structure instance 41 a -- primary equatorial radius (along x-axis) 42 b -- secondary equatorial radius (along y-axis). If b is None 43 (default) then it is set equal to a 44 c -- polar radius (along z-axis). If c is None (default), then it is 45 set equal to a. 46 47 Returns a new structure instance 48 """ 49 if b is None: b = a 50 if c is None: c = a 51 sabc = array([a, b, c]) 52 53 # Create a supercell large enough for the ellipsoid 54 frac = S.lattice.fractional(sabc) 55 mno = map(ceil, 2*frac) 56 mno = max(map(ceil, 2*frac))*array([1,1,1]) 57 # Make the supercell 58 from diffpy.Structure.expansion import supercell 59 newS = supercell(S, mno) 60 lat = newS.lattice 61 62 # Find the central atom 63 ncenter = findCenter(newS) 64 65 cxyz = lat.cartesian(newS[ncenter].xyz) 66 67 delList = [] 68 N = len(newS) 69 j = N 70 for i in xrange(N): 71 j -= 1 72 73 # Calculate (x/a)**2 + (y/b)**2 + (z/c)**2 74 xyz = lat.cartesian(newS[j].xyz) 75 darray = ((xyz-cxyz)/sabc)**2 76 d = sum(darray)**0.5 77 78 # Discard atom if (x/a)**2 + (y/b)**2 + (z/c)**2 > 1 79 if d > 1: 80 delList.append(j) 81 82 for i in delList: 83 newS.pop(i) 84 85 return newS
86 87 if __name__ == "__main__": 88 89 import os.path 90 datadir = "../../tests/testdata" 91 S = Structure() 92 S.read(os.path.join(datadir, "CdSe_bulk.stru"), "pdffit") 93 newS = makeEllipsoid(S, 12) 94 newS.write("CdSe_d24.stru", "pdffit") 95 newS = makeEllipsoid(S, 20, 10, 10) 96 newS.write("CdSe_a20_b10_c10.stru", "pdffit") 97 newS = makeEllipsoid(S, 20, 15, 10) 98 newS.write("CdSe_a20_b15_c10.stru", "pdffit") 99 S = Structure() 100 S.read(os.path.join(datadir, "Ni.stru"), "pdffit") 101 newS = makeEllipsoid(S, 10) 102 newS.write("Ni_d20.stru", "pdffit") 103 newS = makeEllipsoid(S, 20, 4) 104 newS.write("Ni_a20_b4_c20.stru", "pdffit") 105 newS = makeEllipsoid(S, 20, 15, 10) 106 newS.write("Ni_a20_b15_c10.stru", "pdffit") 107