Пример #1
0
 def test_double_run(self):
     # running rdf twice should give the same result
     self._linear_water()
     s1, s2 = self._get_sels()
     rdf = InterRDF(s1, s2).run()
     rdf.run()
     assert_(len(rdf.count[rdf.count == 4]) == 2)
Пример #2
0
    def test_nbins(self):
        s1 = self.u.atoms[:3]
        s2 = self.u.atoms[3:]

        rdf = InterRDF(s1, s2, nbins=412)

        assert_(len(rdf.bins) == 412)
Пример #3
0
def rdf(u, thresholds, n_bins, pair, block):
    '''Computes water radial distribution functions (RDF).

    Args:
        u: MDAnalysis Universe object containing trajectory.
        t: Thresholds specifying layer boundaries.
        n_bins: Number of bins for RDF.
        pair: Pair of elements to consider.
        block: Range of frames composing block.

    Returns:
        Radial distribution function.
    '''

    atoms = list(pair)

    # Select appropriate atom groups within specified layers
    down = 'prop z > %s and prop z < %s' % (*thresholds[:2], )
    up = 'prop z > %s and prop z < %s' % (*thresholds[2:], )
    ag1 = u.select_atoms('name %s and ((%s) or (%s))' % (atoms[0], down, up),
                         updating=True)
    ag2 = u.select_atoms('name %s' % atoms[1])

    # Compute the RDF between ag1 and ag2
    rdf = InterRDF(ag1, ag2, nbins=n_bins, range=(0, 6), verbose=True)
    rdf.run(start=block.start, stop=block.stop)

    return (rdf.results.bins, rdf.results.rdf)
Пример #4
0
 def test_count_sum(self):
     # OW vs HW
     # should see 8 comparisons in count
     self._linear_water()
     s1, s2 = self._get_sels()
     rdf = InterRDF(s1, s2).run()
     assert_(rdf.count.sum() == 8)
Пример #5
0
def test_range(u):
    s1 = u.atoms[:3]
    s2 = u.atoms[3:]
    rmin, rmax = 1.0, 13.0
    rdf = InterRDF(s1, s2, range=(rmin, rmax)).run()

    assert rdf.edges[0] == rmin
    assert rdf.edges[-1] == rmax
Пример #6
0
    def test_range(self):
        s1 = self.u.atoms[:3]
        s2 = self.u.atoms[3:]
        rmin, rmax = 1.0, 13.0
        rdf = InterRDF(s1, s2, range=(rmin, rmax)).run()

        assert_(rdf.edges[0] == rmin)
        assert_(rdf.edges[-1] == rmax)
Пример #7
0
def test_density(u, sels, density, value):
    kwargs = {'density': density} if density is not None else {}
    rdf = InterRDF_s(u, sels, **kwargs).run()
    assert_almost_equal(max(rdf.rdf[0][0][0]), value)
    if not density:
        s1 = u.select_atoms('name ZND and resid 289')
        s2 = u.select_atoms(
            'name OD1 and resid 51 and sphzone 5.0 (resid 289)')
        rdf_ref = InterRDF(s1, s2).run()
        assert_almost_equal(rdf_ref.rdf, rdf.rdf[0][0][0])
Пример #8
0
    def test_exclusion(self):
        # should see two distances with 4 counts each
        self._linear_water()

        s1, s2 = self._get_sels()

        rdf = InterRDF(s1, s2, exclusion_block=(1, 2))
        rdf.run()

        assert_(rdf.count.sum() == 4)
Пример #9
0
    def test_count(self):
        # should see two distances with 4 counts each
        self._linear_water()

        s1, s2 = self._get_sels()

        rdf = InterRDF(s1, s2)
        rdf.run()

        assert_(len(rdf.count[rdf.count == 4]) == 2)
Пример #10
0
def rdf_mdanalysys(props):
    rdfs = {}
    for target in props['targets']:
        rdfs[target] = InterRDF(g1=sel[props['ref']],
                                g2=sel[target],
                                start=ps_frame(props['start_ps'], DT),
                                stop=ps_frame(props['stop_ps'], DT),
                                verbose=True,
                                range=props['r_range'],
                                nbins=props['nbins'])
        rdfs[target].run()
    return rdfs
Пример #11
0
    def setup(self, nbins, range_val):

        self.sel_str = 'name OW'

        self.u = MDAnalysis.Universe(TPR, XTC)

        try:
            self.sel = self.u.select_atoms(self.sel_str)[:200]
        except AttributeError:
            self.sel = self.u.selectAtoms(self.sel_str)[:200]

        # do not include initialization of the
        # InterRDF object in the benchmark itself

        self.rdf = InterRDF(g1=self.sel,
                            g2=self.sel,
                            nbins=nbins,
                            range=range_val)
Пример #12
0
args = parse_command_line(sys.argv)

atom1 = "(segid %s and resid %s and name %s)" % \
    (args.isegid1, args.iresid1, args.iname1)
atom2 = "(segid %s and resid %s and name %s)" % \
    (args.isegid2, args.iresid2, args.iname2)
