def setup_test_molecules(): molecule1 = molecule_import.Molecule(1, 0, 0) molecule2 = molecule_import.Molecule(2, 0, 0) molecule3 = molecule_import.Molecule(3, HEIGHT, WIDTH) list_of_molecules.append(molecule1) list_of_molecules.append(molecule2) list_of_molecules.append(molecule3)
def splitBox(mole, bound, center, cutoff, atom_filter=lambda x: True): """Split molecule `mole` into 2 parts: The atoms inside a box shell centered at `center` with size range `cutoff`, and the ones that are outside. Return 2 molecules. Distance is calculated with boundary condition `bound`. :type mole: molecule.Molecule :type bound: molecule.PeriodicBox :type center: Matrix.Vector3D :type cutoff: tuple :rtype: tuple """ MolInside = molecule.Molecule() MolOutside = molecule.Molecule() for Atom in mole: if not atom_filter(Atom): continue if not bound.inBox(center, Atom.Loc, (cutoff[0],) * 3) and \ bound.inBox(center, Atom.Loc, (cutoff[1],) * 3): MolInside.addAtom(Atom) else: MolOutside.addAtom(Atom) return MolInside, MolOutside
def basic_filtration_test(self): """Test that structures with higher octet deviation get filtered out""" adj1 = """ multiplicity 2 1 N u0 p1 c0 {2,D} {3,S} 2 O u0 p2 c0 {1,D} 3 O u1 p2 c0 {1,S} """ adj2 = """ multiplicity 2 1 N u1 p1 c0 {2,S} {3,S} 2 O u0 p2 c+1 {1,S} 3 O u0 p3 c-1 {1,S} """ adj3 = """ multiplicity 2 1 N u1 p0 c0 {2,T} {3,S} 2 O u0 p1 c+1 {1,T} 3 O u0 p3 c-1 {1,S} """ mol1 = molecule.Molecule().fromAdjacencyList(adj1) mol2 = molecule.Molecule().fromAdjacencyList(adj2) mol3 = molecule.Molecule().fromAdjacencyList(adj3) mol_list = [mol1, mol2, mol3] octet_deviation_list = get_octet_deviation_list(mol_list) filtered_list = filter_structures(mol_list) self.assertEqual(octet_deviation_list, [1, 3, 3]) self.assertEqual(len(filtered_list), 1) self.assertTrue( all([atom.charge == 0 for atom in filtered_list[0].vertices]))
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" mol = molecule.Molecule(np.array([0.2, 0.2], dtype=float), np.array([0.8, 0.8], dtype=float), 1, 2, 1, 0.5) return mol
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" pos1, mass1 = np.array([0.2, 0.2]), 3 pos2, mass2 = np.array([0.8, 0.8]), 3 k, l = 0.1, 0.5 return mol.Molecule(pos1, mass1, pos2, mass2, k, l)
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" p1 = particle.Particle(np.array([.2, .2]), 1) p2 = particle.Particle(np.array([.8, .8]), 2) return molecule.Molecule(p1.pos, p2.pos, p1.m, p2.m, 1, .5)
def __init__(self, mole_str, hess_str): """ Initializes some values then preceeds to calculate normal coordinates and spatial frequencies. :param mole_str: string used to create instance of a new molecule :param hess_str: string used to create Hessian matrix """ self.units = "hertz" self.mol = molecule.Molecule(mole_str) self.mol.to_bohr() self.parse_hessian(hess_str) self.mwhess = self.mass_weight_mat(self.hess, self.mol.masses) self.eigVal, self.eigVec = np.linalg.eig(self.mwhess) """ for a in range(len(self.eigVal)): print np.dot(self.mwhess, self.eigVec[a]) print self.eigVal[a]*self.eigVec[a] """ self.normCoord = np.array([[ (self.eigVec[b][a] / math.sqrt(self.mol.masses[b / 3])) for b in range(len(self.eigVec[a])) ] for a in range(len(self.eigVec))]) self.eigVal *= 4.35974465054 * 10**(-18) #hatree to joule self.eigVal /= (1.6605389 * 10**(-27)) #amu to k self.eigVal /= ((5.2917721067 * 10**(-11))**2) #bohr to m self.spFreq = np.empty([len(self.eigVal)], dtype=complex) for a in range(len(self.eigVal)): self.spFreq[a] = cmath.sqrt(self.eigVal[a]) / (2.0 * math.pi) self.to_wavenumber() with open('project-1-answers.xyz', "w+") as f: f.write(self.xyz_string())
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" p1 = particle.Particle(np.random.rand(2), initial_p1_mass) p2 = particle.Particle(np.random.rand(2), initial_p2_mass) return molecule.Molecule(p1.pos, p2.pos, p1.m, p2.m, initial_k, initial_e_length)
def moleShellCutoff(mol_center, mol_around, cutoff, boundary, atom_filter_center=lambda x: True, atom_filter_around=lambda x: True): """Filter atoms in `mol_center` with `atom_filter_center` into set A, and filter atoms in `mol_around` with `atom_filter_around` into set B. Return all atoms in B as a molecule who are within a distance range of any atoms in A. The distance range is specified by the 2-tuple `cutoff`. :type mol_center: molecule.Molecule :type mol_around: molecule.Molecule :type cutoff: tuple :rtype: molecule.Molecule """ Center = tuple(at for at in mol_center if atom_filter_center(at)) Result = molecule.Molecule() Ids = set() for Atom in mol_around: if not atom_filter_around(Atom): continue for AtomCenter in Center: if boundary.inBox(AtomCenter.Loc, Atom.Loc, (cutoff[1],) * 3) and \ not boundary.inBox(AtomCenter.Loc, Atom.Loc, (cutoff[0],) * 3): if id(Atom) not in Ids: Result.addAtom(Atom) Ids.add(id(Atom)) return Result
def Structure(input_data, **kwargs): if type(input_data) is not qg.Molecule: try: return qg.Molecule(input_data) except: pass else: return input_data
def __init__(self): # all information relating to the field is stored here self.field = sf.ScalarField() # all atomic information is stored here self.molecule = mol.Molecule() self.settings = CubeSettings() self.file = None self.name = None
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" p1_pos = np.array([0.2, 0.2]) p1_mass = 1 p2_pos = np.array([0.8, 0.8]) p2_mass = 2 k = 1 L0 = 0.5 return molecule.Molecule(p1_pos, p1_mass, p2_pos, p2_mass, k, L0)
def randPlaceAround(small, count, center, box_size, rand_rot=False): """Randomly place `count` number of `small` molecules around molecule `center` inside a cubic box with `box_size`, so that all instances of `small` will not be inside `center`. `Center` can be `None`, which means no center molecule will be considered. """ if center is None: Radius = 0.0 else: Radius = center.radius() Box = small.boundingBox() * 0.5 Logger.debug("Bounding box of small molecule: " + str(Box)) SmallZeroed = copy.deepcopy(small) SmallZeroed.translate(-(small.GeoCenter)) # SmallZeroed is now center at (0,0,0). if center is not None: Logger.info("Center molecule is at ({}) with radius {:.2f}.".format( center.GeoCenter, Radius)) if center is None: CenterCenter = matrix.Vector3D(0, 0, 0) # This doesn't actually do anything. else: CenterCenter = center.GeoCenter Result = molecule.Molecule() iRaw = 0 i = 0 while i < count: TransVec = matrix.Vector3D( random.uniform(-box_size / 2.0 + Box.x, box_size / 2.0 - Box.x), random.uniform(-box_size / 2.0 + Box.y, box_size / 2.0 - Box.y), random.uniform(-box_size / 2.0 + Box.z, box_size / 2.0 - Box.z)) if TransVec.distanceFrom(CenterCenter) >= Radius: Logger.debug("Translation vector: " + str(TransVec)) NewMol = SmallZeroed.duplicate() if rand_rot: RotMat = randRotation() NewMol.rotate(RotMat) for a in NewMol: a.ResidueNum = i + 1 a.ResidueID = "HEX1" NewMol.translate(TransVec) Logger.debug("Translated center: " + str(NewMol.GeoCenter)) for a in NewMol: Logger.debug(str(a)) Result.addMolecule(NewMol) i += 1 iRaw += 1 Logger.info("Dropped {:.1f}% random samples.".format( (iRaw - i) / iRaw * 100.0)) return Result
def penalty_for_O4tc_test(self): """Test that an O4tc atomType with octet 8 gets penalized in the octet deviation score""" adj = """ 1 S u0 p1 c0 {2,S} {3,T} 2 O u0 p3 c-1 {1,S} 3 O u0 p1 c+1 {1,T} """ mol = molecule.Molecule().fromAdjacencyList(adj) octet_deviation = get_octet_deviation(mol) self.assertEqual(octet_deviation, 1) self.assertEqual(mol.vertices[2].atomType.label, 'O4tc')
def penalty_for_N_val_9_test(self): """Test that N atoms with valance 9 get penalized in the octet deviation score""" adj = """ multiplicity 2 1 N u1 p0 c0 {2,S} {3,T} 2 O u0 p2 c0 {1,S} {4,S} 3 N u0 p1 c0 {1,T} 4 H u0 p0 c0 {2,S} """ mol = molecule.Molecule().fromAdjacencyList(adj) octet_deviation = get_octet_deviation(mol) self.assertEqual(octet_deviation, 2)
def init_molecule(): """Create Particles p1 and p2 inside boundaries and return a molecule connecting them""" pos1 = np.array([[0.2], [0.2]]) pos2 = np.array([[0.8], [0.8]]) m1 = 1 m2 = 2 k = 1 L0 = 0.5 mol = molecule.Molecule(pos1, pos2, m1, m2, k, L0) return mol
def optimize_geometry(settings, unopt_path, opt_path): config = configparser.ConfigParser(allow_no_value=False) config.read(settings) qcalc.init(config, config["files"]["log_path"] + "/" + "optimize") with open(unopt_path, "r") as unopt_file: unopt_molecule = molecule.Molecule(unopt_file.read()) opt_molecule, energy = qcalc.optimize(unopt_molecule, config) output_writer.write_optimized_geo(opt_molecule, energy, opt_path)
def identity(fin, fout, debug): initial = molecule.Molecule(fin, debug) final = molecule.Molecule(fout, debug) atoms = abs(initial.atoms - final.atoms) bonds = abs(initial.bonds - final.bonds) diff = 0 for el, val in initial.atomsByType.items(): if final.atomsByType.has_key(el): diff += abs(val - final.atomsByType[el]) # common.log('{0}: {1} --- {2} --> {3}', [el, val, final.atomsByType[el], diff], debug) else: diff += val # common.log('{0}: {1} --- 0 --> {2}', [el, val, diff], debug) for el, val in final.atomsByType.items(): if not initial.atomsByType.has_key(el): diff += val # common.log('{0}: 0 --- {1} --> {2}', [el, val, diff], debug) # for el in initial.bondsByType.keys(): # if final.bondsByType.has_key(el): # diff += abs(initial.bondsByType[el] - final.bondsByType[el]) # if debug: # print '{0}: {1} --- {2} --> {3}'.format(el, initial.bondsByType[el], final.bondsByType[el], diff) # else: # diff += initial.bondsByType[el] # if debug: # print '{0}: {1} --- 0 --> {3}'.format(el, initial.bondsByType[el], diff) # for el in final.bondsByType.keys(): # if not initial.bondsByType.has_key(el): # diff += final.bondsByType[el] # if debug: # print '{0}: 0 --- {2} --> {3}'.format(el, final.bondsByType[el], diff) return diff, atoms, bonds
def generate_normal_modes(settings, geo_path, normal_modes_file): config = configparser.ConfigParser(allow_no_value=False) config.read(settings) psi4_helper.init(config, config["files"]["log_path"] + "/" + "optimize") with open(geo_path, "r") as geo_file: molec = molecule.Molecule(geo_file.read()) normal_modes, frequencies, red_masses = qcalc.frequencies(molec, config) dim_null = 3 * molec.num_atoms - len(normal_modes) output_writer.write_normal_modes(normal_modes, frequencies, red_masses, normal_modes_file) print("DIM NULL: " + str(dim_null))
def placeByGrid(ns, big_box_size, mol): Result = molecule.Molecule() for ix in range(ns): for iy in range(ns): for iz in range(ns): Pos = Matrix.Vector3D( big_box_size / ns * ix - big_box_size * 0.5, big_box_size / ns * iy - big_box_size * 0.5, big_box_size / ns * iz - big_box_size * 0.5) Single = random_mol.randPlaceAround(mol, 1, None, big_box_size / ns, True) Single.translate(Pos) Result.addMolecule(Single) return Result
def main(): import sys import getopt import molecule usage = \ """ Copyright (c) 2007 Bosco Ho Calculates the total Accessible Surface Area (ASA) of atoms in a PDB file. Usage: asa.py -s n_sphere in_pdb [out_pdb] - out_pdb PDB file in which the atomic ASA values are written to the b-factor column. -s n_sphere number of points used in generating the spherical dot-density for the calculation (default=960). The more points, the more accurate (but slower) the calculation. """ opts, args = getopt.getopt(sys.argv[1:], "n:") if len(args) < 1: print usage return mol = molecule.Molecule(args[0]) atoms = mol.atoms() molecule.add_radii(atoms) n_sphere = 60 for o, a in opts: if '-n' in o: n_sphere = int(a) print "Points on sphere: ", n_sphere asas = calculate_asa(atoms, 1.4, n_sphere) print "%.1f angstrom squared." % sum(asas) if len(args) > 1: for asa, atom in zip(asas, atoms): atom.bfactor = asa mol.write_pdb(args[1])
def penalty_for_s_triple_s_test(self): """Test that an S#S substructure in a molecule gets penalized in the octet deviation score""" adj = """ 1 C u0 p0 c0 {3,S} {5,S} {6,S} {7,S} 2 C u0 p0 c0 {4,S} {8,S} {9,S} {10,S} 3 S u0 p0 c0 {1,S} {4,T} {11,D} 4 S u0 p1 c0 {2,S} {3,T} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {1,S} 7 H u0 p0 c0 {1,S} 8 H u0 p0 c0 {2,S} 9 H u0 p0 c0 {2,S} 10 H u0 p0 c0 {2,S} 11 O u0 p2 c0 {3,D} """ mol = molecule.Molecule().fromAdjacencyList(adj) octet_deviation = get_octet_deviation(mol) self.assertEqual(octet_deviation, 1)
def process(self): ''' process input from both web form and CLI ''' if not self.webform: self.parseInputCli() else: self.parseInputWeb() # let toolkit parse the molecule, and process it tkmol = self.parseMolecule() # we now know how to deal with orphan atoms #atoms, bonds = tkmol.countAtoms(), tkmol.countBonds() #if atoms <= 1 or bonds == 0: #raise common.MCFError, "Input contains no bonds---can't render structure" mol = molecule.Molecule(self.options, tkmol) return mol
def estimate(cor_number=0, size="png:w1920,h1080"): smi = "current.smi" mol = "current.mol" # TODO - user degrees degmax = {'C': 4, 'N': 3, '0': 2, 'H': 1, 'Br': 1, 'Cl': 1} undefined = 0 failed = 0 wrong_deg = 0 qual = 0 # subprocess.call(['molconvert', 'mol:V3-H+-a_gen', finalsmi, '-o', finalmol]) # subprocess.call(['molconvert', size, finalmol, '-o', finalpng]) subprocess.call(['obabel', smi, '-omol', '-O', mol, '-x3', '-h'], stdout=subprocess.PIPE) final = molecule.Molecule(mol) if final.atoms == 0 and final.bonds == -1: config.log('OSRA failed to convert your image. Output file is empty.', []) failed = 1 qual = 0.0 else: for data in final.atom_dict.values(): if data[0] == 'A': undefined += 1 elif data[0] in degmax.keys(): if int(data[1]) == degmax[data[0]] + data[2]: qual += 1.0 else: wrong_deg += 1 qual += 0.5 # TODO: strange situation with empty files got here try: qual /= float(final.atoms) # qual -= (cor_number / (4 * final.atoms)) except ZeroDivisionError: failed = 1 qual = 0.0 return [undefined, failed, cor_number, wrong_deg]
def penalty_birads_replacing_lone_pairs_test(self): """Test that birads on `S u2 p0` are penalized""" adj = """ multiplicity 3 1 S u2 p0 c0 {2,D} {3,D} 2 O u0 p2 c0 {1,D} 3 O u0 p2 c0 {1,D} """ mol = molecule.Molecule().fromAdjacencyList(adj) mol.update() mol_list = generate_resonance_structures(mol, keep_isomorphic=False, filter_structures=True) for mol in mol_list: if mol.reactive: for atom in mol.vertices: if atom.isSulfur(): self.assertNotEquals(atom.radicalElectrons, 2) self.assertEqual(len(mol_list), 3) self.assertEqual(sum([1 for mol in mol_list if mol.reactive]), 2)
def generate_configurations(settings, geo_path, normal_modes, dim_null, config_dir): print("Running normal distribution configuration generator...") config = configparser.ConfigParser(allow_no_value=False) config.read(settings) with open(geo_path, "r") as geo_file: molec = molecule.Molecule(geo_file.read()) with open(config["files"]["log_path"] + "/" + "config_input.log", "w") as input_file: input_file.write("'" + geo_path + "'\n") # optimized geometry input_file.write("'" + normal_modes + "'\n") # normal modes input_file.write( str(3 * molec.num_atoms) + " " + str(dim_null) + "\n") # dim; dimnull input_file.write(config["config_generator"]["random"] + " " + config["config_generator"]["num_configs"] + "\n") # random method; nconfigs input_file.write("'" + config_dir + "/configs.xyz'\n") # output one-body configurations input_file.write(config["config_generator"]["geometric"] + " " + config["config_generator"]["linear"] + "\n") # geometric, linear input_file.write(".true.") # verbose os.system( os.path.dirname(os.path.abspath(__file__)) + "/../norm_distribution/src/generate_configs_normdistrbn < " + config["files"]["log_path"] + "/config_input.log" + " > " + config["files"]["log_path"] + "/config_output.log") os.system("cp " + geo_path + " " + config_dir + "/geo.opt.xyz") print("Normal Distribution Configuration generation complete.")
def virtualMove(index_of_chosen_mol, direction): chosen_mol = list_of_molecules[index_of_chosen_mol] moved_mol = molecule_import.Molecule(1, 0, 0, False) #print(moved_mol.pos_x, moved_mol.pos_y) if direction == 1: moved_mol.pos_x = chosen_mol.pos_x moved_mol.pos_y = chosen_mol.pos_y + 1 elif direction == 2: moved_mol.pos_x = chosen_mol.pos_x + 1 moved_mol.pos_y = chosen_mol.pos_y elif direction == 3: moved_mol.pos_x = chosen_mol.pos_x moved_mol.pos_y = chosen_mol.pos_y - 1 elif direction == 4: moved_mol.pos_x = chosen_mol.pos_x - 1 moved_mol.pos_y = chosen_mol.pos_y #print("moved molecule", moved_mol.pos_x, " a y ", moved_mol.pos_y) chance = checkNeighbours(index_of_chosen_mol, moved_mol.pos_x, moved_mol.pos_y) move_chance = molecule_import.Movement_chance(direction, chance) #print("kontrola move_chance ma hodnotu ", move_chance.dir, " a sance je", move_chance.prob) return move_chance
def __init__(self, mol_str, convCrit=10, maxIter=100): """ Initializes molecule, molecular properties, molecular integrals, before entering a loop to calculate the RHF energy :param mol: molecule object, specifies geometry, charge, and multiplicity :param mints: molecular integral helper, generates various molecular intergrals :param convCrit: criteria for converge, x in input corresponds to maximum difference of 10^-x :param maxIter: maximum number of iterations to obtain self consistence """ mol = molecule.Molecule(mol_str) #Step 1: Read nuclear repulsion energy from molecule and atomic integrals from MintsHelper mints = integrals.Integral(mol_str) self.VNuc = mints.VNuc #nuclear repulsion energy self.S = np.array(mints.S) #overlap integrals self.T = np.array(mints.T) #kinetic energy integrals self.V = np.array(mints.V) #electron-nuclear attraction integrals self.g = np.array(mints.G).transpose( 0, 2, 1, 3 ) #electron-electron repulsion integrals, transposed from (pq|rs) to <pr|qs> #The transpose is very important, because from here on forward physicist notation is assumed! #Step 1.5: Calculate Hamiltonian and orbital information self.H = self.T + self.V #Hamiltonian self.E = 0.0 #RHF energy self.norb = mints.K #number of orbits (defines size of arrays) nelec = mol.molecular_charge + sum(mol.charges) #number of electrons self.nocc = int(nelec / 2) #number of occupied orbitals #Step 2: Form orthogonalizer (X = S^-1/2) self.X = np.matrix(spla.inv(spla.sqrtm( self.S))) #Orthogonalizer S^-1/2 #Step 3: Set D = 0 as "core" guess self.D = np.zeros((self.norb, self.norb)) #Density Matrix #Iteration to SC convCond = False #convergence condition self.Eold = 1.0 #Previous calculated energy self.Dold = np.zeros((self.norb, self.norb)) #Previous density matrix self.iter = 0 #Iteration count line = "+----+---------------------+------------+" print line print "|iter| Energy | dE |" print line while not convCond: self.I2SC() #Steps 1-7 #Check convergence, if the energy and density matrix are both within a threshold of one another, then it has converged #Additionally the program must iterate twice to avoid the condition when all four variables are initially null if (np.absolute((self.Eold - self.E)) < 10.0**(-convCrit) and np.absolute(spla.norm(self.D) - spla.norm(self.Dold)) < 10.0**(-convCrit) and self.iter > 1): print line print "| Converged |" convCond = True elif self.iter >= maxIter: print line print "| Failed to converge |" break print line
def append_static_molecule(pos_x, pos_y): k = len(list_of_molecules) + 1 molecule = molecule_import.Molecule(k, pos_x, pos_y, True) list_of_molecules.append(molecule)
def setup_molecules(no_molecules): for k in range(0, no_molecules): pos_x = random.randint(0, WIDTH) pos_y = random.randint(0, HEIGHT) molecule = molecule_import.Molecule(k, pos_x, pos_y, False) list_of_molecules.append(molecule)