Skip to content

Commit e74bbd0

Browse files
committed
SF renamed iterated_blocking.py to block_configs.py, added plotting function to utils.py
1 parent ebbdc30 commit e74bbd0

14 files changed

+10912
-10239
lines changed

worm_algorithm/block_configs.ipynb

Lines changed: 2572 additions & 0 deletions
Large diffs are not rendered by default.

worm_algorithm/block_configs.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import numpy as np
2+
import pandas as pd
3+
import os
4+
from operator import xor
5+
6+
def _block_config(config, num_block_steps=1, double_bonds_value=0):
7+
"""Create (iterated) blocked config from un-blocked configu using
8+
approximate (1 + 1 = 0) blocking scheme.
9+
10+
Args:
11+
config (array-like):
12+
Flattened array of length 4L^2 (= 2L * 2L) of pixels
13+
representing the original configuration.
14+
(NOTE: L = linear extent of original lattice)
15+
num_block_steps (int, default = 1):
16+
Number of blocking steps to perform. If num_block_steps > 1,
17+
recursively implement block_config on the returned (blocked)
18+
configuration.
19+
Returns:
20+
blocked_config (np.ndarray):
21+
Flattened blocked configuration of length L^2 /
22+
2^(num_block_steps) of pixels representing the blocked
23+
configuration.
24+
"""
25+
L = int(np.sqrt(len(config.flatten()))/2)
26+
if config.shape != (2*L, 2*L):
27+
config = config.reshape(2*L, 2*L)
28+
blocked_config = np.zeros((L, L), dtype=int)
29+
blocked_sites = [(2*i, 2*j) for i in range(L//2) for j in range(L//2)]
30+
for site in blocked_sites:
31+
i = site[0]
32+
j = site[1]
33+
# look at the number of active external bonds leaving the block to the
34+
# right (ext_x_bonds) and upwards (ext_y_bonds)
35+
ext_x_bonds = [config[2*i, 2*j+3], config[2*i+2, 2*j+3]]
36+
ext_y_bonds = [config[2*i+3, 2*j], config[2*i+3, 2*j+2]]
37+
if double_bonds_value == 0:
38+
ext_x_bonds_active = xor(ext_x_bonds[0], ext_x_bonds[1])
39+
ext_y_bonds_active = xor(ext_y_bonds[0], ext_y_bonds[1])
40+
# active_site = ext_x_bonds_active or ext_y_bonds_active
41+
else:
42+
num_active_x_bonds = sum(ext_x_bonds)
43+
num_active_y_bonds = sum(ext_y_bonds)
44+
ext_x_bonds_active = 0
45+
ext_y_bonds_active = 0
46+
if num_active_x_bonds > 0:
47+
if num_active_x_bonds == 2:
48+
ext_x_bonds_active = double_bonds_value
49+
if num_active_x_bonds == 1:
50+
ext_x_bonds_active = 1
51+
if num_active_y_bonds > 0:
52+
if num_active_y_bonds == 2:
53+
ext_y_bonds_active = double_bonds_value
54+
if num_active_y_bonds == 1:
55+
ext_y_bonds_active = 1
56+
active_site = ext_x_bonds_active or ext_y_bonds_active
57+
blocked_config[i, j] = active_site
58+
blocked_config[i, j+1] = ext_x_bonds_active
59+
blocked_config[i+1, j] = ext_y_bonds_active
60+
61+
for site in blocked_sites:
62+
i = site[0]
63+
j = site[1]
64+
if blocked_config[i, j-1] or blocked_config[i-1, j]:
65+
blocked_config[site] = 1
66+
if double_bonds_value == 0:
67+
while num_block_steps > 1:
68+
return _block_config(blocked_config.flatten(),
69+
num_block_steps-1, double_bonds_value=0)
70+
71+
return blocked_config.flatten()
72+
73+
def block_configs(config_file, double_bonds_value=0, out_dir=None):
74+
"""
75+
Read in all configs stored in `config_file` and construct array of blocked
76+
configs.
77+
78+
Args:
79+
config_file (str):
80+
File containing original (unblocked) configs, one per line.
81+
"""
82+
print("Reading from {}".format(config_file))
83+
try:
84+
# temp = file.splitsx./{]}('/')[-1].split('_')[-1].rstrip('.txt')
85+
temp = config_file.split('/')[-1].split('_')[-1].rstrip('.txt')
86+
configs = pd.read_csv(config_file, header=None, engine='c',
87+
delim_whitespace=True, index_col=0).values
88+
L = int(np.sqrt(len(configs[0].flatten()))/2)
89+
# blocked_configs = np.zeros((configs.shape[0], configs.shape[1]/4))
90+
blocked_configs = []
91+
# for idx, config in enumerate(configs):
92+
for config in configs:
93+
# blocked_configs[idx] = _block_config(config)
94+
blocked_configs.append(_block_config(config))
95+
#blocked_configs = np.array(blocked_configs)
96+
L_blocked = int(np.sqrt(len(blocked_configs[0].flatten()))/2)
97+
if out_dir is None:
98+
out_dir = (f"../data/blocked_configs/{L}_lattice/"
99+
+ f"double_bonds_{double_bonds_value}/")
100+
# out_dir = ("../data/iterated_blocking/"
101+
# + "{}_lattice/blocked_{}".format(L, L_blocked))
102+
save_blocked_configs(blocked_configs, temp, out_dir=out_dir)
103+
except IOError:
104+
print("Unable to read from: {}".format(config_file))
105+
raise
106+
107+
108+
def save_blocked_configs(configs, temperature, out_dir):
109+
"""Save blocked configurations to text file.
110+
111+
Args:
112+
configs (array-like):
113+
Array containing blocked configurations, with one (flattened)
114+
configuration per row.
115+
temperature (string / int):
116+
Temperature at which configurations were generated. Each
117+
temperature gets its own file.
118+
out_dir (string):
119+
Directory in which to save blocked configurations.
120+
Returns:
121+
None
122+
"""
123+
L = int(np.sqrt(len(configs[0].flatten()))/2)
124+
if not os.path.exists(out_dir):
125+
os.makedirs(out_dir)
126+
if out_dir.endswith('/'):
127+
out_file = (
128+
out_dir + "{}_blocked_configs_{}.txt".format(L, str(temperature))
129+
)
130+
else:
131+
out_file = (
132+
out_dir + "/{}_blocked_configs_{}.txt".format(L, str(temperature))
133+
)
134+
if os.path.exists(out_file):
135+
os.rename(out_file, out_file + ".bak")
136+
print("Saving to: {}\n".format(out_file))
137+
pd.DataFrame(configs).to_csv(out_file, header=None, sep=' ')

worm_algorithm/bonds.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,24 @@ def _get_map(self):
9797
self._get_raw_bonds()
9898
self._raw_bonds = np.array(self._raw_bonds, dtype=int)
9999
_map = {}
100-
for i in range(len(self._raw_bonds)):
101-
key = self._raw_bonds[i, 0]
102-
sites = self._raw_bonds[i, 1:]
103-
start_site = sites[:2]
104-
end_site = sites[2:]
100+
keys = self._raw_bonds[:, 0]
101+
start_sites = self._raw_bonds[:, 1:3]
102+
end_sites = self._raw_bonds[:, 3:]
103+
for idx, key in enumerate(keys):
104+
try:
105+
_map[key].append([tuple(start_site[idx]),
106+
tuple(start_site[idx])])
107+
except KeyError:
108+
_map[key] = [tuple(start_site[idx]), tuple(start_site[idx])]
109+
# for i in range(len(self._raw_bonds)):
110+
# key = self._raw_bonds[i, 0]
111+
# sites = self._raw_bonds[i, 1:]
112+
# start_site = sites[:2]
113+
# end_site = sites[2:]
105114
# try:
106115
# _map[key].append([tuple(start_site), tuple(end_site)])
107116
# except KeyError:
108-
_map[key] = [tuple(start_site), tuple(end_site)]
117+
# _map[key] = [tuple(start_site), tuple(end_site)]
109118
return _map
110119

111120
def _get_bonds(self):
@@ -606,9 +615,9 @@ def plot_config(self, T, mode=None, show=True, save=False, save_dir=None):
606615
return fig
607616

608617

609-
def main(**kwargs):
618+
def main(args=None):
610619
parser = argparse.ArgumentParser()
611-
parser.add_argument("-L", "--size", type=int,
620+
parser.add_argument("-L", "--size", type=int, required='True',
612621
help="Define the linear size of the lattice (DEFAULT:"
613622
"16)")
614623
parser.add_argument("-r", "--run_sim", action="store_true",
@@ -666,4 +675,4 @@ def main(**kwargs):
666675

667676

668677
if __name__ == '__main__':
669-
main(sys.argv)
678+
main(sys.argv[1:])

worm_algorithm/count_bonds.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class CountBonds(object):
3030
Whether or not to display information as the analysis is being
3131
performed.
3232
"""
33-
def __init__(self, L, block_val=None, num_blocks=10, data_dir=None,
34-
save_dir=None, save=True, load=False, verbose=False):
33+
def __init__(self, L, block_val=None, num_blocks=10,
34+
data_dir=None, save_dir=None, data_file=None,
35+
save=True, load=False, verbose=False):
3536
self._L = L
3637
self._block_val = block_val
3738
self._num_blocks = num_blocks
@@ -76,7 +77,7 @@ def __init__(self, L, block_val=None, num_blocks=10, data_dir=None,
7677
if not load:
7778
self.count_bonds()
7879
else:
79-
self._load()
80+
self._load(data_file)
8081
if save:
8182
self._save()
8283

@@ -168,6 +169,7 @@ def _count_bonds_with_err(self, data, num_blocks=10):
168169
def count_bonds(self):
169170
"""Calculate bond statistics for all configuration data."""
170171
for idx, config_file in enumerate(self._config_files):
172+
data = None
171173
if self._verbose:
172174
print("Reading in from: {}\n".format(config_file))
173175
# key = self._temp_strings[idx]
@@ -177,6 +179,7 @@ def count_bonds(self):
177179
data = self._get_configs(config_file)
178180
val, err = self._count_bonds_with_err(data, self._num_blocks)
179181
self.bond_stats[key] = [val[0], err[0], val[1], err[1]]
182+
del(data)
180183

181184

182185
def _save(self):
@@ -204,15 +207,16 @@ def _load(self, data_file=None):
204207
"""Load previously computed bond_stats data from .txt file."""
205208
if data_file is None:
206209
data_file = self._save_dir + 'bond_stats_{}.txt'.format(self._L)
207-
raw_data = pd.read_csv(data_file, engine='c', header=None,
208-
delim_whitespace=True).values
210+
print(f"Reading from: {data_file}")
211+
raw_data = pd.read_csv(data_file, engine='c', header=None,
212+
delim_whitespace=True).values
209213
for row in raw_data:
210214
key = str(row[0])
211215
self.bond_stats[key] = [row[1], row[2], row[3], row[4]]
212216

213217

214218

215-
def main(argv):
219+
def main(args=None):
216220
parser = argparse.ArgumentParser()
217221

218222
parser.add_argument("-L", "--size", type=int, required=True,

worm_algorithm/draw_worm_configurations.ipynb

Lines changed: 21 additions & 20 deletions
Large diffs are not rendered by default.

worm_algorithm/generate_data.ipynb

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"execution_count": 1,
2020
"metadata": {
2121
"ExecuteTime": {
22-
"end_time": "2018-01-25T10:33:09.935250Z",
23-
"start_time": "2018-01-25T10:33:08.484556Z"
22+
"end_time": "2018-02-14T06:39:31.740973Z",
23+
"start_time": "2018-02-14T06:39:30.215072Z"
2424
}
2525
},
2626
"outputs": [],
@@ -62,13 +62,13 @@
6262
"execution_count": 2,
6363
"metadata": {
6464
"ExecuteTime": {
65-
"end_time": "2018-01-18T22:31:29.533943Z",
66-
"start_time": "2018-01-18T22:31:29.504640Z"
65+
"end_time": "2018-02-14T06:39:32.786530Z",
66+
"start_time": "2018-02-14T06:39:32.748814Z"
6767
}
6868
},
6969
"outputs": [],
7070
"source": [
71-
"bonds_temp_arr = np.arange(1., 3.7, 0.1)"
71+
"bonds_temp_arr = np.arange(1., 3.5, 0.1)"
7272
]
7373
},
7474
{
@@ -139,8 +139,7 @@
139139
"ExecuteTime": {
140140
"end_time": "2018-01-19T01:41:31.363515Z",
141141
"start_time": "2018-01-19T01:41:30.429168Z"
142-
},
143-
"collapsed": true
142+
}
144143
},
145144
"outputs": [
146145
{
@@ -195,6 +194,76 @@
195194
"bonds16_temp_arr = np.arange(2.1, 2.5, 0.01)"
196195
]
197196
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 3,
200+
"metadata": {
201+
"ExecuteTime": {
202+
"end_time": "2018-02-11T05:25:54.441958Z",
203+
"start_time": "2018-02-11T03:36:01.442165Z"
204+
},
205+
"collapsed": true
206+
},
207+
"outputs": [
208+
{
209+
"name": "stdout",
210+
"output_type": "stream",
211+
"text": [
212+
"compilation -- start\n",
213+
"\n",
214+
"compilation -- done\n",
215+
"\n",
216+
"runs -- start\n",
217+
"\n",
218+
"Running L = 64, T = 1.0, num_steps:10000000.0\n",
219+
"Running L = 64, T = 1.1, num_steps:10000000.0\n",
220+
"Running L = 64, T = 1.2, num_steps:10000000.0\n",
221+
"Running L = 64, T = 1.3, num_steps:10000000.0\n",
222+
"Running L = 64, T = 1.4, num_steps:10000000.0\n",
223+
"Running L = 64, T = 1.5, num_steps:10000000.0\n",
224+
"Running L = 64, T = 1.6, num_steps:10000000.0\n",
225+
"Running L = 64, T = 1.7, num_steps:10000000.0\n",
226+
"Running L = 64, T = 1.8, num_steps:10000000.0\n",
227+
"Running L = 64, T = 1.9, num_steps:10000000.0\n",
228+
"Running L = 64, T = 2.0, num_steps:10000000.0\n",
229+
"Running L = 64, T = 2.1, num_steps:10000000.0\n",
230+
"Running L = 64, T = 2.2, num_steps:10000000.0\n",
231+
"Running L = 64, T = 2.3, num_steps:10000000.0\n",
232+
"Running L = 64, T = 2.4, num_steps:10000000.0\n",
233+
"Running L = 64, T = 2.5, num_steps:10000000.0\n",
234+
"Running L = 64, T = 2.6, num_steps:10000000.0\n",
235+
"Running L = 64, T = 2.7, num_steps:10000000.0\n",
236+
"Running L = 64, T = 2.8, num_steps:10000000.0\n",
237+
"Running L = 64, T = 2.9, num_steps:10000000.0\n",
238+
"Running L = 64, T = 3.0, num_steps:10000000.0\n",
239+
"Running L = 64, T = 3.1, num_steps:10000000.0\n",
240+
"Running L = 64, T = 3.2, num_steps:10000000.0\n",
241+
"Running L = 64, T = 3.3, num_steps:10000000.0\n",
242+
"Running L = 64, T = 3.4, num_steps:10000000.0\n"
243+
]
244+
},
245+
{
246+
"ename": "NameError",
247+
"evalue": "name 'start_site' is not defined",
248+
"output_type": "error",
249+
"traceback": [
250+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
251+
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
252+
"\u001b[0;32m~/worm_algorithm/worm_algorithm/bonds.py\u001b[0m in \u001b[0;36m_get_map\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m _map[key].append([tuple(start_site[idx]),\n\u001b[0m\u001b[1;32m 106\u001b[0m tuple(start_site[idx])])\n",
253+
"\u001b[0;31mKeyError\u001b[0m: 0",
254+
"\nDuring handling of the above exception, another exception occurred:\n",
255+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
256+
"\u001b[0;32m<ipython-input-3-c31ceb3063cf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msim64\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mBonds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m64\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnum_steps\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1E7\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mverbose\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mT_arr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mbonds_temp_arr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
257+
"\u001b[0;32m~/worm_algorithm/worm_algorithm/bonds.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, L, run, num_steps, decay_steps, verbose, T_start, T_end, T_step, T_arr, block_val, write, write_blocked)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_bond_map_dir\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'../data/bond_map/lattice_{}/'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_L\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__map_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_bond_map_dir\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m'bond_map_{}.txt'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_map\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_map\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_bonds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_mapped_bonds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_map_bonds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
258+
"\u001b[0;32m~/worm_algorithm/worm_algorithm/bonds.py\u001b[0m in \u001b[0;36m_get_map\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 106\u001b[0m tuple(start_site[idx])])\n\u001b[1;32m 107\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 108\u001b[0;31m \u001b[0m_map\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart_site\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0midx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart_site\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0midx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 109\u001b[0m \u001b[0;31m# for i in range(len(self._raw_bonds)):\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0;31m# key = self._raw_bonds[i, 0]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
259+
"\u001b[0;31mNameError\u001b[0m: name 'start_site' is not defined"
260+
]
261+
}
262+
],
263+
"source": [
264+
"sim64 = Bonds(64, run=True, num_steps=1E7, verbose=True, T_arr=bonds_temp_arr)"
265+
]
266+
},
198267
{
199268
"cell_type": "code",
200269
"execution_count": 19,
@@ -402,8 +471,7 @@
402471
"ExecuteTime": {
403472
"end_time": "2018-01-16T00:34:40.230694Z",
404473
"start_time": "2018-01-16T00:32:04.118800Z"
405-
},
406-
"collapsed": true
474+
}
407475
},
408476
"outputs": [
409477
{

0 commit comments

Comments
 (0)