|
|
本帖最后由 紫丹渡津 于 2022-12-29 14:29 编辑
随手写了一个Python的脚本,并行化计算结合水和自由水的分数,而且你还可以自由扩展计算结合水的状态,比如与聚合物链结合的数目等。
如何使用,脚本里关键的地方都有注释。
- import multiprocessing as mp
- import functools
- import math
- import MDAnalysis as mda
- import MDAnalysis.analysis.distances as dist
- import MDAnalysis.lib.distances as distances
- import numpy as np
- def assoPrint(filename, data1, data2): # print association properties
- anaout = open(filename + '.xvg', 'w')
- print('# ' + 'condensed' + ' free', file=anaout)
- for i in range(0, len(data1)):
- print('{:10.5f} {:10.5f}'.format(data1[i], data2[i]), file=anaout)
- anaout.close()
- def get_association(top, trj, ag1, ag2, cutoff, frame_id):
- uta = mda.Universe(top, trj)
- group1 = uta.select_atoms("type " + ag1)
- group2 = uta.select_atoms("type " + ag2)
- ts = uta.trajectory[frame_id]
- cell = ts.dimensions
- type1, type2 = [], []
- for atom in range(len(ag1)):
- distpair = distances.capped_distance(ag1.positions[atom], ag2.positions, cutoff, box = cell)[0]
- asso_pair.append(len(distpair))
- if len(distpair) != 0:
- type1.append(atom)
- else:
- type2.append(atom)
- # return the fractions of condensed and free water molecules
- return len(type1) / len(group1), len(type2) / len(group1)
- def main():
- top = "topol.tpr"
- trj = "traj.trr"
- atom_group1 = "AT1" # atom type for the oxygen of water
- atom_group2 = "AT2" # atom type of the given reference on your polymer
- cutoff = 5.0 # cutoff for water - polymer
- nt = 8 # number of processors
- uta = mda.Universe(top, trj)
- # generate the wanted frame_ids
- frame_ids = [ts.frame for ts in uta.trajectory]
- condensed, free = [], []
- do_analyse = functools.partial(get_association, top, trj, atom_group1, atom_group2, cutoff)
- pool = mp.Pool(processes=nt)
- data = pool.imap(do_analyse, frame_ids)
- for f1, f2 in data:
- condensed.append(f1)
- free.append(f2)
- assoPrint('water_fraction', condensed, free)
- if __name__ == "__main__":
- main()
复制代码 |
|