【转帖】径向分布函数程序与简单说明 (小木虫)

径向分布函数g(r)代表了球壳内的平均数密度

为离中心分子距离为r,体积为 的球壳内的瞬时分子数。
具体参见李如生,《平衡和非平衡统计力学》科学出版社:1995

CODE:

SUBROUTINE GR(NSWITCH)
      IMPLICIT DOUBLE PRECISION(A-H,O-Z)
      PARAMETER(NM=40000,PI=3.141592653589793D0,NHIS=100)
      COMMON/LCS/X0(3,-2:2*NM),X(3,-2:2*NM,5),XIN(3,-2:2*NM),
     $XX0(3,-2:2*NM),XX(3,-2:2*NM,5),XXIN(3,-2:2*NM)
      COMMON/MOLEC/LPBC(3),MOLSP,MOLSA,NBX,NBY,NBZ,NPLA,LPBCSM,NC,NN,MC
      COMMON/WALLS/HI(3,3),G(3,3),DH,AREA,VOLUME,SCM(3)
      COMMON/PBCS/HALF,PBCX,PBCY,PBCZ
        COMMON/GR_VAR/ NGR
        DIMENSION H(3,3),GG(0:NHIS),R(0:NHIS)
      EQUIVALENCE(X0(1,-2),H(1,1))
C   *****************************************************************
C      如何确定分子数密度:DEN_IDEAL 
C      取分子总数作为模拟盒中的数密度,可保证采样分子总数=总分子数?
C====================================================================
C         N1=MOLSP+1
C      N2=MOLSP+NC

DEN_IDEAL=MOLSP

G11=G(1,1)
      G22=G(2,2)
      G33=G(3,3)
      G12D=G(1,2)+G(2,1)
      G13D=G(1,3)+G(3,1)
      G23D=G(2,3)+G(3,2)

IF(NSWITCH.EQ.0)THEN
          NGR=0
          DELR=HALF/NHIS
          DO I=1,NHIS
           GG(I)=0.D0 
           R(I)=0.D0 
          ENDDO

ELSE IF(NSWITCH.EQ.1)THEN
         NGR=NGR+1
       DO I=1,MOLSP-1
         DO J=I+1,MOLSP
C====================================================================
C     USE PBC IN X DIRECTION:  SUITABLE FOR PBCX=1
C                              NOT GREAT PROBLEM FOR PBCX=0 
C                              (THIS TIME USUALLY |DELTA X| < HALF)
C====================================================================
          XIJ=X0(1,I)-X0(1,J)
        IF(XIJ.GT.+HALF)XIJ=XIJ-PBCX
        IF(XIJ.LT.-HALF)XIJ=XIJ+PBCX
        YIJ=X0(2,I)-X0(2,J)
        IF(YIJ.GT.+HALF)YIJ=YIJ-PBCY
        IF(YIJ.LT.-HALF)YIJ=YIJ+PBCY
        ZIJ=X0(3,I)-X0(3,J)
        IF(ZIJ.GT.+HALF)ZIJ=ZIJ-PBCZ
        IF(ZIJ.LT.-HALF)ZIJ=ZIJ+PBCZ
        RSQ=XIJ*(G11*XIJ+G12D*YIJ+G13D*ZIJ)+
     $      YIJ*(G22*YIJ+G23D*ZIJ)+G33*ZIJ*ZIJ
          RRR=SQRT(RSQ)
          RRR=RRR/H(1,1)
C====================================================================
C      以上用数组G和H的结果与下同
C      RRR=SQRT(XIJ**2+YIJ**2+ZIJ**2)
C      G11=H(1,1)**2
C====================================================================
          IF(RRR.LT.HALF)THEN
           IG=INT(RRR/DELR)
           GG(IG)=GG(IG)+2
          ENDIF
       ENDDO
         ENDDO

ELSE IF(NSWITCH.EQ.2)THEN
        DO I=1,NHIS
           R(I)=DELR*(I+0.5D0)
        ENDDO
        DO I=1,NHIS
           VB=(4.D0/3.D0)*PI*(((I+1)**3-I**3)*(DELR**3))
           GNID=VB*DEN_IDEAL
           GG(I)=GG(I)/(NGR*MOLSP*GNID)
        ENDDO
        OPEN(UNIT=31,FILE="GR.DAT")
        DO I=1,NHIS
           WRITE(31,*)R(I),GG(I)
        ENDDO
        CLOSE(31)

ENDIF

RETURN 
        END

这样的代码看着不够明了。。。。。。

伪代码:

