-
Notifications
You must be signed in to change notification settings - Fork 2
/
MISC.py
277 lines (217 loc) · 7.97 KB
/
MISC.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
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
###############################################################################
# Source file: ./MISC.py
#
# Copyright (C) 2020
#
# Author: Andreas Juettner juettner@soton.ac.uk
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# See the full license in the file "LICENSE" in the top level distribution
# directory
###############################################################################
import numpy as np
import sys
from parameters import *
separator = "##########################################################"
# Routines for computing the integrated autocorrelation time
eps = sys.float_info.epsilon
class UWerr():
def __init__(self, Data, Stau, Name, function=[], *varargin):
"""
Compute integrated autocorrelation time based on U. Wolff's "Monte
Carlo Errors with less errors" http://arxiv.org/abs/hep-lat/0306017
implementation of
Data:
columns: different observables
lines: consecutive measurements
Stau:
Stau=0: no autocorrelations
"""
self.Data = Data
self.Stau = Stau
self.Name = Name
self.function = function
self.varargin = varargin
self.dim = Data.shape
self.N = self.dim[0]
def doit(self):
if len(self.dim) == 2:
self.Nobs = self.dim[1]
else:
self.Nobs = 1
# means of primary observables
v = np.mean(self.Data, 0)
# means of secondary observables
if self.function == []: # if only primary observables
fv = v
else:
if self.varargin:
fv = self.function(v, self.varargin)
else:
fv = self.function(v)
# derivative with respect to primary observables:
D = []
if self.function == []:
delta = self.Data - v
else:
dum = np.array(v)
h = np.std(self.Data, 0) / np.sqrt(self.N)
D = h * 0.
for i in range(self.Nobs):
if h[i] == 0:
D[i] = 0
else:
dum[i] = v[i] + h[i]
if self.varargin:
D[i] = self.function(dum, self.varargin)
else:
D[i] = self.function(dum)
dum[i] = v[i] - h[i]
if self.varargin:
D[i] = D[i] - self.function(dum, self.varargin)
else:
D[i] = D[i] - self.function(dum)
dum[i] = np.array(v[i])
D[i] = D[i] / (2. * h[i])
delta = np.dot((self.Data-v), D)
Gamma = np.zeros(int(np.floor(1. * self.N / 2)))
Gamma[0] = np.mean(delta ** 2)
if Gamma[0] == 0:
print("UWerr: data contains no no fluctuations: Gamma[0]=0")
exit()
if self.Stau == 0:
Wop = 0
tmax = 0
doGamma = 0
else:
tmax = int(np.floor(1. * self.N / 2.))
doGamma = 1
Gint = 0
t = 1
t = 1
while t <= tmax:
Gamma[t] = np.sum(delta[0: -(t)] * delta[t:]) / (self.N - t)
if doGamma == 1:
Gint = Gint + Gamma[t] / Gamma[0]
if Gint <= 0:
tauW = eps
else:
tauW = self.Stau / (np.log((Gint + 1.) / Gint))
gW = np.exp(-1. * t / tauW) - tauW / np.sqrt(t * self.N)
if gW < 0:
Wopt = t
tmax = np.min([tmax, 2 * t])
doGamma = 0
t = t + 1
if doGamma == 1:
print("UWerr: windowing condition failed up to W=%d\n" % (tmax))
Wopt = tmax
Gamma = Gamma[:t]
GammaOpt = Gamma[0] + 2. * np.sum(Gamma[1: Wopt + 1])
if GammaOpt <= 0:
print("UWerr: Gamma pathological with error below zero")
exit()
Gamma = Gamma + GammaOpt / self.N
GammaOpt = Gamma[0] + 2. * np.sum(Gamma[1: Wopt])
dv = np.sqrt(GammaOpt / self.N)
ddv = dv * (np.sqrt((Wopt + .5) / self.N))
rho = Gamma / Gamma[0]
tauint = (np.cumsum(rho)[Wopt] - 0.5)
dtauint = tauint * 2 * np.sqrt((Wopt-tauint + 0.5) / self.N)
return (fv, dv, ddv, tauint, dtauint, Wopt)
# routine to flatten nested lists or arrays
def flatten(x):
"""
flatten(sequence) -> list
Returns a single, flat list which contains all elements retrieved
from the sequence and all recursively contained sub-sequences
(iterables).
Examples:
>>> [1, 2, [3,4], (5,6)]
[1, 2, [3, 4], (5, 6)]
>>> flatten([[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)])
[1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]
"""
result = []
for el in x:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
result.extend(flatten(el))
else:
result.append(el)
return result
# utility to check whether input parameters match existing simulations
def check_exists(ss, x):
"""
Checks if simulation data for a particular input value of ag, L/a and N
exists, exists if not
"""
if ss == 'L/a':
isin = x in Ls
available = Ls
elif ss == 'ag':
isin = x in gs
available = gs
elif ss == 'N':
isin = x in Ns
available = Ns
if not isin:
print("Value of %s=%s not available" % (ss, x))
print("Available values are ", ' '.join([str(i) for i in available]))
exit()
def disperr3(val, dval):
"""
Helper routine for nicely printing results with error bars.
Based on MATLAB script by Roland Hoffmann
"""
n = len(val)
if n != len(dval):
print("val and dval must have the same length!")
print(val, dval)
print("exiting")
exit()
dig = 2
res = n * ['']
for i in range(n):
if dval[i] == 0. and val[i] == 0.:
res[i] = "0"
elif np.isnan(val[i]) or np.isnan(dval[i]):
res[i] = "nan"
elif dval[i] == 0. and val[i] != 0.:
value = "%d" % val[i]
res[i] = value
elif dval[i] < 1:
location = int(np.floor(np.log10(dval[i])))
append_err = "(" + str(int(np.round(dval[i] * 10 **
(-location + dig - 1)))) + ")"
if np.abs(val[i]) < 1e-100:
val[i] = 0.
location = 1
valformat = "%."+str(-location+dig-1) + "f"
sval = valformat % val[i]
res[i] = sval + append_err
elif dval[i] >= 1:
digits = min(0, int(np.ceil(np.log10(dval[i]))-1)) + 1
error = np.around(dval[i], digits)
value = np.around(val[i], digits)
serr = "%."+str(digits) + "f(%."+str(digits) + "f)"
serr = serr % (value, error)
res[i] = serr # str(value)+"("+str(error)+")"
else:
digits = max(0, int(np.ceil(np.log10(dval[i])) - 1))
error = int(round(dval[i] / 10 ** digits) * 10 ** digits)
value = round(val[i] / 10 ** digits) * 10 ** digits
res[i] = str(value) + "(" + str(error) + ")"
return res