In [5]:
    
import numpy as np
import ctypes
from ctypes import *
import pycuda.gpuarray as gpuarray
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import math
    
In [6]:
    
#To put images inside the notebook
%matplotlib inline
    
In [7]:
    
gridDIM = 64
size = gridDIM*gridDIM
axes0 = 0
axes1 = 1
makeC2C = 0
makeR2C = 1
makeC2R = 1
axesSplit_0 = 0
axesSplit_1 = 1
m = size
segment_axes0 = 0
segment_axes1 = 0
DIR_BASE = "/home/robert/Documents/new1/FFT/code/"
# FAFT 
_faft128_2D = ctypes.cdll.LoadLibrary( DIR_BASE+'FAFT128_2D_R2C.so' )
_faft128_2D.FAFT128_2D_R2C.restype = int
_faft128_2D.FAFT128_2D_R2C.argtypes = [ctypes.c_void_p, ctypes.c_void_p, 
                                         ctypes.c_float, ctypes.c_float, ctypes.c_int, 
                                         ctypes.c_int, ctypes.c_int, ctypes.c_int]
cuda_faft = _faft128_2D.FAFT128_2D_R2C
# Inv FAFT
_ifaft128_2D = ctypes.cdll.LoadLibrary( DIR_BASE+'IFAFT128_2D_C2R.so' )
_ifaft128_2D.IFAFT128_2D_C2R.restype = int
_ifaft128_2D.IFAFT128_2D_C2R.argtypes = [ctypes.c_void_p, ctypes.c_void_p, 
                                           ctypes.c_float, ctypes.c_float, ctypes.c_int, 
                                           ctypes.c_int, ctypes.c_int, ctypes.c_int]
cuda_ifaft = _ifaft128_2D.IFAFT128_2D_C2R
    
In [8]:
    
def Gaussian(x,sigma):
    return np.exp( - x**2/sigma**2/2.  )/(sigma*np.sqrt( 2*np.pi  ))
def fftGaussian(p,sigma):
    return np.exp( - p**2*sigma**2/2.  )
    
In [9]:
    
# Gaussian parameters
mu = 0
sigma = 1.
# Grid parameters
x_amplitude = 5.
p_amplitude = 6.                # With the traditional method p amplitude is fixed to: 2 * np.pi /( 2*x_amplitude ) 
dx = 2*x_amplitude/float(gridDIM)  # This is dx in Bailey's paper
dp = 2*p_amplitude/float(gridDIM)  # This is gamma in Bailey's paper
delta = dx*dp/(2*np.pi)
x_range = np.linspace( -x_amplitude, x_amplitude-dx, gridDIM) 
p = np.linspace( -p_amplitude, p_amplitude-dp, gridDIM) 
x = x_range[ np.newaxis, : ] 
y = x_range[ :, np.newaxis ] 
f =  Gaussian(x,sigma)*Gaussian(y,sigma)
plt.imshow( f, extent=[-x_amplitude , x_amplitude-dx, -x_amplitude , x_amplitude-dx] )
axis_font = {'size':'24'}
plt.text( 0., 5.1, '$W$' , **axis_font)
plt.colorbar()
#plt.ylim(0,0.44)
print ' Amplitude x = ',x_amplitude
print ' Amplitude p = ',p_amplitude
print '        '
print 'sigma = ', sigma
print 'n     = ', x.size
print 'dx    = ', dx
print 'dp    = ', dp
print '           standard fft dp = ',2 * np.pi /( 2*x_amplitude ) , '     '
print '    '
print 'delta = ', delta
print '    '
print 'The Gaussian extends to the numerical error in single precision:'  
print '    min = ', np.min(f)
    
    
    
In [10]:
    
f33 = np.zeros( [1 ,64], dtype = np.complex64 )
    
In [11]:
    
# One gpu array.
f_gpu = gpuarray.to_gpu( np.ascontiguousarray( f , dtype = np.float32 ) )
f33_gpu = gpuarray.to_gpu( np.ascontiguousarray( f33 , dtype = np.complex64 ) )
    
In [12]:
    
# Executing FFT
cuda_faft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, delta, segment_axes0, axes0, makeR2C, axesSplit_0 )
cuda_faft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, delta, segment_axes1, axes1, makeC2C, axesSplit_0 )
    
    Out[12]:
