def fft(data, nfft=None, in_place=False, use_old_plan=True, **kwargs_in): global _plan if USE_OLD_PLAN: use_old_plan = (USE_OLD_PLAN == 1) # print('USE OLD PLAN %s' % str(use_old_plan)) kwargs = dict(flags=['measure']) kwargs.update(kwargs_in) if nfft == None: nfft = len(data) if _plan is None or (use_old_plan and nfft != len(_plan.inarray)): use_old_plan = False if not use_old_plan: input_ = fftw3.create_aligned_array(nfft) if in_place: output = None else: output = fftw3.create_aligned_array(nfft) _plan = fftw3.Plan(input_, output, **kwargs) _plan.inarray[:len(data)] = data _plan.inarray[len(data):] = 0 _plan.outarray[:] = 0 _plan() if _plan.outarray is None: ret = _plan.inarray else: ret = _plan.outarray return ret
def ifft(data, nfft=None, in_place=False, use_old_plan=True, **kwargs_in): global _plan2 if USE_OLD_PLAN: use_old_plan = (USE_OLD_PLAN == 1) kwargs = dict(direction='backward', flags=['measure']) kwargs.update(kwargs_in) if nfft == None: nfft = len(data) if _plan2 is None or (use_old_plan and nfft != len(_plan2.inarray)): use_old_plan = False if not use_old_plan: input_ = fftw3.create_aligned_array(nfft) if in_place: output = None else: output = fftw3.create_aligned_array(nfft) _plan2 = fftw3.Plan(input_, output, **kwargs) _plan2.inarray[:len(data)] = data _plan2.inarray[len(data):] = 0 _plan2() if _plan2.outarray is None: ret = _plan2.inarray else: ret = _plan2.outarray return ret / nfft
def ifft(data, nfft=None, in_place=False, use_old_plan=True, **kwargs_in): global _plan2 if USE_OLD_PLAN: use_old_plan = (USE_OLD_PLAN == 1) kwargs = dict(direction='backward', flags=['measure']) kwargs.update(kwargs_in) if nfft == None: nfft = len(data) if _plan2 is None or(use_old_plan and nfft != len(_plan2.inarray)): use_old_plan = False if not use_old_plan: input_ = fftw3.create_aligned_array(nfft) if in_place: output = None else: output = fftw3.create_aligned_array(nfft) _plan2 = fftw3.Plan(input_, output, **kwargs) _plan2.inarray[:len(data)] = data _plan2.inarray[len(data):] = 0 _plan2() if _plan2.outarray is None: ret = _plan2.inarray else: ret = _plan2.outarray return ret / nfft
def init(self, n, measure, outn): inp = fftw3.create_aligned_array(n, dtype=complex) outp = fftw3.create_aligned_array(n, dtype=complex) plan = fftw3.Plan(inp, outp, direction='backward', flags=('measure' if measure else 'estimate', )) return (plan, None, lambda x: x / len(x))
def init(self, n, measure, outn): inp = fftw3.create_aligned_array(n, dtype=float) outp = fftw3.create_aligned_array(n // 2 + 1, dtype=complex) plan = fftw3.Plan(inp, outp, direction='forward', realtypes='halfcomplex r2c', flags=('measure' if measure else 'estimate', )) return (plan, None, None)
def init(self, n, measure, outn): inp = fftw3.create_aligned_array(n, dtype=complex) outp = fftw3.create_aligned_array(outn if outn is not None else (n - 1) // 2, dtype=float) plan = fftw3.Plan(inp, outp, direction='backward', realtypes='halfcomplex c2r', flags=('measure' if measure else 'estimate', )) return (plan, lambda x: x[:n], lambda x: x / len(x))
def __init__(self, nsx=10.0, nsy=10.0 ,nx=200): nsy /= 2 self.sigma = 4.0 * pi / sqrt(3.0) self.lx = self.sigma * nsx self.ly = self.sigma * nsy * sqrt(3.0) self.nx = nx self.ny = int(nx * self.ly / self.lx) vecx = linspace(0,self.lx,num=self.nx,endpoint=False) vecy = linspace(0,self.ly,num=self.ny,endpoint=False) self.X,self.Y = meshgrid(vecx, vecy) nthreads = 1 # flags = ['measure'] f = fftw3.fftw_flags self.y = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.yp = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y3 = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y3p = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y_to_yp = lib.fftw_plan_dft_2d(self.y.shape[0], self.y.shape[1], self.y, self.yp, -1, f['measure'] | f['preserve input']) self.yp_to_y = lib.fftw_plan_dft_2d(self.y.shape[0], self.y.shape[1], self.yp, self.y, 1, f['measure'] | f['preserve input']) self.y3_to_y3p = lib.fftw_plan_dft_2d(self.y3.shape[0], self.y3.shape[1], self.y3, self.y3p, -1, f['measure'] | f['preserve input']) self.y3p_to_y3 = lib.fftw_plan_dft_2d(self.y3.shape[0], self.y3.shape[1], self.y3p, self.y3, 1, f['measure'] | f['preserve input']) xlen, ylen = self.nx, self.ny kx = array([x for x in range(xlen//2+1)] + [x for x in range(-(xlen-xlen//2-1), 0)]) ky = array([x for x in range(ylen//2+1)] + [x for x in range(-(ylen-ylen//2-1), 0)]) self.lap = zeros_like(self.yp) lx, ly = self.lx, self.ly for iy in xrange(self.lap.shape[0]): for ix in xrange(self.lap.shape[1]): self.lap[iy,ix] = (-2*pi*1j*kx[ix]/lx)**2.0+(-2*pi*1j*ky[iy]/ly)**2.0 self.dop_partial = self.lap * (1 + self.lap)**2.0 self.dop = None
print(np.sum(a1)) t2 = time.time() for i in range(Ntest): b3 = anfft.fft(a0) a2 = anfft.ifft(b3) t2b = time.time() print(np.sum(a2)) t3 = time.time() # create a forward and backward fft plan a = fftw3.create_aligned_array(a0.shape, np.complex128, 16) b = fftw3.create_aligned_array(a0.shape, np.complex128, 16) c = fftw3.create_aligned_array(a0.shape, np.complex128, 16) a[:] = a0 ft3fft = fftw3.Plan(a, b, direction="forward", flags=["measure"]) ft3ifft = fftw3.Plan(b, c, direction="backward", flags=["measure"]) t4 = time.time() for i in range(Ntest): # perform a forward transformation ft3fft() # alternatively fft.execute() or fftw.execute(fft) # perform a backward transformation ft3ifft()
def init(self,n,measure,outn): inp = fftw3.create_aligned_array(n,dtype=complex) outp = fftw3.create_aligned_array(outn if outn is not None else (n-1)//2,dtype=float) plan = fftw3.Plan(inp,outp, direction='backward', realtypes='halfcomplex c2r', flags=('measure' if measure else 'estimate',)) return (plan,lambda x: x[:n],lambda x: x/len(x))
def init(self,n,measure,outn): inp = fftw3.create_aligned_array(n,dtype=complex) outp = fftw3.create_aligned_array(n,dtype=complex) plan = fftw3.Plan(inp,outp, direction='backward', flags=('measure' if measure else 'estimate',)) return (plan,None,lambda x: x/len(x))
def init(self,n,measure,outn): inp = fftw3.create_aligned_array(n,dtype=float) outp = fftw3.create_aligned_array(n//2+1,dtype=complex) plan = fftw3.Plan(inp,outp, direction='forward', realtypes='halfcomplex r2c',flags=('measure' if measure else 'estimate',)) return (plan,None,None)
print np.sum(a1) t2 = time.time() for i in range(Ntest): b3 = anfft.fft(a0) a2 = anfft.ifft(b3) t2b = time.time() print np.sum(a2) t3 = time.time() # create a forward and backward fft plan a = fftw3.create_aligned_array(a0.shape, np.complex128, 16) b = fftw3.create_aligned_array(a0.shape, np.complex128, 16) c = fftw3.create_aligned_array(a0.shape, np.complex128, 16) a[:] = a0 ft3fft = fftw3.Plan(a, b, direction='forward', flags=['measure']) ft3ifft = fftw3.Plan(b, c, direction='backward', flags=['measure']) t4 = time.time() for i in range(Ntest): #perform a forward transformation ft3fft() # alternatively fft.execute() or fftw.execute(fft) #perform a backward transformation ft3ifft()
def fftw_array(self, divs): return fftw3.create_aligned_array(divs, dtype=typeDict['complex'])
def __init__(self,r=-0.2, psi0=-0.5, nsx=10.0, nsy=10.0 ,nx=200, init = 'hex'): self.frame_counter = 0 nsy /= 2 self.sigma = 4.0 * pi / sqrt(3.0) self.r,self.psi0 = float(r),float(psi0) self.lx = (2.0 * pi * nsx) if (init == 'stripe') else (self.sigma * nsx) self.ly = self.sigma * nsy * sqrt(3.0) self.nx = nx self.ny = int(nx * self.ly / self.lx) vecx = linspace(0,self.lx,num=self.nx,endpoint=False) self.xvec = vecx vecy = linspace(0,self.ly,num=self.ny,endpoint=False) self.X,self.Y = meshgrid(vecx, vecy) nthreads = 1 # flags = ['measure'] f = fftw3.fftw_flags self.y = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.yp = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y3 = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y3p = fftw3.create_aligned_array((self.ny, self.nx), dtype=typeDict['complex']) self.y_to_yp = lib.fftw_plan_dft_2d(self.y.shape[0], self.y.shape[1], self.y, self.yp, -1, f['measure'] | f['preserve input']) self.yp_to_y = lib.fftw_plan_dft_2d(self.y.shape[0], self.y.shape[1], self.yp, self.y, 1, f['measure'] | f['preserve input']) self.y3_to_y3p = lib.fftw_plan_dft_2d(self.y3.shape[0], self.y3.shape[1], self.y3, self.y3p, -1, f['measure'] | f['preserve input']) self.y3p_to_y3 = lib.fftw_plan_dft_2d(self.y3.shape[0], self.y3.shape[1], self.y3p, self.y3, 1, f['measure'] | f['preserve input']) sqrt3p2 = sqrt(3.0) / 2.0 # init if init == 'ran': self.y[:] = self.psi0 + 0.001 * (rand(self.ny, self.nx)-0.5)[:] elif init == 'hex': self.y[:] = self.psi0 + (2.0 * cos(self.X[:] * sqrt3p2) * cos(self.Y[:] / 2.0) + cos(self.Y[:])) / 3.0 elif init == 'hom': self.y[:] = self.psi0 elif init == 'stripe': self.y[:] = self.psi0 + 0.5 * cos(self.X[:]) elif init == 'stripe-exp': self.y[:] = self.psi0 + cos(self.X[:]) * sqrt(2.0 * (- self.r - 3.0 * self.psi0 ** 2.0) / 3.0) * exp(-0.0005 * (self.X[:] - self.lx / 2.0)**2.0) else: raise ValueError('not a proper initial condition') # envelope = 0.5 * (tanh(- 0.2 * (self.X - self.l / 2.0 - self.l / 15.0)) + 1.0) * 0.5 * (tanh(0.2 * (self.X - self.l / 2.0 + self.l / 15.0)) + 1.0) # self.y[:] = self.psi0 * (1.0 - envelope[:]) + envelope[:] * self.y[:] # for ix in xrange(self.y.shape[0]): # for iy in xrange(self.y.shape[1]): # if (ix-self.n/2)**2 + (iy-self.n/2)**2 > (self.n/8)**2: # self.y[ix,iy] = self.psi0 xlen, ylen = self.nx, self.ny kx = array([x for x in range(xlen//2+1)] + [x for x in range(-(xlen-xlen//2-1), 0)]) ky = array([x for x in range(ylen//2+1)] + [x for x in range(-(ylen-ylen//2-1), 0)]) self.lap = zeros_like(self.yp) lx, ly = self.lx, self.ly for iy in xrange(self.lap.shape[0]): for ix in xrange(self.lap.shape[1]): self.lap[iy,ix] = (-2*pi*1j*kx[ix]/lx)**2.0+(-2*pi*1j*ky[iy]/ly)**2.0 self.dop = self.lap * (self.r + (1 + self.lap)**2.0) lib.fftw_execute(self.y_to_yp) #self.x = linspace(0, self.l, self.n, endpoint=False) #self.y = sin(2*2*pi/self.l*self.x)+sin(5*2*pi/self.l*self.x)+sin(3*2*pi/self.l*self.x) # tmp = rfft2(self.y) # mask = zeros_like(tmp) # mask[0:mask.shape[0]/2, 0:mask.shape[0]/2] = 1.0 # self.y = irfft(tmp * mask) #self.y = self.psi0 + zeros_like(self.x) # self.y = self.psi0 + zeros(self.n)#+ 0.000001 * randn(self.n) # self.y = self.psi0+0.01*sin(2*pi*self.x) # self.y = self.psi0+0.001*cos(2.0*pi*self.x)*exp(-2.0*(self.x-self.l/2.0)**2.0) # self.kmask=ones_like(self.yp) # self.kmask[:,:20]=0 # self.kmask[:,-20:]=0 # self.kmask[:20,:]=0 # self.kmask[-20:,:]=0 # self.kmask = array([[exp(-0.08 * (k_x**2.0 + k_y**2.0)) for k_x in kx] for k_y in ky]) self.kmask = array([[exp(-0.05 * (k_x**2.0 + (k_y ** 2.0 * self.lx / self.ly))) for k_x in kx] for k_y in ky]) self.saved_curves = []