示例#1
0
 def __exit__(self, exc_type, exc_value, traceback):
     relaxed_name = "relaxed"
     relaxed_molecule = molecules.Molecule(relaxed_name, self.relaxed_file,
                                           self.experiment)
     relaxed_molecule.assemble_from_CHARMM(self.chain_ids,
                                           self.output_molecule_files,
                                           self.resnums)
     CharmmProc.__exit__(self, exc_type, exc_value, traceback)
def main():
    in_file = gaussian.GaussianCom(IN_GAUCOM)
    system = molecules.Molecule('system',in_file.atoms_list)
    charge = system.get_charge()
    charge_line = '{0} 1 {0} 1 {0} 1\n'.format(int(charge))
    in_file.multiplicity_line = charge_line
    route_section = """%nproc=8
%mem=1gb
# oniom(b3lyp/6-31g(d):amber=softonly)=embed geom(notest,connectivity)\n"""
    in_file.route_section = route_section
    for no, atom in enumerate(in_file.atoms_list):
        if atom.resinfo.resname != 'WAT':
            in_file.atoms_list[no].oniom.layer = 'H'

    in_file.redo_connectivity_list()
    in_file.write_to_file(OUT_GAUCOM)      
示例#3
0
 def load_molecules(self):
     """ Create text to load Molecules in a CHARMM script. """
     # Merge all of the molecules.
     merged_structure = molecules.merge(self.molecules)
     # Save the merged structure in charmm_format_file.
     charmm_format_name = "charmm_format"
     handle, charmm_format_file = tempfile.mkstemp(suffix=".pdb",
                                                   dir=self.directory)
     merged_molecule = molecules.Molecule(charmm_format_name,
                                          charmm_format_file,
                                          self.experiment)
     # CHARMM needs all of the molecules to be in separate PDB files, so disassemble the merged molecule into separate files.
     chains, files = merged_molecule.disassemble_for_CHARMM(
         structure=merged_structure, directory=self.directory)
     # Remove the file of the merged structure, which is no longer needed.
     try:
         os.remove(charmm_format_file)
     except OSError:
         pass
     # Write the part of the script that loads the chains.
     self.chain_ids = list()
     for chain, _file in zip(chains, files):
         _id = chain.get_id()
         self.chain_ids.append(_id)
         segment = make_segment_name(_id)
         base_name = os.path.basename(_file)
         self.lines.extend([
             "! Load Chain {}".format(_id),
             "open read unit 10 form name {}".format(base_name),
             "read sequ pdb offi unit 10", "close unit 10",
             "gene {} setup".format(segment),
             "open read unit 10 form name {}".format(base_name),
             "read coor pdb unit 10", "close unit 10"
         ])
     # Add any missing atoms to the chain.
     self.lines.extend(ic_fill())
示例#4
0
    def iteration(stdgeom, currentfile, opt, loopid, convthreshold):
        os.system('cp ' + currentfile.comname + ' MM0.com')
        with open('MM0.com', 'r') as f:
            dihds = ''
            for line in f:
                if line.find('AmbTrs') >= 0:
                    dihds += line
        while True:
            currentfile = rxccfile.File('MM' + str(loopid))
            if opt == 'opt':
                os.system('sed -i "s/#p opt=(nomicro,cartesian) /#p /g" ' +
                          currentfile.comname)
                os.system('sed -i "s/#p/#p opt=(nomicro,cartesian)/g" ' +
                          currentfile.comname)
                os.system('sed -i "/freq/d" ' + currentfile.comname)
                os.system('sed -i "/chk/d" ' + currentfile.comname)
            elif opt == 'calcall':
                os.system(
                    'sed -i "s/#p opt=(nomicro,cartesian,tight,calcall) /#p /g" '
                    + currentfile.comname)
                os.system(
                    'sed -i "s/#p opt=(nomicro,cartesian,tight,calcall) /#p /g" '
                    + currentfile.comname)
                os.system(
                    'sed -i "s/#p/#p opt=(nomicro,cartesian,tight,calcall)/g" '
                    + currentfile.comname)
                os.system('sed -i "/freq/d" ' + currentfile.comname)
                os.system('sed -i "/chk/d" ' + currentfile.comname)


