def update_energy(self, torschange, angchange, dict): QSimulation.k_Qpin = dict['k_Qpin'] Simulation.nsigma2 = dict['nsigma2'] Simulation.totnc = dict['totnc'] Simulation.update_energy(self, torschange, angchange, dict) self.newQ = energyfunc.nativecontact(self.r2new, Simulation.nativeparam, Simulation.nsigma2) / Simulation.totnc self.u1 += QSimulation.k_Qpin*(self.newQ - self.Qpin)**2
def setenergy(self): # sets the u0, r2, torsE, angE from the current configuration # called when restarting from a checkpoint Simulation.setenergy(self) self.Q = energyfunc.nativecontact( self.r2, Simulation.nativeparam, Simulation.nsigma2) / Simulation.totnc self.u0 += QSimulation.k_Qpin * (self.Q - self.Qpin)**2
def setenergy(self): Simulation.setenergy(self) self.surfE = energyfunc.csurfenergy(self.coord, SurfaceSimulation.surface, Simulation.numbeads, SurfaceSimulation.nspint, SurfaceSimulation.surfparam, SurfaceSimulation.scale) self.u0 += sum(self.surfE)
def update_energy(self, torschange, angchange, dict): QSimulation.k_Qpin = dict['k_Qpin'] Simulation.nsigma2 = dict['nsigma2'] Simulation.totnc = dict['totnc'] Simulation.update_energy(self, torschange, angchange, dict) self.newQ = energyfunc.nativecontact( self.r2new, Simulation.nativeparam, Simulation.nsigma2) / Simulation.totnc self.u1 += QSimulation.k_Qpin * (self.newQ - self.Qpin)**2
def update_energy(self, torschange, angchange, dict): Simulation.numbeads = dict['numbeads'] SurfaceSimulation.surface = dict['surface'] SurfaceSimulation.nspint = dict['nspint'] SurfaceSimulation.surfparam = dict['surfparam'] SurfaceSimulation.scale = dict['scale'] Simulation.update_energy(self, torschange, angchange, dict) self.newsurfE = energyfunc.csurfenergy(self.newcoord, SurfaceSimulation.surface, Simulation.numbeads, SurfaceSimulation.nspint, SurfaceSimulation.surfparam,SurfaceSimulation.scale) self.u1 += numpy.sum(self.newsurfE)
def output(self): Simulation.output(self) if self.trmoves: print 'translation: %d percent acceptance (%i/%i)' % ( float(self.acceptedtr) / float(self.trmoves) * 100, self.acceptedtr, self.trmoves) if self.rmoves: print 'rotation: %d percent acceptance (%i/%i)' % ( float(self.acceptedr) / float(self.rmoves) * 100, self.acceptedr, self.rmoves)
def update_energy(self, torschange, angchange, dict): Simulation.numbeads = dict['numbeads'] SurfaceSimulation.surface = dict['surface'] SurfaceSimulation.nspint = dict['nspint'] SurfaceSimulation.surfparam = dict['surfparam'] SurfaceSimulation.scale = dict['scale'] Simulation.update_energy(self, torschange, angchange, dict) self.newsurfE = energyfunc.csurfenergy(self.newcoord, SurfaceSimulation.surface, Simulation.numbeads, SurfaceSimulation.nspint, SurfaceSimulation.surfparam, SurfaceSimulation.scale) self.u1 += numpy.sum(self.newsurfE)
def __init__(self, name, outputdirectory, coord, temp, surf_coord): self.coord = coord self.addsurface(surf_coord) # translates self.coord Simulation.__init__(self, name, outputdirectory, self.coord, temp) # self.coord != coord anymore self.surfE_array = numpy.empty((Simulation.totmoves/Simulation.save + 1,2)) self.surfE_array[0,:] = self.surfE self.moveparam = numpy.array([2., # translation 2., # rotation 10., # bend 10., # torsion 1., # global crankshaft 5.]) # ParRot move self.moveparam = self.moveparam * numpy.pi / 180 * self.T / 300 * 50 / Simulation.numbeads self.energyarray[0] = self.u0 self.trmoves = 0 self.rmoves = 0 self.acceptedtr = 0 self.acceptedr = 0
def __init__(self, name, outputdirectory, coord, temp, surf_coord): self.coord = coord self.addsurface(surf_coord) # translates self.coord Simulation.__init__(self, name, outputdirectory, self.coord, temp) # self.coord != coord anymore self.surfE_array = numpy.empty( (Simulation.totmoves / Simulation.save + 1, 2)) self.surfE_array[0, :] = self.surfE self.moveparam = numpy.array([ 2., # translation 2., # rotation 10., # bend 10., # torsion 1., # global crankshaft 5. ]) # ParRot move self.moveparam = self.moveparam * numpy.pi / 180 * self.T / 300 * 50 / Simulation.numbeads self.energyarray[0] = self.u0 self.trmoves = 0 self.rmoves = 0 self.acceptedtr = 0 self.acceptedr = 0
def save_state(self, dict): Simulation.save = dict['save'] Simulation.save_state(self, dict) self.surfE_array[self.move / Simulation.save, :] = self.surfE
def loadextend(self, extenddirec): Simulation.loadextend(self, extenddirec) # u0 is reset self.energyarray[0] = self.u0 self.surfE_array[0, :] = self.surfE
def loadstate(self): Simulation.loadstate(self) self.surfE_array = numpy.load('%s/surfenergy%s.npy' % (self.out, self.suffix))
def accept_state(self): Simulation.accept_state(self) self.Q = self.newQ
def __init__(self, name, outputdirectory, coord, temp, Qpin): self.Qpin = Qpin Simulation.__init__(self, name, outputdirectory, coord, temp) self.suffix = '%i_%2.2f' % (int(temp), Qpin)
def save_state(self, dict): Simulation.save = dict['save'] Simulation.save_state(self, dict) self.surfE_array[self.move/Simulation.save,:] = self.surfE
def loadextend(self, extenddirec): Simulation.loadextend(self, extenddirec) # u0 is reset self.energyarray[0] = self.u0 self.surfE_array[0,:] = self.surfE
def loadstate(self): Simulation.loadstate(self) self.surfE_array = numpy.load('%s/surfenergy%s.npy' %(self.out, self.suffix))
def output(self): Simulation.output(self) if self.trmoves: print 'translation: %d percent acceptance (%i/%i)' %(float(self.acceptedtr)/float(self.trmoves)*100, self.acceptedtr, self.trmoves) if self.rmoves: print 'rotation: %d percent acceptance (%i/%i)' %(float(self.acceptedr)/float(self.rmoves)*100, self.acceptedr, self.rmoves)
def accept_state(self): Simulation.accept_state(self) self.surfE = self.newsurfE
def setenergy(self): # sets the u0, r2, torsE, angE from the current configuration # called when restarting from a checkpoint Simulation.setenergy(self) self.Q = energyfunc.nativecontact(self.r2, Simulation.nativeparam, Simulation.nsigma2) / Simulation.totnc self.u0 += QSimulation.k_Qpin*(self.Q - self.Qpin)**2
def main(): print 'Start time', t1 print '' # --- process inputs --- # # to do: add some checks to input, e.g. files exist, nmoves > save args = parse_args() coord, _ = writetopdb.get_coord(args.filename) numbeads = len(coord) mass = energyfunc.getmass('%stop' % (args.paramfile[0:-5]), numbeads) T = get_temperature(args) beta = 1 / (kb * T) percentmove = get_movefreq(args) tsize, tsteps = args.md tsize /= 100. # input is in fs tsteps = int(tsteps) direc = set_up_dir(args) if args.Qfile: Q = numpy.loadtxt(args.Qfile) else: Q = '' # --- report inputs --- # print '' print '-----Inputs-----' print 'Output directory:', os.path.abspath(direc) print 'System:', args.filename if args.Qfile: print ' Running Q umbrella sampling with restraints at:', Q if args.Zumbrella: print ' Running surface umbrella sampling simulation with pinning at %f A' % args.umbrella assert (args.surf == True) if args.surf: print ' Running surface simulation' print 'Number of temperature replicas:', args.nreplicas print 'Temperature(s):' print T print 'Total number of moves:', args.totmoves print 'Save interval:', args.save print 'Replica exchange interval:', args.swap print 'Swaps at each exchange point:', args.nswap print 'Ratio of moves frequencies (tr:rot:ang:dih:crank:parrot:MD):', args.freq print 'MD time step:', args.md[0], ' fs' print 'MD steps per move:', args.md[1] print '' # --- get parameters from .param file --- # angleparam = energyfunc.getangleparam(args.paramfile, numbeads) torsparam = energyfunc.gettorsionparam(args.paramfile, numbeads) # --- pregenerate list of interactions --- # numint = numpy.around(comb(numbeads, 2)) # number of interactions numint = numint - 2 * (numbeads - 2) - 1 # don't count 12 and 13 neighbors # --- get native LJ parameter --- # nativeparam = energyfunc.getnativefix_n( args.paramfile, numint, numbeads) # [ones and zeros, native epsilon, native sigma] totnc = numpy.sum(nativeparam[:, 0]) #total native contacts nativecutoff2 = 1.2**2 nsigma2 = nativecutoff2 * nativeparam[:, 2] * nativeparam[:, 2] # --- get nonnative LJ parameter --- # (nonnativesig, nnepsil) = energyfunc.getLJparam_n( args.paramfile, numbeads, numint) #[nonnative sigmas for every interaction, epsilon (one value)] nonnatindex = -1 * (nativeparam[:, 0] - 1) # array of ones and zeros nonnativeparam = numpy.column_stack( (nonnatindex, nonnativesig)) #[ones and zeros, nonnative sigma] # --- set Simulation class variables --- # Simulation.angleparam = angleparam Simulation.torsparam = torsparam Simulation.totnc = totnc Simulation.nativeparam = nativeparam Simulation.nsigma2 = nsigma2 Simulation.nonnativeparam = nonnativeparam Simulation.nnepsil = nnepsil Simulation.totmoves = args.totmoves Simulation.save = args.save Simulation.numbeads = numbeads Simulation.numint = numint Simulation.mass = mass Simulation.percentmove = percentmove Simulation.tsize = tsize Simulation.tsteps = tsteps # --- put class variables in a dictionary for pprun --- # dict = { 'tsize': tsize, 'tsteps': tsteps, 'percentmove': percentmove, 'numbeads': numbeads, 'save': args.save, 'totmoves': args.totmoves, 'numint': numint, 'angleparam': angleparam, 'torsparam': torsparam, 'nativeparam': nativeparam, 'nonnativeparam': nonnativeparam, 'nnepsil': nnepsil, 'nsigma2': nsigma2, 'writetraj': args.writetraj, 'mass': mass, 'totnc': totnc } # --- set up surface --- # if args.surf: # to do: not have surface be hardcoded xlength = 135 ylength = 135 spacing = 7 yspacing = spacing * 3.**.5 surface = surfacesimulation.getsurf(xlength + 15, ylength + 15, spacing) surfacesimulation.writesurf('surface.pdb', surface) nsurf = len(surface) nspint = nsurf * numbeads # surface-protein interactions sfile = args.paramfile[0:args.paramfile.find('GO_')] + args.paramfile[ args.paramfile.find('GO_') + 3:-5] + 'pdb' surfparam = energyfunc.getsurfparam(sfile, args.surfparamfile, numbeads, nsurf, nspint, args.scale) SurfaceSimulation.scale = args.scale SurfaceSimulation.surface = surface SurfaceSimulation.nsurf = nsurf SurfaceSimulation.nspint = nspint SurfaceSimulation.surfparam = surfparam dict.update({ 'nspint': nspint, 'nsurf': nsurf, 'surfparam': surfparam, 'surface': surface, 'xlength': xlength, 'ylength': ylength, 'spacing': spacing, 'yspacing': yspacing, 'scale': args.scale }) print '-----Surface Details-----' print 'Surface is %i by %i array of leucine residues with spacing %i A' % ( xlength, ylength, spacing) print 'Surface energy parameters scaled by %f' % args.scale print '' if args.Qfile: QSimulation.k_Qpin = args.k_Qpin dict.update({'k_Qpin': args.k_Qpin}) # --- instantiate replicas --- # replicas = [] for i in range(args.nreplicas): name = 'replica%i' % i if args.Zumbrella: replicas.append( UmbrellaSimulation(name, os.path.abspath(direc), coord, T[i], surface, args.Zumbrella, mass, args.k_Zpin)) elif args.surf: if args.Qfile: replicas.append( QSurfaceSimulation(name, os.path.abspath(direc), coord, T[i], surface, Q[i])) else: replicas.append( SurfaceSimulation(name, os.path.abspath(direc), coord, T[i], surface)) else: if args.Qfile: replicas.append( QSimulation(name, os.path.abspath(direc), coord, T[i], Q[i])) else: replicas.append( Simulation(name, os.path.abspath(direc), coord, T[i])) replicas[i].whoami = i if args.writetraj: f = open('%s/trajectory%i' % (replicas[i].out, int(replicas[i].T)), 'wb') numpy.save(f, replicas[i].coord) f.close() # --- begin simulation --- # if args.Zumbrella: print 'Starting umbrella simulation... at %f A' % args.Zumbrella elif args.surf: print 'Starting surface simulation...' else: print 'Starting simulation...' move = 0 swapaccepted = numpy.zeros(args.nreplicas - 1) swaprejected = numpy.zeros(args.nreplicas - 1) protein_location = [[i] for i in range(args.nreplicas)] # --- setup ppserver --- # if args.cluster: print ' Running on a cluster...' if args.Zumbrella: try: f = open('nodefile%i-%s.txt' % (args.id, args.Zumbrella), 'r') except: f = open('nodefile%i-%i.txt' % (args.id, args.Zumbrella), 'r') else: f = open('nodefile%i.txt' % args.id, 'r') ppservers = f.read().split('\n') f.close() ppservers = filter(None, ppservers) ppservers = [x + ':43334' for x in ppservers] ppservers = tuple(ppservers) job_server = pp.Server(0, ppservers=ppservers) print 'Running pp on: ' print ppservers print 'Starting pp with', job_server.get_ncpus(), 'workers' else: # running on one machine job_server = pp.Server(ppservers=()) print 'Starting pp with', job_server.get_ncpus(), 'workers' # --- set up simulation extension --- # if args.extend: print ' Extending simulation %i...' % args.extend if args.Zumbrella: extenddirec = os.getcwd() + '/replicaexchange/umbrella%i/%i' % ( args.extend, int(args.Zumbrella)) else: extenddirec = os.getcwd( ) + '/replicaexchange/simlog%i' % args.extend if not os.path.exists(extenddirec): sys.exit('Simulation %i does not exist at %s' % (args.extend, extenddirec)) input = open('%s/protein_location.pkl' % extenddirec, 'rb') protein_location = cPickle.load(input) protein_location = [[protein_location[i][-1]] for i in range(args.nreplicas)] input.close() for i in range(args.nreplicas): replicas[i].loadextend(extenddirec) replicas[protein_location[i][-1]].whoami = i # --- load existing simulation --- # if args.restart: print ' Restarting from last checkpoint...' move, replicas, protein_location = loadstate(direc, replicas, protein_location) ti = datetime.datetime.now() tcheck = ti for i in xrange(move / args.swap, args.totmoves / args.swap): replicas = pprun(job_server, replicas, args.swap, dict) job_server.wait() if args.swap != args.totmoves: swapaccepted, swaprejected, protein_location = replicaexchange.tryrepeatedswaps( args, replicas, swapaccepted, swaprejected, protein_location, beta, Q) tnow = datetime.datetime.now() t_remain = (tnow - ti) / (i + 1) * (args.totmoves / args.swap - i - 1) if not args.cluster: stdout.write(str(t_remain) + '\r') stdout.flush() # checkpoint if tnow - tcheck > datetime.timedelta(seconds=900): #every 15 minutes savestate(args, direc, replicas, protein_location) f = open('%s/status.txt' % direc, 'w') f.write('Completed %i moves out of %i moves\n' % (replicas[0].move, args.totmoves)) f.write('%i swaps performed in %s\n' % (i, str(tnow - ti))) #useful for the MD vs. MC comparison f.close() tcheck = tnow # --- output --- # job_server.print_stats() output = open('%s/protein_location.pkl' % direc, 'wb') cPickle.dump(protein_location, output) output.close() for i in range(args.nreplicas): replicas[i].output() replicas[i].saveenergy() replicas[i].savenc() print 'The average Q is %f' % (numpy.average(replicas[i].nc)) replicas[i].savecoord() if args.surf: replicas[i].savesurfenergy() if args.Zumbrella: replicas[i].save_z() if args.swap != args.totmoves: Q_trajec_singleprot = numpy.zeros( (args.nreplicas, args.totmoves / args.save + 1)) k = args.swap / args.save for i in xrange(len(protein_location[0])): for j in range(args.nreplicas): rep = protein_location[j][i] Q_trajec_singleprot[j, k * i + 1:k * (i + 1) + 1] = replicas[rep].nc[k * i + 1:k * (i + 1) + 1] #Q_trajec_singleprot[:,0] = totnc numpy.save('%s/Qtraj_singleprot.npy' % direc, Q_trajec_singleprot) for i in range(args.nreplicas - 1): print 'swaps accepted between replica%i and replica%i: %3.2f percent' % ( i, i + 1, (swapaccepted[i] / float(swapaccepted[i] + swaprejected[i]) * 100)) print 'Total swaps accepted: %i' % numpy.sum(swapaccepted) print 'Total swaps rejected: %i' % numpy.sum(swaprejected)