refactor: comments removed in input/openQCD, typo corrected

This commit is contained in:
Fabian Joswig 2022-01-16 16:53:57 +01:00
parent f8cbaef626
commit 5993f1a4ba

View file

@ -1,6 +1,3 @@
#!/usr/bin/env python
# coding: utf-8
import os import os
import fnmatch import fnmatch
import re import re
@ -42,7 +39,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
if 'files' in kwargs: if 'files' in kwargs:
ls = kwargs.get('files') ls = kwargs.get('files')
else: else:
# Exclude files with different names
for exc in ls: for exc in ls:
if not fnmatch.fnmatch(exc, prefix + '*' + postfix + '.dat'): if not fnmatch.fnmatch(exc, prefix + '*' + postfix + '.dat'):
ls = list(set(ls) - set([exc])) ls = list(set(ls) - set([exc]))
@ -69,7 +65,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
print('Read reweighting factors from', prefix[:-1], ',', print('Read reweighting factors from', prefix[:-1], ',',
replica, 'replica', end='') replica, 'replica', end='')
# Adjust replica names to new bookmarking system
if names is None: if names is None:
rep_names = [] rep_names = []
for entry in ls: for entry in ls:
@ -88,7 +83,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
tmp_array = [] tmp_array = []
with open(path + '/' + ls[rep], 'rb') as fp: with open(path + '/' + ls[rep], 'rb') as fp:
# header
t = fp.read(4) # number of reweighting factors t = fp.read(4) # number of reweighting factors
if rep == 0: if rep == 0:
nrw = struct.unpack('i', t)[0] nrw = struct.unpack('i', t)[0]
@ -97,7 +91,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
for k in range(nrw): for k in range(nrw):
deltas.append([]) deltas.append([])
else: else:
# little weird if-clause due to the /2 operation needed.
if ((nrw != struct.unpack('i', t)[0] and (not version == '2.0')) or (nrw != struct.unpack('i', t)[0] / 2 and version == '2.0')): if ((nrw != struct.unpack('i', t)[0] and (not version == '2.0')) or (nrw != struct.unpack('i', t)[0] / 2 and version == '2.0')):
raise Exception('Error: different number of reweighting factors for replicum', rep) raise Exception('Error: different number of reweighting factors for replicum', rep)
@ -110,8 +103,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
for i in range(nrw): for i in range(nrw):
t = fp.read(4) t = fp.read(4)
nfct.append(struct.unpack('i', t)[0]) nfct.append(struct.unpack('i', t)[0])
# print('nfct: ', nfct) # Hasenbusch factor,
# 1 for rat reweighting
else: else:
for i in range(nrw): for i in range(nrw):
nfct.append(1) nfct.append(1)
@ -124,7 +115,6 @@ def read_rwms(path, prefix, version='2.0', names=None, **kwargs):
if not struct.unpack('i', fp.read(4))[0] == 0: if not struct.unpack('i', fp.read(4))[0] == 0:
print('something is wrong!') print('something is wrong!')
# body
while 0 < 1: while 0 < 1:
t = fp.read(4) t = fp.read(4)
if len(t) < 4: if len(t) < 4:
@ -220,7 +210,6 @@ def extract_t0(path, prefix, dtr_read, xmin,
if not ls: if not ls:
raise Exception('Error, directory not found') raise Exception('Error, directory not found')
# Exclude files with different names
for exc in ls: for exc in ls:
if not fnmatch.fnmatch(exc, prefix + '*.ms.dat'): if not fnmatch.fnmatch(exc, prefix + '*.ms.dat'):
ls = list(set(ls) - set([exc])) ls = list(set(ls) - set([exc]))
@ -232,7 +221,6 @@ def extract_t0(path, prefix, dtr_read, xmin,
r_start = kwargs.get('r_start') r_start = kwargs.get('r_start')
if len(r_start) != replica: if len(r_start) != replica:
raise Exception('r_start does not match number of replicas') raise Exception('r_start does not match number of replicas')
# Adjust Configuration numbering to python index
r_start = [o - 1 if o else None for o in r_start] r_start = [o - 1 if o else None for o in r_start]
else: else:
r_start = [None] * replica r_start = [None] * replica
@ -251,7 +239,6 @@ def extract_t0(path, prefix, dtr_read, xmin,
for rep in range(replica): for rep in range(replica):
with open(path + '/' + ls[rep], 'rb') as fp: with open(path + '/' + ls[rep], 'rb') as fp:
# Read header
t = fp.read(12) t = fp.read(12)
header = struct.unpack('iii', t) header = struct.unpack('iii', t)
if rep == 0: if rep == 0:
@ -270,7 +257,6 @@ def extract_t0(path, prefix, dtr_read, xmin,
Ysl = [] Ysl = []
# Read body
while 0 < 1: while 0 < 1:
t = fp.read(4) t = fp.read(4)
if(len(t) < 4): if(len(t) < 4):
@ -334,12 +320,6 @@ def _parse_array_openQCD2(d, n, size, wa, quadrupel=False):
return arr return arr
# mimic the read_array routine of openQCD-2.0.
# fp is the opened file handle
# returns the dict array
# at this point we only parse a 2d array
# d = 2
# n = [nfct[irw], 2*nsrc[irw]]
def _read_array_openQCD2(fp): def _read_array_openQCD2(fp):
t = fp.read(4) t = fp.read(4)
d = struct.unpack('i', t)[0] d = struct.unpack('i', t)[0]
@ -400,13 +380,11 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
last configurations that need to be read (per replicum) last configurations that need to be read (per replicum)
files: list files: list
specify the exact files that need to be read specify the exact files that need to be read
from path, pratical if e.g. only one replicum is needed from path, practical if e.g. only one replicum is needed
names: list names: list
Alternative labeling for replicas/ensembles. Alternative labeling for replicas/ensembles.
Has to have the appropriate length Has to have the appropriate length
""" """
# one could read L from the header in case of sfQCD
# c = 0.35
known_versions = ["1.0", "1.2", "1.4", "1.6", "2.0", "sfqcd"] known_versions = ["1.0", "1.2", "1.4", "1.6", "2.0", "sfqcd"]
if version not in known_versions: if version not in known_versions:
@ -426,11 +404,9 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
r_start = kwargs.get("r_start") r_start = kwargs.get("r_start")
if "r_stop" in kwargs: if "r_stop" in kwargs:
r_stop = kwargs.get("r_stop") r_stop = kwargs.get("r_stop")
# if one wants to read specific files with this method...
if "files" in kwargs: if "files" in kwargs:
files = kwargs.get("files") files = kwargs.get("files")
else: else:
# find files in path
found = [] found = []
files = [] files = []
for (dirpath, dirnames, filenames) in os.walk(path + "/"): for (dirpath, dirnames, filenames) in os.walk(path + "/"):
@ -441,14 +417,12 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
if fnmatch.fnmatch(f, prefix + "*" + ".ms.dat"): if fnmatch.fnmatch(f, prefix + "*" + ".ms.dat"):
files.append(f) files.append(f)
print(files) print(files)
# now that we found our files, we dechiffer them...
rep_names = [] rep_names = []
deltas = [] deltas = []
idl = [] idl = []
for rep, file in enumerate(files): for rep, file in enumerate(files):
with open(path + "/" + file, "rb") as fp: with open(path + "/" + file, "rb") as fp:
# header
t = fp.read(12) t = fp.read(12)
header = struct.unpack('<iii', t) header = struct.unpack('<iii', t)
# step size in integration steps "dnms" # step size in integration steps "dnms"
@ -477,7 +451,6 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
Q = [] Q = []
ncs = [] ncs = []
while 0 < 1: while 0 < 1:
# int nt
t = fp.read(4) t = fp.read(4)
if(len(t) < 4): if(len(t) < 4):
break break
@ -524,8 +497,6 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
if len(Q_round) != len(ncs) // dtr_cnfg: if len(Q_round) != len(ncs) // dtr_cnfg:
raise Exception("qtops and ncs dont have the same length") raise Exception("qtops and ncs dont have the same length")
# replica = len(files)
truncated_file = file[:-7] truncated_file = file[:-7]
print(truncated_file) print(truncated_file)
idl_start = 1 idl_start = 1
@ -553,7 +524,6 @@ def read_qtop(path, prefix, c, dtr_cnfg=1, version="1.2", **kwargs):
rep_names = names rep_names = names
deltas.append(np.array(Q_round)) deltas.append(np.array(Q_round))
idl.append(range(idl_start, idl_stop)) idl.append(range(idl_start, idl_stop))
# print(idl)
result = Obs(deltas, rep_names, idl=idl) result = Obs(deltas, rep_names, idl=idl)
return result return result
@ -594,7 +564,6 @@ def read_qtop_sector(target=0, **kwargs):
dtr_cnfg = 1 dtr_cnfg = 1
qtop = read_qtop(path, prefix, c, dtr_cnfg=dtr_cnfg, qtop = read_qtop(path, prefix, c, dtr_cnfg=dtr_cnfg,
version=version, **kwargs) version=version, **kwargs)
# unpack to original values, project onto target sector
names = qtop.names names = qtop.names
print(names) print(names)
print(qtop.deltas.keys()) print(qtop.deltas.keys())