-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrandom_fields.py
228 lines (167 loc) · 6.96 KB
/
random_fields.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Main dependencies
import numpy
import scipy.fftpack
import numpy as np
import cv2
import torch
import math
def grf_idct_2d(Ln, alpha=2.0, tau=3.0):
"""
Gaussian random field
mean 0
covariance operator C = (-Delta + tau^2)^(-alpha)
Delta is the Laplacian with zero Neumann boundary condition
"""
k = np.arange(Ln)
K1,K2 = np.meshgrid(k,k)
# Define the (square root of) eigenvalues of the covariance operator
C = (np.pi**2)*(np.square(K1)+np.square(K2))+tau**2
C = np.power(C,-alpha/2.0)
C = (tau**(alpha-1))*C
# # sample from normal discribution
xr = np.random.standard_normal(size=(Ln,Ln))
# coefficients in fourier domain
L= C*xr
L= Ln*L
# apply boundary condition
L[0,0] = 0.0
# transform to real domain
u = cv2.idct(L)
print("max: ", u.max())
print("min: ", u.min())
return u
import numpy as np
import cv2
class GaussianRF_idct(object):
"""
Gaussian random field Non-Periodic Boundary
mean 0
covariance operator C = (-Delta + tau^2)^(-alpha)
Delta is the Laplacian with zero Neumann boundary condition
"""
def __init__(self, dim, Ln, alpha=2.0, tau=3.0, device=None):
self.dim = dim
self.Ln = Ln
self.device = device
k = np.arange(Ln)
K1,K2 = np.meshgrid(k,k)
# Define the (square root of) eigenvalues of the covariance operator
C = (np.pi**2)*(np.square(K1)+np.square(K2))+tau**2
C = np.power(C,-alpha/2.0)
C = (tau**(alpha-1))*C
# store coefficient
self.coeff = C
def _sample2d(self):
"""
Single 2D Sample
:return: GRF numpy.narray (Ln,Ln)
"""
# # sample from normal discribution
xr = np.random.standard_normal(size=(self.Ln,self.Ln))
# coefficients in fourier domain
L= self.coeff*xr
L= self.Ln*L
# apply boundary condition
L[0,0] = 0.0
# transform to real domain
u = cv2.idct(L)
return u
def sample(self, N, mul=1):
z_mat = np.zeros((N,self.Ln, self.Ln), dtype=np.float32)
for ix in range(N):
z_mat[ix,:,:] = self._sample2d()
# convert to torch tensor
z_mat = torch.from_numpy(z_mat)
if self.device is not None:
z_mat = z_mat.to(self.device)
return z_mat
class GaussianRF_odd(object):
def __init__(self, dim, size, alpha=2.0, tau=3.0, sigma=None, boundary="periodic", device=None):
self.dim = dim
self.device = device
if sigma is None:
sigma = tau**(0.5*(2*alpha - self.dim))
k_max = size//2
if dim == 1:
k = torch.cat((torch.arange(start=0, end=k_max+1, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0)
self.sqrt_eig = size*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0] = 0.0
elif dim == 2:
wavenumers = torch.cat((torch.arange(start=0, end=k_max+1, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0).repeat(size,1)
k_x = wavenumers.transpose(0,1)
k_y = wavenumers
self.sqrt_eig = (size**2)*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k_x**2 + k_y**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0,0] = 0.0
elif dim == 3:
wavenumers = torch.cat((torch.arange(start=0, end=k_max+1, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0).repeat(size,size,1)
k_x = wavenumers.transpose(1,2)
k_y = wavenumers
k_z = wavenumers.transpose(0,2)
self.sqrt_eig = (size**3)*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k_x**2 + k_y**2 + k_z**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0,0,0] = 0.0
self.size = []
for j in range(self.dim):
self.size.append(size)
self.size = tuple(self.size)
def sample(self, N, mul=1):
coeff = torch.randn(N, *self.size, 2, device=self.device)*mul
coeff[...,0] = self.sqrt_eig*coeff[...,0] #real
coeff[...,1] = self.sqrt_eig*coeff[...,1] #imag
##########torch 1.7###############
#u = torch.ifft(coeff, self.dim, normalized=False)
#u = u[...,0]
##################################
#########torch latest#############
coeff_new = torch.complex(coeff[...,0],coeff[...,1])
#print(coeff_new.size())
u = torch.fft.ifft2(coeff_new, dim = (-2,-1), norm=None)
u = u.real
return u
class GaussianRF(object):
def __init__(self, dim, size, alpha=2.0, tau=3.0, sigma=None, boundary="periodic", device=None):
self.dim = dim
self.device = device
if sigma is None:
sigma = tau**(0.5*(2*alpha - self.dim))
k_max = size//2
if dim == 1:
k = torch.cat((torch.arange(start=0, end=k_max, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0)
self.sqrt_eig = size*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0] = 0.0
elif dim == 2:
wavenumers = torch.cat((torch.arange(start=0, end=k_max, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0).repeat(size,1)
k_x = wavenumers.transpose(0,1)
k_y = wavenumers
self.sqrt_eig = (size**2)*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k_x**2 + k_y**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0,0] = 0.0
elif dim == 3:
wavenumers = torch.cat((torch.arange(start=0, end=k_max, step=1, device=device), \
torch.arange(start=-k_max, end=0, step=1, device=device)), 0).repeat(size,size,1)
k_x = wavenumers.transpose(1,2)
k_y = wavenumers
k_z = wavenumers.transpose(0,2)
self.sqrt_eig = (size**3)*math.sqrt(2.0)*sigma*((4*(math.pi**2)*(k_x**2 + k_y**2 + k_z**2) + tau**2)**(-alpha/2.0))
self.sqrt_eig[0,0,0] = 0.0
self.size = []
for j in range(self.dim):
self.size.append(size)
self.size = tuple(self.size)
def sample(self, N, mul=1):
coeff = torch.randn(N, *self.size, 2, device=self.device)*mul
coeff[...,0] = self.sqrt_eig*coeff[...,0] #real
coeff[...,1] = self.sqrt_eig*coeff[...,1] #imag
##########torch 1.7###############
#u = torch.ifft(coeff, self.dim, normalized=False)
#u = u[...,0]
##################################
#########torch latest#############
coeff_new = torch.complex(coeff[...,0],coeff[...,1])
#print(coeff_new.size())
u = torch.fft.ifft2(coeff_new, dim = (-2,-1), norm=None)
u = u.real
return u