-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxy.py
82 lines (68 loc) · 4.12 KB
/
xy.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
# 2D classical XY model
import torch
import math
def energy(sample, ham, lattice, boundary):
sample=sample*2*math.pi # angular
energy = xyenergy(sample, ham, lattice, boundary)
vortices = vortex(sample, boundary)
return energy, vortices
def xyenergy(sample, ham, lattice, boundary):
# calculate the energy
term = torch.cos(sample[:, :, 1:, :] - sample[:, :, :-1, :])
term = term.sum(dim=(1, 2, 3))
output = term
term = torch.cos(sample[:, :, :, 1:] - sample[:, :, :, :-1])
term = term.sum(dim=(1, 2, 3))
output += term
if lattice == 'tri':
term = torch.cos(sample[:, :, 1:, 1:] - sample[:, :, :-1, :-1])
term = term.sum(dim=(1, 2, 3))
output += term
if boundary == 'periodic':
term = torch.cos(sample[:, :, 0, :] - sample[:, :, -1, :])
term = term.sum(dim=(1, 2))
output += term
term = torch.cos(sample[:, :, :, 0] - sample[:, :, :, -1])
term = term.sum(dim=(1, 2))
output += term
if lattice == 'tri':
term = torch.cos(sample[:, :, 0, 1:] - sample[:, :, -1, :-1])
term = term.sum(dim=(1, 2))
output += term
term = torch.cos(sample[:, :, 1:, 0] - sample[:, :, :-1, -1])
term = term.sum(dim=(1, 2))
output += term
term = torch.cos(sample[:, :, 0, 0] - sample[:, :, -1, -1])
term = term.sum(dim=1)
output += term
if ham == 'fm':
output *= -1
return output
def vortex(sample, boundary):
# count the number of vortices
theta=torch.fmod(sample[:, :, 1:, :-1] - sample[:, :, :-1, :-1],math.pi)-(sample[:, :, 1:, :-1] - sample[:, :, :-1, :-1])//math.pi*math.pi
theta+=torch.fmod(sample[:, :, :-1, :-1]-sample[:, :, :-1, 1:],math.pi)-(sample[:, :, :-1, :-1]-sample[:, :, :-1, 1:])//math.pi*math.pi
theta+=torch.fmod(sample[:, :, :-1, 1:] - sample[:, :, 1:, 1:],math.pi)-(sample[:, :, :-1, 1:] - sample[:, :, 1:, 1:])//math.pi*math.pi
theta+=torch.fmod(sample[:, :, 1:, 1:]-sample[:, :, 1:, :-1],math.pi)-(sample[:, :, 1:, 1:]-sample[:, :, 1:, :-1])//math.pi*math.pi
vortices=(theta.abs()/2/math.pi).sum(dim=(1, 2, 3))
if boundary == 'periodic':
theta1=torch.fmod(sample[:, :, 1:, -1]-sample[:, :, :-1, -1],math.pi)-(sample[:, :, 1:, -1]-sample[:, :, :-1, -1])//math.pi*math.pi
theta1+=torch.fmod(sample[:, :, :-1, -1]-sample[:, :, :-1, 0],math.pi)-(sample[:, :, :-1, -1]-sample[:, :, :-1, 0])//math.pi*math.pi
theta1+=torch.fmod(sample[:, :, :-1, 0]-sample[:, :, 1:, 0],math.pi)-(sample[:, :, :-1, 0]-sample[:, :, 1:, 0])//math.pi*math.pi
theta1+=torch.fmod(sample[:, :, 1:, 0]-sample[:, :, 1:, -1],math.pi)-(sample[:, :, 1:, 0]-sample[:, :, 1:, -1])//math.pi*math.pi
vortices+=(theta1.abs()/2/math.pi).sum(dim=(1, 2))
# calculate the transverse
theta2=torch.fmod(sample[:, :, 0, :-1]-sample[:, :, -1, :-1],math.pi)-(sample[:, :, 0, :-1]-sample[:, :, -1, :-1])//math.pi*math.pi
theta2+=torch.fmod(sample[:, :, -1, :-1]-sample[:, :, -1, 1:],math.pi)-(sample[:, :, -1, :-1]-sample[:, :, -1, 1:])//math.pi*math.pi
theta2+=torch.fmod(sample[:, :, -1, 1:]-sample[:, :, 0, 1:],math.pi)-(sample[:, :, -1, 1:]-sample[:, :, 0, 1:])//math.pi*math.pi
theta2+=torch.fmod(sample[:, :, 0, 1:]-sample[:, :, 0, :-1],math.pi)-(sample[:, :, 0, 1:]-sample[:, :, 0, :-1])//math.pi*math.pi
vortices+=(theta2.abs()/2/math.pi).sum(dim=(1, 2))
# calculate the longitudinal
theta3=torch.fmod(sample[:, :, 0, -1]-sample[:, :, -1, -1],math.pi)-(sample[:, :, 0, -1]-sample[:, :, -1, -1])//math.pi*math.pi
theta3+=torch.fmod(sample[:, :, -1, -1]-sample[:, :, -1, 0],math.pi)-(sample[:, :, -1, -1]-sample[:, :, -1, 0])//math.pi*math.pi
theta3+=torch.fmod(sample[:, :, -1, 0]-sample[:, :, 0, 0],math.pi)-(sample[:, :, -1, 0]-sample[:, :, 0, 0])//math.pi*math.pi
theta3+=torch.fmod(sample[:, :, 0, 0]-sample[:, :, 0, -1],math.pi)-(sample[:, :, 0, 0]-sample[:, :, 0, -1])//math.pi*math.pi
vortices+=(theta3.abs()/2/math.pi).sum(dim=1)
# calculate the corner
vortices=vortices/2
return vortices