File size: 2,422 Bytes
849505e | 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 | import torch
import math
torch.manual_seed(0)
class GaussianRF(object):
def __init__(self, dim, size, length=1.0, alpha=2.0, tau=3.0, sigma=None, boundary="periodic", constant_eig=False, device=None):
self.dim = dim
self.device = device
if sigma is None:
sigma = tau**(0.5*(2*alpha - self.dim))
k_max = size//2
const = (4*(math.pi**2))/(length**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*((const*(k**2) + tau**2)**(-alpha/2.0))
if constant_eig:
self.sqrt_eig[0] = size*sigma*(tau**(-alpha))
else:
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*((const*(k_x**2 + k_y**2) + tau**2)**(-alpha/2.0))
if constant_eig:
self.sqrt_eig[0,0] = (size**2)*sigma*(tau**(-alpha))
else:
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*((const*(k_x**2 + k_y**2 + k_z**2) + tau**2)**(-alpha/2.0))
if constant_eig:
self.sqrt_eig[0,0,0] = (size**3)*sigma*(tau**(-alpha))
else:
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):
coeff = torch.randn(N, *self.size, dtype=torch.cfloat, device=self.device)
coeff = self.sqrt_eig*coeff
u = torch.fft.irfftn(coeff, self.size, norm="backward")
return u |