-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIO.py
196 lines (159 loc) · 6.52 KB
/
IO.py
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
from color import *
import numpy as np
import glob
import re
import sys
import os
# import seaborn as sns
# import matplotlib as mat
# mat.use("agg")
# import matplotlib.pyplot as plt
# mat.rcParams.update({'font.size': 16})
# mat.rcParams["font.family"] = "Times New Roman"
# size = 12
# sns.set_style("whitegrid")
# sns.set_palette("colorblind", n_colors=16)
def GetLine(file):
while True:
line = file.readline().strip()
if len(line) > 0 and line[0] != "#":
return line
def getListOfFiles(dirName):
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(dirName):
listOfFiles += [os.path.join(dirpath, file) for file in filenames]
return listOfFiles
class param:
# Order, Beta, Rs, Mass2, Lambda, Charge2, TotalStep = [None, ]*7
# kF, Nf, EF, Bubble = [0.0, ]*4
def __init__(self, D, Spin):
# self.DataFolder = "Data"
self.InputFile = "parameter"
self.Dim = D
self.Spin = Spin
with open(self.InputFile, "r") as file:
para = file.readline().split(" ")
self.Order = int(para[0])
self.Beta = float(para[1])
self.Rs = float(para[2])
self.Mass2 = float(para[3])
self.Lambda = float(para[4])
self.MinExtMom = float(para[5])
self.MaxExtMom = float(para[6])
self.TotalStep = int(para[7])
# if self.MaxExtMom==10:
# self.DataFolder = "beta{0}_rs{1}_lam{2}_o{3}_freq_lq".format(self.Beta,self.Rs,self.Lambda,self.Order)
# elif self.MaxExtMom==15:
# self.DataFolder = "beta{0}_rs{1}_lam{2}_o{3}_freq_hq".format(self.Beta,self.Rs,self.Lambda,self.Order)
# else:
self.DataFolder = "beta{0}_rs{1}_lam{2}_o{3}_freqTau".format(
self.Beta, self.Rs, self.Lambda, self.Order)
#self.DataFolder = "beta{0}_rs{1}_lam{2}_o{3}_eqTime".format(self.Beta,self.Rs,self.Lambda,self.Order)
print(self.DataFolder)
if self.Dim == 3:
self.kF = (9.0*np.pi/4.0)**(1.0/3.0)/self.Rs
self.Nf = self.kF/4.0/np.pi**2*self.Spin
# self.kF = (9.0*np.pi/4.0)**(1.0/3.0)/self.Rs
# self.Nf = self.kF/2.0/np.pi**2*self.Spin
elif self.Dim == 2:
self.kF = np.sqrt(2.0)/self.Rs # 2D
self.Nf = 1.0/4.0/np.pi*self.Spin
else:
print("Not Implemented for Dimension {0}".format(self.Dim))
sys.exit(0)
#self.EF = self.kF**2/2.0
self.EF = self.kF**2
self.Beta /= self.EF
self.MaxExtMom *= self.kF
print(yellow("Parameters:"))
print("Rs={0}, kF={1}, EF={2}, Beta={3}, Mass2={4}, Lambda={5}\n".format(
self.Rs, self.kF, self.EF, self.Beta, self.Mass2, self.Lambda))
# For the given path, get the List of all files in the directory tree
def LoadFile(Folder, FileName):
Groups = []
ReWeight = []
Step = []
Data = []
Grid = {}
for f in getListOfFiles(Folder):
if re.search(FileName, f) and not('group' in f) and not ('Diag' in f):
# print ("Loading ", f)
try:
with open(f, "r") as file:
line = file.readline().strip().split(":")[1]
Step.append(float(line))
line = file.readline().strip().split(":")[1]
if len(Groups) == 0:
for e in [e for e in line.split(",") if len(e) > 0]:
Groups.append(tuple([int(o)
for o in e.split("_")]))
line = file.readline().strip().split(":")[1]
ReWeight = [float(e)
for e in line.split(",") if len(e) > 0]
while True:
g = file.readline().split(":")
if g[0].find("Grid") != -1:
key = g[0].strip(" #")
Grid[key] = np.fromstring(g[1], sep=' ')
else:
break
data = np.loadtxt(f)
# if abs(data[1,0]-2.948e8) > 5e6:
# continue
# print (f, data[1])
Data.append(data)
except Exception as e:
print("Failed to load {0}".format(f))
print(str(e))
Data = np.array(Data)
DataDict = {}
for (idx, g) in enumerate(Groups):
DataDict[g] = np.array(Data[:, idx, :])
return DataDict, np.array(Step), Groups, np.array(ReWeight), Grid
def LoadFile_Diag(Folder):
# Diags = {'0_0': None, '1_0_0_0': None, '1_0_1_0': None, '1_0_2_0': None, '1_0_2_1': None}
DataDict = {}
Step = {}
Grid = None
fname1 = "Diag"
fname2 = "pid[0-9]+.dat"
for f in getListOfFiles(Folder):
if re.search(fname1, f) and re.search(fname2, f):
# print ("Loading ", f)
try:
with open(f, "r") as file:
line = file.readline().strip().split(":")
DiagName = line[-2].split(",")[0]
DiagName = tuple([int(o)for o in DiagName.split("_")])
# Step.append(float(line[-1]))
data = np.loadtxt(file)
if DiagName == (0, 0):
DiagName = (0,)
if DiagName in DataDict:
# Diags[DiagName] = np.row_stack(Diags[DiagName],data[:,1])
DataDict[DiagName].append(data[:, 1])
Step[DiagName].append(float(line[-1]))
else:
DataDict[DiagName] = [data[:, 1]]
Step[DiagName] = [float(line[-1])]
Grid = data[:, 0]
except Exception as e:
print("Failed to load {0}".format(f))
print(str(e))
for g in DataDict.keys():
DataDict[g] = np.array(DataDict[g])
return DataDict, Step[0, ], Grid
def ErrorPlot(p, x, d, color='k', marker='s', label=None, size=4, shift=False):
p.plot(x, d, marker=marker, c=color, label=label,
lw=1, markeredgecolor="None", linestyle="--", markersize=size)
ColorList = ['k', 'r', 'b', 'g', 'm', 'c', 'navy',
'y', 'cyan', 'darkgreen', 'violet', 'lime', 'purple']
ColorList = ColorList*40
if __name__ == '__main__':
Para = param(3, 2)
# dirName = "./data"
dirName = Para.DataFolder
filename = "pid[0-9]+.dat"
LoadFile(dirName, filename)
# for elem in getListOfFiles(dirName):
# print(elem)