コード例 #1
0
ファイル: main.py プロジェクト: huangy22/Data_Analysis_DiagMC
def Dyson(IsDysonOnly, IsNewCalculation, para, Map, Lat):
    ParaDyson=para["Dyson"]
    if not para.has_key("Version"):
        para["Version"]=0
    ########## Calulation INITIALIZATION ##########################
    Factory=model.BareFactory(Map, Lat,  para["Model"], ParaDyson["Annealing"])
    G0,W0=Factory.Build()
    IO.SaveDict("Coordinates","w", Factory.ToDict())
    Observable=measure.Observable(Map, Lat)
    W=weight.Weight("SmoothT", Map, "FourSpins", "Symmetric","R","T")
    SigmaDeltaT=weight.Weight("DeltaT", Map, "TwoSpins", "AntiSymmetric","R")
    Sigma=weight.Weight("SmoothT", Map, "TwoSpins", "AntiSymmetric","R","T")
    Polar=weight.Weight("SmoothT", Map, "FourSpins", "Symmetric","R","T")
    if IsNewCalculation:
        #not load WeightFile
        log.info("Start from bare G, W")
        G=G0.Copy()
    else:
        #load WeightFile, load G,W
        log.info("Load G, W, Sigma, Polar from {0}".format(WeightFile))
        data=IO.LoadBigDict(WeightFile)
        G=weight.Weight("SmoothT", Map, "TwoSpins", "AntiSymmetric", "R", "T").FromDict(data["G"])
        W.FromDict(data["W"])
        SigmaDeltaT.FromDict(data["SigmaDeltaT"])
        Sigma.FromDict(data["Sigma"])
        Polar.FromDict(data["Polar"])

    Gold, Wold = G, W

    #while para["Version"]<2:
    while True:
        para["Version"]+=1
        log.info(green("Start Version {0}...".format(para["Version"])))
        try:
            ratio=None   #set this will not use accumulation!
            #ratio = para["Version"]/(para["Version"]+10.0)
            G0,W0=Factory.Build()
            log.info("calculating SigmaDeltaT..")
            SigmaDeltaT.Merge(ratio, calc.SigmaDeltaT_FirstOrder(G, W0, Map))
            log.info("SigmaDeltaT is done")

            if IsDysonOnly or IsNewCalculation:
                log.info("accumulating Sigma/Polar statistics...")
                Sigma.Merge(ratio, calc.SigmaSmoothT_FirstOrder(G, W, Map))
                log.info("calculating G...")
                G = calc.G_Dyson(G0, SigmaDeltaT, Sigma, Map)
                Polar.Merge(ratio, calc.Polar_FirstOrder(G, Map))
            else:
                log.info("Collecting Sigma/Polar statistics...")
                Statis=collect.CollectStatis(Map)
                Sigma, Polar, ParaDyson["OrderAccepted"]=collect.UpdateWeight(Statis,
                        ParaDyson["ErrorThreshold"], ParaDyson["OrderAccepted"])
                log.info("calculating G...")
                G = calc.G_Dyson(G0, SigmaDeltaT, Sigma, Map)
            #######DYSON FOR W AND G###########################
            log.info("calculating W...")
            W, ChiTensor, Determ = calc.W_Dyson(W0, Polar, Map, Lat)

        except calc.DenorminatorTouchZero as err:
            #failure due to denorminator touch zero
            log.info(green("Version {0} fails due to:\n{1}".format(para["Version"],err)))
            Factory.RevertField(ParaDyson["Annealing"])
            G, W = Gold, Wold
            SigmaDeltaT.RollBack()
            Sigma.RollBack()
            Polar.RollBack()
        except collect.CollectStatisFailure as err:
            #failure due to statis files collection
            log.info(green("Version {0} fails due to:\n{1}".format(para["Version"],err)))
            G, W = Gold, Wold
            SigmaDeltaT.RollBack()
            Sigma.RollBack()
            Polar.RollBack()
        except KeyboardInterrupt, SystemExit:
            #exit
            log.info("Terminating Dyson\n {1}".format(para["Version"], traceback.format_exc()))
            sys.exit(0)
        except:
コード例 #2
0
ファイル: main.py プロジェクト: iintSjds/Feynman_Simulator
def Dyson(IsDysonOnly, IsNewCalculation, EnforceSumRule, para, Map, Lat):
    ParaDyson = para["Dyson"]
    if not para.has_key("Version"):
        para["Version"] = 0
    ########## Calulation INITIALIZATION ##########################
    Factory = model.BareFactory(Map, Lat, para["Model"],
                                ParaDyson["Annealing"])
    G0, W0 = Factory.Build()
    # G0.FFT("K","W")
    # print G0.Data[UP,0,UP,0,0,:]
    # print "Comparison 1"
    # for n in range(Map.MaxTauBin):
    # wn=1j*(2*n+1)*np.pi/Map.Beta
    # print 1.0/(wn-4.0), G0.Data[UP,0,UP,0,0,n]*Map.Beta/Map.MaxTauBin

    # print "Comparison 2"
    # G0.FFT("K","T")
    # for t in range(Map.MaxTauBin):
    # tau=Map.IndexToTau(t)
    # G0w=-np.exp(4*tau)*(1.0-1.0/(1.0+np.exp(-Map.Beta*4)))
    # print G0.Data[UP,0,UP,0,0,t], G0w

    # print "Comparison 3"
    # for n in range(Map.MaxTauBin):
    # Gw=0.0
    # wn=(2*n+1)*np.pi/Map.Beta
    # for t in range(Map.MaxTauBin):
    # tau=Map.IndexToTau(t)
    # G0w=-np.exp(4*tau)*(1.0-1.0/(1.0+np.exp(-Map.Beta*4)))
    # Gw+=G0w*np.exp(-1j*wn*tau)*Map.Beta/Map.MaxTauBin
    # # Gw+=G0.Data[UP,0,UP,0,0,t]*np.exp(-1j*wn*tau)*Map.Beta/Map.MaxTauBin
    # print 1.0/(1j*wn-4.0), Gw

    G0.FFT("K", "T")
    print G0.Data[UP, 0, UP, 0, 0, :]
    plot.PlotTimeForList("G0UPUP_r", G0, UP, 0, UP, 0, range(Map.L[0]))
    plot.PlotBand(G0, Lat)

    IO.SaveDict("Coordinates", "w", Factory.ToDict())
    Observable = measure.Observable(Map, Lat)
    W = weight.Weight("SmoothT", Map, "FourSpins", "Symmetric", "R", "T")
    SigmaDeltaT = weight.Weight("DeltaT", Map, "TwoSpins", "AntiSymmetric",
                                "R")
    Sigma = weight.Weight("SmoothT", Map, "TwoSpins", "AntiSymmetric", "R",
                          "T")
    Polar = weight.Weight("SmoothT", Map, "FourSpins", "Symmetric", "R", "T")

    if IsNewCalculation:
        #not load WeightFile
        log.info("Start from bare G, W")
        G = G0.Copy()
        if para["Gamma3"]:
            GGGammaG = gamma3.SimpleGG(G, Map)
    else:
        #load WeightFile, load G,W
        log.info("Load G, W from {0}".format(WeightFile))
        data = IO.LoadBigDict(WeightFile)
        G = weight.Weight("SmoothT", Map, "TwoSpins", "AntiSymmetric", "R",
                          "T").FromDict(data["G"])
        W.FromDict(data["W"])
        SigmaDeltaT.FromDict(data["SigmaDeltaT"])
        Sigma.FromDict(data["Sigma"])
        Polar.FromDict(data["Polar"])

        if para["Gamma3"]:
            if data.has_key("GGGammaG"):
                GGGammaG = data["GGGammaG"]["SmoothT"]
                print "Read existing GGGammaG"
            else:
                GGGammaG = gamma3.SimpleGG(G, Map)

    Gold, Wold = G, W

    #while para["Version"]==0:
    while True:
        para["Version"] += 1
        log.info(green("Start Version {0}...".format(para["Version"])))
        try:
            # ratio=None   #set this will not use accumulation!
            ratio = para["Version"] / (para["Version"] + 10.0)
            G0, W0 = Factory.Build()
            # print W0.Data[:,0,:,0,1]
            log.info("calculating SigmaDeltaT..")
            SigmaDeltaT.Merge(ratio, calc.SigmaDeltaT_FirstOrder(G, W0, Map))
            log.info("SigmaDeltaT is done")

            # print "Polar[UP,UP]=\n", Polar.Data[spinUP,0,spinUP,0,0,:]
            # print "GammaG[UP,UP]=\n", GammaG[UP,0,:,-1]

            if IsDysonOnly or IsNewCalculation:
                log.info("accumulating Sigma/Polar statistics...")
                G = calc.G_Dyson(G0, SigmaDeltaT, Sigma, Map)
                Sigma.Merge(ratio, calc.SigmaSmoothT_FirstOrder(G, W, Map))
                log.info("calculating G...")

                G = calc.G_Dyson(G0, SigmaDeltaT, Sigma, Map)
                Polar.Merge(ratio, calc.Polar_FirstOrder(G, Map))

                if para["Gamma3"]:
                    # irreducible GGGammaG = simpleGG + GGGammaG_2 + GGGammaG_3

                    # the second term GammaG: the term from dSigma/dG
                    # GGGammaG_2 = G*(W*GGGammaG)*G
                    print "Attach W to GGGammaG"
                    GammaG = gamma3.AddW_To_GGGammaG(GGGammaG, W, G.Map)
                    print "Calculate GammaG contribution to GGGammaG"
                    GGGammaG_2 = gamma3.AddTwoG_To_GammaG(GammaG, G, G.Map)

                    if Map.MaxTauBin == Map.MaxTauBinTiny:
                        # the third term: the term from dSigma/dW
                        # GGGammaG_3 = G*((W*(G*GGGammaG)*W)*G)*G
                        print "Calculate GammaW"
                        GammaW = gamma3.AddG_To_GGGammaG(GGGammaG, G, G.Map)
                        print "Calculate WWGammaW"
                        WWGammaW = gamma3.AddTwoW_To_GammaW(
                            GammaW, W0, W, G.Map)
                        print "Calculate GammaG from WWGammaW"
                        GammaG_FromWWGammaW = gamma3.AddG_To_WWGammaW(
                            WWGammaW, G, G.Map)
                        print "Calculate WWGammaW contribution to GGGammaG"
                        GGGammaG_3 = gamma3.AddTwoG_To_GammaG(
                            GammaG_FromWWGammaW, G, G.Map)
                    else:
                        WWGammaW = None
                        GGGammaG_3 = 0.0

                    SimpleGGGammaG = gamma3.SimpleGG(G, Map)

                    GGGammaG = SimpleGGGammaG
                    GGGammaG += +GGGammaG_2 - GGGammaG_3

            else:
                log.info("Collecting Sigma/Polar statistics...")
                SigmaStatis, PolarStatis, GammaG_MC, GammaW_MC = collect.CollectStatis(
                    Map, para["Gamma3"])
                Sigma, Polar_MC, ParaDyson[
                    "OrderAccepted"] = collect.UpdateWeight(
                        [SigmaStatis, PolarStatis],
                        ParaDyson["ErrorThreshold"],
                        ParaDyson["OrderAccepted"])
                #print Sigma.Data[0,0,0,0,0,0], Sigma.Data[0,0,0,0,0,-1]
                log.info("calculating G...")

                G = calc.G_Dyson(G0, SigmaDeltaT, Sigma, Map)
                SigmaDyson = calc.SigmaSmoothT_FirstOrder(G, W, Map)
                print "SigmaFromDyson=\n", SigmaDyson.Data[UP, 0, UP, 0, 0, :]

                Polar.Merge(ratio, Polar_MC)

                if para["Gamma3"]:
                    print "Calculate WWGammaW contribution to GGGammaG"
                    WWGammaW = gamma3.AddTwoW_To_GammaW(
                        GammaW_MC, W0, W, G.Map)

                    print "Calculate GGGammaW contribution to GGGammaG"
                    GGGammaG_MC = gamma3.AddTwoG_To_GammaG(GammaG_MC, G, G.Map)
                    print "Add Simple GG contribution to GGGammaG"
                    GGGammaG = gamma3.SimpleGG(G, Map) + GGGammaG_MC
                    # print "GammaG, mc=\n",  0.5*(np.sum(GGGammaG_MC[DOWN, :, :, :]-GGGammaG_MC[UP, :, :, :], axis=0)).diagonal()

            #######DYSON FOR W AND G###########################
            log.info("calculating W...")

            Wtmp, ChiTensor, Determ = calc.W_Dyson(W0, Polar, Map, Lat)

            if EnforceSumRule:
                ChiTensor = calc.Add_ChiTensor_ZerothOrder(ChiTensor, G, Map)
                Chi = calc.Calculate_Chi(ChiTensor, Map)
                Chi.FFT("R", "T")

                while abs(Chi.Data[0, 0, 0, 0, 0, 0] - 0.75) > 1.e-3:
                    SumRuleRatio = np.sqrt(0.75 / Chi.Data[0, 0, 0, 0, 0, 0])
                    PolarSumRule = Polar
                    PolarSumRule.Data = PolarSumRule.Data * SumRuleRatio
                    Wtmp, ChiTensor, Determ = calc.W_Dyson(W0, Polar, Map, Lat)
                    ChiTensor = calc.Add_ChiTensor_ZerothOrder(
                        ChiTensor, G, Map)
                    Chi = calc.Calculate_Chi(ChiTensor, Map)
                    Chi.FFT("R", "T")
                    print "Chi(r=0,t=0)", Chi.Data[0, 0, 0, 0, 0, 0]

            W = Wtmp

        except calc.DenorminatorTouchZero as err:
            #failure due to denorminator touch zero
            log.info(
                green("Version {0} fails due to:\n{1}".format(
                    para["Version"], err)))
            Factory.RevertField(ParaDyson["Annealing"])
            G, W = Gold, Wold
            SigmaDeltaT.RollBack()
            Sigma.RollBack()
            Polar.RollBack()

        except collect.CollectStatisFailure as err:
            #failure due to statis files collection
            log.info(
                green("Version {0} fails due to:\n{1}".format(
                    para["Version"], err)))
            G, W = Gold, Wold
            SigmaDeltaT.RollBack()
            Sigma.RollBack()
            Polar.RollBack()
        except KeyboardInterrupt, SystemExit:
            #exit
            log.info("Terminating Dyson\n {1}".format(para["Version"],
                                                      traceback.format_exc()))
            sys.exit(0)
        except: