Beispiel #1
0
 def test_mc(self):
     self.mc = CanonicalMonteCarlo(TestMC.model(), 1.0, [1.0,1.0])
     # self.mc = CanonicalMonteCarlo(TestMC.dmodel, 1.0, 1)
     N = 100
     S = 0.0
     S2 =0.0
     for i in range(N):
         E = self.mc.run(100, 1)[0]
         S += E
         S2 += E*E
     m = S/N
     er = np.sqrt((S2 - S*S/N)/(N-1)/N)
     self.assertTrue(np.isclose(-np.tanh(1.0), m, atol=3*er, rtol=0.0))
Beispiel #2
0
class TestMC(unittest.TestCase):
    class model(model):
        def energy(self, config):
            return config[0] * config[1]

        def newconfig(self, config, dconfig):
            ret = config[:]
            ret[dconfig] *= -1.0
            return ret

        def trialstep(self, config, energy):
            dconfig = random.randint(0, 1)
            dE = self.energy(self.newconfig(config, dconfig)) - energy
            return dconfig, dE

    def test_mc(self):
        self.mc = CanonicalMonteCarlo(TestMC.model(), 1.0, [1.0,1.0])
        # self.mc = CanonicalMonteCarlo(TestMC.dmodel, 1.0, 1)
        N = 100
        S = 0.0
        S2 =0.0
        for i in range(N):
            E = self.mc.run(100, 1)[0]
            S += E
            S2 += E*E
        m = S/N
        er = np.sqrt((S2 - S*S/N)/(N-1)/N)
        self.assertTrue(np.isclose(-np.tanh(1.0), m, atol=3*er, rtol=0.0))
Beispiel #3
0
    nstep_param = 12
    eqsteps = 2**nstep_param * 10  # equilibration steps
    mcsteps = 2**nstep_param * 40  # measurement steps
    sample_frequency = 1  # we sample every step
    # the frequency for printing observer.logfunc() to obs.dat
    print_frequency = 100000000

    binlevel = int(np.log2(mcsteps // 30))
    config = ising2D_config(size, size)
    config.prepare_random()
    model = ising2D(J)
    binning_fileE = open("binningE.dat", "w")
    binning_fileM = open("binningM.dat", "w")
    for kT in np.linspace(5.0, 0.01, 10):
        kT = abs(J) * kT
        calc = CanonicalMonteCarlo(model, kT, config)
        calc.run(eqsteps)  # run equilibration steps

        myobserver = observer()  # provide a new observer instance
        obs = calc.run(
            mcsteps,
            sample_frequency,
            print_frequency,
            myobserver  # Run sampling steps
        )

        # binning analysis for putting error bars on data
        error_estimateE = binning(
            np.asarray(myobserver.energy_obs) / nspin, binlevel)
        error_estimateM = binning(
            np.asarray(myobserver.magnet_obs) / nspin, binlevel)
Beispiel #4
0
if __name__ == "__main__":
    L = 1.0
    rdisc = 1.0 / 14.0
    Ndisc = 20
    dr = 0.01
    maxstep = rdisc / 2
    kT = 1

    grid = grid_1D(dr, dr, L / 2.0)
    config = HC_2D_config(L, Ndisc, rdisc)
    model = HC_2D(maxstep)
    config.prepare_ordered()
    print(config.coors)
    print(g_r(config, grid))
    calc = CanonicalMonteCarlo(model, kT, config, grid=grid)
    plot_fig(calc, 10000)
    # calc.run(10000, observefunc=observables)
    # print(config.coors)

    """calc = Canonical
    
    J = -1.0
    kT = abs(J) * 1.0
    size = 10
    eqsteps = 100000
    mcsteps = 1000000
    sample_frequency = size*size
    config = ising2D_config(size,size)
    config.prepare_random()
    model = ising2D(J)
Beispiel #5
0
    m = 1.0 / 6.023 * 1e-26
    T = 300
    h = 6.62607004e-34
    size = 5
    nspin = size * size
    eqsteps = nspin * 1000
    mcsteps = nspin * 1000
    sample_frequency = 1  # nspin
    config = latgas_config(size, size)
    config.prepare_random()
    binning_file = open("binning.dat", "a")
    mu0 = -kb * T * np.log((2.0 * np.pi * m * kb * T / h**2)**(3 / 2) * kb * T)
    Eads = -0.4 * 1.602e-19
    for p in np.linspace(1e4, 0.0001, 20):
        mu = mu0 + kb * T * np.log(p)
        model = latticegas(Eads=Eads, J=0, mu=mu)
        kT = kb * T
        calc = CanonicalMonteCarlo(model, kT, config)
        calc.run(eqsteps)
        myobserver = observer()
        obs = calc.run(mcsteps, sample_frequency, myobserver)
        print(p, "\t", "\t".join([str(x / nspin) for x in obs]))
        # binning analysis
        error_estimate = binning(myobserver.density_obs, 10)
        binning_file.write("\n".join([str(x)
                                      for x in error_estimate]) + "\n\n")
        sys.stdout.flush()
        model = calc.model
        config = calc.config
        # print(config)