-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSD_g2.R
170 lines (121 loc) · 6.43 KB
/
MSD_g2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
## Write a function to calculate MSD averaged over all molecules for all temperatures.
### First define a function to do the calculation for one temeprature.
MSD_g2_one_temp=function(path="~/Dropbox/lammps/PMMA_big/atom300",filename="atom.300_1.txt",timestep=5001
,num_mol=64,atom_type=1:10,atom_type_mass=c(1.0079,12.011,12.011,12.011,15.9999,15.9999,12.011,12.011,1.0079,12.011)){
# load the timeRecord functions from my github account.
source("https://raw.githubusercontent.com/edwardcooper/mlmodel_select/master/timeRecord_functions.R")
setwd(path)
# record the time
timeRecordB()
# load all library
library(data.table)
library(foreach)
library(dplyr)
library(matrixStats)
library(magrittr)
### Load the data with fread
atom.300.1_fread=fread(input=filename, sep=" ", stringsAsFactors = FALSE, fill=TRUE
#,col.names = c("atom-id","type","mol","xu","yu","zu")
,colClasses = c("numeric","numeric","numeric","numeric","numeric","numeric","character","character")
,na.strings = c("ITEM:","TIMESTEP","NUMBER","OF","ATOMS","BOX","BOUNDS", "pp","id","type","mol","xu","yu","zu")
)
timeRecordB(output_message = "Load in data fread")
# select the non-NA columns
atom.300.1_fread=atom.300.1_fread[,.(V1,V2,V3,V4,V5,V6)]
# clear the rows that have NA values, V6 contains the most NAs.
atom.300.1_fread=atom.300.1_fread[complete.cases(atom.300.1_fread[,V6])]
# set the column names.
colnames(atom.300.1_fread)=c("atom.id","type","mol","xu","yu","zu")
# define the total number of atoms.
tot_atom_num=atom.300.1_fread[,"atom.id"]%>%max()
# minimum mol number
mini_mol_num=atom.300.1_fread[,"mol"]%>%min()
#################################################################################
# Define MSD function
MSD=function(data){
(data$xu-data$xu[1])^2+ (data$yu-data$yu[1])^2+ (data$zu-data$zu[1])^2
}
#define a function for replacement
decode = function(x, search, replace, default = NULL) {
# build a nested ifelse function by recursion
decode.fun <- function(search, replace, default = NULL)
if (length(search) == 0) {
function(x) if (is.null(default)) x else rep(default, length(x))
} else {
function(x) ifelse(x == search[1], replace[1],
decode.fun(tail(search, -1),
tail(replace, -1),
default)(x))
}
return(decode.fun(search, replace, default)(x))
}
#################################################################################
# mutate original data frame with mass variable (7 variables)
timeRecordB()
atom.300.1_fread[,mass:=decode(x = type, search = atom_type
, replace = atom_type_mass)]
timeRecordB(output_message = "add mass variable data.table")
gc()
# mass of molecule
totmass= atom.300.1_fread[1:tot_atom_num,]%>%.[mol==1,.(mass)]%>%sum
# add time variable for easier data handling and safe guarding any unwanted ordering to change the ordering of time in the data.
timeRecordB()
atom.300.1_fread[,time_step:=seq(1,timestep,by=1)%>%rep(times=tot_atom_num)%>%sort()]
timeRecordB(output_message = "add time variable data.table")
########calculate the MSD for all molecules into a matrix with each row a series of MSD changes with time.
## First, calculate the center of mass. Then calculate the MSD for difference molecules.
MSD_g2_matrix=function(data,timestep,num_mol,totmass,mol_min){
MSD_g2_empty_matrix=matrix(NA,ncol=timestep,nrow=num_mol)
###############################################################
# calculate the center of mass for every timestep and every molecule.
center_of_mass=data[,.(xu=sum(xu*mass)/totmass,yu=sum(yu*mass)/totmass,zu=sum(zu*mass)/totmass),by=.(mol,time_step)]
# transform it into a data.table for faster subset.
center_of_mass=center_of_mass%>%as.data.table()
###############################################################
# change the index here if the molecule number starts from 1.
for(j in 1:num_mol){
MSD_g2_empty_matrix[j,]=center_of_mass[mol==(j-1+mol_min),]%>% MSD
}
return(MSD_g2_empty_matrix)
}
timeRecordB()
MSD.matrix=MSD_g2_matrix(atom.300.1_fread,timestep=timestep,num_mol=num_mol ,totmass=totmass,mol_min = mini_mol_num)
# echo the minimum mol number
paste("The minimum mol number:",mini_mol_num)%>%message()
timeRecordB(output_message = "MSD for center of mass")
gc()
# Calculate the averaged MSD over all molecules.
MSD.matrix%>%colMeans()%>%write.table(file="MSD.g2.colmean.1.txt", sep=",")
## Add NGP calculation here.
timeRecordB()
NGP.COM=(0.6)*colMeans(MSD.matrix^2)/(colMeans(MSD.matrix))^2-1
NGP.COM%>%write.table(file="NGP.g2.1.txt", sep=",")
timeRecordB(output_message = "NGP for center of mass")
return( timeRecordR(ignore=0.1)%>%filter(output_message!="None")%>%select(output_message,run_time) )
}
# test usage()
# MSD_g2_one_temp(path="~/Dropbox/lammps/PMMA_big/atom300",filename="atom.300_1.txt",timestep=5001)
## echo the current calculation and percentage of entire calculation.
MSD_g2=function(Path="~/Dropbox/lammps/",polymer="PMMA_big",temperatures=seq(300,600,by=20),timestep=5001
,num_mol=64,atom_type=1:10,atom_type_mass=c(1.0079,12.011,12.011,12.011,15.9999,15.9999,12.011,12.011,1.0079,12.011)){
library(magrittr)
# the loop to calculate the same thing in all temepratures defined above.
for (i in seq_along(temperatures)){
# echo beginning of calculation
paste("Begin calculation of temperature:",temperatures[i],sep="")%>%message
# set correct path for the data file
path=paste(Path,"/", polymer,"/atom",temperatures[i], sep='')
# find the correct file to read and calculate.
filename=paste("atom.",temperatures[i],"_1.txt",sep="")
# calculation for MSD
MSD_g2_one_temp(path=path,filename =filename ,timestep=timestep
,num_mol=num_mol,atom_type=atom_type,atom_type_mass=atom_type_mass)
# echo end of calculation
paste("End calculation of temperature:",temperatures[i],sep="")%>%message
paste(i,"/",length(temperatures))%>%message
gc()
}
return( timeRecordR(ignore=0.1)%>%filter(output_message!="None")%>%select(output_message,run_time) )
}
# example use
# MSD_g2()