Esempio n. 1
0
def getPath(Reac, Prod, gl):
    xyzfile3 = open(("IRC3.xyz"), "w")
    Path = []
    Path.append(Reac.copy())
    for i in range(0, 30):
        image = Reac.copy()
        image = tl.setCalc(image, "DOS/", gl.lowerMethod, gl.lowerLevel)
        Path.append(image)
    image = Prod.copy()
    image = tl.setCalc(image, "DOS/", gl.lowerMethod, gl.lowerLevel)
    Path.append(image)

    neb1 = NEB(Path, k=1.0, remove_rotation_and_translation=True)
    try:
        neb1.interpolate('idpp', optimizer="MDMin", k=1.0)
    except:
        neb1.interpolate()

    optimizer = FIRE(neb1)
    optimizer.run(fmax=0.07, steps=500)
    neb2 = NEB(Path, k=1.0, climb=True, remove_rotation_and_translation=True)
    optimizer = FIRE(neb2)
    optimizer.run(fmax=0.07, steps=1500)
    for i in range(0, len(Path)):
        tl.printTraj(xyzfile3, Path[i])
    xyzfile3.close()

    return Path
Esempio n. 2
0
    def optNEB(self, trans, path, changePoints, mols):

        # Open files for saving IRCdata
        xyzfile3 = open((path + "/IRC3.xyz"), "w")
        MEP = open((path + "/MEP.txt"), "w")
        imagesTemp1 = []
        index = changePoints[0] - 100
        molTemp = mols[index].copy()
        imagesTemp1.append(molTemp.copy())
        for i in range(0, 100):
            imagesTemp1.append(mols[changePoints[0] - 100].copy())
        try:
            imagesTemp1.append(mols[changePoints[-1] + 300])
        except:
            imagesTemp1.append(self.CombProd.copy())
        neb1 = NEB(imagesTemp1, k=1.0, remove_rotation_and_translation=True)
        try:
            neb1.interpolate('idpp')
        except:
            neb1.interpolate()

        for i in range(0, len(imagesTemp1)):
            try:
                imagesTemp1[i] = tl.setCalc(imagesTemp1[i], self.lowString,
                                            self.lowMeth, self.lowLev)
                for i in range(0, len(trans)):
                    c = FixAtoms(trans)
                    imagesTemp1[i].set_constraint(c)
                min = BFGS(imagesTemp1[i])
                min.run(fmax=0.005, steps=40)
                del imagesTemp1[i].constraints
            except:
                pass

        optimizer = FIRE(neb1)
        try:
            optimizer.run(fmax=0.07, steps=300)
        except:
            pass
            print("passed seccond neb")

        neb1_2 = NEB(imagesTemp1, k=0.01, remove_rotation_and_translation=True)
        try:
            optimizer.run(fmax=0.07, steps=200)
        except:
            pass

        neb2 = NEB(imagesTemp1,
                   climb=True,
                   remove_rotation_and_translation=True)
        try:
            optimizer.run(fmax=0.07, steps=200)
        except:
            pass

        for i in range(0, len(imagesTemp1)):
            tl.printTraj(xyzfile3, imagesTemp1[i])

        print("NEB printed")

        xyzfile3.close()

        point = 0
        maxEne = -50000000

        try:
            for i in range(0, len(imagesTemp1)):
                MEP.write(
                    str(i) + ' ' + str(imagesTemp1[i].get_potential_energy()) +
                    '\n')
                if imagesTemp1[i].get_potential_energy() > maxEne and i > 5:
                    point = i
                    maxEne = imagesTemp1[i].get_potential_energy()
            self.TS2 = imagesTemp1[point]
        except:
            point = 0

        print("TS Climb part")
        write(path + '/TSClimbGuess.xyz', self.TS2)
        try:
            self.TS2Freqs, self.imaginaryFreq2, zpe, energy, self.TS2, rmol, pmol = self.characteriseTSExt(
                self.TS2, False, path, self.QTS3)
        except:
            self.TS2Freqs, self.imaginaryFreq2, zpe, energy, self.TS2, rmol, pmol = self.characteriseTSExt(
                self.TS2, True, path, self.QTS3)

        self.TScorrect = self.compareRandP(rmol, pmol)
        self.forwardBarrier2 = energy + zpe
