-
Notifications
You must be signed in to change notification settings - Fork 1
/
ran_gauss2D.py
94 lines (64 loc) · 2.42 KB
/
ran_gauss2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import numpy as np
import matplotlib.pyplot as pl
def model0(k):
return k**0*0.5
def gen_2Dgauss(N, Lx, Ly, model):
field = np.zeros((N,N), dtype = complex)
dkx = (2.*np.pi)/Lx
dky = (2.*np.pi)/Ly
for ix in range(0, N):
if ix <= N/2:
kx = ix*dkx
else:
kx = (ix-N)*dkx
for iy in range(0, N):
if iy <= N/2:
ky = iy*dky
else:
ky = (iy-N)*dky
kval = (kx**2 + ky**2)**0.5
field[ix, iy] = np.random.normal(0.,(model(kval)/2.)**0.5) + np.random.normal(0.,(model(kval)/2.)**0.5)*1j
# Now we have to set \delta(-k) = \delta^*(k)
# Note that \delta_n = \delta_{n+N} and therefore we have to set \delta(2dk,-dk) = \delta^*(2dk,dk)
for ix in range(N/2+1, N):
jx = N-ix
field[ix, 0] = field[jx, 0].real - field[jx, 0].imag*1j
field[0, ix] = field[0, jx].real - field[0, jx].imag*1j
for iy in range(1, N):
jy = N-iy
field[ix, iy] = field[jx, jy].real - field[jx, jy].imag*1j
if N % 2 == 0:
for ix in range(1, N/2):
jx = N-ix
field[N/2,ix] = field[N/2,jx].real - field[N/2,jx].imag*1j
kval = dkx*N/2
# Set the complex part to zero if there is no partner (note the factor of 2 difference)
field[0,N/2] = np.random.normal(0.,model(kval)**0.5) + 0.*1j;
field[N/2,0] = np.random.normal(0.,model(kval)**0.5) + 0.*1j;
kval = (dkx*N/2)*(2**0.5)
field[N/2,N/2] = np.random.normal(0.,model(kval)**0.5) + 0.*1j;
field[0,0] = 0. + 0.*1j
return field
def main():
Lx = 1.
Ly = 1.
V = Lx*Ly
N = 101
dk = (2.*np.pi)/Lx
#-----------------------------------------#
#-- Generate Gaussian random field -------#
#-----------------------------------------#
kspace_field = gen_2Dgauss(N, Lx, Ly, model0)
config_field = np.fft.ifft2(kspace_field)*kspace_field.size**0.5
fig, ax = pl.subplots()
im = ax.imshow(config_field.real, cmap=pl.cm.jet)
fig.colorbar(im, ax=ax)
pl.title("field.real, config_space")
pl.show()
fig, ax = pl.subplots()
im = ax.imshow(config_field.imag, cmap=pl.cm.jet)
fig.colorbar(im, ax=ax)
pl.title("field.imag, config_space")
pl.show()
if __name__ == '__main__':
main()