bins = int(args.inbins)
start = float(args.istart)
end = float(args.iend)

u = mda.Universe(args.ipdb, args.idcd, topology_format="PDB", format="DCD")
x = u.select_atoms(atom1)
y = u.select_atoms(atom2)

rdf = InterRDF(x, y, nbins=bins, range=(start, end))
rdf.run()
bins = rdf.bins
bins = np.around(bins, decimals=3)
RDF = rdf.rdf
zip(bins, RDF)

with open(args.output, 'w') as f:
    writer = csv.writer(f, delimiter='\t')
    writer.writerows(zip(bins, RDF))

with open(args.output) as f:
    g = [xtmp.strip() for xtmp in f]
    data = [tuple(map(float, xtmp.split())) for xtmp in g[0:]]
    time = [xtmp[0] for xtmp in data]
    rdf = [xtmp[1] for xtmp in data]
def compute_microstructure(molten_salt_system, ensemble, start, stop, step, nbins=500, rdf_flag=True, cn_flag=True, adf_flag=True):
	"""
		Description:
			compute microstructure info including radial distribution fuction(rdf), coordination number(cn) and angle distribution function(adf) using MDAnalysis module. adf is not completed

		Args:
			molten_salt_system: 
			ensemble: 
			start: 
			stop: 
			step: 
			rdf_flag: 
			cn_flag: 
			adf_flag: 

		Returns:
			return dicts according to setting
	"""

	global dir_molten_salt_system
	global dir_list

	# analysis elements and get cations and inions according cif file
	os.chdir(dir_molten_salt_system)
	cations_list, anions_list = [], []
	with open(molten_salt_system+'.cif', 'r') as f_cif:
		flag = False
		atoms_list = []
		for line in f_cif:
			if line.startswith(' _atom_type_oxidation_number'):
				flag = True
				continue
			if line.startswith('loop_'):
				flag = False
				continue
			if flag:
				tmp_tuple = tuple(list(filter(None, line.strip().split(' '))))
				atoms_list.append(tmp_tuple)
	for i in range(len(atoms_list)):
		if float(atoms_list[i][1]) > 0:
			cations_list.append((re.findall(r"[A-Za-z]+", atoms_list[i][0])[0]).upper()) # transfered to uppercase because MDAnalysis can just identify capitalization
		elif float(atoms_list[i][1]) < 0:
			anions_list.append((re.findall(r"[A-Za-z]+", atoms_list[i][0])[0]).upper())
	elements_list = cations_list+anions_list
	elements_count = len(elements_list)

	# determine # of pairs(used in rdf and cn)
	pairs_count = 0
	for i in range(elements_count):
		for j in range(i, elements_count):
			pairs_count += 1

	# radial_distribution_function_dense_dict, coordination_number_dense_dict= [{} for i in range(pairs_count)], [{} for i in range(pairs_count)] # 加密的
	radial_distribution_function_dict, coordination_number_dict= [{} for i in range(pairs_count)], [{} for i in range(pairs_count)]	# 原始的
	for dirname in dir_list:
		os.chdir(os.path.join(dir_molten_salt_system, dirname))
		deal_with_PDB(dirname, ensemble) # write MODEL and ENDMDL into xxx.pdb file and stored as traj.pdb
		universe = MDA.Universe('traj.pdb')
		# determine cutoff of rdf
		tmp_list = list(filter(None, os.popen('grep CRY '+dirname+'-'+ensemble+'.pdb').readlines()[-1].strip().split(' ')))
		equilibrium_lattice_constant_a = float(tmp_list[1])
		equilibrium_lattice_constant_b = float(tmp_list[2])
		equilibrium_lattice_constant_c = float(tmp_list[3])
		equilibrium_lattice_constant_min = min(equilibrium_lattice_constant_a, equilibrium_lattice_constant_b, equilibrium_lattice_constant_c)
		cutoff = equilibrium_lattice_constant_min//2

		# begin compute rdf and cn
		columns_count = -1
		for i in range(elements_count):
			for j in range(i, elements_count):
				columns_count += 1
				atomgroup1 = universe.select_atoms('name '+elements_list[i])
				atomgroup2 = universe.select_atoms('name '+elements_list[j])
				rdf = InterRDF(atomgroup1, atomgroup2, nbins=nbins, range=(0.0, cutoff))
				rdf.run(start=start, stop=stop, step=step) # after run, rdf and count have been generated
				rdf.rdf[0] = 0 # 第一个数莫名奇妙的超级大,本该为0
				
				# This part is discarded because of the limitation of 16MB for single file in mongodb
				'''
				# 加密10倍的
				# store rdf results into a dict
				# make curve smoother
				rdf_new = np.linspace(rdf.bins.min(), rdf.bins.max(), nbins*10) # 加密十倍, 画出来的图更光滑
				rdf_smooth = spline(rdf.bins, rdf.rdf, rdf_new)
				# get rdf_rmax(first peak) and rdf_rmin(first peak valley) of rdf
				rdf_rmax_index = np.argmax(rdf_smooth)
				rdf_rmax = rdf_new[rdf_rmax_index]
				rdf_rmin_index = np.argmin(rdf_smooth[rdf_rmax_index:])
				rdf_rmin = rdf_new[rdf_rmax_index:][rdf_rmin_index]
				radial_distribution_function_dense_dict[columns_count]['pair'] = elements_list[i]+'-'+elements_list[j]
				radial_distribution_function_dense_dict[columns_count][dirname] = {'rdf_rmax': rdf_rmax, 'rdf_rmin': rdf_rmin, 'rdf_value': Binary(pickle.dumps(np.vstack((rdf_new, rdf_smooth)), protocol=2))} # numpy必须转换成二进制才能存进pymongo
				# store cn results into a dict
				rdf_count = rdf.count.copy()
				rdf_count = rdf_count/(len(atomgroup1)*rdf.__dict__['n_frames']) # average
				cn = rdf_count.cumsum() # integrate
				# make curve smoother
				cn_smooth = spline(rdf.bins, cn, rdf_new)
				# get cn_rmin according to first peak valley in rdf
				cn_rmin = cn_smooth[rdf_rmax_index:][rdf_rmin_index]
				coordination_number_dense_dict[columns_count]['pair'] = elements_list[i]+'-'+elements_list[j]
				coordination_number_dense_dict[columns_count][dirname] = {'cn_rmin': cn_rmin, 'cn_value': Binary(pickle.dumps(np.vstack((rdf_new, cn_smooth)), protocol=2))}
				'''

				# 原始的
				# rdf
				radial_distribution_function_dict[columns_count]['pair'] = elements_list[i]+'-'+elements_list[j]
				radial_distribution_function_dict[columns_count][dirname] = Binary(pickle.dumps(np.vstack((rdf.bins, rdf.rdf)), protocol=2))
				# cn
				rdf_count = rdf.count.copy()
				rdf_count = rdf_count/(len(atomgroup1)*rdf.__dict__['n_frames']) # average
				cn = rdf_count.cumsum() # integrate
				coordination_number_dict[columns_count]['pair'] = elements_list[i]+'-'+elements_list[j]
				coordination_number_dict[columns_count][dirname] = Binary(pickle.dumps(np.vstack((rdf.bins, cn)), protocol=2))
		os.system('rm traj.pdb')
		os.chdir(dir_molten_salt_system)

	return radial_distribution_function_dict, coordination_number_dict
