/
snapshot.jl
96 lines (84 loc) · 2.9 KB
/
snapshot.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
export gen_snapshot!, gensave_snapshot!, load_snapshot
@doc """
gen_snapshot!(model, T, [N=1])
generate and return `N` snapshots (spin configuration).
# Arguments
* `model::Model` : model
* `T::Real` : temperature
* `N::Integer=1` : the number of snapshots to be generated
* `MCS::Integer=8192` : the number of Monte Carlo steps followed by snapshot generation
"""
function gen_snapshot!(model::Model, T::Real; MCS::Integer=8192)
for m in 1:MCS
SW_update!(model, T)
end
return copy(model.spins)
end
function gen_snapshot!(model::Model, T::Real, N::Integer; MCS::Integer=8192)
nsites = numsites(model.lat)
X = zeros(nsites, N)
for n in 1:N
spins[:,n] = gen_snapshot!(model, T, MCS=MCS)
end
return X
end
@doc """
gensave_snapshot!(io, model, T, [N=1])
gensave_snapshot!(filename, model, T, [N=1])
generate and write `N` snapshots into `io` or `filename`.
# Arguments
* `io::IO` : output stream where snapshots will be written
* `filename::String` : the name of file where snapshots will be written
* `model::Model` : model to be simulated
* `T::Real` : temperature
* `N::Integer=1` : the number of snapshots to be generated
* `MCS::Integer=1` : the number of Monte Carlo steps followed by snapshot generation
* `sep::String=" "` : field separator of snapshot
"""
function gensave_snapshot!(io::IO, model::Model, T::Real, N::Integer=1; MCS::Integer=8192, sep::String=" ")
nsites = numsites(model.lat)
for n in 1:N
for m in 1:MCS
SW_update!(model, T)
end
spins = model.spins
print(io, spins[1])
for s in 2:nsites
print(io, sep, spins[s])
end
println(io)
end
end
function gensave_snapshot!(filename::String, model::Model, T::Real, N::Integer=1; MCS::Integer=8192, sep::String=" ", append::Bool=false)
c = ifelse(append, "a", "w")
open(filename, c) do io
gensave_snapshot!(io, model, T, N, MCS=MCS, sep=sep)
end
end
function load_snapshot_impl(io::IO, splitter)
for line in eachline(io)
if ismatch(r"^\s*($|#)", line)
continue
end
produce(float(split(strip(line), splitter)))
end
end
function load_snapshot_impl(filename::String, splitter)
io = open(filename)
for line in eachline(io)
if ismatch(r"^\s*($|#)", line)
continue
end
produce(float(split(strip(line), splitter)))
end
close(io)
end
const _default_delims = [' ','\t','\n','\v','\f','\r']
@doc """
load_snapshot(io, splitter)
load_snapshot(filename, splitter)
load snapshot from `io` or `filename`.
`splitter` is the set of field splitter and will be passed `Base.split` as the second argument (see the doc of `Base.split`).
"""
load_snapshot(io::IO, splitter = _default_delims) = @task load_snapshot_impl(io, splitter)
load_snapshot(filename::String, splitter = _default_delims) = @task load_snapshot_impl(filename, splitter)