def scaling_motion(self,x,y,xold,yold): newpos = array((x,y)) oldpos = array((xold,yold)) (dx,dy)=newpos-oldpos scale = 1 + float(dy)/self.get_view_height() self.set_position(scale*array(self.get_position())) return
def __init__(self,xyz1,xyz2,xyz3): self.xyz1 = array(xyz1) self.xyz2 = array(xyz2) self.xyz3 = array(xyz3) v12 = self.xyz2-self.xyz1 v13 = self.xyz3-self.xyz1 self.simplenorm = norm(cross(v12,v13)) self.norm1 = self.simplenorm self.norm2 = self.simplenorm self.norm3 = self.simplenorm return
def translation_motion(self,x,y,xold,yold): newpos = array((x,y)) oldpos = array((xold,yold)) oldpos_model = self.model_coords(oldpos[0],oldpos[1]) newpos_model = self.model_coords(newpos[0],newpos[1]) dp = oldpos_model - newpos_model up=array(self.get_up()) right=array(self.get_right()) t_up=(dot(dp,up))*up; t_right=(dot(dp,right))*right; self.set_look_at(self.get_look_at()+t_up+t_right); return
def rotation_motion(self,x,y,xold,yold): newpos = array((x,y)) oldpos = array((xold,yold)) diff_pos=newpos-oldpos r=sqrt(dot(diff_pos,diff_pos)) if r==0: return diff_pos=diff_pos/r angle=r*self.rotate_scale rot_mat=self.rotation_matrix(diff_pos,angle); self.set_up(matrixmultiply(rot_mat,array(self.get_up()))[:]) self.set_right(matrixmultiply(rot_mat,array(self.get_right()))[:]) self.set_position(matrixmultiply(rot_mat, array(self.get_position()))[:]) return
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material = Material(fileprefix) file = open(fullfilename, 'r') line = file.readline() title = line.strip() line = file.readline() title2 = line.strip() line = file.readline() title3 = line.strip() line = file.readline() natoms = int(line[:3]) nbonds = int(line[3:6]) # Note: I'm skipping the H information here, which is # typically given in fields 5- for i in range(natoms): line = file.readline() words = line.split() xyz = array(map(float, words[:3])) sym = cleansym(words[3]) atno = sym2no[sym] material.add_atom(Atom(atno, xyz, sym, sym + str(i))) atoms = material.get_atoms() for i in range(nbonds): line = file.readline() words = line.split() # bond order is the third field, which I'm ignoring iat, jat, iorder = map(int, words[:3]) material.add_bond(Bond(atoms[iat - 1], atoms[jat - 1], iorder)) return material
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material=Material(fileprefix) file=open(fullfilename, 'r') first_run = 1 while 1: line = file.readline() if not line: break words = line.split() nat = int(words[0]) comment = file.readline() if first_run: first_run = 0 else: material.new_geo() for i in range(nat): line = file.readline() words=line.split() sym=cleansym(words[0]) atno = sym2no[sym] xyz = array(map(float,words[1:4])) material.add_atom(Atom(atno, xyz, sym, sym+str(i))) material.bonds_from_distance() return material
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material = Material(fileprefix) file = open(fullfilename, 'r') first_run = True while 1: line = file.readline() if not line: break words = line.split() nat = int(words[0]) comment = file.readline() ax, ay, az, bx, by, bz, cx, cy, cz = [ float(i) for i in comment.split() ] cell = Cell((ax, ay, az), (bx, by, bz), (cx, cy, cz)) if first_run: first_run = False else: material.new_geo() material.set_cell(cell) for i in range(nat): line = file.readline() words = line.split() sym = cleansym(words[0]) atno = sym2no[sym] xyz = array(map(float, words[1:4])) material.add_atom(Atom(atno, xyz, sym, sym + str(i))) material.bonds_from_distance() return material
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material = Material(fileprefix) file = open(fullfilename, 'r') atoms = [] bfs = [] orbs = [] mode = None for line in file.readlines(): if atpat.search(line): mode = 'atoms' elif bfpat.search(line): mode = 'bfs' elif orbpat.search(line): mode = 'orbs' elif mode == 'atoms': atoms.append(line) elif mode == 'bfs': bfs.append(line) elif mode == 'orbs': orbs.append(line) else: print '? ', line, for line in atoms: words = line.split() if len(words) < 6: continue atno = int(words[2]) xyz = map(float, words[3:6]) material.add_atom(Atom(atno, array(xyz))) material.bonds_from_distance() material.geo.basis = parse_basis(bfs) material.geo.orbs = parse_orbs(orbs) return material
def add_the_unit_cell(material, buffer): name = material.get_name() cell = material.get_cell() atoms = material.get_atom_list() if cell: return material # Don't do anything for now newname = "%s_cell" % name newmaterial = Material(newname) if not buffer: buffer = 2. # Can still set to a small value like 0.01 xmin, xmax, ymin, ymax, zmin, zmax = bbox_atoms(atoms, buffer) for atom in atoms: atno = atom.get_atno() xyz = atom.get_position() xyznew = array((xyz[0] - xmin, xyz[1] - ymin, xyz[2] - zmin)) newmaterial.add_atom(Atom(atno, xyznew)) newmaterial.set_cell( Cell((xmax - xmin, 0, 0), (0, ymax - ymin, 0), (0, 0, zmax - zmin))) opts = getattr(material, "seqquest_options", {}) if opts: newmaterial.seqquest_options = opts.copy() opts = getattr(material, "socorro_options", {}) if opts: newmaterial.socorro_options = opts.copy() newmaterial.bonds_from_distance() return newmaterial
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material=Material(fileprefix) file=open(fullfilename, 'r') first_run = 1 while 1: line = file.readline() if not line: break if not geostart.search(line): continue if first_run: first_run = 0 else: material.new_geo() i = 1 while 1: line = file.readline() if geoend.search(line): break match = atpat.match(line) sym = match.group(1) x = float(match.group(2)) y = float(match.group(3)) z = float(match.group(4)) sym=cleansym(sym) atno = sym2no[sym] xyz = array((x,y,z)) material.add_atom(Atom(atno, xyz, sym, sym+str(i))) i += 1 material.bonds_from_distance() return material
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material = Material(fileprefix) file = open(fullfilename, 'r') line = file.readline() title = line.strip() line = file.readline() natoms, nbonds = map(int, line.split()) for i in range(natoms): line = file.readline() words = line.split() xyz = array(map(float, words[:3])) sym = cleansym(words[3]) atno = sym2no[sym] material.add_atom(Atom(atno, xyz, sym, sym + str(i))) atoms = material.get_atoms() for i in range(nbonds): line = file.readline() words = line.split() # I think order is the third one, but I'm not sure iat, jat, iorder = map(int, words[:3]) material.add_bond(Bond(atoms[iat - 1], atoms[jat - 1], iorder)) return material
def func(x, y, z): xyz = array((x, y, z)) #xyzl = sqrt(dot(xyz,xyz)) #xyz = xyz/xyzl al = dot(self.axyz, self.axyz) bl = dot(self.bxyz, self.bxyz) cl = dot(self.cxyz, self.cxyz) return dot(xyz, self.axyz) / al, dot(xyz, self.bxyz) / bl, dot( xyz, self.cxyz) / cl
def build_the_crystal(crystal_type, sym1, sym2, a, c_a): uc, atoms = builders[crystal_type](a, c_a, sym1, sym2) material = Material(crystal_type.replace(' ', '_')) for sym, (x, y, z) in atoms: atno = sym2no[sym] material.add_atom(Atom(atno, array((x, y, z)))) cell = Cell(uc[0], uc[1], uc[2]) material.set_cell(cell) material.bonds_from_distance() return material
def build_the_crystal_from_db(crystal_type): uc, atoms = crystal_type_dict[crystal_type] material = Material(crystal_type.replace(' ', '_')) for sym, (x, y, z) in atoms: atno = sym2no[sym] material.add_atom(Atom(atno, array((x, y, z)), sym)) cell = Cell(uc[0], uc[1], uc[2]) material.set_cell(cell) material.bonds_from_distance() return material
def parse_cart(lines): material = Material("vimm") for line in lines: words = line.split() if not words: continue sym = str(words[0]) atno = sym2no[sym] xyz = array(map(float, words[1:4])) material.add_atom(Atom(atno, xyz, sym, sym)) material.bonds_from_distance() return material
def __init__(self, axyz, bxyz, cxyz, **opts): self.scaleA = opts.get('scaleA', 1.0) self.scaleB = opts.get('scaleB', 1.0) self.scaleC = opts.get('scaleC', 1.0) verbose = opts.get('verbose', False) if verbose: print "Forming Cell" print axyz print bxyz print cxyz print self.scaleA print self.scaleB print self.scaleC self.axyz = array(axyz) * self.scaleA self.bxyz = array(bxyz) * self.scaleB self.cxyz = array(cxyz) * self.scaleC self.origin = array([0, 0, 0]) self.aLabel = opts.get('aLabel', 'A') self.bLabel = opts.get('bLabel', 'B') self.cLabel = opts.get('cLabel', 'C') self.originLabel = opts.get('originLabel', 'O') return
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material=Material(fileprefix) file=open(fullfilename, 'r') hatpat = re.compile('HETATM') atpat = re.compile('^ATOM') conpat = re.compile('^CONECT') ordpat = re.compile('^ORDER') endpat = re.compile('^END') ucpat = re.compile('^CRYSTX') bonds = {} orders = {} iat = 0 for line in open(fullfilename): if hatpat.search(line) or atpat.search(line): d1,i,d2,d3,d4,d5,x,y,z,attype,d6,d7,q = read(line,atom_format) xyz = array([x,y,z]) sym = cleansym(attype) atno = sym2no[sym] atom = Atom(atno,xyz,sym,sym+str(iat)) atom.fftype = attype # save just in case material.add_atom(atom) iat += 1 elif conpat.search(line): ats = map(int,line[6:].split()) index = ats[0]-1 bonds[index] = [atj-1 for atj in ats[1:]] orders[index] = [1]*(len(ats)-1) elif ordpat.search(line): ords = map(int,line[6:].split()) index = ords[0]-1 orders[index] = ords[1:] elif ucpat.search(line): words = line.split() a,b,c,alpha,beta,gamma = map(float,words[1:7]) axyz,bxyz,cxyz = abcabg2abc(a,b,c,alpha,beta,gamma) cell = Cell(axyz,bxyz,cxyz) material.set_cell(cell) atoms = material.get_atoms() #print len(atoms)," atoms loaded" for iat in bonds.keys(): bond_partners = bonds[iat] bond_orders = orders[iat] for jat,ij_order in zip(bond_partners,bond_orders): if jat > iat: material.add_bond(Bond(atoms[iat],atoms[jat], ij_order)) return material
def rotation_matrix(self,diffpos,alpha): # diffpos given in model coords #r=dot(diffpos,diffpos); dx,dy=diffpos; up=array(self.get_up()); right=array(self.get_right()); u=-(dy*right)-(dx*up); u=u/sqrt(dot(u,u)); # normalize the vector (a,b,c)=u # # construct the S and M matrices, see # OpenGL Programming Guide, p. 672 # # The M matrix is the rotation matrix # S=array((( 0, -c, b), ( c, 0, -a), (-b, a, 0))); # # u_uT is u times its transpose (to give a 3x3 matrix) # u_uT=matrixmultiply(reshape(u,(3,1)),reshape(u,(1,3))); M=u_uT+cos(alpha)*(identity(3)-u_uT)+sin(alpha)*S return M;
def get_geo(file): geo = [] lines2skip = 1 for i in range(lines2skip): file.readline() while 1: line = file.readline() if not line: break words = string.split(line) if len(words) < 6: break #sym = cleansym(words[1]) # do we need cleansim? sym = cleansym(words[1]) atno = int(float(words[2])) xyz = array(map(float, words[3:6])) geo.append((atno, xyz)) return geo
def cell_change(self, evt): row = evt.GetRow() col = evt.GetCol() if col == 0: val = self.grdCartesians.GetCellValue(row,col) self.material.geo.atoms[row].set_label(val) self.redraw() elif col == 1: print "You probably shouldn't edit this" else: new_x = float(self.grdCartesians.GetCellValue(row, 2)) new_y = float(self.grdCartesians.GetCellValue(row, 3)) new_z = float(self.grdCartesians.GetCellValue(row, 4)) new_xyz = array([new_x, new_y, new_z]) self.material.geo.atoms[row].set_xyz(new_xyz) self.redraw()
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material = Material(fileprefix) parser = CMLParser() geos = parser.process(fullfilename) molecule = geos[0] atoms = molecule.atoms cell = molecule.cell nat = len(atoms) for i in range(nat): sym, pos = atoms[i] pos = array(pos) atno = sym2no[sym] material.add_atom(Atom(atno, pos, sym, sym + str(i))) if cell: the_cell = Cell(cell[0], cell[1], cell[2]) material.set_cell(the_cell) material.bonds_from_distance() return material
def load(fullfilename): filedir, fileprefix, fileext = path_split(fullfilename) material=Material(fileprefix) curresnum = None curres = None locator = None residues = [] for line in open(fullfilename): tag = line[:6].strip() if tag == 'ATOM' or tag == 'HETATM': loctag = line[16:17].strip() resnum = int(line[22:26]) modifyer = line[26].strip() restag = line[18:21].strip() x = float(line[30:38]) y = float(line[38:46]) z = float(line[46:54]) xyz = array([x,y,z]) try: sym = line[13:15].strip() sym = symconv[sym] atno = sym2no[sym] except: sym = line[12:15].strip() atno = sym2no[sym] if loctag: if not locator: locator = loctag if loctag and loctag != locator: continue if modifyer: continue # skip residue #25B if we already have #25 material.add_atom(Atom(atno,xyz)) elif tag == 'CRYST1': words = line.split() a,b,c,alpha,beta,gamma = map(float,words[1:7]) #finish material.bonds_from_distance() return material
self.z += z return def get_position(self): return (self.x,self.y,self.z) def povray(self): return POVRay.Sphere((self.x,self.y,self.z),self.rad, (self.red,self.green,self.blue)) class Cylinder: name = 'Cylinder' def __init__(self,xyz1,xyz2, (red,green,blue)=(0.5,0.5,0.5),rad=CylRad, nsides=20): self.xyz1 = array(xyz1,'d') self.xyz2 = array(xyz2,'d') self.dxyz = self.xyz2-self.xyz1 self.length = math.sqrt(dot(self.dxyz,self.dxyz)) xd,yd,zd = self.dxyz/self.length self.rot = (-yd,xd,0) self.theta = math.acos(zd)*rad2deg self.rad = rad self.nsides = nsides self.red = red self.green = green self.blue = blue return def povray(self): return POVRay.Cylinder(tuple(self.xyz1),tuple(self.xyz2),self.rad,
def shift(self,x,y,z): d = array((x,y,z)) self.xyz1 += d self.xyz2 += d return
def H(x, y, z): return (1, array((x, y, z)))
def C(x, y, z): return (6, array((x, y, z)))
def load(fullfilename): gstep = 0 filedir, fileprefix, fileext = path_split(fullfilename) file=open(fullfilename, 'r') # Initialize atom data material = Material(fileprefix) opts = {} material.seqquest_options = opts dconv = 1. # default to Angstroms, which don't need conversion cell = None while 1: line = file.readline() if not line: break line = line.strip() if line == '@=KEYCHAR': continue # ignore other keychars for now elif line == '@DISTANCE UNIT': line = file.readline().strip() if line == 'ANGSTROM': continue elif line == 'BOHR': dconv = bohr2ang else: print "Warning: unknown distance unit ",line elif line == '@DIMENSION': line = file.readline().strip() opts['ndim'] = int(line) elif line == '@NUMBER OF ATOMS': line = file.readline().strip() opts['nat'] = int(line) opts['attypes'] = [] nread = int(ceil(opts['nat']/20.)) for i in range(nread): line = file.readline().strip() opts['attypes'].extend(map(int,line.split())) assert len(opts['attypes']) == opts['nat'] elif line == '@TYPES': line = file.readline().strip() opts['ntyp'] = int(line) opts['types'] = [] for i in range(opts['ntyp']): line = file.readline().strip() opts['types'].append(cleansym(line)) elif line == '@CELL VECTORS': line = file.readline() axyz = map(float,line.split()) line = file.readline() bxyz = map(float,line.split()) line = file.readline() cxyz = map(float,line.split()) if abs(dconv-1) > 1e-4: # careful when comparing floats axyz = axyz[0]*dconv,axyz[1]*dconv,axyz[2]*dconv bxyz = bxyz[0]*dconv,bxyz[1]*dconv,bxyz[2]*dconv cxyz = cxyz[0]*dconv,cxyz[1]*dconv,cxyz[2]*dconv cell = Cell(axyz,bxyz,cxyz) elif line == '@GSTEP': line = file.readline().strip() gstep = int(line) elif line == '@COORDINATES': atoms = [] for i in range(opts['nat']): line = file.readline() xyz = map(float,line.split()) if abs(dconv-1) > 1e-4: # careful when comparing floats xyz = xyz[0]*dconv,xyz[1]*dconv,xyz[2]*dconv isym = opts['attypes'][i] sym = opts['types'][isym-1] atoms.append((sym,xyz)) if gstep > 1: material.new_geo() for i in range(opts['nat']): sym,xyz = atoms[i] atno = sym2no[sym] xyz = array(xyz) material.add_atom(Atom(atno,xyz)) if cell: material.set_cell(cell) elif line == '@ESCF': line = file.readline().strip() escf = float(line) # Consider saving these for plotting elif line == '@FORCES': forces = [] for i in range(opts['nat']): line = file.readline() forces.append(map(float,line.split())) elif line == '@FMAX': line = file.readline().strip() fmax = float(line) elif line == '@FRMS': line = file.readline().strip() frms = float(line) else: print "Unknown line ",line material.bonds_from_distance() return material
def cross(a,b): return array((a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0])) def norm(a): return a/math.sqrt(dot(a,a))
def ZMatrix(zatoms): natoms = len(zatoms) atoms = [] # keep a running track of the labels for reference labels = {} for i in range(natoms): zatom = zatoms[i] if type(zatom) == type(''): label = zatom else: label = zatom[0] assert label not in labels sym = cleansym(label) atno = sym2no[sym] if i == 0: x, y, z = 0.0, 0.0, 0.0 elif i == 1: label1 = zatom[1] assert label1 in labels r = zatom[2] x, y, z = r, 0, 0 elif i == 2: label1 = zatom[1] assert label1 in labels atom1 = labels[label1] x1, y1, z1 = atom1.xyz r = zatom[2] label2 = zatom[3] assert label2 in labels atom2 = labels[label2] x2, y2, z2 = atom2.xyz if abs(x2 - x1) < 1e-5: thetap = pi else: thetap = atan((y2 - y1) / (x2 - x1)) if x2 < x1: thetap += pi theta = zatom[4] * deg2rad ang = theta + thetap rc = r * cos(ang) rs = r * sin(ang) x, y, z = x1 + rc, y1 + rs, z1 else: label1 = zatom[1] assert label1 in labels atom1 = labels[label1] r = zatom[2] label2 = zatom[3] assert label2 in labels atom2 = labels[label2] theta = zatom[4] * deg2rad label3 = zatom[5] assert label3 in labels atom3 = labels[label3] phi = zatom[6] * deg2rad v12 = atom1.xyz - atom2.xyz v13 = atom1.xyz - atom3.xyz n = cross(v12, v13) nn = cross(v12, n) n = normalize(n) nn = normalize(nn) n = -n * sin(phi) nn = nn * cos(phi) v3 = normalize(n + nn) v3 = v3 * r * sin(theta) v12 = normalize(v12) v12 = v12 * r * cos(theta) v2 = atom1.xyz + v3 - v12 x, y, z = v2 atom = Atom(atno, array((x, y, z)), label) atoms.append(atom) labels[label] = atom return atoms
def cross(a, b): return array((a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]))