forked from jcosborn/qhmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Field.lua
215 lines (198 loc) · 5.31 KB
/
Field.lua
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
require 'Util'
local fieldmt = {}
fieldmt._type = "Field"
fieldmt.__index = fieldmt
local momentummap = -- momentum lie group for given gauge group
{ GL="GL", SL="TGL", U="AH", SU="TAH", R="R", SR="TR", O="AS", SO="TAS" }
local groupNum = { GL=0, U=1, H=2, AH=3, SL=4, SU=5, SH=6, SAH=7, TGL=8,
TU=9, TH=10, TAH=11, TSL=12, TSU=13, TSH=14, TSAH=15 }
function Field(lattice, opts)
local f = {}
f.lattice = lattice
f.precision = lattice.defaultPrecision
f.nupdate = 0
f.wantForce = true
--f.parents = nil -- hash of [parent]=nupdate
--f.filename = nil
--f.metadata = nil
if opts.kind == "gauge" then
f.group = lattice.defaultGroup
f.nc = lattice.defaultNc
end
tableCopyTo(f, opts)
if f.kind == "gauge" then
if f.momentum then
--f.field = qopqdp.force(f.precision, f.nc, lattice.qdplat)
f.field = qopqdp.gauge(f.precision, f.nc, lattice.qdplat)
f.name = "M" .. f.group .. f.nc
else
f.field = qopqdp.gauge(f.precision, f.nc, lattice.qdplat)
f.name = "GF" .. f.group .. f.nc
end
else
--f.name = "F" .. opts.kind
error(string.format("unknown Field kind: %s\n", f.kind))
end
clearStats(f, "update")
clearStats(f, "transform")
return setmetatable(f,fieldmt)
end
function fieldmt:__tostring()
local s = {}
tostringRecurse(s, self, {self.lattice})
if s.done then return "" end
s[#s+1] = self.name
s[#s+1] = " = "
s[#s+1] = self.lattice.name
s[#s+1] = ":Field {\n"
s[#s+1] = string.format(" group = \"%s\",\n",self.group)
s[#s+1] = string.format(" nc = %g\n",self.nc)
s[#s+1] = "}\n"
return table.concat(s)
end
function SetTransform(newfs, oldfs, funcs, args)
for i=1,#newfs do
local nf = newfs[i]
if not nf.transform then nf.transform = {} end
local t = nf.transform
t.newfs = newfs
t.oldfs = oldfs
t.funcs = funcs
t.args = args
nf.parents = {}
for j=1,#oldfs do
local p = oldfs[j]
nf.parents[p] = -1
end
end
end
function fieldmt:GetUpdateNumber()
return self.nupdate
end
function fieldmt:IncrementUpdateNumber()
self.nupdate = self.nupdate + 1
end
-- group, momentum group
function fieldmt:Momentum()
local fgroup = momentummap[self.group]
return Field(self.lattice, {kind=self.kind,group=fgroup,nc=self.nc,
precision=self.precision,momentum=true})
end
function fieldmt:GetField()
-- check if parent fields have been updated
if self.parents then
local update = false
for p,n in pairs(self.parents) do
if p.nupdate > n then
update = true
p.nupdate = n
end
end
if update then
local t = self.transform
local t0 = clock()
t.funcs.transform(t.newfs, t.oldfs, t.args)
updateStats(self, "transform", {seconds=(clock()-t0)})
self.nupdate = self.nupdate + 1
end
end
return self.field
end
function fieldmt:SetField(f)
self.field:set(f)
self.nupdate = self.nupdate + 1
end
function fieldmt:CopyField()
local f = self:GetField()
local fc = f:copy()
return fc
end
function fieldmt:Clone()
local f = self:GetField()
local fn = tableCopy(self)
fn.field = qopqdp.gauge(fn.precision, fn.nc, fn.lattice.qdplat)
fn.field:set(f)
return setmetatable(fn,fieldmt)
end
function fieldmt:Set(opts, ...)
if type(opts)=="table" then
local t = opts._type
if t=="Field" then
self:SetField(opts:GetField())
elseif t==nil then
tableCopyTo(self, opts)
else
error("unknown type for opts: %s\n", t)
end
elseif type(opts)=="string" then -- don't remember what this was for
self.field[opts](self.field, ...)
self.nupdate = self.nupdate + 1
else
error("unknown type for opts: %s\n", type(opts))
end
end
function fieldmt:Load(filename)
local fmd,rmd = self.field:load(filename)
self.filename = filename
self.metadata = {file=fmd,record=rmd}
self.nupdate = self.nupdate + 1
end
function fieldmt:Save(filename, opts)
if not self.metadata then
self.metadata = {}
end
if not self.metadata.file then
self.metadata.file =
"<?xml version=\"1.0\"?>\n<note>generated by qhmc</note>\n"
end
if not self.metadata.record then
self.metadata.record =
"<?xml version=\"1.0\"?>\n<note>generated by qhmc</note>\n"
end
local precision = (opts and opts.precision) or 'F'
local f = self:GetField()
f:save(filename,self.metadata.file,self.metadata.record,precision)
self.filename = filename
end
function fieldmt:Norm2()
local f = self:GetField()
local s,t = 0,{}
for i=1,#f do
local fn = f(i):norm2()
s = s + fn
t[i] = fn
end
return s, t
end
function fieldmt:Random(var) -- ... for variance
local f = self.field
local v
if type(var) ~= "table" then v = var or 1 end
if self.group == "TAH" then
f:randomTAH()
if var then
for i=1,#f do
local fi = f(i)
fi:combine({fi},{v or var[i]})
end
end
else
local gr = qopqdp.groupNumber(self.group) or 0
for i=1,#f do
local fi = f(i)
fi:random(v or var[i])
fi:makeGroup(gr)
end
end
self.nupdate = self.nupdate + 1
end
function fieldmt:Update(momentum, eps)
local f = self:GetField()
local m = momentum:GetField()
local t0 = clock()
--printf("Field:update: %s\t%g\n", tostring(momentum), eps)
-- for gauge: exponential, for momentum: add
f:update(m, eps)
updateStats(self, "update", {seconds=(clock()-t0)})
self.nupdate = self.nupdate + 1
end