コード例 #1
0
 def __init__(self, L=50):
     self.L = L  # number of interior points in x and y
     self.omega = 1.88177  # over-relaxation parameter for L = 50
     self.N = L + 2  # interior plus two boundary points
     N = self.N
     self.V = cpt.Matrix(N, N)  # potential to be found
     self.rho = cpt.Matrix(N, N)  # given charge density
     self.VNew = cpt.Matrix(N, N)  # new potential after each step
     self.h = 1.0 / (L + 1)  # lattice spacing assuming size in x and y = 1
     self.q = 10.0  # point charge
     i = N / 2  # center of lattice
     self.rho[i][i] = self.q / self.h**2  # charge density
コード例 #2
0
ファイル: rwalk.py プロジェクト: attesa/computation-physics
import numpy as np

print " Random walk in 1 dimension"
print " --------------------------"
n_walkers = int(input(" Enter number of walkers: "))
n_steps = int(input(" Enter number of steps: "))

# walker positions initialized at x = 0  y=0
x = [ 0.0 ] * n_walkers
y = [ 0.0 ] * n_walkers
z = [ 0.0 ] * n_walkers

steps = [ 0.0 ] * n_steps       # to save step number i (time)
x2ave = [ 0.0 ] * n_steps       # to accumulate x^2 values
sigma = [ 0.0 ] * n_steps        # to accumulate fluctuations in x^2
x2in = cpt.Matrix(n_walkers,n_steps)       # individual x^2+y^2

# loop over walkers
for walker in range(n_walkers):

    # loop over number of steps
    for step in range(n_steps):

        # take a random step
        x[walker] += random.choice( (-1, 1) )
        y[walker] += random.choice( (-1, 1) )
        z[walker] += random.choice( (-1, 1) )
        #while x[walker]**2+y[walker]**2 >= 1:
        #    x[walker] += random.choice( (-1, 1) )
        #    y[walker] += random.choice( (-1, 1) )            
        # accumulate data