239 lines
6.9 KiB
Python
239 lines
6.9 KiB
Python
from lattices import SCC_Lattice, VO2_Lattice
|
|
import sys
|
|
from spin_image import SpinImage
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import tqdm
|
|
from extractors import Rect_Evaluator
|
|
from cache import timeit
|
|
from scipy import signal
|
|
from plotter import Plotter
|
|
import scipy.fftpack as sfft
|
|
import logging
|
|
logger = logging.getLogger('fft')
|
|
# logger.setLevel(logging.DEBUG)
|
|
ch = logging.StreamHandler()
|
|
ch.setLevel(logging.DEBUG)
|
|
formatter = logging.Formatter(
|
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
ch.setFormatter(formatter)
|
|
logger.addHandler(ch)
|
|
|
|
|
|
def test_mixed():
|
|
fig, axs = plt.subplots(3, 3)
|
|
LEN = 40
|
|
lat = VO2_Lattice(LEN, LEN)
|
|
plot = Plotter(lat)
|
|
si = SpinImage(lat.get_phases())
|
|
mask_misk = np.ones((LEN, LEN))
|
|
ind = np.arange(mask_misk.size)
|
|
np.random.shuffle(ind)
|
|
mask_misk[np.unravel_index(ind[:800], (LEN, LEN))] = 0
|
|
|
|
si.apply_mask(lat.parse_mask(np.zeros((LEN, LEN))))
|
|
print("Clean Rutile: ", si.get_intens(
|
|
lat.parse_mask(np.zeros((LEN, LEN)))))
|
|
si.gaussian(20)
|
|
print("Rutile: ", si.get_intens(lat.parse_mask(np.zeros((LEN, LEN)))))
|
|
intens_mono = si.fft()
|
|
intens_mono.clean()
|
|
plot.plot_spins(si=si, ax_lin=axs[0, 0])
|
|
|
|
si.apply_mask(lat.parse_mask(np.ones((LEN, LEN))))
|
|
print("Clean Mono: ", si.get_intens(lat.parse_mask(np.ones((LEN, LEN)))))
|
|
si.gaussian(20)
|
|
print("Mono: ", si.get_intens(lat.parse_mask(np.ones((LEN, LEN)))))
|
|
intens_rutile = si.fft()
|
|
intens_rutile.clean()
|
|
plot.plot_spins(si=si, ax_lin=axs[0, 2])
|
|
|
|
si.apply_mask(lat.parse_mask(mask_misk))
|
|
print("Clean Mixed: ", si.get_intens(lat.parse_mask(mask_misk)))
|
|
si.gaussian(20)
|
|
print("Mixed: ", si.get_intens(lat.parse_mask(mask_misk)))
|
|
intens_mixed = si.fft()
|
|
intens_mixed.clean()
|
|
plot.plot_spins(si=si, ax_lin=axs[0, 1])
|
|
|
|
plot.plot_fft(intens_mono,
|
|
ax_log=axs[1, 0], ax_lin=axs[2, 0])
|
|
plot.plot_fft(intens_rutile,
|
|
ax_log=axs[1, 2], ax_lin=axs[2, 2])
|
|
plot.plot_fft(intens_mixed,
|
|
ax_log=axs[1, 1], ax_lin=axs[2, 1])
|
|
plt.figure()
|
|
plot.plot_fft(intens_mixed,
|
|
ax_log=plt.gca())
|
|
# Plotting cuts
|
|
|
|
|
|
def test_pdf():
|
|
LEN = 40
|
|
lat = VO2_Lattice(LEN, LEN)
|
|
plot = Plotter(lat)
|
|
si = SpinImage(lat.get_phases())
|
|
integrate = 10
|
|
out_intens = None
|
|
already_inited = False
|
|
for i in range(integrate):
|
|
mask_misk = np.ones((LEN, LEN))
|
|
ind = np.arange(mask_misk.size)
|
|
np.random.shuffle(ind)
|
|
mask_misk[np.unravel_index(ind[:800], (LEN, LEN))] = 0
|
|
|
|
si.apply_mask(lat.parse_mask(mask_misk))
|
|
si.gaussian(20)
|
|
intens = si.fft()
|
|
intens.clean()
|
|
if not already_inited:
|
|
print("Init")
|
|
rect = Rect_Evaluator(lat.get_spots())
|
|
rect.generate_mask(intens, merge=True)
|
|
out_intens = intens
|
|
already_inited = True
|
|
else:
|
|
out_intens.intens += intens.intens
|
|
out_intens = intens
|
|
rect.purge(intens)
|
|
plt.figure()
|
|
plot.plot_fft(intens, ax_log=plt.gca())
|
|
pdf = sfft.fft2(intens.intens)
|
|
pdf = sfft.fftshift(pdf)
|
|
plt.figure()
|
|
plt.imshow(np.abs(pdf))
|
|
|
|
|
|
def random(seed):
|
|
np.random.seed(seed)
|
|
LEN = 40
|
|
lat = VO2_Lattice(LEN, LEN)
|
|
maske = np.zeros((LEN, LEN))
|
|
ind = np.arange(LEN * LEN)
|
|
np.random.shuffle(ind)
|
|
rect = Rect_Evaluator(lat.get_spots())
|
|
|
|
out_rect = [[] for x in range(4)]
|
|
percentage = []
|
|
weighted_percentage = []
|
|
counter = 0
|
|
si = SpinImage(lat.get_phases())
|
|
already_inited = False
|
|
for i in tqdm.tqdm(ind):
|
|
maske[np.unravel_index(i, (LEN, LEN))] = True
|
|
counter += 1
|
|
if np.mod(counter, 100) != 0 and not i == ind[-1]:
|
|
continue
|
|
|
|
si.apply_mask(lat.parse_mask(maske))
|
|
si.gaussian(20)
|
|
|
|
intens = si.fft()
|
|
if not already_inited:
|
|
rect.generate_mask(intens, merge=True)
|
|
already_inited = True
|
|
|
|
ir, vr = rect.extract(intens)
|
|
for lis, val in zip(out_rect, vr):
|
|
lis.append(val)
|
|
percentage.append(np.sum(maske))
|
|
[p1, p2] = si.get_intens(lat.parse_mask(maske))
|
|
weighted_percentage.append(p1/(p1+p2))
|
|
|
|
percentage = np.array(percentage)
|
|
weighted_percentage = np.array(weighted_percentage)
|
|
percentage /= np.max(percentage)
|
|
|
|
np.savez(f"random_rect_{seed}.npz",
|
|
w_percentage=weighted_percentage, percentage=percentage, out_1=out_rect[0],
|
|
out_2=out_rect[1], out_3=out_rect[2], out_4=out_rect[3])
|
|
|
|
|
|
def sample_index(p):
|
|
i = np.random.choice(np.arange(p.size), p=p.ravel())
|
|
return np.unravel_index(i, p.shape)
|
|
|
|
|
|
def ising(seed, temp=0.5):
|
|
np.random.seed(seed)
|
|
LEN = 40
|
|
lat = VO2_Lattice(LEN, LEN)
|
|
maske = np.zeros((LEN, LEN))
|
|
rect = Rect_Evaluator(lat.get_spots())
|
|
|
|
out_rect = [[] for x in range(4)]
|
|
percentage = []
|
|
weighted_percentage = []
|
|
counter = 0
|
|
si = SpinImage(lat.get_phases())
|
|
already_inited = False
|
|
for i in tqdm.tqdm(range(LEN*LEN)):
|
|
probability = np.roll(maske, 1, axis=0).astype(float)
|
|
probability += np.roll(maske, -1, axis=0).astype(float)
|
|
probability += np.roll(maske, 1, axis=1).astype(float)
|
|
probability += np.roll(maske, -1, axis=1).astype(float)
|
|
|
|
probability = np.exp(probability/temp)
|
|
probability[maske > 0] = 0
|
|
probability /= np.sum(probability)
|
|
maske[sample_index(probability)] = True
|
|
counter += 1
|
|
if np.mod(counter, 100) != 0:
|
|
continue
|
|
|
|
si.apply_mask(lat.parse_mask(maske))
|
|
si.gaussian(20)
|
|
|
|
intens = si.fft()
|
|
if not already_inited:
|
|
rect.generate_mask(intens, merge=True)
|
|
already_inited = True
|
|
|
|
ir, vr = rect.extract(intens)
|
|
for lis, val in zip(out_rect, vr):
|
|
lis.append(val)
|
|
percentage.append(np.sum(maske))
|
|
[p1, p2] = si.get_intens(lat.parse_mask(maske))
|
|
weighted_percentage.append(p1/(p1+p2))
|
|
|
|
percentage = np.array(percentage)
|
|
weighted_percentage = np.array(weighted_percentage)
|
|
percentage /= np.max(percentage)
|
|
|
|
np.savez(f"ising_{temp}_rect_{seed}.npz",
|
|
w_percentage=weighted_percentage, percentage=percentage, out_1=out_rect[0],
|
|
out_2=out_rect[1], out_3=out_rect[2], out_4=out_rect[3])
|
|
|
|
|
|
def runner():
|
|
np.random.seed(1234)
|
|
seeds = np.random.randint(0, 10000, 200)
|
|
idx = int(sys.argv[1])
|
|
if idx < 0:
|
|
return
|
|
if idx >= seeds.size:
|
|
return
|
|
seed = seeds[idx]
|
|
random(seed)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
np.random.seed(1234)
|
|
runner()
|
|
# test_me()
|
|
# test_square()
|
|
# test_mixed()
|
|
# plt.show()
|
|
# random(1234)
|
|
# ising(1234)
|
|
# test_pdf()
|
|
# plt.show()
|
|
# exit()
|
|
# for i in np.random.randint(0, 10000, 5):
|
|
# random(i)
|
|
# ising(i, 0.5)
|
|
# ising(i, 1.0)
|
|
# ising(i, 1.5)
|
|
|
|
# plt.show()
|