Esempio n. 3
0
    def runTrajectory(self):
        # Create specific directory
        fails = 0
        self.Mol =tl.setCalc(self.Mol, 'Traj_' + str(self.procNum), self.method, self.level)
        coords = self.Mol.get_positions()
        reac_smile = tl.getSMILES(self.Mol,True)
        self.Mol.set_positions(coords)
        workingDir = os.getcwd()
        newpath = workingDir + '/Raw/traj' + str(self.procNum)
        print("making directory " + newpath)


        if not os.path.exists(newpath):
            os.makedirs(newpath)
        os.chdir(newpath)
        file_name = str(newpath + "/totaltraj.xyz")
        if os.path.isfile(file_name):
            expand = 1
            while True:
                expand += 1
                new_file_name = file_name.split(".xyz")[0] + str(expand) + ".xyz"
                if os.path.isfile(new_file_name):
                    continue
                else:
                    file_name = new_file_name
                    break

        namefile = open((file_name), "a")

        self.numberOfSteps = 0
        consistantChange = 0

        #Multiple stepsizes
        self.smallStep = self.timeStep
        timeStep = self.timeStep



        eneBXDon = False
        eBounded= False
        comBounded = False

        print("getting first forces")
        try:
            self.forces = self.Mol.get_forces()
        except:
            try:
                self.forces = self.Mol.get_forces()
            except:
                print("forces error")
                self.forces = np.zeros(len(self.forces.shape))

        #Get MDintegrator type
        if self.MDIntegrator == 'VelocityVerlet':
            mdInt = ChemDyME.MDIntegrator.VelocityVerlet(self.forces, self.velocity, self.Mol)
        elif self.MDIntegrator == 'Langevin':
            mdInt = ChemDyME.MDIntegrator.Langevin(units.kB * self.LangTemp, self.LangFric, self.forces, self.velocity, self.Mol,timeStep)



        # Then set up reaction criteria or connectivity map
        con = ChemDyME.Connectivity.NunezMartinez(self.Mol)


        # Then set up various BXD procedures
        if self.comBXD or self.biMolecular:
            self.comBXD = True
            if self.mixedTimestep == True:
                mdInt.reset(self.timeStep * 10)
            comBxd = ChemDyME.BXDconstraint.COM(self.Mol, 0, self.minCOM, hitLimit = 100000, activeS = self.fragIdx, runType="fixed")
        if self.eneBXD:
            eneBXD = ChemDyME.BXDconstraint.Energy(self.Mol, -10000, 10000, hitLimit = 1, adapMax = self.adaptiveSteps, runType="adaptive")


        # Run MD trajectory for specified number of steps
        for i in range(0,self.mdSteps):
            t = process_time()

            try:
                self.ene = self.Mol.get_potential_energy()
            except:
                pass



            # Update the COM seperation and check whether it is bounded
            if self.comBXD:
                comBxd.update(self.Mol)
                comBounded = comBxd.inversion


            if self.comBXD and eneBXDon and i % self.printFreq == 0:
                print("Ene = " + str(eneBXD.s[0]) + ' S = ' + str(comBxd.s[0]) + ' step = ' + str(i) + ' process = ' + str(self.procNum) + ' time = ' + str(process_time()-t) + ' temperature = ' + str(self.Mol.get_temperature()))
            elif self.comBXD and i % self.printFreq == 0 :
                print("Ene = " + "NA" + ' S = ' + str(comBxd.s[0]) + ' step = ' + str(i) + ' process = ' + str(self.procNum) + ' time = ' + str(process_time()-t) + ' temperature = ' + str(self.Mol.get_temperature()))
            elif eneBXDon and i % self.printFreq == 0:
                print("Ene = " + str(self.Mol.get_potential_energy()) + ' box = ' + str(eneBXD.box) + ' step = ' + str(i) + ' process = ' + str(self.procNum) + ' time = ' + str(process_time()-t) + ' temperature = ' + str(self.Mol.get_temperature()) + ' Etot ' + str(self.Mol.get_potential_energy() + self.Mol.get_kinetic_energy()))

            if i % self.printFreq == 0 and self.geom_print is True:
                tl.printTraj(namefile, self.Mol.copy())


            #  Now check whether to turn BXDE on
            if self.comBXD:
                if (self.biMolecular or self.comBXD) and comBxd.s[0] < self.minCOM and eneBXDon == False:
                    if self.mixedTimestep == True:
                        mdInt.reset(self.timeStep)
                    eneBXDon = False
            elif self.eneBXD and self.ReactionCountDown == 0:
                eneBXDon = True

            if eneBXDon == True:
                eneBXD.update(self.Mol)
                eBounded = eneBXD.inversion

            if comBounded is True and self.ReactionCountDown == 0:
                self.Mol.set_positions(mdInt.old_positions)
                com_del_phi = comBxd.del_constraint(self.Mol)
                if comBxd.stuck == True  and comBxd.s[0] < self.minCOM:
                    comBxd.stuckFix()

            if eBounded:
                self.Mol.set_positions(mdInt.old_positions)
                try:
                    e_del_phi = eneBXD.del_constraint(self.Mol)
                except:
                    pass

            # Perform inversion if required
            if eBounded is True and comBounded is True:
                mdInt.constrain2(e_del_phi, com_del_phi)
            elif eBounded is False and comBounded is True:
                mdInt.constrain(com_del_phi)
            elif eBounded is True and comBounded is False:
                mdInt.constrain(e_del_phi)
                timeStep = self.smallStep
            else:
                self.numberOfSteps += 1
                timeStep = self.timeStep


            mdInt.md_step_pos(self.forces, timeStep, self.Mol)

            if eBounded and eneBXD.boxList[eneBXD.box].lower.hit( self.Mol.get_potential_energy(), 'down'):
                ene = self.Mol.get_potential_energy()
                self.Mol.set_positions(mdInt.old_positions)
                mdInt.current_positions = mdInt.old_positions
                ene1 = self.Mol.get_potential_energy()
                hit1 = eneBXD.boxList[eneBXD.box].lower.hit(ene1, 'down')
                mdInt.constrained = True
                mdInt.md_step_pos(self.forces, timeStep*0.0000001, self.Mol)
                ene2 = self.Mol.get_potential_energy()
                hit2 = eneBXD.boxList[eneBXD.box].lower.hit(ene2, 'down')

            try:
                self.forces = self.Mol.get_forces()
            except:
                try:
                    self.forces = self.Mol.get_forces()
                except:
                    print("forces error")
                    self.forces = np.zeros(len(self.forces.shape))

            mdInt.md_step_vel(self.forces, timeStep, self.Mol)

            self.MolList.append(self.Mol.copy())

            # Update connectivity map to check for reaction
            con.update(self.Mol)
            if con.criteriaMet is True:
                if consistantChange == 0:
                    self.TSpoint = i
                    self.TSgeom = self.Mol.copy()
                    consistantChange = self.consistantWindow
                elif consistantChange > 0:
                    if consistantChange == 1:
                        self.ReactionCountDown = self.window
                        eneBXDon = False
                        consistantChange -= 2
                    else:
                        consistantChange -= 1
            else:
                if consistantChange > 0:
                    consistantChange = 0
                con.criteriaMet = False


            if not self.ReactionCountDown == 0:
                self.ReactionCountDown -= 1
            if self.ReactionCountDown == 1:
                awrite('temp.xyz', self.Mol)
                coords = self.Mol.get_positions()
                min = BFGS(self.Mol)
                min.run(fmax=0.1, steps=5)
                con2 = ChemDyME.Connectivity.NunezMartinez(self.Mol)
                pmol_name = tl.getSMILES(self.Mol,False)
                self.Mol.set_positions(coords)
                if not np.array_equal(con.C,con2.C):
                    self.ReactionCountDown = 0
                    self.productGeom = self.Mol.get_positions()
                    os.chdir(workingDir)
                    break
                elif fails < 5:
                    self.ReactionCountDown = self.window
                    fails +=1
                else:
                    fails = 0
                    self.ReactionCountDown = 0
                    consistantChange = 0
                    while eneBXD.boxList[eneBXD.box].lower.hit( self.Mol.get_potential_energy(), 'down'):
                        eneBXD.box -=1
        try:
            self.Mol._calc.close()
        except:
            pass
        namefile.close()
        os.chdir(workingDir)
