Skip to content

Commit

Permalink
improve fat7lderiv testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosborn committed Dec 17, 2024
1 parent 8729615 commit c112d75
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 49 deletions.
46 changes: 39 additions & 7 deletions src/gauge/fat7l.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ when isMainModule:
import qex
import physics/qcdTypes
import gauge
import strformat
qexInit()
#var defaultGaugeFile = "l88.scidac"
let defaultLat = @[8,8,8,8]
Expand All @@ -174,19 +175,50 @@ when isMainModule:

var info: PerfInfo
var coef: Fat7lCoefs
coef.oneLink = 1.0
coef.threeStaple = 1.0
coef.fiveStaple = 1.0
coef.sevenStaple = 1.0
coef.lepage = 1.0
var naik = 1.0

var fl = lo.newGauge()
var ll = lo.newGauge()
var g3 = g

proc checkfat1(name: string) =
makeImpLinks(fl, g, coef, info)
let p = fl.plaq
let c = coef.oneLink + 6*coef.threeStaple + 24*coef.fiveStaple +
48*coef.sevenStaple + 6*coef.lepage
let s = pow(c,4)/6.0
var e2 = 0.0
for x in p:
e2 += (x-s)^2
echo &"{name:12} relerr: {sqrt(e2)/s}"

coef.oneLink = 1.0
checkfat1("oneLink")
coef.oneLink = 0.0
coef.threeStaple = 1.0
checkfat1("threeStaple")
coef.threeStaple = 0.0
coef.fiveStaple = 1.0
checkfat1("fiveStaple")
coef.fiveStaple = 0.0
coef.sevenStaple = 1.0
checkfat1("sevenStaple")
coef.sevenStaple = 0.0
coef.lepage = 1.0
checkfat1("lepage")
coef.oneLink = 1.0
coef.threeStaple = 1.0
coef.fiveStaple = 1.0
coef.sevenStaple = 1.0
coef.lepage = 1.0
checkfat1("all")

makeImpLinks(fl, g, coef, ll, g3, naik, info)
echo g.plaq
echo fl.plaq
echo ll.plaq
#[
echo g.plaq
makeImpLinks(fl, g, coef, info)
makeImpLinks(fl, g, coef, ll, g3, naik, info)
echo fl.plaq
echo ll.plaq
Expand All @@ -195,5 +227,5 @@ when isMainModule:
echo pow(1.0+6.0+6.0*4.0,4)/6.0
echo pow(1.0+6.0+6.0*4.0+6.0*4.0*2.0,4)/6.0
echo pow(1.0+6.0+6.0*4.0+6.0*4.0*2.0+6.0,4)/6.0

]#
qexFinalize()
150 changes: 108 additions & 42 deletions src/gauge/fat7lderiv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ side_force(QDP_ColorMatrix *force, QDP_ColorMatrix *bot0, QDP_ColorMatrix *side0
}
]#

proc fat7lDeriv(deriv: auto, gauge: auto, mid: auto, coef: Fat7lCoefs,
proc fat7lDeriv*(deriv: auto, gauge: auto, mid: auto, coef: Fat7lCoefs,
llderiv: auto, llgauge: auto, llmid: auto, naik: float,
perf: var PerfInfo) =
tic("fat7lDeriv")
Expand Down Expand Up @@ -328,7 +328,23 @@ proc fat7lDeriv(deriv: auto, gauge: auto, mid: auto, coef: Fat7lCoefs,
perf.flops += nflops * gauge[0].l.localGeom.prod
perf.secs += getElapsedTime()

proc fat7lDeriv(deriv: auto, gauge: auto, mid: auto, coef: Fat7lCoefs,
proc fat7lDeriv*(
mid: auto,
deriv: auto,
gauge: auto,
coef: Fat7lCoefs,
llderiv: auto,
llgauge: auto,
naik: float,
perf: var PerfInfo
) =
var (fx,fxl) = (newOneOf(deriv),newOneOf(llderiv))
fat7lderiv(fx,gauge,deriv,coef,fxl,llgauge,llderiv,naik,perf)
threads:
for mu in 0..<mid.len:
for s in mid[mu]: mid[mu][s] := fx[mu][s] + fxl[mu][s]

proc fat7lDeriv*(deriv: auto, gauge: auto, mid: auto, coef: Fat7lCoefs,
perf: var PerfInfo) =
fat7lDeriv(deriv, gauge, mid, coef, deriv, gauge, mid, 0.0, perf)

Expand All @@ -339,54 +355,103 @@ when isMainModule:
qexInit()
#var defaultGaugeFile = "l88.scidac"
let defaultLat = @[8,8,8,8]
defaultSetup()
for mu in 0..<g.len: g[mu] := 1
#g.random
#defaultSetup()

var info: PerfInfo
var coef: Fat7lCoefs
var
(lo, g, r) = setupLattice(defaultLat)
fl = lo.newGauge()
fl2 = lo.newGauge()
ll = lo.newGauge()
ll2 = lo.newGauge()
ch = lo.newGauge()
g2 = lo.newGauge()
dg = lo.newGauge()
fd = lo.newGauge()
ld = lo.newGauge()
gc = GaugeActionCoeffs(plaq:1.0)
info: PerfInfo
coef: Fat7lCoefs
naik = 1.0
eps = floatParam("eps", 1e-5)
coef.oneLink = 1.0
coef.threeStaple = 0.0
coef.fiveStaple = 0.0
coef.sevenStaple = 0.0
coef.lepage = 0.0

#g.unit
g.random r
#dg.randomTAH r
dg.gaussian r
for mu in 0..<g.len:
for e in g[mu]:
dg[mu][e] *= eps
#g2[mu][e] := exp(dg[mu][e]) * g[mu][e]
g2[mu][e] := g[mu][e] + dg[mu][e]

proc check(da: float, tol: float) =
var ds = 0.0
for mu in 0..3:
ds += redot(dg[mu], fd[mu])
let r = (da-ds)/da
#echo "Checking ", name
echo &" da {da} ds {ds} rel {r}"
if abs(r) > tol*eps:
echo &"> ERROR rel error |{r}| > {tol*eps}"

proc checkG =
echo "Checking GaugeForce"
for mu in 0..<fd.len:
fd[mu] := 0
let a = gc.gaugeAction2(g)
let a2 = gc.gaugeAction2(g2)
gc.gaugeDeriv2(g, fd)
check(a2-a, 1)

proc checkS(name: string, tol: float) =
echo "Checking ", name
for mu in 0..<fd.len:
ch[mu] := 0
fd[mu] := 0
makeImpLinks(fl, g, coef, info)
info.clear
resetTimers()
makeImpLinks(fl, g, coef, info)
echo " ", info
makeImpLinks(fl2, g2, coef, info)
echo " ", info
let a = gc.gaugeAction2(fl)
let a2 = gc.gaugeAction2(fl2)
gc.gaugeDeriv2(fl, ch)
fat7lderiv(fd, g, ch, coef, info)
check(a2-a, tol)

checkG()

checkS("oneLink", 1)
coef.oneLink = 0.0
coef.threeStaple = 1.0
checkS("threeStaple", 2)
coef.threeStaple = 0.0
coef.fiveStaple = 1.0
checkS("fiveStaple", 2)
coef.fiveStaple = 0.0
coef.sevenStaple = 1.0
checkS("sevenStaple", 3)
coef.sevenStaple = 0.0
coef.lepage = 1.0
var naik = 1.0

var fl = lo.newGauge()
var fl2 = lo.newGauge()
var ll = lo.newGauge()
var ll2 = lo.newGauge()
var dfl = lo.newGauge()
var g2 = lo.newGauge()
var dg = lo.newGauge()
var fd = lo.newGauge()
var ld = lo.newGauge()
for mu in 0..<dg.len:
dg[mu] := 0.00001 * g[mu]
g2[mu] := g[mu] + dg[mu]
fd[mu] := 0

echo g.plaq
echo g2.plaq
makeImpLinks(fl, g, coef, info)
info.clear
resetTimers()
makeImpLinks(fl, g, coef, info)
echo info
makeImpLinks(fl2, g2, coef, info)
echo info
echo fl.plaq
echo fl2.plaq
checkS("Lepage", 1)
coef.oneLink = 1.0
coef.threeStaple = 1.0
coef.fiveStaple = 1.0
coef.sevenStaple = 1.0
coef.lepage = 0.0
checkS("not Lepage", 60)
coef.lepage = 1.0
checkS("all", 60)

fat7lderiv(fd, g, dg, coef, info)
echo info
for mu in 0..3:
dfl[mu] := fl2[mu] - fl[mu]
#echo dfl[mu].norm2
#echo fd[mu].norm2
dfl[mu] -= fd[mu]
echo dfl[mu].norm2

#[
for mu in 0..<dg.len:
fd[mu] := 0
ld[mu] := 0
Expand All @@ -403,6 +468,7 @@ when isMainModule:
dfl[mu] := ll2[mu] - ll[mu]
dfl[mu] -= ld[mu]
echo dfl[mu].norm2
]#

echoProf()
qexFinalize()
94 changes: 94 additions & 0 deletions src/gauge/hisqsmear.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import qex
import physics/[hisqLinks]
import gauge
import gauge/[fat7l,fat7lderiv]

export hisqLinks

proc newHISQ*(lepage: float = 0.0; naik: float = 1.0): HisqCoefs =
result = HisqCoefs(naik: -naik/24.0)
result.fat7first.setHisqFat7(lepage,0.0)
result.fat7second.setHisqFat7(2.0-lepage,naik)

proc smearGetForce*[T](
self: HisqCoefs;
u: T;
su,sul: T;
displayPerformance: bool = false
): proc(dsdu: var T; dsdsu,dsdsul: T) =
mixin projectU,projectUderiv
let
lo = u[0].l
fat7l1 = self.fat7first
fat7l2 = self.fat7second
naik = self.naik
var
v = newOneOf(u)
w = newOneOf(u)
info: PerfInfo

# Smear
v.makeImpLinks(u,fat7l1,info) # First fat7
threads: # Unitary projection
for mu in 0..<w.len:
for s in w[mu]: w[mu][s].projectU(v[mu][s])
makeImpLinks(su,w,fat7l2,sul,w,naik,info) # Second fat7

# Chain rule - retains a reference to u,su,sul
proc smearedForce(dsdu: var T; dsdsu,dsdsul: T) =
var
dsdx_dxdw = newOneOf(dsdu)
dsdx_dxdw_dwdv = newOneOf(dsdu)
dsdx_dxdw.fat7lderiv(dsdsu,su,fat7l2,dsdsul,sul,naik,info) # Second fat7
threads: # Unitary projection
for mu in 0..<dsdx_dxdw_dwdv.len:
for s in dsdx_dxdw_dwdv[mu]:
dsdx_dxdw_dwdv[mu][s].projectUderiv(w[mu][s],v[mu][s],dsdx_dxdw[mu][s])
dsdu.fat7lderiv(dsdx_dxdw_dwdv,u,fat7l1,info) # First fat7

if displayPerformance: echo $(info)
return smearedForce

if isMainModule:
qexInit()
let
defaultLat = @[8,8,8,8]
hisq = newHISQ()
defaultSetup()
var
sg = lo.newGauge()
sgl = lo.newGauge()
f = lo.newGauge()
ff = lo.newGauge()
ffl = lo.newGauge()
g2 = lo.newGauge()
sg2 = lo.newGauge()
sgl2 = lo.newGauge()
dg = lo.newGauge()
fd = lo.newGauge()
g.random
for mu in 0..<dg.len:
dg[mu] := 0.00001 * g[mu]
g2[mu] := g[mu] + dg[mu]
fd[mu] := 0

echo g.plaq
echo g2.plaq
#hisq.smear(g,sg,sgl)
var force = hisq.smearGetForce(g,sg,sgl)
f.force(dg,dg)
hisq.smear(g2,sg2,sgl2)

echo f.plaq
echo sg.plaq
echo sgl.plaq
echo "--"
echo sg.plaq
echo sgl.plaq
ff.gaugeForce(sg)
ffl.gaugeForce(sgl)
echo "--"
echo f.plaq
echo sg.plaq
echo sgl.plaq
qexFinalize()

0 comments on commit c112d75

Please sign in to comment.