Пример #14
0
def test_exclusion(sels):
    # should see two distances with 4 counts each
    s1, s2 = sels
    rdf = InterRDF(s1, s2, exclusion_block=(1, 2)).run()
    assert rdf.count.sum() == 4
Пример #15
0
def test_double_run(sels):
    # running rdf twice should give the same result
    s1, s2 = sels
    rdf = InterRDF(s1, s2).run()
    rdf.run()
    assert len(rdf.count[rdf.count == 4]) == 2
Пример #16
0
def test_count(sels):
    # should see two distances with 4 counts each
    s1, s2 = sels
    rdf = InterRDF(s1, s2).run()
    assert len(rdf.count[rdf.count == 4]) == 2
Пример #17
0
# =============================================================================

print ("\nThe minimum distance between spheres is %s" %np.min(pos_m))


vol_atom = 4/3 *np.pi*(0.5)**3
ff = atoms.n_atoms* vol_atom / geometry.volume

print ("The old ff is %lf" %ff)

# =============================================================================
# Pair correlation
# =============================================================================
from MDAnalysis.analysis.rdf import InterRDF

rdf = InterRDF(atoms,atoms,exclusion_block = (1,1),range = [0,2.5],verbose = True)
rdf.run()

plt.plot(rdf.bins, rdf.rdf)
plt.savefig("gr.pdf")
plt.show()



# =============================================================================
# Converting to real units
# =============================================================================

diameter = 2*real_radius

positions = convert_into_real(positions,diameter,1)
Пример #18
0
def test_nbins(u):
    s1 = u.atoms[:3]
    s2 = u.atoms[3:]
    rdf = InterRDF(s1, s2, nbins=412).run()

    assert len(rdf.bins) == 412
Пример #19
0
# Example #2: Write the configuration to a PDB file        "
# ========================================================="
#

u.atoms.write("system.pdb")
print("===> The initial configuration has been written to system.pdb ")

#
# ========================================================="
# Example #3: Calculate a radial distribution function     "
# ========================================================="
#
from MDAnalysis.analysis.rdf import InterRDF

charged = u.select_atoms("prop charge  > 0")
rdf = InterRDF(charged, charged, nbins=7, range=(0, 10))

# This runs so far only over the single frame we have loaded.
# Multiframe averaging must be done by hand
rdf.run()

#
# ========================================================="
# Example #4: Saving frames to a GROMACS's TRR trajectory
# ========================================================="
#

from MDAnalysis.coordinates.TRR import TRRWriter

W = TRRWriter("traj.trr", n_atoms=len(system.part))
Пример #20
0
def test_count_sum(sels):
    # OW vs HW
    # should see 8 comparisons in count
    s1, s2 = sels
    rdf = InterRDF(s1, s2).run()
    assert rdf.count.sum() == 8