-
Notifications
You must be signed in to change notification settings - Fork 0
/
Presentation.Rmd
715 lines (449 loc) · 21.2 KB
/
Presentation.Rmd
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
---
title: 'Predict glass transition from short time simulation '
author: "Zijun Lu"
output:
html_document: default
---
## recap on what we are trying to achieve
We are tryng to use a **short time (~14ps) simulation** to get the glass transition temperature compatible with long time simulation.
```{r,echo=FALSE,include=FALSE}
library(foreach)
library(dplyr)
Data.import=function(Path="~/Dropbox/lammps" , filename = "MSD.colmean.matrix.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA"){
# read in data into a matrix
MSDcol=foreach(i=1:length(temp),.combine = cbind)%do% {
# set correct path for the data file
path=paste(Path,"/", polymer,"/atom",temp[i], sep='')
path
setwd(path)
# read in the data
MSDcol=read.table(file=filename,header=TRUE,sep=',')
library(magrittr)
MSDcol%>%dim
MSDcol=MSDcol[[1]]
return(MSDcol)
}
# add a time variable
time=1:dim(MSDcol)[1]
# change data into data frame
MSDcol=cbind(time,MSDcol)%>%as.data.frame()
}
```
## MSD g0 plot for PS
```{r,echo=FALSE}
#Run (optimizaed data import function.R) file first
# temp=c(200,250,300,350,400,450,500,550,600)
# import g0 MSD colmean data for PS
########################################################
library(dplyr)
MSD.PS.g0=Data.import(filename="MSD.colmean.matrix.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS")
MSD.PS.g0=MSD.PS.g0%>%as.data.frame()
# add correct colname
temp=c(200,250,300,350,400,450,500,550,600)
colnames(MSD.PS.g0)=c("time",paste("T",temp,sep=""))
#regular plot
###########################################
library(reshape2)
MSD.PS.g0.melt=melt(MSD.PS.g0,id.vars="time")
library(dplyr)
library(ggplot2)
ggplot(data=MSD.PS.g0.melt,aes(x=time,y=value,colour=variable)) +geom_point(size=0.5)+
ylab("MSD")+
ggtitle("PS g0 MSD vs time plot")
# log-log plot
###########################################
library(reshape2)
MSD.PS.g0.melt=melt(MSD.PS.g0,id.vars="time")
library(ggplot2)
ggplot(data=MSD.PS.g0.melt,aes(x=log(time),y=log(value),colour=variable)) +geom_point(size=0.5)+
ylab("log(MSD)")+
ggtitle("PS g0 MSD vs time log-log plot")
```
## MSD g0 plot for PMMA
```{r}
library(dplyr)
MSD.PMMA.g0=Data.import(filename="MSD.colmean.matrix.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA")
MSD.PMMA.g0=MSD.PMMA.g0%>%as.data.frame()
# add correct colname
temp=c(320,345,370,395,420,445,470,500,550,600)
colnames(MSD.PMMA.g0)=c("time",paste("T",temp,sep=""))
#regular plot
###########################################
library(reshape2)
MSD.PMMA.g0.melt=melt(MSD.PMMA.g0,id.vars="time")
library(dplyr)
library(ggplot2)
ggplot(data=MSD.PMMA.g0.melt,aes(x=time,y=value,colour=variable)) +geom_point(size=0.5)+
ylab("MSD")+
ggtitle("PMMA g0 MSD vs time plot")
# log-log plot
###########################################
library(reshape2)
MSD.PMMA.g0.melt=melt(MSD.PMMA.g0,id.vars="time")
library(ggplot2)
ggplot(data=MSD.PMMA.g0.melt,aes(x=log(time),y=log(value),colour=variable)) +geom_point(size=0.5)+
ylab("log(MSD)")+
ggtitle("PMMA g0 MSD vs time log-log plot")
```
## MSD g0 plot for PIB
```{r}
library(dplyr)
MSD.PIB.g0=Data.import(filename="MSD.colmean.matrix.1.txt", temp=c(150,175,200,225,250,275,300,350,400),polymer="PIB")
MSD.PIB.g0=MSD.PIB.g0%>%as.data.frame()
# add correct colname
temp=c(150,175,200,225,250,275,300,350,400)
colnames(MSD.PIB.g0)=c("time",paste("T",temp,sep=""))
#regular plot
###########################################
library(reshape2)
MSD.PIB.g0.melt=melt(MSD.PIB.g0,id.vars="time")
library(dplyr)
library(ggplot2)
ggplot(data=MSD.PIB.g0.melt,aes(x=time,y=value,colour=variable)) +geom_point(size=0.5)+
ylab("MSD")+
ggtitle("PIB g0 MSD vs time plot")
# log-log plot
###########################################
library(reshape2)
MSD.PIB.g0.melt=melt(MSD.PIB.g0,id.vars="time")
library(ggplot2)
ggplot(data=MSD.PIB.g0.melt,aes(x=log(time),y=log(value),colour=variable)) +geom_point(size=0.5)+
ylab("log(MSD)")+
ggtitle("PIB g0 MSD vs time log-log plot")
```
## MSD g2 plot for PS
```{r,echo=FALSE}
#Run (optimizaed data import function.R) file first
# temp=c(200,250,300,350,400,450,500,550,600)
# import g2 MSD colmean data for PS
########################################################
library(dplyr)
MSD.PS.g2=Data.import(filename="MSD.COM.colmean.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS")
MSD.PS.g2=MSD.PS.g2%>%as.data.frame()
# add correct colname
temp=c(200,250,300,350,400,450,500,550,600)
colnames(MSD.PS.g2)=c("time",paste("T",temp,sep=""))
#regular plot
###########################################
library(reshape2)
MSD.PS.g2.melt=melt(MSD.PS.g2,id.vars="time")
library(dplyr)
library(ggplot2)
ggplot(data=MSD.PS.g2.melt,aes(x=time,y=value,colour=variable)) +geom_point(size=0.5)+
ylab("MSD")+
ggtitle("PS g2 MSD vs time plot")
# log-log plot
###########################################
library(reshape2)
MSD.PS.g2.melt=melt(MSD.PS.g2,id.vars="time")
library(ggplot2)
ggplot(data=MSD.PS.g2.melt,aes(x=log(time),y=log(value),colour=variable)) +geom_point(size=0.5)+
ylab("log(MSD)")+
ggtitle("PS g2 MSD vs time log-log plot")
```
## MSD g2 plot for PMMA
```{r}
library(dplyr)
MSD.PMMA.g2=Data.import(filename="MSD.COM.colmean.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA")
MSD.PMMA.g2=MSD.PMMA.g2%>%as.data.frame()
# add correct colname
temp=c(320,345,370,395,420,445,470,500,550,600)
colnames(MSD.PMMA.g2)=c("time",paste("T",temp,sep=""))
#regular plot
###########################################
library(reshape2)
MSD.PMMA.g2.melt=melt(MSD.PMMA.g2,id.vars="time")
library(dplyr)
library(ggplot2)
ggplot(data=MSD.PMMA.g2.melt,aes(x=time,y=value,colour=variable)) +geom_point(size=0.5)+
ylab("MSD")+
ggtitle("PMMA g2 MSD vs time plot")
# log-log plot
###########################################
library(reshape2)
MSD.PMMA.g2.melt=melt(MSD.PMMA.g2,id.vars="time")
library(ggplot2)
ggplot(data=MSD.PMMA.g2.melt,aes(x=log(time),y=log(value),colour=variable)) +geom_point(size=0.5)+
ylab("log(MSD)")+
ggtitle("PMMA g2 MSD vs time log-log plot")
```
## MSD g2 plot for PIB
Need to calculate the g2 MSD
## Method 1: Taylor expansion of MSD with Brownian motion approximation $\langle x^2(t) \rangle =2Dt$ .
Let us first define the velocity auto-correlation function for a stationary process **x(t)**
$$
C_{\upsilon}(t_2,t_1)=<\upsilon(t_2),\upsilon(t_1)>=\int\int d\upsilon_1d\upsilon_2P(\upsilon_2,t_2|\upsilon_1,t_1) W(\upsilon_1,t_1)\upsilon_1\upsilon_2
$$
$P(\upsilon_2,t_1|\upsilon_1,t_1)$ is the transition probability density from velocity $\upsilon_1$ at $t_1$ to velocity $\upsilon_2$ at $t_2$.
$W(\upsilon_1,t_1)$ is the probability density finding particle with velocity $\upsilon_1$ at $t_1$
### Green Kubo relation
Thus, the stochastic process x(t) has constant mean and constant variance, which means that $C_{\upsilon}(t_2,t_1)=C_{\upsilon}(t_2-t_1,0)=C_{\upsilon}(\tau,0)$
Recall that $$<x(t)x(t)>=\int^t_0 \int^t_0 dt_1dt_2 \times<\upsilon(t_2),\upsilon(t_1)>=\int^t_0\int^t_0 dt_1dt_2 C_{\upsilon}(t_2,t_1)$$
Thus, $$D=\frac{<x^2(t)> }{2t} =\int^t_0 C_{\upsilon}(\tau,0)d\tau$$
Taylor expansion of $C_{\upsilon}(t_2,t_1)$ is
\begin{align*}
C_{\upsilon}(\tau+t,t)&=<\upsilon(\tau+t),\upsilon(t)>=\sum\limits_{n=0}^{\infty} \frac{\tau^{2n}}{2n!} <\upsilon^{(2n)}(t),\upsilon(t)>\\
&\textit{Since that} <\upsilon^{(2)}(t),\upsilon(t)>=-<\upsilon^{(1)}(t),\upsilon^{(1)}(t)>\\
&=\sum\limits_{n=0}^{\infty}(-1)^{2n} \frac{\tau^{2n}}{2n!} <\upsilon^{(n)}(t),\upsilon^{(n)}(t)>\\
&=(<\upsilon(t),\upsilon(t)>-\frac{1}{2}\alpha \tau^2+\frac{1}{4}\beta \tau^4+O(\tau^6))
\end{align*}
where $\alpha=<\upsilon^{(1)}(t),\upsilon^{(1)}(t)>$.
### Expression for alpha
\begin{align*}
<x^2(t)>=&2t\int^t_0 C_{\upsilon}(\tau,0)d\tau\\
&=2t\int^t_0d\tau (<\upsilon(t),\upsilon(t)>-\frac{1}{2}\alpha \tau^2+O(\tau^4))\\
&\textit{Since that }<\upsilon(t),\upsilon(t)>=\frac{3k_BT}{m}\\
&=2t^2(\frac{3k_BT}{m}-\frac{1}{6}\alpha t^2 +O(t^5))
\end{align*}
**In actual calculation, Solomon's method is to use linear regression with all even orders on MSD and extract the coefficient for t^4.**
## What if MSD~$t^{0.5}$ ?
\begin{align*}
<x^2(t)>=&2t^{0.5}\int^t_0 C_{\upsilon}(\tau,0)d\tau\\
&=2t^{0.5}\int^t_0d\tau (<\upsilon(t),\upsilon(t)>-\frac{1}{2}\alpha \tau^2+O(\tau^4))\\
&\textit{Since that }<\upsilon(t),\upsilon(t)>=\frac{3k_BT}{m}\\
&=2t^{1.5}(\frac{3k_BT}{m}-\frac{1}{6}\alpha t^2 +O(t^5))
\end{align*}
\begin{align*}
\frac{d^2}{dt^2}[\frac{<x^2(t)>}{t^{1.5}}]\simeq-&\frac{1}{3}\alpha=-\frac{1}{3}<\upsilon^{(1)}(t),\upsilon^{(1)}(t)>\\&=-\frac{1}{3m^2}<F(t),F(t)>
\end{align*}
If this is the case, the regression should be done
## alpha term : MSD by averaging over all atoms for PMMA, PS and PIB.
First, change the units of time and distance to ps and nm. The original timestep is 10fs, and distance unit is angstrom. So I achieve it by dividing MSD by 100 and (1:tot_num_of_timestep)*0.01.
Second, the linear regression is done as $MSD/t^2=c_0+c_1*t^2+c_2*t^4+...+c_5*t^{10}$. Then the alpha term (curvature of MSD) is extracted from the simulation by extracting $c_2$ from the linear regression coeeficients.
```{r,echo=FALSE}
# all even terms and MSD increase linearly with time.
###############################################################################################################
#Run ( data_import.R) file first
# write a function to plot the alpha term for different polymers and different MSD calculation method.
#Run (optimizaed data import function.R) file first
alpha.even.ngp.plot=function(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS",method="linear"){
# import colmean data and do linear regression for alpha term
##############################################################
MSD.PS.g0=Data.import(filename=filename1, temp=temp,polymer=polymer)
MSD.PS.g0=MSD.PS.g0%>%as.data.frame()
# Regression method 1 : using all even order and no intercept
################################################################
# change the time units to ps
time=MSD.PS.g0[,1]*0.01
# change the length unit to nm.
MSD.PS.g0=MSD.PS.g0/100
library(foreach)
############################################################################
if(method=="linear"){
#MSD is increasing linearly with time.
alpha=foreach(i=2:(dim(MSD.PS.g0)[2]),.combine = rbind)%do%{
(lm(MSD.PS.g0[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20))%>%confint(level=0.95))[2,]
}
Rsquared=foreach(i=2:(dim(MSD.PS.g0)[2]),.combine = rbind)%do%{
c( ( lm(MSD.PS.g0[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20)) )%>%summary%>%.$adj.rsquared,
( lm(MSD.PS.g0[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20)) )%>%summary%>%.$r.squared
)
}
}else if(method=="frac"){
#MSD is increasing linearly with fractional power of time.
alpha=foreach(i=2:(dim(MSD.PS.g0)[2]),.combine = rbind)%do%{
(lm(MSD.PS.g0[,i]~-1+I(time^(3/2))+I(time^(7/2))+I(time^(11/2))+I(time^(15/2))+I(time^(19/2)))%>%confint(level=0.95))[2,]
}
Rsquared=foreach(i=2:(dim(MSD.PS.g0)[2]),.combine = rbind)%do%{
c(
(
lm(MSD.PS.g0[,i]~-1+I(time^(3/2))+I(time^(7/2))+I(time^(11/2))+I(time^(15/2))+I(time^(19/2)) )
)%>%summary%>%.$adj.rsquared,
(
lm(MSD.PS.g0[,i]~-1+I(time^(3/2))+I(time^(7/2))+I(time^(11/2))+I(time^(15/2))+I(time^(19/2)) )
) %>%summary%>%.$r.squared
)
}
}else{
print("Enter a valid method like linear or frac.")
}
###############################################################
#first transform the matrix then turn it into a vector
alpha=alpha%>%t%>%as.vector
# combine alpha and temp into a data frame
temp=rep(temp,each=2)
alpha=data.frame(alpha,temp,Rsquared,row.names = NULL)
# calculate the average between two alpha values
alpha_average=alpha%>%group_by(temp) %>% summarize(alpha_average=mean(alpha))
alpha_average=rbind(alpha_average,alpha_average)%>%arrange(temp)
alpha=cbind(alpha,alpha_average["alpha_average"])
# unscaled plot for two 95% confidence interval and average of the end points of the range
library(ggplot2)
p1=ggplot(data=alpha, aes(y=alpha/temp,x=temp))+geom_point()+geom_line(aes(x=temp,y=alpha_average/temp),color="red")+
#geom_smooth(method= "loess")+
xlab("Temperature")+ylab("Curvature of MSD/Temperature ")
p3=ggplot(data=alpha, aes(y=Rsquared,x=temp))+geom_point(color="orange")+
xlab("Temperature")+ylab("R^2 and adj R^2")
####################################################################################################################
#NGP
NGP=Data.import(filename=filename2, temp=temp,polymer=polymer)
temp=temp
# add column names
colnames(NGP)=c("time",paste("T",temp,sep=""))
# delete NA values
NGP=NGP[complete.cases(NGP),]
NGP=NGP%>%colMeans()
NGP=NGP[-1]
NGP=data.frame(NGP,temp)
library(ggplot2)
p2=ggplot(data=NGP, aes(y=NGP,x=temp))+geom_line()+
xlab("Temperature")+ylab("NGP value")
## Plot NGP and alpha term together
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2), ggplotGrob(p3), size = "first"))
#return the plot.
}
## End of function
```
## PS
### PS g0 alpha term all even orders (t^2 to t^20)
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS",method="linear")
```
### PS g0 alpha term fractional orders (t^(3/2) to t^(19/2))
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS",method="frac")
```
## PMMA
### PMMA g0 alpha term even orders (t^2 to t^20)
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA")
```
### PMMA g0 alpha term fractional orders (t^(3/2) to t^(19/2))
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA",method="frac")
```
## PIB
### PIB g0 alpha term even orders (t^2 to t^20)
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(150,175,200,225,250,275,300,350,400),polymer="PIB")
```
### PIB g0 alpha term fractional orders (t^(3/2) to t^(19/2))
```{r}
alpha.even.ngp.plot(filename1="MSD.colmean.matrix.1.txt",filename2="NGP.1.txt", temp=c(150,175,200,225,250,275,300,350,400),polymer="PIB",method = "frac")
```
## PS
### PS g2 alpha term all even orders (t^2 to t^20)
```{r}
alpha.even.ngp.plot(filename1="MSD.COM.colmean.1.txt",filename2="NGP.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS")
```
### PS g2 alpha term all fractional orders
```{r}
alpha.even.ngp.plot(filename1="MSD.COM.colmean.1.txt",filename2="NGP.1.txt", temp=c(200,250,300,350,400,450,500,550,600),polymer="PS",method="frac")
```
## PMMA
### PMMA g2 alpha term even orders (t^2 to t^20)
```{r}
alpha.even.ngp.plot(filename1="MSD.COM.colmean.1.txt",filename2="NGP.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA")
```
### PMMA g2 alpha term fractional orders
```{r}
alpha.even.ngp.plot(filename1="MSD.COM.colmean.1.txt",filename2="NGP.1.txt", temp=c(320,345,370,395,420,445,470,500,550,600),polymer="PMMA",method="frac")
```
## PIB
### PIB g2 alpha term even orders (t^2 to t^20)
Need to calculate.
## alpha term : MSD by averaging over same atom type
## PS
### atom type all even orders+Non_gaussian parameter (t^2 to t^20)
```{r,echo=FALSE}
#Run (optimizaed data import function.R) file first
# write a function to import data, analyze and print out the graph.
########################################################################################################################
atom.type.plot=function(temp, polymer, type){
filename1=paste("atom.type.",type,".MSD.colmean.txt",sep="")
filename2=paste("atom.type.",type,".NGP.txt",sep="")
# alpha term
#############################################################################
# import MSD colmean data
########################################################
MSD=Data.import(filename=filename1, temp=temp,polymer=polymer)
MSD=MSD%>%as.data.frame()
# Regression method 1 : using all even order and no intercept
########################################################
time=MSD[,1]*0.01
MSD=MSD/100
library(foreach)
alpha=foreach(i=2:(dim(MSD)[2]),.combine = rbind)%do%{
(lm(MSD[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20))%>%confint(level=0.95))[2,]
}
Rsquared=foreach(i=2:(dim(MSD)[2]),.combine = rbind)%do%{
c( (
lm(MSD[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)
+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20)
))%>%summary%>%.$adj.rsquared,
(
lm(MSD[,i]/time^2~I(time^2)+I(time^4)+I(time^6)+I(time^8)+I(time^10)
+I(time^12)+I(time^14)+I(time^16)+I(time^18)+I(time^20)
))%>%summary%>%.$r.squared
)
}
#first transform the matrix then turn it into a vector
alpha=alpha%>%t%>%as.vector
# combine alpha and temp into a data frame
temp=rep(temp,each=2)
alpha=data.frame(alpha,temp,Rsquared,row.names = NULL)
# calculate the average between two alpha values
alpha_average=alpha%>%group_by(temp) %>% summarize(alpha_average=mean(alpha))
alpha_average=rbind(alpha_average,alpha_average)%>%arrange(temp)
alpha=cbind(alpha,alpha_average["alpha_average"])
alpha
#########################################################################################
alpha.title1=paste("alpha term for", polymer, "atom type", type, sep=" ")
# unscaled plot
library(ggplot2)
p1=ggplot(data=alpha, aes(y=alpha/temp,x=temp))+geom_point()+geom_line(aes(x=temp,y=alpha_average/temp))+
xlab("Temperature")+ylab("Curvature of MSD/Temperature ")+ggtitle(alpha.title1)
p3=ggplot(data=alpha, aes(y=Rsquared,x=temp))+geom_point(color="orange")+
xlab("Temperature")+ylab("R^2 and adj R^2")
#NGP
##################################################################################################
#NGP
NGP=Data.import(filename=filename2, temp=temp,polymer=polymer)
temp=temp
# add column names
colnames(NGP)=c("time",paste("T",temp,sep=""))
# delete NA values
NGP=NGP[complete.cases(NGP),]
NGP=NGP%>%colMeans()
NGP=NGP[-1]
NGP
NGP.title=paste("NGP for", polymer, "atom type", type,sep=" ")
NGP=data.frame(NGP,temp)
library(ggplot2)
p2=ggplot(data=NGP, aes(y=NGP,x=temp))+geom_line()+
xlab("Temperature")+ylab("NGP value")+
ggtitle(NGP.title)
## Plot NGP and alpha term together
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(p1), ggplotGrob(p2),ggplotGrob(p3), size = "first"))
}
##########################################################################################################
#PMMA
##########################################################################################################
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=1)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=2)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=3)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=4)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=5)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=6)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=7)
atom.type.plot(temp=c(320,345,370,395,420,445,470,500,550,600), polymer="PMMA", type=8)
# PS
##########################################################################################################
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=1)
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=2)
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=3)
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=4)
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=5)
atom.type.plot(temp=c(200,250,300,350,400,450,500,550,600), polymer="PS", type=6)
# PIB
##########################################################################################################
atom.type.plot(temp=c(175,200,225,250,275,300,350,400), polymer="PIB", type=1)
atom.type.plot(temp=c(175,200,225,250,275,300,350,400), polymer="PIB", type=2)
atom.type.plot(temp=c(175,200,225,250,275,300,350,400), polymer="PIB", type=3)
atom.type.plot(temp=c(175,200,225,250,275,300,350,400), polymer="PIB", type=4)
atom.type.plot(temp=c(175,200,225,250,275,300,350,400), polymer="PIB", type=5)
```