def main(): """ This test shows various operators within Bond and BondContainer classes """ print "************************************************************************************" print " This test shows various operators within Bond and BondContainer classes " print "************************************************************************************ \n" print "Testing Bond and BondContainer" b1 = Bond( 1, 2, 1.233, "hooke") b2 = Bond( 3, 4, 0.5, "hooke") b3 = Bond( ) b4 = Bond( 3, 8, 0.5, "stiff") bonds = BondContainer() bonds.put(b1) bonds.put(b2) bonds.put(b3) bonds.put(b4) del b1, b2, b3, b4 print "\n Cleaning memory for initial objects \n" print "Check for pre-existing bond" if bonds.hasBond([2,1]): print "bond 1--2 exists" else: print "bond 1--2 does NOT exists" print "Check for pre-existing bond" if bonds.hasBond([2,3]): print "bond 2--3 exists" else: print "bond 2--3 does NOT exist" print " " print "Bonds: ", bonds print " " print "Test iterator for bond container " for gid, bondObj in bonds: print "bondID = ", gid, "bond object ", bondObj.__dict__ print " " searchID = 2 print "Testing 'in' operator for searchID ", searchID, " in bonds" if searchID in bonds: print "ID ",searchID, " in bonds" else: print "nope" print "-----------------------------------------------------" print "bonds.getTypeInfoDict() ", bonds.getTypeInfoDict() print "-----------------------------------------------------" print " " print "Here's an issue... globalID's are assigned by ParticleContainer class " print "Setting Bond object with gid's of Particles needs to be done after PC class set " print "or there could be conflicts \n"
def main(): """ This test shows how reorder (compress) global IDs for a StructureContainer """ p1 = Particle([0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle([5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle([5.0, 2.3, -20.1], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.233, "hooke") b2 = Bond(2, 3, 0.500, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) bonds1 = BondContainer() bonds1.put(b1) bonds1.put(b2) polymer1 = StructureContainer(atoms1, bonds1) # Complete structure 1 p1other = Particle([0.0, 2.3, -20.1], "C", 1.0, 2.34) p2other = Particle([50.0, 0.3, -0.1], "Ar", 2.0, 2.34) p3other = Particle([50.0, 0.3, -0.1], "Ar", 2.0, 2.34) b1other = Bond(1, 3, 1.233, "hooke") # Correct ptclIDs for second structure atoms2 = ParticleContainer() atoms2.put(p1other) atoms2.put(p2other) atoms2.put(p3other) del atoms2[2] # remove 2nd particle so ID's are non-consecutive bonds2 = BondContainer() bonds2.put(b1other) polymer2 = StructureContainer(atoms2, bonds2) # Complete structure 1 completely del p1, p2, p3, p1other, p2other, b1, b2, b1other, atoms1, atoms2, bonds1, bonds2 print "\n Cleaning memory for initial objects \n" print "-------------------- Before adding --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------------- After adding --------------------" polymer2 += polymer1 print "polymer2 = ", polymer2 polymer2.compressPtclIDs() print "-------------------- After compressing --------------------" print "polymer2 = ", polymer2
def main(): """ This test shows how to add StructureContainer objects together A special test to double-check re-labeling ... add a big container to a 'small' one """ print "***************************************************************************************" print " This test shows how to add StructureContainer objects together " print " A special test to double-check re-labeling ... add a big container to a 'small' one " print "*************************************************************************************** \n" p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "C", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.111, "hooke") b2 = Bond(2, 3, 2.222, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) bonds1 = BondContainer() bonds1.put(b1) bonds1.put(b2) polymer1 = StructureContainer(atoms1, bonds1) # Complete structure 1 completely p1other = Particle([1.11, 1.11, 1.11], "C", 1.0, 2.34) p2other = Particle([2.22, 2.22, 2.22], "Ar", 2.0, 2.34) b1other = Bond(1, 2, 1.1, "hooke-2") # Correct ptclIDs for second structure atoms2 = ParticleContainer() atoms2.put(p1other) atoms2.put(p2other) bonds2 = BondContainer() bonds2.put(b1other) polymer2 = StructureContainer(atoms2, bonds2) # Complete structure 1 completely del p1, p2, p3, p1other, p2other, b1, b2, b1other, atoms1, atoms2, bonds1, bonds2 print "\n Cleaning memory for initial objects \n" print "-------------------- Before adding --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------------- After adding --------------------" # polymer1 += polymer2 polymer2 += polymer1 print "polymer2 = ", polymer2
def runStrucAdd(numPtcls1, numPtcls2): """ Creates structures with ptcls and bonds between consecutive IDs Adds and reports timing Args: numPtcls1 (int): # of ptcls in first struc numPtcls2 (int): # of ptcls in second struc Returns: (initTime, addTime, compressTime) """ atoms1 = ParticleContainer() bonds1 = BondContainer() atoms2 = ParticleContainer() bonds2 = BondContainer() start_time = time.time() for n in range(numPtcls1): pobj1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, float(n + 1)) atoms1.put(pobj1) for n in range(1, numPtcls1): bobj1 = Bond(n, n + 1, 1.233, "hooke-1") bonds1.put(bobj1) for n in range(numPtcls2): pobj2 = Particle([2.2, 2.2, 2.2], "C", 1.0, float(n + 1)) atoms2.put(pobj2) for n in range(1, numPtcls2): bobj2 = Bond(n, n + 1, 1.233, "hooke-2") bonds2.put(bobj2) end_time = time.time() initTime = end_time - start_time polymer1 = StructureContainer(atoms1, bonds1) # Complete structure 1 polymer2 = StructureContainer(atoms2, bonds2) # Complete structure 2 del atoms1, atoms2 print "\n Cleaning memory for initial objects \n" start_time = time.time() polymer1 += polymer2 end_time = time.time() addTime = end_time - start_time start_time = time.time() polymer1.compressPtclIDs() end_time = time.time() compressTime = end_time - start_time return (numPtcls1, numPtcls2, initTime, addTime, compressTime)
def main(): """ This test shows how the setPtclPos method works to externally set all particle positions from a list """ print "************************************************************************************" print " This test shows how the setPtclPos method works to externally set all particle " print " positions from a list" print "************************************************************************************ \n" p1 = Particle([0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle([5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle([5.0, 2.3, -20.1], "C", 1.0, 2.34) p4 = Particle([0.0, 2.3, -20.1], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.233, "hooke") b2 = Bond(2, 3, 0.500, "hooke") b3 = Bond(3, 4, 2.301, "hooke") b4 = Bond(1, 3, 0.828, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) bonds = BondContainer() bonds.put(b1) bonds.put(b2) bonds.put(b3) bonds.put(b4) del p1, p2, p3, p4, b1, b2, b3, b4 polymer1 = StructureContainer(atoms1, bonds) del atoms1, bonds polymer1.setBoxLengths([[-3.0, 100], [-5, 23.0], [34.3, 100.1]]) print "Initial state of structure before reset ", polymer1 newPtclPos = [[1, 0.111, 0.222, 0.333], [2, 0.222, 0.333, 0.444], [4, 0.444, 0.555, 0.666]] print "new positions = ", newPtclPos polymer1.setPtclPositions(newPtclPos) ptclPosList = polymer1.getPtclPositions() print "new positions from structure = ", ptclPosList print " " print "After position reset/get ", polymer1
def main(): """ This test shows how to save/dump/restore state of structureContainers using pickle """ print "************************************************************************************" print " This test shows how to save/dump/restore state of structureContainers using pickle" print "************************************************************************************ \n" p1 = Particle([0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle([5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle([5.0, 2.3, -20.1], "C", 1.0, 2.34) p4 = Particle([0.0, 2.3, -20.1], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.233, "hooke") b2 = Bond(2, 3, 0.500, "hooke") b3 = Bond(3, 4, 2.301, "hooke") b4 = Bond(1, 3, 0.828, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) bonds = BondContainer() bonds.put(b1) bonds.put(b2) bonds.put(b3) bonds.put(b4) del p1, p2, p3, p4, b1, b2, b3, b4 polymer1 = StructureContainer(atoms1, bonds) del atoms1, bonds polymer1.setBoxLengths([[-3.0, 100], [-5, 23.0], [34.3, 100.1]]) print "Initial state of structure before dump ", polymer1 print "-------------------------------------------------------------------------------- \n" polymer1.dump('polymer1') polymerNew = StructureContainer() polymerNew.restore('polymer1.pkl') print "After load from pickle \n" print polymerNew print "-------------------------------------------------------------------------------- \n"
def atoms_and_bonds_from_structure(structure, atom_types, bond_types): """ Defines the atoms and bonds from the structure and given information from params. Args: structure (obj): A pymatgen structural object created from the transformed matrix structure, with forces included as site properties. atom_types (list(obj)): AtomType objects including atom_type_index (int), label (str), mass (float), charge (float), and core_shell (str). bond_types (list(obj)): BondType objects including bond_type_index (int) and label (str). Returns: atoms (list(obj)): Atom objects including atom_index (int), molecule_index (int), coords (np.array), forces (np.array), and atom_type (obj:AtomType). bonds (list(obj)): Bond objects including bond_index (int), atom_indices (list(int)), and bond_type (obj:BondType). """ atoms = [] bonds = [] atom_types_dict = {} atom_types_dict = {at.label: at for at in atom_types} bond_types_dict = {} bond_types_dict = {bt.label: bt for bt in bond_types} atom_index = 0 bond_index = 0 molecule_index = 0 for site in structure: molecule_index += 1 if site.species_string in atom_types_dict: # not core-shell atom atom_index += 1 atom_type = atom_types_dict[site.species_string] atoms.append( Atom(atom_index=atom_index, molecule_index=molecule_index, coords=site.coords, atom_forces=site.properties['forces'], atom_type=atom_type)) else: # need to handle core + shell atom_index += 1 atom_type = atom_types_dict[site.species_string + ' core'] atoms.append( Atom(atom_index=atom_index, molecule_index=molecule_index, coords=site.coords, atom_forces=site.properties['forces'], atom_type=atom_type)) atom_index += 1 atom_type = atom_types_dict[site.species_string + ' shell'] atoms.append( Atom(atom_index=atom_index, molecule_index=molecule_index, coords=site.coords, atom_forces=np.array([0.0, 0.0, 0.0]), atom_type=atom_type)) bond_index += 1 bond_type = bond_types_dict['{}-{} spring'.format( site.species_string, site.species_string)] bonds.append( Bond(bond_index=bond_index, atom_indices=[atom_index - 1, atom_index], bond_type=bond_type)) return atoms, bonds
def read_cply(self, cply_file, debug=False): """ Read cply file """ # Load periodic table pt = periodictable() read_lattice = False read_segment = False with open(cply_file) as f: for line in f: col = line.split() if (read_lattice): self.latvec[lv_cnt][0] = float(col[0]) self.latvec[lv_cnt][1] = float(col[1]) self.latvec[lv_cnt][2] = float(col[2]) if (lv_cnt == 2): read_lattice = False lv_cnt += 1 elif (read_segment): if (debug): print " Readin degment line ", col if (str(col[0]) == 'unit'): if (len(col) > 2): error_line = "Each segment can only have 1 unit " error_line += "\n {} in unit line will be ignored ".format( str(col[2::])) print error_line segment_i['unit'] = col[1] if (str(col[0]) == 'func'): segment_i['func'] = col[1::] if (str(col[0]) == 'end'): read_segment = False elif (len(col) >= 4 and col[0] != "bond" and col[0] != "#"): pt_i = Particle() pt_i.type = str(col[0]) pt_i.position = [ float(col[1]), float(col[2]), float(col[3]) ] add_dict = pt_i.tagsDict el = pt.getelementWithSymbol(str(col[0])) add_dict["symbol"] = str(col[0]) add_dict["number"] = el.number add_dict["mass"] = el.mass add_dict["cplytag"] = "" add_dict["linkid"] = "" add_dict["link"] = "" pt_i.mass = el.mass add_dict["chain"] = 1 add_dict["ring"] = 0 add_dict["residue"] = 1 add_dict["resname"] = "RES" add_dict["qgroup"] = 1 add_dict["fftype"] = "??" add_dict["label"] = el.symbol add_dict["cov_radii"] = el.cov_radii add_dict["vdw_radii"] = el.vdw_radii add_dict["lmptype"] = -1 if (len(col) >= 14): add_dict["label"] = str(col[4]) add_dict["fftype"] = str(col[5]) add_dict["mass"] = float(col[6]) pt_i.mass = float(col[6]) pt_i.charge = float(col[7]) add_dict["qgroup"] = int(col[8]) add_dict["ring"] = int(col[9]) add_dict["residue"] = int(col[10]) add_dict["resname"] = str(col[11]) add_dict["chain"] = int(col[12]) add_dict["cplytag"] = str(col[13]) elif (len(col) == 13): add_dict["label"] = str(col[4]) add_dict["fftype"] = str(col[5]) add_dict["mass"] = float(col[6]) pt_i.mass = float(col[6]) pt_i.charge = float(col[7]) add_dict["qgroup"] = int(col[8]) add_dict["ring"] = int(col[9]) add_dict["residue"] = int(col[10]) add_dict["resname"] = str(col[11]) add_dict["chain"] = int(col[12]) elif (len(col) == 8): add_dict["residue"] = int(col[5]) add_dict["resname"] = str(col[6]) add_dict["cplytag"] = str(col[7]) elif (len(col) == 7): pt_i.charge = float(col[4]) add_dict["residue"] = int(col[5]) add_dict["resname"] = str(col[6]) elif (len(col) == 5): add_dict["cplytag"] = str(col[4]) pt_i.setTagsDict(add_dict) self.ptclC.put(pt_i) # # # print "debug pt_i ",len(col),pt_i elif (len(col) >= 3): if (col[0] == "bond"): b_i = int(col[1]) b_j = int(col[2]) bnd = Bond(b_i, b_j) #print "process_line bond line ",col self.bondC.put(bnd) # Key word search if (len(col) > 0): if (str(col[0]) == 'lattice'): read_lattice = True lv_cnt = 0 if (str(col[0]) == 'segment'): read_segment = True segment_i = {} #segment(str(col[1])) # segment_i = segment() segment_i['tag'] = str(col[1]) segment_i['segment'] = {} self.segments.append(segment_i) if (debug): print segments sys.exit("debug in read_cply is True ")
def main(): """ This test shows how to set up Structure container with Particle/Bond-Containers and shows how IDs changed in StructureContainer propagate to values set in BondContainer for its held particle ID values Illustrates how a substructure method can return subgroup """ print "*****************************************************************************************************" print " This test shows how to set up Structure container with Particle/Bond-Containers \n" print " and shows how IDs changed in StructureContainer propagate to values set in BondContainer for its " print " held particle ID values \n" print " Illustrates how a substructure method can return subgroup" print "************************************************************************************ \n" p1 = Particle( [0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle( [5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle( [5.0, 2.3, -20.1], "C", 1.0, 2.34) p4 = Particle( [0.0, 2.3, -20.1], "C", 1.0, 2.34) p5 = Particle( [0.2, 1.3, 33.0], "Si", 2.0, 1.23) b1 = Bond( 5, 2, 1.233, "hooke") # This setup mimics earlier state of test b2 = Bond( 2, 3, 0.500, "hooke") # Still matches with diagram b3 = Bond( 3, 4, 2.301, "hooke") b4 = Bond( 5, 3, 0.828, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) atoms1.put(p5) del atoms1[1] # This is to mimic state of container for an older test bonds = BondContainer() bonds.put(b1) bonds.put(b2) bonds.put(b3) bonds.put(b4) del p1, p2, p3, p4, b1, b2, b3, b4 polymer1 = StructureContainer(atoms1, bonds) del atoms1, bonds print "********************************************************** \n" print polymer1 print diagramAfter print "********************************************************** \n" print "-------------------------------------------------------------------------------- \n" print "********************************************************** \n" print "Testing polymer1.getSubStructure([5,2])" print " currently ID's are reassigned in substructure \n" subpolymer = polymer1.getSubStructure([5,2]) print subpolymer print diagramAfter print "********************************************************** \n" print "********************************************************** \n" print "polymer1 Struture after returning sub-structure" print polymer1 print "********************************************************** \n" print "-------------------------------------------------------------------------------- \n" print "********************************************************** \n" print "Testing polymer1.getSubStructure([2,3,4])" print " currently ID's are reassigned in substructure \n" subpolymer = polymer1.getSubStructure([2,3,4]) print subpolymer print diagramAfter print "********************************************************** \n"
def main(): """ This shows operations on empty StructureContainer objects for 'robustness' """ print "************************************************************************************" print " This shows operations on empty StructureContainer objects for 'robustness'" print "************************************************************************************ \n" atoms1 = ParticleContainer() bonds1 = BondContainer() p1 = Particle([0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle([5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle([5.0, 2.3, -20.1], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.233, "hooke") b2 = Bond(2, 3, 0.500, "hooke") atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) bonds1.put(b1) bonds1.put(b2) atoms2 = ParticleContainer() bonds2 = BondContainer() polymer1 = StructureContainer(atoms1, bonds1) # Non-empty structure 1 polymer2 = StructureContainer(atoms2, bonds2) # Empty structure 2 # ------------------------------------------------------------------------------------------------------------------------ print "Non-empty container", polymer1 print "Empty container", polymer2 print "Testing empty dump" polymer2.dump("empty") print "Testing empty compressPtclIDs" polymer2.compressPtclIDs() print "Testing empty getSubStructure" subStruc = polymer2.getSubStructure([]) print "subStruc = ", subStruc print "Testing empty getPtclPositions" posList = polymer2.getPtclPositions() print "posList = ", posList print "------------------------------------------------------------------------------------ \n" print "Testing struc add --> non-empty (polymer1) += empty (polymer2)" polymer1 += polymer2 print "polymer1 = ", polymer1 print "------------------------------------------------------------------------------------ \n" print "polymer2 = ", polymer2 print "------------------------------------------------------------------------------------ \n" print "Testing struc add --> empty (polymer2) += non-empty (polymer1)" polymer2 += polymer1 print "polymer2 = ", polymer2 print "------------------------------------------------------------------------------------ \n" print "polymer1 = ", polymer1 print "------------------------------------------------------------------------------------ \n" print "Testing empty getSubStructure with non-zero id list (should return ERROR)" polymer3 = StructureContainer() subStruc = polymer3.getSubStructure([1, 2]) print "subStruc = ", subStruc
def main(): """ Illustrates how a substructure method returns subgroup with ONLY particle container included This is for speed considerations """ print "**********************************************************************************************" print "Illustrates how a substructure method returns subgroup with ONLY particle container included" print "This is for speed considerations" print "**********************************************************************************************\n" p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "Si", 1.0, 2.34) p4 = Particle([4.4, 4.4, 4.4], "Si", 1.0, 2.34) p5 = Particle([5.5, 5.5, 5.5], "C", 1.0, 2.34) # 1 p6 = Particle([6.6, 6.6, 6.6], "C", 1.0, 2.34) # 2 p7 = Particle([7.7, 7.7, 7.7], "C", 1.0, 2.34) # 3 p8 = Particle([8.8, 8.8, 8.8], "C", 1.0, 2.34) # 4 b1 = Bond(1, 2, 1.11, "hooke") b2 = Bond(2, 3, 2.22, "hooke") b3 = Bond(3, 4, 3.33, "hooke") b4 = Bond(4, 5, 4.44, "hooke") b5 = Bond(5, 6, 5.55, "hooke") b6 = Bond(6, 7, 6.66, "hooke") b7 = Bond(7, 8, 7.77, "hooke") a1 = Angle(1, 2, 3, 1.11, "harmonic") a2 = Angle(2, 3, 4, 2.22, "harmonic") a3 = Angle(3, 4, 5, 3.33, "harmonic") a4 = Angle(4, 5, 6, 4.44, "harmonic") a5 = Angle(5, 6, 7, 5.55, "harmonic") a6 = Angle(6, 7, 8, 6.66, "harmonic") d1 = Dihedral(1, 2, 3, 4, 1.11, "stiff") d2 = Dihedral(2, 3, 4, 5, 2.22, "stiff") d3 = Dihedral(3, 4, 5, 6, 3.33, "stiff") d4 = Dihedral(4, 5, 6, 7, 4.44, "stiff") d5 = Dihedral(5, 6, 7, 8, 5.55, "stiff") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) atoms1.put(p5) atoms1.put(p6) atoms1.put(p7) atoms1.put(p8) bonds = BondContainer() bonds.put(b1) bonds.put(b2) bonds.put(b3) bonds.put(b4) bonds.put(b5) bonds.put(b6) bonds.put(b7) angles = AngleContainer() angles.put(a1) angles.put(a2) angles.put(a3) angles.put(a4) angles.put(a5) angles.put(a6) dihedrals = DihedralContainer() dihedrals.put(d1) dihedrals.put(d2) dihedrals.put(d3) dihedrals.put(d4) dihedrals.put(d5) del p1, p2, p3, p4, p5, p6, p7, p8 del b1, b2, b3, b4, b5, b6, b7 del a1, a2, a3, a4, a5, a6 del d1, d2, d3, d4, d5 polymer1 = StructureContainer(atoms1, bonds, angles, dihedrals) del atoms1, bonds, angles, dihedrals print "********************************************************** \n" print "polymer1 Structure before returning sub-structure" print polymer1 print "********************************************************** \n" print "-------------------------------------------------------------------------------- \n" print "********************************************************** \n" print "Testing polymer1.getSubParticleContainer([2,3,4])" print " currently ID's are reassigned in substructure \n" subPtclC = polymer1.getSubStructure([2, 3, 4], particlesOnly=True) print subPtclC print "********************************************************** \n"
def main(): """ This test shows various operators within Angle and AngleContainer classes. Shows memory management structure and access methods """ p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "Si", 1.0, 2.34) # p4 = Particle([4.4, 4.4, 4.4], "C", 1.0, 2.34) # ptcl 1 p5 = Particle([5.5, 5.5, 5.5], "C", 1.0, 2.34) # ptcl 2 p6 = Particle([6.6, 6.6, 6.6], "C", 1.0, 2.34) # ptcl 3 b1 = Bond(1, 2, 1.11, "hooke") b2 = Bond(2, 3, 2.22, "hooke") # b3 = Bond(1, 2, 3.33, "hooke") b4 = Bond(2, 3, 4.44, "hooke") a1 = Angle(1, 2, 3, 1.11, "harmonic") # a2 = Angle(1, 2, 3, 2.22, "harmonic") atoms1 = ParticleContainer() atoms2 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) # atoms2.put(p4) atoms2.put(p5) atoms2.put(p6) bonds1 = BondContainer() bonds2 = BondContainer() bonds1.put(b1) bonds1.put(b2) bonds2.put(b3) bonds2.put(b4) angles1 = AngleContainer() angles2 = AngleContainer() angles1.put(a1) angles2.put(a2) del p1, p2, p3, p4, p5, p6, b1, b2, b3, b4, a1, a2 print "\n Cleaning memory for initial objects \n" print "angles1 container" print angles1 print " " print "angles2 container" print angles2 print "Testing 'in' operator (1 in angles1)" if (1 in angles1): print "angles1 contains gid 1" else: print "key not found in angles1" print "Testing 'in' operator (5 in angles1)" if (5 in angles1): print "angles1 contains gid 5" else: print "key not found in angles1" print " " angles1 += angles2 print "Will print the new angles1 after adding angles1 += angles2" print angles1 print "Check for pre-existing angle" if angles1.hasAngle([3, 2, 1]): print "angle 1--2--3 exists" else: print "angle 1--2--3 does NOT exists" print "Check for pre-existing angle" if angles1.hasAngle([2, 3, 1]): print "angle 2--3--1 exists" else: print "angle 2--3--1 does NOT exists"
def main(): """ This test shows structureContainer functionality with dihedrals included """ print "************************************************************************************" print " This test shows structureContainer functionality with dihedrals included" print "************************************************************************************ \n" p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "Si", 1.0, 2.34) p4 = Particle([4.4, 4.4, 4.4], "Si", 1.0, 2.34) # p5 = Particle([5.5, 5.5, 5.5], "C", 1.0, 2.34) # 1 p6 = Particle([6.6, 6.6, 6.6], "C", 1.0, 2.34) # 2 p7 = Particle([7.7, 7.7, 7.7], "C", 1.0, 2.34) # 3 p8 = Particle([8.8, 8.8, 8.8], "C", 1.0, 2.34) # 4 b1 = Bond(1, 2, 1.11, "hooke") b2 = Bond(2, 3, 2.22, "hooke") b3 = Bond(3, 4, 3.33, "hooke") # b4 = Bond(1, 2, 4.44, "hooke") b5 = Bond(2, 3, 5.55, "hooke") b6 = Bond(3, 4, 6.66, "hooke") a1 = Angle(1, 2, 3, 1.11, "harmonic") # a2 = Angle(1, 2, 3, 2.22, "harmonic") d1 = Dihedral(1, 2, 3, 4, 1.11, "stiff") # d2 = Dihedral(1, 2, 3, 4, 2.22, "stiff") atoms1 = ParticleContainer() atoms2 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) # atoms2.put(p5) atoms2.put(p6) atoms2.put(p7) atoms2.put(p8) bonds1 = BondContainer() bonds2 = BondContainer() bonds1.put(b1) bonds1.put(b2) bonds1.put(b3) # bonds2.put(b4) bonds2.put(b5) bonds2.put(b6) angles1 = AngleContainer() angles2 = AngleContainer() angles1.put(a1) angles2.put(a2) dih1 = DihedralContainer() dih2 = DihedralContainer() dih1.put(d1) dih2.put(d2) polymer1 = StructureContainer(atoms1, bonds1, angles1, dih1) polymer2 = StructureContainer(atoms2, bonds2, angles2, dih2) del p1, p2, p3, p4, p5, p6, p7, p8 del b1, b2, b3, b4, b5, b6, a1, a2, d1, d2 del atoms1, atoms2, bonds1, bonds2, angles1, angles2, dih1, dih2 print "\n Cleaning memory for initial objects \n" print "-------------------- Initial structures --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------- After adding (polymer1 += polymer2) ----------------" polymer2 += polymer1 print "polymer2 = ", polymer2
def read_lmpdata( strucC , parmC , data_file): """ Read Lammps data file Arguments: strucC (StructureContainer) parmC (ParameterContainer) data_file (str) data file ReturnS: strucC (StructureContainer) parmC (ParameterContainer) """ debug = 0 verbose = True set_chain_numbers = True if( not set_chain_numbers ): print " Warning not reading in chain numbers!!! " # Load periodic table pt = periodictable() F = open(data_file , 'r' ) lines = F.readlines() F.close() # # Read in data header with number of parameters # for line in lines: col = line.split() if ( len(col) >=2 ): # Read in number of each topolgical component if( col[1] == "atoms" ): n_atoms = int( col[0] ) elif( col[1] == "bonds" ): n_bonds = int( col[0] ) elif( col[1] == "angles" ): n_angles = int( col[0] ) elif( col[1] == "dihedrals" ): n_dihedrals = int( col[0] ) elif( col[1] == "impropers" ): n_impropers = int( col[0] ) if ( len(col) >= 3 ): # Read in number of each parameter type if( col[1] == "atom" and col[2] == "types" ): n_atypes = int( col[0] ) elif( col[1] == "bond" and col[2] == "types" ): n_btypes = int( col[0] ) elif( col[1] == "angle" and col[2] == "types" ): n_angtypes = int( col[0] ) elif( col[1] == "dihedral" and col[2] == "types" ): n_dtypes = int( col[0] ) elif( col[1] == "improper" and col[2] == "types" ): n_imptypes = int( col[0] ) # Read in box size if ( len(col) >= 4 ): if( col[2] == "xlo" and col[3] == "xhi" ): strucC.latvec[0][0] = float( col[1] ) - float( col[0] ) if( col[2] == "ylo" and col[3] == "yhi" ): strucC.latvec[1][1] = float( col[1] ) - float( col[0] ) if( col[2] == "zlo" and col[3] == "zhi" ): strucC.latvec[2][2] = float( col[1] ) - float( col[0] ) # Prind debug if( verbose ): print " atoms ",n_atoms print " n_bonds ",n_bonds print " n_angles ",n_angles print " n_dihedrals ",n_dihedrals print " n_impropers ",n_impropers print "" print "n_atom_types",n_atypes print "n_bond_types",n_btypes print "n_angle_types",n_angtypes print "n_dihedral_types",n_dtypes print "n_imp_dihedral_types",n_imptypes # Check to see if a previous read has occured pt_overwrite = False if( len(strucC.ptclC) > 0 ): pt_overwrite = True # Check of conistent number of atoms if( pt_overwrite ): if( len(strucC.ptclC) != n_atoms): print " %d atoms in passed structure "%(len(strucC.ptclC)) print " %d atoms in data file "%(n_atoms) sys.exit(" Inconsistent number of atoms " ) else: # # Initialize particle container # for pid_i in range(n_atoms): pt_i = Particle( ) strucC.ptclC.put(pt_i) bonds_overwrite = False if( len(strucC.bondC) > 0 ): bonds_overwrite = True if( len(strucC.bondC) != n_bonds): print " %d bonds in passed structure "%(len(strucC.bondC)) print " %d bonds in data file "%(n_bonds) sys.exit(" Inconsistent number of bonds " ) angles_overwrite = False if( len(strucC.angleC) > 0 ): angles_overwrite = True if( len(strucC.angleC) != n_angles): print " %d angles in passed structure "%(len(strucC.angleC)) print " %d angles in data file "%(n_angles) sys.exit(" Inconsistent number of angles " ) dih_overwrite = False if( len(strucC.dihC) > 0 ): dih_overwrite = True if( len(strucC.dihC) != n_dihedrals): print " %d dihedrals in passed structure "%(len(strucC.dihC)) print " %d dihedrals in data file "%(n_dihedrals) sys.exit(" Inconsistent number of dihedrals " ) imp_overwrite = False if( len(strucC.impC) > 0 ): imp_overwrite = True if( len(strucC.impC) != n_impropers): print " %d impropers in passed structure "%(len(strucC.impC)) print " %d impropers in data file "%(n_impropers) sys.exit(" Inconsistent number of impropers " ) # # Intialize # - read in boolean to off # read_Masses = 0 read_Pair = 0 read_Bond_coeff = 0 read_Angle_coeff = 0 read_Dihedral_coeff = 0 read_Improper_coeff = 0 read_Atoms = 0 read_Bonds = 0 read_Angles = 0 read_Dihedrals = 0 read_Impropers = 0 # - lists as indecise can be out of order in data file ATYPE_REF = n_atypes*[""] ATYPE_MASS = np.zeros(n_atypes) ATYPE_EP = np.zeros(n_atypes) ATYPE_SIG = np.zeros(n_atypes) BTYPE_REF = n_btypes*[2*[""]] BONDTYPE_R0 = np.zeros(n_btypes) BONDTYPE_K = np.zeros(n_btypes) ANGTYPE_REF = n_angtypes*[3*[""]] ANGLETYPE_R0 = np.zeros(n_angtypes) ANGLETYPE_K = np.zeros(n_angtypes) DTYPE_REF = n_dtypes*[4*[""]] DIHTYPE_C = np.zeros((n_dtypes,4)) DIHTYPE_F = np.zeros(n_dtypes) DIHTYPE_K = np.zeros(n_dtypes) DIHTYPE_PN = np.zeros(n_dtypes) DIHTYPE_PHASE = np.zeros(n_dtypes) IMPTYPE_REF = n_imptypes*[4*[""]] IMPTYPE_F = np.zeros(n_imptypes) IMPTYPE_E0 = np.zeros(n_imptypes) IMPTYPE_K = np.zeros(n_imptypes) MOLNUMB = n_atoms*[0] ATYPE_IND = n_atoms*[0] CHARGES = np.zeros(n_atoms) R = n_atoms*[np.zeros(3)] ATYPE = n_atoms*[""] BONDS = n_bonds*[[0,0]] BTYPE_IND = n_bonds*[0] ANGLES = n_angles*[[0,0,0]] ANGTYPE_IND = n_angles*[0] DIH = n_dihedrals*[[0,0,0,0]] DTYPE_IND = n_dihedrals*[0] # # Check if values exist and need to be updated or don't and need to be created # ljtyp_update = False ljtyp_cnt = 0 if( len(parmC.ljtypC) > 0 ): print " LJ types will be updated " ljtyp_update = True btyp_update = False btyp_cnt = 0 if( len(parmC.btypC) > 0 ): print " Bond types will be updated " btyp_update = True atyp_update = False atyp_cnt = 0 if( len(parmC.atypC) > 0 ): print " Angle types will be updated " atyp_update = True dtyp_update = False dtyp_cnt = 0 if( len(parmC.dtypC) > 0 ): print " Dihedral types will be updated " dtyp_update = True imptyp_update = False imptyp_cnt = 0 if( len(parmC.imptypC) > 0 ): print " Improper dihedrals types will be updated " imptyp_update = True # # Read in data parameters # for line in lines: col = line.split() if( read_Masses and len(col) >= 2 ): cnt_Masses += 1 ind = int(col[0]) - 1 ATYPE_MASS[ind] = float(col[1]) if( len(col) >= 4 ): ATYPE_REF[ind] = col[3] ptype1 = col[3] else: ATYPE_REF[ind] = "??" ptype1 = "??" mass_i = float(col[1]) if( ljtyp_update ): ljtyp_cnt = ind + 1 if( ljtyp_cnt > len(parmC.ljtypC) ): print "Mass index %d larger then length of previously read ljtypC %d"%(ind,len(parmC.ljtypC)) ljtyp_i = parmC.ljtypC[ljtyp_cnt] ljtyp_i.setmass(mass_i) else: ljtyp_i = ljtype(ptype1) ljtyp_i.setmass(mass_i) parmC.ljtypC.put(ljtyp_i) # Turn of mass read if(cnt_Masses == n_atypes ): read_Masses = 0 if( read_Pair and len(col) >= 3 ): cnt_Pair += 1 ind = int(col[0]) - 1 ATYPE_EP[ind] = float(col[1]) ATYPE_SIG[ind] = float(col[2]) epsilon = float(col[1]) sigma = float(col[2]) ljtyp_ind = int(col[0]) ljtyp_i = parmC.ljtypC[ljtyp_ind] ljtyp_i.setparam(epsilon,sigma) # Turn pair parameter read off if(cnt_Pair >= n_atypes ): read_Pair = 0 if( read_Bond_coeff and len(col) >= 3 ): cnt_Bond_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) b_ind = int( col[0]) - 1 if( b_ind > n_btypes ): error_line = " Error in data file index of bond parameter exceeds number of bond parameters specified with bond types " sys.exit(error_line) BTYPE_REF[b_ind][0] = "??" BTYPE_REF[b_ind][1] = "??" BONDTYPE_K[b_ind] = float(col[1]) BONDTYPE_R0[b_ind] = float(col[2]) ptype1 = "??" ptype2 = "??" lmpindx = int( col[0]) kb = float(col[1]) r0 = float(col[2]) btype = "harmonic" g_type = 1 if( btyp_update ): btyp_cnt = b_ind + 1 btyp_i = parmC.btypC[btyp_cnt] btyp_i.setharmonic(r0,kb) btyp_i.set_g_indx(g_type) btyp_i.set_lmpindx(lmpindx) else: btyp_i = bondtype(ptype1,ptype2,btype) btyp_i.setharmonic(r0,kb) btyp_i.set_g_indx(g_type) btyp_i.set_lmpindx(lmpindx) parmC.btypC.put(btyp_i) if( cnt_Bond_coeff >= n_btypes ): read_Bond_coeff = 0 if( read_Angle_coeff and len(col) >= 3 ): cnt_Angle_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) a_ind = int( col[0]) - 1 if( a_ind > n_angtypes ): print sys.exit(" Error in data file index of angle parameter exceeds number of angle parameters specified with angle types ") ANGTYPE_REF[a_ind][0] = "??" ANGTYPE_REF[a_ind][1] = "??" ANGTYPE_REF[a_ind][2] = "??" ANGLETYPE_K[a_ind] = float(col[1]) ANGLETYPE_R0[a_ind] = float(col[2]) ptype1 = "??" ptype2 = "??" ptype3 = "??" lmpindx = int( col[0]) theta0 = float( col[2] ) # degrees kb = float( col[1] ) atype = "harmonic" gfunc_type = 1 if( atyp_update ): atyp_cnt = a_ind + 1 atyp_i = parmC.atypC[atyp_cnt] atyp_i.set_g_indx(gfunc_type) atyp_i.set_lmpindx(lmpindx) atyp_i.setharmonic(theta0,kb) else: atyp_i = angletype(ptype1,ptype2,ptype3,atype) atyp_i.set_g_indx(gfunc_type) atyp_i.set_lmpindx(lmpindx) atyp_i.setharmonic(theta0,kb) parmC.atypC.put(atyp_i) if( cnt_Angle_coeff >= n_angtypes ): read_Angle_coeff = 0 if( read_Dihedral_coeff and len(col) >= 3 ): cnt_Dihedral_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) d_ind = int( col[0]) - 1 if( debug): print " reading dih type ",d_ind," cnt ",cnt_Dihedral_coeff," of ",n_dtypes if( d_ind > n_dtypes ): error_line = " Error in data file index of dihedral parameter %d exceeds number of dihedral parameters %d "%(d_ind , n_dtypes) error_line += " specified with dihedral types " print sys.exit(error_line) DTYPE_REF[d_ind][0] = "??" DTYPE_REF[d_ind][1] = "??" DTYPE_REF[d_ind][2] = "??" DTYPE_REF[d_ind][3] = "??" # Assume OPLS dihedral type DIHTYPE_F[d_ind] = 3 DIHTYPE_C[d_ind][0] = float(col[1]) DIHTYPE_C[d_ind][1] = float(col[2]) DIHTYPE_C[d_ind][2] = float(col[3]) DIHTYPE_C[d_ind][3] = float(col[4]) ptype1 = "??" ptype2 = "??" ptype3 = "??" ptype4 = "??" # Set parameters according to type gfunc_type = 3 dtype = "opls" lmpindx = int( col[0] ) k1 = float( col[1] ) k2 = float( col[2] ) k3 = float( col[3] ) k4 = float( col[4] ) if( dtyp_update ): dtyp_cnt = d_ind + 1 dtyp_i = parmC.dtypC[dtyp_cnt] dtyp_i.set_g_indx(gfunc_type) dtyp_i.set_lmpindx(lmpindx) dtyp_i.setopls(k1,k2,k3,k4) else: dtyp_i = dihtype(ptype1,ptype2,ptype3,ptype4,dtype) dtyp_i.set_g_indx(gfunc_type) dtyp_i.set_lmpindx(lmpindx) dtyp_i.setopls(k1,k2,k3,k4) parmC.dtypC.put(dtyp_i) if( cnt_Dihedral_coeff >= n_dtypes ): read_Dihedral_coeff = 0 if( read_Improper_coeff and len(col) >= 3 ): cnt_Improper_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) imp_ind = int( col[0]) - 1 if( debug): print " reading imp dih type ",imp_ind," cnt ",cnt_Improper_coeff," of ",n_imptypes if( imp_ind > n_imptypes ): error_line = " Error in data file index of improper parameter %d exceeds number of improper parameters %d "%(imp_ind , n_imptypes) error_line += " specified with dihedral types " print sys.exit(error_line) IMPTYPE_REF[imp_ind][0] = "??" IMPTYPE_REF[imp_ind][1] = "??" IMPTYPE_REF[imp_ind][2] = "??" IMPTYPE_REF[imp_ind][3] = "??" # Assume OPLS dihedral type IMPTYPE_F[imp_ind] = 2 KE = float(col[1]) Eo = float(col[2]) IMPTYPE_E0[imp_ind] = Eo IMPTYPE_K[imp_ind] = KE ptype1 = "??" ptype2 = "??" ptype3 = "??" ptype4 = "??" # Set parameters according to type g_indx = 2 dtype = "improper" lmpindx = int( col[0] ) if( imptyp_update ): imptyp_cnt = imp_ind + 1 imptyp_i = parmC.imptypC[imptyp_cnt] imptyp_i.set_g_indx(g_indx) imptyp_i.setimp(Eo,KE) imptyp_i.set_lmpindx(lmpindx) else: imptyp_i = imptype(ptype1,ptype2,ptype3,ptype4,dtype) imptyp_i.set_g_indx(g_indx) imptyp_i.setimp(Eo,KE) imptyp_i.set_lmpindx(lmpindx) parmC.imptypC.put(imptyp_i) if( cnt_Improper_coeff >= n_imptypes ): read_Improper_coeff = 0 if( read_Atoms and len(col) >= 7 ): cnt_Atoms += 1 ind = int( col[0]) - 1 if( ind > n_atoms ): print sys.exit(" Error in data file index of atoms exceeds number of atoms specified with atoms ") chain_i = int(col[1]) lmptype_i = int(col[2]) #- 1 indx = int(col[2]) - 1 q_i = float(col[3]) m_i = ATYPE_MASS[indx] fftype_i = ATYPE_REF[indx] el = pt.getelementWithMass(m_i) if( el.symbol == "VS" ): el.symbol = ATYPE_l[atom_indx].strip() fftype_i = "VS" m_i = 0.0 # HACK !! if( ATYPE_MASS[indx] == 9.0 ): el.symbol = "LP" fftype_i = "LP" m_i = 0.0 r_i = [ float(col[4]),float(col[5]),float(col[6])] type_i = str(lmptype_i) #tagsD = {"chain":chain_i,"symbol":el.symbol,"number":el.number,"mass":el.mass,"cov_radii":el.cov_radii,"vdw_radii":el.vdw_radii} #if( pt_overwrite ): pt_i = strucC.ptclC[ind+1] pt_i.position = r_i update_coord = True if( not update_coord ): pt_i.type = el.symbol pt_i.charge = q_i pt_i.mass = m_i add_dict = pt_i.tagsDict # Set properties read in data file if( set_chain_numbers ): add_dict["chain"] = chain_i add_dict["symbol"] = el.symbol add_dict["number"] = el.number add_dict["mass"] = el.mass add_dict["cov_radii"] = el.cov_radii add_dict["vdw_radii"] = el.vdw_radii add_dict["lmptype"] = lmptype_i add_dict["fftype"] = fftype_i # add_dict["ffmass"] = ATYPE_MASS[indx] add_dict["residue"] = chain_i add_dict["qgroup"] = chain_i add_dict["resname"] = "MOLR" add_dict["ring"] = 0 add_dict["label"] = add_dict["symbol"] pt_i.setTagsDict(add_dict) if( cnt_Atoms >= n_atoms ): read_Atoms = 0 if(read_Bonds and len(col) >= 4 ): cnt_Bonds += 1 ind = int( col[0]) - 1 if( ind > n_bonds ): print sys.exit(" Error in data file index of bonds exceeds number of bonds specified with bonds ") BTYPE_IND[ind] = int(col[1] ) - 1 BONDS[ind] = [int(col[2]) - 1 , int(col[3]) - 1 ] i_o = int(col[2]) j_o = int(col[3]) if( bonds_overwrite ): bondObj = strucC.bondC[cnt_Bonds] bondObj.pgid1 = i_o bondObj.pgid2 = j_o bondObj.set_lmpindx(int(col[1] )) else: bondObj = Bond( i_o, j_o ) bondObj.set_lmpindx(int(col[1] )) strucC.bondC.put(bondObj) if( cnt_Bonds >= n_bonds ): read_Bonds = 0 if(read_Angles and len(col) >= 5 ): cnt_Angles += 1 ind = int( col[0]) - 1 ANGTYPE_IND[ind] = int(col[1] ) - 1 ANGLES[ind] = [int(col[2]) - 1, int(col[3]) - 1, int(col[4]) - 1 ] k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) if( cnt_Angles >= n_angles ): read_Angles = 0 if( angles_overwrite ): angleObj = strucC.angleC[cnt_Angles] angleObj.pgid1 = k_o angleObj.pgid2 = i_o angleObj.pgid3 = j_o angleObj.set_lmpindx(int(col[1] )) else: angleObj = Angle( k_o,i_o, j_o ) angleObj.set_lmpindx(int(col[1] )) strucC.angleC.put(angleObj) if(read_Dihedrals and len(col) >= 6 ): cnt_Dihedrals += 1 ind = int( col[0]) - 1 DTYPE_IND[ind] = int(col[1] ) - 1 DIH[ind] = [int(col[2]) - 1,int(col[3]) - 1, int(col[4]) - 1,int(col[5]) - 1] k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) l_o = int(col[5]) if( dih_overwrite ): dObj = strucC.dihC[cnt_Dihedrals] dObj.pgid1 = k_o dObj.pgid2 = i_o dObj.pgid3 = j_o dObj.pgid4 = l_o dObj.set_lmpindx(int(col[1] )) else: dObj = Dihedral( k_o,i_o, j_o,l_o ) dObj.set_lmpindx(int(col[1] )) strucC.dihC.put(dObj) if( cnt_Dihedrals >= n_dihedrals ): read_Dihedrals = 0 if(read_Impropers and len(col) >= 2 ): cnt_Impropers += 1 ind = int( col[0]) - 1 k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) l_o = int(col[5]) if( imp_overwrite ): impObj = strucC.impC[cnt_Impropers] impObj.pgid1 = k_o impObj.pgid2 = i_o impObj.pgid3 = j_o impObj.pgid4 = l_o impObj.set_lmpindx(int(col[1] )) impObj.set_type("improper") else: impObj = Improper( k_o,i_o, j_o,l_o ) impObj.set_lmpindx(int(col[1] )) impObj.set_type("improper") strucC.impC.put(impObj) if( cnt_Impropers >= n_impropers ): read_Impropers = 0 if ( len(col) >= 1 ): if( col[0] == "Masses" ): read_Masses = 1 cnt_Masses = 0 if( col[0] == "Atoms" ): read_Atoms = 1 cnt_Atoms = 0 if( col[0] == "Bonds" ): read_Bonds = 1 cnt_Bonds = 0 if( col[0] == "Angles" ): read_Angles = 1 cnt_Angles = 0 if( col[0] == "Dihedrals" ): read_Dihedrals = 1 cnt_Dihedrals = 0 if( col[0] == "Impropers" ): read_Impropers = 1 cnt_Impropers = 0 if ( len(col) >= 2 ): if( col[0] == "Pair" and col[1] == "Coeffs" ): read_Pair = 1 cnt_Pair = 0 if( col[0] == "Bond" and col[1] == "Coeffs" ): read_Bond_coeff = 1 cnt_Bond_coeff = 0 if( col[0] == "Angle" and col[1] == "Coeffs" ): read_Angle_coeff = 1 cnt_Angle_coeff = 0 if( col[0] == "Dihedral" and col[1] == "Coeffs" ): read_Dihedral_coeff = 1 cnt_Dihedral_coeff = 0 if( col[0] == "Improper" and col[1] == "Coeffs" ): read_Improper_coeff = 1 cnt_Improper_coeff = 0 # cnt_Bonds += 1 # ind = int( col[0]) - 1 # BTYPE_IND[ind] = int(col[1] ) - 1 # BONDS[ind][0] = int(col[2]) # if( cnt_Bonds >= n_atoms ): # read_Bonds = 0 # # if( ljtyp_update ): if( ljtyp_cnt != len(parmC.ljtypC) ): print " Number of LJ types read in %d does not match previously read %d "%(ljtyp_cnt,len(parmC.ljtypC)) if( debug): for ind in range(len(ATYPE_MASS)): print ind+1,ATYPE_MASS[ind] for ind in range(len(ATYPE_EP)): print ind+1,ATYPE_EP[ind],ATYPE_SIG[ind] for ind in range(n_btypes): print ind+1,BONDTYPE_R0[ind],BONDTYPE_K[ind] for ind in range(n_angtypes): print ind+1,ANGLETYPE_R0[ind],ANGLETYPE_K[ind] for ind in range(n_dtypes): print ind+1,DIHTYPE_C[ind] debug =0 if( debug): for ind in range(len(BONDS)): print ind+1,BONDS[ind] if(debug): sys.exit("debug 1 ") # # return (strucC,parmC)
def read_lmpdata(strucC, parmC, data_file): """ Read Lammps data file Arguments: strucC (StructureContainer) parmC (ParameterContainer) data_file (str) data file ReturnS: strucC (StructureContainer) parmC (ParameterContainer) """ debug = 0 verbose = True set_chain_numbers = True if (not set_chain_numbers): print " Warning not reading in chain numbers!!! " # Load periodic table pt = periodictable() F = open(data_file, 'r') lines = F.readlines() F.close() # # Read in data header with number of parameters # for line in lines: col = line.split() if (len(col) >= 2): # Read in number of each topolgical component if (col[1] == "atoms"): n_atoms = int(col[0]) elif (col[1] == "bonds"): n_bonds = int(col[0]) elif (col[1] == "angles"): n_angles = int(col[0]) elif (col[1] == "dihedrals"): n_dihedrals = int(col[0]) elif (col[1] == "impropers"): n_impropers = int(col[0]) if (len(col) >= 3): # Read in number of each parameter type if (col[1] == "atom" and col[2] == "types"): n_atypes = int(col[0]) elif (col[1] == "bond" and col[2] == "types"): n_btypes = int(col[0]) elif (col[1] == "angle" and col[2] == "types"): n_angtypes = int(col[0]) elif (col[1] == "dihedral" and col[2] == "types"): n_dtypes = int(col[0]) elif (col[1] == "improper" and col[2] == "types"): n_imptypes = int(col[0]) # Read in box size if (len(col) >= 4): if (col[2] == "xlo" and col[3] == "xhi"): strucC.latvec[0][0] = float(col[1]) - float(col[0]) if (col[2] == "ylo" and col[3] == "yhi"): strucC.latvec[1][1] = float(col[1]) - float(col[0]) if (col[2] == "zlo" and col[3] == "zhi"): strucC.latvec[2][2] = float(col[1]) - float(col[0]) # Prind debug if (verbose): print " atoms ", n_atoms print " n_bonds ", n_bonds print " n_angles ", n_angles print " n_dihedrals ", n_dihedrals print " n_impropers ", n_impropers print "" print "n_atom_types", n_atypes print "n_bond_types", n_btypes print "n_angle_types", n_angtypes print "n_dihedral_types", n_dtypes print "n_imp_dihedral_types", n_imptypes # Check to see if a previous read has occured pt_overwrite = False if (len(strucC.ptclC) > 0): pt_overwrite = True # Check of conistent number of atoms if (pt_overwrite): if (len(strucC.ptclC) != n_atoms): print " %d atoms in passed structure " % (len(strucC.ptclC)) print " %d atoms in data file " % (n_atoms) sys.exit(" Inconsistent number of atoms ") else: # # Initialize particle container # for pid_i in range(n_atoms): pt_i = Particle() strucC.ptclC.put(pt_i) bonds_overwrite = False if (len(strucC.bondC) > 0): bonds_overwrite = True if (len(strucC.bondC) != n_bonds): print " %d bonds in passed structure " % (len(strucC.bondC)) print " %d bonds in data file " % (n_bonds) sys.exit(" Inconsistent number of bonds ") angles_overwrite = False if (len(strucC.angleC) > 0): angles_overwrite = True if (len(strucC.angleC) != n_angles): print " %d angles in passed structure " % (len(strucC.angleC)) print " %d angles in data file " % (n_angles) sys.exit(" Inconsistent number of angles ") dih_overwrite = False if (len(strucC.dihC) > 0): dih_overwrite = True if (len(strucC.dihC) != n_dihedrals): print " %d dihedrals in passed structure " % (len(strucC.dihC)) print " %d dihedrals in data file " % (n_dihedrals) sys.exit(" Inconsistent number of dihedrals ") imp_overwrite = False if (len(strucC.impC) > 0): imp_overwrite = True if (len(strucC.impC) != n_impropers): print " %d impropers in passed structure " % (len(strucC.impC)) print " %d impropers in data file " % (n_impropers) sys.exit(" Inconsistent number of impropers ") # # Intialize # - read in boolean to off # read_Masses = 0 read_Pair = 0 read_Bond_coeff = 0 read_Angle_coeff = 0 read_Dihedral_coeff = 0 read_Improper_coeff = 0 read_Atoms = 0 read_Bonds = 0 read_Angles = 0 read_Dihedrals = 0 read_Impropers = 0 # - lists as indecise can be out of order in data file ATYPE_REF = n_atypes * [""] ATYPE_MASS = np.zeros(n_atypes) ATYPE_EP = np.zeros(n_atypes) ATYPE_SIG = np.zeros(n_atypes) BTYPE_REF = n_btypes * [2 * [""]] BONDTYPE_R0 = np.zeros(n_btypes) BONDTYPE_K = np.zeros(n_btypes) ANGTYPE_REF = n_angtypes * [3 * [""]] ANGLETYPE_R0 = np.zeros(n_angtypes) ANGLETYPE_K = np.zeros(n_angtypes) DTYPE_REF = n_dtypes * [4 * [""]] DIHTYPE_C = np.zeros((n_dtypes, 4)) DIHTYPE_F = np.zeros(n_dtypes) DIHTYPE_K = np.zeros(n_dtypes) DIHTYPE_PN = np.zeros(n_dtypes) DIHTYPE_PHASE = np.zeros(n_dtypes) IMPTYPE_REF = n_imptypes * [4 * [""]] IMPTYPE_F = np.zeros(n_imptypes) IMPTYPE_E0 = np.zeros(n_imptypes) IMPTYPE_K = np.zeros(n_imptypes) MOLNUMB = n_atoms * [0] ATYPE_IND = n_atoms * [0] CHARGES = np.zeros(n_atoms) R = n_atoms * [np.zeros(3)] ATYPE = n_atoms * [""] BONDS = n_bonds * [[0, 0]] BTYPE_IND = n_bonds * [0] ANGLES = n_angles * [[0, 0, 0]] ANGTYPE_IND = n_angles * [0] DIH = n_dihedrals * [[0, 0, 0, 0]] DTYPE_IND = n_dihedrals * [0] # # Check if values exist and need to be updated or don't and need to be created # ljtyp_update = False ljtyp_cnt = 0 if (len(parmC.ljtypC) > 0): print " LJ types will be updated " ljtyp_update = True btyp_update = False btyp_cnt = 0 if (len(parmC.btypC) > 0): print " Bond types will be updated " btyp_update = True atyp_update = False atyp_cnt = 0 if (len(parmC.atypC) > 0): print " Angle types will be updated " atyp_update = True dtyp_update = False dtyp_cnt = 0 if (len(parmC.dtypC) > 0): print " Dihedral types will be updated " dtyp_update = True imptyp_update = False imptyp_cnt = 0 if (len(parmC.imptypC) > 0): print " Improper dihedrals types will be updated " imptyp_update = True # # Read in data parameters # for line in lines: col = line.split() if (read_Masses and len(col) >= 2): cnt_Masses += 1 ind = int(col[0]) - 1 ATYPE_MASS[ind] = float(col[1]) if (len(col) >= 4): ATYPE_REF[ind] = col[3] ptype1 = col[3] else: ATYPE_REF[ind] = "??" ptype1 = "??" mass_i = float(col[1]) if (ljtyp_update): ljtyp_cnt = ind + 1 if (ljtyp_cnt > len(parmC.ljtypC)): print "Mass index %d larger then length of previously read ljtypC %d" % ( ind, len(parmC.ljtypC)) ljtyp_i = parmC.ljtypC[ljtyp_cnt] ljtyp_i.setmass(mass_i) else: ljtyp_i = ljtype(ptype1) ljtyp_i.setmass(mass_i) parmC.ljtypC.put(ljtyp_i) # Turn of mass read if (cnt_Masses == n_atypes): read_Masses = 0 if (read_Pair and len(col) >= 3): cnt_Pair += 1 ind = int(col[0]) - 1 ATYPE_EP[ind] = float(col[1]) ATYPE_SIG[ind] = float(col[2]) epsilon = float(col[1]) sigma = float(col[2]) ljtyp_ind = int(col[0]) ljtyp_i = parmC.ljtypC[ljtyp_ind] ljtyp_i.setparam(epsilon, sigma) # Turn pair parameter read off if (cnt_Pair >= n_atypes): read_Pair = 0 if (read_Bond_coeff and len(col) >= 3): cnt_Bond_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) b_ind = int(col[0]) - 1 if (b_ind > n_btypes): error_line = " Error in data file index of bond parameter exceeds number of bond parameters specified with bond types " sys.exit(error_line) BTYPE_REF[b_ind][0] = "??" BTYPE_REF[b_ind][1] = "??" BONDTYPE_K[b_ind] = float(col[1]) BONDTYPE_R0[b_ind] = float(col[2]) ptype1 = "??" ptype2 = "??" lmpindx = int(col[0]) kb = float(col[1]) r0 = float(col[2]) btype = "harmonic" g_type = 1 if (btyp_update): btyp_cnt = b_ind + 1 btyp_i = parmC.btypC[btyp_cnt] btyp_i.setharmonic(r0, kb) btyp_i.set_g_indx(g_type) btyp_i.set_lmpindx(lmpindx) else: btyp_i = bondtype(ptype1, ptype2, btype) btyp_i.setharmonic(r0, kb) btyp_i.set_g_indx(g_type) btyp_i.set_lmpindx(lmpindx) parmC.btypC.put(btyp_i) if (cnt_Bond_coeff >= n_btypes): read_Bond_coeff = 0 if (read_Angle_coeff and len(col) >= 3): cnt_Angle_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) a_ind = int(col[0]) - 1 if (a_ind > n_angtypes): print sys.exit( " Error in data file index of angle parameter exceeds number of angle parameters specified with angle types " ) ANGTYPE_REF[a_ind][0] = "??" ANGTYPE_REF[a_ind][1] = "??" ANGTYPE_REF[a_ind][2] = "??" ANGLETYPE_K[a_ind] = float(col[1]) ANGLETYPE_R0[a_ind] = float(col[2]) ptype1 = "??" ptype2 = "??" ptype3 = "??" lmpindx = int(col[0]) theta0 = float(col[2]) # degrees kb = float(col[1]) atype = "harmonic" gfunc_type = 1 if (atyp_update): atyp_cnt = a_ind + 1 atyp_i = parmC.atypC[atyp_cnt] atyp_i.set_g_indx(gfunc_type) atyp_i.set_lmpindx(lmpindx) atyp_i.setharmonic(theta0, kb) else: atyp_i = angletype(ptype1, ptype2, ptype3, atype) atyp_i.set_g_indx(gfunc_type) atyp_i.set_lmpindx(lmpindx) atyp_i.setharmonic(theta0, kb) parmC.atypC.put(atyp_i) if (cnt_Angle_coeff >= n_angtypes): read_Angle_coeff = 0 if (read_Dihedral_coeff and len(col) >= 3): cnt_Dihedral_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) d_ind = int(col[0]) - 1 if (debug): print " reading dih type ", d_ind, " cnt ", cnt_Dihedral_coeff, " of ", n_dtypes if (d_ind > n_dtypes): error_line = " Error in data file index of dihedral parameter %d exceeds number of dihedral parameters %d " % ( d_ind, n_dtypes) error_line += " specified with dihedral types " print sys.exit(error_line) DTYPE_REF[d_ind][0] = "??" DTYPE_REF[d_ind][1] = "??" DTYPE_REF[d_ind][2] = "??" DTYPE_REF[d_ind][3] = "??" # Assume OPLS dihedral type DIHTYPE_F[d_ind] = 3 DIHTYPE_C[d_ind][0] = float(col[1]) DIHTYPE_C[d_ind][1] = float(col[2]) DIHTYPE_C[d_ind][2] = float(col[3]) DIHTYPE_C[d_ind][3] = float(col[4]) ptype1 = "??" ptype2 = "??" ptype3 = "??" ptype4 = "??" # Set parameters according to type gfunc_type = 3 dtype = "opls" lmpindx = int(col[0]) k1 = float(col[1]) k2 = float(col[2]) k3 = float(col[3]) k4 = float(col[4]) if (dtyp_update): dtyp_cnt = d_ind + 1 dtyp_i = parmC.dtypC[dtyp_cnt] dtyp_i.set_g_indx(gfunc_type) dtyp_i.set_lmpindx(lmpindx) dtyp_i.setopls(k1, k2, k3, k4) else: dtyp_i = dihtype(ptype1, ptype2, ptype3, ptype4, dtype) dtyp_i.set_g_indx(gfunc_type) dtyp_i.set_lmpindx(lmpindx) dtyp_i.setopls(k1, k2, k3, k4) parmC.dtypC.put(dtyp_i) if (cnt_Dihedral_coeff >= n_dtypes): read_Dihedral_coeff = 0 if (read_Improper_coeff and len(col) >= 3): cnt_Improper_coeff += 1 #AT_i = int(col[0]) #AT_j = int(col[1]) imp_ind = int(col[0]) - 1 if (debug): print " reading imp dih type ", imp_ind, " cnt ", cnt_Improper_coeff, " of ", n_imptypes if (imp_ind > n_imptypes): error_line = " Error in data file index of improper parameter %d exceeds number of improper parameters %d " % ( imp_ind, n_imptypes) error_line += " specified with dihedral types " print sys.exit(error_line) IMPTYPE_REF[imp_ind][0] = "??" IMPTYPE_REF[imp_ind][1] = "??" IMPTYPE_REF[imp_ind][2] = "??" IMPTYPE_REF[imp_ind][3] = "??" # Assume OPLS dihedral type IMPTYPE_F[imp_ind] = 2 KE = float(col[1]) Eo = float(col[2]) IMPTYPE_E0[imp_ind] = Eo IMPTYPE_K[imp_ind] = KE ptype1 = "??" ptype2 = "??" ptype3 = "??" ptype4 = "??" # Set parameters according to type g_indx = 2 dtype = "improper" lmpindx = int(col[0]) if (imptyp_update): imptyp_cnt = imp_ind + 1 imptyp_i = parmC.imptypC[imptyp_cnt] imptyp_i.set_g_indx(g_indx) imptyp_i.setimp(Eo, KE) imptyp_i.set_lmpindx(lmpindx) else: imptyp_i = imptype(ptype1, ptype2, ptype3, ptype4, dtype) imptyp_i.set_g_indx(g_indx) imptyp_i.setimp(Eo, KE) imptyp_i.set_lmpindx(lmpindx) parmC.imptypC.put(imptyp_i) if (cnt_Improper_coeff >= n_imptypes): read_Improper_coeff = 0 if (read_Atoms and len(col) >= 7): cnt_Atoms += 1 ind = int(col[0]) - 1 if (ind > n_atoms): print sys.exit( " Error in data file index of atoms exceeds number of atoms specified with atoms " ) chain_i = int(col[1]) lmptype_i = int(col[2]) #- 1 indx = int(col[2]) - 1 q_i = float(col[3]) m_i = ATYPE_MASS[indx] fftype_i = ATYPE_REF[indx] el = pt.getelementWithMass(m_i) if (el.symbol == "VS"): el.symbol = ATYPE_l[atom_indx].strip() fftype_i = "VS" m_i = 0.0 # HACK !! if (ATYPE_MASS[indx] == 9.0): el.symbol = "LP" fftype_i = "LP" m_i = 0.0 r_i = [float(col[4]), float(col[5]), float(col[6])] type_i = str(lmptype_i) #tagsD = {"chain":chain_i,"symbol":el.symbol,"number":el.number,"mass":el.mass,"cov_radii":el.cov_radii,"vdw_radii":el.vdw_radii} #if( pt_overwrite ): pt_i = strucC.ptclC[ind + 1] pt_i.position = r_i pt_i.charge = q_i pt_i.mass = m_i add_dict = pt_i.tagsDict # Set properties read in data file if (set_chain_numbers): add_dict["chain"] = chain_i add_dict["symbol"] = el.symbol add_dict["number"] = el.number add_dict["mass"] = el.mass add_dict["cov_radii"] = el.cov_radii add_dict["vdw_radii"] = el.vdw_radii add_dict["lmptype"] = lmptype_i add_dict["fftype"] = fftype_i # add_dict["ffmass"] = ATYPE_MASS[indx] pt_i.setTagsDict(add_dict) if (cnt_Atoms >= n_atoms): read_Atoms = 0 if (read_Bonds and len(col) >= 4): cnt_Bonds += 1 ind = int(col[0]) - 1 if (ind > n_bonds): print sys.exit( " Error in data file index of bonds exceeds number of bonds specified with bonds " ) BTYPE_IND[ind] = int(col[1]) - 1 BONDS[ind] = [int(col[2]) - 1, int(col[3]) - 1] i_o = int(col[2]) j_o = int(col[3]) if (bonds_overwrite): bondObj = strucC.bondC[cnt_Bonds] bondObj.pgid1 = i_o bondObj.pgid2 = j_o bondObj.set_lmpindx(int(col[1])) else: bondObj = Bond(i_o, j_o) bondObj.set_lmpindx(int(col[1])) strucC.bondC.put(bondObj) if (cnt_Bonds >= n_bonds): read_Bonds = 0 if (read_Angles and len(col) >= 5): cnt_Angles += 1 ind = int(col[0]) - 1 ANGTYPE_IND[ind] = int(col[1]) - 1 ANGLES[ind] = [int(col[2]) - 1, int(col[3]) - 1, int(col[4]) - 1] k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) if (cnt_Angles >= n_angles): read_Angles = 0 if (angles_overwrite): angleObj = strucC.angleC[cnt_Angles] angleObj.pgid1 = k_o angleObj.pgid2 = i_o angleObj.pgid3 = j_o angleObj.set_lmpindx(int(col[1])) else: angleObj = Angle(k_o, i_o, j_o) angleObj.set_lmpindx(int(col[1])) strucC.angleC.put(angleObj) if (read_Dihedrals and len(col) >= 6): cnt_Dihedrals += 1 ind = int(col[0]) - 1 DTYPE_IND[ind] = int(col[1]) - 1 DIH[ind] = [ int(col[2]) - 1, int(col[3]) - 1, int(col[4]) - 1, int(col[5]) - 1 ] k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) l_o = int(col[5]) if (dih_overwrite): dObj = strucC.dihC[cnt_Dihedrals] dObj.pgid1 = k_o dObj.pgid2 = i_o dObj.pgid3 = j_o dObj.pgid4 = l_o dObj.set_lmpindx(int(col[1])) else: dObj = Dihedral(k_o, i_o, j_o, l_o) dObj.set_lmpindx(int(col[1])) strucC.dihC.put(dObj) if (cnt_Dihedrals >= n_dihedrals): read_Dihedrals = 0 if (read_Impropers and len(col) >= 2): cnt_Impropers += 1 ind = int(col[0]) - 1 k_o = int(col[2]) i_o = int(col[3]) j_o = int(col[4]) l_o = int(col[5]) if (imp_overwrite): impObj = strucC.impC[cnt_Impropers] impObj.pgid1 = k_o impObj.pgid2 = i_o impObj.pgid3 = j_o impObj.pgid4 = l_o impObj.set_lmpindx(int(col[1])) impObj.set_type("improper") else: impObj = Improper(k_o, i_o, j_o, l_o) impObj.set_lmpindx(int(col[1])) impObj.set_type("improper") strucC.impC.put(impObj) if (cnt_Impropers >= n_impropers): read_Impropers = 0 if (len(col) >= 1): if (col[0] == "Masses"): read_Masses = 1 cnt_Masses = 0 if (col[0] == "Atoms"): read_Atoms = 1 cnt_Atoms = 0 if (col[0] == "Bonds"): read_Bonds = 1 cnt_Bonds = 0 if (col[0] == "Angles"): read_Angles = 1 cnt_Angles = 0 if (col[0] == "Dihedrals"): read_Dihedrals = 1 cnt_Dihedrals = 0 if (col[0] == "Impropers"): read_Impropers = 1 cnt_Impropers = 0 if (len(col) >= 2): if (col[0] == "Pair" and col[1] == "Coeffs"): read_Pair = 1 cnt_Pair = 0 if (col[0] == "Bond" and col[1] == "Coeffs"): read_Bond_coeff = 1 cnt_Bond_coeff = 0 if (col[0] == "Angle" and col[1] == "Coeffs"): read_Angle_coeff = 1 cnt_Angle_coeff = 0 if (col[0] == "Dihedral" and col[1] == "Coeffs"): read_Dihedral_coeff = 1 cnt_Dihedral_coeff = 0 if (col[0] == "Improper" and col[1] == "Coeffs"): read_Improper_coeff = 1 cnt_Improper_coeff = 0 # cnt_Bonds += 1 # ind = int( col[0]) - 1 # BTYPE_IND[ind] = int(col[1] ) - 1 # BONDS[ind][0] = int(col[2]) # if( cnt_Bonds >= n_atoms ): # read_Bonds = 0 # # if (ljtyp_update): if (ljtyp_cnt != len(parmC.ljtypC)): print " Number of LJ types read in %d does not match previously read %d " % ( ljtyp_cnt, len(parmC.ljtypC)) if (debug): for ind in range(len(ATYPE_MASS)): print ind + 1, ATYPE_MASS[ind] for ind in range(len(ATYPE_EP)): print ind + 1, ATYPE_EP[ind], ATYPE_SIG[ind] for ind in range(n_btypes): print ind + 1, BONDTYPE_R0[ind], BONDTYPE_K[ind] for ind in range(n_angtypes): print ind + 1, ANGLETYPE_R0[ind], ANGLETYPE_K[ind] for ind in range(n_dtypes): print ind + 1, DIHTYPE_C[ind] debug = 0 if (debug): for ind in range(len(BONDS)): print ind + 1, BONDS[ind] if (debug): sys.exit("debug 1 ") # # return (strucC, parmC)
def main(): """ This test shows various operators within Dihedral and DihedralContainer classes Also shows memory management structure and access methods """ print "************************************************************************************" print " This test shows various operators within Dihedral and DihedralContainer classes " print " Also shows memory management structure and access methods " print "************************************************************************************ \n" p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "Si", 1.0, 2.34) p4 = Particle([4.4, 4.4, 4.4], "Si", 1.0, 2.34) # p5 = Particle([5.5, 5.5, 5.5], "C", 1.0, 2.34) p6 = Particle([6.6, 6.6, 6.6], "C", 1.0, 2.34) p7 = Particle([7.7, 7.7, 7.7], "C", 1.0, 2.34) p8 = Particle([8.8, 8.8, 8.8], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.11, "hooke") b2 = Bond(2, 3, 2.22, "hooke") # b3 = Bond(1, 2, 3.33, "hooke") b4 = Bond(1, 2, 4.44, "hooke") d1 = Dihedral(1, 2, 3, 4, 1.11, "stiff") # d2 = Dihedral(1, 2, 3, 4, 2.22, "stiff") atoms1 = ParticleContainer() atoms2 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) # atoms2.put(p4) atoms2.put(p5) atoms2.put(p6) bonds1 = BondContainer() bonds2 = BondContainer() bonds1.put(b1) bonds1.put(b2) bonds2.put(b3) bonds2.put(b4) dihedrals1 = DihedralContainer() dihedrals2 = DihedralContainer() dihedrals1.put(d1) dihedrals2.put(d2) del p1, p2, p3, p4, p5, p6, b1, b2, b3, b4, d1, d2 print "\n Cleaning memory for initial objects \n" print "dihedral1 container" print dihedrals1 print " " print "dihedral2 container" print dihedrals2 print "Testing 'in' operator (1 in dihedrals1)" if (1 in dihedrals1): print "dihedrals1 contains gid 1" else: print "key not found in dihedrals1" print "Testing 'in' operator (5 in dihedrals1)" if (5 in dihedrals1): print "dihedrals1 contains gid 5" else: print "key not found in dihedrals1" print " " dihedrals1 += dihedrals2 print "Will print the new dihedrals1 after adding dihedrals1 += dihedrals2" print dihedrals1 print "Check for pre-existing dihedral" if dihedrals1.hasDihedral([4, 3, 2, 1]): print "dihedral 1--2--3--4 exists" else: print "dihedral 1--2--3--4 does NOT exists" print "Check for pre-existing dihedral" if dihedrals1.hasDihedral([2, 3, 1, 4]): print "dihedral 2--3--1--4 exists" else: print "dihedral 2--3--1--4 does NOT exists"
def main(): """ This test shows compressing IDs when structureContainer has angles included """ p1 = Particle([1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle([2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle([3.3, 3.3, 3.3], "Si", 1.0, 2.34) p4 = Particle([0.0, 0.0, 0.0], "Si", 1.0, 2.34) # p5 = Particle([4.4, 4.4, 4.4], "C", 1.0, 2.34) # 1 p6 = Particle([5.5, 5.5, 5.5], "C", 1.0, 2.34) # 2 p7 = Particle([6.6, 6.6, 6.6], "C", 1.0, 2.34) # 3 b1 = Bond(1, 3, 1.11, "hooke") b2 = Bond(3, 4, 2.22, "hooke") # b3 = Bond(1, 2, 3.33, "hooke") b4 = Bond(2, 3, 4.44, "hooke") a1 = Angle(1, 3, 4, 1.11, "harmonic") # a2 = Angle(1, 2, 3, 2.22, "harmonic") atoms1 = ParticleContainer() atoms2 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) atoms1.put(p4) # atoms2.put(p5) atoms2.put(p6) atoms2.put(p7) del atoms1[2] # remove 2nd particle so ID's are non-consecutive bonds1 = BondContainer() bonds2 = BondContainer() bonds1.put(b1) bonds1.put(b2) bonds2.put(b3) bonds2.put(b4) angles1 = AngleContainer() angles2 = AngleContainer() angles1.put(a1) angles2.put(a2) polymer1 = StructureContainer(atoms1, bonds1, angles1) polymer2 = StructureContainer(atoms2, bonds2, angles2) del p1, p2, p3, p4, p5, p6, b1, b2, b3, b4, a1, a2 del atoms1, atoms2, bonds1, bonds2, angles1, angles2 print "\n Cleaning memory for initial objects \n" print "-------------------- Initial structures --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------- After adding (polymer2 += polymer1) ----------------" polymer2 += polymer1 print "polymer2 = ", polymer2 polymer2.compressPtclIDs() print "-------------------- After compressing --------------------" print "polymer2 = ", polymer2
def __iadd__(self, other): """ 'Magic' method to implement the '+=' operator (eg struc1 += struc2) Compare global IDs of particles and reassign globalIDs for particle container using the max ID between the two lists. Tracks these changes for all other (bond, angle, dihedral) containers that reference particleIDs """ self.verbose = False # Empty container checks if len(other) == 0: # If struc2 is empty (has no particles) return self # simply return unchanged current container if len(self ) == 0: # If struc1 (this struc) is empty (has no particles) return copy.deepcopy(other) # full copy other and return # # Special method for connecting building blocks # # Count number of connectors connect_along_bond = False if (self.type != "mol" and other.type != "mol"): # IF not complete molecules that will just be added # connect along bonds connect_along_bond = True self.cnt_connectors() other.cnt_connectors() if (self.verbose): log_line = "\n building block 1 type {} ".format(self.type) log_line += "\n building block 1 has {} connections ".format( self.connect_cnt) log_line += "\n building block 1 has {} func connections ".format( self.func_connect_cnt) log_line += "\n building block 2 type {} ".format(other.type) log_line += "\n building block 2 has {} connections ".format( other.connect_cnt) log_line += "\n building block 2 has {} func connections ".format( other.func_connect_cnt) print log_line self.connect_id = "term_" self.cap_id = "termcap_" other.connect_id = "term_" other.cap_id = "termcap_" if (self.type == "unit" and other.type == "unit"): self.connectionpoint = 1 other.connectionpoint = 0 new_termpoint = 1 elif (self.type == "term" and other.type == "unit"): # elif( self.connect_cnt == 1 and other.connect_cnt == 2 ): self.connectionpoint = 0 other.connectionpoint = 0 new_termpoint = 0 elif (self.type == "term" and other.type == "term"): # elif( self.connect_cnt == 1 and other.connect_cnt == 1 ): self.connectionpoint = 0 other.connectionpoint = 0 new_termpoint = -1 elif (self.type == "unit" and other.type == "term"): #elif( self.connect_cnt == 2 and other.connect_cnt == 1 ): self.connectionpoint = 1 other.connectionpoint = 0 new_termpoint = -1 elif (self.type == "unit" and other.type == "func"): #elif( self.connect_cnt == 2 and other.connect_cnt == 1 ): self.connect_id = "func_" self.cap_id = "funccap_" # this is zero since each functional possition will be removed once a functional group is added self.connectionpoint = 0 other.connect_id = "term_" other.connectionpoint = 0 new_termpoint = -1 else: error_line = " Unknow connection configuration {} - {} \n".format( self.type, other.type) error_line += " building block 1 has {} connnections \n".format( self.connect_cnt) error_line += " building block 2 has {} connnections \n".format( other.connect_cnt) sys.exit(error_line) if (self.verbose): log_line = "\n Adding connection along bond " log_line += "\n Connecting block 2 {} at point {} ".format( other.connect_id, other.connectionpoint) log_line += "\n to block 1 {} at point {} ".format( self.connect_id, self.connectionpoint) print log_line # Shift second buildingblock so first connector is bonded to the second connector of first buildingblock if (self.verbose): print " Shift along bonds " self.align_termbond(self.connectionpoint, origin="term") other.align_termbond(other.connectionpoint, origin="termcap") if (self.verbose): print " Find terminal atoms " self.get_connectors(self.connectionpoint) other.get_connectors(other.connectionpoint) bond_length = np.array([ self.ptclC[self.pid_term].radii + other.ptclC[other.pid_term].radii, 0.0, 0.0 ]) # Shift second building block to have the correct bond length other.ptclC.shift( (bond_length - 1.0 * np.array(other.ptclC[other.pid_term].position))) # Delete termcaps self.bondC.deletepid(self.pid_termcap) other.bondC.deletepid(other.pid_termcap) del self.ptclC[self.pid_termcap] del other.ptclC[other.pid_termcap] # Reset term tags to null self.ptclC[self.pid_term].tagsDict["cplytag"] = "" other.ptclC[other.pid_term].tagsDict["cplytag"] = "" # If an intern ring connection is made change the fftype if (self.ptclC[self.pid_term].tagsDict["ring"] > 0 and other.ptclC[other.pid_term].tagsDict["ring"] > 0): #print " ring self ", self.ptclC[self.pid_term].tagsDict["ring"] #print " ring other ", other.ptclC[other.pid_term].tagsDict["ring"] self.ptclC[self.pid_term].tagsDict["fftype"] = 'C!' other.ptclC[other.pid_term].tagsDict["fftype"] = 'C!' # sys.exit(" ring test ") idFromToDict = dict( ) # Need to keep track of all ptcl ID changes at once # {fromID1:toID1, fromID2:toID2...} # eg {1:3, 3:5, 2:20...} bondC = BondContainer() # Local bond container copy so ptclIDs angleC = AngleContainer() # Local angle container copy so ptclIDs dihC = DihedralContainer() # Local dihedral container copy so ptclIDs bondC = copy.deepcopy( other.bondC) # inside can be changed (for adding below) angleC = copy.deepcopy( other.angleC) # inside can be changed (for adding below) dihC = copy.deepcopy( other.dihC) # inside can be changed (for adding below) impC = copy.deepcopy( other.impC) # inside can be changed (for adding below) keys1 = self.ptclC.particles.keys( ) # global IDs of particles in this object keys2 = other.ptclC.particles.keys( ) # global IDs in object being added self.ptclC.maxgid = max( keys1 + keys2) # find max globalID in keys, set this object maxID self.get_max() other.get_max() # Update chain, residue and charge group number if (self.max_chain == other.max_chain): other.shift_tag("qgroup", self.max_qgroup) other.shift_tag("residue", self.max_residue) other.shift_tag("ring", self.max_ring) else: other.add_chain(self.max_chain) for ptclkey2 in other.ptclC.particles: self.ptclC.put(other.ptclC.particles[ptclkey2] ) # Pushes ptcl to this struc's ptcl container fromPtclID = ptclkey2 # Track IDs from--->to toPtclID = self.ptclC.maxgid # --> toID (to is the maxid of this ptclC) idFromToDict[fromPtclID] = toPtclID # Store ID changes if (connect_along_bond): # Add inter building block bond if (self.verbose): print " Add inter building block bond " bb_bb = Bond(self.pid_term, idFromToDict[other.pid_term]) self.bondC.put(bb_bb) bondC.replacePtclIDs(idFromToDict) # Use tracked list of ID changes self.bondC += bondC # Now add bondC with 'corrected' IDs angleC.replacePtclIDs( idFromToDict) # Now add angleC with 'corrected' IDs self.angleC += angleC # Use tracked list of ID changes dihC.replacePtclIDs(idFromToDict) # Use tracked list of ID changes self.dihC += dihC # Now add dihC with 'corrected' IDs impC.replacePtclIDs(idFromToDict) # Use tracked list of ID changes self.impC += impC # Now add impC with 'corrected' IDs if (connect_along_bond): self.compressPtclIDs() self.bondC_nblist() if (new_termpoint >= 0): if (self.verbose): print " Add new cplytag to " self.get_connectors(new_termpoint) self.ptclC[ self.pid_term].tagsDict["cplytag"] = "term_C({})".format( self.pid_term) self.ptclC[self.pid_termcap].tagsDict[ "cplytag"] = "termcap_H({})_on_C({})".format( self.pid_termcap, self.pid_term) # Append segments for seg_i in other.segments: self.segments.append(seg_i) return self
def main(): """ This test shows how to add StructureContainer objects together """ print "************************************************************************************" print " This test shows how to add StructureContainer objects together " print "************************************************************************************ \n" p1 = Particle([0.2, 1.3, 33.0], "Si", 2.0, 1.23) p2 = Particle([5.0, 2.3, -22.1], "C", 1.0, 2.34) p3 = Particle([5.0, 2.3, -20.1], "C", 1.0, 2.34) b1 = Bond(1, 2, 1.233, "hooke") b2 = Bond(2, 3, 0.500, "hooke") atoms1 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) bonds1 = BondContainer() bonds1.put(b1) bonds1.put(b2) polymer1 = StructureContainer(atoms1, bonds1) # Complete structure 1 p1other = Particle([0.0, 2.3, -20.1], "C", 1.0, 2.34) p2other = Particle([50.0, 0.3, -0.1], "Ar", 2.0, 2.34) b1other = Bond(1, 2, 1.233, "hooke") # Correct ptclIDs for second structure atoms2 = ParticleContainer() atoms2.put(p1other) atoms2.put(p2other) bonds2 = BondContainer() bonds2.put(b1other) polymer2 = StructureContainer(atoms2, bonds2) # Complete structure 2 print "Number of particles in polymer2 = ", polymer2.getPtclNum() del p1, p2, p3, p1other, p2other, b1, b2, b1other, atoms1, atoms2, bonds1, bonds2 print "\n Cleaning memory for initial objects \n" print "-------------------- Before adding --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------------- After adding --------------------" polymer1 += polymer2 print "polymer1 = ", polymer1 print "Number of particles in polymer1 after add = ", polymer1.getPtclNum() print "-------------------- Results (check above) --------------------" print " 1---b1---2---b2---3 + 1---b1----2 should go to" print " " print " 1---b1---2---b2---3 4---b3----5 \n" print " " print "------ After adding polymer 2 should be unchanged--------------" print "polymer2 = ", polymer2
def main(): """ Tests a specifc workflow for LAMMPS that uses the simulationLAMMPS1 derived class """ global g global p # Get useful methods g=Geometry() # Get comm object p = mpiBase.getMPISerialObject() rank = p.getRank() size = p.getCommSize() # Sync random stream for tests random.seed(0) ############################################################################################# # # Generate list of [gid, x,y,z] points ---> allPoints list # copied on all processors. # Global pt index included # UNITS distance --> A [0.1 nm] # ############################################################################################# rad_avg = 15.0 # Moving to use this to make rad_sig = 0.025 # initial cubic grid thats used to pass to MD ptcl_dist = 10.0 # # NOTE: this is an arbitrary choice cutoff_dist = math.sqrt(2)*((2.00 * rad_avg) + ptcl_dist) nQD_spacing = (2.00 * rad_avg) + ptcl_dist # Makes number of points along side grid nQDs = [5, 5, 5] sysLs = setSystemSizes(nQDs, nQD_spacing) # Set boundary condition list (used in PBC calc) xL = sysLs[0] yL = sysLs[1] zL = sysLs[2] bcLs = [0.0, yL[1]-yL[0], zL[1]-zL[0] ] # x is NOT PB # Status info if rank == 0: print "rad_avg = ", rad_avg print "rad_sig = ", rad_sig print "cutoff_dist = ", cutoff_dist print " " print "bcLs = ", bcLs print "sysLs = ", sysLs print " " ############################################################################################# # Generate initial points p.barrier() tmpPoints = setRandomGridPoints(nQDs, rad_avg, ptcl_dist) # Generate global pts lst allPoints = p.bcast(tmpPoints) # ensures rand pts same across procs numpoints = len(allPoints) # Total number of points (for diag) if numpoints == 0: print "Number of points generated == 0" sys.exit(3) myPoints = p.splitListOnProcs(allPoints) # Split elements on across processors p.barrier() ############################################################################################# ################################################################### # Store particles in struc and find neighbors/bonds # ptPos includes index eg. [1, 3.4, 1.2, -2.0] nanoPtcls = ParticleContainer() nanoBonds = BondContainer() for ptPos in allPoints: axisLoc = [bcLs[1]/2.0, bcLs[2]/2.0] cylinderRadius = 120.0 inside = isPtInCylinder(ptPos[1:], axisLoc, "yz", cylinderRadius) if inside: pt = Particle(ptPos[1:], type="QDbig", mass=2.0) tagsD = {"molnum":1, "QDSizeAvg":20.0} else: pt = Particle(ptPos[1:], type="QDsmall", mass=1.0) tagsD = {"molnum":1, "QDSizeAvg":15.0} pt.setTagsDict(tagsD) nanoPtcls.put(pt) p.barrier() # Get bond info allDist, allNeighbors, numTotalNeighbors, allBonds = \ setQDNeighbors(myPoints, allPoints, cutoff_dist, bcLs, rank, size) if rank == 0: print "Begin building bond container" # Put bonds into container for bond in allBonds: if not nanoBonds.hasBond(bond): # Check if bond is unique bnd = Bond(bond[0], bond[1], type="normBond") # Put into STREAMM object nanoBonds.put(bnd) # add if not in container p.barrier() if rank == 0: print "length of bond container = ", len(nanoBonds) ############################################################################### ############################################################################ # Write LAMMPS file with atoms/bonds strucQD = StructureContainer(nanoPtcls, nanoBonds) simObjQD = SimulationLAMMPS1("LammpsAug14", verbose=False) simObjQD.setStructureContainer(strucQD) if isinstance(simObjQD, SimulationLAMMPS1): print "strucQD is a SimulationLAMMPS1" else: print "strucQD is NOT a SimulationLAMMPS1" boxSizes = sysLs strucQD.setBoxLengths(boxSizes) small_rad_avg = 15.0 big_bond_min = (2.0* rad_avg) + ptcl_dist small_sigma_min = ((2.0*small_rad_avg) + ptcl_dist) / pow(2, 1/6.) big_sigma_min = ((2.0* rad_avg) + ptcl_dist) / pow(2, 1/6.) ptclParamMap = {("QDsmall", "epsilon"):1.0, ("QDsmall", "sigma"):small_sigma_min, ("QDbig", "epsilon"):1.0, ("QDbig", "sigma"):big_sigma_min} # Just one bond type for now bondParamMap = {("normBond", "Kenergy"):1.0, ("normBond", "r0"):big_bond_min} if rank == 0: # nanoPtcls.scatterPlot() # strucQD.dumpLammpsInputFile("qd.data", ptclParamMap, bondParamMap) # strucQD.writeInput("qd.data", ptclParamMap, bondParamMap) # simObjQD.writeInput("qd.data", ptclParamMap, bondParamMap) simObjQD.setCoeffs(ptclParamMap, bondParamMap) simObjQD.writeInput("qd.data") # For format of test check os.system("cat qd.data")
def main(): """ This test shows structureContainer functionality with angles included """ print "************************************************************************************" print " This test shows structureContainer functionality with angles included" print "************************************************************************************ \n" p1 = Particle( [1.1, 1.1, 1.1], "Si", 2.0, 1.23) p2 = Particle( [2.2, 2.2, 2.2], "Si", 1.0, 2.34) p3 = Particle( [3.3, 3.3, 3.3], "Si", 1.0, 2.34) # p4 = Particle( [4.4, 4.4, 4.4], "C", 1.0, 2.34) # 1 p5 = Particle( [5.5, 5.5, 5.5], "C", 1.0, 2.34) # 2 p6 = Particle( [6.6, 6.6, 6.6], "C", 1.0, 2.34) # 3 b1 = Bond( 1, 2, 1.11, "hooke") b2 = Bond( 2, 3, 2.22, "hooke") # b3 = Bond( 1, 2, 3.33, "hooke") b4 = Bond( 2, 3, 4.44, "hooke") a1 = Angle(1, 2, 3, 1.11, "harmonic") # a2 = Angle(1, 2, 3, 2.22, "harmonic") atoms1 = ParticleContainer() atoms2 = ParticleContainer() atoms1.put(p1) atoms1.put(p2) atoms1.put(p3) # atoms2.put(p4) atoms2.put(p5) atoms2.put(p6) bonds1 = BondContainer() bonds2 = BondContainer() bonds1.put(b1) bonds1.put(b2) bonds2.put(b3) bonds2.put(b4) angles1 = AngleContainer() angles2 = AngleContainer() angles1.put(a1) angles2.put(a2) polymer1 = StructureContainer(atoms1, bonds1, angles1) polymer2 = StructureContainer(atoms2, bonds2, angles2) del p1, p2, p3, p4, p5, p6, b1, b2, b3, b4, a1, a2 del atoms1, atoms2, bonds1, bonds2, angles1, angles2 print "\n Cleaning memory for initial objects \n" print "-------------------- Initial structures --------------------" print "polymer1 = ", polymer1 print "polymer2 = ", polymer2 print " " print "-------------- After adding (polymer1 += polymer2) ----------------" polymer2 += polymer1 print "polymer2 = ", polymer2