|
| 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=None): |
| 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 representing |
| 13 | + 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 / 2^(num_block_steps) |
| 22 | + of pixels representing the blocked configuration. |
| 23 | + """ |
| 24 | + L = int(np.sqrt(len(config.flatten()))/2) |
| 25 | + if config.shape != (2*L, 2*L): |
| 26 | + config = config.reshape(2*L, 2*L) |
| 27 | + blocked_config = np.zeros((L, L), dtype=int) |
| 28 | + blocked_sites = [(2*i, 2*j) for i in range(L//2) for j in range(L//2)] |
| 29 | + for site in blocked_sites: |
| 30 | + i = site[0] |
| 31 | + j = site[1] |
| 32 | + # look at the number of active external bonds lejving the block to the |
| 33 | + # right (ext_x_bonds) and upwards (ext_y_bonds) |
| 34 | + ext_x_bonds = [config[2*i, 2*j+3], config[2*i+2, 2*j+3]] |
| 35 | + ext_y_bonds = [config[2*i+3, 2*j], config[2*i+3, 2*j+2]] |
| 36 | + if double_bonds_value is None: |
| 37 | + ext_x_bonds_active = xor(ext_x_bonds[0], ext_x_bonds[1]) |
| 38 | + ext_y_bonds_active = xor(ext_y_bonds[0], ext_y_bonds[1]) |
| 39 | + active_site = ext_x_bonds_active or ext_y_bonds_active |
| 40 | + else: |
| 41 | + if ext_x_bonds == [1, 1]: |
| 42 | + ext_x_bonds_active = double_bonds_value |
| 43 | + if ext_y_bonds == [1, 1]: |
| 44 | + ext_y_bonds_active = double_bonds_value |
| 45 | + if ext_x_bonds_active or ext_y_bonds_active: |
| 46 | + active_site = double_bonds_value |
| 47 | + blocked_config[i, j] = active_site |
| 48 | + blocked_config[i, j+1] = ext_x_bonds_active |
| 49 | + blocked_config[i+1, j] = ext_y_bonds_active |
| 50 | + |
| 51 | + for site in blocked_sites: |
| 52 | + i = site[0] |
| 53 | + j = site[1] |
| 54 | + if blocked_config[i, j-1] or blocked_config[i-1, j]: |
| 55 | + blocked_config[site] = 1 |
| 56 | + while num_block_steps > 1: |
| 57 | + return _block_config(blocked_config.flatten(), num_block_steps-1) |
| 58 | + |
| 59 | + return blocked_config.flatten() |
| 60 | + |
| 61 | +def block_configs(file, out_dir=None): |
| 62 | + """ |
| 63 | + Read in all configs stored in `file` and construct array of blocked configs. |
| 64 | +
|
| 65 | + Args: |
| 66 | + file (str): |
| 67 | + File containing original (unblocked) configs, one per line. |
| 68 | + """ |
| 69 | + print("Reading from {}".format(file)) |
| 70 | + try: |
| 71 | + # temp = file.splitsx./{]}('/')[-1].split('_')[-1].rstrip('.txt') |
| 72 | + temp = file.split('/')[-1].split('_')[-1].rstrip('.txt') |
| 73 | + configs = pd.read_csv( |
| 74 | + file, header=None, engine='c', delim_whitespace=True, index_col=0, |
| 75 | + ).values |
| 76 | + L = int(np.sqrt(len(configs[0].flatten()))/2) |
| 77 | + blocked_configs = [] |
| 78 | + for config in configs: |
| 79 | + blocked_configs.append(_block_config(config)) |
| 80 | + blocked_configs = np.array(blocked_configs) |
| 81 | + L_blocked = int(np.sqrt(len(blocked_configs[0].flatten()))/2) |
| 82 | + if out_dir is None: |
| 83 | + out_dir = ('../data/iterated_blocking/' |
| 84 | + + '{}_lattice/blocked_{}/'.format(L, L_blocked)) |
| 85 | + else: |
| 86 | + out_dir = out_dir |
| 87 | + save_blocked_configs(blocked_configs, temp, out_dir=out_dir) |
| 88 | + except IOError: |
| 89 | + print("Unable to read from: {}".format(file)) |
| 90 | + raise |
| 91 | + |
| 92 | + |
| 93 | +def save_blocked_configs(configs, temperature, out_dir): |
| 94 | + """Save blocked configurations to text file. |
| 95 | +
|
| 96 | + Args: |
| 97 | + configs (array-like): |
| 98 | + Array containing blocked configurations, with one (flattened) |
| 99 | + configuration per row. |
| 100 | + temperature (string / int): |
| 101 | + Temperature at which configurations were generated. Each |
| 102 | + temperature gets its own file. |
| 103 | + out_dir (string): |
| 104 | + Directory in which to save blocked configurations. |
| 105 | + Returns: |
| 106 | + None |
| 107 | + """ |
| 108 | + L = int(np.sqrt(len(configs[0].flatten()))/2) |
| 109 | + if not os.path.exists(out_dir): |
| 110 | + os.makedirs(out_dir) |
| 111 | + if out_dir.endswith('/'): |
| 112 | + out_file = ( |
| 113 | + out_dir + "{}_blocked_configs_{}.txt".format(L, str(temperature)) |
| 114 | + ) |
| 115 | + else: |
| 116 | + out_file = ( |
| 117 | + out_dir + "/{}_blocked_configs_{}.txt".format(L, str(temperature)) |
| 118 | + ) |
| 119 | + if os.path.exists(out_file): |
| 120 | + os.rename(out_file, out_file + ".bak") |
| 121 | + print("Saving to: {}\n".format(out_file)) |
| 122 | + pd.DataFrame(configs).to_csv(out_file, header=None, sep=' ') |
0 commit comments