In [13]:
    
plt.imshow( np.append( f_gpu.get()[:32, :], f33_gpu.get().real, axis=0 )/float(np.sqrt(size)), 
           extent=[-p_amplitude , p_amplitude-dp, 0, p_amplitude-dp] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( 0., 6.2, '$Re \\mathcal{F}(W)$',  **axis_font )
plt.xlim(-p_amplitude , p_amplitude-dp)
plt.ylim(0 , p_amplitude)
    
    Out[13]:
    
In [14]:
    
plt.imshow( np.append( f_gpu.get()[32:, :], f33_gpu.get().imag, axis=0 )/float(np.sqrt(size)), 
           extent=[-p_amplitude , p_amplitude-dp, 0, p_amplitude-dp] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( 0., 6.2, '$Im \\mathcal{F}(W)$',  **axis_font )
plt.xlim(-p_amplitude , p_amplitude-dp)
plt.ylim(0 , p_amplitude)
    
    Out[14]:
    
In [15]:
    
plt.figure(figsize=(10,10))
plt.plot( p[:33], np.append( f_gpu.get()[:32, 32], f33_gpu.get()[:, 32].real, axis=0 )/(f_gpu.get().size) , 
         'o', label='numerical')
plt.plot( p[:33], 4*fftGaussian(p,sigma)[:33] , label = 'analytical')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[15]:
    
In [16]:
    
plt.figure(figsize=(10,10))
plt.semilogy( p[:33], np.append( f_gpu.get()[:32, 32], f33_gpu.get()[:, 32].real, axis=0 )/ (f_gpu.get().size), 
             'o', label='numerical')
plt.semilogy( p[:33], 4*fftGaussian(p,sigma)[:33] , label = 'analytical')
plt.legend(loc='upper left')
plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[16]:
    
In [17]:
    
plt.plot( p[:33], np.append(f_gpu.get()[32:, 32], f33_gpu.get()[:, 32].imag, axis=0)/(f_gpu.get().size), 'o',
         label='numerical imag error')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.xlabel('$p$',**axis_font)
    
    Out[17]:
    
In [18]:
    
# Executing iFFT
cuda_ifaft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, -delta, segment_axes1, axes1, makeC2C, axesSplit_0 )
cuda_ifaft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, -delta, segment_axes0, axes0, makeC2R, axesSplit_0 )
    
    Out[18]:
In [19]:
    
plt.imshow( f_gpu.get()/(float(size*size))  ,
           extent=[-x_amplitude , x_amplitude-dx, -x_amplitude, x_amplitude-dx] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( -1, 6.2, '$W$',  **axis_font )
plt.xlim(-x_amplitude , x_amplitude-dx)
plt.ylim(-x_amplitude , x_amplitude-dx)
    
    Out[19]:
    
In [20]:
    
plt.figure(figsize=(10,10))
plt.plot( y, f_gpu.get()[:, 32]/(f_gpu.get().size) /(43.8*f_gpu.get().size), 
         'o', label='numerical')
plt.plot( y, 4*Gaussian(y, sigma) , label = 'analytical')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[20]:
    
In [21]:
    
# LOG PLOT 
plt.figure(figsize=(10,10))
plt.semilogy( y, f_gpu.get()[:, 32]/(f_gpu.get().size) /(450*f_gpu.get().size)  , 'o', label='numerical')
plt.semilogy( y, f[:, 32]  , '.', label='original')
plt.semilogy( y, Gaussian(y, sigma)/2.5 , label = 'analytical')
plt.legend(loc='upper left')
plt.ylim(1e-7,0.5)
plt.ylabel('$e^{- \\frac{x^2}{2 \\sigma^2 } }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[21]:
    
In [22]:
    
f33 = np.zeros( [64, 1], dtype = np.complex64 )
    
In [23]:
    
# One gpu array.
f_gpu = gpuarray.to_gpu( np.ascontiguousarray( f , dtype = np.float32 ) )
f33_gpu = gpuarray.to_gpu( np.ascontiguousarray( f33 , dtype = np.complex64 ) )
    
In [24]:
    
# Executing FFT
cuda_faft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, delta, segment_axes1, axes1, makeR2C, axesSplit_1 )
cuda_faft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, delta, segment_axes0, axes0, makeC2C, axesSplit_1 )
    
    Out[24]:
