Exemple #1
0
def startSimulaton():
    time0 = time.time()
    gui.submitBtn.config(state='disabled')
    io.collectParam()

    TList = np.linspace(io.T0, io.T1, io.nT)
    bondList=[lat.Bond(bond_data[0],bond_data[1],\
                       np.array([int(x) for x in bond_data[2]]),\
                       bond_data[3],bond_data[4],bond_data[5]) \
                        for bond_data in io.bondList]
    LMatrix = np.array(io.LMatrix)
    pos = np.array(io.pos)

    if (io.modelType == 'Ising'):
        if io.algorithm != 'Metroplis' and io.algorithm != 'Wolff':
            print(
                'For now, only Metroplis and Wolff algorithm is supported for Ising model'
            )
            gui.submitBtn.config(state='normal')
            return

        paramPack = []
        for iT, T in enumerate(TList):
            paramPack.append([
                iT, T, bondList, LMatrix, pos, io.S, io.DList, io.nsweep,
                io.nthermal, io.LPack[0], io.LPack[1], io.LPack[2],
                io.algorithm
            ])

        TResult = []
        magResult = []
        susResult = []
        energyResult = []
        capaResult = []
        u4Result = []
        while (True):  # using pump strategy to reduce the costs of RAM
            if len(paramPack) == 0:
                break
            # pump tasks
            paramPack_tmp = []
            for index in range(io.ncores):
                paramPack_tmp.append(paramPack.pop(0))
                if len(paramPack) == 0:
                    break
            pool = Pool(processes=io.ncores)
            for result in pool.imap_unordered(startMC, paramPack):
                ID, T, mData, eData = result
                TResult.append(T)
                magResult.append(np.mean(mData))
                susResult.append(np.std(mData))
                energyResult.append(np.mean(eData))
                capaResult.append(np.std(eData))
                u4Result.append(np.mean(mData * mData)**2 / np.mean(mData**4))
            pool.close()
        gui.updateResultViewer(TList=TResult,
                               magList=magResult,
                               susList=susResult)
    # continuous model settings
    elif (io.modelType == 'XY' or io.modelType == 'Heisenberg'):
        for bond in bondList:
            bond.On = True  # switch on the vector type bonding
        if io.algorithm != 'Metroplis' and io.algorithm != 'Wolff':
            print(
                'For now, only Metroplis and Wolff algorithm is supported for O(n) model'
            )
            gui.submitBtn.config(state='normal')
            return

        On = 2 if io.modelType == 'XY' else 3
        paramPack = []
        for iT, T in enumerate(TList):
            paramPack.append([
                iT, T, bondList, LMatrix, pos, io.S, io.DList, io.nsweep,
                io.nthermal, io.LPack[0], io.LPack[1], io.LPack[2],
                io.algorithm, On
            ])

        TResult = []
        magResult = []
        susResult = []
        energyResult = []
        capaResult = []
        u4Result = []
        while (True):  # using pump strategy to reduce the costs of RAM
            if len(paramPack) == 0:
                break
            # pump tasks
            paramPack_tmp = []
            for index in range(io.ncores):
                paramPack_tmp.append(paramPack.pop(0))
                if len(paramPack) == 0:
                    break
            pool = Pool(processes=io.ncores)
            for result in pool.imap_unordered(startMCForOn, paramPack_tmp):
                ID, T, mData, eData = result
                TResult.append(T)
                magResult.append(np.mean(mData))
                susResult.append(np.std(mData))
                energyResult.append(np.mean(eData))
                capaResult.append(np.std(eData))
                u4Result.append(np.mean(mData * mData)**2 / np.mean(mData**4))
            pool.close()
        gui.updateResultViewer(TList=TResult,
                               magList=magResult,
                               susList=susResult)
    else:
        print("for now only Ising, XY and Heisenberg model is supported")
        gui.submitBtn.config(state='normal')
        return

    # writting result file
    f = open('./result.txt', 'w')
    f.write('#Temp #Spin    #Susc      #energy  #capacity #Binder cumulante\n')
    for T, mag, sus, energy, capa, u4 in zip(TResult, magResult, susResult,
                                             energyResult, capaResult,
                                             u4Result):
        f.write('%.3f %.6f %.6f %.6f %.6f %.6f\n' %
                (T, mag, sus, energy, capa, u4))
    f.close()
    gui.submitBtn.config(state='normal')
    print("time elapsed %.3f s" % (time.time() - time0))
    return
