diff --git a/src/experimental/symbolic/gauge.nim b/src/experimental/symbolic/gauge.nim new file mode 100644 index 0000000..a933a32 --- /dev/null +++ b/src/experimental/symbolic/gauge.nim @@ -0,0 +1,246 @@ +import experimental/symbolic/graph +import ../../gauge, physics/qcdTypes + +# +# gauge support +# + +# TODO: needs more thought +type Gauge = seq[DLatticeColorMatrixV] + +type SymNodeValueGauge = ref object of SymNodeValueConcrete + gaugeValue: Gauge + +method getGauge*(v: SymNodeValue): Gauge {.base.} = + raiseValueError("Custom method required for value: " & $v) + +method getGauge*(v:SymNodeValueGauge): Gauge = v.gaugeValue + +method `$`*(v: SymNodeValueGauge): string = "gaugeValue" + +proc assign*(z: SymNode, v: Gauge) = + z.assign SymNodeValueGauge(gaugeValue: v) + +method copySymNodeValue*(v: SymNodeValueGauge): SymNodeValueGauge = + # TODO: we don't need this, if we don't take gradient after assign + let z = v.gaugeValue[0].l.newGauge + threads: + for mu in 0.. SymNode + +proc `*`(x: float, y: SymNode): SymNode = + return newSymNode(value = SymNodeValueFloat(floatValue: x)).mul y + +proc `*`(x: SymNode, y: float): SymNode = + return x.mul newSymNode(value = SymNodeValueFloat(floatValue: y)) + +when isMainModule: + let p = newSym("p") + let k = 0.5 * p.norm2 + let dkdp = k.gradient p + + import qex + import os, strutils + + qexInit() + type GaugeActType = enum ActWilson, ActAdjoint, ActRect, ActSymanzik, ActIwasaki, ActDBW2 + converter toGaugeActType(s:string):GaugeActType = parseEnum[GaugeActType](s) + letParam: + gaugefile = "" + savefile = "config" + savefreq = 10 + lat = + if fileExists(gaugefile): + getFileLattice gaugefile + else: + if gaugefile.len > 0: + qexWarn "Nonexistent gauge file: ", gaugefile + @[4,4,4,8] + gact:GaugeActType = "ActWilson" + beta = 6.0 + adjFac = -0.25 + rectFac = -1.4088 + seed:uint64 = 4321 + echoParams() + echo "rank ", myRank, "/", nRanks + threads: echo "thread ", threadNum, "/", numThreads + + let + gc = case gact + of ActWilson: GaugeActionCoeffs(plaq: beta) + of ActAdjoint: GaugeActionCoeffs(plaq: beta, adjplaq: beta*adjFac) + of ActRect: gaugeActRect(beta, rectFac) + of ActSymanzik: Symanzik(beta) + of ActIwasaki: Iwasaki(beta) + of ActDBW2: DBW2(beta) + lo = lat.newLayout + vol = lo.physVol + + echo gc + var r = lo.newRNGField(MRG32k3a, seed) + var R:MRG32k3a # global RNG + R.seed(seed, 987654321) + + var mom = lo.newgauge + threads: + mom.randomTAH r + + p.assign mom + optimize(output = @[k,dkdp], variables = @[p]) + + k.allocate + dkdp.allocate + echo k.treerepr + echo dkdp.treerepr + + k.eval + dkdp.eval + + echo k.value + let dp = dkdp.value.getGauge + var d2 = 0.0 + threads: + var d2t = 0.0 + for mu in 0..