def MC(temperature, spins, MCc, MSpins): # Temperature, Number of Spins, Number of cycles PBC = lambda idx, lim, add: (idx + lim + add) % lim E = 0. Eavr = 0. M = 0. Mavr = 0. lMl = 0. VE = np.zeros(MCc + 1) VM = np.zeros(MCc + 1) # Sampling rule w = np.zeros(17, np.float64) for de in range(-8, 9, 4): w[de + 8] = math.exp(-de / temperature) # Initial energy and magnetization M = MSpins.sum() for ii in range(spins): for jj in range(spins): E -= MSpins[ii, jj] * (MSpins[PBC(ii, spins, -1), jj] + MSpins[ii, PBC(jj, spins, 1)]) VE[0] = E VM[0] = M #MC for ii in range(MCc): # print(ii) for ss in range(spins**2): x = int(nrand() * spins) y = int(nrand() * spins) # print(x,y) dE = 2 * MSpins[x, y] * ( MSpins[PBC(x, spins, -1), y] + MSpins[PBC(x, spins, 1), y] + MSpins[x, PBC(y, spins, -1)] + MSpins[x, PBC(y, spins, 1)]) # print(dE) if nrand() <= w[dE + 8]: MSpins[x, y] *= -1 M += 2 * MSpins[x, y] E += dE Eavr += E VE[ii + 1] = Eavr / (ii + 1) lMl += int(math.fabs(M)) VM[ii + 1] = lMl / (ii + 1) #Eavr /= float(MCc* spins**2) #Mavr /= float(MCc) #lMl /= float(MCc * spins**2) VE /= float(spins**2) VM /= float(spins**2) return (VE, VM)
def sim(self, arm): """ Draw a sample from given arm """ self.nsamples += 1 self.sample_size[arm] += 1 if self.dist == 'bernoulli': if random.random() > self.mean_list[arm]: return 0 else: return 1 else: return nrand(self.mean_list[arm], 0.25)
def MCC(MCc, spins, MSpins, E, M, Eavr, E2av, Mavr, M2av, lMl, w): for ii in range(MCc): for ss in range(spins**2): x = int(nrand() * spins) #Pick up a spin y = int(nrand() * spins) #Pick up a spin dE = 2 * MSpins[x, y] * ( MSpins[PBC(x, spins, -1), y] + MSpins[PBC(x, spins, 1), y] + MSpins[x, PBC(y, spins, -1)] + MSpins[x, PBC(y, spins, 1)]) # Metropolis if nrand() <= w[dE + 8]: MSpins[x, y] *= -1 M += 2 * MSpins[x, y] E += dE Eavr += E E2av += E**2 Mavr += M M2av += M**2 lMl += int(math.fabs(M)) return (Eavr, E2av, Mavr, M2av, lMl)
print(len(output_error)) f_in = open(input, 'r') f_out_raw = open(output_raw, 'w') f_out_error = [] for e in output_error: f_out_error.append(open(e, 'w')) lines = f_in.readlines() for l in lines: seq = l.rstrip().split(" ") for data in seq: f_out_raw.write(data + ":" + data + ":" + '0' + " ") for j in range(1, 11): stddev = 0.2 * j error = nrand(data, stddev) f_out_error[j-1].write(data + ":" + str(error) + ":" + str(stddev) + " ") f_out_raw.write("\n") for e in f_out_error: e.write("\n") f_in.close() f_out_raw.close() for e in f_out_error: e.close()