for (int i = 0; i < TOTN - 1; ++i)
  for (int j = i + 1; j < TOTN; ++j) {
    double dij = sqrt( pow(Pos[0]-Pos[j][0], 2) + pow(Pos[1]-Pos[j][1], 2) + pow(Pos[2]-Pos[j][2], 2));
    int kbin = func(dij); // dij所对应的bin的序号
    g(kbin) += 2;
  }
  // normalize
  for (int k = 0; k < NBIN; ++k)
    g(k) /= 4.0 * PI * r(k) * r(k) * dr * RHO; // r 为第k个bin所对应的距离值

calculate radial distribution function in molecular dynamics (转载科学网樊哲勇

Here are the computer codes for this article:

md_rdf.cpp

find_rdf.m

test_rdf.m

 

         Calculating radial distribution function in molecular dynamics

First I recommend a very good book on molecular dynamics (MD) simulation: the book entitled "Molecular dynamics simulation: Elementary methods" by J. M. Haile. I read this book 7 years ago when I started to learn MD simulation, and recently I enjoyed a second reading of this fantastic book. If a beginner askes me which book he/she should read about MD, I will only recommend this. This is THE BEST introductory book on MD. It tells you what is model, what is simulation, what is MD simulation, and what is the correct attitude for doing MD simulations.

In my last blog article, I have presented a Matlab code for calculating velocity autocorrelation function (VACF) and phonon density of states (PDOS) from saved velocity data. In this article, I will present a Matlab code for calculating the radial distribution function (RDF) from saved position data. The relevant definition and algorithm are nicely presented in Section 6.4 and Appendix A of Haile‘s book. Here I only present a C code for doing MD simulation and a Matlab code for calculating and plotting the RDF. We aim to reproduce Fig. 6.22 in Haile‘s book!

Step 1.

Use the C code provided above to do an MD simulation. Note that I have used a different unit systems than that used in Haile‘s book (he used the LJ unit system). This code only takes 14 seconds to run in my desktop. Here are my position data (there are 100 frames and each frame has 256 atoms):

r.txt

Step 2.

Write a Matlab function which can calculate the RDF for one frame of positions:

function [g] = find_rdf(r, L, pbc, Ng, rc)

% determine some parameters

N = size(r, 1);         % number of particles

L_times_pbc = L .* pbc; % deal with boundary conditions

rho = N / prod(L);      % global particle density

dr = rc / Ng;           % bin size

% accumulate

g = zeros(Ng, 1);

for n1 = 1 : (N - 1)                               % sum over the atoms

for n2 = (n1 + 1) : N                          % skipping half of the pairs

r12 = r(n2, :) - r(n1, :);                  % position difference vector

r12 = r12 - round(r12 ./L ) .* L_times_pbc; % minimum image convention

d12 = sqrt(sum(r12 .* r12));                % distance

if d12 < rc                                 % there is a cutoff

index = ceil(d12 / dr);                % bin index

g(index) = g(index) + 1;                % accumulate

end

end

end

% normalize

for n = 1 : Ng

g(n) = g(n) / N * 2;           % 2 because half of the pairs have been skipped

dV = 4 * pi * (dr * n)^2 * dr; % volume of a spherical shell

g(n) = g(n) / dV;              % now g is the local density

g(n) = g(n) / rho;             % now g is the RDF

end

Step 3.

Write a Matlab script to load the position data, call the function above, and plot the results:

clear; close all;

load r.txt; % length in units of Angstrom

% parameters from MD simulation

N = 256;                % number of particles

L = 5.60 * [4, 4, 4]; % box size

pbc = [1, 1, 1];        % boundary conditions

% number of bins (number of data points in the figure below)

Ng = 100;

% parameters determined automatically

rc = min(L) / 2;     % the maximum radius

dr = rc / Ng;        % bin size

Ns = size(r, 1) / N; % number of frames

% do the calculations

g = zeros(Ng, 1); % The RDF to be calculated

for n = 1 : Ns

r1 = r(((n - 1) * N + 1) : (n * N), :); % positions in one frame

g = g + find_rdf(r1, L, pbc, Ng, rc);    % sum over frames

end

g = g / Ns;                                 % time average in MD

% plot the data

r = (1 : Ng) * dr / 3.405;

figure;

plot(r, g, ‘o-‘);

xlim([0, 3.5]);

ylim([0, 3.5]);

xlabel(‘r^{\ast}‘, ‘fontsize‘, 15)

ylabel(‘g(r)‘, ‘fontsize‘, 15)

set(gca, ‘fontsize‘, 15);

Here is the figure I obtained:

Does it resemble Fig. 6. 22 in Haile‘s book?

原文地址:https://www.cnblogs.com/Simulation-Campus/p/8994935.html

时间: 2024-07-30 08:53:28

【转帖】径向分布函数程序与简单说明 (小木虫)的相关文章

信息安全之程序实现简单替换加密,并用字母频率统计进行破解

1程序实现简单密码替换 首先我们找一篇英文文章 然后写程序简单替换,这里我们使用移位替换a移3位替换成d(key表示移位数) 读入文件函数 测试加密System.out.println(encode(readfile("2.txt"),3)); 加密前 加密后 然后我们来破解 我们知道英文中出现频率最高字母的是e字母,我们先测试下: 测试代码: 主函数输出:System.out.println(find(readfile("2.txt"))); 结果果然是e 现在我

输出多行字符的一个简单JAVA小程序

1 public class JAVA 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("----------------------"); 6 System.out.println("|| 我要学会 ||"); 7 System.out.println("|| JAVA语言 ||"); 8 System.out.println("-------

GDB 程序调试简单实践

用了好久的GCC/G++ 却一直都没用过GDB调试过程序,有时程序不是很大,一般有错,直接看编译器编译结果就差不多知道错在哪儿了,或者使用codeblocks单步调试,甚至回到windows下面调试,但是总是不太方便,因此有必要看一下GDB调试方法和基本步骤. 下面是一个简单的演示: 首先创建一个有错误的代码,如下: 这个程序很简单,目的是接受用户的输入,并将用户的输入回应输出来. 但是这个程序的第17行有个错误,使用了未初始化的字符指针name,因此编译运行后会出现段错误,如下: 下面利用GD

运维程序】简单的命令控制器(支持定时命令执行、重复定时任务命令和进程管理,开发这个小程序主要是为了方便管理服务进程)【个人github项目】

一.前言: command-controller 一个运维程序,简单的命令控制器(支持定时命令执行和重复定时命令,开发这个程序主要是为了方便管理服务进程) 本来是要用python做的,但是之前做ffmpeg的时候已经写了一部分Java的命令控制功能了,有些代码就拿过来改改用了(其实是为了偷懒qaq) 二.实现功能 1.进程管理 只支持本程序启动的进程管理,本程序主要功能是定时执行某些脚本或者系统命令,当然命令行和脚本是很自由的,更多用法请自行探索 2.定时命令.任务 3.重复定时命令.任务 4.

程序开发的一点小总结

程序开发的一点小总结, 给要学习一门新语言的朋友一些帮助, :P 1.多项条件下的处理 第一种方法: 每个需要执行A函数的条件下都写一边A函数调用, 这种方式也是最中规中矩的写法, 代码相对臃肿, 如果A有任何变动, 就要修改多处, 这种代码块写多了, 容易漏掉 if(b==1) a() else if(b==2) else if(b==3) a() else a() 第二种写法: 在B条件筛选前, 创建一个临变量布尔c, 用来监控需要A函数需要的条件, 需要就为true, 不需要就不写(默认初

Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念

一.开发前的准备 两个开发包spring-framework-3.1.1.RELEASE-with-docs.zip和commons-logging-1.2-bin.zip,将它们解压,然后把Spring开发包下dist目录的所有包和commons-logging包下的commons-logging-1.1.1.jar复制到名为Spring3.1.1的文件夹下.那么Spring开发所需要的包就组织好了. 二.建立项目,导入包 在项目节点上右键,Build Path/ADD Libraries/U

如何在Android实现桌面清理内存简单Widget小控件

如何在Android实现桌面清理内存简单Widget小控件 我们经常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一键清理内存,杀死后台进程的功能,那么这个功能是如何实现的呢,我们今天也来尝试做一个类似的功能的小控件. 效果图: 一.UI部分的编写: 参照Google的文档,首先在建立一个类继承AppWidgetProvider import android.appwidget.AppWidgetProvider

微信小程序视图层WXML_小程序条件渲染

微信小程序视图层WXML_小程序条件渲染 wx:if 在微信小程序的框架中,我们用wx:if="{{condition}}"来判断微信小程序页面是否需要渲染该代码块: <view wx:if="{{condition}}"> True </view> 也可以用wx:elif和wx:else来添加一个else块: <view wx:if="{{length > 5}}"> 1 </view> &

三周学会小程序第一讲:小程序申请和注意事项

注册 注册邮箱 个人申请小程序非常简单,首先你需要注册一个全新的邮箱. 当然用你的个人邮箱也可以,小编考虑到后面你可以再次开发自己的小程序,所以这里还是重新申请一个比较好.网易邮件一个手机号可以申请15个邮箱,是一个不错的选择 注册小程序 进入 https://mp.weixin.qq.com/ 页面,直接点击注册,一直下一步就好了.注意的是需要想好名字和头像.因为有如下限制. 名称提交前可修改2次名称,一年可以修改两次. 头像一个月可以修改5次. 这样就大功告成了,是不是很简单? 注意事项 个