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
/
Analysis.jl
700 lines (557 loc) · 20 KB
/
Analysis.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# Analysis functions
__precompile__()
module Analysis
push!(LOAD_PATH, pwd())
using FileIO, Images, LaTeXStrings, Dates, DataFrames, CSV
import Plots as pl
using IsingGraphs
using SquareAdj
using Observables
export dataToDF, tempSweep, MPlot, sampleCorrPeriodic, sampleCorrPeriodicDefects, corrFuncXY, dfMPlot, dataFolderNow, csvToDF, corrPlotDF, corrPlotDFSlices, corrPlotTRange
mutable struct AInt32
@atomic x::Int32
end
mutable struct AFloat32
@atomic x::Float32
end
"""
User functions
"""
# Does analysis over multiple temperatures.
# Analyzes magnetization and correlation length.
# Usage: Goes from current temperature of simulation, to TF, in stepsizes of TStep.
# Every step, a number of datapoints (dpoints) are recorded, with inervals between them of dpointwait
# In between Temperatures there is an optional wait time of stepwait, for equilibration
# There is an initial wait time of equiwait for equilibration
function tempSweep(g, TIs, M_array; TI = TIs[], TF = 13, TStep = 0.5, dpoints = 6, dpointwait = 5, stepwait = 0, equiwait = 0, saveImg = false, img = Ref([]), corrF = sampleCorrPeriodic, analysisRunning = Observable(true), savelast = true, absvalcorrplot = false)
""" Make folder """
foldername = dataFolderNow("Tempsweep")
println("Starting temperature sweep")
println("Parameters TI: $TI,TF: $TF, TStep: $TStep, dpoints: $dpoints, dpointwait: $dpointwait, stepwait: $stepwait, equiwait: $equiwait, saveImg: $saveImg")
first = true
if TF < TI
TStep *= -1
end
TRange = TI:TStep:TF
# DataFrames
mdf = DataFrame()
cldf = DataFrame()
for (tidx, T) in enumerate(TRange)
println("Gathering data for temperature $T, $dpoints data points in intervals of $dpointwait seconds")
waittime = length(T:TStep:TF)*(dpoints*dpointwait + stepwait)
TIs[] = T
if first # If first run, wait equiwait seconds for equibrilation
println("The sweep will take approximately $(equiwait + waittime) seconds")
if equiwait != 0
println("Waiting $equiwait seconds for equilibration")
sleep(equiwait)
end
first=false
else # Else wait in between temperature steps
# User feedback of remaining time
println("Approximately $waittime seconds remaining")
println("Now waiting $stepwait seconds in between temperatures")
sleep(stepwait)
end
# Gather Datapoints
for point in 1:dpoints
if !(analysisRunning[])
println("Interrupted analysis")
return
end
tpointi = time()
(lVec,corrVec) = corrF(g)
cldf = vcat(cldf, corrToDF((lVec,corrVec) , point, TIs[]) )
mdf = vcat(mdf, MToDF(last(M_array[]),point, TIs[]) )
if saveImg && (!savelast || point == dpoints)
# Image of ising
save(File{format"PNG"}("$(foldername)Ising $tidx d$point T$T.PNG"), img[])
# Image of correlation plot
plotCorr(lVec,corrVec, foldername, tidx, point, T)
end
tpointf = time()
dtpoint = tpointf-tpointi
println("Datapoint $point took $dtpoint seconds")
sleep(max(dpointwait-dtpoint,0))
end
end
analysisRunning[] = false
try
if saveImg
dfMPlot(mdf, foldername)
end
catch(error)
error(error)
finally
CSV.write("$(foldername)Ising 0 CorrData.csv", cldf)
CSV.write("$(foldername)Ising 0 MData.csv", mdf)
end
println("Temperature sweep done!")
end
function plotCorr(lVec,corrVec, foldername, tidx, point, T)
corrPlot = pl.plot(lVec,corrVec, xlabel = "Length", label = L"C(L)")
Tstring = replace("$T", '.' => ',')
pl.savefig(corrPlot,"$(foldername)Ising Corrplot $tidx d$point T$Tstring")
end
""" Correlation length calculations """
rthetas = 2*pi .* rand(10^7) # Saves random angles to save computation time
# Takes a random number of pairs for every length to calculate correlation length function
# This only works well with defects.
function sampleCorrPeriodic(g::IsingGraph, Lstep::Float16 = Float16(.5), lStart::Integer = Int8(1), lEnd::Integer = Int16(256), npairs::Integer = Int16(10000) )
function sigToIJ(sig, L)
return (L*cos(sig),L*sin(sig))
end
function sampleIdx2(idx1,L,rtheta)
ij = idxToCoord(idx1,g.N)
dij = Int32.(round.(sigToIJ(rtheta,L)))
idx2 = coordToIdx(latmod.((ij.+dij),g.N),g.N)
return idx2
end
theta_i = rand([1:length(rthetas);])
avgsum = (sum(g.state)/g.size)^2
lVec = [lStart:Lstep:lEnd;]
corrVec = Vector{Float32}(undef,length(lVec))
# Sample all startidx to be used
# Slight bit faster to do it this way than to sample it every time in the loop
idx1s = rand(g.d.aliveList,length(lVec)*npairs)
# Index of above vector
idx1idx = 1
# Iterate over all lengths to be checked
for (lidx,L) in enumerate(lVec)
sumprod = 0 #Track the sum of products sig_i*sig_j
for _ in 1:npairs
idx1 = idx1s[idx1idx]
rtheta = rthetas[(theta_i -1) % length(rthetas)+1]
idx2 = sampleIdx2(idx1,L,rtheta)
sumprod += g.state[idx1]*g.state[idx2]
theta_i += 1 # Sample next random angle
idx1idx += 1 # Sample next random startidx
end
# println((sum(g.state)/g.N)^2)
# println(avgsum1*avgsum2/(npairs^2))
corrVec[lidx] = sumprod/npairs - avgsum
end
return (lVec,corrVec)
end
# Sample correlation length function when there are defects.
function sampleCorrPeriodicDefects(g::IsingGraph, lend = -floor(-sqrt(2)*g.N/2), binsize = .5, npairs::Integer = Int64(round(lend/binsize * 40000)); sig = 1000, periodic = true)
function torusDist(i1,j1,i2,j2, N)
dy = abs(i2-i1)
dx = abs(j2-j1)
if dy > .5*g.N
dy = g.N - dy
end
if dx > .5*g.N
dx = g.N - dx
end
return sqrt(dx^2+dy^2)
end
if length(g.d.aliveList) <= 2
error("Too little alive spins to do analysis")
return
end
idxs1 = rand(g.d.aliveList,npairs)
idxs2 = rand(g.d.aliveList,npairs)
lbins = zeros(length(1:binsize:lend))
lVec = [1:binsize:lend;]
corrbins = zeros(length(lVec))
prodavg = sum(g.state[g.d.aliveList])/length(g.d.aliveList)
for sample in 1:npairs
idx1 = idxs1[sample]
idx2 = idxs2[sample]
while idx1 == idx2
idx2 = rand(g.d.aliveList)
end
(i1,j1) = idxToCoord(idx1,g.N)
(i2,j2) = idxToCoord(idx2,g.N)
if periodic
l = torusDist(i1,j1,i2,j2,g.N )
else
l = sqrt((i1-i2)^2+(j1-j2)^2)
end
# println("1 $idx1, 2 $idx2")
# println("1 $((i1,j1)), 2 $((i2,j2)) ")
# println("L $l")
binidx = Int32(floor((l-1)/binsize)+1)
lbins[binidx] += 1
corrbins[binidx] += g.state[idx1]*g.state[idx2]
end
remaining_idxs = []
for (startidx,pairs_sampled) in enumerate(lbins)
if pairs_sampled >= sig
append!(remaining_idxs, startidx)
end
end
corrVec = (corrbins[remaining_idxs] ./ lbins[remaining_idxs]) .- prodavg
lVec = lVec[remaining_idxs]
return (lVec,corrVec)
end
"""Correlation Length Data"""
# Save correlation date (lVec,corrVec) to dataframe
function corrToDF((lVec,corrVec), dpoint::Integer = Int8(1), T::Real = Float16(1))
return DataFrame(L = lVec, Corr = corrVec, D = dpoint, T = T)
end
# Not used currently, used to fit correleation length data to a function f
function fitCorrl(dat,dom_end, f, params...)
dom = Domain(1.:dom_end)
data = Measures(Vector{Float64}(dat[1:dom_end]),0.)
model = Model(:comp1 => FuncWrap(f,params...))
prepare!(model,dom, :comp1)
return fit!(model,data)
end
""" PLOTTING """
function MToDF(M, dpoint::Integer = Int8(1), T::Real = Float16(1))
return DataFrame(M = M, D = dpoint, T = T)
end
# Plot a magnetization plot from dataframe
function dfMPlot(mdf::DataFrame, foldername::String, filename::String = "")
""" DF FORMAT """
""" M : D : T """
# All magnetizations
ms = mdf[:,1]
# All Temperatures
ts = mdf[:,3]
slices = []
lastt = ts[1]
lasti = 1
for startidx in 2:length(ts)
if !(ts[startidx] == lastt)
append!(slices,[lasti:(startidx-1)])
lasti=startidx
end
lastt = ts[startidx]
end
append!(slices, [lasti:length(ts)])
avgM = Vector{Float32}(undef,length(slices))
newTs = Vector{Float32}(undef,length(slices))
for (startidx,slice) in enumerate(slices)
avgM[startidx] = sum(ms[slice])/length(slice)
newTs[startidx] = ts[first(slice)]
end
if newTs[1] < newTs[end]
if avgM[1] < 0
avgM .*= -1
end
else
if avgM[end] < 0
avgM .*= -1
end
end
mPlot = pl.plot(newTs,avgM, xlabel = "T", ylabel="M", label=false)
filename = replace(filename, '.' => ',')
pl.savefig(mPlot,"$(foldername)Ising 0 Mplot $filename")
end
dfMPlot(filename::String, foldername::String, savefilename::String) = let df = csvToDF(filename); dfMPlot(df,foldername,savefilename) end
corrPlotDF(filename::String , dpoint, temp; savefolder::String = "", absval = false) = let cdf = csvToDF(filename); corrPlotDF(cdf, dpoint, temp; savefolder, absval) end
function corrPlotDF(cdf::DataFrame, dpoint, temp; savefolder::String = "", absval = false)
Larray = @view cdf[:,1]
corrArray = @view cdf[:,2]
dpointArray = @view cdf[:,3]
temparray = @view cdf[:,4]
startidx = 0
for (tidx,T) in enumerate(temparray)
if T == temp
startidx += tidx
break
end
if tidx == length(temparray)
error("Didn't find temperature")
end
end
for (didx,point) in enumerate(@view dpointArray[startidx:end])
if point == dpoint
startidx += didx - 1
break
end
if didx == length(@view dpointArray[startidx:end]) || temparray[startidx] != temp
error("Didn't find datapoint for temp")
end
end
endidx = startidx
last_l = Larray[startidx]
for (lidx,llength) in enumerate(@view Larray[(startidx+1):end])
if llength < last_l
endidx += lidx - 1
break
end
if lidx == length(@view Larray[(startidx+1):end])
endidx += lidx
end
last_l = llength
end
println("Start startidx $startidx")
println("End endidx $endidx")
x = @view Larray[startidx:endidx]
y = @view corrArray[startidx:endidx]
if absval
y = abs.(y)
end
if !absval
ylabel = L"C(L)"
else
ylabel = L"|C(L)|"
end
cplot = pl.plot(x,y,xlabel = "Length", ylabel=ylabel, label = "T = $temp" )
Tstring = replace("$temp", '.' => ',')
pl.savefig(cplot,"$(savefolder)Ising Cplot T=$Tstring d$dpoint")
end
function corrPlotTRange(filename::String, dpoint, Ts; savefolder = "Data/Images/Correlation/", absval = false)
trymakefolder(savefolder)
df = csvToDF(filename)
for T in Ts
corrPlotDF(df, dpoint, T; savefolder, absval)
end
end
"""General data"""
# Determine amount of datapoints per temperature from dataframe data (if homogeneous over temps)
function detDPoints(dat)
dpoint = 0
lastt = dat[:,1][1]
for temp in dat[:,1]
if temp == lastt
dpoint += 1
else
return dpoint
end
end
return dpoint
end
# Input the temperature sweep data into a dataframe
function dataToDF(tsData, lMax = length(tsData[2][1]))
return DataFrame(Temperature = tsData[1], Correlation_Function = [corrLXY[1:lMax] for corrLXY in tsData[2]], Magnetization = tsData[3] )
end
# Read CSV and outputs dataframe
csvToDF(filename) = DataFrame(CSV.File(filename))
""" Helper Functions"""
function insertShift(vec::Vector{T}, el::T) where T
newVec = Vector{T}(undef, length(vec))
newVec[1:(end-1)] = vec[2:end]
newVec[end] = el
return newVec
end
# Create folder in data folder with given name and return path
function dataFolderNow(dataString::String)
nowtime = "$(now())"[1:(end-7)]
try; mkdir(joinpath(dirname(Base.source_path()),"Data")); catch end
try; mkdir(joinpath(dirname(Base.source_path()), "Data", dataString)); catch end
try; mkdir(joinpath(dirname(Base.source_path()), "Data", dataString, "$nowtime")); catch end
return "Data/Tempsweep/$nowtime/"
end
function trymakefolder(foldername::String)
if foldername[end] == '/'
foldername = foldername[1:(end-1)]
end
try; mkdir(joinpath(dirname(Base.source_path()),foldername)); catch end
end
""" OLD STUFF """
# Sample random spins for correlation length, but make sure every pair is only sampled once
# Colissions are quite unlikely for smaller number of sampled pairs, making this redundant and slower
function sampleCorrPeriodicUnique(g::IsingGraph, Lstep::Float16, lStart::Int8 = 1, lEnd::Int16 = 256, npairs::Integer = 1000 )
function sigToIJ(sig, L)
return (L*cos(sig),L*sin(sig))
end
function sampleIdx2(idx1,L,rtheta)
ij = idxToCoord(idx1,g.N)
dij = Int32.(round.(sigToIJ(rtheta,L)))
idx2 = coordToIdx(latmod.((ij.+dij),g.N),g.N)
return idx2
end
theta_i = rand([1:length(rthetas);])
avgsum = (sum(g.state)/g.size)^2
lVec = [lStart:Lstep:lEnd;]
corrVec = Vector{Float32}(undef,length(lVec))
pairs = Set() # To check wether pair is already checked
# Iterate over all lengths to be checked
for (lidx,L) in enumerate(lVec)
pairs_done = 0
sumprod = 0 #Track the sum of products sig_i*sig_j
while pairs_done <= npairs
idx1 = rand(g.d.aliveList)
rtheta = rthetas[(theta_i -1) % length(rthetas)+1]
idx2 = sampleIdx2(idx1,L,rtheta)
if !((idx1,idx2) in pairs)
sumprod += g.state[idx1]*g.state[idx2]
pairs_done +=1
union!(pairs,(idx1,idx2))
else
continue
end
theta_i += 1 #sample next random angle
end
# println((sum(g.state)/g.N)^2)
# println(avgsum1*avgsum2/(npairs^2))
corrVec[lidx] = sumprod/npairs - avgsum
end
return (lVec,corrVec)
end
# Sweep the lattice to find x and y correlation data.
function corrLXY(g::IsingGraphs.IsingGraph, L)
avgprod = 0
prodavg1 = 0
prodavg2 = 0
Mprod = 0
M1 = 0
M2 = 0
# filter = [ #only do for spin pairs within matrix
# let (i1,j1) = idxToCoord(state,g.N), i2 = i1+L, j2 = j1+L
# i2 <= g.N && j2 <=g.N
# end
# for state in 1:g.size
# ]
# for state1 in g.state[filter]
for stateIdx in g.d.aliveList
state1 = g.state[stateIdx]
(i1,j1) = idxToCoord(stateIdx,g.N)
i2 = i1+L
j2 = j1+L
# Check if points are added
addedi2 = false
addedj2 = false
if i2 < g.N && !g.d.defectBools[coordToIdx(i2,j1,g.N)]
addedi2 = true
statey = g.state[coordToIdx(i2,j1,g.N)]
prodavg2 += statey
avgprod += state1*statey
Mprod += 1
M2 +=1
end
if j2 < g.N && !g.d.defectBools[coordToIdx(i1,j2,g.N)]
addedj2 = true
statex = g.state[coordToIdx(i1,j2,g.N)]
prodavg2 += statex
avgprod += state1*statex
Mprod += 1
M2 +=1
end
if addedi2 || addedj2
prodavg1 += state1
M1 += 1
end
end
return avgprod/Mprod-prodavg1*prodavg2/(M1*M2)
end
# Calculates the two points correlation function for different lengths and returns a vector with all the data
# Returned vector index corresponds to dinstance L``
function corrFuncXY(g::IsingGraphs.IsingGraph, plot = true)
corr::Vector{Float32} = []
x = [1:(g.N-2);]
for L in 1:(g.N-2)
append!(corr,corrLXY(g,L))
end
if plot
display(pl.plot(x,corr))
end
return corr
end
# Tries all pairs, way to expensive for larger grids
function corrFuncPeriodic(g::IsingGraph)
dict = Dict{Float32,Tuple{Float32,Int32}}()
for (idx1,state1) in enumerate(g.state)
for (idx2,state2) in enumerate(g.state)
(i1,j1) = idxToCoord(idx1,g.N)
(i2,j2) = idxToCoord(idx2,g.N)
L::Float32 = sqrt((i1-i2)^2+(j1-j2)^2)
if haskey(dict,L) == false
dict[L] = (0,0)
end
dict[L] = (dict[L][1] + state1*state2, dict[L][2]+1)
end
end
return dict
end
# Parse Correlation Length Data from string in DF
function parseCorrL(corr_dat)
corrls = []
for line in corr_dat
append!(corrls, [eval(Meta.parse(line))] )
end
return corrls
end
# Input dataframe and get correlation length data for all temps
function dfToCorrls(df)
corrls = df[:,2]
Ts = df[:,1]
corrls = parseCorrL(corrls)
return (Ts,corrls)
end
""" Old stuff """
# Aggregate all Magnetization measurements for a the same temperatures
function datMAvgT(dat,dpoints = detDPoints(dat))
temps = dat[:,1]
Ms = dat[:,3]
tempit = 1:dpoints:length(temps)
temps = temps[tempit]
Ms = [(sum( Ms[(1+(i-1)*dpoints):(i*dpoints)] )/length(tempit)) for i in 1:length(tempit)]
return (temps,Ms)
end
# Expand measurements in time
function datMExpandTime(dat,dpoints,dpointwait)
return
end
# Plots Correlation Length Plot from dataframe
# Needs the datapoint number and temperature to be plotted
# Set Absval = true if you want to plot absolute value of correlation length data
# Is a bit faster than the other method, but doesn't work for variable lengths of lvec and corrvec
corrPlotDFSlices(filename::String , dpoint, temp, savefolder::String, absval = false) = let cdf = csvToDF(filename); corrPlotDFSlices(cdf, dpoint, temp, savefolder::String, absval) end
function corrPlotDFSlices(cdf::DataFrame, dpoint, temp, savefolder::String, absval = false)
Larray = @view cdf[:,1]
corrArray = @view cdf[:,2]
dpointArray = @view cdf[:,3]
temparray = @view cdf[:,4]
l_blocksize = let _
len = 1
lastel = Larray[1]
for startidx in 2:length(Larray)
if !(Larray[startidx] < lastel)
len+=1
lastel = Larray[startidx]
else
break
end
end
len
end
dpoints = let _
len = 1
lastel = dpointArray[1]
for startidx in (1+l_blocksize):l_blocksize:length(dpointArray)
if !(dpointArray[startidx] < lastel)
len+=1
lastel = dpointArray[startidx]
else
break
end
end
len
end
tslices = 1:(dpoints*l_blocksize):length(temparray)
tidx = 0
for (startidx,T) in enumerate(@view temparray[tslices])
if T == temp
tidx = startidx
end
end
if tidx == 0
error("T not found")
return
end
startidx = 1+dpoints*l_blocksize*(tidx-1)+l_blocksize*(dpoint-1)
endidx = dpoints*l_blocksize*(tidx-1)+l_blocksize*(dpoint-1)+l_blocksize
println("T index $tidx")
println("Start startidx $startidx")
println("End startidx $endidx")
x = @view Larray[startidx:endidx]
y = @view corrArray[startidx:endidx]
if absval
y = abs.(y)
end
if !absval
ylabel = L"C(L)"
else
ylabel = L"|C(L)|"
end
cplot = pl.plot(x,y,xlabel = "Length", ylabel=ylabel, label = "T = $temp" )
Tstring = replace("$temp", '.' => ',')
pl.savefig(cplot,"$(savefolder)Ising Cplot T=$Tstring d$dpoint")
end
end