OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
lcorrzFit
1#!@Python3_EXECUTABLE@
2
3__author__ = "Patrick Louden (plouden@nd.edu)"
4__copyright__ = "Copyright (c) 2004-present The University of Notre Dame. All Rights Reserved."
5__license__ = "OpenMD"
6
7import sys
8import os
9import datetime
10import argparse
11import textwrap
12import numpy as np
13import math
14from scipy.optimize import curve_fit
15from argparse import RawDescriptionHelpFormatter
16from scipy import stats
17
18
19
20def usage():
21 print(__doc__)
22
23
24# Fit function we are optimizing data to
25def func(x, a, tau0, b, tau1, tau2):
26 ''' fit function
27 C2(z,t) = a*exp(-t/tau0) + b*exp(-t/tau1) + (1-a-b)*exp(-t/tau2)
28 '''
29 return ( a*np.exp(-x/tau0) + b*np.exp(-x/tau1) + (1-a-b)*np.exp(-x/tau2) )
30
31
32
33def lcorrzExtractor(lcorrzFileName, cutoffTime):
34 if os.path.exists(lcorrzFileName):
35 lcorrzFile = open(lcorrzFileName, 'r')
36 else:
37 print('Error: cannot open ' + lcorrzFile)
38 sys.exit(2)
39
40 time = []
41 lcorrzArray = []
42
43 while True:
44 line = lcorrzFile.readline()
45 if not line: break
46
47 if ("#" in line):
48 pass
49 else:
50 tempArray = np.array([])
51 splitLine = line.strip().split()
52
53 if (len(splitLine) > 0):
54 for i in range(0, len(splitLine)):
55 if (i == 0):
56 if (float(splitLine[0]) < float(cutoffTime)):
57 time = np.append(time, float(splitLine[i]))
58 else:
59 if (float(splitLine[0]) < float(cutoffTime)):
60 tempArray = np.append(tempArray, float(splitLine[i]))
61 if (len(tempArray > 0)):
62 lcorrzArray.append(tempArray)
63
64 lcorrzArray = np.asarray(lcorrzArray)
65 return (time, lcorrzArray)
66
67
68
69def lcorrzFitter(time, lcorrzArray, tau0, tau1, tau2, a, b, cutoffTime, nbins, fitFileName):
70
71 fitFile = open(fitFileName, "w")
72 fitFile.write("# bin no. \t a \t tau0 \t b \t tau1 \t tau2 \n")
73
74 for i in range(0, int(nbins)):
75 lcorrzBin = np.array([])
76
77 for j in range(0, len(lcorrzArray)):
78 lcorrzBin = np.append(lcorrzBin, lcorrzArray[j][i]) #This generates an array with all the data from one bin
79
80 popt, pcov = curve_fit(func, time, lcorrzBin, [a, tau0, b, tau1, tau2], ftol=0.01)
81 fitFile.write( str(i) + "\t" + str(popt[0]) + "\t" + str(popt[1]) + "\t" + str(popt[2]) + "\t" + str(popt[3]) + "\t" + str(popt[4]) + "\n")
82
83
84
85def main(argv):
86 parser = argparse.ArgumentParser(
87 description='OpenMD lcorrZ fitting script.',
88 formatter_class=RawDescriptionHelpFormatter,
89 epilog="Fit function: y = a*exp(-t/tau0) + b*exp(-t/tau1) + (1-a-b)*exp(-t/tau2) \nExample: lcorrzFit -i waterSim.lcorrZ -o waterSim.lcorrZFit")
90 parser.add_argument("-i", "--rnemd-file=", action="store", dest="lcorrzFileName", help="use specified input (.lcorrz) file")
91 parser.add_argument("-o", "--fit-file=", action="store", dest="fitFileName", help="use specified output (.fit) file")
92 parser.add_argument("-c", "--cutoffTime=", action="store", dest="cutoffTime", help="specified time to truncate data at")
93 parser.add_argument("-n", "--nbins=", action="store", dest="nbins", help="specified number of bins used in the lcorrz calculation")
94 parser.add_argument("-t0", "--tau0=", action="store", default=1e2, type=float, dest="tau0", help="the fastest decay time")
95 parser.add_argument("-t1", "--tau1=", action="store", default=1e3, type=float, dest="tau1", help="the middle decay time")
96 parser.add_argument("-t2", "--tau2=", action="store", default=1e6, type=float, dest="tau2", help="the slow decay time")
97 parser.add_argument("-a", "--a=", action="store", dest="a", default=0.25, type=float, help="fraction of contribution of slow motion to the overall decay")
98 parser.add_argument("-b", "--b=", action="store", dest="b", default=0.25, type=float, help="fraction of contribution of middle motion to the overall decay")
99
100
101 if len(sys.argv) == 1:
102 parser.print_help()
103 sys.exit(2)
104 args = parser.parse_args()
105
106 if (not args.lcorrzFileName):
107 parser.print_help()
108 parser.error("No input-file was specified")
109
110 if (not args.fitFileName):
111 parser.print_help()
112 parser.error("No output-file was specified")
113
114 if (not args.nbins):
115 parser.print_help()
116 parser.error("No value for nbins was specified")
117
118 #Call functions here, pass appropriate variables.
119 (time, lcorrzArray) = lcorrzExtractor(args.lcorrzFileName, args.cutoffTime)
120 lcorrzFitter(time, lcorrzArray, args.tau0, args.tau1, args.tau2, args.a, args.b, args.cutoffTime, args.nbins, args.fitFileName)
121
122
123
124if __name__ == "__main__":
125 main(sys.argv[1:])