Esempio n. 4
0
    def optDynPath(self, trans, path, MolList, changePoints):

        # Open files for saving IRCdata
        xyzfile = open((path + "/Data/dynPath.xyz"), "w")
        MEP = open((path + "/Data/MEP.txt"), "w")
        orriginal = open((path + "/Data/orriginalPath.txt"), "w")
        dyn = open((path + "/Data/traj.xyz"), "w")

        dynList = []
        end = int(changePoints + self.dynPrintStart)
        length = int(len(MolList))
        if end < length:
            endFrame = end
        else:
            endFrame = length
        for i in range(int(changePoints - self.dynPrintStart), int(endFrame),
                       self.inc):
            iMol = MolList[i].copy()
            tl.printTraj(dyn, iMol)
            iMol = tl.setCalc(iMol, self.lowString, self.lowMeth, self.lowLev)
            orriginal.write(
                str(i) + ' ' + str(iMol.get_potential_energy()) + '\n')
            c = FixAtoms(trans)
            iMol.set_constraint(c)
            min = BFGS(iMol)
            try:
                min.run(fmax=0.1, steps=150)
            except:
                min.run(fmax=0.1, steps=1)
            del iMol.constraints
            tl.printTraj(xyzfile, iMol)
            dynList.append(iMol.copy())

        maxEne = -50000
        point = 0
        for i in range(0, len(dynList)):
            dynList[i] = tl.setCalc(dynList[i], self.lowString, self.lowMeth,
                                    self.lowLev)
            MEP.write(
                str(i) + ' ' + str(dynList[i].get_potential_energy()) + '\n')
            if dynList[i].get_potential_energy() > maxEne:
                point = i
                maxEne = dynList[i].get_potential_energy()

        self.TS2 = dynList[point]
        TS2Guess = self.TS2.copy()
        write(path + '/Data/TS2guess.xyz', self.TS2)
        try:
            self.TS2Freqs, self.imaginaryFreq2, zpe, energy, self.TS2, rmol, pmol = self.characteriseTSExt(
                self.TS2, True, path, self.QTS3)
        except:
            try:
                self.TS2Freqs, self.imaginaryFreq2, zpe, energy, self.TS2, rmol, pmol = self.characteriseTSExt(
                    self.TS2, False, path, self.QTS3)
            except:
                pass
        try:
            self.TS2correct = self.compareRandP(rmol, pmol)
        except:
            print("TS2 does not connect products")
            self.TS2correct = False
            self.TS2 = TS2Guess
            self.TS2Freqs, self.imaginaryFreq2, zpe, energy = self.characteriseTSinternal(
                self.TS2)

        write(path + '/TS2.xyz', self.TS2)
        self.forwardBarrier2 = energy
Esempio n. 5
0
    def runGenBXD(self, Reac, Prod, maxHits, adapMax, pathType, path, bonds, decSteps, histogramBins, pathLength, fixToPath, pathDistCutOff, epsilon):

        self.iterations = 0

        keepGoing = True

        workingDir = os.getcwd()
        file = open("geo.xyz","w")
        sfile = open("plotData.txt", "w")





        #Get MDintegrator type
        if self.MDIntegrator == 'VelocityVerlet':
            mdInt = ChemDyME.MDIntegrator.VelocityVerlet(self.forces, self.velocity, self.Mol)
        elif self.MDIntegrator == 'Langevin':
            mdInt = ChemDyME.MDIntegrator.Langevin(units.kB * self.LangTemp, self.LangFric, self.forces, self.velocity, self.Mol, self.timeStep)

        #Check whether a list of bounds is present? If so read adaptive boundaries from previous run
        BXD = ChemDyME.BXDconstraint.genBXD(self.Mol, Reac, Prod, adapMax = adapMax, activeS = bonds, path = path, pathType = pathType, decorrelationSteps = decSteps, runType = 'adaptive', hitLimit = 1, hist = histogramBins, endDistance=pathLength, fixToPath=fixToPath, pathDistCutOff=pathDistCutOff, epsilon=epsilon )
        #Check whether a list of bounds is present? If so read adaptive boundaries from previous run
        if os.path.isfile("BXDbounds.txt"):
            BXD.readExisitingBoundaries("BXDbounds.txt")
            BXD.reset("fixed", maxHits)

        else:
            BXDfile = open("BXDbounds.txt", "w")

        # Get forces from designated potential
        try:
            self.forces = self.Mol.get_forces()
        except:
            print('forces error')

        # Run MD trajectory for specified number of steps
        while keepGoing:

            t = process_time()
            bxdt = process_time()
            BXD.update(self.Mol)
            bxdte = process_time()
            eBounded = BXD.inversion
            if eBounded:
                self.Mol.set_positions(mdInt.oldPos)
                bxd_del_phi = BXD.del_constraint(self.Mol)

            if self.MDIntegrator == 'VelocityVerlet' and not eBounded:
                print('S ' + str(BXD.s[2])  + ' box ' + str(BXD.box) + ' time ' + str(process_time()-t) + ' Etot ' + str(self.Mol.get_potential_energy() + self.Mol.get_kinetic_energy()))
            elif self.iterations % self.printFreq == 0 and not eBounded:
                print('pathNode = ' +str(BXD.pathNode) + ' distFromPath = ' + str(BXD.distanceToPath) + ' project = ' +str(BXD.s[2]) + ' S ' + str(BXD.s[0]) + " hits " + str(BXD.boxList[BXD.box].upper.hits) + ' ' + str(BXD.boxList[BXD.box].lower.hits) + " points in box " + str(len(BXD.boxList[BXD.box].data))  + ' box ' + str(BXD.box) + ' time ' + str(process_time()-t) + ' temperature ' + str(self.Mol.get_temperature()))

            if self.iterations % (self.printFreq/100) == 0:
                sfile.write('S \t=\t' + str(BXD.s[0]) + '\tbox\t=\t' + str(BXD.box) + " hits " + str(BXD.boxList[BXD.box].upper.hits) + ' ' + str(BXD.boxList[BXD.box].lower.hits) + "\n")
                sfile.flush()

            # Perform inversion if required
            if eBounded is True:
                mdInt.constrain(bxd_del_phi)

            mdpt = process_time()
            mdInt.mdStepPos(self.forces, self.timeStep, self.Mol)
            mdpte = process_time()
            # Get forces from designated potential
            ft = process_time()
            try:
                self.forces = self.Mol.get_forces()
            except:
                print('forces error')
            fte = process_time()
            mdvt = process_time()
            mdInt.mdStepVel(self.forces, self.timeStep, self.Mol)
            mdvte = process_time()

            self.numberOfSteps += 1

            if self.iterations % self.printFreq == 0:
                tl.printTraj(file,self.Mol)
            self.iterations += 1

            #print( "Forces time = " + str(fte - ft) + " BXDtime = " + str(bxdte - bxdt) + " MDP time = " + str(mdpte - mdpt) + "MDV time = " + str(mdvte - mdvt))

            #check if one full run is complete, if so stop the adaptive search
            if BXD.completeRuns == 1:
                keepGoing = False

        #Check whether inital run and adaptive box placement run, save boundaries and rest all bxd boundaries ready for convergence
        if BXD.runType == "adaptive":
            # reset all box data other than boundary positions and prepare to run full BXD
            # print boundaries to file
            keepGoing = True
            BXD.printBounds(BXDfile)
            BXD.reset("fixed", maxHits)

        file.close()

        # Rerun BXD with new adaptively set bounds to converge box to box statistics
        # Get forces and energy from designated potential
        try:
            self.forces = self.Mol.get_forces()
        except:
            print('forces error')

        while keepGoing:
            t = process_time()

            BXD.update(self.Mol)
            eBounded = BXD.inversion
            if eBounded:
                self.Mol.set_positions(mdInt.oldPos)

            eBounded = BXD.inversion
            if self.MDIntegrator == 'VelocityVerlet' and self.iterations % self.printFreq == 0:
                print('S ' + str(BXD.s[2])  + ' box ' + str(BXD.box) + ' time ' + str(process_time()-t) + ' Epot ' + str(self.Mol.get_potential_energy() + self.Mol.get_kinetic_energy()))
            elif self.iterations % self.printFreq == 0:
                print('pathNode = ' +str(BXD.pathNode) + ' project = ' +str(BXD.s[2]) + ' S ' + str(BXD.s[0]) + " hits " + str(BXD.boxList[BXD.box].upper.hits) + ' ' + str(BXD.boxList[BXD.box].lower.hits) + " points in box " + str(len(BXD.boxList[BXD.box].data))  + ' box ' + str(BXD.box) + ' time ' + str(process_time()-t) + ' temperature ' + str(self.Mol.get_temperature()))

            # Perform inversion if required
            if eBounded is True:
                mdInt.constrain(BXD.del_phi)

            mdInt.mdStepPos(self.forces, self.timeStep, self.Mol)
            # Get forces from designated potential
            try:
                self.forces = self.Mol.get_forces()
            except:
                print('forces error')
            mdInt.mdStepVel(self.forces, self.timeStep, self.Mol)

            self.numberOfSteps += 1

            if BXD.completeRuns == 1:
                keepGoing = False

        #Look for exsisting results files from previous runs and open a new results file numbered sequentially from those that exsist already
        i = 0
        while(os.path.isfile("BXDprofile" + str(i) + ".txt")):
            i += 1
        BXDprofile = open("BXDprofile" + str(i) + ".txt", "w")
        BXDrawData = open("BXDrawData" + str(i) + ".txt", "w")
        BXDlowRes = open("BXDlowResProfile" + str(i) + ".txt" , "w")
        BXD.gatherData(BXDprofile, BXDrawData, self.LangTemp, BXDlowRes)