This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareAdj.jl
216 lines (170 loc) · 5.33 KB
/
SquareAdj.jl
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Stuff for initialization of adjacency matrix
"""
__precompile__()
module SquareAdj
push!(LOAD_PATH, pwd())
using WeightFuncs
export fillAdjList!, numEdges, latmod, initAdj, adjToMatrix
# Matrix Coordinates to vector Coordinates
@inline function coordToIdx(i, j, N)
return (i - 1) * N + j
end
# Insert coordinates as tuple
coordToIdx((i, j), N) = coordToIdx(i, j, N)
# Go from idx to lattice coordinates
@inline function idxToCoord(idx::Integer, N::Integer)
return ((idx - 1) ÷ N + 1, (idx - 1) % N + 1)
end
# Put a lattice index (i or j) back onto lattice by looping it around
function latmod(i, N)
if i < 1 || i > N
return i = mod((i - 1), N) + 1
end
return i
end
function adjEntry!(adj, N, idx, weightFunc=defaultIsing)
periodic = weightFunc.periodic
NN = weightFunc.NN
(i, j) = idxToCoord(idx, N)
inv = weightFunc.invoke
# Returns bool representing wether there is a connection
# Bases connection on periodic and NN
function doesConnect(i, j, icoup, jcoup, weight)
# If weight is zero, does no connections
if weight == 0.0
return false
end
if periodic
return !(icoup == i && jcoup == j)
else
return (!(icoup == i && jcoup == j) && (icoup > 0 && jcoup > 0 && icoup <= N && jcoup <= N))
end
end
function coupIdxFunc(icoup, jcoup)
if periodic
return coordToIdx(latmod(icoup, N), latmod(jcoup, N), N)
else
return coordToIdx(icoup, jcoup, N)
end
end
# Finds a weight for an index
function findWeight(idx, tupls)
for tupl in tupls
if tupl[1] == idx
return tupl[2]
end
end
end
entry = []
for icoup in (i-NN):(i+NN)
for jcoup in (j-NN):(j+NN)
dr = sqrt((i - icoup)^2 + (j - jcoup)^2)
weight = inv(dr, (i+icoup)/2, (j+jcoup)/2)
if doesConnect(i, j, icoup, jcoup, weight)
coup_idx = coupIdxFunc(icoup, jcoup)
# Undirected graph, so copy edge weight if already present
if idx < coup_idx
newWeight = weight
else
newWeight = findWeight(idx, adj[coup_idx])
end
append!(entry, [(coup_idx, newWeight)])
end
end
end
adj[idx] = entry
end
# Should also include function!!!!
# Init the adj list of g
function fillAdjList!(adj, N, weightFunc=defaultIsing)
for idx in 1:N*N
adjEntry!(adj, N, idx, weightFunc)
end
end
function adjToMatrix(adj)
N = length(adj)
matr = Matrix{Float32}(undef, N, N)
for (idx, tupls) in enumerate(adj)
for tupl in tupls
matr[idx, tupl[1]] = tupl[2]
end
end
return matr
end
""" New Implementation """
"""
Stuff for initialization of adjacency matrix
"""
""" Old functions """
# Input idx, gives all other indexes which it is coupled to. NN is how many nearest neighbors,
# can set periodic or not
# rfunc is distance function
function adjEntry(idx, N, NN=1, periodic=true, rfunc=dr -> 1 / dr^2)::Vector{Tuple{Int32,Float32}}
(i, j) = idxToCoord(idx, N)
if periodic
couple = [(coordToIdx(latmod(i2, N), latmod(j2, N), N), rfunc(sqrt((i - i2)^2 + (j - j2)^2))) for i2 in (i-NN):(i+NN), j2 in (j-NN):(j+NN) if (!(i2 == i && j2 == j) && (rfunc(sqrt((i - i2)^2 + (j - j2)^2)) != 0.0))]
else
couple = [(coordToIdx(i2, j2, N), rfunc(sqrt((i - i2)^2 + (j - j2)^2))) for i2 in (i-NN):(1+NN), j2 in (j-NN):(j+NN) if (!(i2 == i && j2 == j) && (i2 > 0 && j2 > 0 && i2 <= N && j2 <= N) && (rfunc(sqrt((i - i2)^2 + (j - j2)^2)) != 0.0))]
end
return couple
end
# Count number of edges in adjacency matrix
function numEdges(adj::Vector)
num = 0
for (in, outs) in enumerate(adj)
for out in outs
if in <= out
num += 1
end
end
end
return num
end
# Returns the indices of all the spins a particular spin is connected with
function coupleIndices(i, j, N)
# Left right up down
# Sets any of the above to false if spins are at the edge of lattice
# Representing that there are no spins to that side anymore
l, dr, u, d = (true, true, true, true)
if j == 1
l = false
end
if j == N
dr = false
end
if i == 1
u = false
end
if i == N
d = false
end
# Filters nearest neighbors that are outside of grid
"""lu u ru dr rd d dl l"""
idxs = [l && u, u, dr && u, dr, dr && d, d, d && l, l]
cn = (y, x) -> coordToIdx(y, x, N)
nn = [cn(i - 1, j - 1), cn(i - 1, j), cn(i - 1, j + 1), cn(i, j + 1), cn(i + 1, j + 1), cn(i + 1, j), cn(i + 1, j - 1), cn(i, j - 1)]
return nn[idxs]
end
# Fills adjacency list for a square grid
function fillAdjListOld!(adj, N)
size = N * N
@inbounds for idx in 1:size
adj[idx] = []
end
@inbounds for idx in 1:size
i, j = idxToCoord(idx, N)
adj[idx] = coupleIndices(i, j, N)
end
end
# Connection rule for lattice, not used right now
function conn_rule(i, j, N)
i1, j1 = idxToCoord(i, N)
i2, j2 = idxToCoord(j, N)
# If any of the distances is greater than 1, not nearest neighbor
if max(abs(i1 - i2), abs(j1 - j2)) > 1 || i == j
return False
end
return True
end
end