#            if loopid>1000:
#               raise StopIteration
            currentfile.com.read()
            try:
                currentfile.com.rung09()
                currentfile.com.isover()
                currentfile.runformchk()
                ifstop = True
                os.system('rm ' + currentfile.chkname + ' ' +
                          currentfile.logname)
            except:
                logging.error("Calculation failed, try again.")
                try:
                    currentfile.com.rung09()
                    currentfile.com.isover()
                    currentfile.runformchk()
                    ifstop = True
                    os.system('rm ' + currentfile.chkname + ' ' +
                              currentfile.logname)
                except:
                    logging.critical('Calculation still failed, continue...')
                    currentfile.runformchk()
                    os.system('rm ' + currentfile.chkname + ' ' +
                              currentfile.logname)
                    ifstop = False
                    # logging.info('minimum max2 is chosen from loop'+str(minmax2loop))
                    # os.system('cp MM'+str(minmax2loop)+'.com ../'+mmresult[0:3]+'amd'+mmresult[3:]+'.com')
                    # os.system('sed -i "s/#p opt=(verytight,z-matrix,calcall)/#p /g" ../*.com')
                    # os.system('sed -i "/chk/d" ../*.com')
                    # return

            currentfile.fchk.read()

            currentgeom = rxmol.Molecule('currentgeom')
            currentgeom.readfromxyz(io.StringIO(currentfile.fchk.xyz))
            currentgeom.readchargefromlist(currentfile.com.atomchargelist)
            currentgeom.readtypefromlist(currentfile.com.atomtypelist)
            currentgeom.readconnectivity(currentfile.com.connectivity)

            for angle in currentgeom.anglelist.values():
                for anglefunc in currentfile.com.nozomuanglefunc:
                    if matchangle(angle, anglefunc):
                        angle.nozomufunc = anglefunc
            for bond in currentgeom.bondlist.values():
                for bondfunc in currentfile.com.nozomubondfunc:
                    if matchbond(bond, bondfunc):
                        bond.nozomufunc = bondfunc

            # reassign current eqvalue
            for nozomufunc in currentfile.com.nozomuanglefunc:
                eq = 0
                i = 0
                for angle in currentgeom.anglelist.values():
                    if angle.nozomufunc == nozomufunc:
                        eq += angle.anglevalue
                        i += 1
                nozomufunc.eqvalue = eq / i
            for nozomufunc in currentfile.com.nozomubondfunc:
                eq = 0
                i = 0
                for bond in currentgeom.bondlist.values():
                    if bond.nozomufunc == nozomufunc:
                        eq += bond.length
                        i += 1
                nozomufunc.eqvalue = eq / i

            currentL = []
            currentL.extend(currentfile.com.nozomuanglefunc)
            currentL.extend(currentfile.com.nozomubondfunc)
            for item1 in currentL:
                for item2 in currentL:
                    if item1.value == item2.value and item1.repr != item2.repr:
                        item1.eqvalue = (item1.eqvalue + item2.eqvalue) / 2
                        item2.eqvalue = item1.eqvalue
                        logging.debug('Averaged old eqvalue ' + item1.repr +
                                      ' and ' + item2.repr + ' ' +
                                      str(item2.eqvalue))

            delta1 = [
                x.eqvalue - y.eqvalue for x, y in zip(stdL, currentL)
                if x.type == 'bond'
            ]
            delta2 = [
                x.eqvalue - y.eqvalue for x, y in zip(stdL, currentL)
                if x.type == 'angle'
            ]
            max1 = sorted(delta1, key=abs, reverse=True)[0]
            max2 = sorted(delta2, key=abs, reverse=True)[0]
            try:
                if abs(max2) < abs(minmax2):
                    minmax2 = abs(max2)
                    minmax2loop = loopid
            except:
                minmax2 = abs(max2)
                minmax2loop = loopid

            if loopid - minmax2loop > convthreshold:
                logging.info(
                    'Stopped for convergence: max Delta2 do not decrease in ' +
                    str(convthreshold) + ' cycles')
                if opt == 'calcall':
                    logging.info('minimum max2 is chosen from loop' +
                                 str(minmax2loop))
                    os.system('cp MM' + str(minmax2loop) + '.com ../katachi_' +
                              mmresult + '.com')
                    os.system(
                        'sed -i "s/#p opt=(nomicro,cartesian,tight,calcall)/#p freq/g" ../*.com'
                    )
                    os.system('sed -i "/chk/d" ../*.com')

                    return loopid
                else:
                    opt = 'calcall'
                    minmax2 = 100
                    minmax2loop = loopid

            logging.info('------------------------------')
            logging.info('Loop ' + str(loopid) + ' keyword ' + opt +
                         ': max bond delta: ' + str(max1) +
                         '  max angle delta: ' + str(max2))
            logging.info('MinMax2 is ' + str(minmax2) + ' at loop ' +
                         str(minmax2loop))
            logging.info('------------------------------')
            if abs(max1) < 0.0001 and abs(max2) < 0.01:

                if opt == 'opt':
                    opt = 'calcall'
                    logging.info('-------------------------------------------')
                    logging.info('opt converged at ' + str(max1) + ' ' +
                                 str(max2) + ' ' + str(loopid))
                    logging.info('-------------------------------------------')
                    minmax2 = 100
                    minmax2loop = loopid + 1
                elif opt == 'calcall':
                    logging.info('-------------------------------------------')
                    logging.info('calcall converged at ' + str(max1) + ' ' +
                                 str(max2) + ' ' + str(loopid))
                    logging.info('-------------------------------------------')
                    os.system('cp ' + currentfile.comname + ' ../katachi_' +
                              mmresult + '.com')
                    os.system(
                        'sed -i "s/#p opt=(nomicro,cartesian,tight,calcall)/#p freq/g" ../*.com'
                    )
                    os.system('sed -i "/chk/d" ../*.com')
                    if ifstop:
                        return loopid

            delta = [x.eqvalue - y.eqvalue for x, y in zip(stdL, currentL)]

            try:
                type(last)
            except UnboundLocalError:
                if loopid != 0:
                    lastfile = rxccfile.File('MM' + str(loopid - 1))
                    lastfile.com.read()
                    lastL = []
                    lastL.extend(lastfile.com.nozomuanglefunc)
                    lastL.extend(lastfile.com.nozomubondfunc)
                    last = lastL
                else:
                    last = copy.deepcopy(stdL)
            for now, std, former, delt in zip(currentL, stdL, last, delta):
                if former.eqvalue + delt > 0 and former.eqvalue + delt < 180:
                    now.eqvalue = former.eqvalue + delt
                if now.type == 'bond' and abs(delt) > 0.0001:
                    logging.warning(now.repr + ' ' + str(delt))
                if now.type == 'angle' and abs(delt) > 0.01:
                    logging.warning(now.repr + ' ' + str(delt))
            for item1 in currentL:
                for item2 in currentL:
                    if item1.value == item2.value and item1.repr != item2.repr:
                        item1.eqvalue = (item1.eqvalue + item2.eqvalue) / 2
                        item2.eqvalue = item1.eqvalue
                        logging.debug('Averaged new eqvalue ' + item1.repr +
                                      ' and ' + item2.repr + ' ' +
                                      str(item2.eqvalue))

            last = copy.deepcopy(currentL)

            finalxyz = ''
            for atom in stdgeom:
                finalxyz += atom.atomsym + '-' + atom.atomtype + '-' + '{:<9.6f}'.format(
                    float(atom.atomcharge)) + '   ' + '    '.join(
                        ["{: .12f}".format(x) for x in atom.coords]) + '\n'
            finalhead = currentfile.com.commandline + '\nfinal\n\n' + str(
                currentfile.fchk.totalcharge
            ) + ' ' + str(
                currentfile.fchk.multiplicity
            ) + '\n' + finalxyz + '\n' + currentfile.com.connectivity + '\n'

            finaltail = ''
            finaltail += dihds

            for item in currentfile.com.nozomuanglefunc:
                finaltail += 'HrmBnd1 ' + item.repr + ' '
                parm = "{: .3f}".format(item.value)
                finaltail += ' ' + parm + ' {: .4f}'.format(
                    item.eqvalue) + '\n'
            for item in currentfile.com.nozomubondfunc:
                finaltail += 'HrmStr1 ' + item.repr + ' '
                parm = "{: .3f}".format(item.value)
                finaltail += ' ' + parm + ' {: .5f}'.format(
                    item.eqvalue) + '\n'

            for addfunc in currentfile.com.additionfunc:
                finaltail += addfunc.content
            for nozovdw in currentfile.com.nozomuvdw:
                finaltail += nozovdw.content

            finaltail += '\n\n'

            loopid += 1
            with open('MM' + str(loopid) + '.com', 'w') as f:
                f.write(finalhead + finaltail)

            del currentgeom
            del currentfile