In [25]:
    
plt.imshow( np.append( f_gpu.get()[:, :32], f33_gpu.get().real, axis=1 )/float(np.sqrt(size)), 
           extent=[-p_amplitude , 0, -p_amplitude, p_amplitude-dp] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( -3.0, 6.2, '$Re \\mathcal{F}(W)$',  **axis_font )
plt.xlim(-p_amplitude , 0)
plt.ylim(-p_amplitude , p_amplitude-dp)
    
    Out[25]:
    
In [26]:
    
plt.imshow( np.append( f_gpu.get()[:, 32:], f33_gpu.get().imag, axis=1 )/float(np.sqrt(size)), 
           extent=[-p_amplitude , 0, -p_amplitude, p_amplitude-dp] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( -3.0, 6.2, '$Im \\mathcal{F}(W)$',  **axis_font )
plt.xlim(-p_amplitude , 0)
plt.ylim(-p_amplitude , p_amplitude-dp)
    
    Out[26]:
    
In [27]:
    
plt.figure(figsize=(10,10))
plt.plot( p[:33], np.append( f_gpu.get()[32, :32], f33_gpu.get()[32, :].real, axis=0 )/(f_gpu.get().size) , 
         'o', label='numerical')
plt.plot( p[:33], 4*fftGaussian(p,sigma)[:33] , label = 'analytical')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[27]:
    
In [28]:
    
plt.figure(figsize=(10,10))
plt.semilogy( p[:33], np.append( f_gpu.get()[32, :32], f33_gpu.get()[32, :].real, axis=0 )/ (f_gpu.get().size), 
             'o', label='numerical')
plt.semilogy( p[:33], 4*fftGaussian(p,sigma)[:33] , label = 'analytical')
plt.legend(loc='upper left')
plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[28]:
    
In [29]:
    
plt.plot( p[:33], np.append(f_gpu.get()[32, 32:], f33_gpu.get()[32, :].imag, axis=0)/(f_gpu.get().size), 'o',
         label='numerical imag error')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.xlabel('$p$',**axis_font)
    
    Out[29]:
    
In [30]:
    
# Executing iFFT
cuda_ifaft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, -delta, segment_axes0, axes0, makeC2C, axesSplit_1 )
cuda_ifaft( int(f_gpu.gpudata), int(f33_gpu.gpudata), dx, -delta, segment_axes1, axes1, makeC2R, axesSplit_1 )
    
    Out[30]:
In [31]:
    
plt.imshow( f_gpu.get()/float(size)  ,
           extent=[-x_amplitude , x_amplitude-dx, -x_amplitude , x_amplitude-dx] )
plt.colorbar()
axis_font = {'size':'24'}
plt.text( -1, 5.2, '$W$',  **axis_font )
plt.xlim(-x_amplitude , x_amplitude-dx)
plt.ylim(-x_amplitude , x_amplitude-dx)
    
    Out[31]:
    
In [32]:
    
plt.figure(figsize=(10,10))
plt.plot( y, f_gpu.get()[32, :]/(f_gpu.get().size) /(43.8*f_gpu.get().size), 
         'o', label='numerical')
plt.plot( y, 4*Gaussian(y, sigma) , label = 'analytical')
plt.legend(loc='upper left')
#plt.ylim(0,1.1)
plt.ylabel('$e^{- \\frac{\\sigma x^2}{2} }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[32]:
    
In [33]:
    
# LOG PLOT 
plt.figure(figsize=(10,10))
plt.semilogy( y, f_gpu.get()[32, :]/(f_gpu.get().size) /(450*f_gpu.get().size)  , 'o', label='numerical')
plt.semilogy( y, f[32, :]  , '.', label='original')
plt.semilogy( y, Gaussian(y, sigma)/2.5 , label = 'analytical')
plt.legend(loc='upper left')
plt.ylim(1e-7,0.5)
plt.ylabel('$e^{- \\frac{x^2}{2 \\sigma^2 } }$',**axis_font)
plt.xlabel('$p$',**axis_font)
    
    Out[33]:
    
In [ ]: