Пример #1
0
	def read_trajectory(self):
		if(not path.exists(self.trr_fn)):
			raise(Exception("%s not found."%self.trr_fn))
		
		if(self.__dict__.has_key("_trajectory_cache") and self.__dict__["_trajectory_cache_time"] >= path.getmtime(self.trr_fn)):
			#print "Using trajectory cache."
			return(self._trajectory_cache)
		
		# using print for newline, so that converter warnings are more readable
		#sys.stdout.write("Loading trr-file: %s... "%self.trr_fn)
		#sys.stdout.flush()
		print("Loading trr-file: %s... "%self.trr_fn)
		frames_int = self.pool.converter.read_trajectory(self.trr_fn)
		print("done.")
								
		if(self.has_internals and self.has_restraints):
			phi_values = get_phi(frames_int, self)
			penalty_potential = np.zeros(frames_int.n_frames)
			for (r, c) in zip(self.restraints, self.pool.converter):
				penalty_potential += r.energy(frames_int[:,c])				

			beta = self.pool.thermo_beta
			frameweights = phi_values / np.exp(-beta * penalty_potential)
		
		else:
			phi_values = np.zeros(frames_int.n_frames)
			penalty_potential = np.zeros(frames_int.n_frames)
			frameweights = np.ones(frames_int.n_frames)
		
		trajectory = InternalArray(frames_int.converter, frames_int.array, frameweights)
		self.__dict__["_phi_values_cache"] = phi_values
		self.__dict__["_penalty_potential_cache"] = penalty_potential
		self.__dict__["_trajectory_cache"] = trajectory
		self.__dict__["_trajectory_cache_time"] = path.getmtime(self.trr_fn)
		return(self._trajectory_cache)
Пример #2
0
def get_phi_mat(ints, nodes):
    "caclculates the membership of every frame to every node"
    phi = np.empty((ints.n_frames, len(nodes)))

    for j in xrange(len(nodes)):
        phi[:, j] = get_phi(ints, nodes[j])

    return phi
Пример #3
0
def get_phi_mat (ints, nodes):
	"caclculates the membership of every frame to every node"
	phi = np.empty((ints.n_frames, len(nodes)))
	
	for j in xrange(len(nodes)):
		phi[:,j] = get_phi(ints, nodes[j])
		
	return phi
Пример #4
0
def reweight_presampling(nodes, presamp_temp, moi_energies, sol_energy):
	print "Presampling analysis reweighting: see formula 18 in Fackeldey, Durmaz, Weber 2011"
	
	# presampling data
	presampling_internals = nodes[0].pool.root.trajectory # alternatively pool[0].trajectory
	
	# presampling and sampling beta
	beta_samp = nodes[0].pool.thermo_beta
	beta_presamp = 1/(presamp_temp*BOLTZMANN*AVOGADRO)
		
	# calculate free energy per node 
	for n in nodes:
		log = open(n.reweighting_log_fn, "a") # using separate log-file
		def output(message):
			print(message)
			log.write(message+"\n")
			
		output("======= Starting node reweighting %s"%datetime.now())
		
		# get potential V and substract penalty potential
		energies = load_energies(n, with_penalty=False, with_sol=sol_energy, with_moi_energies=moi_energies)

		frame_weights = n.frameweights
		phi_values = n.phi_values
		phi_weighted_energies = energies + get_phi_potential(n.trajectory, n)


		# calculate mean V and standard deviation
		n.obs.mean_V = np.average(phi_weighted_energies, weights=frame_weights)
		n.tmp['weight'] = 1.0
		n.obs.std_V = np.sqrt(np.average(np.square(phi_weighted_energies - n.obs.mean_V), weights=frame_weights))
			
		# number of presampling points in node i => free energy at high temperature
		n.tmp['presamp_weight'] = np.sum(get_phi(presampling_internals, n))
		n.tmp['presamp_A']= -1/beta_presamp * np.log(n.tmp['presamp_weight'])
			
		# estimate global optimum potential energy
		factor= 1.0
		n.tmp['opt_pot_energy']= n.obs.mean_V - 3.0 * factor * n.obs.std_V
			
		# compute free energy and entropy at sampling temperature
		n.obs.S = 0.0 #TODO can we get separate entropy from the term below?
		n.obs.A = (beta_samp - beta_presamp) / beta_samp * n.tmp['opt_pot_energy'] + np.log(beta_samp / beta_presamp) * factor * n.obs.std_V + (beta_presamp / beta_samp) * n.tmp['presamp_A']
		if('refpoints' in n.obs):
			del n.obs['refpoints']

		log.close()

	nodes.sort(key = lambda n: n.obs.A) # sort in ascending order by free energy values
	for (n1, n2) in zip(nodes[1:], nodes[:-1]): # calculate and normalize weights
		n1.tmp['weight'] = np.exp(-nodes[0].pool.thermo_beta*( n1.obs.A - n2.obs.A )) * n2.tmp['weight']
Пример #5
0
def calc_matrix(nodes, shift=0, cache_denom=False):
	mat = np.zeros( (len(nodes), len(nodes)) )
	for (i, ni) in enumerate(nodes):
		print("Working on: %s"%ni)
		if(cache_denom):
			phi_denom = get_phi_denom(ni.trajectory, nodes)
		frame_weights = ni.frameweights
		if shift > 0:
			frame_weights = frame_weights[:-shift]
		for (j, nj) in enumerate(nodes):
			if(cache_denom):
				mat[i, j] = np.average(get_phi_num(ni.trajectory, nj)[shift:] / phi_denom[shift:], weights=frame_weights)
			else:
				mat[i, j] = np.average(get_phi(ni.trajectory, nj)[shift:], weights=frame_weights)
	return(mat)
Пример #6
0
    def read_trajectory(self):
        if (not path.exists(self.trr_fn)):
            raise (Exception("%s not found." % self.trr_fn))

        if (self.__dict__.has_key("_trajectory_cache")
                and self.__dict__["_trajectory_cache_time"] >= path.getmtime(
                    self.trr_fn)):
            #print "Using trajectory cache."
            return (self._trajectory_cache)

        # using print for newline, so that converter warnings are more readable
        #sys.stdout.write("Loading trr-file: %s... "%self.trr_fn)
        #sys.stdout.flush()
        print("Loading trr-file: %s... " % self.trr_fn)
        frames_int = self.pool.converter.read_trajectory(self.trr_fn)
        print("done.")

        if (self.has_internals and self.has_restraints):
            phi_values = get_phi(frames_int, self)
            penalty_potential = np.zeros(frames_int.n_frames)
            for (r, c) in zip(self.restraints, self.pool.converter):
                penalty_potential += r.energy(frames_int[:, c])

            beta = self.pool.thermo_beta
            frameweights = phi_values / np.exp(-beta * penalty_potential)

        else:
            phi_values = np.zeros(frames_int.n_frames)
            penalty_potential = np.zeros(frames_int.n_frames)
            frameweights = np.ones(frames_int.n_frames)

        trajectory = InternalArray(frames_int.converter, frames_int.array,
                                   frameweights)
        self.__dict__["_phi_values_cache"] = phi_values
        self.__dict__["_penalty_potential_cache"] = penalty_potential
        self.__dict__["_trajectory_cache"] = trajectory
        self.__dict__["_trajectory_cache_time"] = path.getmtime(self.trr_fn)
        return (self._trajectory_cache)