Page MenuHomec4science

test_fftransform.py
No OneTemporary

File Metadata

Created
Thu, Jun 27, 02:43

test_fftransform.py

#!/usr/bin/env python
# coding: utf-8
# -----------------------------------------------------------------------------
# @author Lucas Frérot <lucas.frerot@epfl.ch>
#
# @section LICENSE
#
# Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
# Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
# Solides)
#
# Tamaas is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
import sys
import tamaas as tm
import numpy as np
from numpy.linalg import norm
def main():
size = 128
# Generate random rough surface
SG = tm.SurfaceGeneratorFilterFFT()
SG.getGridSize().assign(size)
SG.getHurst().assign(0.8)
SG.getRMS().assign(1.);
SG.getQ0().assign(4);
SG.getQ1().assign(4);
SG.getQ2().assign(32);
SG.getRandomSeed().assign(156);
SG.Init()
surface = SG.buildSurface()
# Compute fourier transform through numpy
numpy_spectral = np.fft.rfft2(surface)
# Compute fourier tranform through tamaas
tamaas_spectral = tm.SurfaceRealComplex(size, 1.)
transform = tm.FFTransformFFTWReal(surface, tamaas_spectral)
transform.forwardTransform()
spectral = tm.convertGrid(tamaas_spectral)
error_npy = np.abs(np.sum((spectral-numpy_spectral)**2))
surface_copy = surface.copy()
surface[:,:] = 0
transform.backwardTransform()
error_back = np.abs(np.sum((surface_copy-surface)**2))
print error_back
print error_npy
if error_back > 1e-16 or error_npy > 1e-16:
return 1
# Testing multi-component FFT
multi = np.zeros((size, size, 3))
multi_hermit = tm.GridHermitian2dReal()
multi_hermit.setNbComponents(3)
multi_hermit.resize(np.asarray((size, size/2+1), np.uint32))
transform = tm.FFTransformFFTWReal(multi, multi_hermit)
hermit = tm.convertGrid(multi_hermit)
norms = []
# Checking no overflow in other components of spectral representation
multi.fill(0.)
multi[:, :, 0] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (1,2)]))
multi.fill(0.)
multi[:, :, 1] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (0,2)]))
multi.fill(0.)
multi[:, :, 2] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (0,1)]))
for i in range(3):
if norms[i] != 0:
print "Error in multi-component data separation"
return 1
multi[:, :, i] = surface[:, :]
# Multi is full of surface
transform.forwardTransform()
for i in range(3):
error = norm(hermit[:, :, i] - numpy_spectral)
if error > 1e-10:
print "Error in multi-component spectral data : {} => {}".format(i, error)
return 1
return 0
if __name__ == '__main__':
sys.exit(main())

Event Timeline