Exemple #2
0
def startSimulation(updateGUI=True, rpath=''):
    time0 = time.time()
    # clean possible existed files
    with open('./out', 'w') as fout:
        fout.write('#T #H\n')
    with open('./spinDotSpin.txt', 'w') as fout:
        fout.write('#T #H\n')

    if updateGUI:
        gui.submitBtn.config(state='disabled')
        io.collectParam()
    else:
        if not io.loadParam(updateGUI=False, rpath=rpath):
            'Error: win::startSimulation load file failed. stop this function.'
            return

    TList = np.linspace(io.T0, io.T1, io.nT)
    HList = np.linspace(io.H0, io.H1, io.nH)
    bondList = [
        lat.Bond(
            bond_data[0],
            bond_data[1],  # source and target  
            np.array([int(x) for x in bond_data[2]]),  # over lat.
            bond_data[3],
            bond_data[4],
            bond_data[5],  # strength Jxx, Jyy, Jzz
            bond_data[6],
            bond_data[7],
            bond_data[8],  # strength Jxy, Jxz, Jyz
            bond_data[9],
            bond_data[10],
            bond_data[11],  # strength Jyx, Jzx, Jzy
            True if io.modelType != 'Ising' else False)  # On
        for bond_data in io.bondList
    ]  # ergodic
    LMatrix = np.array(io.LMatrix)
    pos = np.array(io.pos)

    if (io.modelType == 'Ising'):
        if io.algorithm != 'Metropolis' and io.algorithm != 'Wolff':
            print(
                'For now, only Metropolis and Wolff algorithm is supported for Ising model'
            )
            if updateGUI: gui.submitBtn.config(state='normal')
            return

        paramPack = []
        for iH, H in enumerate(HList):
            for iT, T in enumerate(TList):
                paramPack.append([
                    iH * len(TList) + iT, T, bondList, LMatrix, pos, io.S,
                    io.DList, H, io.nsweep, io.nthermal, io.ninterval,
                    io.LPack[0], io.LPack[1], io.LPack[2], io.algorithm,
                    io.GcOrb, io.orbGroupList, io.groupInSC, io.dipoleAlpha,
                    io.spinFrame
                ])

        TResult = []
        HResult = []
        SpinIResult = []
        SpinJResult = []
        susResult = []
        energyResult = []
        capaResult = []
        u4Result = []
        autoCorrResult = []
        while (True):  # using pump strategy to reduce the costs of RAM
            if len(paramPack) == 0:
                break
            # pump tasks
            paramPack_tmp = []
            for index in range(io.ncores):
                paramPack_tmp.append(paramPack.pop(0))
                if len(paramPack) == 0:
                    break
            pool = Pool(processes=io.ncores)
            for result in pool.imap_unordered(startMC, paramPack_tmp):
                ID, T, h, spin_i, spin_j, spin_ij, autoCorr, E, E2, U4, N = result
                TResult.append(T)
                HResult.append(h)
                SpinIResult.append(spin_i)
                SpinJResult.append(spin_j)
                susResult.append((spin_ij - spin_i * spin_j) / T)
                autoCorrResult.append(autoCorr)
                energyResult.append(E)
                capaResult.append((E2 - E * E) / T**2 * N)
                u4Result.append(U4)
            pool.close()
        if updateGUI:
            gui.updateResultViewer(
                TList=TResult if io.xAxisType == 'T' else HResult,
                magList=[(si + sj) / 2
                         for si, sj in zip(SpinIResult, SpinJResult)],
                susList=capaResult)
    # continuous model settings
    elif (io.modelType == 'XY' or io.modelType == 'Heisenberg'):
        for bond in bondList:
            bond.On = True  # switch on the vector type bonding
        if io.algorithm != 'Metropolis' and io.algorithm != 'Wolff':
            print(
                'For now, only Metropolis and Wolff algorithm is supported for O(n) model'
            )
            if updateGUI: gui.submitBtn.config(state='normal')
            return

        On = 2 if io.modelType == 'XY' else 3
        paramPack = []
        for iH, H in enumerate(HList):
            for iT, T in enumerate(TList):
                paramPack.append([
                    iH * len(TList) + iT, T, bondList, LMatrix, pos, io.S,
                    io.DList, H, io.nsweep, io.nthermal, io.ninterval,
                    io.LPack[0], io.LPack[1], io.LPack[2], io.algorithm, On,
                    io.GcOrb, io.orbGroupList, io.groupInSC, io.dipoleAlpha,
                    io.spinFrame
                ])

        TResult = []
        HResult = []
        SpinIResult = []
        SpinJResult = []
        susResult = []
        energyResult = []
        capaResult = []
        u4Result = []
        autoCorrResult = []
        while (True):  # using pump strategy to reduce the costs of RAM
            if len(paramPack) == 0:
                break
            # pump tasks
            paramPack_tmp = []
            for index in range(io.ncores):
                paramPack_tmp.append(paramPack.pop(0))
                if len(paramPack) == 0:
                    break
            pool = Pool(processes=io.ncores)
            for result in pool.imap_unordered(startMCForOn, paramPack_tmp):
                ID, T, h, spin_i, spin_j, spin_ij, autoCorr, E, E2, U4, N = result
                TResult.append(T)
                HResult.append(h)
                SpinIResult.append(np.sqrt(sum(spin_i * spin_i)))
                SpinJResult.append(np.sqrt(sum(spin_j * spin_j)))
                susResult.append((spin_ij - np.dot(spin_i, spin_j)) / T)
                autoCorrResult.append(autoCorr)
                energyResult.append(E)
                capaResult.append((E2 - E * E) / T**2 * N)
                u4Result.append(U4)
            pool.close()
        if updateGUI:
            gui.updateResultViewer(
                TList=TResult if io.xAxisType == 'T' else HResult,
                magList=SpinIResult,
                susList=capaResult)
    else:
        print("for now only Ising, XY and Heisenberg model is supported")
        if updateGUI: gui.submitBtn.config(state='normal')
        return

    # writting result file
    f = open('./result.txt', 'w')
    f.write(
        '#Temp #<Si>    #<Sj>    #Susc    #Energy_per_orb(K)  #capacity_per_orb(K/K) #Binder_cumulate #auto-corr.\n'
    )
    for T, si, sj, sus, energy, capa, u4, autoCorr in zip(
            TResult, SpinIResult, SpinJResult, susResult, energyResult,
            capaResult, u4Result, autoCorrResult):
        f.write('%.3f %.6f %.6f %.6f %.6f %.6f %.6f %.6f\n' %
                (T, si, sj, sus, energy, capa, u4, autoCorr))
    f.close()
    if updateGUI: gui.submitBtn.config(state='normal')
    print("time elapsed %.3f s" % (time.time() - time0))
    return