def calc_statistic_xmp2(C, FitClass): '''\ return statistic informations for xMP2 type optimization\ ''' from my_io import print_List from my_io import print_Error from opt_func import update_qchem_xmp2 print_List(FitClass.IOut, C, 4, Info='Testing parameters in this round') update_qchem_xmp2(C, FitClass) FitClass.run_QcmBatch() if FitClass.OptAlgo[:5] == 'batch': FitClass.get_OptResu(iop=1) print_List(FitClass.IOut,FitClass.InitGuess,\ 4,Info='Batch corresponds to these parameters ::') FitClass.run_Statistic() else: FitClass.get_OptResu(iop=0) AD, wAD, MAD, wMAD, RMS, wRMS = FitClass.run_Statistic() if FitClass.OptAlgo == 'leastsq': return array(FitClass.Result) elif FitClass.OptAlgo[-3:] == 'rms': return wRMS elif FitClass.OptAlgo[-3:] == 'mad': return wMAD elif FitClass.OptAlgo[-2:] == 'ad': return wAD else: print_Error(FitClass.IOut, 'Error in return required type of statistic data'+\ ' \"calc_statistic()->%s\"' % FitClass.OptAlgo) return
def calc_statistic_xyg3(C, FitClass): '''\ return statistic informations for "XYG3" type optimization\ ''' from my_io import print_List from my_io import print_Error from opt_func import opt_func print_List(FitClass.IOut, C, 4, Info='Testing parameters in this round') #for x,y in FitClass.CompDict.iteritems(): # print x,y FitClass.run_OptCalc(opt_func, C) if FitClass.OptAlgo[:5] == 'batch': FitClass.get_OptResu(iop=1) print_List(FitClass.IOut,FitClass.InitGuess,\ 4,Info='Batch corresponds to these parameters ::') FitClass.run_Statistic() else: FitClass.get_OptResu(iop=0) AD, wAD, MAD, wMAD, RMS, wRMS = FitClass.run_Statistic() if FitClass.OptAlgo == 'leastsq': return array(FitClass.Result) elif FitClass.OptAlgo[-3:] == 'rms': return wRMS elif FitClass.OptAlgo[-3:] == 'mad': return wMAD elif FitClass.OptAlgo[-2:] == 'ad': return wAD else: print_Error(FitClass.IOut, 'Error in returning required type of statistic data'+\ ' \"calc_statistic()->%s\"' % FitClass.OptAlgo) return
def main(argv=None): from os import getcwd from os import getenv from os import chdir from os.path import isfile from os.path import split from os.path import splitext from time import ctime from time import time import sys if argv is None: argv = sys.argv WorkDir = getcwd().strip() # STRING, current DIR HomeDir = getenv('HOME') # STRING, Home DIR if getenv('IGOR_MODULES_PATH'): # STRING, private module DIR ModuDir = getenv('IGOR_MODULES_PATH') sys.path.append(ModuDir) # Append it into "sys.path" sys.path.append(WorkDir) # Append it into "sys.path" else: print 'Error in getting grobal environment '+\ '\"$IGOR_MODULES_PATH\" which is the ' +\ 'direction to private modules' sys.exit(1) from my_io import print_Error # Import private modules from my_io import print_List from my_io import print_String from my_io import ConfigIO filename = argv[1] WorkPath, JobName = split(filename) Name, Prefix = splitext(JobName) if len(WorkPath) > 0: chdir(WorkPath) iout = file('%s.Fit' % Name, 'w') # Open the output file print_String(iout, 'Fitting start from ' + ctime(), 1) # Count time consuming TimeStart = time() MainFit = ConfigIO(iout, fn=filename, bugctrl=iprint) # Analysis config file MainFit.get_ProjEnvr() MainFit.get_Batch() MainFit.get_TrainSet() MainFit.get_OptJob() if MainFit.OptJob == 'xyg3': # XYG3-like optimization job MainFit.get_OptInit() MainFit.get_OptAlgo() MainFit.get_OptComp() MainFit.get_OptFunc() run_optimization(MainFit) # Run optimization based on MainFit elif MainFit.OptJob.lower() == 'xmp2': # extend MP2 optimization job if MainFit.ProjTool != 'qchem': print_String(iout,'At present, the optimization for'+\ 'could only be combined with Q-Chem package',1) return MainFit.get_QcmIOCmd() MainFit.get_OptInit() MainFit.get_OptAlgo() MainFit.get_OptFunc() run_optimization(MainFit) # Run optimization based on MainFit elif MainFit.OptJob.lower() == 'scpt2' or\ MainFit.OptJob.lower() == 'scsrpa': if MainFit.ProjTool != 'aims': print_String(iout,'At present, the optimization for'+\ 'could only be combined with FHI-aims package',1) return MainFit.get_AimIOCmd() MainFit.get_OptInit() MainFit.get_OptAlgo() MainFit.get_OptFunc() run_optimization(MainFit) # Run optimization based on MainFit if MainFit.OptAlgo != 'batch': MainFit.OptAlgo = 'batch' run_optimization(MainFit) # Print details for final one TimeEnd = time() - TimeStart # Count time consuming print_String(iout, 'Total job time: %10.2f(wall)' % TimeEnd, 1) print_String(iout, 'Fitting end at ' + ctime(), 1) print_List(iout, __info__, 2, '%s' % '-' * 76 + '==') # Print Authors Info. iout.close() return
def run_optimization(FitClass): '''\ run optimization based on FitClass\ ''' from my_io import print_List if FitClass.OptAlgo == 'leastsq': # leastsq algorithm plsq = opt.leastsq(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass])) tmpresult = [float(x) for x in plsq[0]] print_List(FitClass.IOut, tmpresult, 4, Info='Optimized result by leastsq algorithm') FitClass.InitGuess = tmpresult[:] elif FitClass.OptAlgo[:11] == 'fmin_cobyla': # fmin algorithm try: from opt_func import opt_func_constrained except: print_Error(FitClass.IOut,'\"opt_func_constrained\"'+\ ' is required for \"fmin_cobyla\"') interval = 1.0 # Set initial changes for i in FitClass.InitGuess: if abs(i) < interval: interval = abs(i) if i == 0.0: interval = 1.0 break plsq = opt.fmin_cobyla(calc_statistic,FitClass.InitGuess,\ cons=opt_func_constrained, args=tuple([FitClass]),rhobeg=interval) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by fmin_cobyla algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:4] == 'fmin': # fmin algorithm plsq = opt.fmin(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass])) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by fmin algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:5] == 'brute': # brute algorithm plsq = opt.brute(calc_statistic,\ ((-4.,-2.),(-1.,1.),(-4.,-2.),(-1.,1.)),\ args=(FitClass)) print_List(FitClass.IOut, plsq, 4, Info='Optimized result by brute algorithm') FitClass.InitGuess = plsq[:] elif FitClass.OptAlgo[:6] == 'anneal': # anneal algorithm plsq = opt.anneal(calc_statistic,FitClass.InitGuess,\ args=tuple([FitClass]),dwell=200,maxiter=2000,lower=0.0,upper=1.0, T0=200.0) print_List(FitClass.IOut, plsq[0], 4, Info='Optimized result by anneal algorithm') FitClass.InitGuess = plsq[0][:] elif FitClass.OptAlgo[:5] == 'batch': # Just DEBUGING calc_statistic(FitClass.InitGuess, FitClass) return
def calc_statistic_scsrpa(C, FitClass): '''\ return statistic informations for SCPT2 type optimization\ ''' import time from os.path import isdir, abspath, isfile, splitext from os import listdir, remove, system from my_io import print_List from my_io import print_String from my_io import print_Error from opt_func import update_aims_scsrpa import re print_List(FitClass.IOut, C, 4, Info='Testing parameters in this round') # Initialize the running folder if isdir(FitClass.ProjDir): for xFile in listdir(FitClass.ProjDir): axFile = '%s/%s' % (FitClass.ProjDir, xFile) if isfile(axFile) and xFile[-4:] == '.log': remove(axFile) if isdir(axFile): for yFile in listdir(axFile): ayFile = '%s/%s' % (axFile, yFile) if isfile(ayFile) and\ yFile=='RUNNING' or\ yFile[-4:]=='.log': remove(ayFile) time.sleep(2) update_aims_scsrpa(C, FitClass) if FitClass.BatchType == 'serial': FitClass.run_AimBatch() elif FitClass.BatchType == 'queue': # we should not re-run all finished jobs during this check tmpProjCtrl = FitClass.ProjCtrl FitClass.ProjCtrl = 2 # we would like to turn of detailed output during the check tmpIPrint = FitClass.IPrint FitClass.IPrint = 0 FlagBatch = FitClass.run_AimBatch() interval = 0 while not FlagBatch: time.sleep(2) if interval >= 100: if FitClass.BatchCmd == 'bsub': qq = 'bjobs' else: qq = 'qstate' qn = FitClass.BatchQueueName un = 'wenxinz' system('bjobs > checkFile') cFile = open('checkFile', 'r') tFile = cFile.read() cFile.close() #WARNNING: dangerous! should be updated by further effort tmpString =\ '(?P<iters>\d+)\s*%s\s*RUN\s*%s\s*\S*\s*\S*\s*(?P<jobs>\S+)' %(un,qn) p16 = re.compile(tmpString) p16p = p16.findall(tFile) for (jobi, jobn) in p16p: for job in FitClass.BatcList: fn, fe = splitext(job[2]) fR = '%s/%s/RUNNING' % (FitClass.ProjDir, fn) if fn == jobn[-len(fn):] and isfile(fR): remove(fR) xFile = '%s/%s/%s.log' % (FitClass.ProjDir, fn, fn) if isfile(xFile): remove(xFile) xFile = '%s/%s.log' % (FitClass.ProjDir, fn) if isfile(xFile): remove(xFile) os.system('bkill %s' % jobi) print_String( FitClass.IOut, 'Unexpected error for the job of %s' % fn, 1) continue interval = 0 FlagBatch = FitClass.run_AimBatch() interval = interval + 2 FitClass.ProjType = tmpProjCtrl FitClass.IPrint = tmpIPrint # Print the results when all jobs are finished FlagBatch = FitClass.run_AimBatch() print_String(FitClass.IOut, 'This batch takes %i Seconds' % interval, FitClass.IPrint) if FitClass.OptAlgo[:5] == 'batch': FitClass.get_OptResu(iop=1) print_List(FitClass.IOut,FitClass.InitGuess,\ 4,Info='Batch corresponds to these parameters ::') FitClass.run_Statistic() else: FitClass.get_OptResu(iop=0) AD, wAD, MAD, wMAD, RMS, wRMS = FitClass.run_Statistic() if FitClass.OptAlgo == 'leastsq': return array(FitClass.Result) elif FitClass.OptAlgo[-3:] == 'rms': return wRMS elif FitClass.OptAlgo[-3:] == 'mad': return wMAD elif FitClass.OptAlgo[-2:] == 'ad': return wAD else: print_Error(FitClass.IOut, 'Error in return required type of statistic data'+\ ' \"calc_statistic()->%s\"' % FitClass.OptAlgo) return
def get_ForcList(self): '''\ Calculate the corresponding force for DFT+D methods\n\ Contribution of atom j in atom i is :\n\ Fij_x = - Scal * (6.0 * (X_i-X_j) ) /|r_i-r_j|^(-8)\n\ Fij_y = - Scal * (6.0 * (Y_i-Y_j) ) /|r_i-r_j|^(-8)\n\ Fij_z = - Scal * (6.0 * (Z_i-Z_j) ) /|r_i-r_j|^(-8)\ ''' from math import sqrt from my_io import print_List from my_io import print_String Scal = -1.0 * DFTD.FauCon * self.SixPara self.ForcList = [] if self.IPrint>=2: print_String(self.IOut, '%10s%10s%8s%8s%8s%8s%8s%8s%8s%8s%8s%8s' %('Atom1','Atom2','Rij','Rr','Cij','Damp', 'Fx_Disp','Fx_FDamp','Fy_Disp','Fy_FDamp', 'Fz_Disp','Fz_FDamp'),2) if self.GauIO.NAtom==1: NTT = self.GauIO.NAtom*3 self.ForcList = [0.0]*NTT if self.IPrint>=2: print_List(self.IOut,self.ForcList,4, 'CM Dispersion Cartesian Gradient:') return for i in range(self.GauIO.NAtom): TmpForceX = 0.0 TmpForceY = 0.0 TmpForceZ = 0.0 for j in range(self.GauIO.NAtom): if j!=i: Rij = get_Dist(self.CList[i],self.CList[j]) Rr = DFTD.R0Para[self.IAn[i]]+\ DFTD.R0Para[self.IAn[j]] Cijg= DFTD.C6Para[self.IAn[i]]*\ DFTD.C6Para[self.IAn[j]] Cij = sqrt(Cijg) Damp= get_Damp(self.d,Rij,Rr) XTerm1, YTerm1, ZTerm1 =\ get_Force(self.CList[i],self.CList[j],Rij) XTerm = XTerm1 * Damp YTerm = YTerm1 * Damp ZTerm = ZTerm1 * Damp XTerm2, YTerm2, ZTerm2 =\ get_DampD(self.CList[i],self.CList[j],\ self.d,Rij,Rr) XTermD = XTerm2 * pow(Rij,-6) YTermD = YTerm2 * pow(Rij,-6) ZTermD = ZTerm2 * pow(Rij,-6) TmpForceX += XTerm * Cij TmpForceY += YTerm * Cij TmpForceZ += ZTerm * Cij TmpForceX += XTermD * Cij TmpForceY += YTermD * Cij TmpForceZ += ZTermD * Cij if self.IPrint>=2: self.IOut.write(\ '%7s%3dth%5s%3dth%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f%8.4f\n' \ %(self.AtLabel[i],i+1,self.AtLabel[j],j+1,\ Rij,Rr,Cij,Damp,XTerm,XTermD,YTerm,YTermD,ZTerm,ZTermD)) TmpForceX = Scal * TmpForceX TmpForceY = Scal * TmpForceY TmpForceZ = Scal * TmpForceZ self.ForcList.append(TmpForceX) self.ForcList.append(TmpForceY) self.ForcList.append(TmpForceZ) if self.IPrint>=2: print_List(self.IOut,self.ForcList,4, 'CM Dispersion Cartesian Gradient:') return